Arduino MEGA rotodyn connected with Raspberry Pi over wifi

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.

seems RPi handles data wrong or it is right received but you store or read out it wrong from file.

it isn't cause when you look at the output from the Serial monitor you can see that it doens't read anything on the serial bus. So the values transfered to the RPI are the ones declared in the start of the program.

show diagram where all parts are presented.

i see A_Mega sends proper, i see Serial Monitor from side of Serial shows proper, only not proper is text on the screen of RPi, so decision is simple - look post#2.

image
Look at this part of the code above. If the ESP8266 was reading something at Serial.available, then it would print out "Recieved from Arduino" and the number gotten from the Arduino. Now when you look at the picture below (also in the first post) It doens't show this message on the Serial monitor. So i must conclude that its not reading anything from the Serial bus. Or am I missing something? I mean maybe i'm stupid but i think if the problem was the RPI it would still show this message.

show diagram where i can see how you connect each device together.

how they are connected?

#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 = "101";
String getal2 = "202";
// 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 = " + getal));
  if (Serial.available() > 0) {
    getal1 = Serial.readString();
    Serial.print("Received from Arduino: ");
    getal1.trim();
    delay(500);
    Serial.println("getal1_2 = " + getal1);
  }

  Serial.println("getal2_1 = " + getal2);
  if (Serial1.available() > 0) {
    getal2 = Serial1.readString();
    getal2.trim();
    Serial.print("Received from Arduino: ");
    Serial.println("getal2_2 = " + getal2);
    delay(500);
  }

  // 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);
}

It's An Arduino rotodyn so they are built in 1 module

I can try this code tomorrow thanks.

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