-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSmoke_Alarm_Public.ino
152 lines (116 loc) · 3.88 KB
/
Smoke_Alarm_Public.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*based on project https://github.com/rniemand/ArduinoProjects/blob/master/projects/gate-alarm/gate-alarm.ino */
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
/************************* WiFi and MQTT Config *********************************/
#define WLAN_SSID "xxx"
#define WLAN_PASS "xxx"
const char* mqtt_server = "192.168.0.x";
#define MQTT_PORT 1883
String MQTT_CLIENT_NAME = "home-assistant";
#define MQTT_USER "xxx"
#define MQTT_PASS "xxx"
/************************* Configuration *********************************/
#define DEBUG true
/************************* Sensor Vars *********************************/
boolean fire_state_changed = false;
boolean fire_switch_closed = false;
long fire_last_triggered = 0;
short fire_trigger_threshold = 100;
short fire_publish_interval = 10 * 60 * 5; // Every 5 min
short fire_loop_count = fire_publish_interval; // Publish immediatley
String fire_topic = "home-assistant/security/smoke_alarm";
/************************* Global Vars *********************************/
WiFiClient esp_client;
PubSubClient mqtt_client(esp_client);
void setup() {
#if DEBUG == true
Serial.begin(9600);
#endif
// Setup WiFi connection & connect to MQTT server
waitForWiFiConnection();
connectMqtt();
while (!mqtt_client.connected()) { Serial.print(".");}
publishFireAlert();
}
void loop(){
}
void publishFireAlert() {
// Update the state of the client over MQTT
mqtt_client.publish(fire_topic.c_str(), "Fire");
#if DEBUG == true
Serial.print("(MQTT) fire switch state updated: ");
Serial.println("Fire");
#endif
}
// ############################## WiFi functions
void waitForWiFiConnection() {
#if DEBUG == true
Serial.println("Connecting to WiFi");
#endif
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
#if DEBUG == true
Serial.print(".");
#endif
delay(250);
}
#if DEBUG == true
Serial.println();
Serial.print("Connected: ");
Serial.println(WiFi.localIP());
#endif
}
// ############################## MQTT functions
void connectMqtt() {
mqtt_client.setServer(mqtt_server, MQTT_PORT);
mqtt_client.setCallback(callback);
reconnect();
}
void reconnect() {
#if DEBUG == true
Serial.print("Attempting to connect to MQTT server: ");
Serial.println(mqtt_server);
#endif
while (!mqtt_client.connected()) {
if (mqtt_client.connect(MQTT_CLIENT_NAME.c_str(), MQTT_USER, MQTT_PASS, ("node/" + MQTT_CLIENT_NAME).c_str(), 1, false, "timeout")) {
mqtt_client.publish(("node/" + MQTT_CLIENT_NAME).c_str(), "connected");
//mqtt_client.subscribe(String("cmd/" + MQTT_CLIENT_NAME).c_str());
//mqtt_client.subscribe("cmd/node_kitchen");
mqtt_client.subscribe(String("cmd/" + MQTT_CLIENT_NAME).c_str());
#if DEBUG == true
Serial.println("connected");
#endif
} else {
#if DEBUG == true
Serial.println("unable to connect, waiting 5 seconds...");
#endif
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
String command = getCommand(payload, length);
if(command == "update_all") {
publishFireAlert();
return;
}
if(command == "update_fire") {
#if DEBUG == true
Serial.println("(MQTT) Updating fire switch information");
#endif
publishFireAlert();
return;
}
#if DEBUG == true
Serial.print("UNKNOWN MQTT COMMAND: ");
Serial.println(command);
#endif
}
String getCommand(byte* payload, unsigned int length) {
char msg[length + 1];
for (int i = 0; i < length; i++) {
msg[i] = (char) payload[i];
}
msg[length] = '\0';
return String(msg);
}