# [Record] How to setup a WiFi-UART serial bridge by using NodeMCU
###### tags: `NodeMCU v3`, `ESP8266`, `wireless console`, `WiFi-UART serial bridge`, `esp-link`, `MCU`, `embedded`, `RPi`, `raspberry pi`
[toc]
## 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.

<iframe frameborder="0" style="width:100%;height:500px;" src="https://viewer.diagrams.net/?tags=%7B%7D&highlight=0000ff&edit=_blank&layers=1&nav=1#G19Td5xP6r7a_mO8DJnMEavV7yYwQOFkZz"></iframe>
## 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

1. <font color=red>NodeMCU RXD0 to RPi UART0 TX</font>
2. <font color=blue>NodeMCU TXD0 to RPi UART0 RX</font>
3. <font color=black>NodeMCU GND to RPi UART0 GND</font>
## Software
### Enable UART in RPi2
- Read [this](https://www.abelectronics.co.uk/kb/article/1035/serial-port-setup-in-raspberry-pi-os)
### Download and install CP210x_Windows_Drivers for NodeMCU v3
- [CP210x Universal Windows Driver](https://www.silabs.com/documents/public/software/CP210x_Universal_Windows_Driver.zip)
### How to flash the esp-link firmware to NodeMCU
#### 1. Prepare esp-link firmware
> One of the most well-known (appeared on [Hackaday’s blog](https://hackaday.com/2015/09/18/transparent-esp8266-wifi-to-serial-bridge/)) and well-designed projects to make ESP8266 as a WiFi-UART bridge is jeelabs’s [esp-link](https://github.com/jeelabs/esp-link).
- [esp-link-v3.0.14-g963ffbb](https://github.com/jeelabs/esp-link/releases/tag/V3.0.14)
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](https://github.com/jeelabs/esp-link/releases/tag/V3.0.14)**
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](https://github.com/nodemcu/nodemcu-flasher/blob/master/Win64/Release/ESP8266Flasher.exe) or [Win32/Release](https://github.com/nodemcu/nodemcu-flasher/blob/master/Win32/Release/ESP8266Flasher.exe)
#### 3. Flash esp-link firmware
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
- 
3. Flash it
- 
You will see the LED on esp-12 module is blinking during flashing the firmware as shown below.

4. 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."

5. Reset NodeMCU by clicking the RST button on the board

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

```shell
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!(lh~*-ít DXo8!(l¯hthl (hl/*rP5
r
X?lH/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

<iframe frameborder="0" style="width:100%;height:259px;" src="https://viewer.diagrams.net/?tags=%7B%7D&highlight=0000ff&edit=_blank&layers=1&nav=1#G1t3-soJn0ic451C3dWH1jp8cdX7JwD_r-"></iframe>
### 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

:::info
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](https://www.hackster.io/chuygen/wireless-serial-bridge-d149ef).

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

{%gist d5c39582790ac45a17748204c30340a3%}
### 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-