Wednesday, March 2, 2016

IOT-ing with ESP8266

Update on May 26, 2016...yet another surprise from this board. After flashing and for programming the board I observed TX on USB converter has to be connected to TX on ESP board, same with RX pin. Yes you typically see you have to pair TX with RX and viceversa but not with this board.

I always find myself seeing that nothing will work the first time I try... and playing with ESP8266 modules was not going to be the exception. This post will therefore cover the eventual right steps I took towards making these modules work as I haven't seen any other blog out there covering these specifically. As you can see it is an ESP8266-12 development board that comes with a battery holder. (Details of this device below in the last section).

The failures :(

Before purchasing the above module there were some failed attempts included ESP8266-1, ESP8266-3 and ESP8266-12. The "beauty" of these modules and I guess with hardware-ish projects is that the chance of failure is now increased by physical factors: bad soldering, overheating, wrong connections, faulty power source, manufacturing error ... yeah I think I have been bitten (and maybe learnt) from all those. I leave a picture here of two defunct modules, still they can serve as an example/reference of how they can be assembled. I overheated the ESP8266-12 but still I feel proud I was able to solder that many jumper wires somewhat nicely (they are male to male plugged to that small white breadboard).

The success :)

In this section I will detail the steps I took and config changes I made to start using the module, for a list of the tools I used/purchased have a look at the section below.
Note this module comes with a pre-installed somewhat shady (I would say) firmware that requires a mobile app to work. In some pages they direct you to an APK that requires an excessive number of permissions to run, I would be wary to install that APK on your phone. Therefore the first step I took was re-flashing, note I am using Linux for flashing and Windows for programming.

  • Connect the wiring and jumper as follows:
    • 1: Ground from the USB to serial converter to GPIO0 (zero) of the ESP module, yes this is required in addition to step 4.
    • 2: Make sure the USB to serial converter is set up to work on 3.3volts.
    • 3: Double check that RX on the USB to serial converter is connected to RX on the ESP module, and that TX on the converter is connected to TX on the ESP. (Yes not the typical way of pairing TX and RX pins).
    • 4: The jumper that comes with the ESP board should be plugged for flashing.
    • 5: sorry dummy number :)
    • 6: this will be used later, leave it disconnected for now.
    • *Instead of preferred normal AA batteries I used rechargeable batteries, I measured the output from the voltage regulator and it was providing the required 3.3 volts to the development board.
  • Obtain a fresh NodeMCU image to flash, I wanted this better than the AT commands based ones. I left the default basic modules proposed in the web site.
  • The exact image I flashed can be found here if you need. Disclaimer I don't work for any 3 letter agency.
  • After the layout is ready and the USB to serial converter is connected to your computer it is time to proceed to flashing.
  • The windows flashers tools didn't work for me so I used a Kali Linux VMware Player virtual machine to run the script, make sure you enable the USB to Serial converter on VMware. 
  • Not surprisingly esptool didn' work for me out of the box either, I was getting errors such as
warning: espcomm_send_command(FLASH_DOWNLOAD_DATA) failed
warning: espcomm_send_command: didn't receive command response 
  • However by modifying its source so that It would flash with smaller block sizes it worked. Check the code contains the following:
    ESP_RAM_BLOCK   = 0x1800
    ESP_FLASH_BLOCK = 0x4 
  • To do a preamble check that esptool works attempt to execute the read_mac command. To do so first restart the module (i typically do by separating a battery away from the coil in the battery holder and attaching it again) and execute the command highlighted below.
python read_mac
Connecting...MAC: 18:fe:34:zz:yy:xx
  • If esptool manages to return the configured mac of your ESP module it means it communicated well with the device and you are ready to go with the flashing command as follows:
python write_flash 0x00000 nodemcu-master-7-modules-2016-03-01-21-25-51-integer.bin
Erasing flash...
Took 2.10s to erase flash block
Wrote 397520 bytes at 0x00000000 in 574.7 seconds (5.5 kbit/s)...
  • It will take sometime (because of the source code changes we made) however it should work.
  • Now it is time to test it! Turn off the ESP module and detach the USB to serial converter from VMware so that it is visible by your windows native system (if you are using windows, if Linux/Mac you can do this by using minicom or other terminals).
  • With the ESP module turned off, make the following changes to the physical wiring/connections to the device:
    • Remove that jumper marked with number 4 in the picture above
    • Connect the pin marked as 6 to ground. In other words the wire that was previously connected to GPIO0 (zero) now should be connected where number 6 points.
    • Verify that TX from USB converter is connected to TX on ESP module, do the same with the RX connectors. Yes this is not the typical way to connect it, but once again this board got me with this.
  • Once all the arrangements are done power on the ESP module.
  • Verify in which COM port the USB to serial converter is connected to, go to the windows control panel/device manager (use the shortcut Windows Key + Pause to get faster to control panel) and observe the Ports (COM & LPT) section. 
  • Download the ESPlorer tool from here and run the bat file.
  • Once inside ESPlorer select the COM port, COM4 in my case, specify a 9600 baud speed and click on the "open" button. You should see something like the following and be able to write your first print("hello world") command.

  • Note: if you don't connect the ground wire the ESP8266 module will ignore any command you provide to it... yeah this was the last thing bothering me until I got it all working.
That's it, now you can go ahead and code whatever you need on it. Hopefully this guide was useful for you, next posts I will try to cover some cool stuff you can do with modules like these.

The gear I used for success

The gear I used and what/where I purchased:

  • ESP8266-12 development board with battery holder. Link

  • USB to serial converter (FTDI FT232RL USB to TTL Serial Converter Adapter Module 5V + 3.3V) Link

  • Female to female (lesbian?) jumper wire cables. Link