Serial UDP/IP Gateway for Arduino Based on ESP8266 Shield
by cuillerj in Circuits > Arduino
1889 Views, 2 Favorites, 0 Comments
Serial UDP/IP Gateway for Arduino Based on ESP8266 Shield
I already published in 2016 this instructable "How to make your own Wifi gateway to connect your Arduino to IP Network". Since I did some code improvements and I am still using this solution.
Nevertheless there is now some ESP8266 shields that allow to do quite the same without soldering as long as you are not using Micro or Nano boards.
This instructable will explain how to use these ESP82 shields as serial UDP/IP gateway.
This takes part of a global home automation infrastructure you can have a look here
More information about the shield with this instructable
Supplies
1 Arduino Mega (the best is to have at least 1 Mega for development and 1 Uno for running phase)
1 Rokoo ESP8266 ESP-12E UART WIFI Convertisseur
1 FTDI 3.3v
2 breadboard wires
How Does It Work ?
The Gateway is based on an ESP8266 module.
This module is connected from one side with the serial link from the other side to IP network with the Wifi.
It acts as a black box. Data packets coming from the serial link are sent to an IP/Udp port and vis et versa.
You just have to set your own configuration (IP, WIFI ...) once the first time you will power on the Gateway.
It can transfer either raw ASCII and binary data (no HTTP, JSON...)
It is designed to connect objects with server home made softwares that need fast and frequent transfers of short packet of data.
What Are the Main Functions ?
Mostly it is a black box that convert serial data to UDP packet in both ways.
But the gateway can also act by his own by sending internal information to the server and receiving some commands from the server.
Arduino can send and receive messages toward / from a server connected over Internet just by printing / reading a serial link. There is no need for the Arduino developer to bother of IP protocol.
On top of that it provides a GPIO that can be used by Arduino to check that the Gateway is correctly connected to the WIFI and the Gateway can automaticaly swith beetwen 2 different SSID in case of failure
The Gateway has 2 different running modes that are selected by setting a GPIO
Setting GPIO to ground and the Gateway enters in configuration mode.
Setting GPIO free and the Gateway enters in gateway running mode.
On top of that the Gateway UDP/IP configuration can be modified remotely
Build of Material
Very few things are required
1 ESP8266 Shield - I found this UNO R3 ESP8266 Serial WiFi Shield Extend Board Module at less than 9€
1 UNO that is the target Arduino target
1 Mega that is the arduino development tool (you can do without but it is quite difficult to debug)
1 FTDI 3.3/5v for development
Some wires
Download the Gateway Code
During this step the Arduino is only used to power (with USB or an other power source) on the ESP8266 shield
Connect the ESP8266 GPIO4 to ground (to enter configuration mode)
Take care to set the FTDI to 3.3v as required by the shield
Connect the FTDI to the shield (RX to TX)
Set set shield switch to 1:off 2:off 3:on 4:on
Connect the USB side of the FTDI to your computer
Reset the shield with the ESP-RST push button
Download the Gateway code there on GitHub
Open Arduino IDE
- Select the FTDI monitor port
- Open the IDE Serial Monitor - Set speed to 38400
- Select Generic ES8266 module board
- Upgrade the shield firmware with the gateway code
Set switch 3:off 4:off
Open the IDE Serial Monitor
Reset the shield with the ESP-RST push button
You must see messages on the monitor starting with "EEPROM initialization" "Initialization completed"....
It is time to do the configuration
Let's Do the Gateway Configuration
When downloading the gateway code for the first time, the ESP8266 Eeprom will be initialized with the default values. You will see this kind of messages "set parameter: x size: yy"
You can find these values inside the code definition of paramValue. Of course you can change these default values before downloading the code but you can also set your configuration by command later. This is better if you plan to have multipe gateway ans keep only one version of code.
Use the monitor serial port to send command (set to NL and CR).
As SSID is not currently defined wait for "Could not connect to yoursecondssid retry:5"
Then a scan of WIFI will automatically start
It is time to set your SSIDs with the following commands:
- SSID1=yourchoice1
- PSW1=yourpsw1
- SSID2=yourchoice2
- PSW2=yourpsw2
- SSID=1 (to choose which SSID to start with)
- RestartWifi
After a few seconds you can check the connection with the command "ShowWifi". You must see the IP address the gateway got from your DNS server. If it is the case it is time to go further
It's time to define your IP server address by entering the 4 subaddresses (server that will run the Java test code). For instance:
- "IP1=192"
- "IP2=168"
- "IP3=1"
- "IP4=10"
By send an empty command you will see all the supported commands that you can use later to fit your requirements. All the parameters values are stored in Eeprom and need a reboot to be taken into account.
Remove the configuration wire
The shield is now running as a gateway
Download the Arduino Code Example
First download the main Arduino code there on GitHub
Then download the Arduino code this commands definition and this serial link code into your librairies
Then open the main code with a new Arduino IDE
Set the shield Switch 1 and 2 off to free the Arduino serial link 0 for USB connection
Reset the shield
Connect the FTDI wires to the Mega Serial 2 (TX FTDI to RX Mega and so on)
Start a new Arduino IDE (or a TTY tool) , connect the FTDI Usb and start to monitor the serial link
Upload the Arduino code inside the Mega
Set the shield Switch 1 and 2 on to connect the Arduino serial link 0
Reset the shield
You must see this message "start usb print" on monitor
Let's Do the Server Side !
The server example is a Java program that you can download here on GitHub
Just run it and look at the Java console and look at the FTDI monitor
You will see data exchanges between server and Arduino
To Go Further
This Arduino code example is based on a framework part of my home automation infrastructure.
If you are interested in this infrastructure, let me know. I will publish sources.
If you just want to use the gateway you can simplify the Arduino code.
After having developp and test your code on a Arduino Mega, you can easily replace it by a Uno !
On top of that you can connect a wire between Arduino GPIO 7 and ESP8266 GPIO 5 if you want your Arduino to check the Wifi connection