Now sends different topics for each sensor

main
Filip B P 2 years ago
parent d7e2a5a869
commit 6acd4a8153

@ -3,6 +3,8 @@
#include <PubSubClient.h> #include <PubSubClient.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
const int deviceId = 1;
const char *ssid = "PET Aflytningsvogn #43"; const char *ssid = "PET Aflytningsvogn #43";
const char *password = "zwr33htm"; const char *password = "zwr33htm";
@ -81,11 +83,11 @@ void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
pinMode(lm35_pin, INPUT); pinMode(lm35_pin, INPUT);
pinMode(mic_pin, INPUT); pinMode(mic_pin, INPUT);
pinMode(light_pin, INPUT); pinMode(light_pin, INPUT);
pinMode(motion_pin, INPUT); pinMode(motion_pin, INPUT);
pinMode(13, OUTPUT); pinMode(13, OUTPUT);
digitalWrite(13, HIGH); digitalWrite(13, HIGH);
setup_wifi(); setup_wifi();
@ -140,88 +142,58 @@ void loop()
DynamicJsonDocument docTemp(1024); DynamicJsonDocument docTemp(1024);
docTemp["device"] = "esp32-1"; docTemp["deviceId"] = deviceId;
docTemp["type"] = "temperature";
docTemp["value"] = celsius; docTemp["value"] = celsius;
char bufferTemp[1024]; char bufferTemp[1024];
size_t bufferTempSize = serializeJson(docTemp, bufferTemp); size_t bufferTempSize = serializeJson(docTemp, bufferTemp);
client.publish("esp32/data", bufferTemp, bufferTempSize); client.publish("temperature", bufferTemp, bufferTempSize);
Serial.print("Light: "); Serial.print("Light: ");
Serial.println(analogValueLight); Serial.println(analogValueLight);
DynamicJsonDocument docLight(1024); DynamicJsonDocument docLight(1024);
docLight["device"] = "esp32-1"; docLight["deviceId"] = deviceId;
docLight["type"] = "light";
docLight["value"] = analogValueLight; docLight["value"] = analogValueLight;
char bufferLight[1024]; char bufferLight[1024];
size_t bufferLightSize = serializeJson(docLight, bufferLight); size_t bufferLightSize = serializeJson(docLight, bufferLight);
client.publish("esp32/data", bufferLight, bufferLightSize); client.publish("light", bufferLight, bufferLightSize);
} }
uint16_t soundValue = analogRead(mic_pin); bool motionState = digitalRead(motion_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;
}
if (currentMillis - previousSoundMillis >= SOUND_MEASURE_INTERVAL) if (lastMotionState != motionState)
{ {
previousSoundMillis = currentMillis; Serial.print("Motionstate: ");
Serial.println(motionState);
Serial.print(j);
Serial.print(": sound: ");
Serial.println(runningAverageSound);
DynamicJsonDocument doc(1024); DynamicJsonDocument doc(1024);
doc["device"] = "esp32-1"; doc["deviceId"] = deviceId;
doc["type"] = "sound"; doc["value"] = motionState;
doc["value"] = runningAverageSound;
char buffer[1024]; char buffer[1024];
size_t n = serializeJson(doc, buffer); size_t n = serializeJson(doc, buffer);
client.publish("esp32/data", buffer, n); client.publish("motion", buffer, n);
runningAverageSound = 0; lastMotionState = motionState;
j = 0; }
}
bool motionState = digitalRead(motion_pin); uint16_t soundValue = analogRead(mic_pin);
if (lastMotionState != motionState) { if (soundValue > 3072 && (currentMillis - previousHighestSoundMillis > 1000))
Serial.print("Motionstate: "); {
Serial.println(motionState); previousHighestSoundMillis = currentMillis;
DynamicJsonDocument doc(1024); DynamicJsonDocument doc(1024);
doc["device"] = "esp32-1"; doc["deviceId"] = deviceId;
doc["type"] = "motion"; doc["value"] = soundValue;
doc["value"] = motionState;
char buffer[1024]; char buffer[1024];
size_t n = serializeJson(doc, buffer); size_t n = serializeJson(doc, buffer);
client.publish("esp32/data", buffer, n); client.publish("sound", buffer, n);
lastMotionState = motionState;
} }
} }
Loading…
Cancel
Save