From 6acd4a8153470e62aa243fd4a0ff0490b38ad26b Mon Sep 17 00:00:00 2001 From: Filip B P Date: Mon, 14 Aug 2023 11:24:29 +0200 Subject: [PATCH] Now sends different topics for each sensor --- src/main.cpp | 80 +++++++++++++++++----------------------------------- 1 file changed, 26 insertions(+), 54 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 18da69c..49d7e2f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,8 @@ #include #include +const int deviceId = 1; + const char *ssid = "PET Aflytningsvogn #43"; const char *password = "zwr33htm"; @@ -81,11 +83,11 @@ void setup() { Serial.begin(115200); pinMode(lm35_pin, INPUT); - pinMode(mic_pin, INPUT); - pinMode(light_pin, INPUT); - pinMode(motion_pin, INPUT); + pinMode(mic_pin, INPUT); + pinMode(light_pin, INPUT); + pinMode(motion_pin, INPUT); - pinMode(13, OUTPUT); + pinMode(13, OUTPUT); digitalWrite(13, HIGH); setup_wifi(); @@ -140,88 +142,58 @@ void loop() DynamicJsonDocument docTemp(1024); - docTemp["device"] = "esp32-1"; - docTemp["type"] = "temperature"; + docTemp["deviceId"] = deviceId; docTemp["value"] = celsius; char bufferTemp[1024]; size_t bufferTempSize = serializeJson(docTemp, bufferTemp); - client.publish("esp32/data", bufferTemp, bufferTempSize); + client.publish("temperature", bufferTemp, bufferTempSize); Serial.print("Light: "); Serial.println(analogValueLight); DynamicJsonDocument docLight(1024); - docLight["device"] = "esp32-1"; - docLight["type"] = "light"; + docLight["deviceId"] = deviceId; docLight["value"] = analogValueLight; char bufferLight[1024]; size_t bufferLightSize = serializeJson(docLight, bufferLight); - client.publish("esp32/data", bufferLight, bufferLightSize); + client.publish("light", bufferLight, bufferLightSize); } - uint16_t soundValue = analogRead(mic_pin); - - if (soundValue > highestSound) { - highestSound = soundValue; - } - - if (currentMillis - previousHighestSoundMillis >= SOUND_HIGHEST_MEASURE_INTERVAL) - { - previousHighestSoundMillis = currentMillis; - - if (runningAverageSound == 0) - { - runningAverageSound = highestSound; - } - else - { - runningAverageSound = (runningAverageSound + highestSound) / 2; - } - - highestSound = 0; - } + bool motionState = digitalRead(motion_pin); - if (currentMillis - previousSoundMillis >= SOUND_MEASURE_INTERVAL) + if (lastMotionState != motionState) { - previousSoundMillis = currentMillis; - - Serial.print(j); - Serial.print(": sound: "); - Serial.println(runningAverageSound); + Serial.print("Motionstate: "); + Serial.println(motionState); DynamicJsonDocument doc(1024); - doc["device"] = "esp32-1"; - doc["type"] = "sound"; - doc["value"] = runningAverageSound; + doc["deviceId"] = deviceId; + doc["value"] = motionState; char buffer[1024]; size_t n = serializeJson(doc, buffer); - client.publish("esp32/data", buffer, n); + client.publish("motion", buffer, n); - runningAverageSound = 0; - j = 0; - } + lastMotionState = motionState; + } - bool motionState = digitalRead(motion_pin); + uint16_t soundValue = analogRead(mic_pin); - if (lastMotionState != motionState) { - Serial.print("Motionstate: "); - Serial.println(motionState); + if (soundValue > 3072 && (currentMillis - previousHighestSoundMillis > 1000)) + { + previousHighestSoundMillis = currentMillis; DynamicJsonDocument doc(1024); - doc["device"] = "esp32-1"; - doc["type"] = "motion"; - doc["value"] = motionState; + doc["deviceId"] = deviceId; + doc["value"] = soundValue; char buffer[1024]; size_t n = serializeJson(doc, buffer); - client.publish("esp32/data", buffer, n); - - lastMotionState = motionState; + client.publish("sound", buffer, n); } } \ No newline at end of file