Week 15 - Networking and Communications

Assignment


My final project will include a WiFi connection for downloading updates of the schedule from the Internet. Hence, I decided to work with the ESP8266 WiFi module, investigating its potential and how it works in conjunction with the ATtiny and other wireless microcontrollers.

The ESP8266 family

ESP8266 is a low cost RF module produced by the Chinese manufacturer Espressif Systems which includes a full TCP/IP stack and comes pre-programmed with an AT command set firmware, like old-fashioned modems: so, you can simply hook this module up to your Arduino (or hello board) and get WiFi-capabilities. However, the ESP8266 has on-board processing and storage capability (like an Arduino) that allows it to be programmed and extended. Moreover, it has a few GPIOs to which sensors and other interfaces can be attached to.

Most common modules come from AI-Thinkers vendor, Espressif Systems itself has produced the ESP-WROOM-02 Wi-Fi Module and there are also modules from other vendors, such as Olimex. AI-Thinker modules, which remain the most widely available (and often the cheapest), are collectively referred to as "ESP-xx modules". The ESP-01 is the first (and less powerful) model: since 2014, the family has evolved and many other versions came out. ESP-12 is a widespread, more recent module, and ESP-14 is the most recent.

Aside the AT firmware, a C++ based firmware enables the ESP8266 CPU and its Wi-Fi components to be programmed like any other Arduino device using the Arduino IDE. Similarly, other firmwares for the ESP8266 include a port of the MicroPython interpreter, a Lua-based scripting environment (NodeMCU), and a BASIC interpreter specifically tailored for the IoT.

The ESP8266 ESP-01

The ESP-01 has 8 pins available in 2x4 dual in-line package; the antenna is a PCB trace and the module has a size of 14.3 × 24.8 mm

The 8 pins are:

The Interface Board - Rel. 1.0

I decided to design a board for the ESP8266 - specifically, I wanted a multi-purpose board usable both for connecting the ESP8266 to the PC through the FTDI cable and for connecting the ESP to the hello board.

Given the pinout of the ESP-01 described above, I connected the ESP8266 supply to the FTDI VCC through a L1117 voltage regulator (which converts the 5V to 3.3V), I exposed the TX and RX pins and finally I added two push buttons for resetting the ESP and enable programming mode without troubles.

As usual, I milled the board using the Roland MDX-40 and the standard, well-tested settings described in week 10.

I soldered all the components on the board. You can clearly see the LM1117 voltage regulator, the two push-buttons for resetting and programming the board, the ESP8266 module and the FTDI connector.

Testing the module

I connected the module to my PC through the FTDI cable. At this point, I wanted to start talking to the ESP. For that, a terminal emulator like picocom or the standard screen command will do the job. First, your board might talk at any of several baud rates. The ones to try first are 9600 and 115200 (it depends on the version of the firmware running onboard).

Please note that most recent ESP firmwares require Carriage-Return-and-New-Line (\n\r - ASCII 10 13) line endings. Hence, you need press followed by Ctrl-J to send a command. However, picocom can be easily configured to send a linefeed after each carriage return. So you can run picocom as follows.

picocom -b 115200 /dev/ttyUSB0 --omap crcrlf
and you are ready to send your first commands!

If your serial transmission rate is correct, try typing "AT": it should echo the characters as you type them and should return an "OK". And you are now ready to start working with the ESP8266.

AT

OK

AT+GMR
AT version:0.25.0.0(Jun  5 2015 16:27:16)
SDK version:1.1.1
Ai-Thinker Technology Co. Ltd.
Jun 23 2015 23:23:50


AT+RST

OK

 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x40100000, len 1396, room 16 
tail 4
chksum 0x89
load 0x3ffe8000, len 776, room 4 
tail 4
chksum 0xe8
load 0x3ffe8308, len 540, room 4 
tail 8
chksum 0xc0
csum 0xc0

2nd boot version : 1.4(b1)
  SPI Speed      : 40MHz
  SPI Mode       : QIO
  SPI Flash Size & Map: 8Mbit(512KB+512KB)
jump to run user1 @ 1000

���(�SQS�(RQ�)HT�)SHHHC���r�ù�
Ai-Thinker Technology Co. Ltd.

invalid


AT+CWMODE?
+CWMODE:2

OK

AT+CWMODE=1

OK

AT+CWLAP
+CWLAP:(4,"FASTWEB-1-T4oittrFQO6h",-85,"f0:84:2f:4d:62:08",6)
+CWLAP:(3,"Vodafone-25253631",-89,"00:24:89:ae:1b:f2",7)
+CWLAP:(0,"Vodafone-WiFi",-90,"00:24:89:ae:1b:f4",7)
+CWLAP:(2,"NoFlyZone",-61,"68:72:51:02:e0:17",9)
+CWLAP:(4,"WIND-WiFi-C7BE",-79,"64:a6:51:00:c7:be",11)
+CWLAP:(3,"Vodafone-30083995",-80,"90:35:6e:7b:a0:b8",12)

