sml/ESP8266-Stromauslesen.ino

157 lines
2.5 KiB
C++

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoOTA.h>
#include "ph.h"
int httpCode;
int pos = 0;
int datenpos = 0;
bool lesen = false;
ESP8266WebServer server(80);
const byte beginn[] = { 0x1b, 0x1b, 0x1b, 0x1b, 0x01, 0x01, 0x01, 0x01 };
const byte ende[] = { 0x1b, 0x1b, 0x1b, 0x1b, 0x1a };
byte daten[512];
double stand = 0;
double leistung = 0;
void setup()
{
Serial.begin(9600);
WiFi.begin(phSSID, phPasswort);
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
}
ArduinoOTA.setHostname("tutorial");
ArduinoOTA.setPassword("12345");
ArduinoOTA.begin();
server.onNotFound([]()
{
server.send(404, "text/plain", "Die Seite wurde nicht gefunden!");
});
server.on("/", []()
{
server.send(200, "text/plain", "Mahlzeit und willkommen bei Projekt Hirnfrei");
});
server.begin();
}
void loop()
{
ArduinoOTA.handle();
server.handleClient();
WiFiClient client;
HTTPClient http;
if(Serial.available() > 0)
{
byte input = Serial.read();
if(lesen)
{
daten[datenpos] = input;
datenpos++;
if(input == ende[pos])
{
pos++;
if(pos == sizeof(ende))
{
lesen = false;
pos = 0;
standLesen();
leistungLesen();
if(http.begin(client, "http://192.168.178.123:2280/speichern.php?wert="+String(stand)+";"+String(leistung)))
{
httpCode = http.GET();
}
}
}
else pos = 0;
}
if(!lesen)
{
if(input == beginn[pos])
{
pos++;
if(pos == sizeof(beginn))
{
lesen = true;
pos = 0;
}
}
else pos = 0;
}
}
}
void standLesen()
{
byte seq[] = { 0x01, 0x00, 0x01, 0x08, 0x00, 0xff };
int index = 0;
for(int i=0;i<sizeof(daten);i++)
{
if(daten[i] == seq[index])
{
index++;
if(index == sizeof(seq))
{
i += 8;
unsigned long s = ((long)daten[i+1] << 24 | (long)daten[i+2] << 16 | (long)daten[i+3] << 8 | (long)daten[i+4]);
stand = (double)s/10000;
}
}
else index = 0;
}
}
void leistungLesen()
{
byte seq[] = { 0x01, 0x00, 0x10, 0x07, 0x00, 0xff };
int index = 0;
for(int i=0;i<sizeof(daten);i++)
{
if(daten[i] == seq[index])
{
index++;
if(index == sizeof(seq))
{
i += 7;
unsigned long s = ((long)daten[i+1] << 8 | (long)daten[i+2]);
leistung = (double)s/10;
}
}
else index = 0;
}
}