mirror of
https://github.com/michivonah/bbzw-horizon.git
synced 2025-12-22 17:16:27 +01:00
Update firmware.ino
This commit is contained in:
parent
504235bbaf
commit
fb6d9440fe
1 changed files with 58 additions and 48 deletions
|
|
@ -6,15 +6,15 @@
|
||||||
#include <NTPClient.h>
|
#include <NTPClient.h>
|
||||||
|
|
||||||
// WLAN-Zugangsdaten
|
// WLAN-Zugangsdaten
|
||||||
#define SSID ""
|
#define SSID "INF-LAB"
|
||||||
#define PASSWORT ""
|
#define PASSWORT "INF-LAB@BBZW-2024"
|
||||||
|
|
||||||
// API-Konfiguration
|
// API-Konfiguration
|
||||||
#define API_HOST ""
|
#define API_HOST "172.18.14.32"
|
||||||
#define API_PORT 8080
|
#define API_PORT 8080
|
||||||
#define API_ENDPOINT "/sensors/push-data"
|
#define API_ENDPOINT "/sensors/push-data"
|
||||||
#define CLIENT_ID ""
|
#define CLIENT_ID "1.54"
|
||||||
#define API_TOKEN ""
|
#define API_TOKEN "test2"
|
||||||
|
|
||||||
// Sensor & Netzwerk
|
// Sensor & Netzwerk
|
||||||
Bsec iaqSensor;
|
Bsec iaqSensor;
|
||||||
|
|
@ -29,7 +29,20 @@ NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
|
||||||
// Sendeintervall
|
// Sendeintervall
|
||||||
unsigned long sendInterval = 30000;
|
unsigned long sendInterval = 30000;
|
||||||
|
|
||||||
// Werte begrenzen (nur für sensible Werte wie Temp, Feuchte, VOC, Gas)
|
// Fehlercode per LED ausgeben (Morse-artig)
|
||||||
|
void errorBlink(int code) {
|
||||||
|
while (true) {
|
||||||
|
for (int i = 0; i < code; i++) {
|
||||||
|
digitalWrite(LED_BUILTIN, HIGH);
|
||||||
|
delay(150);
|
||||||
|
digitalWrite(LED_BUILTIN, LOW);
|
||||||
|
delay(150);
|
||||||
|
}
|
||||||
|
delay(1000); // Pause zwischen Zyklen
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Werte begrenzen
|
||||||
float clampValue(float val) {
|
float clampValue(float val) {
|
||||||
if (val >= 1000.0) return 999.999;
|
if (val >= 1000.0) return 999.999;
|
||||||
if (val <= -1000.0) return -999.999;
|
if (val <= -1000.0) return -999.999;
|
||||||
|
|
@ -81,24 +94,40 @@ String getTimestamp() {
|
||||||
return String(buf);
|
return String(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
// API-Health-Check
|
||||||
Serial.begin(115200);
|
bool checkApiHealth() {
|
||||||
while (!Serial);
|
HttpClient healthClient = HttpClient(wifi, API_HOST, API_PORT);
|
||||||
|
healthClient.get("/health");
|
||||||
|
int statusCode = healthClient.responseStatusCode();
|
||||||
|
healthClient.stop();
|
||||||
|
return statusCode == 200;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
pinMode(LED_BUILTIN, OUTPUT);
|
||||||
|
digitalWrite(LED_BUILTIN, LOW);
|
||||||
|
|
||||||
|
// WLAN verbinden mit Timeout
|
||||||
WiFi.begin(SSID, PASSWORT);
|
WiFi.begin(SSID, PASSWORT);
|
||||||
Serial.print("Verbinde mit WLAN...");
|
unsigned long startAttemptTime = millis();
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 20000) {
|
||||||
delay(500);
|
delay(500);
|
||||||
Serial.print(".");
|
|
||||||
}
|
}
|
||||||
Serial.println("\nWLAN verbunden!");
|
if (WiFi.status() != WL_CONNECTED) {
|
||||||
|
errorBlink(1); // Fehlercode 1: WLAN-Fehler
|
||||||
|
}
|
||||||
|
|
||||||
|
// API-Healthcheck
|
||||||
|
if (!checkApiHealth()) {
|
||||||
|
errorBlink(4); // Fehlercode 4: API nicht erreichbar
|
||||||
|
}
|
||||||
|
|
||||||
Wire.begin();
|
Wire.begin();
|
||||||
|
|
||||||
// Automatische Adresserkennung
|
// Automatische Sensor-Erkennung
|
||||||
bool sensorFound = false;
|
byte sensorAddress = 0;
|
||||||
byte sensorAddress;
|
|
||||||
byte possibleAddresses[] = {0x76, 0x77};
|
byte possibleAddresses[] = {0x76, 0x77};
|
||||||
|
bool sensorFound = false;
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
byte addr = possibleAddresses[i];
|
byte addr = possibleAddresses[i];
|
||||||
Wire.beginTransmission(addr);
|
Wire.beginTransmission(addr);
|
||||||
|
|
@ -110,18 +139,12 @@ void setup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sensorFound) {
|
if (!sensorFound) {
|
||||||
Serial.println("Kein BME680 gefunden!");
|
errorBlink(2); // Fehlercode 2: Sensor nicht gefunden
|
||||||
while (1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.print("BME680 gefunden bei I2C-Adresse 0x");
|
|
||||||
Serial.println(sensorAddress, HEX);
|
|
||||||
iaqSensor.begin(sensorAddress, Wire);
|
iaqSensor.begin(sensorAddress, Wire);
|
||||||
|
|
||||||
if (iaqSensor.bsecStatus != BSEC_OK) {
|
if (iaqSensor.bsecStatus != BSEC_OK) {
|
||||||
Serial.print("BSEC Status Fehler: ");
|
errorBlink(3); // Fehlercode 3: Sensor-Init fehlgeschlagen
|
||||||
Serial.println(iaqSensor.bsecStatus);
|
|
||||||
while (1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bsec_virtual_sensor_t sensorList[] = {
|
bsec_virtual_sensor_t sensorList[] = {
|
||||||
|
|
@ -137,42 +160,36 @@ void setup() {
|
||||||
while (!timeClient.update()) {
|
while (!timeClient.update()) {
|
||||||
timeClient.forceUpdate();
|
timeClient.forceUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
digitalWrite(LED_BUILTIN, HIGH); // Alles bereit – LED dauerhaft an
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if (iaqSensor.run()) {
|
if (iaqSensor.run()) {
|
||||||
|
// Kurzes LED-Blink zur Messanzeige
|
||||||
|
digitalWrite(LED_BUILTIN, LOW);
|
||||||
|
delay(100);
|
||||||
|
digitalWrite(LED_BUILTIN, HIGH);
|
||||||
|
|
||||||
float temperature = clampValue(iaqSensor.temperature);
|
float temperature = clampValue(iaqSensor.temperature);
|
||||||
float humidity = clampValue(iaqSensor.humidity);
|
float humidity = clampValue(iaqSensor.humidity);
|
||||||
float voc = clampValue(iaqSensor.iaq);
|
float voc = clampValue(iaqSensor.iaq);
|
||||||
float gas = clampValue(iaqSensor.gasResistance / 1000.0);
|
float gas = clampValue(iaqSensor.gasResistance / 1000.0);
|
||||||
float pressure = iaqSensor.pressure / 100.0; // <-- Kein Clamping hier!
|
float pressure = iaqSensor.pressure / 100.0;
|
||||||
|
|
||||||
String timestamp = getTimestamp();
|
String timestamp = getTimestamp();
|
||||||
|
|
||||||
// Debug-Ausgabe
|
|
||||||
Serial.println("Messwerte:");
|
|
||||||
Serial.println("Temp: " + String(temperature));
|
|
||||||
Serial.println("Feuchte: " + String(humidity));
|
|
||||||
Serial.println("Druck: " + String(pressure));
|
|
||||||
Serial.println("VOC/IAQ: " + String(voc));
|
|
||||||
Serial.println("Gas: " + String(gas));
|
|
||||||
Serial.println("Zeit: " + timestamp);
|
|
||||||
|
|
||||||
// JSON erstellen
|
|
||||||
String payload = "{";
|
String payload = "{";
|
||||||
payload += "\"timestamp\": \"" + timestamp + "\",";
|
payload += "\"timestamp\": \"" + timestamp + "\",";
|
||||||
payload += "\"temperature\": " + String(temperature, 3) + ",";
|
payload += "\"temperature\": " + String(temperature, 3) + ",";
|
||||||
payload += "\"humidity\": " + String(humidity, 3) + ",";
|
payload += "\"humidity\": " + String(humidity, 3) + ",";
|
||||||
payload += "\"pressure\": " + String(pressure, 3) + ","; // unverfälscht
|
payload += "\"pressure\": " + String(pressure, 3) + ",";
|
||||||
payload += "\"voc\": " + String(voc, 3) + ",";
|
payload += "\"voc\": " + String(voc, 3) + ",";
|
||||||
payload += "\"gas\": " + String(gas, 3);
|
payload += "\"gas\": " + String(gas, 3);
|
||||||
payload += "}";
|
payload += "}";
|
||||||
|
|
||||||
String fullPath = String(API_ENDPOINT) + "?client=" + CLIENT_ID;
|
String fullPath = String(API_ENDPOINT) + "?client=" + CLIENT_ID;
|
||||||
|
|
||||||
Serial.println("Sende an API:");
|
|
||||||
Serial.println(payload);
|
|
||||||
|
|
||||||
client.beginRequest();
|
client.beginRequest();
|
||||||
client.post(fullPath);
|
client.post(fullPath);
|
||||||
client.sendHeader("Content-Type", "application/json");
|
client.sendHeader("Content-Type", "application/json");
|
||||||
|
|
@ -182,15 +199,8 @@ void loop() {
|
||||||
client.print(payload);
|
client.print(payload);
|
||||||
client.endRequest();
|
client.endRequest();
|
||||||
|
|
||||||
int statusCode = client.responseStatusCode();
|
client.responseStatusCode();
|
||||||
String response = client.responseBody();
|
client.responseBody();
|
||||||
|
|
||||||
Serial.print("Status: ");
|
|
||||||
Serial.println(statusCode);
|
|
||||||
Serial.print("Antwort: ");
|
|
||||||
Serial.println(response);
|
|
||||||
} else {
|
|
||||||
Serial.println("Noch keine gültigen Daten von BSEC.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(sendInterval);
|
delay(sendInterval);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue