[c] /* Generic ESP8266 Modul sketch_mar27c.ino aus BasicHTTPClient.ino Created on: 27.03.2024 liest den Status aus Shelly 3EM, filtert nach "POWER",ermittelt den Strom eines Moduls, schaltet die notwendige/zulässige Anzahl Module zu oder ab, und funkt das Ergebnis an einen ESP01 zur Anzeige am Monitor. */ #include #include #include #include #include ESP8266WiFiMulti WiFiMulti; // REPLACE WITH RECEIVER MAC Address (ESP01:E1) //uint8_t broadcastAddress[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; //uint8_t broadcastAddress[] = { 0xDC, 0x4F, 0x22, 0x26, 0x28, 0xA1 };//E2 //uint8_t broadcastAddress[] = { 0xC8, 0x2B, 0x96, 0x20, 0x85, 0x33 };//E5 //uint8_t broadcastAddress[] = { 0x48, 0x3F, 0xDA, 0x4A, 0x27, 0x27 };//Eb uint8_t broadcastAddress[] = { 0xC8, 0xC9, 0xA3, 0x56, 0x46, 0xCB }; //A //uint8_t broadcastAddress[] = { 0xDC, 0x4F, 0x22, 0x26, 0x02, 0xF4 };//E3 //******** //01 11 21 22 String Verteil[11] = { "0000", "1000", "0100", "0010", "0001", "0110", "1110", "0011", "1011", "0111", "1111" }; // 00 10 11 21 22 23 33 34 44 54 55 // 0 1 1 2 2 3 3 4 4 5 5 //******** typedef struct struct_message { char d[60]; } struct_message; struct_message myData; unsigned long lastTime = 0; unsigned long timerDelay = 1000; // send readings timer String payload = ""; String x1; String Ausgabe; String Strom = ""; int Summe; int Schwelle; int Laststufe = 0; int Limit; float Ampere; int s; String inputString; char Puffer[60]; WiFiClient client; HTTPClient http; //*************** Setup **************** void setup() { pinMode(12, OUTPUT); pinMode(13, OUTPUT); pinMode(14, OUTPUT); pinMode(16, OUTPUT); WiFi.mode(WIFI_STA); WiFiMulti.addAP("shellyem3-3494547B8D61", ""); Serial.begin(115200); // Init Serial Monitor Serial.println(""); Serial.println(WiFi.macAddress()); if (esp_now_init() != 0) // Init ESP-NOW { Serial.println("Error initializing ESP-NOW"); return; } esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER); uint8_t result = esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_CONTROLLER, 0, NULL, 0); if (result != 0) { Serial.println("Failed to add peer"); } else { Serial.println("Peer ok"); } esp_now_register_send_cb(OnDataSent); // we will register for Send CB } //************** Loop ***************** void loop() { if ((millis() - lastTime) > timerDelay) { lastTime = millis(); if ((WiFiMulti.run() == WL_CONNECTED)) { if (http.begin(client, "http://192.168.33.1/status")) { int httpCode = http.GET(); // start connection and send HTTP header if (httpCode > 0) { if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { LeseShelly(); Auswertung(); Senden(); } } } } } } //**************** Auswertung *********************** void Auswertung() { Strom = analogRead(A0); Ampere = ((Strom.toInt() - 512) / 512.000 * 60); // ADC-Ausgabe umgerechnet in Ampere if (Ampere < .01) { Ampere = .01; } Limit = 2 * 16 / Ampere; if (Limit > 10) { Limit = 10; } Schwelle = (Schwelle * 3 + Summe) / 4; if (Schwelle <= 0 and Laststufe > 0) { Laststufe -= 1; } if (Schwelle >= 200 and Laststufe < 10) { Laststufe += 1; } if (Laststufe > Limit) { Laststufe = Limit; } //Schalten: (LOW: MODUL abgeklemmt, HIGH: MODUL zugeschaltet) if (Verteil[Laststufe].substring(0, 1) == "0") { digitalWrite(12, LOW); } if (Verteil[Laststufe].substring(1, 2) == "0") { digitalWrite(13, LOW); } if (Verteil[Laststufe].substring(2, 3) == "0") { digitalWrite(14, LOW); } if (Verteil[Laststufe].substring(3, 4) == "0") { digitalWrite(16, LOW); } if (Verteil[Laststufe].substring(0, 1) != "0") { digitalWrite(12, HIGH); } if (Verteil[Laststufe].substring(1, 2) != "0") { digitalWrite(13, HIGH); } if (Verteil[Laststufe].substring(2, 3) != "0") { digitalWrite(14, HIGH); } if (Verteil[Laststufe].substring(3, 4) != "0") { digitalWrite(16, HIGH); } } //***************** LeseShelly ******************** void LeseShelly() { String payload = http.getString(); Summe = 0; Ausgabe = ""; for (s = 400; s < 750; s++) // zwischen Stelle 400 und 750 stehen die Werte für die 3 Phasen { String A = payload.substring(s, s + 5); if (A == "power") { s = s + 7; //************************** x1 = payload.substring(s, s + 10); Ausgabe += " "; Ausgabe += x1.toInt(); Summe = Summe + x1.toInt(); s = s + 10; //************************** } } } //***************** Senden ******************** void Senden() { Ausgabe = Verteil[Laststufe] + " " + Strom + " " + Ampere + " " + Limit + " " + Summe + " " + Schwelle + Ausgabe + " "; Ausgabe.toCharArray(Puffer, 60); esp_now_send(broadcastAddress, (uint8_t *)&Puffer, sizeof(Puffer)); Serial.println(Puffer); } //***************** OnDataSent ******************** void OnDataSent(uint8_t *broadcastAddress, uint8_t sendStatus) // Callback when data is sent { if (sendStatus == 0) { Serial.println("Last Packet Send Status: Delivery success"); } else { Serial.println("Last Packet Send Status: Delivery fail"); } } [/c]