Previous posts:
EasyConfigure - Modular/Configurable System Intro
EasyConfigure - Hardware Components
EasyConfigure - BASH scripting
EasyConfigure - MQTT test setup
ESP8266
I recently received a couple of ESP8266 WiFi modules that I considered using for remote sensor data transmission via MQTT protocol.
Initial plan was to access this module from Arduino Micro (interfaced via UART) but as I was getting more familiar with these modules I learned that they can be programmed with custom firmware that could do all the work. But that's not all, there are some very interesting community projects that allow scripting instead of making a custom firmware — scripts can perform specific tasks, not firmware (by itself). This allows for much easier development and sometimes removes the need of a separate MCU.
NodeMCU firmware for ESP8266 supports Lua and MicroPython. I will use Lua for my script(s). NodeMCU's Lua build includes MQTT library out of the box so it should be very easy to use and it turned out that it is.
I have a couple of other WiFi modules but this one will most likely be used. I will have to test the sensor acquisition capabilities (and FPU computation performance for acceleration vector magnitude calculation).
ESP-07 variant looks like this (right and bottom modules):
Figure 1. ESP8266 ESP-07 - The first module on the right side along with ESP-201 variant (middle) and Microchip's module (left)
At the bottom is ESP-07 with antenna attached and extension wires soldered.
Flashing NodeMCU firmware
Module I got from Ebay didn't come with NodeMCU so I had to flash it myself. It took me some time to find the correct pin configuration for flashing but I finally managed to. So, here's what I did...
Necessary parts:
1) USB <-> Serial (I used a Prolific USB to Serial converter that I got with my Remzibi OSD module that I use for FPV flying).
2) ESP8266 ESP-07 module (There are many different ESP8266 modules, each having a slightly different configuration in terms of pins that are broken out and antenna options)
3) Level shifter (ESP8266 operates on 3.3V, yet Prolific module operates on 5V)
4) Jumper wires
My USB<->Serial came with a small connector to fit the small OSD board size so I soldered 2.54mm male and female headers in the middle of the cable so I can use it for other projects, not only for OSD module.
Figure 2. Prolific USB <-> Serial with additional 2.54mm connectors
Figure 3. ESP8266 ESP-07 pinout (bottom view)
I soldered some expansion wires to be able to use the module on a breadboard (I think that original pads pitch is 2mm). I only extended the required pins, others are unpopulated:
Figure 4. Expansion wires soldered
Some people say that ESP8266 can handle 5V on Tx/Rx but I wanted to play safe so I used a level converter (in the middle of my breadboard).
Figure 5. Breadboard setup
Figure 6. Fritzing drawing, manually modified to include ESP8266 (orange wire is 3.3V and red is 5V — disregard the CTS label on FTDI board, I used that board just as an illustration)
For flashing I used the NodeMCU flasher utility (https://github.com/nodemcu/nodemcu-flasher download the appropriate prebuilt binary for Windows).
Also download the latest pre-built firmware (From https://github.com/nodemcu/nodemcu-firmware/tree/master/pre_build select a desired version and download the .bin file)
There are plenty of videos on this topic on YouTube.
One important note is that GPIO0 must be grounded during firmware flashing. After the flashing completes, GPIO0 pin should be disconnected to allow normal boot (otherwise it would boot into bootloader again).
Additionally, GPIO15 must be connected to ground lead all the time. CH_PD and GPIO02 must be pulled up to 3.3V. Module RX connects to USB<->Serial TX and vice versa.
At this point ESPlorer tool can be used to test the module with new firmware (http://esp8266.ru/esplorer/). Before flashing module, it's UART was running at 115200 baud but after flashing I was able to access it only at 9600. That was fine with me.
Startup Lua script - initial version
The following Lua scripts are able to start the module in STATIONAP mode (WiFi client and AP at the same time) and allow a client to connect and send the configuration data. After configuration data is received, module will connect to the configured AP using given credentials. I will expand the functionality so it will also receive the MQTT broker parameters as part of initial configuration string. It will then enumerate sensors and connect to MQTT broker and start sending the sensor readings.
These two scripts were taken from www.esp8266.com forum thread.
--init.lua if (wifi.ap.getip() == "0.0.0.0") then print("Configuring as AP...") wifi.setmode(wifi.STATIONAP) wifi.ap.config({ssid="si_abc123",pwd="12345678"}) else print("Waiting to be configured with the wifi credentials...") dofile("onboardServer.lua") end print("AP address: " .. wifi.ap.getip())
-- onboardServer.lua ss=net.createServer(net.TCP) ss:listen(80,function(c) c:on("receive",function(c,pl) print(pl) ssidBegin = string.find(pl, "=", 0)+1 print(ssidBegin) ssidEnd = string.find(pl, "&", 0)-1 print(ssidEnd) passBegin = string.find(pl, "=", ssidEnd+2)+1 passEnd = string.find(pl, "&", ssidEnd+2)-1 ssidName = string.sub(pl, ssidBegin, ssidEnd) pass = string.sub(pl, passBegin, passEnd) print ("SSID: " .. ssidName) print ("Password: " .. pass) c:send("OK") c:send("\nTMR:"..tmr.now().." MEM:"..node.heap()) c:on("sent",function(c) c:close() ss:close() end) wifi.sta.config(ssidName,pass) print("Connected to your wifi network!") end) end)
Each module will have a unique SSID and password but all SSIDs will have the same prefix that will allow my BASH scripts (EasyConfigure - BASH scripting) to enumerate and initialise all the available modules.
I still have to find the way to execute a given Lua script after module boot-up. But from what I read, it shouldn't be hard at all.
That's it for this update. It's been real fun working with ESP8266 so far
Hopefully, I will get my Analog Devices boards soon so I can switch to that part of project too.