Sunday 25 February 2018

Internet of Things (IoT) for just RM21.90: the ESP8266 NodeMCU ESP-12E Lua V3

ESP-12E. Reset button is at the top left.
When I finished the last post on the ESP8266, I bought an ESP-12E from robotedu.my for just RM21.90, and it arrived the day after! How could I resist the new toy?

Communication should be simple. There is a CH340G USB to serial converter IC on the ESP-12E and a micro USB connector to boot, so I plugged it into my laptop. True enough a new device file came up: /dev/ttyUSB0. No surprises there.

I ran minicom, and this time it failed with 'Device /dev/ttyUSB0 is locked'. Started up minicom without resetting the device with
minicom -o

The ESP-12E was not responsive. Now there is a reset button on it and every time it was pressed the blue led flashed, and minicom detected something. Eventually, by trying the baud rates one by one, at 115200 baud I got part of a message when it was reset: 'ready'. Well and good, but it refused to go any further. 

So it is not going to be simple. A few weeks later, I tried again. This time by first searching the web for startup problems with this board. This link mentioned the board was a little prone to power problems.

Luckily, by this time, yet another new toy had arrived, the D-Link DUB-1370 USB 3.0 Hub, which came with a stonking 4A 5V external power supply. Time to upgrade the ESP-12E's power supply.


This time minicom had no trouble with the device file (/dev/ttyUSB0). The PCB has 'use 9600bps' helpfully printed on it, but it did not work. Luckily I noticed the Reset button, marked 'RST'. On pressing it there are a couple of satisfying flashes from a blue blinkenlight.

A search of WiFi access points came up with a new one:

          Cell 03 - Address: 2E:3A:E8:42:C1:55
                    Channel:1
                    Frequency:2.412 GHz (Channel 1)
                    Quality=70/70  Signal level=-20 dBm
                    Encryption key:off
                    ESSID:"ESP_42C155"
                    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=0000000054adf21d
                    Extra: Last beacon: 5750ms ago
                    IE: Unknown: 000A4553505F343243313535
                    IE: Unknown: 01088B9682840C183060
                    IE: Unknown: 030101
                    IE: Unknown: 32046C122448
                    IE: Unknown: DD0918FE34030100000000

So there are some signs of life. An Internet search for ESP_42C155 turned up nothing. But luckily the reset button produced some rubbish characters. I simply kept changing the serial port (/dev/USB0)'s baud rate until the output made sense on reset. Eventually, it produced 'ready' at 115200 baud, 8-bit, 1-stop and no parity.

Aha. Trying the tested command sequence (see my past post)

AT

followed by Ctrl-M and Ctrl-J. And got a very satisfying output:
AT

OK

It was time to look in the manual. This produced the version numbers:

AT+GMR
AT version:1.1.0.0(May 11 2016 18:09:56)
SDK version:1.5.4(baaeaebb)
compile time:May 20 2016 15:08:19
OK

Now since the Access Point ESP_42C155 was not encrypted, I logged my laptop into it. dhclient ran successfully and assigned the IP address of 192.168.4.2 to my laptop:

$dhclient -v -1 wlan0
Internet Systems Consortium DHCP Client 4.3.4
Copyright 2004-2016 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/wlan0/54:27:1e:50:38:c7
Sending on   LPF/wlan0/54:27:1e:50:38:c7
Sending on   Socket/fallback
DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 8
DHCPREQUEST on wlan0 to 255.255.255.255 port 67
DHCPOFFER from 192.168.4.1
DHCPACK from 192.168.4.1
bound to 192.168.4.2 -- renewal in 2863 seconds

So the little PCB not only has an Access Point, but has a DHCP server. Many of the AT commands in the manual did not work, but a good many did. There are probably a lot of variations in the NodeMCU firmware. This AT command confirms the DHCP server:

AT+CWDHCP?
+CWDHCP:3
OK

And I can even ping my laptop:
AT+PING="192.168.4.2"
+1

OK

However trying the link http://192.168.4.1 from a browser failed. Some posts had suggested some ESP-12E boards also had a webserver. Just not this one.

No matter. Now an IoT device needs to be able to connect to the Internet. I set the ESP-12E to be a WiFi client as well as an Access Point:

AT+CWMODE=3

OK

And set it to log into my house WiFi Access Point:

AT+CWJAP="HouseAP","password"
WIFI CONNECTED
WIFI GOT IP

OK

AT+CIFSR
+CIFSR:APIP,"192.168.4.1"
+CIFSR:APMAC,"2e:3a:e8:42:c1:55"
+CIFSR:STAIP,"172.16.1.102"
+CIFSR:STAMAC,"2c:3a:e8:42:c1:55"

OK

Cool. It looks like the house AP has assigned it an IP. To confirm Internet access I pinged the Google nameserver 8.8.8.8:

AT+PING="8.8.8.8"
+38

OK

This looks good. The manual mentions the ESP-12E can be made to communicate with a WeChat App (the China version of WhatsApp). It can also be upgraded from the Internet. But to keep this blog post short, we go with the AT command manual for now.

An IoT device also needs to be able to receive commands from my phone or laptop. The ESP-12E not having a webserver or ssh server running, perhaps we can try a raw network socket. From the manual, we first dip a toe in the water:

AT+CIPSTART=?
+CIPSTART:(id)("type"),("ip address"),(port)
+CIPSTART:((id)"type"),("domain name"),(port)

OK

That looked good, so let's try to connect to an arbitrary network socket 3344 on the laptop. We can use TCP/IP but let's try the socket raw, using UDP:

AT+CIPSTART="UDP","192.168.4.2",3344
CONNECT

OK

A quick check shows the connection is good:

AT+CIPSTATUS
STATUS:4
+CIPSTATUS:0,"UDP","192.168.4.2",3344,43972,0

OK

Over at the laptop I ran a bash command nc to read the port:

$nc -ul -p 3344

Back at the minicom terminal, I issue:

AT+CIPSEND=11

OK
>

This looks good. The string 'Hello world' is exactly 11 characters, and on typing it into minicom, over at the laptop I get:

$nc -ul -p 3344
Hello world

So we have the basic elements of an IoT device. It can send messages to my laptop, albeit with much typing (in particular the use of uppercase AT commands terminated with Ctrl-M Ctrl-J made things quite ponderous. This can be automated from the laptop using a python script:

$python
Python 2.7.11 (default, Mar  3 2016, 13:35:30)
[GCC 5.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>> port=serial.Serial('/dev/ttyUSB0', 115200, timeout=3)
>>> port.write('AT\r\n');port.read(100)
4
'\r\nOK\r\nAT\r\r\n\r\nOK\r\n'
>>> port.write('Hello world');port.flush();port.read(100)
11
'\r\nRecv 11 bytes\r\n\r\nSEND OK\r\n'
>>>

Much work remains. The GPIO commands like AT+SYSGPIOREAD and AT+SYSGPIOWRITE did not work, otherwise a simple (if a little ponderous) IoT application can be written in python using the AT commands in the manual. Without ssh there are probably serious security issues to be addressed first. The ESP-12E firmware should probably be updated, and it would be nice to be able to communicate with it using a browser.

Until the next post then, happy trails.

No comments:

Post a Comment