Code: Select all
/*
*/
#include <Arduino.h>
#include <HTTPClient.h>
#include <TFT_eSPI.h> // Hardware-specific library
#include <ArduinoJson.h>
#include <WiFiClientSecure.h>
TFT_eSPI tft = TFT_eSPI();
//const char* ssid = "XXXXXX"; // twoje WIFI
//const char* password = "XXXXXX"; // hasło do Wifi
WiFiClientSecure client;
String serverName = "link/read?format=json"; //Direct Link mew-01
unsigned long lastTime = 0;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;
void setup() {
Serial.begin(115200);
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setCursor(70, 40);
tft.setTextFont(4);
//tft.setTextSize(1);
tft.setTextColor(TFT_GREEN,TFT_BLACK);
tft.print("SUPLA ZAMEL");
tft.setCursor(70, 80);
tft.setTextFont(4);
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.print("MEW-1 Display");
tft.setCursor(40, 120);
tft.setTextFont(4);
//tft.setTextSize(1);
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.print("Designed by J.A.M.S.");
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
client.setInsecure();
Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}
void loop() {
//Send an HTTP POST request every 10 minutes
if ((millis() - lastTime) > timerDelay) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
String serverPath = serverName;
// Your Domain name with URL path or IP address with path
http.begin(client, serverPath.c_str());
// Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String json = http.getString();
StaticJsonDocument<300> filter;
filter["phases"][0]["voltage"] = true;
filter["phases"][0]["current"] = true;
filter["phases"][0]["powerFactor"] = true;
filter["phases"][0]["powerActive"] = true;
filter["phases"][0]["phaseAngle"] = true;
filter["phases"][0]["totalForwardActiveEnergy"] = true;
StaticJsonDocument<300> doc;
deserializeJson(doc, json, DeserializationOption::Filter(filter));
//void myFunction1(DynamicJsonDocument doc);
//DynamicJsonDocument myFunction1(1024);
double val = doc["phases"][0]["voltage"];
double val1 = doc["phases"][1]["voltage"];
double val2 = doc["phases"][2]["voltage"];
double val3 = doc["phases"][0]["powerActive"];
double val4 = doc["phases"][1]["powerActive"];
double val5 = doc["phases"][2]["powerActive"];
double val6 = doc["phases"][0]["current"];
double val7 = doc["phases"][1]["current"];
double val8 = doc["phases"][2]["current"];
double val9 = doc["phases"][0]["powerFactor"];
double val10 = doc["phases"][1]["powerFactor"];
double val11 = doc["phases"][2]["powerFactor"];
double val12 = doc["phases"][0]["phaseAngle"];
double val13 = doc["phases"][1]["phaseAngle"];
double val14 = doc["phases"][2]["phaseAngle"];
double val15 = doc["phases"][0]["totalForwardActiveEnergy"];
double val16 = doc["phases"][1]["totalForwardActiveEnergy"];
double val17 = doc["phases"][2]["totalForwardActiveEnergy"];
tft.fillScreen(TFT_BLACK);
tft.setCursor(0, 0);
tft.setTextFont(1);
//tft.setTextSize(1);
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.print("Napiecia:");
tft.setCursor(0, 10);
tft.setTextFont(4);
tft.setTextSize(1);
tft.setTextColor(TFT_RED,TFT_BLACK);
tft.print(val);
tft.print(" v");
tft.setCursor(110, 10);
tft.setTextFont(4);
tft.setTextSize(1);
tft.setTextColor(TFT_GREEN,TFT_BLACK);
tft.print(val1);
tft.print(" v");
tft.setCursor(220, 10);
tft.setTextFont(4);
tft.setTextSize(1);
tft.setTextColor(TFT_CYAN,TFT_BLACK);
tft.print(val2);
tft.print(" v");
//tft.fillScreen(TFT_BLACK);
tft.setCursor(0, 34);
tft.setTextFont(1);
//tft.setTextSize(1);
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.print("Prady:");
tft.setCursor(0, 42);
tft.setTextFont(4);
tft.setTextSize(1);
tft.setTextColor(TFT_RED,TFT_BLACK);
tft.print(val6);
tft.print(" A");
tft.setCursor(110, 42);
tft.setTextFont(4);
tft.setTextSize(1);
tft.setTextColor(TFT_GREEN,TFT_BLACK);
tft.print(val7);
tft.print(" A");
tft.setCursor(220, 42);
tft.setTextFont(4);
tft.setTextSize(1);
tft.setTextColor(TFT_CYAN,TFT_BLACK);
tft.print(val8);
tft.print(" A");
tft.setCursor(0, 66);
tft.setTextFont(1);
//tft.setTextSize(1);
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.print("Moce:");
tft.setCursor(0, 74);
tft.setTextFont(4);
tft.setTextSize(1);
tft.setTextColor(TFT_RED,TFT_BLACK);
tft.print(val3);
tft.print(" w");
tft.setCursor(0, 94);
tft.setTextFont(4);
tft.setTextSize(1);
tft.setTextColor(TFT_GREEN,TFT_BLACK);
tft.print(val4);
tft.print(" w");
tft.setCursor(0, 114);
tft.setTextFont(4);
tft.setTextSize(1);
tft.setTextColor(TFT_CYAN,TFT_BLACK);
tft.print(val5);
tft.print(" w");
tft.setCursor(140, 125);
tft.setTextFont(1);
//tft.setTextSize(1);
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.print("Razem:");
tft.setCursor(200, 114);
tft.setTextFont(4);
tft.setTextSize(1);
tft.setTextColor(TFT_YELLOW,TFT_BLACK);
tft.print(val3 + val4 + val5);
tft.print(" w");
tft.setCursor(0, 141);
tft.setTextFont(1);
//tft.setTextSize(1);
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.print("PF:");
tft.setCursor(20, 136);
tft.setTextFont(2);
//tft.setTextSize(1);
tft.setTextColor(TFT_RED,TFT_BLACK);
tft.print(val9);
tft.setCursor(60, 136);
tft.setTextFont(2);
//tft.setTextSize(1);
tft.setTextColor(TFT_GREEN,TFT_BLACK);
tft.print(val10);
tft.setCursor(100, 136);
tft.setTextFont(2);
//tft.setTextSize(1);
tft.setTextColor(TFT_CYAN,TFT_BLACK);
tft.print(val11);
tft.setCursor(0, 158);
tft.setTextFont(1);
//tft.setTextSize(1);
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.print("Katy faz:");
tft.setCursor(60, 154);
tft.setTextFont(2);
//tft.setTextSize(1);
tft.setTextColor(TFT_RED,TFT_BLACK);
tft.print(val12);
tft.setCursor(120, 154);
tft.setTextFont(2);
//tft.setTextSize(1);
tft.setTextColor(TFT_GREEN,TFT_BLACK);
tft.print(val13);
tft.setCursor(180, 154);
tft.setTextFont(2);
//tft.setTextSize(1);
tft.setTextColor(TFT_CYAN,TFT_BLACK);
tft.print(val14);
tft.setCursor(0, 176);
tft.setTextFont(1);
//tft.setTextSize(1);
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.print("Energia Czynna:");
tft.setCursor(0, 188);
tft.setTextFont(2);
//tft.setTextSize(1);
tft.setTextColor(TFT_RED,TFT_BLACK);
tft.print(val15);
tft.print(" kWh");
tft.setCursor(110, 188);
tft.setTextFont(2);
//tft.setTextSize(1);
tft.setTextColor(TFT_GREEN,TFT_BLACK);
tft.print(val16);
tft.print(" kWh");
tft.setCursor(220, 188);
tft.setTextFont(2);
//tft.setTextSize(1);
tft.setTextColor(TFT_CYAN,TFT_BLACK);
tft.print(val17);
tft.print(" kWh");
tft.setCursor(0, 210);
tft.setTextFont(1);
//tft.setTextSize(1);
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.print("Energia razem:");
tft.setCursor(0, 220);
tft.setTextFont(2);
//tft.setTextSize(1);
tft.setTextColor(TFT_YELLOW,TFT_BLACK);
tft.print(val15 + val16 + val17);
tft.print(" kWh");
Serial.println("Napięcia:");
Serial.print(val);
Serial.println(" V");
Serial.print(val1);
Serial.println(" V");
Serial.print(val2);
Serial.println(" V");
Serial.println(" ");
Serial.println("Moce:");
Serial.print(val3);
Serial.println(" W");
Serial.print(val4);
Serial.println(" W");
Serial.print(val5);
Serial.println(" W");
Serial.print("Razem:");
Serial.print(val3 + val4 + val5);
Serial.println(" W");
Serial.println(" ");
Serial.println("Prądy:");
Serial.print(val6);
Serial.println(" A");
Serial.print(val7);
Serial.println(" A");
Serial.print(val8);
Serial.println(" A");
Serial.println(" ");
Serial.println("PF:");
Serial.println(val9);
Serial.println(val10);
Serial.println(val11);
Serial.println(" ");
Serial.println("Kąty Faz:");
Serial.println(val12);
Serial.println(val13);
Serial.println(val14);
Serial.println(" ");
Serial.println("Energia Czynna Pobrana:");
Serial.println(val15);
Serial.println(val16);
Serial.println(val17);
Serial.print("Razem:");
Serial.print(val15 + val16 + val17);
Serial.println(" kWh");
Serial.println(" ");
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
Code: Select all
20:28:39.089 -> [347492][D][HTTPClient.cpp:303] beginInternal(): protocol: https, host: svr41.supla.org port: 443 url: /xxx
20:28:39.089 -> [347493][D][HTTPClient.cpp:598] sendRequest(): request type: 'GET' redirCount: 0
20:28:39.135 ->
20:28:44.103 -> [352503][I][ssl_client.cpp:112] start_ssl_client(): select returned due to timeout 5000 ms for fd 48
20:28:44.103 -> [352503][E][WiFiClientSecure.cpp:144] connect(): start_ssl_client: -1
20:28:44.103 -> [352507][D][HTTPClient.cpp:1163] connect(): failed connect to svr41.supla.org:443
20:28:44.150 -> [352514][W][HTTPClient.cpp:1483] returnError(): error(-1): connection refused
20:28:44.150 -> Error code: -1
20:28:44.150 -> [352531][D][HTTPClient.cpp:408] disconnect(): tcp is closed
Tu jest ciekawy wątek https://github.com/espressif/arduino-esp32/issues/3722, sugerują że można zrobić licznik dla zebyt wielu returnError(): error(-1): connection refused i resetować esp w razie co...
Dodatkowo miałem problem z
Code: Select all
Guru Meditation Error: Core 1 panic'ed (Unhandled debug exception)
Debug exception reason: Stack canary watchpoint triggered (loopTask)
Code: Select all
StaticJsonDocument<300> filter;
StaticJsonDocument<300> doc;