Go Down

Topic: Serial communicatio between Arduino Mega and ESP8266 (Read 794 times) previous topic - next topic

chrishaccour

Hi,

I'm trying to get reliable serial comms to work between my Arduino Mega and an ESP8266-01.
The idea is to have the ESP handle all the network communications like connecting to WiFi and then ultimately connect to my MySQL server to store data comming from the sensors and stuff haning on the Arduino.
So, the Arduino does all the measuring and logic stuff and when it wants to log something it communicates this to the ESP8266 who then sends it off to the database.
But first I need to get serial comms working reliably

I've searching the net and different fora to find a solution and I've build something which works.
I've setup the ESP so that it will connect to the Wifi networks and publish it's IP address over the serial port every second. The arduino then receives this on Serial2 and prints it out on serial. This works but there is a lot of garbage in the information the Arduino receives which messes up the communication.
I don't see any garbage when I hang the ESP directly on a USB port with an adaptor.

This is the sketch on the ESP:
Code: [Select]

#include <ESP8266WiFi.h>

const char* ssid = "**********";
const char* password = "*********";
WiFiClient client;
String sLine;

void setup() {
  Serial.begin(115200);
  while (!Serial);

  // We start by connecting to a WiFi network
  sLine = "Connecting to " + String(ssid); send2Serial(sLine);

  WiFi.mode(WIFI_STA);  //Explicitly set the ESP8266 to be a WiFi-client
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {delay(500);}
  send2Serial("WiFi connected");
}

void loop()
{
  delay(1000);
  sLine = ("IP address: ") + String(WiFi.localIP()); send2Serial(sLine);
}

//-------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------SubRoutines----------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------

void send2Serial(String msg)
{
  msg.trim();
  msg = String("<") + msg + ">" ;
  Serial.println(msg);
  //Serial.end();
}


This is the code on the Arduino:

Code: [Select]

const long espBaud = 115200;
const char startMarker = '<';
const char endMarker = '>';
const byte numChars = 128;
char receivedChars[numChars];
boolean newData = false;

void setup()
{
  Serial.begin(9600);
  Serial2.begin(espBaud);
  Serial.println("\nSerial Comms Demo");

  Serial.println("End Setup\n");
}

void loop()
{
  recvWithStartEndMarkers();
  showNewData();
}


//-------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------SubRoutines----------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------


boolean sendCommData(String command)
{
  Serial2.println(command);
}

char rc;
bool recvInProgress = false;
int ndx = 0;
void recvWithStartEndMarkers()
{
  while (Serial2.available())
  {
    rc = Serial2.read();
    //Serial.print(rc);
    if (recvInProgress)
    {
      if (rc != endMarker)
      {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) ndx = numChars - 1;
      }
      else
      {
        receivedChars[ndx] = '\n'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }
    if (rc == startMarker) recvInProgress = true;
  }
}

String showNewData()
{
  if (newData == true)
  {
    Serial.print("Rec: ");
    Serial.println(receivedChars);
    for ( int i = 0; i < sizeof(receivedChars);  ++i ) receivedChars[i] = (char)0;
    newData = false;
  }
}


As you can see I already use start and end markers on the serial information being passed, but sometimes the garbage actually contains the start marker.

Is there someone out there who can help me getting rid of the garbage?
Or a better way of obtaining my goal?
I've also tried to control the ESP directly from the Arduino using AT commands and that does work, I can connect to Wifi etc... but I need to use the MySQL library which needs a client instance to communicate with. And that I can't do (or don't know how to do) in combination with the AT commands.

With an ethernet module this would be a breeze and I've done this several times but this time I need mobile comms and this ESP module has been really given me sleepless nights over these past weeks.
Perhaps I need to consider another board with Wifi included, but this seemed so simple and cheap that I'm now stubbornly commited to getting it to work

Thank you, Chris.

Juraj

it will be a wiring problem. how did you wire the esp8266 to Mega?

chrishaccour

I'm using an "esp8266 Serial Wi-Fi Wireless ESP-01 Adapter module" that I got from amazon under https://www.amazon.de/gp/product/B0768DKDB1/ref=oh_aui_detailpage_o03_s00?ie=UTF8&psc=1
So I have only 4 pins to connect Vcc, GND, Tx and Rx.

Thinking about this, Tx and Rx are connected to the Arduino but the Vcc and GND are connected to a seperate power supply, as not to overload the Arduino power regulator, and the GND of the Arduino is NOT conneced to the GND of that power supply.

Might this stupidity be causing the problem?

Regards, Chris.

Juraj

the GND of the Arduino is NOT conneced to the GND of that power supply.

Might this stupidity be causing the problem?

yes

chrishaccour

I'll try this out as soon as I get home and get back to you.

But you think the code is Ok?


chrishaccour

Hi  Juraj,

I checked it out, added the ground wire between the powersupply and the arduino and all the problems seem to have gone away ...
Pretty emnarrasing rookie mistake for someone who is an electronics engineer.

Thanks for the help, Chris.

gark81

Hi @chrishaccour

I'm trying some project similar as your
Esp01 as network control (Wi-Fi, connect to SQL and others) and Arduino Mega to I/O controller, and both communicate each other.

Do your project works well?

I'm thinking to use "Wifimanager library" on ESP8266, and modify it to set MQTT, TelegramBot token, SQL URL and/or other parameters.

I've been playing with Nodemcu and works well, but in the future i'll need more I/O pins, so I think to use ESP8266 and Arduino MEGA.

Go Up