MEGA CODE:
#include <SoftwareSerial.h>
#define RX_PIN 19 // Arduino RX pin
#define TX_PIN 18// Arduino TX pin
SoftwareSerial espSerial(RX_PIN, TX_PIN); // RX, TX
String wave = "95";
String pulse = "50";
String status = "On";
void setup() {
Serial.begin(115200);
espSerial.begin(115200); // Initialize software serial communication with ESP8266
}
void loop() {
// Construct the message to send to ESP8266
String dataFromArduino = "wave=" + wave + "&pulse=" + pulse + "&status=" + status;
// Send the message over serial to ESP8266
espSerial.println(dataFromArduino);
Serial.println(dataFromArduino);
delay(5000); // Adjust delay according to your needs
}
ESP CODE
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <SoftwareSerial.h>
ESP8266WiFiMulti WiFiMulti;
const char* ssid = "ssid";
const char* password = "password";
#define RX_PIN 3 // GPIO pin to which TX of Arduino is connected
#define TX_PIN 1 // GPIO pin to which RX of Arduino is connected
SoftwareSerial arduinoSerial(RX_PIN, TX_PIN); // RX, TX
void setup() {
Serial.begin(115200);
arduinoSerial.begin(115200); // Initialize software serial communication with Arduino
for (uint8_t t = 4; t > 0; t--) {
arduinoSerial.printf("[SETUP] WAIT %d...\n", t);
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP(ssid, password);
}
void loop() {
if (Serial.available() > 0) {
String dataFromArduino = arduinoSerial.readStringUntil('\n'); // Read data from Arduino until newline character
arduinoSerial.println("Received data from Arduino:");
arduinoSerial.println(dataFromArduino);
// Wait for WiFi connection
if (WiFiMulti.run() == WL_CONNECTED) {
WiFiClient client;
HTTPClient http;
arduinoSerial.print("[HTTP] begin...\n");
String url = "http://192.168.1.39/ESP/get_data.php?" + dataFromArduino;
arduinoSerial.println("URL: " + url);
if (http.begin(client, url)) {
arduinoSerial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been sent and Server response header has been handled
arduinoSerial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at the server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
arduinoSerial.println(payload);
}
} else {
arduinoSerial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
arduinoSerial.println("[HTTP] Unable to connect");
}
} else {
arduinoSerial.println("WiFi not connected");
}
delay(10000);
}
}