OK
AT+CIPSTAMAC?
+CIPSTAMAC:"18:fe:34:da:c4:5b"

OK

Note the noise when you reset the device: some typical bootup messages are sent at 76800, . But there should be a ”ready“ message at the selected baud rate if your UART Rx is wired correctly.

So, ESP8266 boots up into the serial modem mode, and you can communicate with it using a set of AT commands (historically AT commands are based on the Hayes Command Set). Have a look at a list of the AT Commands supported by ESP8266 for more information on the commands shown above.

Upgrading the firmware

Esptool.py is a tool that can be used to install/upgrade firmware onto ESP-XX modules. It is a Python-based, open source, platform independent, utility to communicate with the ROM bootloader in Espressif ESP8266 ∧ ESP32 chips that was started as an unofficial community project but now is also supported by Espressif.

The latest stable esptool.py release can be installed from pypi via pip or pip3 with

pip install esptool
esptool.py -p /dev/ttyUSB0 -b 115000 write_flash 0x000000 ai-thinker-v1.1.1.bin 
esptool.py v1.3
Connecting.....
Auto-detected Flash size: 8m
Running Cesanta flasher stub...
Flash params set to 0x0020
Writing 1044080 @ 0x0... 55296 (5 %)
...

Wrote 1044480 bytes at 0x0 in 90.6 seconds (92.3 kbit/s)...
Leaving...

Manually assembling a bootable firmware image can be a bit of a pain, but the tool is useful to install official, ready-made AiThinker images.

Moreover, the most recent version of the Espressif firmware cannot run on the ESP-01. A working version of the firmware is ai-thinker-v1.1.1 (md5sum 26fce93d7fa31fba177da9a4ae171657)

Arduino IDE setup

First, Arduino IDE needs to be configured to support the ESP module (Arduino version 1.6.4 or higher is required). To install this additional board, as I did for the ATtiny44, follow this step:

I left untouched all the board settings under Tools and I only checked the upload speed (115200).

Hello World

The first program I upload was the classic Blink sketch available from the Arduino examples. However, I changed the LED pin from 13 to 2 from 13, since ESP8266-01 has only two GPIO pins: GPIO0 and GPIO2.

