Thursday 11 January 2018

The ESP8266 NodeMCU: the ultimate IoT system?

NodeMCU ESP-12E Development Kit
In my previous posts I started making IoT devices from full Linux systems, like the Raspberry Pi solar battery voltmeter. Then we progressed to a bluetooth sensor/actuator with a Linux IoT gateway like the IoT Autogate remote.

The NodeMCU development kit promises to provide both: a remote sensor/actuator that provides it own WiFi IoT gateway. The NodeMCU is open source firmware based on Expressif System's ESP8266, a System-on-a-Chip (SoC) which has an 80MHz  32-bit Tensilica CPU paired with a full-stack WiFi networking at an extraordinarily low cost. The Lua ESP-12E NodeMCU costs a mere RM21.80

ESP-12E prices online in Malaysia
Watch out for the ESP-12E V1, and V2 versions; their physical sizes are different! There is a write-up here.

Be careful of the ESP-12 V1 and V2 differences

While my previous Microchip PIC18F14K50 plus Arduino HC-06 IoT can be commercialized, no one else has done it like that. The reason may be something like the ESP8266. ITEAD's Sonoff mains power switches and sockets actually uses the ESP8266.

I am starting with the ESP-01 and a CH340 USB to RS232 TTL dongle. Just because I happen to have them lying around. Okay maybe it was because the ESP-01 cost me a mere RM14. The CH340 is even better at RM7.90.
Earlier ESP modules


ESP-01 at RM14
CH340 at RM7.90

The ESP-01 and CH340 setup provides a bridge from my Linux comfort zone to explore the ESP8266, after which I fully expect the ESP8266 to be a standalone IoT system. For this I will need NodeMCU, which is the software framework to base future IoT work.

First we plug the CH340 dongle into my laptop. Slackware 14.2 Linux recognized the device immediately:

[Tue Jan  9 06:38:49 2018] usb 1-4.5.4: new full-speed USB device number 113 using xhci_hcd
[Tue Jan  9 06:38:49 2018] usb 1-4.5.4: New USB device found, idVendor=1a86, idProduct=7523
[Tue Jan  9 06:38:49 2018] usb 1-4.5.4: New USB device strings: Mfr=0, Product=2, SerialNumber=0
[Tue Jan  9 06:38:49 2018] usb 1-4.5.4: Product: USB2.0-Serial
[Tue Jan  9 06:38:50 2018] usbcore: registered new interface driver usbserial
[Tue Jan  9 06:38:50 2018] usbcore: registered new interface driver usbserial_generic
[Tue Jan  9 06:38:50 2018] usbserial: USB Serial support registered for generic
[Tue Jan  9 06:38:50 2018] usbcore: registered new interface driver ch341
[Tue Jan  9 06:38:50 2018] usbserial: USB Serial support registered for ch341-uart
[Tue Jan  9 06:38:50 2018] ch341 1-4.5.4:1.0: ch341-uart converter detected
[Tue Jan  9 06:38:50 2018] usb 1-4.5.4: ch341-uart converter now attached to ttyUSB0

The pinouts are:



The wiring will be (in format ESP-01 to CH340):

RX to TX, VCC to VCC, GPIO0 to VCC (Note this is different from the following picture), RST to GND, GPIO2 No connection (N/C), CH_PD to VCC, GND to GND, and TX to RX.

Next I will need to make a cable to connect the ESP-01. The connection should be similar to that for the other USB to serial dongle, the FTDI:
The ESP-01 and the FTDI USB to Serial TTL dongle. Note GPIO0 should be high and not as shown.
The ESP-01 will draw power from the CH340 which in turn gets its power from the laptop's USB socket. The important thing to remember is the ESP-01 is a 3.3V device. Luckily the CH340 (like the PIC18F14K50) can be set to run at 3.3V. The ESP-01 can draw a non-trivial 170mA(a typical external USB drive will draw 300mA), so I plan to use the laptop's USB2 connector and connect the setup via an externally-powered DLINK USB hub as a backup and for additional safety.

I powered on, and ... nothing happened. Oh the power LED in the CH340 came on. So did the red power LED in the ESP-01. Now disconnect RST from GND and the ESP-01's blue LED flashed like it was supposed to, but the CH340 would not respond. Its Linux device file, /dev/ttyUSB0 would come up when the CH340 is powered on.

But when I hooked up the ESP-01 the file disappeared.

[Wed Jan 10 12:23:34 2018] usb 1-4.5.4: USB disconnect, device number 38
[Wed Jan 10 12:23:34 2018] ch341-uart ttyUSB0: ch341-uart converter now disconnected from ttyUSB0
[Wed Jan 10 12:23:34 2018] ch341 1-4.5.4:1.0: device disconnected

The ESP-01 appeared OK though and I noticed a new WiFi Access Point had appeared:

          Cell 05 - Address: 5E:CF:7F:FD:1D:EB
                    Channel:1
                    Frequency:2.412 GHz (Channel 1)
                    Quality=70/70  Signal level=-23 dBm
                    Encryption key:off
                    ESSID:"AI-THINKER_FD1DEB"
                    Bit Rates:5.5 Mb/s; 11 Mb/s; 1 Mb/s; 2 Mb/s; 6 Mb/s
                              12 Mb/s; 24 Mb/s; 48 Mb/s
                    Bit Rates:54 Mb/s; 9 Mb/s; 18 Mb/s; 36 Mb/s
                    Mode:Master
                    Extra:tsf=00000000027d8f4a
                    Extra: Last beacon: 5705ms ago
                    IE: Unknown: 001141492D5448494E4B45525F464431444542
                    IE: Unknown: 01088B9682840C183060
                    IE: Unknown: 030101
                    IE: Unknown: 32046C122448
                    IE: Unknown: DD0918FE34030100000000

