RobotDyn UNO+WiFi: SoftwareSerial


Hey. This page is more than 3 years old! The content here is probably outdated, so bear that in mind. If this post is part of a series, there may be a more recent post that supersedes this one.

The RobotDyn board has some DIP switches on, which allow you to UART to either the ESP8266 or ATmega328p, so you can upload code to one or the other and ‘log’ for debugging.

But, for the two to play nicely (with ESP8226 in ATmode) they need to use UART (or ‘serial’ or TX and RX) to talk. The onboard UNO on the RobotDyn only has a single ‘hardware’ serial port which means you can either use the built-in serial for logging or comms to the ESP8266. Not both.

Consequently, unless I am missing something, you cannot be developing your Arduino code and have the wi-fi connection via the ESP8266 working at the same time 😕 This make things a like cumbersome.

Enter the 🌟SoftwareSerial🌟 library

SoftwareSerial lets you coerce 2 * n of the Arduino’s digital pins to add n software additional serial ports. So you talk to the ESP8266 through this, and use the actual Serial on the Uno for uploading etc.

Upside

I can dev my code for the Arduino with serial output for debugging AND have the UNO and ESP talking to each other (using the ESP8266 AT instruction set).

Not sure why they did not document this on the RobotDyn FAQs – flicking DIP switches all the time is a massive hassle.

Downsides

  • You lose some digital pins you could use for something else.
  • Speed (baud rate)- see below.
  • One particular potential (get it!) issue I am not too sure about is that the Arduino runs on 5V and the ESP8266 on 3.3V. If you look at the schematic for the RobotDyn board when you put the Arduino and ESP8266 in direct communication (DIP switch 1 &2) the feed from the former to the latter gets adjusted by some circuitry. I believe Q1 is a MOSFET performing some voltage regulation.

Needless to say, if you are just going to wire directly between the ESP8266 RXD & TXD pins and the Arduino digital pins (like I am!) to use SoftwareSerial...you might break something!

Wiring

Ahem. You have have signed all the 5V-3.3V-caveat forms? Then, we can proceed.

I have connected to pins 3 and 4 to keep 5 to 13 clear for my relays. I am using pins 2 for another sensor. The ones you cannot use are 0 and 1 – they are the proper serial ports.

Reducing Baud Rate for SoftwareSerial

The default baud rate for ESP8266 is 115200bps – this seems to break SoftwareSerial. Too fast. Jumping ahead a little, I found this out with some head-scratching trying to get MQTT (via PubSubClient) working. So, I read the docs for SoftwareSerial which mentions a limit of 57600bps for Arduino boards. I went for full-throttle 57600bps which fixed the problems I was having. You can change the ESP8266 baud rate using AT commands.

Current configuration

The following gives you your current configuration:

AT+UART_CUR?

This will return five comma-separated numbers, with the first being the current <baud rate> (probably the default 115200). The remaining parameters are:

  • <databits>,
  • <stop bits>,
  • <parity>, and;
  • <flow control>

If you know all that already – not sure what you are doing still here.

New configuration

Swapping the 115200 for 57600 but keeping the same value for everything else, and replacing _CUR with _DEF (so setting as the default) will lower your baud rate:

AT+UART_DEF=57600,8,1,0,1

Code

I am using WiFiEspAT. Note it requires at least firmware v1.7 on the ESP8266.

#include <Arduino.h>
#include <SoftwareSerial.h> 
#include <WiFiEspAT.h>
#include <WiFiClient.h>

SoftwareSerial soft(3,4); // RX, TX digital pins on arduino
const char mySSID[] = <wifi router name>;
const char myPSK[] = "<p/w>;

...

void setup(){
  soft.begin(57600);
 // initialize ESP module
  WiFi.init(&soft);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(mySSID);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(mySSID, myPSK);
  }

  // you're connected now, so print out the data
  Serial.println("You're connected to the network");
}

...etc