|
| 1 | +// Using Sonoff wifi switch, esp-livingroomlight, 80MHz, 1M / 64K |
| 2 | +#include <ESP8266WiFi.h> |
| 3 | +#include <ESP8266mDNS.h> |
| 4 | +#include <WiFiUdp.h> |
| 5 | +#include <ArduinoOTA.h> |
| 6 | +#include <WiFiClientSecure.h> |
| 7 | +#include <PubSubClient.h> |
| 8 | +#include <ArduinoJson.h> |
| 9 | +#include <Ticker.h> |
| 10 | +#include <pgmspace.h> |
| 11 | +#include "/usr/local/src/aptls_setting.h" |
| 12 | + |
| 13 | +extern "C" { |
| 14 | +#include "user_interface.h" |
| 15 | +} |
| 16 | + |
| 17 | +#define BUTTON_PIN 0 |
| 18 | +#define RELAY_PIN 12 |
| 19 | +#define LED_PIN 13 |
| 20 | + |
| 21 | +#define BETWEEN_RELAY_ACTIVE 1000 |
| 22 | +#define REPORT_INTERVAL 5000 // in msec |
| 23 | + |
| 24 | +IPAddress mqtt_server = MQTT_SERVER; |
| 25 | + |
| 26 | +const char* subscribe_topic = "light/livingroomlight"; |
| 27 | +const char* reporting_topic = "light/livingroomlight/report"; |
| 28 | +const char* status_topic = "light/livingroomlight/status"; |
| 29 | +const char* hellotopic = "HELLO"; |
| 30 | + |
| 31 | +long lastReconnectAttempt = 0; |
| 32 | +volatile bool bUpdated = false; |
| 33 | +volatile bool bRelayState = false; |
| 34 | +volatile bool bRelayReady = false; |
| 35 | +volatile bool bsendReport = false; |
| 36 | + |
| 37 | +String clientName; |
| 38 | +unsigned long lastRelayActionmillis; |
| 39 | +unsigned long startMills; |
| 40 | + |
| 41 | +// send reset info |
| 42 | +String getResetInfo; |
| 43 | +int ResetInfo = LOW; |
| 44 | + |
| 45 | +void ICACHE_RAM_ATTR callback(char* intopic, byte* inpayload, unsigned int length); |
| 46 | + |
| 47 | +WiFiClientSecure sslclient; |
| 48 | +PubSubClient client(mqtt_server, 8883, callback, sslclient); |
| 49 | +Ticker ticker; |
| 50 | + |
| 51 | +void tick() |
| 52 | +{ |
| 53 | + //toggle state |
| 54 | + int state = digitalRead(LED_PIN); // get the current state of GPIO13 pin |
| 55 | + digitalWrite(LED_PIN, !state); // set pin to the opposite state |
| 56 | +} |
| 57 | + |
| 58 | +bool ICACHE_RAM_ATTR sendmqttMsg(const char* topictosend, String payloadtosend, bool retain = false) |
| 59 | +{ |
| 60 | + unsigned int msg_length = payloadtosend.length(); |
| 61 | + |
| 62 | + byte* p = (byte*)malloc(msg_length); |
| 63 | + memcpy(p, (char*) payloadtosend.c_str(), msg_length); |
| 64 | + |
| 65 | + if (client.publish(topictosend, p, msg_length, retain)) |
| 66 | + { |
| 67 | + free(p); |
| 68 | + //client.loop(); |
| 69 | + return true; |
| 70 | + |
| 71 | + } else { |
| 72 | + free(p); |
| 73 | + //client.loop(); |
| 74 | + return false; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +void ICACHE_RAM_ATTR sendreport() |
| 79 | +{ |
| 80 | + String payload; |
| 81 | + if (bRelayState) |
| 82 | + { |
| 83 | + payload = "true"; |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + payload = "false"; |
| 88 | + } |
| 89 | + sendmqttMsg(reporting_topic, payload, true); |
| 90 | +} |
| 91 | + |
| 92 | +void ICACHE_RAM_ATTR sendCheck() |
| 93 | +{ |
| 94 | + |
| 95 | + DynamicJsonBuffer jsonBuffer; |
| 96 | + JsonObject& root = jsonBuffer.createObject(); |
| 97 | + root["FreeHeap"] = ESP.getFreeHeap(); |
| 98 | + root["RSSI"] = WiFi.RSSI(); |
| 99 | + root["millis"] = millis(); |
| 100 | + String json; |
| 101 | + root.printTo(json); |
| 102 | + |
| 103 | + sendmqttMsg(status_topic, json, false); |
| 104 | +} |
| 105 | + |
| 106 | +void ICACHE_RAM_ATTR parseMqttMsg(String receivedpayload, String receivedtopic) |
| 107 | +{ |
| 108 | + if (receivedtopic == subscribe_topic) |
| 109 | + { |
| 110 | + if (bRelayReady) |
| 111 | + { |
| 112 | + if ( receivedpayload == "true") |
| 113 | + { |
| 114 | + bRelayState = HIGH; |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + bRelayState = LOW; |
| 119 | + } |
| 120 | + bUpdated = true; |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + sendreport(); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +void ICACHE_RAM_ATTR callback(char* intopic, byte* inpayload, unsigned int length) |
| 130 | +{ |
| 131 | + String receivedtopic = intopic; |
| 132 | + String receivedpayload ; |
| 133 | + |
| 134 | + for (unsigned int i = 0; i < length; i++) |
| 135 | + { |
| 136 | + receivedpayload += (char)inpayload[i]; |
| 137 | + } |
| 138 | + |
| 139 | + parseMqttMsg(receivedpayload, receivedtopic); |
| 140 | +} |
| 141 | + |
| 142 | +bool verifytls() |
| 143 | +{ |
| 144 | + |
| 145 | + if (!sslclient.connect(mqtt_server, 8883)) |
| 146 | + { |
| 147 | + return false; |
| 148 | + } |
| 149 | + |
| 150 | + if (sslclient.verify(MQTT_FINGERPRINT, MQTT_SERVER_CN)) |
| 151 | + { |
| 152 | + sslclient.stop(); |
| 153 | + return true; |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + sslclient.stop(); |
| 158 | + return false; |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +boolean reconnect() |
| 163 | +{ |
| 164 | + if (!client.connected()) |
| 165 | + { |
| 166 | + if (verifytls()) |
| 167 | + { |
| 168 | + if (client.connect((char*) clientName.c_str(), MQTT_USER, MQTT_PASS)) |
| 169 | + { |
| 170 | + if ( ResetInfo == LOW) { |
| 171 | + client.publish(hellotopic, (char*) getResetInfo.c_str()); |
| 172 | + ResetInfo = HIGH; |
| 173 | + } |
| 174 | + |
| 175 | + client.subscribe(subscribe_topic); |
| 176 | + //client.loop(); |
| 177 | + ticker.attach(1, tick); |
| 178 | + } |
| 179 | + else |
| 180 | + { |
| 181 | + ticker.attach(0.5, tick); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + return client.connected(); |
| 186 | +} |
| 187 | + |
| 188 | +void wifi_connect() |
| 189 | +{ |
| 190 | + if (WiFi.status() != WL_CONNECTED) |
| 191 | + { |
| 192 | + wifi_set_phy_mode(PHY_MODE_11N); |
| 193 | + WiFi.setOutputPower(20); |
| 194 | + WiFi.mode(WIFI_STA); |
| 195 | + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); |
| 196 | + WiFi.hostname("esp-livingroomlight"); |
| 197 | + |
| 198 | + int Attempt = 0; |
| 199 | + while (WiFi.status() != WL_CONNECTED) |
| 200 | + { |
| 201 | + delay(100); |
| 202 | + Attempt++; |
| 203 | + if (Attempt == 150) |
| 204 | + { |
| 205 | + ESP.restart(); |
| 206 | + delay(200); |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +void change_light() |
| 213 | +{ |
| 214 | + digitalWrite(RELAY_PIN, bRelayState); |
| 215 | + bsendReport = true; |
| 216 | +} |
| 217 | + |
| 218 | +void run_lightcmd_isr() |
| 219 | +{ |
| 220 | + bRelayState = !bRelayState; |
| 221 | + bUpdated = true; |
| 222 | +} |
| 223 | + |
| 224 | +String macToStr(const uint8_t* mac) |
| 225 | +{ |
| 226 | + String result; |
| 227 | + for (int i = 0; i < 6; ++i) |
| 228 | + { |
| 229 | + result += String(mac[i], 16); |
| 230 | + if (i < 5) |
| 231 | + result += ':'; |
| 232 | + } |
| 233 | + return result; |
| 234 | +} |
| 235 | + |
| 236 | +void ArduinoOTA_config() |
| 237 | +{ |
| 238 | + //OTA |
| 239 | + // Port defaults to 8266 |
| 240 | + ArduinoOTA.setPort(8266); |
| 241 | + ArduinoOTA.setHostname("esp-livingroomlight"); |
| 242 | + ArduinoOTA.setPassword(OTA_PASSWORD); |
| 243 | + ArduinoOTA.onStart([]() |
| 244 | + { |
| 245 | + //ticker.attach(0.1, tick); |
| 246 | + }); |
| 247 | + ArduinoOTA.onEnd([]() { }); |
| 248 | + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { }); |
| 249 | + ArduinoOTA.onError([](ota_error_t error) |
| 250 | + { |
| 251 | + //ESP.restart(); |
| 252 | + if (error == OTA_AUTH_ERROR) abort(); |
| 253 | + else if (error == OTA_BEGIN_ERROR) abort(); |
| 254 | + else if (error == OTA_CONNECT_ERROR) abort(); |
| 255 | + else if (error == OTA_RECEIVE_ERROR) abort(); |
| 256 | + else if (error == OTA_END_ERROR) abort(); |
| 257 | + }); |
| 258 | + |
| 259 | + ArduinoOTA.begin(); |
| 260 | +} |
| 261 | + |
| 262 | +void setup() |
| 263 | +{ |
| 264 | + pinMode(LED_PIN, OUTPUT); |
| 265 | + pinMode(RELAY_PIN, OUTPUT); |
| 266 | + pinMode(BUTTON_PIN, INPUT_PULLUP); |
| 267 | + |
| 268 | + startMills = lastRelayActionmillis = millis(); |
| 269 | + |
| 270 | + attachInterrupt(BUTTON_PIN, run_lightcmd_isr, RISING); |
| 271 | + |
| 272 | + ticker.attach(0.2, tick); |
| 273 | + |
| 274 | + wifi_connect(); |
| 275 | + |
| 276 | + clientName += "esp8266-"; |
| 277 | + uint8_t mac[6]; |
| 278 | + WiFi.macAddress(mac); |
| 279 | + clientName += macToStr(mac); |
| 280 | + clientName += "-"; |
| 281 | + clientName += String(micros() & 0xff, 16); |
| 282 | + |
| 283 | + getResetInfo = "hello from livingroomlight "; |
| 284 | + getResetInfo += ESP.getResetInfo().substring(0, 50); |
| 285 | + |
| 286 | + configTime(9 * 3600, 0, "pool.ntp.org", "time.nist.gov"); |
| 287 | + |
| 288 | + ArduinoOTA_config(); |
| 289 | + |
| 290 | + reconnect(); |
| 291 | + lastReconnectAttempt = 0; |
| 292 | +} |
| 293 | + |
| 294 | +void loop() |
| 295 | +{ |
| 296 | + if ((millis() - lastRelayActionmillis) > BETWEEN_RELAY_ACTIVE) |
| 297 | + { |
| 298 | + bRelayReady = true; |
| 299 | + } |
| 300 | + else |
| 301 | + { |
| 302 | + bRelayReady = false; |
| 303 | + } |
| 304 | + |
| 305 | + if (bUpdated) |
| 306 | + { |
| 307 | + if (bRelayReady) |
| 308 | + { |
| 309 | + change_light(); |
| 310 | + lastRelayActionmillis = millis(); |
| 311 | + bUpdated = false; |
| 312 | + } |
| 313 | + else |
| 314 | + { |
| 315 | + bUpdated = false; |
| 316 | + } |
| 317 | + } |
| 318 | + |
| 319 | + if (WiFi.status() == WL_CONNECTED) |
| 320 | + { |
| 321 | + if (!client.connected()) |
| 322 | + { |
| 323 | + long now = millis(); |
| 324 | + if (now - lastReconnectAttempt > 1000) |
| 325 | + { |
| 326 | + lastReconnectAttempt = now; |
| 327 | + if (reconnect()) |
| 328 | + { |
| 329 | + lastReconnectAttempt = 0; |
| 330 | + } |
| 331 | + } |
| 332 | + } |
| 333 | + else |
| 334 | + { |
| 335 | + if (bsendReport) |
| 336 | + { |
| 337 | + sendreport(); |
| 338 | + bsendReport = false; |
| 339 | + } |
| 340 | + |
| 341 | + if ((millis() - startMills) > REPORT_INTERVAL) |
| 342 | + { |
| 343 | + sendCheck(); |
| 344 | + startMills = millis(); |
| 345 | + } |
| 346 | + |
| 347 | + client.loop(); |
| 348 | + } |
| 349 | + ArduinoOTA.handle(); |
| 350 | + } |
| 351 | + else |
| 352 | + { |
| 353 | + ticker.attach(0.2, tick); |
| 354 | + wifi_connect(); |
| 355 | + } |
| 356 | +} |
| 357 | +// end of file |
0 commit comments