void setup() {
  // initialize digital esp8266 gpio 2 as an output.
  pinMode(2, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(2, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(2, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

I compiled the code, clicked on the Upload button and...

Sketch uses 222,133 bytes (51%) of program storage space. Maximum is 434,160 bytes.
Global variables use 31,508 bytes (38%) of dynamic memory, leaving 50,412 bytes for local variables. Maximum is 81,920 bytes.
error: failed reading byte
warning: espcomm_send_command: cant receive slip payload data
error: failed reading byte
warning: espcomm_send_command: cant receive slip payload data
error: failed reading byte
...
warning: espcomm_send_command: cant receive slip payload data
warning: espcomm_sync failed
error: espcomm_open failed
error: espcomm_upload_mem failed
error: espcomm_upload_mem failed
WARNING
You need to enable flash programming when turning on the module. So, if you want to program the ESP8266, follows these steps:
  1. turn off the ESP;
  2. bring GPIO0 to ground;
  3. turn on the ESP;
  4. upload the code.
If you are using my board you just have to
  1. press (and keep pressed) the program button;
  2. push the reset button;
  3. upload the code;
In both cases, you can release the pushbutton as soon as Arduino IDE starts uploading.

So I kept the Program pressed button on my board, then i pushed Reset button and, finally, tried to upload the program again

Sketch uses 222,133 bytes (51%) of program storage space. Maximum is 434,160 bytes.
Global variables use 31,508 bytes (38%) of dynamic memory, leaving 50,412 bytes for local variables. Maximum is 81,920 bytes.
Uploading 233280 bytes from /tmp/arduino_build_830276/WiFiTelnetToSerial.ino.bin to flash at 0x00000000
................................................................................ [ 35% ]
................................................................................ [ 70% ]
....................................................................             [ 100% ]

Unfortunately, I did not put a led on my GPIO2 pin, so I had to use a multimeter to check the voltage: It was switching periodically from 3.3V to 0V, so it was working!!! Great

The Interface Board - Rel. 2.0

After a few hours spent working with the board, I found a couple of issues in my design and I ideintified a pair of possible improvements.

I redesigned the board to fix all these issues. The improved electronic schematic is the following:

I designed and milled the board as usual. As you can see, the PCB looks much more complex: however, this is the price for a stable and durable interface.

The BOM is the following:

I soldered all the components on the board, and the result is quite satisfactory:

At this point, I uploaded again the Blink code shown above and the LED on GPIO2 started blinking!

Network test

Next, I uploaded the WiFiTelnetToSerial example provided with the ESP8266 package: it's a program which forwards to Serial port the packets received from the WiFi.

However, the core of the code still consists of a few lines in the while loop which waits for a network connection, reads data coming from the network connection and writes them on the serial port (below it's a simplified version of the a bit the code to limit it to only one client)

if (serverClients && serverClients.connected()) {
  if(serverClients.available()){
	while(serverClients.available()) Serial.write(serverClients.read());
  }
}

Hence, I connected the ESP8266 board to a first computer through the FTDI cable, I uploaded the WiFiTelnetToSerial program and I opened picocom. The ESP showed it IP address acquired while connecting to the network

$ telnet 192.168.1.113 23
	
Connecting to NoFlyZone
Ready! Use 'telnet 192.168.1.113 23' to connect

Then, I opened a telnet terminal on a second computer connected to the network and I connected to the ESP IP address.

Telnet is a (insecure!) protocol used in the past on the Internet or local area networks to provide a bidirectional TCP, interactive text-oriented communication facility using a virtual terminal connection. Nowadays is used for debugging purposes only, although some embedded and resource-constrained devices still uses it for providing remote access and management.
$ telnet 192.168.1.113 23
Trying 192.168.1.113...
Connected to 192.168.1.113.
Escape character is '^]'.
ciao

When I typed 'ciao', the characters started flowing on the serial port of the ESP8266

Connecting to NoFlyZone
Ready! Use 'telnet 192.168.1.113 23' to connect
New client: 
ciao

The ESP 32

The SparkFun ESP32 Thing is a comprehensive development platform for Espressif’s ESP32, their super-charged version of the popular ESP8266. Like the 8266, the ESP32 is a WiFi-compatible microcontroller, but to that it adds support for Bluetooth low-energy (i.e BLE, BT4.0, Bluetooth Smart), and nearly 30 I/O pins. According to many vendors, the ESP32's power and versatility will help make it the foundation of IoT and connected projects for many years to come.

The SparkFun ESP32 Thing equips the ESP32 with everything necessary to program, including an FTDI FT231x for USB programming, a lithium-polymer (LiPo) battery charger, a handful of LEDs and buttons for rapid testing and development.

Following the Sparkfun Hookup Guide, I setup the Arduino IDE to support the ESP32 dev board.

Network of Microcontrollers

In order to fulfill the assignment, I decided to connect the ESP32 to my hello board + ESP8266 + output board; then, I wanted to use the programmable button on the ESP32 to activate a LED on the output board (one of my week 10 outputs). The setup of the network is quite simple: the ESP32 connects to a WiFi network and sends messages to the ESP8266, which is connected to the same network, trough a TCP connection. Then, the ESP8266 forwards the message to the Hello board through the UART connection, and finally the hello brings up the led pin.

The addressing used in the network it's IPv4, the standard Internet Protocol: since I worked in a LAN, I used the private addressing range 192.168.0.0/16.

        -------                               -------      -------    -------
        | ESP |  )))        \  /        )))   | ESP | -Tx- |HELLO| -- | OUT |
        |  32 |         -----------           | 8266| -Rx- |BOARD| -- |BOARD|
        -------         | WIFI AP |           -------      -------    -------
                        -----------

The format of the messages sent over the network is really simple: A for button pressed, B for button released. I was interesting in testing MQTT with the ESP, but I was running out of time and I left this interesting protocol on my to-do list for a while.

Moreover, the ESP8266 acknowledges the reception of the message (it sends and 'OK' string). Due to the lack of time, I did not implemented the functions for handling ACK and subsequent retransmissions at application level. I relied on TCP for all these stuff: it's fine enough for simple applications. In production, additional mechanisms could be implemented at application level (timers, etc): otherwise, a message loss or corruption within the application could result in the led not switching to the desired status.

First, I implemented the right-hand side of the network (ESP8266+HELLO+OUTPUT) and I tested them using telnet (I simulated the ESP32 sending A and B characters to turn on and off the led). After checking that everything was working fine, I moved to the ESP32.

You can see the final network up'n'running in the video below! On the left the finger is pressing and releasing the small button on the ESP32; on the right side, the first yellow led on the output board is turning on and off.

The code for the ESP32, the ESP8266 and the ATtiny44 are all available at the bottom of this page.


Data transmission over Audio

In 2016 I created a simple demo for transmitting data between two Arduino boards using audio morse encoding and decoding, a microphone sound sensor and a piezo buzzer using two Arduino boards and two simple sensors. It's not a Fab Academy achievement but I believe it's worth a look... View the system in action in this YouTube video: You can find more information and browse the source code on my ardumorse GitHub repo.

Download zone - List of files