|
|
|
|
@ -3,6 +3,8 @@
|
|
|
|
|
#include <PubSubClient.h>
|
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
|
|
|
|
|
const int deviceId = 1;
|
|
|
|
|
|
|
|
|
|
const char *ssid = "PET Aflytningsvogn #43";
|
|
|
|
|
const char *password = "zwr33htm";
|
|
|
|
|
|
|
|
|
|
@ -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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t soundValue = analogRead(mic_pin);
|
|
|
|
|
|
|
|
|
|
if (soundValue > highestSound) {
|
|
|
|
|
highestSound = soundValue;
|
|
|
|
|
client.publish("light", bufferLight, bufferLightSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|