Example script to log flashing light to influxdb via UDP. See https://blog.haschek.at/smartmeter for more info
#include <ESP8266WiFi.h> | |
#include <WiFiUdp.h> | |
WiFiUDP Udp; | |
const char* ssid = "yourwifiSSID"; | |
const char* password = "yourwifipassword"; | |
const int threshold = 400; //this is the threshold how high the value has to be to be registered as a flash. | |
//400 works great for me since flashes are usually ~600 | |
IPAddress remoteIP(192,168,1,117); // the IP address of your Influxdb server | |
unsigned int remotePort = 8093; // the UDP (!!) port of your Influxdb you set up | |
// how to do that? https://docs.influxdata.com/influxdb/v1.6/supported_protocols/udp/#config-examples | |
int lasthigh = 0; | |
int lastval = 0; | |
unsigned long counter = 0; | |
void setup() { | |
Serial.begin(115200); | |
WifiConnect(); | |
} | |
void loop() { | |
WifiConnect(); | |
int val = analogRead(A0); | |
if(lastval < threshold && val > threshold) | |
{ | |
char replyPacket[] = "strom,loc=zaehler value=1"; | |
Udp.beginPacket(remoteIP, remotePort) | |
Serial.println(Udp.write(replyPacket)); | |
Serial.println(Udp.endPacket()); | |
} | |
lastval = val; | |
delay(10); | |
} | |
void WifiConnect() | |
{ | |
if(WiFi.status() == WL_CONNECTED) | |
{ | |
return; | |
} | |
int count = 0; | |
WiFi.mode(WIFI_AP_STA); | |
WiFi.begin(ssid, password); | |
Serial.println("Reconnecting to wifi"); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
count++; | |
if(count>10){ | |
Serial.println("tried 10 times to connect.. restarting"); | |
WiFi.disconnect(); | |
ESP.restart(); | |
return; | |
} | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println(WiFi.localIP()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment