Sending integer data from Mega2560 to ESP8266

I have a fairly large sketch running on a Mega2560. I would like the sketch to send out emails when error situations occur within the sketch. This would be done via the following code on a ESP8266-01

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
 
const char* ssid = "SSID";
const char* password = "Password";

//-------------------------------------------------------
// NEED TO GET THESE TWO VALUES FROM CODE ON ARDUINO 
//
int paramOne = 78;
int paramTwo = 16;
//-------------------------------------------------------

String strOne = "errnum=";
String strTwo = "&error=";

 
void setup () {

  strOne += paramOne;
  strTwo += paramTwo;
  
  Serial.begin(9600);
  WiFi.begin(ssid, password);
  
  delay(3000);
  
  if (WiFi.status() != WL_CONNECTED)  {
 
    delay(1000);
    Serial.println("Connecting..");

  }  
  else {
    Serial.println("Connected!!!");  
  }
   
  }
 
void loop() {
 
  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
 
    HTTPClient http;  // declare an object of class HTTPClient

    //
    // fill in needed parameters
    //
    String dataline = "http://www.mytest.com/alarm_test.php?";
    dataline += strOne += strTwo;

    bool httpResult = http.begin(dataline);  //specify request destination

    // http.GET to send the email
    
    int httpCode = http.GET(); //send the request

    if(!httpResult){
      Serial.println("Invalid HTTP request:");
      Serial.println(dataline);
      }
    else
      {
      int httpCode = http.GET();
      }
      if (httpCode > 0)
    { // Request has been made
      Serial.printf("HTTP status: %d Message: ", httpCode);
      //String payload = http.getString();
      //Serial.println(payload);
    }
    else
    { // Request could not be made
      Serial.printf("HTTP request failed. Error: %s\r\n", http.errorToString(httpCode).c_str());
    }
    if (httpCode > 0) { //Check the returning code
 
      //String payload = http.getString();   //Get the request response payload
      //Serial.println(payload);                     //Print the response payload
 
    }
 
    http.end();   //Close connection
 
  }
 
  //delay(30000);    //Send a request every 30 seconds
 
}

I found pieces of this code at a couple different places. None of it was directly attributed to any one, so it was a little difficult for me to obtain any guiding info. I did manage to get the above code to work. Each time the ESP8266 is started an email is sent from my website to my mail address.

My question is; would someone mind explaining to me how I can send the 2 required integers (paramOne & paramTwo) from the Arduino Mega to the ESP8266? Then how would I trigger the ESP code to cause the email to be sent? A small amount of example code would be great. I am learning steadily, but at my age this Wifi stuff has still got me a little confused.

Thanks!

Get the age thing out of your head, I'm probably older than you. The key to transmitting multi-digit bytes is to use Serial.write as opposed to Serial.print. The latter pre-supposes you are streaming ASCII serial in which case '78' would be sent as 0x55 followed by 0x56. Serial.write however, will send the byte value (assuming 78 to be a byte) of 0x4E.

All that said, you can't, and shouldn't, do it with String class variable. Use character arrays and life will be much simpler.

What is the connection between the Mega and the ESP? Without knowing that, we can't help you send data using that connection.

ESP8266 GND to Arduino GND

ESP8266 VIN to Arduino 3.3V

ESP8266 ENABLE to Arduino 3.3V

ESP8266 TX to Arduino RX

ESP8266 RX to Arduino TX

@OP

This tutorial could be helpful for you where MEGA is sending two integer data to the ESP8266. ESP8266 receives the data items and present them on the Serial Monitor - 2 (SM2). In this setup, the ESP8266 is just behaving like a simple Arduino (UNO).

1. Build the following connection between MEGA (Sender) and ESP (Receiver).
uartNodeMCU.png
Figure-1: Connection between MEGA and ESP using Serial Port

2. Upload the following sketch into MEGA (Sender).

int paramOne = 78; //0x004E
int paramTwo = 16;  //0x0010

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
}

void loop()
{
  Serial.println("Sending Two Data Items to ESP!");
  Serial1.write(highByte(paramOne));
  Serial1.write(lowByte(paramOne));
  Serial1.write(highByte(paramTwo));
  Serial1.write(lowByte(paramTwo));
  delay(2000);
}

3. Upload the following sketch into ESP (Receiver).

#include<SoftwareSerial.h>
SoftwareSerial mySUART(4, 5);  //D2, D1 = SRX, STX

void setup()
{
  Serial.begin(115200);
  mySUART.begin(115200);
}

void loop()
{
  if (mySUART.available() == 4)
  {
    byte paramOneHbyte = mySUART.read();
    byte paramOneLbyte = mySUART.read();
    int paramOne = (paramOneHbyte << 8) | (paramOneLbyte);
    Serial.println(paramOne);
    //-------------------------------------------------
    byte paramTwoHbyte = mySUART.read();
    byte paramTwoLbyte = mySUART.read();
    int paramTwo = (paramTwoHbyte << 8) | (paramTwoLbyte);
    Serial.println(paramTwo);
    Serial.println("//==========================");
  }
}

4. Press RESET button of ESP. Bring in SM2 (Serial Monitor - 2) of ESP.

5. Press RESET button of MEGA. Bring SM1 of MEGA.

6. Observe the relevant messages on SM1 and SM2 as per following screenshots.


Figure-2: Screenshots for SM1 and SM2

uartNodeMCU.png

Thanks for the replies! I'll see if I can achieve some success and report back.