Hi All,
I'm trying to get data from the HomeWizard P1 monitor. I'm able to read the data by visiting
http://{IP_ADDRESS}/api/v1/data
as noted in the documentation of HomeWizard (Getting Started — HomeWizard Energy API documentation)
So i'm connecting to HomeWizard using the following code;
if (!HTTPclient.connect("192.168.10.208", 80)) {
Serial.println(F("Connection failed"));
return;
}
Serial.println(F("Connected!"));
HTTPclient.println(F("GET /api/v1/data HTTP/1.1"));
HTTPclient.println(F("Host: 192.168.10.208"));
HTTPclient.println(F("Connection: keep-alive"));
delay(1000);
if (HTTPclient.println() == 0) {
Serial.println(HTTPclient.println());
Serial.println(F("Failed to send request"));
HTTPclient.stop();
return;
}
// Check HTTP response
char status[32] = {0};
HTTPclient.readBytesUntil('\r', status, sizeof(status));
if (strcmp(status + 9, "200 OK") != 0) {
Serial.print(F("Unexpected response: "));
Serial.println(status);
HTTPclient.stop();
return;
}
// Skip HTTP headers
char endOfHeaders[] = "\r\n\r\n";
if (!HTTPclient.find(endOfHeaders)) {
Serial.println(F("Invalid response"));
HTTPclient.stop();
return;
}
DynamicJsonDocument doc(2048);
// Parse JSON object
DeserializationError error = deserializeJson(doc, HTTPclient);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
HTTPclient.stop();
return;
}
HTTPclient.stop();
Then i'm able to retrieve data. BUT mostly only once or twice, then my HomeWizard is unreachable. (pinging to the device stops working, and even the ring on the HomeWizard P1 monitor turns from green to red)
Sometimes i'm briefly getting a 408 timeout, and then it keeps saying "Connection failed" (because the device is offline)
After reconnecting it to my netwerk, or reconnecting it to my P1 port is directly starts working again. Until i request data with my Arduino Mega again over HTTP GET.
However when i request data with HTTP GET using postman, or just by loading the webpage itself it remains online, and there is no problem.
Then i build a PHP script to retrieve the JSON data from that page at a face pace to check for stability. This also works like a charm.
So it seems that something on the Arduino is causing the HomeWizard to crash.
Can someone please assist me in this?