-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfirmware.ino
125 lines (105 loc) · 2.86 KB
/
firmware.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
/*
It connects to an MQTT server and waits for the 'o' message
which means "open the door".
After that it would open the door for the 4 seconds and send
notifications about door status on the topic "/door/status"
(can be "open", "closed").
Topic "/device/status" (can be "online" or "offline") uses
Last Will and Testament configuration.
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "configuration.h"
const char* outStatus = "/device/status";
const char* outTopic = "/door/status";
const char* inTopic = "/door/command";
int relay_pin = 0;
bool relayState = LOW;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifiSSID);
WiFi.begin(wifiSSID, wifiPassword);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
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();
// Open the door
if ((char)payload[0] == 'o') {
Serial.println("opening the door for 4 seconds");
digitalWrite(relay_pin, LOW);
client.publish(outTopic, "open");
delay(4000);
digitalWrite(relay_pin, HIGH);
client.publish(outTopic, "closed");
Serial.println("door closed");
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(mqttClientName, mqttUser, mqttPassword, outStatus, 2, 1, "offline")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish(outStatus, "online", true);
// ... and resubscribe
client.subscribe(inTopic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
// Initialize the relay pin as an output
pinMode(relay_pin, OUTPUT);
digitalWrite(relay_pin, HIGH);
// Blink to indicate setup
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
Serial.begin(115200);
// Connect to wifi
setup_wifi();
client.setServer(mqttServer, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}