Hi so I'm doing a project where I connect a raspberry Pi to a Arduino MEGA rotodyn (so with built-in ESP8266). The goal is send some sensor data from the Arduino to the Raspberrry Pi over wifi. Now I already set up a web API on my Raspberry and made that whole thing work. I can send data from the buit-in ESP8266 to the Raspberry but i don't know how to send it directly from the Arduino MEGA module. I read i could first send my sensor data from the Arduino to the ESP8266 and then transfer it over. But i can't get it to send data from Arduino MEGA to the buit-in ESP8266. Also what switches should be on when the system is working with the Arduino and ESP. I thought either just 1 and 2 or 1,2,3 and 4. Can Anyone help please?
Another problem I have is that the http get only works 1 time. I put this function in the loop, but i won't do it muliple times. If anyone can help with this it would be greatly appriciated.
Thanks in advance.
Code for built-in ESP8266:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
const char* ssid = "bureau"; // Wifi name
const char* password = "11afafafafafafafafafafafaf"; // Wifi Password
const char* host = "192.168.0.196";
int port = 5001;
String getal1 = "10";
String getal2 = "20";
// Use WiFiClient to create a TCP connection
WiFiClient client;
void setup() {
Serial.begin(115200);
Serial1.begin(115200);
delay(100);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Connect to the server
Serial.print("Connecting to server: ");
Serial.println(host);
if (!client.connect(host, port)) {
Serial.println("Connection failed.");
return;
}
}
void loop() {
Serial.println("getal1_1 = " + String(getal1));
if (Serial.available()) {
String data1 = Serial.readStringUntil('\n');
Serial.print("Received from Arduino: ");
getal1 = data1;;
delay(500);
Serial.println("getal1_2 = " + String(getal1));
}
Serial.println("getal2_1 = " + String(getal2));
if (Serial1.available()) {
String data2 = Serial.readStringUntil('\n');
Serial.print("Received from Arduino: ");
getal2 = data2;
delay(500);
Serial.println("getal2_2 = " + String(getal2));
}
// Make an HTTP request
Serial.println("Sending HTTP request...");
client.print("GET /Test?waarde1=" + getal1 + "&waarde2=" + getal2 + " HTTP/1.1\r\n");
client.print("Host: ");
client.print(host);
client.print("\r\n");
client.print("Connection: close\r\n");
client.print("\r\n");
// Wait for the server to respond
while (!client.available()) {
delay(100);
}
// Read the response into a string variable
String response = "";
while (client.available()) {
response += client.readString();
}
Serial.println("Response:");
Serial.println(response);
delay(60000);
}
Code for the Arduino MEGA module:
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial1.begin(115200);
Serial2.begin(115200);
Serial3.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print("12");
delay(100);
Serial1.print("23");
delay(100);
Serial2.print("34");
delay(100);
Serial3.print("45");
delay(100);
}
and here is the output on the Raspberry.
This is the Serial monitor when swiches 1,2,3 and 4 are on.
This is the Serial monitor when swiches 5 and 6 are on.