Skip to content
Snippets Groups Projects
Commit 7add2ea0 authored by Egmont Schreiter's avatar Egmont Schreiter
Browse files

Quelltext und Beschreibung erstellt

parent 8204b9f0
Branches
No related tags found
No related merge requests found
# Arduino_Energie_Thingspeak
Software zum Übertragen von Stromzählerwerten (Blinken einer LED proportional zur Leistung) an einen IoT Server
\ No newline at end of file
Software zum Übertragen von Stromzählerwerten (Blinken einer LED proportional zur Leistung) an einen IoT Server
An einem ESP32 oder ESP8266 wird ein IR Empfänger angeschlossen (z.B. CNY70). Ein Eingang wird mit Pullup aktiviert, so dass ein IR Blinken den Eingang auf Low schaltet und sonst bleibt er high. Die Zeit zwischen den Blinken wird gemessen und über die Blinkrate von 1000x Blinken pro 1 kWh umgerechnet. Aller 30 Sekunden wird versucht den aktuellen Wert der Leistung zu einem IoT Server (hier Thingspeak) hochzuladen.
* In der Software kann gewählt werden, ob ESP32 oder ESP8266 genutzt wird.
* Es können mehrere WLANs konfiguriert werden. Es kann in einer Datei passworte.h im selben Verzeichnis Passworte und API-Keys hinterlegt werden, um sie nicht im Quelltext zu haben, wenn man den Quelltext jemanden zeigt.
Lizenz: GNU GPLv3
// https://tttapa.github.io/ESP8266/Chap13%20-%20OTA.html
// ESP32 oder ESP8266?
#define ESP32
// Boardverwaltung:
// ESP8266: NodeMCU 1.0 (ESP-12E Module)
// ESP32: NodeMCU-32S
//
// Werkzeuge --> Bibliotheksverwaltung: Thingspeak
#ifdef ESP32
#include <WiFi.h>
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
#else
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
#endif
#include <ThingSpeak.h>
#if __has_include ("passworte.h")
#include "passworte.h"
#else
// define here your WLAN and thingspeak.com credentials for later use,
// or put them in the file "passworte.h" in the same directory
#define PASSWORTE_myChannelNumber 0
#define PASSWORTE_myWriteAPIKey "ABC"
// add all yout WLAN credentials in one line
#define PASSWORTE_Wifi wifiMulti.addAP("WLAN-SSID", "password"); wifiMulti.addAP("FreeInet", "");
#endif
/*
- Zählerstand ermitteln
- WLAN verbinden (mehrere WLANs eintragbar)
- aller 30s per URL Zählerstand an thingspeak melden
- bei verlorener Verbindung: Zähler intern weiterzählen, versuchen WLAN zu verbinden und
- für ESP32 und ESP8266 nutzbar?
- mehrere Zähleingänge?
- todo: previousTime_xyz timer overflow?
*/
unsigned long myChannelNumber = PASSWORTE_myChannelNumber; // Replace the 0 with your channel number
const char * myWriteAPIKey = PASSWORTE_myWriteAPIKey; // Paste your ThingSpeak Write API Key between the quotes
// https://arduino-esp8266.readthedocs.io/en/2.6.3/
// Impulse vom Stromzähler zählen
// https://randomnerdtutorials.com/interrupts-timers-esp8266-arduino-ide-nodemcu/
//
WiFiClient client;
// Startvalue of Counter kWh
unsigned long counter_electricity_Wh = 35408600UL; // Counter der Impulse = 1000-tel kWh!!
float rate = 0;
unsigned long previousTime_led = millis();
unsigned long interval_led = 200;
unsigned long previousTime_pulse = millis();
unsigned long interval_pulse = 2000;
unsigned long previousTime_thingspeak = millis();
unsigned long interval_thingspeak = 30 * 1000;
unsigned long last_pulse = 0;
unsigned long last_pulse_before_thingspeak = 0;
const byte led = 2;
unsigned int new_impulse = 0;
// ISR rutine to count pulses of electricity meter
ICACHE_RAM_ATTR void ISR_count() {
new_impulse++;
last_pulse = millis();
// blink LED: set LED output and time
//Serial.print('P');
digitalWrite(led, 0); // LED an
previousTime_led = millis();
interval_led = 150;
} // ISR_count()
void setup() {
Serial.begin(115200); // Start the Serial communication to send messages to the computer
delay(10);
Serial.println('\n');
attachInterrupt(digitalPinToInterrupt(12), ISR_count, FALLING);
// The ESP8266 supports interrupts in any GPIO, except GPIO16.
//wifiMulti.addAP("wlan ssid", "password"); // add Wi-Fi networks you want to connect to
//wifiMulti.addAP("Freifunk", "");
// inlcude wifi credentials from #define
PASSWORTE_Wifi
Serial.println("Connecting ...");
int i = 0;
while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(250);
Serial.print('.');
}
Serial.println('\n');
Serial.print("Connected to ");
Serial.println(WiFi.SSID()); // Tell us what network we're connected to
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP to the computer
pinMode(led, OUTPUT);
digitalWrite(led, 1); // LED off
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
ThingSpeak.begin(client);
}
void loop() {
//ArduinoOTA.handle();
// switch LED off, after short blink
unsigned long diff = millis() - previousTime_led;
if (diff > interval_led) {
digitalWrite(led, 1); // Change the state of the LED to off
interval_led = 2000;
Serial.print(digitalRead(12));
Serial.print(digitalRead(13));
Serial.print(digitalRead(14));
Serial.print(digitalRead(15));
Serial.print(' ');
Serial.println(new_impulse);
previousTime_led += diff;
}
// periodisch aller x Sekunden versuchen Thingspeak-Server zu erreichen
diff = millis() - previousTime_thingspeak;
if (wifiMulti.run() == WL_CONNECTED && \
new_impulse > 0 && \
diff > interval_thingspeak) { // Only if the Wi-Fi is connected and new impulse since last update
// Thingspeak code Anfang
// Feld 1 aktuelle Watt = kWh/Zeit
// Feld 2 Counter = kWh
// Feld 3 0 oder 1 von Pin Bewegungsmelder
unsigned long diff_last = last_pulse - last_pulse_before_thingspeak;
if (diff_last > 0 && new_impulse > 0) {
rate = (1000 * 60 * 60 * new_impulse) / (diff_last); // ergibt Watt
Serial.print("Thingspeak: ");
}
else {
rate = 0;
Serial.print("divide by zero! ");
}
counter_electricity_Wh += new_impulse;
Serial.print("Thingspeak: ");
Serial.print(rate);
Serial.print(" ,");
Serial.print(counter_electricity_Wh / 1000);
Serial.print(".");
Serial.print(counter_electricity_Wh % 1000);
Serial.print(" ,");
Serial.println(digitalRead(13));
ThingSpeak.setField(1, rate);
ThingSpeak.setField(2, (float)(counter_electricity_Wh / 1000.0));
ThingSpeak.setField(3, digitalRead(13));
int err_status = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (err_status == 200) { // if update to thingspeak was successful
Serial.println("Thinkspeak erfolgreich aktualisiert!");
new_impulse = 0; // reset counter of pulses
last_pulse_before_thingspeak = last_pulse; // store time of last pulse
}
// Thingspeak Code Ende
Serial.println('\n');
Serial.print("Connected to ");
Serial.println(WiFi.SSID()); // Tell us what network we're connected to
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266/ESP32 to the computer
previousTime_thingspeak += diff;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment