ESP8226 to Atmega2560 serial communication

Hi everyone,

I have some experience in programming but after trying to use an Atmega2560 to receive values from its built-in ESP8226, I'm pulling my hair out.

The end goal is to be able to read a value from my Thingspeak channel using the built-in ESP, then communicate that value through the serial monitor where the Atmega can recieve that value and display it on a 3.2' TFT shield.

For now, I am desperate for a simple example of reading a value from the serial monitor of the ESP to the Atmega. After doing extensive research, this is the best I have (see below) but I need some advice on if the formatting is right and how to do it. (I have uploaded each set of code with the right switches)

Thanks so much

// code for the esp8226 built-in
#include <SPI.h>
#include <Wire.h>
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
const char ssid[] = "networkname";  // your network SSID (name)
const char pass[] = "networkpass";   // your network password

WiFiClient  client;

//---------Channel Details---------//
unsigned long counterChannelNumber = 0000000;            // Channel ID (Current)
const char * myCounterReadAPIKey = "apikey"; // Read API Key (Needs to be changed)
const int FieldNumber = 1;  // The field you wish to read

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  WiFi.mode(WIFI_STA);
  ThingSpeak.begin(client);
}

void loop() {
  //----------------- Network -----------------//
  if (WiFi.status() != WL_CONNECTED)
  {
    Serial.print("Connecting to ");
    Serial.print(ssid);
    Serial.println(" ....");
    while (WiFi.status() != WL_CONNECTED)
    {
      WiFi.begin(ssid, pass);
      delay(5000);
    }
    Serial.println("Connected to Wi-Fi Succesfully.");
  }

  //----------------- Room1 -----------------//  
  long Room1 = ThingSpeak.readLongField(counterChannelNumber, FieldNumber, myCounterReadAPIKey);
  int statusCode;
  statusCode = ThingSpeak.getLastReadStatus();
  if (statusCode == 200)
  {
    Serial.write("Room 1: ");
    Serial.write(Room1);
  }
  else
  {
    Serial.println("Unable to read channel / No internet connection");
  }
  //----------------- End of Room1 -----------------//  

}
//code for the Atmega for reading values
void setup()

{
  Serial.begin(115200);
  Serial3.begin(115200);
  pinMode(13,OUTPUT);
  delay(500);
  Serial.println("AT+CIPMUX=1");
  Serial3.println("AT+CIPMUX=1");
  delay(2000);
  Serial.println("AT+CIPSERVER=1,5000");
  Serial3.println("AT+CIPSERVER=1,5000");
  delay(2000);
  Serial.println("AT+CIPSERVER=1,5000");
  Serial3.println("AT+CIPSTO=3600");
  delay(2000);
}

 

void loop()
{
    // listen for communication from the ESP8266 and then write it to the serial monitor
    if ( Serial3.available() )   {  Serial.write( Serial3.read() );  }
 
    // listen for user input and send it to the ESP8266
    if ( Serial.available() )       {  Serial3.write( Serial.read() );  }
}```

you have an Arduino sketch in the esp8266, not AT firmware. why do you send AT commands?

1 Like

Sorry, I don't have much experience with the Atmega, I thought that I was on the right track with the code above. It would be good to be referred to a guide that works for simple communication from the ESP8226 built-in to the Atmega2560

one option

1 Like

Thankyou so much! That works, but I need the Mega 2560 to be on the receiving end for the serial readings, so I need to be able to program both sides. There's barely any documentation on doing this.

with WiFiEspAT you can use code written for any Arduino WiFi library.
see the examples of the WiFiEspAT library in Arduino IDE examples menu

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.