Easy ESP8266 WiFi Debugging With Python

download Easy ESP8266 WiFi Debugging With Python

of 5

description

Easy ESP8266 WiFi Debugging With Python

Transcript of Easy ESP8266 WiFi Debugging With Python

  • http://www.instructables.com/id/Easy-ESP8266-WiFi-Debugging-with-Python/

    Food Living Outside Play Technology Workshop

    Easy ESP8266 WiFi Debugging with Pythonby jimk3038 on November 4, 2014

    Table of Contents

    Easy ESP8266 WiFi Debugging with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

    Intro: Easy ESP8266 WiFi Debugging with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

    Step 1: Hardware Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

    Step 2: Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

    File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

    Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

    Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

    Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

  • http://www.instructables.com/id/Easy-ESP8266-WiFi-Debugging-with-Python/

    Author:jimk3038 www.ph-elec.comFounder of Powerhouse Electronics.For more info goto: www.ph-elec.com

    Intro: Easy ESP8266 WiFi Debugging with PythonThe new sub $5 dollar ESP8266 WiFi module makes inexpensive IoT way more possible and easy for mere mortals like me. In fact, I just did a search on AliExpress.comand I found the same module for $3.25 (free shipping, but of course) in quantities of one. How I can have one of these modules shipped from the other side of the earthright to my door for $3.25 is still a mystery to me. Anyway...In a nutshell, the ESP8266 module is a super inexpensive (note, I did not say cheap because they seem really good) WiFi module for embedded apps. It appears themodule has an ARM micro that is re-programmable and there are a lot of folks gaga over that.

    For me, I just want a little IoT board that can chirp out some data onto my WiFi network now and again. You know, like when the mailman drops off the mail. TheESP8266 right out of the box has a set of TTL Rx / Tx pins that communicate at a default 115k serial baud rate. Note, early modules came set with a default of 57.6kbaud rate. So, the default module communicates at 115k using an old modem style super simple AT command protocol. Basically, commands (all start with "AT" hencethe name) are sent to the module using something like "AT+RST" (commands the module to reset) and the module responds - usually responding to each command witha "OK".

    But the devil is in the details.

    Before there is any hope of embedding a ESP8266 module in a mailbox detector IoT device the AT commands between the ESP8266 and the controlling micro reallymust be understood fully. Therefore, to make it easier to test and debug controlling the ESP8266 module this Instructable will show how to hook up a new ESP8266 to aPC and test / control it using some simple Python code.

    Image Notes1. 5V LED comes on whenever the USB cable is jacked in.2. ESP8266 Red LED comes on when the Mic regulator is enabled.3. MSOP parts are hard to work with. This is really ugly but it works. I should add a wad of epoxy over the tiny wires before they get broken.4. The copper PCB acts like a heat-sink for the regulator.

    Step 1: Hardware SetupThere are two problems that make hooking up the ESP8266 directly to PC difficult. First, the module uses 3.3Vdc TTL Rx and Tx lines to communicate. Second, themodule's datasheet indicates the module will need up to 300mA of 3.3Vdc supply voltage.

    The easy solution to the first problem is to simply use a FTDI 3.3V USB serial cable. I've had a couple of these USB-to-Serial cables for years. Looks like Adafruit hasthese in stock for around $20 bucks. There are some other types around but the FTDI is the gold standard. Only problem with the FTDI converter is that the Vcc outputfrom the cable is 5Vdc and not the needed 3.3Vdc. Seems strange given the TTL signals on the FTDI cable are at 3.3Vdc - guess they got cheap and dropped putting astep down regulator in the converter.

    So, the second problem is to down convert the FTDI 5Vdc output to 3.3Vdc @ 300mA. My solution was to use a little regulator I had in my parts bin. Specifically, I used aMic5239 which can be found at Mouser. Since I had ten of these in my parts bin I decided to sacrifice one for the greater good. I solder the regulator down to a little hunkof PCB and hot glued the whole mess to a DIP socket. In the first picture in this Instructable the regulator is visible on the right-hand edge of the breadboard. It's ugly butseems to work well. Would have been much easier if I had originaly order the regulator in a larger SOIC package instead of the tiny MSOP-8 package. Rule to self - nomore super tiny parts.

    As a plus, in the 8 pin package of the Mic5239 regulator there is an enable pin. The enable pin must be pulled high for the 3.3Vdc output to come on. So, as a trick, I tiedthe regulator enable pin to the FTDI RTS output pin. Therefore, using Python, power to the ESP8266 can be turned on and off! Turns out, even though I did NOT turn onhardware flow control in the Python code the Python serial driver (called PySerial) still allows control over the RTS output pin.

  • http://www.instructables.com/id/Easy-ESP8266-WiFi-Debugging-with-Python/

    Image Notes1. Pin #12. ESP8266 WiFi Module

    Step 2: SoftwareThere are three bits of Python code for this Instructable.

    1) "tcp_server.py" -> This Python code provides a TCP server that the ESP8266 can connect to. Simply edit the code so that the IP number in the source code matchesthe IP number of the machine your running this on. The server simply allows a client to make a TCP connection, accepts some data, and sends back some junk data.That's it. The server just starts up and then waits in an endless loop for clients to connect.2) "tcp_client.py" -> This Python code provides a way to test "tcp_server.py" is working correctly. So, with the server code running on one machine, run "tcp_client.py" onanother computer on your network. Run the client as "tcp_client.py Some data to send goes here." The client app sends whatever is given on the command line. In thiscase, the client would send "Some data goes here." to the server. Both the server and the client will show some output that indicates a successful transfer.

    3) Finally, "Test_ESP8266.py" can now be used to test for a TCP connection to the Python server. If all goes well, the server and "Test_ESP8266.py" will show someoutput that indicates a successful transfer. Once again, before running "Test_ESP8266.py" edit the code so that the IP numbers match. Run as "Test_ESP8266.pyMySSID MyPasscode" - the SSID and passcode must match your WiFi settings.

    Using the Python Test_ESP8266 code I learned a couple things about the module. After getting everything working and verified with the server I simply tried using a badpasscode, as a test to see what would happen. Everything appeared normal until the actual command to form the TCP connection. The command that failed was"AT+CIPSTART='TCP','192.168.1.130',9999". However, I would have expected the command "AT+CWJAP?" to fail first. The "AT+CWJAP?" command returns the SSIDand RSSI of the AP the ESP8266 is connected to. With a bad passcode it should not have been able to connect.

    Goofing with the code I think I've discovered a trick. After connecting to the AP, ask for the ESP8266 IP number. Even with a good passcode it may return "ERROR". Noworries, just dwell a couple seconds and ask again. With a good SSID and passcode the ESP8266 will eventually return the IP number. After a good IP number isreturned the TCP connection command always seems to work.

    Another trick, if a bad passcode is used the ESP8266 module gets stuck! Regardless of the command sent the module returns "buzy now ...". Even the "AT+RST" (reset)command is ignored. The work around is to use the regulator to cycle power to the module. Using the RTS pin on the FTDI USB-to-Serial converter - which connects tothe regulator enable pin - the regulator 3.3Vdc output can be switched off. An off time of 10 seconds seems to work well.

    Well, that's is. Hope this helps,

    Jim

    BTW: For the latest and greatest updates please visit my web site: www.ph-elec.com

  • http://www.instructables.com/id/Easy-ESP8266-WiFi-Debugging-with-Python/

    File Downloads

    tcp_client.py (485 bytes)[NOTE: When saving, if you see .tmp as the file ext, rename it to 'tcp_client.py']

    tcp_server.py (1 KB)[NOTE: When saving, if you see .tmp as the file ext, rename it to 'tcp_server.py']

    Test_ESP8266.py (3 KB)[NOTE: When saving, if you see .tmp as the file ext, rename it to 'Test_ESP8266.py']

    Related Instructables

    Use ESP8266 toInternet enabledAC Appliancesby shinteo

    ESP8266 WiFitemperature andhumidity sensorby EasyIoT

    ESP8266 WiFirelay switch byEasyIoT

    ESP8266 WiFIwater leaksensor byEasyIoT

    A newbie'sguide to setupESP8266 withArduino Mega2560 or Uno byshinteo

    An inexpensiveIoT enablerusing ESP8266bygopinath.marappan

    Advertisements

    Comments7 comments Add Comment

    doctek says: Nov 18, 2014. 7:45 PM REPLYYour website is AWESOME! Thanks for sharing all your projects.

    doctek says: Nov 18, 2014. 8:06 PM REPLYI'd really like to see more and comment on your site. So I registered, but after half an hour or so, I still haven't gotten an email with a password. How longshould I expect to wait??

    Akin Yildiz says: Nov 10, 2014. 10:06 AM REPLYhello everyone, can anybody do a simpler project with this? I just want to collect data using arduino Analog0 input with an LDR photoresistor. collect the dataand simply post it on Thingspeak ?!?!

    .

    all the examples I find are very complicated and include libraries. I need my project to be much simpler. just read some data from the analog sensor anddisplay the value on the cloud..

    can anybody please help me with this? I have updated the firmware on my ESP8266 and I can connect to my house network using the CoolTerm software..

    any help is highly appreciated !!!thank you,akin

    jimk3038 says: Nov 10, 2014. 5:41 PM REPLYHave a look at the following TI CC3200 Launchpad board:

    http://www.ti.com/tool/cc3200-launchxl

    This board cost $30 bucks and has built in WiFi right in the TI micro. The best part is, the board works with Energia open-source programming softwarewhich is kind of a drop in duplicate of Arduino.

  • http://www.instructables.com/id/Easy-ESP8266-WiFi-Debugging-with-Python/

    The latest Energia software has a bunch of examples just for the CC3200 board. One of the examples measured the internal temperature sensor that isinternal to the micro. After the measurement is made the WiFi connection is used to upload the data to a cloud service. The cloud service charts andplots the data to either a web page or your smart phone real-time!

    Truly amazing to be able to take a new board and have it connected to the cloud in about 10 minutes. Most of that time consumed with downloading andinstalling Energia. Running the demo only takes 30 seconds.

    I was really impressed - and no, I have no ties to TI,Jim

    Akin Yildiz says: Nov 10, 2014. 7:32 PM REPLYhello Jim, thank you for your reply. I checked out the board, it seems really nice and would love to work with one. although size is critical for myproject, an ESP8266 is much smaller compared to the TI. also the price, for $30 we can get 6x ESP wifi modules..

    I have gotten some help on my question, you should check it out; http://www.instructables.com/answers/ESP8266-wifi-...

    I am still not able to upload data, but seems like everything is correct..

    Silent_Shadow says: Nov 4, 2014. 7:58 PM REPLYStep three was quite funny in my opinion...

    jimk3038 says: Nov 5, 2014. 5:58 PM REPLYOK, I'm over ranting about loosing my work to bugs. Step three now has real comments.