Hier meine geänderte Version für den Shelly 1PM und zusätzlich den Shelly 3EM. Beim 3EM wird die aktuelle Gesamtleistung und die ins Netz eingespeiste Tagesleistung angezeigt. Ganz wichtig ist beim kompilieren vorher unter Arduino IDE bei Werkzeuge den MMU Wert auf(...Heap (shared) zu ändern. Ich hoffe es hilft einigen.
C
#include <Wire.h>
#include "SSD1306Wire.h"
#include "OLEDDisplayUi.h"
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>
#include <Arduino_JSON.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
// -------------------------------
const char* ssid = "Hier WLAN eintragen";
const char* password = "Hier WLAN Passwort eintragen";
const String shelly_server = "Hier Shelly Gerät1 Server eintragen";
const String shelly_server1 = "Hier Shelly Gerät2 IP-Adresse (http://192.168.178.2) eintragen";
const String shelly_auth_key = "Hier Shelly Auth Key eintragen";
const String shelly_device_id = "Hier Shelly Gerät1 ID eintragen";
const String shelly_device_id1 = "Hier Shelly Gerät2 ID eintragen";
// -------------------------------
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");
SSD1306Wire display(0x3c, D2, D1);
OLEDDisplayUi ui ( &display );
void setup() {
Serial.begin(115200);
delay(4000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { //Check for the connection
delay(1000);
Serial.print(".");
}
Serial.println("Connected to the WiFi network");
timeClient.begin();
display.init();
}
void loop() {
if(WiFi.status()== WL_CONNECTED){
display.clear();
display.setFont(ArialMT_Plain_16);
display.drawString(0, 0, currentConsumption() + " W PV");
display.drawString(0, 17, totalpower() + " W total");
display.drawString(0, 35, dayreturned() + " Wh/24 ret");
display.setFont(ArialMT_Plain_10);
display.drawString(0, 53, "Heute: " + dayConsumption());
display.display();
}
delay(10000);
}
String dayConsumption() {
JSONVar response = request("/statistics/relay/consumption?channel=0&id=" + shelly_device_id);
String unit = JSON.stringify(response["data"]["units"]["consumption"]);
unit.remove(0, 1);
unit.remove(unit.length() -1, 1);
const String consumption = JSON.stringify(response["data"]["total"]) + " " + unit;
Serial.println(consumption);
return consumption;
}
String totalpower() {
JSONVar response2 = request2("/status");
Serial.print("totalpower: ");
Serial.println(response2);
const String consumption2 = JSON.stringify(response2["total_power"]);
return consumption2;
}
String dayreturned() {
JSONVar response1 = request("/statistics/emeter/consumption?channel=2&id=" + shelly_device_id1);
Serial.println("dayreturned");
Serial.println(response1);
const String consumption1 = JSON.stringify(response1["data"]["total_r"]);
return consumption1;
}
String currentConsumption() {
JSONVar response = request("/device/status?channel=0&id=" + shelly_device_id);
const String consumption = JSON.stringify(response["data"]["device_status"]["meters"][0]["power"]);
return consumption;
}
JSONVar request(String path) {
WiFiClientSecure client;
HTTPClient http;
client.setInsecure();
http.begin(client, shelly_server + path);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.POST("&auth_key=" + shelly_auth_key + "&date_range=day&date_from=" + currenDate());
const JSONVar responseObject = JSON.parse(http.getString());
http.end();
return responseObject;
}
JSONVar request2(String path) {
WiFiClient client;
HTTPClient http;
//client.setInsecure();
http.begin(client, shelly_server1 + path);
//http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.POST("");
const JSONVar responseObject = JSON.parse(http.getString());
http.end();
return responseObject;
}
String currenDate() {
timeClient.update();
time_t epochTime = timeClient.getEpochTime();
struct tm *ptm = gmtime ((time_t *)&epochTime);
int monthDay = ptm->tm_mday;
int currentMonth = ptm->tm_mon+1;
int currentYear = ptm->tm_year+1900;
return String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay);
}
Alles anzeigen