|
|
|
|
@ -9,7 +9,6 @@ const char *ssid = "PET Aflytningsvogn #43";
|
|
|
|
|
const char *password = "zwr33htm";
|
|
|
|
|
|
|
|
|
|
const char *mqtt_server = "192.168.24.215";
|
|
|
|
|
// const char *mqtt_server = "test.mosquitto.org";
|
|
|
|
|
|
|
|
|
|
WiFiClient espClient;
|
|
|
|
|
PubSubClient client(espClient);
|
|
|
|
|
@ -18,6 +17,7 @@ const int lm35_pin = 34; /* LM35 O/P pin */
|
|
|
|
|
const int mic_pin = 35;
|
|
|
|
|
const int light_pin = 32;
|
|
|
|
|
const int motion_pin = 33;
|
|
|
|
|
const int led_pin = 25;
|
|
|
|
|
|
|
|
|
|
#define ADC_VREF_mV 3300.0 // in millivolt
|
|
|
|
|
#define ADC_RESOLUTION 4096.0
|
|
|
|
|
@ -67,6 +67,8 @@ void reconnect()
|
|
|
|
|
if (client.connect("ESP8266Client"))
|
|
|
|
|
{
|
|
|
|
|
Serial.println("connected");
|
|
|
|
|
|
|
|
|
|
client.subscribe("alarm/state");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
@ -79,6 +81,35 @@ void reconnect()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool flashTriggeredLed = false;
|
|
|
|
|
bool flashArmingLed = false;
|
|
|
|
|
|
|
|
|
|
int flashStatus = 0;
|
|
|
|
|
|
|
|
|
|
void callback(char *topic, byte *payload, unsigned int length)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
Serial.print("Message arrived [");
|
|
|
|
|
Serial.print(topic);
|
|
|
|
|
Serial.print("] ");
|
|
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
Serial.print((char)payload[i]);
|
|
|
|
|
}
|
|
|
|
|
Serial.println();
|
|
|
|
|
StaticJsonDocument<512> doc;
|
|
|
|
|
deserializeJson(doc, payload, length);
|
|
|
|
|
|
|
|
|
|
const char *status = doc["status"];
|
|
|
|
|
|
|
|
|
|
flashTriggeredLed = strcmp(doc["status"], "Triggered") == 0;
|
|
|
|
|
flashArmingLed = strcmp(doc["status"], "Arming") == 0 || strcmp(doc["status"], "Disarming") == 0;
|
|
|
|
|
if (flashTriggeredLed == false && flashArmingLed == false)
|
|
|
|
|
{
|
|
|
|
|
digitalWrite(led_pin, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
|
{
|
|
|
|
|
Serial.begin(115200);
|
|
|
|
|
@ -87,11 +118,13 @@ void setup()
|
|
|
|
|
pinMode(light_pin, INPUT);
|
|
|
|
|
pinMode(motion_pin, INPUT);
|
|
|
|
|
|
|
|
|
|
pinMode(led_pin, OUTPUT);
|
|
|
|
|
pinMode(13, OUTPUT);
|
|
|
|
|
digitalWrite(13, HIGH);
|
|
|
|
|
|
|
|
|
|
setup_wifi();
|
|
|
|
|
client.setServer(mqtt_server, 1883);
|
|
|
|
|
client.setCallback(callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define SOUND_MEASURE_INTERVAL 1000 * 10
|
|
|
|
|
@ -101,6 +134,7 @@ void setup()
|
|
|
|
|
unsigned long previousTemperatureMillis = 0;
|
|
|
|
|
unsigned long previousSoundMillis = 0;
|
|
|
|
|
unsigned long previousHighestSoundMillis = 0;
|
|
|
|
|
unsigned long previousFlashMillis = 0;
|
|
|
|
|
|
|
|
|
|
bool lastMotionState;
|
|
|
|
|
|
|
|
|
|
@ -120,6 +154,27 @@ void loop()
|
|
|
|
|
|
|
|
|
|
unsigned long currentMillis = millis();
|
|
|
|
|
|
|
|
|
|
if (currentMillis - previousFlashMillis >= 100 && flashTriggeredLed)
|
|
|
|
|
{
|
|
|
|
|
digitalWrite(led_pin, !flashStatus);
|
|
|
|
|
flashStatus = !flashStatus;
|
|
|
|
|
previousFlashMillis = currentMillis;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentMillis - previousFlashMillis >= 1000 && flashArmingLed && flashStatus == 0)
|
|
|
|
|
{
|
|
|
|
|
digitalWrite(led_pin, 0);
|
|
|
|
|
flashStatus = (flashStatus + 1) % 6;
|
|
|
|
|
previousFlashMillis = currentMillis;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentMillis - previousFlashMillis >= 100 && flashArmingLed && flashStatus > 0)
|
|
|
|
|
{
|
|
|
|
|
digitalWrite(led_pin, flashStatus % 2 == 0);
|
|
|
|
|
flashStatus = (flashStatus + 1) % 6;
|
|
|
|
|
previousFlashMillis = currentMillis;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentMillis - previousTemperatureMillis >= TEMP_MEASURE_INTERVAL)
|
|
|
|
|
{
|
|
|
|
|
// save the last time you blinked the LED
|
|
|
|
|
@ -183,7 +238,7 @@ void loop()
|
|
|
|
|
|
|
|
|
|
uint16_t soundValue = analogRead(mic_pin);
|
|
|
|
|
|
|
|
|
|
if (soundValue > 3072 && (currentMillis - previousHighestSoundMillis > 1000))
|
|
|
|
|
if (soundValue > 3300 && (currentMillis - previousHighestSoundMillis > 1000))
|
|
|
|
|
{
|
|
|
|
|
previousHighestSoundMillis = currentMillis;
|
|
|
|
|
|
|
|
|
|
|