Quellcode hinzugefügt

This commit is contained in:
Dany Thinnes 2024-05-22 12:05:08 +02:00
parent c9389cb986
commit 49d48f0669
2 changed files with 175 additions and 0 deletions

156
ESP8266-Stromauslesen.ino Normal file
View File

@ -0,0 +1,156 @@
#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;
}
}

19
Werte.txt Normal file
View File

@ -0,0 +1,19 @@
Zählerstand
7 1 0 1 8 0 ff
62 82
1
62 1e
52 ff
55 12 fa d9 3f
1
Leistung
7 1 0 10 7 0 ff
1
1
62 1b
52 0
53 a 8e 1