GruppeC/frontend/SmartLectureRoom/Arduino/SmartRoomSensor/SmartRoomSensor.ino

128 lines
4.1 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
Smart Lecture Room – Sensorknoten für den Unity-Zwilling
---------------------------------------------------------
Sendet alle 2 s eine JSON-Zeile über USB-Seriell (115200 Baud) an Unity:
{"t":23.4,"co2":812,"lux":350,"noise":46}
und empfängt die Steuerbefehle der virtuellen Aktoren:
{"window":"open","blind":1.00,"heat":0.50,"vent":0.80,"light":0.40,"beamer":0}
Verdrahtung (Beispiel, Arduino Uno – einfach an eure Sensoren anpassen):
A0 TMP36 Temperatursensor
A1 MQ-135 Gassensor (grobe CO2-Schätzung; besser: SCD30/SCD41 über I2C)
A2 LDR-Spannungsteiler (Helligkeit)
A3 Mikrofonmodul (Lärm)
D9 Servo -> Fenstermodell (0° zu, 30° gekippt, 90° offen)
D6 PWM -> Lüfter (über MOSFET/Transistor!)
D5 PWM -> LED Heizung
D3 PWM -> LED Licht
Auf dem ESP32 statt <Servo.h> die Bibliothek "ESP32Servo" verwenden; für WLAN die
JSON-Zeile per UDP an Port 5005 des Unity-Rechners senden (Antworten kommen auf Port 5006).
*/
#include <Servo.h>
const int PIN_TEMP = A0;
const int PIN_GAS = A1;
const int PIN_LDR = A2;
const int PIN_MIC = A3;
const int PIN_SERVO = 9;
const int PIN_FAN = 6;
const int PIN_HEAT_LED = 5;
const int PIN_LIGHT_LED = 3;
const unsigned long SEND_INTERVAL_MS = 2000;
Servo windowServo;
String inbox;
unsigned long lastSend = 0;
float readTemperatureC() {
float volts = analogRead(PIN_TEMP) * (5.0 / 1023.0);
return (volts - 0.5) * 100.0; // TMP36: 10 mV/°C, 500 mV Offset
}
float readCo2ppm() {
// Sehr grobe Abbildung des MQ-135 – für echte Messungen SCD30/SCD41 nutzen und kalibrieren.
int raw = analogRead(PIN_GAS);
return 400.0 + raw * (4600.0 / 1023.0);
}
float readLux() {
int raw = analogRead(PIN_LDR);
return raw * (1000.0 / 1023.0); // grob, je nach Widerstand anpassen
}
float readNoiseDb() {
// Spitze-Spitze-Wert über 50 ms -> grobe dB-Schätzung
unsigned long start = millis();
int lo = 1023, hi = 0;
while (millis() - start < 50) {
int v = analogRead(PIN_MIC);
if (v < lo) lo = v;
if (v > hi) hi = v;
}
int pp = max(hi - lo, 1);
return 30.0 + 20.0 * log10(pp);
}
// Liest einen Zahlenwert zu "key" aus einer flachen JSON-Zeile (ohne Bibliothek).
float jsonNumber(const String& json, const char* key, float fallback) {
String k = String("\"") + key + "\":";
int i = json.indexOf(k);
if (i < 0) return fallback;
return json.substring(i + k.length()).toFloat();
}
String jsonString(const String& json, const char* key) {
String k = String("\"") + key + "\":\"";
int i = json.indexOf(k);
if (i < 0) return "";
int start = i + k.length();
int end = json.indexOf('"', start);
return end < 0 ? "" : json.substring(start, end);
}
void applyCommand(const String& json) {
String window = jsonString(json, "window");
if (window == "open") windowServo.write(90);
else if (window == "tilted") windowServo.write(30);
else if (window == "closed") windowServo.write(0);
analogWrite(PIN_FAN, (int)(constrain(jsonNumber(json, "vent", 0), 0, 1) * 255));
analogWrite(PIN_HEAT_LED, (int)(constrain(jsonNumber(json, "heat", 0), 0, 1) * 255));
analogWrite(PIN_LIGHT_LED, (int)(constrain(jsonNumber(json, "light", 0), 0, 1) * 255));
// "blind" (0 = oben, 1 = unten) und "beamer" stehen ebenfalls zur Verfügung, z. B. für einen Schrittmotor.
}
void setup() {
Serial.begin(115200);
windowServo.attach(PIN_SERVO);
windowServo.write(0);
pinMode(PIN_FAN, OUTPUT);
pinMode(PIN_HEAT_LED, OUTPUT);
pinMode(PIN_LIGHT_LED, OUTPUT);
}
void loop() {
// Befehle von Unity zeilenweise einlesen
while (Serial.available()) {
char c = (char)Serial.read();
if (c == '\n') {
if (inbox.startsWith("{")) applyCommand(inbox);
inbox = "";
} else if (inbox.length() < 200) {
inbox += c;
}
}
if (millis() - lastSend >= SEND_INTERVAL_MS) {
lastSend = millis();
Serial.print("{\"t\":"); Serial.print(readTemperatureC(), 1);
Serial.print(",\"co2\":"); Serial.print(readCo2ppm(), 0);
Serial.print(",\"lux\":"); Serial.print(readLux(), 0);
Serial.print(",\"noise\":"); Serial.print(readNoiseDb(), 0);
Serial.println("}");
}
}