├── ftdi_pinout.png ├── esp8266_pinout.png ├── esp8266test.py ├── esp8266server.py └── README.md /ftdi_pinout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shivasiddharth/pyesp8266/master/ftdi_pinout.png -------------------------------------------------------------------------------- /esp8266_pinout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shivasiddharth/pyesp8266/master/esp8266_pinout.png -------------------------------------------------------------------------------- /esp8266test.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: iso-8859-15 -*- 3 | 4 | import logging 5 | import sys, serial 6 | from time import * 7 | import datetime, string 8 | 9 | def enum(**enums): 10 | return type('Enum', (), enums) 11 | 12 | Status = enum(ERR='ERROR', OK=['OK', 'ready', 'no change'], BUSY='busy') 13 | logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) 14 | 15 | # modified code from: http://www.instructables.com/id/Easy-ESP8266-WiFi-Debugging-with-Python/step2/Software/ 16 | def send_cmd( sCmd, waitTm=1, retry=5 ): 17 | lp = 0 18 | ret = "" 19 | 20 | logging.info( "Sending command: %s" % sCmd ) 21 | 22 | for i in range(retry): 23 | ser.flushInput() 24 | ser.write( sCmd + "\r\n" ) 25 | ret = ser.readline() # Eat echo of command. 26 | sleep( 0.2 ) 27 | while( lp < waitTm or 'busy' in ret): 28 | while( ser.inWaiting() ): 29 | ret = ser.readline().strip( "\r\n" ) 30 | logging.debug( ret ) 31 | lp = 0 32 | if( ret in Status.OK ): break 33 | #if( ret == 'ready' ): break 34 | if( ret == Status.ERR ): break 35 | sleep( 1 ) 36 | lp += 1 37 | 38 | sleep(1) 39 | if( ret in Status.OK ): break 40 | 41 | logging.info( "Command result: %s" % ret ) 42 | return ret 43 | 44 | if len(sys.argv) != 5: 45 | print "Usage: esp8266test.py port baud_rate ssid password" 46 | sys.exit() 47 | 48 | port = sys.argv[1] 49 | #Baud rate should be: 9600 or 115200 50 | speed = sys.argv[2] 51 | ssid = sys.argv[3] 52 | pwd = sys.argv[4] 53 | 54 | ser = serial.Serial(port,speed) 55 | if ser.isOpen(): 56 | ser.close() 57 | ser.open() 58 | ser.isOpen() 59 | 60 | send_cmd( "AT" ) 61 | # send_cmd( "AT+RST", 5 ) # NOTE: seems to cause problems that require manually reset (pulling down the RST pin) 62 | # sleep(3) 63 | send_cmd( "AT+CWMODE=1" ) # set device mode (1=client, 2=AP, 3=both) 64 | #The mode will be changed on Olimex MOD-WIFI-ESP8266-DEV only after a reset 65 | #The command below will reset the device 66 | send_cmd( "AT+RST"); 67 | send_cmd( "AT+CWLAP", 10) # scan for WiFi hotspots 68 | send_cmd( "AT+CWJAP=\""+ssid+"\",\""+pwd+"\"", 5 ) # connect 69 | addr = send_cmd( "AT+CIFSR", 5) # check IP address 70 | 71 | ser.close() 72 | -------------------------------------------------------------------------------- /esp8266server.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: iso-8859-15 -*- 3 | 4 | import logging 5 | import sys, serial 6 | from time import * 7 | import datetime, string 8 | 9 | def enum(**enums): 10 | return type('Enum', (), enums) 11 | 12 | Status = enum(ERR=['ERROR', 'Fail'], OK=['OK', 'ready', 'no change', 'SEND OK'], BUSY='busy', LINK='Link') 13 | 14 | def send_cmd( sCmd, waitTm=1, retry=5, delay=1): 15 | lp = 0 16 | ret = "" 17 | 18 | print( "Sending command: %s" % sCmd ) 19 | 20 | for i in range(retry): 21 | ser.flushInput() 22 | ser.write( sCmd + "\r\n" ) 23 | ret = ser.readline() # Eat echo of command. 24 | sleep( 0.2 ) 25 | while( lp < waitTm or 'busy' in ret): 26 | while( ser.inWaiting() ): 27 | ret = ser.readline().strip( "\r\n" ) 28 | print( ret ) 29 | lp = 0 30 | if( ret in Status.OK ): break 31 | #if( ret == 'ready' ): break 32 | if( ret in Status.ERR ): break 33 | sleep( delay ) 34 | lp += 1 35 | 36 | sleep(delay) 37 | if( ret in Status.OK ): break 38 | 39 | print( "Command result: %s" % ret ) 40 | return ret 41 | 42 | def send_response(response, cid='0'): 43 | # no need to wait for response between these commands (or retry), so just send them out 44 | # ser.flushInput() 45 | ser.write( "AT+CIPSEND=" + cid + "," + str(len(response)) + "\r\n") 46 | sleep(0.3) 47 | ser.write( response + "\r\n" ) 48 | sleep(0.3) 49 | 50 | send_res = False 51 | for i in range(100): 52 | while( ser.inWaiting() ): 53 | ret = ser.readline().strip( "\r\n" ) 54 | # print ret 55 | if( ret == Status.OK[3] ): 56 | # print "send ok!" 57 | send_res = True 58 | if send_res: break 59 | sleep(0.1) 60 | 61 | sleep(0.3) 62 | ser.write( "AT+CIPCLOSE=" + cid + "\r\n" ) 63 | sleep(0.3) 64 | 65 | 66 | def process_request(response): 67 | has_link = False 68 | cid = '0' 69 | while( ser.inWaiting() ): 70 | ret = ser.readline().strip( "\r\n" ) 71 | # print ret 72 | if (ret in Status.LINK): 73 | has_link = True 74 | ipd_str = '+IPD,' 75 | if (ipd_str in ret): 76 | cid = ret[ret.find(ipd_str) + len(ipd_str)] 77 | 78 | if has_link: 79 | # process response 80 | send_response(response, cid) 81 | 82 | if len(sys.argv) != 5: 83 | print "Usage: esp8266test.py port baud_rate ssid password" 84 | sys.exit() 85 | 86 | port = sys.argv[1] 87 | #Baud rate should be: 9600 or 115200 88 | speed = sys.argv[2] 89 | ssid = sys.argv[3] 90 | pwd = sys.argv[4] 91 | p = 80 92 | 93 | ser = serial.Serial(port,speed) 94 | if ser.isOpen(): 95 | ser.close() 96 | ser.open() 97 | ser.isOpen() 98 | 99 | send_cmd( "AT" ) 100 | send_cmd( "AT+CWMODE=1" ) # set device mode (1=client, 2=AP, 3=both) 101 | send_cmd( "AT+CWLAP", 30) # scan for WiFi hotspots 102 | send_cmd( "AT+CWJAP=\""+ssid+"\",\""+pwd+"\"", 5 ) # connect 103 | send_cmd( "AT+CIFSR", 5) # check IP address 104 | 105 | send_cmd( "AT+CIPMUX=1" ) # multiple connection mode 106 | send_cmd("AT+CIPSERVER=1," + str(p)) 107 | 108 | # process requests 109 | while (1): 110 | process_request("GOT IT! (" + str(datetime.datetime.now()) + ")") 111 | sleep(0.3) 112 | 113 | ser.close() 114 | 115 | 116 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ESP8266 Serial-to-WiFi Module 2 | ========= 3 | 4 | The ESP8266 is a low cost Serial-to-WiFi module that interfaces nicely to any microcontroller. However, a word of caution -- it is highly undocumented (primary reason for writing this document), and more importantly, it is frequently updated and not backward compatible. A good example is how newer versions use 9600 baud rate, while older versions (by old I'm referring to 2-3 months old modules) used 57600-115200 baud rates. 5 | 6 | In general, the tutorial below will get you started. Once you are set up, you should learn more about the module's protocol here: https://nurdspace.nl/ESP8266#AT_Commands 7 | 8 | ## Usage 9 | 10 | First, it is important to understand how the board works. The ESP8266 has a full TCP/UDP stack support. It can also be easily configured as a web server. The module accepts commands via a simple serial interface. It then responds back with the operation's outcome (assuming everything is running correctly). Also, once the device is connected and is set to accept connections, it will send unsolicited messages whenever a new connection or a new request is issued. 11 | 12 | ### Testing the module via FTDI (or a USB-to-Serial cable) 13 | 14 | Before connecting the module to a microcontroller, it's important to try it directly via a serial interface. An easy solution is to use a 3V3 FTDI cable. Note that the module is not designed for more than 3.6V, so a 3.3V power supply should be used - both for power and logic. The current batch of the FTDI cables deliver 5V in the supply rail even for the 3V3 version. Apparently, this is an error made by the manufacturer, so it might be corrected at some point. 15 | 16 | #### Hardware setup 17 | 18 | ![alt text](https://raw.githubusercontent.com/guyz/pyesp8266/master/esp8266_pinout.png "ESP8266 Pinout") ![alt text](https://raw.githubusercontent.com/guyz/pyesp8266/master/ftdi_pinout.png "FTDI Pinout") 19 | 20 | 1. Connect the RX/TX pins in a 3v3 FTDI cable to the TX/RX pins in the ESP module. 21 | 2. Connect a 3v3 power supply to the VCC/GND pins. Note that it is possible to use an Arduino 3v3 supply for this. 22 | 3. Connect the CH_PID pin to VCC as well. 23 | 4. The rest of the pins should be floating. However, be prepared to occasionally ground the RST pin. This would help if the board is stuck on some command. 24 | 25 | ### Software and testing: 26 | 1. Clone this repository. 27 | 2. Unzip and execute the following command: 28 | ```python esp8266test.py ``` 29 | 3. You should see a bunch of commands going through, including a list of available APs. Eventually, you should see the IP address assigned to the module printed. 30 | 4. ping the IP address obtained in (3). If that works, then you got it working and can start doing cool stuff! 31 | 32 | ### Webserver example: 33 | Once you've successfully set up the device and confirmed that it can connect to a WiFi, you can try something more elaborate. the file esp8266server.py will go through the same flow as the test module, but it will also continue to setting up the device to accepting multiple connections: 34 | 35 | ```python 36 | send_cmd( "AT+CIPMUX=1" ) # multiple connection mode 37 | send_cmd( "AT+CIPSERVER=1,80" ) 38 | ``` 39 | 40 | To run this module, issue the following command in the right directory: 41 | ``` 42 | python esp8266server.py 43 | ``` 44 | 45 | Then, head to your browser and enter the following URL: 46 | ``` 47 | http:// 48 | ``` 49 | 50 | If everything works correctly, you should see the serial request being pushed through the serial interface. The python code would in turn serve a response back which will show on the browser. If you want to fiddle around with the response, look at this line in the code: 51 | 52 | ```python 53 | process_request("GOT IT! (" + str(datetime.datetime.now()) + ")") 54 | ``` 55 | 56 | ### Troubleshooting: 57 | 58 | If you encountered issues, check the following: 59 | 1. Is the red LED on the module lit? If it isn't, the board isn't getting power. 60 | 2. When trying to issue commands, do you see the blue LED on the module blinking? If not, check the RX/TX connections. If the LED is constantly lit, then one of the connections is wrong - probably RX/TX or one of the other pins. 61 | 3. Are you seeing gibberish? You're probably doing well, but try a different BAUD rate. 62 | 63 | If you receive the error `ImportError: No module named serial` 64 | Run `sudo pip install pyserial` 65 | 66 | ### Testing the module via a microcontroller 67 | 68 | Once you've got the module up and running with a direct serial connection, you can move on to plugging it into a microcontroller board. The steps are largely the same - you connect the RX/TX pins to the TX/RX pins of a microcontroller, or - if you're using an Arduino like board - you can set up a SoftwareSerial interface and use any 2 digital pins for the communication. 69 | 70 | As to powering up the device - while I have briefly tried and confirmed that a direct 5V supply could work, it is not recommended and your mileage may vary. There are multiple ways to regulate the voltage (e.g., http://rayshobby.net/?p=9734), or if you're at the CBA lab - try using one of the larger 3.3V SOT223 voltage regulators, or a similar capable one. 71 | 72 | If you're using the Arduino IDE, you should check out these resources for some reference code: 73 | 74 | 1. http://rayshobby.net/?p=9734 75 | 2. https://github.com/INEXTH/Arduino-ESP8266_libs 76 | 77 | The result should look something like this: 78 | 79 | << vid arduino >> 80 | --------------------------------------------------------------------------------