Try   HackMD

[Record] How to setup a WiFi-UART serial bridge by using NodeMCU

Scenario

In order to control or access a DUT without NIC, we can use ESP8266 as our solution.

User 1 can telnet to NodeMCU to control Raspberry Pi 2 remotely through WLAN or WAN as shown below.

ESP8266 is a cost-effective and highly integrated Wi-Fi MCU for IoT applications.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Hardware

  • Access Point Or a Soft AP (enabling wireless hotspot on a phone)
  • NodeMCU v3
  • Raspberry Pi 2 Model B (including SD card and power supply)
  • Laptop or Desktop with a wireless NIC
  • type-A and micro USB cable

UART connection between RPi and NodeMCU

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. NodeMCU RXD0 to RPi UART0 TX
  2. NodeMCU TXD0 to RPi UART0 RX
  3. NodeMCU GND to RPi UART0 GND

Software

Enable UART in RPi2

Download and install CP210x_Windows_Drivers for NodeMCU v3

One of the most well-known (appeared on Hackaday’s blog) and well-designed projects to make ESP8266 as a WiFi-UART bridge is jeelabs’s esp-link.

  • esp-link-v3.0.14-g963ffbb
    whcih contains
    1. boot_v1.6.bin
    2. blank.bin
    3. esp_init_data_default.bin
    4. user1.bin
    5. user2.bin
    6. wiflash
    7. avrflash

  1. Download esp-link firmware: currently stable version is esp-link-v3.0.14-g963ffbb
  2. Extract esp-link-v3.0.14-g963ffbb.tgz
  3. Untar esp-link-v3.0.14-g963ffbb.tar

2. Prepare NodeMCU Flasher

  • NodeMCU flasher is a firmware programmer for NodeMCU DEVKIT V0.9.
  • You can use it to program NodeMCU DEVKIT or your own ESP8266 board.
  1. Download ESP8266Flasher.exe Win64/Release or Win32/Release
  1. Execute ESP8266Flasher.exe
  2. Config it with
    • flash boot_v1.X.bin from the official SDK or from the release tgz to 0x00000
    • flash blank.bin from the official SDK or from the tgz to 0x3FE000
    • flash esp_init_data_default.bin from the official SDK or from the tgz to 0x3FC000
    • flash user1.bin to 0x01000
    • Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
  3. Flash it
    • Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

You will see the LED on esp-12 module is blinking during flashing the firmware as shown below.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. Finish flashing
  • The blue LED on esp-12 module will be dark.
  • You will see the log end with the line "Note:Serial port disconnected."
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  1. Reset NodeMCU by clicking the RST button on the board
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

The LED will light up for about 2 secs while you press the RST button.

How to config NodeMCU

1. Boot NodeMCU

We can read u-boot messages while booting NodeMCU with esp-link firmware by using a serial console with 76600 baud rate as shown below.

  • Boot by connecting a serial console with 76600 baud rate
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x40100000, len 2408, room 16
tail 8
chksum 0xe5
load 0x3ffe8000, len 776, room 0
tail 8
chksum 0x84
load 0x3ffe8310, len 632, room 0
tail 8
chksum 0xd8
csum 0xd8

2nd boot version : 1.6
  SPI Speed      : 40MHz
  SPI Mode       : DIO
  SPI Flash Size & Map: 32Mbit(512KB+512KB)
jump to run user1 @ 1000

rf cal sector: 128
rf[112] : 00
rf[113] : 00
rf[114] : 01

