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 ????