I’m trying to make an alarm system here! And I’m executing two loops:
What I think I’ve done is…
- One loop every second for Fire/Smoke Alarm (I’ve included an Activity LED at D7, Photon’s LED, as this runs every second)
- Another loop every ten seconds for Temperature/Humidity Alarm
Stability Issues:-
After flashing code, breathing cyan, activity LED blinks every second as it should… But, mostly within five minutes of running, these events happen…
- LED to warn Temp/Humidity raise, addressed as buzzPin on my code, goes high at D6 (I’ve included an alert function, which set D6 to HIGH. But I don’t see the function getting called… I’ve checked this with a serial monitor.)
- Activity LED stops at D7
- Photon blinks RED!
- Blinks GREEN!
- Blinks CYAN!
- Breathes CYAN!
- Activity LED blinks at D7
- After a few minutes… Starts again from Step 1…
To test if this happens on the alert function getting called, anyway. I’ve set a low threshold limit of 30 degrees while temperature is already 32 here… Did that, and yes! This happens only when the alert function is called…
I’ve tried these with and without SYSTEM_THREAD(ENABLED);
So there are two issues here…
- Why is the Alert Function getting called even when the threshold is not met?
- And even if its called why is the series of 8 events mentioned above happening?
As per my requirement at no point, should the code should stop running the timed events!
Let me know, what’s stopping these events from running forever?
Also there’s another issue, after a few hours the device starts to breathe green and doesn’t recover itself. I’ll have to press the reset button on the device…
Guide me on this guys!
SYSTEM_THREAD(ENABLED);
// This #include statement was automatically added by the Particle IDE.
#include "blynk/blynk.h"
// This #include statement was automatically added by the Particle IDE.
#include "PietteTech_DHT/PietteTech_DHT.h"
// For Mathematical Operations
#include "math.h"
#define ActLED D7
#define buzzPin D6
#define LDRPin A2
#define SmokePin A1
#define DHTTYPE DHT22 // Choose if you're using DHT11 / DHT22
#define DHTPIN 2 // DHT22's Data Pin to Arduino's Digital Pin 7
int calibrationTime = 10; // Sensors Calibration Time, in seconds
// DHT Declaration
void dht_wrapper(); // must be declared before the lib initialization
// Initializing DHT Library
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
// DHT Globals
bool DHTStarted; // flag to indicate we started acquisition
//this is coming from http://www.instructables.com/id/Datalogging-with-Spark-Core-Google-Drive/?ALLSTEPS
char resultstr[64]; //String to store the sensor data
//DANGER - DO NOT SHARE!!!!
char auth[] = "????"; // Put your blynk token here
//DANGER - DO NOT SHARE!!!!
char VERSION[64] = "0.04";
// Initial Alert States //
int actState = LOW; // Arduino & Blynk's Initial Activity State
int Buzzer = HIGH;
int Push = HIGH;
int Email = HIGH;
// Initial Alert Threshold Values //
int SmokeThreshold = 220;
int FireThreshold = 220;
int TempThreshold = 30;
int HumidThreshold = 90;
// Variables for Fire/Smoke Surge Detection
int ttimer = 3; // Value in Minutes
int htimer = 3; // Value in Minutes
int tdiff = 4; // Value in Degrees
int hdiff = 20; // Value in Percentage%
int tcount = 0;
int t1;
int t2;
int hcount = 0;
int h1;
int h2;
int first = true;
// Device Location //
String Location = "ilak2k Home";
Timer timer1(10000, TemperatureHumidity);
Timer timer2(1000, SmokeFireMotion);
void Alert(String Sensor, int AlertType, int LEDAlert)
{
if (Buzzer == HIGH)
{
Serial.println("Buzzer Activated!");
digitalWrite(buzzPin, HIGH); // Activate Buzzer
}
if (Push == HIGH)
{
if (AlertType == 1)
{
// Blynk.notify(Location + ": Emergency " + Sensor + " Alert!"); // Notification with Location First
Serial.println("Emergency Notification Sent!");
Blynk.notify("Emergency: " + Sensor + " Alert! @" + Location); // Notification with Situation First
}
else
{
// Blynk.notify(Location + ": " + Sensor + " Surge Detected!");
Blynk.notify(Sensor + " Surge Detected! @" + Location);
Serial.println("Surge Notification Sent!");
}
}
if (Email == HIGH)
{
if (AlertType == 1)
{
Blynk.email("ilak2k@gmail.com", "Emergency: " + Sensor + " Alert! @" + Location, "This is a sensor-triggered alert, from Monitoring System! Investigate specified location");
Serial.println("Emergency Email Sent!");
}
else
{
Blynk.email("ilak2k@gmail.com", Sensor + " Surge Detected! @" + Location, "This is a sensor-triggered alert, from Monitoring System! Investigate specified location");
Serial.println("Surge Email Sent!");
}
}
Blynk.virtualWrite(LEDAlert, 1023); // Write HIGH to Virtual Pin 3
}
void TemperatureHumidity() {
// Temperature, Relative Humidity, Dew Point
float temp;
float relhumid;
float dewpt;
float humidex;
if (!DHTStarted) { // start the sample
DHT.acquire();
DHTStarted = true;
}
if (!DHT.acquiring()) { // has sample completed?
temp = (float)DHT.getCelsius();
String temps = String(temp, 1);
// Serial.print(String("Temmperature, in Celcius: "));
Serial.println(temps);
Blynk.virtualWrite(V1, temps);
Particle.publish("Temperature:", temps, 60, PRIVATE);
relhumid = (float)DHT.getHumidity();
String relhumids = String(relhumid, 1);
// Serial.print(String("Relative Humidity, in %: "));
Serial.println(relhumids);
Blynk.virtualWrite(V2, relhumids);
Particle.publish("Relative Humidity:", relhumids, 60, PRIVATE);
dewpt = (float)DHT.getDewPoint();
String dewpts = String(dewpt, 1);
// Serial.print(String("Dew Point, in Celcius: "));
Serial.println(dewpts);
Blynk.virtualWrite(V3, dewpts);
Particle.publish("Dew Point:", dewpts, 60, PRIVATE);
humidex = calc_humidex(temp, dewpt);
String humidexs = String(humidex, 1);
// Serial.print(String("Humidex, in Celcius: "));
Serial.println(humidexs);
Blynk.virtualWrite(V4, humidexs);
Particle.publish("Humidex:", humidexs, 60, PRIVATE);
// double heatindex = calc_heatIndexFast(temp, relhumid);
// String heatindexs = String(heatindex, 1);
// Serial.print(String("Dew Point, in Celcius: "));
// Serial.println(heatindexs);
// Blynk.virtualWrite(V5, heatindexs);
// Particle.publish("Heat Index:", heatindexs, 60, PRIVATE);
DHTStarted = false;
}
// Temperature/Humidity Surge Alert System
tcount = tcount + 1;
if (tcount == 1)
{
t1 = temp;
// Serial.print("Temp Value: ");
// Serial.println(t1);
}
if (tcount == (ttimer * 6)) // Multiplied by 6 instead of 60, cause the Temperature Function runs only every 10 seconds
{
t2 = temp;
// Serial.print("Temp Difference: ");
// Serial.println(t2-t1);
if ((t2 - t1) > tdiff)
{
// Alert("Temperature", 2, 3);
}
tcount = 0;
}
hcount = hcount + 1;
if (hcount == 1)
{
h1 = relhumid;
// Serial.print("Humidity Value 1: ");
// Serial.println(h1);
}
if (hcount == (htimer * 6)) // Multiplied by 6 instead of 60, cause the Temperature Function runs only every 10 seconds
{
h2 = relhumid;
// Serial.print("Humidity Value 2: ");
// Serial.println(h2);
// Serial.print("Humidity Difference: ");
// Serial.println(h2-h1);
if ((h2 - h1) > hdiff)
{
// Alert("Humidity", 2, 4);
}
hcount = 0;
}
// Temperature Alarm System
if (temp >= TempThreshold)
{
Alert("Temperature", 1, 3);
}
// Relative Humidity Alarm System
if (relhumid >= HumidThreshold)
{
Alert("Humidity", 1, 4);
}
}
double calc_humidex(double tempC, double DewPoint)
{
double e = 5417.7530*((1/273.16)-(1/(273.16 + DewPoint)));
double h = tempC + 0.5555 * ( 6.11 * exp (e) - 10);
return h;
}
void SmokeFireMotion() {
int lightlevel = analogRead(LDRPin);
lightlevel = map(lightlevel, 0, 4095, 0, 100);
int smokelevel = analogRead(SmokePin);
Blynk.virtualWrite(V11, lightlevel);
Blynk.virtualWrite(V12, smokelevel);
//Blynk Activity LED Status
if (actState == LOW)
{
digitalWrite(ActLED, HIGH);
Blynk.virtualWrite(V0, 1023); // Write HIGH to Virtual Pin 0
actState = HIGH;
}
else
{
digitalWrite(ActLED, LOW);
Blynk.virtualWrite(V0, 0); // Write LOW to Virtual Pin 0
actState = LOW;
}
}
void dht_wrapper() {
DHT.isrCallback();
}
void setup() {
Serial.begin(9600);
Serial.println("Hi!");
Blynk.begin(auth);
Particle.publish("DHT22 - firmware version", VERSION, 60, PRIVATE);
pinMode(ActLED, OUTPUT);
pinMode(buzzPin, OUTPUT);
pinMode(LDRPin, INPUT);
pinMode(SmokePin, INPUT);
// Sensors Calibration & Warmup Delay, at startup
Serial.print("Calibrating Sensors");
for(int i = 0; i < calibrationTime; i++)
{
Serial.print(".");
delay(1000);
}
Serial.println(" Complete!");
Serial.println("Sensors, ACTIVE!!!");
delay(100);
timer1.start();
timer2.start();
}
void loop() {
Blynk.run();
}