Skip to content

Instantly share code, notes, and snippets.

@gerhc
Created December 16, 2015 14:59
Show Gist options
  • Save gerhc/1583b76911fe418a07d6 to your computer and use it in GitHub Desktop.
Save gerhc/1583b76911fe418a07d6 to your computer and use it in GitHub Desktop.
a piece of code found at https://www.microchip.com/forums/m758342.aspx that shows how to connect to an AP using python and RN171
import serial
import os
import sys
import time
chunk_size = 5000
os.chdir("D:\govind\Python\RN-wifly")
print "Starting WiFly Comm"
# To change baudrate -> set uart baudrate 115200, then save <CR>, then reboot
print "Opening Port"
ser = serial.Serial(11, 9600, timeout=3,parity=serial.PARITY_NONE)
print ser.portstr # check which port was really used
print "OpenResult ="
print ser.isOpen()
ser
#if(!ser.isOpen())
# Quit
# set uart baudrate 115200
# Enter Command mode
ser.write("$$$")
line = ser.readlines() # read a '\n' terminated line
print "***"
print line
if("CMD\r\n" in line):
print "Commmand mode Entered"
ser.write("set uart mode 1\r\n") # disable echo
line = ser.readlines() # read a '\n' terminated line
print line
ser.write("set comm remote 0\r\n") # Disable the remote message
line = ser.readlines() # read a '\n' terminated line
print line
ser.write("set wlan join 0\r\n") # Disable auto associate
line = ser.readlines() # read a '\n' terminated line
print line
ser.write("get e\r\n") # Get the settings
line = ser.readlines()
print line
# Set WiFi Parameters
ser.flushInput()
ser.write("set wlan pass your_AP_passwordr\n")
line = ser.readlines()
print line
if("AOK\r\n" in line):
print "Password Set"
wifi = 0;
while(wifi == 0):
ser.write("scan\r\n")
line = ser.readlines()
print line
ser.flushInput()
ser.write("join your_AP_name\r\n")
line = ser.readlines()
print line
if("Associated!\r\n" in line):
print "Joined AP"
wifi = 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment