-
Notifications
You must be signed in to change notification settings - Fork 2
/
esp8266_sketch
109 lines (91 loc) · 3.59 KB
/
esp8266_sketch
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
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define wifi_ssid "yourssid" // Use your SSID
#define wifi_password "yourpass" // Use your WiFi password
#define mqtt_server "yourmqttIPaddress" // Use your MQTT Server IP Address
#define mqtt_port 1883 // Default MQTT port is 1883
#define mqtt_user "yourmqttuser" // Use the MQTT user you set up in Home Assistant
#define mqtt_password "yourmqttpass" // Use the MQTT password you set up in Home Assistant
#define in_topic "/smoke/in" // Note - change the SMOKE part of this to differentiate each smoke alarm in the home ->
#define out_topic "/smoke/out" // You need different topics for each of the smoke alarms.
WiFiClient espClient;
PubSubClient client;
void setup() {
Serial.begin(115200);
setup_wifi();
client.setClient(espClient);
client.setServer(mqtt_server, mqtt_port);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
IPAddress ip(192, 168, 2, 148); // I put in a static IP for ESP8266 to save time, not get DHCP address
IPAddress dns(192, 168, 2, 252); // Your home LAN DNS IP (I use Pi-Hole on a Raspberry Pi Zero W)
IPAddress gateway(192, 168, 2, 1); // Your router gateway IP address to the Internet
IPAddress subnet(255, 255, 255, 0); // Subnet mask of your LAN
WiFi.config(ip, dns, gateway, subnet);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
// If you do not want to use a username and password, change next line to
// if (client.connect("ESP8266Client")) {
if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Publishes a random 0 and 1 like someone switching off and on randomly (random(2))
if(client.publish(out_topic, "SMOKE Master Bedroom")) // I change this for each smoke alarm to match the bedroom it's in
{
Serial.println("Publish messge success");
}
else
{
Serial.println("Could not send message : (");
}
delay(500); // delay to send MQTT message before repeating.
}
Do not copy this below for the sketch - additional info you may need to know...
Note --
If you have multiple smoke alarms in the home, you need to change Arduino sketch for each alarm. In the topic lines (there are two of them), you'll need to give each detector a different topic name so that Home Assistant / MQTT can tell the difference and let you know which alarm is activated.
#define in_topic "/Masterbed/in"
#define out_topic "/Masterbed/out"
If you have another alarm in a second bedroom, you'll need to change the Arduino sketch to something like this:
#define in_topic "/2ndbed/in"
#define out_topic "/2ndbed/out"
And in the configuration.yaml, you need a section for each of the alarms:
sensor:
- platform: mqtt
name: "Smoke - Master Bedroom"
state_topic: "/Masterbed/out"
expire_after: 30
- platform: mqtt
name: "Smoke - Second Bedroom"
state_topic: "/2ndbed/out"
expire_after: 30