Transfering data from one arduino to another over wifi

I have one arduino measuring the power coming in (and sometimes out) of my home. Works fine.

Now I'd like to transfer the power reading to another arduino, that can make use of the extra power. I want to go through wifi, because the two are not at the same place in my house, and I don't want to run miles of electrical cable...

The first arduino has a simple web site running. A major point of entry, that returns a full page giving all the info about my power.

I've set up a second entry point, that just gives the overall power, a simple number. Here is the code for that :

  // starting the small web server to send info to the user
  server.on("/", handleRoot);

  server.on("/inline", []() {
    server.send(200, "text/plain", "this works as well");
  });

  server.on("/power", []() {
    server.send(200, "text/plain", String(elec.power));
  });

works OK. I can test it but hitting it with a simple pc browser :

Now is where I get stuck.

I'm trying to read that simple piece of data from another arduino. I lifted the code from the WifiClientBasic example.

The connection works, but client.available() times out. Next time round, it can't connect :

20:52:03.540 -> WiFi connected

20:52:03.540 -> IP address:

20:52:03.540 -> 192.168.0.56

20:52:04.054 -> Connecting to 192.168.0.61

20:52:04.760 ->

20:52:04.760 -> connected to host

20:52:05.755 -> client.available() timed out

20:52:05.787 -> Closing connection.

20:52:05.787 -> Waiting 5 seconds before restarting...

20:52:10.761 -> Connecting to 192.168.0.61

Here is the full code :

/*
*  This sketch sends a message to a TCP server
*
*/

#include <WiFi.h>
#include <WiFiMulti.h>

WiFiMulti WiFiMulti;

void setup() {
 Serial.begin(115200);
 delay(10);

 // We start by connecting to a WiFi network
 WiFiMulti.addAP("routeur_bureau", "2019611956");

 Serial.println();
 Serial.println();
 Serial.print("Waiting for WiFi... ");

 while (WiFiMulti.run() != WL_CONNECTED) {
   Serial.print(".");
   delay(500);
 }

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

 delay(500);
}


void loop() {
 //    const uint16_t port = 80;
 //    const char * host = "192.168.1.1"; // ip or dns
 const uint16_t port = 80;
 const char* host = "192.168.0.61";  // ip or dns

 Serial.print("Connecting to ");
 Serial.println(host);

 // Use WiFiClient class to create TCP connections
 WiFiClient client;

 if (!client.connect(host, port)) {
   Serial.println("Connection failed.");
   Serial.println("Waiting 5 seconds before retrying...");
   delay(5000);
   return;
 }
 Serial.println();
 Serial.println("connected to host");

 // This will send a request to the server
 //uncomment this line to send an arbitrary string to the server
 //client.print("Send this data to the server");
 //uncomment this line to send a basic document request to the server
 client.print("GET /power HTTP/1.1\n\n");

 int maxloops = 0;

 //wait for the server's reply to become available
 while (!client.available() && maxloops < 1000) {
   maxloops++;
   delay(1);  //delay 1 msec
 }
 if (client.available() > 0) {
   //read back one line from the server
   String line = client.readStringUntil('\r');
   Serial.println(line);
 } else {
   Serial.println("client.available() timed out ");
 }

 Serial.println("Closing connection.");
 client.stop();

 Serial.println("Waiting 5 seconds before restarting...");
 delay(5000);
}

What am I doing wrong ????

Your code looks like if you were using two ESP8266 or ESP32-microcontroller-boards but not original Arduino-Uno or something like that.

you could use UDP-messages or TCP-messages.

best regards Stefan

Maybe this will help:

1 Like

Right you are, I am using two esp-32s (Wemos DI mini esp32).

Is that a problem ? I'm new to this game, but I thought that arduino cc code was platform-independent (once you've found the board description).

I'm not sure how to do udp or tcp transfers, I'll look it up.

Thanks,

JP

thanks, I'll have a (long) look.

1 Like

also have a look at ESP-NOW which is a direct device to device using MAC addresses
how far apart are the devices? are there walls between them? if so what type of walls? walls tend to attenuate the signals - too many and no signal is received

thanks, that looks interesting, I'll give it a go.

I'm not sure if they are close enough. But the one farther away communicates fine with the router in my office, so ...

Trial will tell.

OK, by searching other code examples that seem to work, I found a way around, though I have no idea why this magic incantation works.

to connect to the other arduino, I used the following call :

client.print("GET /power HTTP/1.1\n\n");

Yep, doesn't work. What does work is the following :

client.print(String("GET /power HTTP/1.1\r\n") + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");

I suppose we have to close the connection before the other guys is ready to talk to us. Or something like that.

Thanks for the help

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