Need help with separating/combining code Arduino Mega and Esp8266

I have 2 working codes one for arduino and other for esp8266.
My question is how can i combine these two codes,or which part to upload to esp and other to arduino so that they can work together.
Goal is to send telegram alert when Gas threshold exceeded limit

Arduino

#include <WiFiEsp.h>
#include <WiFiEspServer.h>
#include <DHT.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24Network.h>

#define DHTPIN 2       
#define DHTTYPE DHT11   
#define MQ2PIN A0       
#define LED_PIN 3

char ssid[] = "xxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxx";
int status = WL_IDLE_STATUS;

WiFiEspServer server(80);
WiFiEspClient client;

int threshold = 250; 
bool alert = false;


DHT dht(DHTPIN, DHTTYPE);

struct MyData {
  byte h;
  byte t;
};
MyData data;

struct MyData2 {
  byte humi;
  byte temp;
};
MyData2 data2;

RF24 radio(7, 8);
RF24Network network(radio);

const uint16_t this_node = 00;
const uint16_t node01 = 01;
const uint16_t node02 = 02;

void setup() {
  Serial.begin(9600);
  Serial1.begin(115200);
  pinMode(MQ2PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
  dht.begin();
  WiFi.init(&Serial1);

  radio.begin();
  network.begin(/*channel*/ 90, /*node address*/ this_node);

  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true);
  }

  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(3000);
  }

  server.begin();
}

void loop() {
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();
  float readGas = analogRead(MQ2PIN);
  network.update();

Node1();
  delay(5000);
    WiFiEspClient client = server.available();
    if (client) {
      Serial.println("New client");
      client.println("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
      client.println("<html><body>");
      //client.println("<meta http-equiv='refresh' content='7'>");
      client.println("<button id='refreshButton' onclick='location.reload();'>Refresh</button>");
      client.println("<div style='text-align: center;'>");

      client.println("<h1>MASTER 00</h1>");
      if (temperature > 40) {
      client.print("<p>Temperature: <span style='color: red;'>");
} else {
      client.print("<p>Temperature: ");
}
      client.print(temperature);
      client.println(" C</span></p>");
      client.print("Humidity: ");
      client.print(humidity);
      client.println("%</p>");
      client.print("<p>Smoke Value: ");
      client.print(readGas);
      if (readGas > threshold) {
        client.println("<p><span style='color: red;'>Alert: Gas threshold exceeded!</span></p>");
        
digitalWrite(LED_PIN, HIGH); 
      } else {
        digitalWrite(LED_PIN, LOW); 
      }
      client.print("<hr>");

      client.print("<h1>NODE 01</h1>");
if (temperature > 40) {
      client.print("<p>Temperature: <span style='color: red;'>");
} else {
      client.print("<p>Temperature: ");
}
      client.print(data.t);
      client.println(" C</span></p>");
      client.println("Humidity: ");
      client.println(data.h);
      client.println("%</p>");
      client.print("<hr>");

      client.print("<h1>NODE 02</h1>");
if (temperature > 40) {
      client.print("<p>Temperature: <span style='color: red;'>");
} else {
      client.print("<p>Temperature: ");
}
      client.print(data2.temp);
      client.println(" C</span></p>");
      client.println("Humidity: ");
      client.println(data2.humi);
      client.println("%</p>");
      client.println("</div>");
      client.print("<hr>");
      client.print("</body></html>");
      delay(1);
      client.stop();
      Serial.println("Client disconnected");
    }
  }


void Node1() {

    const char text[] = "A";
    RF24NetworkHeader header(node01);
    bool ok = network.write(header, &text, sizeof(text));
    if (ok)
      Serial.println("Node 01 data sent.");
    else
      Serial.println("Failed to send Node 01 data.");
    while (network.available()) {
      RF24NetworkHeader header;
      network.read(header, &data, sizeof(MyData));
    Node2();
    }
  }


void Node2() {
    const char text[] = "B";
    RF24NetworkHeader header(node02);
    bool ok = network.write(header, &text, sizeof(text));
    if (ok)
      Serial.println("Node 02 data sent.");
    else
      Serial.println("Failed to send Node 02 data.");
    while (network.available()) {
      RF24NetworkHeader header;
      network.read(header, &data2, sizeof(MyData2));
    }
  }

Esp8266

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>

const char* ssid = "xxxxxxxxxxxxxxxxxx";
const char* password = "xxxxxxxxxxxxxx";


#define BOTtoken "xxxxxxxxx:xxxxxxxxxxxxxx"  

#define CHAT_ID "xxxxxxxxxxxxxxxxx"

X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

void setup() {
  Serial.begin(115200);
  configTime(0, 0, "pool.ntp.org");     
  client.setTrustAnchors(&cert);

  Serial.print("Connecting Wifi: ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

}

void loop() {
  
    
    bot.sendMessage(CHAT_ID, "Bot started up", "");
    //delay(8000);
  }

It seems the codes can't work together, because the ESP8266 code is standalone, but the Mega code is intended to use with EspWiFi module on Serial1

1 Like

i am using esp as arduino mega wifi shield , maybe upload second script to esp and when event triggers send command(over arduino) to esp to run part of code to send message

But your second code is for standalone ESP8266, not for WiFi shield

Alternative? any ideas?

Put the sensor on the esp8266 and add the required code to the esp8266. Put the Arduino in a drawer for a future project.

2 Likes

get a makerfriendly board with an ESP8266 (NodeMCU, Wemos D1) or ESP32,
leave away the Arduino Mega
Just program the ESP board.

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