Connecting an Uno/Nano to an ESP-01
A typical setup uses an Uno or Nano (which have just one hardware serial port) with an ESP-01 to give WiFi connectivity.
Scenario 1 – Hardware serial port is used for debugging, SoftwareSerial is use for the peripheral
The simplest approach is to keep the hardware serial port for debugging and uploading code, and create a SoftwareSerial port to connect the ESP-01 to. However, this will only work if your ESP-01 is set to communicate at 9600 baud, as SoftwareSerial doesn’t work effectively at higher speeds on an underpowered board like the Uno or Nano.
A common mistake is to see that the Uno/Nano has pins marked Rx and Tx and connect the ESP-01 to these. These Rx & Tx pins (GPIO0 and GPIO1) are connected to the USB connector on the Uno/Nano via the onboard FTDI adapter, so in this scenario they need to be left unconnected.
A typical Blynk sketch for this scenario would look like this
#define BLYNK_TEMPLATE_ID "TMPLxxxxxx"
#define BLYNK_DEVICE_NAME "Device"
#define BLYNK_AUTH_TOKEN "YourAuthToken"
// Send Blynk user messages to the hardware serial port…
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
// Create a SoftwareSerial port on pins 2 & 3 and call it EspSerial…
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX
// Define your ESP8266 baud rate (Max 9600):
#define ESP8266_BAUD 9600
// Tell the Blynk library to use the SoftwareSerial port for WiFi..
ESP8266 wifi(&EspSerial);
void setup()
{
// Initialise the debug (hardware) serial port
Serial.begin(115200);
// Initialise the SoftwareSerial port…
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
}
void loop()
{
Blynk.run();
}
This line of code…
SoftwareSerial EspSerial(2, 3); // RX, TX
tells the sketch that we are going to connect the ESP-01 to pins 2 and 3 on the Uno/Nano. Remember that pin 2 (SoftwareSerial Rx) will connect to the Tx pin on the ESP-01 and that pin 3 (SoftwareSerial Tx) will connect to the Rx pin on the ESP-01.
With this setup, if you want to send your own debug messages to the serial monitor then you would use the regular Serial.print() command, and you can see that we use #define BLYNK_PRINT Serial
to tell the Blynk library to send Blynk related data to the hardware serial port.
Scenario 2 – Hardware serial port is used for the peripheral, SoftwareSerial is use for debugging (using an FTDI adapter)
This approach is normally used when your peripheral needs to communicate at a higher baud rate than 9600. This usually occurs when you are unable to change the baud rate of the peripheral device. Of course, this can occur when you don’t have an FTDI adapter available to re-configure the peripheral, but if you don’t have an FTDI adapter available then you won’t be able to see the debug messages from Blynk either.
In this scenario, the ESP-01 will be connected to pins 0 & 1 (labelled Rx and Tx on the board). The Rx on the Uno/Nano will connect to the Tx on the ESP-01 and the Tx on the Uno/Nano will connect to the Rx on the ESP-01.
You will probably have to disconnect the ESP-01 from the Uno/Nano when you want to upload a new sketch to the Uno/Nano, as the ESP-01 will probably interfere with the upload process.
A typical sketch for this scenario would look like this…
#define BLYNK_TEMPLATE_ID "TMPLxxxxxx"
#define BLYNK_DEVICE_NAME "Device"
#define BLYNK_AUTH_TOKEN "YourAuthToken"
// Send Blynk user messages to the SoftwareSerial port…
#define BLYNK_PRINT DebugSerial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
// Create a SoftwareSerial port on pins 2 & 3 and call it DebugSerial…
#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX
// Define your ESP8266 baud rate:
#define ESP8266_BAUD 38400
// Tell the Blynk library to use the Serial port for WiFi..
ESP8266 wifi(&Serial);
void setup()
{
// Initialise the debug (SoftwareSerial) port (Max 9600)
Serial.begin(9600);
// Initialise the hardware Serial port…
Serial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
}
void loop()
{
Blynk.run();
}
To view the debug messages that are being sent to the SoftwareSerial port (pins 2 & 3) you will need an FTDI adapter connected to these pins (pin 2 (Rx) to the FTDI Tx pin, pin 3 (Tx) to the FTDI Rx pin).
With this setup, if you want to send your own debug messages to the serial monitor then you would use the DebugSerial.print() command. If you used Serial.print() command instead then these would go to the ESP-01 and probably cause a disconnection from Blynk. You’ll also see that #define BLYNK_PRINT DebugSerial
is used in the sketch to tell the Blynk library to send Blynk related data to the SoftwareSerial port.