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 <ArduinoJson.h>
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);
}
}
Loading…
Cancel
Save