· 6 years ago · Mar 13, 2019, 08:46 AM
1#include <ESP8266WiFi.h>
2#include <PubSubClient.h>
3
4#define MQTT_VERSION MQTT_VERSION_3_1_1
5
6// Wifi: SSID and password
7const char* WIFI_SSID = "ADRE0";
8const char* WIFI_PASSWORD = "trapano123";
9
10// MQTT: ID, server IP, port, username and password
11const PROGMEM char* MQTT_CLIENT_ID = "14b_DoorLock";
12const PROGMEM char* MQTT_SERVER_IP = "192.168.1.12";
13const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
14
15
16// MQTT: topics
17const char* MQTT_LOCK_STATE_TOPIC = "home/14b_office_door/status";
18const char* MQTT_LOCK_COMMAND_TOPIC = "home/14b_office_door/command";
19
20// payloads by default (on/off)
21const char* UNLOCK = "UNLOCK";
22const char* LOCK = "LOCK";
23
24const PROGMEM uint8_t LOCK_PIN = 5;
25boolean lock_state = false;
26
27WiFiClient wifiClient;
28PubSubClient client(wifiClient);
29
30// function called to publish the state of the light (on/off)
31void publishLightState() {
32 if (lock_state) {
33 client.publish(MQTT_LOCK_STATE_TOPIC, UNLOCK, true);
34 } else {
35 client.publish(MQTT_LOCK_STATE_TOPIC, LOCK, true);
36 }
37}
38
39// function called to turn on/off the light
40void setLockState() {
41 if (lock_state) {
42 digitalWrite(LOCK_PIN, HIGH);
43 delay(100);
44 digitalWrite(LOCK_PIN, LOW);
45 delay(100);
46 digitalWrite(LOCK_PIN, HIGH);
47 delay(100);
48 digitalWrite(LOCK_PIN, LOW);
49 delay(100);
50 digitalWrite(LOCK_PIN, HIGH);
51 delay(100);
52 Serial.println("INFO: Door is unlocked...");
53 } else {
54 digitalWrite(LOCK_PIN, HIGH);
55 delay(100);
56 digitalWrite(LOCK_PIN, LOW);
57 delay(100);
58 digitalWrite(LOCK_PIN, HIGH);
59 Serial.println("INFO: Door is locked...");
60 }
61}
62
63// function called when a MQTT message arrived
64void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
65 // concat the payload into a string
66 String payload;
67 for (uint8_t i = 0; i < p_length; i++) {
68 payload.concat((char)p_payload[i]);
69 }
70
71 // handle message topic
72 if (String(MQTT_LOCK_COMMAND_TOPIC).equals(p_topic)) {
73 if (payload.equals(String(UNLOCK))) {
74 lock_state = true;
75 setLockState();
76 publishLightState();
77 } else if (payload.equals(String(LOCK))) {
78 lock_state = false;
79 setLockState();
80 publishLightState();
81 }
82 }
83}
84
85void reconnect() {
86 // Loop until we're reconnected
87 while (!client.connected()) {
88 Serial.print("INFO: Attempting MQTT connection...");
89 // Attempt to connect
90 if (client.connect(MQTT_CLIENT_ID)) {
91 Serial.println("INFO: connected");
92 // Once connected, publish an announcement...
93 publishLightState();
94 // ... and resubscribe
95 client.subscribe(MQTT_LOCK_COMMAND_TOPIC);
96 } else {
97 Serial.print("ERROR: failed, rc=");
98 Serial.print(client.state());
99 Serial.println("DEBUG: try again in 5 seconds");
100 // Wait 5 seconds before retrying
101 delay(5000);
102 }
103 }
104}
105
106void setup() {
107 // init the serial
108 Serial.begin(115200);
109
110 // init the led
111 pinMode(LOCK_PIN, OUTPUT);
112 digitalWrite(LOCK_PIN, HIGH);
113 analogWriteRange(255);
114 setLockState();
115
116 // init the WiFi connection
117 Serial.println();
118 Serial.println();
119 Serial.print("INFO: Connecting to ");
120 WiFi.mode(WIFI_STA);
121 Serial.println(WIFI_SSID);
122 WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
123
124 while (WiFi.status() != WL_CONNECTED) {
125 delay(500);
126 Serial.print(".");
127 }
128
129 Serial.println("");
130 Serial.println("INFO: WiFi connected");
131 Serial.print("INFO: IP address: ");
132 Serial.println(WiFi.localIP());
133
134 // init the MQTT connection
135 client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
136 client.setCallback(callback);
137}
138
139void loop() {
140
141 if (!client.connected()) {
142 reconnect();
143 }
144 client.loop();
145}