$*zz@kn+t*N(%rKPz|'5 (h'lA#.4(h`# (h(¨z*êj  (h7Ez7$P
                                                    r' @H/k¬Hl (h./ip)+++i (hvjr@++i (h1d?P@++i (h~
                   hv)3|i (h./-*ªrJv,v[!(tt¨z*é|y,R% (h¨~P(vmrjAëvh (h./pªz#l|n-7^zz@kn+t*X,@^4(h7dhv{v;vîA7ë|>\p7Ex%l (hh%=v7(pzè4(h,/gz¨vëpïp+êq«8!(l­h~*-ít DXo8!(l¯hthl (hl/*rP5
                    r
                     X?l­H/Ekjr{X³¹©¢­H/Ekh (¯%j,l

We can read messages after booting u-boot by using a serial console with 115200 baud rate as shown below.

  • Boot by connecting a serial console with 115200 baud rate
lr$ân


       Ä
        lÇ
          b|?rbÄ
                bònnlnnb
                        bp
                          $blrlpòn


                                  l





                                   b
                                    nÂnÄ


                                        bÄònn'l
                                               l`nn
                                                   l`nr?n

                                                         l`pòn

                                                              r




                                                               b
                                                                nÂnìäÄbÄònn'

                                                                            l`nn
                                                                                l`nr?n


      $`pòn

           r
            Çl

              b
               nÂn
                  ?Ä
                    bÄònn'

                          l`nn
                              l`nr?n

                                    l`rn

                                        l`rl

                                            nònn
                                                brnnnä
                                                      l
                                                       b$`
                                                          l2lL




                                                              ä

                                                               lll~rl
                                                                     Lllln





                                                                          ä

                                                                           lll
                                                                              Ll
                                                                                nrnlbb
      llb
         ÇÄllpbbrÄ'brbrÄ'bblln
                              ònEânlrb

                                      l


                                       l`rl
                                           nÇròn|ì
                                                  lÄ
                                                    l`
                                                      rlÄlä


                                                           l`
                                                             rlÇlä


                                                                  l`
                                                                    rl
                                                                      $ä

                                                                        ll`rl$rl
                                                                                bb
  Cb
    b
     brì$
         bòn'nn
               lO



   131>
   132> ** esp-link v3.0.14-g963ffbb
   132> Flash config restore ok
   132> CONN led=0
   132> SER led=2
   134> Wifi init, mode=AP
   136> Wifi uses static IP 192.168.90.11
   139> "ip": "0.0.0.0"
   142> "netmask": "0.0.0.0"
   144> "gateway": "0.0.0.0"
   147> "hostname": "<null>"
   149> sleep enable,type: 2
   152> Httpd init, conn=0x3fff3820
   156> No user file system found!
   158> Serbridge pins: reset=12 isp=14 swap=0
   171> Reset cause: 6=external
   172> exccause=0 epc1=0x0 epc2=0x0 epc3=0x0 excvaddr=0x0 depc=0x0
   172> Flash map 4MB:512/512, manuf 0xD8 chip 0x4016
   176> ** esp-link v3.0.14-g963ffbb: ready, heap=21368
   182> SNTP timesource set to us.pool.ntp.org with offset 0
   187> initializing user application
   190> Waiting for work to do...
   193> mode : softAP(ea:db:84:a4:4a:77)
   197> add if1
   199> dhcp server start:(ip:192.168.4.1,mask:255.255.255.0,gw:192.168.4.1)
   207> bcn 100

2. Connect to NodeMCU AP

After reboot NodeMCU, you should see a new WiFi network with SSID ESP_XXXXX as shown below. And you should connect to it.

===>

3. Config it

  1. Open your browser with http://192.168.4.1 to see the esp-link homepage as shown below

  2. Config "Pin assignment" to esp-12

  3. Config WiFi station

  • Switch to STA+AP mode and scan

  • Connect to the SoftAP (Handsomeguy->connect is my mobile hotspot)

  • Use static IP

4. Finish

Demo

Topology

Procedure

  1. Enable the mobile hotspot in my phone
  2. Open my laptop and connect to my mobile hotspot
  3. Connect NodeMCU to RPi2 with a type-A and micro USB cable
  4. Connect UART interface between RPi2 and NodeMCU
  5. Power up RPi2
  6. Ping 192.168.90.11 -t
  7. telnet to 192.168.90.11
  8. Login to RPi2 successfully

The blue LED on NodeMCU will blink once when you input a character each time.

Troubleshooting

Cannot see what I am typing in telnet through teraterm

Fix it by enabling localecho.

Appendix

NodeMCU Flasher logs

Note:Detect serial port changed.
Note:Auto MAP serial port.Port-->COM3

Note:Serial port connected.
Note:Begin find ESP8266.
Note:ESP8266 ACK success.
Note:Set base address success.
Note:Program flash success.
.
.
Note:Program flash success.
Note:Set base address success.
Note:Program flash success.
Note:Program flash success.
Note:Set base address success.
Note:Program flash success.
Note:Set base address success.
Note:Program flash success.
.
.
.
Note:Program flash success.
Note:Program flash success.
Note:Serial port disconnected.

NodeMCU programming in C with Arduino IDE

Reference to this article.

We can upload these compiled code to NodeMCU by with Arduino IDE.

Methods to access the console

  • telnet
  • uC console
  • socat

You can also use socat instead of telnet:

socat stdio, tcp-connect:192.168.0.10:23

Another way of using socat is to redirect the TCP to a /dev/tty entry like /dev/vmodem0 for example:

socat pty,link=/dev/vmodem0,waitslave tcp:192.168.0.10:23

One socat is running, you can connect to the device (with CRTL-C working and being able to out of top) with screen:

screen /dev/vmodem0
  • putty

Reference

https://www.espressif.com/en/products/socs/esp8266
https://github.com/jeelabs/esp-link
https://github.com/nodemcu/nodemcu-flasher
https://atadiat.com/en/e-esp8266-esp32-wifi-serial-bridge-converter/amp/
https://pi4j.com/1.2/pins/model-2b-rev1.html
https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers?tab=downloads
https://www.abelectronics.co.uk/kb/article/1035/serial-port-setup-in-raspberry-pi-os
https://www.hackster.io/higgsino/huzzah-feather-esp8266-with-max3232-as-rs232-to-wifi-device-5004f6
https://www.hackster.io/chuygen/wireless-serial-bridge-d149ef
http://www.zoobab.com/esp8266-serial2wifi-bridge
https://www.printables.com/model/226933-case-for-nodemcu-esp8266-v3-rs232-to-ttl-breakout-