I could connected the laptop to it, as there is no password required, and once in a little probing showed an active IP address 192.168.4.1:

$nmap -O 192.168.4.1-254

Starting Nmap 7.12 ( https://nmap.org ) at 2018-01-10 20:14 MYT
Nmap scan report for 192.168.4.1
Host is up (0.0010s latency).
All 1000 scanned ports on 192.168.4.1 are closed
MAC Address: 5E:CF:7F:FD:1D:EB (Unknown)
Warning: OSScan results may be unreliable because we could not find at least 1 o
pen and 1 closed port
Aggressive OS guesses: Philips Hue Bridge (lwIP stack v1.4.0) (95%), 2N Helios I
P VoIP doorbell (91%), Advanced Illumination DCS-100E lighting controller (91%),
 British Gas GS-Z3 data logger (91%), Espressif WiFi system-on-a-chip (91%), Gra
ndstream GXP1105 VoIP phone (91%), LaSAT satellite receiver (91%), lwIP 1.4.0 li
ghtweight TCP/IP stack (91%), m3 muvid IR 715-2 Internet radio receiver (91%), M
ilight WiFi Receiver bridge (91%)
No exact OS matches for host (test conditions non-ideal).
Network Distance: 1 hop

Voltage seems normal at 3.3V and only 70mA current was being drawn. A little digging in the datasheets of the ESP8266 and CH340 showed the minimum operating voltage of the ESP8266 was 3V and the CH340 3.3V. It is possible that on power up the power surge of the ESP8266 dragged the voltage down enough for the CH340 to panic and go to bed, but I could not be sure without hooking up the oscilloscope.

Instead I popped out one of those deliciously cheap Arduino LM2596 buck converters and set it to take in 5V and put out 3.3V just for the ESP8266. I now needed to connect the GND of the CH340 and the ESP8266 to provide a common signal reference, and did not change any other connections.
Rats' nest: clockwise from top LM2956, CH340 and ESP-01

I connected the CH340 to my laptop, and ran minicom, setting it to /dev/ttyUSB0 and baud rate 115200. And lo and behold, on turning on the ESP-01:

ESP-01 screen on power on
And just to make sure it is no fluke I type 'AT' followed by Ctrl-M and Ctrl-J, and it replied with 'OK'.

And to connect it to the Internet like any good IoT device (the commands are in bold and the ESP-01's replies in normal font; at the end of every command line type Ctrl-M and Ctrl-J):

AT

OK
AT+CWMODE=3

OK
AT+CWLAP
+CWLAP:(1,"AP1",-83,"xx::xx::xx:xx:xx:xx",1,-17,0)
+CWLAP:(3,"......",-80,"yy:yy:yy:yy:yy:yy",8,-21,0)
+CWLAP:(3,"AP2",-65,"zz:zz:zz:zz:zz:zz",9,-14,0)

AT+CWJAP="AT2","verysecretpassword"
WIFI CONNECTED
WIFI GOT IP

OK

AT+CIFSR
+CIFSR:APIP,"192.168.4.1"
+CIFSR:APMAC,"5e:cf:7f:fd:1d:eb"
+CIFSR:STAIP,"172.16.1.105"
+CIFSR:STAMAC,"5c:cf:7f:fd:1d:eb"

OK

It connected to my home WiFi network, assigned it an IP address different from its own () access point; 172.16.1.105. And to make doubly sure we launch a network probe at the new IP address:

$nmap -O 172.16.1.105

Starting Nmap 7.12 ( https://nmap.org ) at 2018-01-11 19:39 MYT                 
Nmap scan report for 172.16.1.105                                               
Host is up (0.012s latency).
All 1000 scanned ports on 172.16.1.105 are closed                               
MAC Address: 70:62:B8:A9:C0:CA (D-Link International)
Warning: OSScan results may be unreliable because we could not find at least 1 o
pen and 1 closed port
Aggressive OS guesses: Philips Hue Bridge (lwIP stack v1.4.0) (95%), 2N Helios I
P VoIP doorbell (91%), Advanced Illumination DCS-100E lighting controller (91%),
 British Gas GS-Z3 data logger (91%), Espressif WiFi system-on-a-chip (91%), Gra
ndstream GXP1105 VoIP phone (91%), LaSAT satellite receiver (91%), lwIP 1.4.0 li
ghtweight TCP/IP stack (91%), m3 muvid IR 715-2 Internet radio receiver (91%), M
ilight WiFi Receiver bridge (91%)
No exact OS matches for host (test conditions non-ideal).
Network Distance: 1 hop

OS detection performed. Please report any incorrect results at https://nmap.org/
submit/ .
Nmap done: 1 IP address (1 host up) scanned in 4.43 second

And there you have it. The ESP-01 with its ESP8266 SoC certainly lived up to its reputation. Next I shall try to program it to control a relay from WiFi, much like the IoT bluetooth Autogate Remote project.

Happy trails.

1 comment: