| 1 | 1 |
new file mode 100755 |
| ... | ... |
@@ -0,0 +1,284 @@ |
| 0 |
+#include <FS.h> //this needs to be first, e folosit de salvarea credentialelor pe FS |
|
| 1 |
+#include <ESP8266WiFi.h> |
|
| 2 |
+#include <PubSubClient.h> |
|
| 3 |
+#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager |
|
| 4 |
+#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson pentru salvarea credentialelor in eprom |
|
| 5 |
+#include <WiFiClient.h> |
|
| 6 |
+#include <OneWire.h> |
|
| 7 |
+#include <ESP8266HTTPClient.h> |
|
| 8 |
+#include <ESP8266httpUpdate.h> |
|
| 9 |
+ |
|
| 10 |
+#define DEBUG 1 //0=off 1=on |
|
| 11 |
+#define PIR D1 // pinul de date D1 la care se conecteaza PIRul |
|
| 12 |
+ |
|
| 13 |
+bool pir1,lastVAL; |
|
| 14 |
+float t_pir1; |
|
| 15 |
+ |
|
| 16 |
+char mqtt_user[9]; //le va lua din FS |
|
| 17 |
+char mqtt_devid[5]; //le va lua din FS |
|
| 18 |
+char mqttPassword[15]; //urmatoarele le va genera cu functia gen |
|
| 19 |
+char mqttSUB[22]; |
|
| 20 |
+char espName[13]; |
|
| 21 |
+char mqttESP[22]; |
|
| 22 |
+char mqttLWT[22]; |
|
| 23 |
+ |
|
| 24 |
+const String model = "NodeMCU PIR"; |
|
| 25 |
+const String ver = "v2.0.0"; |
|
| 26 |
+const char* mqttServer = "mqtt.clickhome.ro"; |
|
| 27 |
+const int mqttPort = 1883; |
|
| 28 |
+long loopTimer = 1000; // miliseconds - debug |
|
| 29 |
+long lastMsg = 0; |
|
| 30 |
+float loopTemp = 0; |
|
| 31 |
+String mqttMessage; |
|
| 32 |
+ |
|
| 33 |
+WiFiClient espClient; |
|
| 34 |
+PubSubClient client(espClient); |
|
| 35 |
+bool readConfigFile(); |
|
| 36 |
+ |
|
| 37 |
+void generate_vars(){
|
|
| 38 |
+ strcpy (mqttPassword, "8219CH"); |
|
| 39 |
+ strcat (mqttPassword, mqtt_user); |
|
| 40 |
+ |
|
| 41 |
+ strcpy (espName, mqtt_user); |
|
| 42 |
+ strcat (espName, "-"); |
|
| 43 |
+ strcat (espName, mqtt_devid); |
|
| 44 |
+ |
|
| 45 |
+ strcpy (mqttSUB, "cmnd/"); |
|
| 46 |
+ strcat (mqttSUB, espName); |
|
| 47 |
+ strcat (mqttSUB, "/ESP"); |
|
| 48 |
+ |
|
| 49 |
+ strcpy (mqttESP, "stat/"); |
|
| 50 |
+ strcat (mqttESP, espName); |
|
| 51 |
+ strcat (mqttESP, "/ESP"); |
|
| 52 |
+ |
|
| 53 |
+ strcpy (mqttLWT, "tele/"); |
|
| 54 |
+ strcat (mqttLWT, espName); |
|
| 55 |
+ strcat (mqttLWT, "/LWT"); |
|
| 56 |
+} |
|
| 57 |
+ |
|
| 58 |
+String ipToString(IPAddress ip){
|
|
| 59 |
+ String s=""; |
|
| 60 |
+ for (int i=0; i<4; i++) |
|
| 61 |
+ s += i ? "." + String(ip[i]) : String(ip[i]); |
|
| 62 |
+ return s; |
|
| 63 |
+} |
|
| 64 |
+String getMacAddress() {
|
|
| 65 |
+ byte mac[6]; |
|
| 66 |
+ WiFi.macAddress(mac); |
|
| 67 |
+ String cMac = ""; |
|
| 68 |
+ for (int i = 0; i < 6; ++i) {
|
|
| 69 |
+ cMac += String(mac[i],HEX); |
|
| 70 |
+ if(i<5) |
|
| 71 |
+ cMac += "-"; |
|
| 72 |
+ } |
|
| 73 |
+ cMac.toUpperCase(); |
|
| 74 |
+ return cMac; |
|
| 75 |
+} |
|
| 76 |
+ |
|
| 77 |
+void reconectez() {
|
|
| 78 |
+ while (String(mqtt_user)==""){
|
|
| 79 |
+ #if DEBUG |
|
| 80 |
+ Serial.println("Invalid user!");
|
|
| 81 |
+ #endif |
|
| 82 |
+ delay(99999999); |
|
| 83 |
+ } |
|
| 84 |
+ |
|
| 85 |
+ // ma conectez la mqtt server |
|
| 86 |
+ while (!client.connected()) {
|
|
| 87 |
+ client.setServer(mqttServer, mqttPort); |
|
| 88 |
+ #if DEBUG |
|
| 89 |
+ Serial.print("AtloopTempting MQTT connection...");
|
|
| 90 |
+ #endif |
|
| 91 |
+ // Incerc sa ma reconectez cu LWT din 5 in 5 secunde |
|
| 92 |
+ if (client.connect(espName, mqtt_user, mqttPassword, mqttLWT, 1, 1, "Offline")) {
|
|
| 93 |
+ #if DEBUG |
|
| 94 |
+ Serial.println("connected");
|
|
| 95 |
+ #endif |
|
| 96 |
+ client.publish(mqttLWT,"Online",TRUE); |
|
| 97 |
+ // trimit informatii utile cand ma conectez |
|
| 98 |
+ String esp_info = "{\"ESPMac\":\"";
|
|
| 99 |
+ esp_info += getMacAddress(); |
|
| 100 |
+ esp_info += "\",\"IPAddress\":\""; |
|
| 101 |
+ esp_info += ipToString(WiFi.localIP()); |
|
| 102 |
+ esp_info += "\"}"; |
|
| 103 |
+ String netinfo = "{\"Module\":\"";
|
|
| 104 |
+ netinfo += String (model); |
|
| 105 |
+ netinfo += "\",\"Version\":\""; |
|
| 106 |
+ netinfo += String (ver); |
|
| 107 |
+ netinfo += "\"}"; |
|
| 108 |
+ |
|
| 109 |
+ client.publish(mqttESP, netinfo.c_str(),TRUE); |
|
| 110 |
+ #if DEBUG |
|
| 111 |
+ Serial.println(netinfo); |
|
| 112 |
+ #endif |
|
| 113 |
+ client.publish(mqttESP, esp_info.c_str(),TRUE); |
|
| 114 |
+ #if DEBUG |
|
| 115 |
+ Serial.println(esp_info); |
|
| 116 |
+ #endif |
|
| 117 |
+ |
|
| 118 |
+ client.subscribe(mqttSUB); |
|
| 119 |
+ } else {
|
|
| 120 |
+ #if DEBUG |
|
| 121 |
+ Serial.print("failed, rc=");
|
|
| 122 |
+ Serial.print(client.state()); |
|
| 123 |
+ Serial.println(" try again in 60 seconds");
|
|
| 124 |
+ #endif |
|
| 125 |
+ // Wait 60 seconds before retrying |
|
| 126 |
+ delay(60000); |
|
| 127 |
+ } |
|
| 128 |
+ } |
|
| 129 |
+} |
|
| 130 |
+ |
|
| 131 |
+ |
|
| 132 |
+void setup() |
|
| 133 |
+{
|
|
| 134 |
+ pinMode(PIR,INPUT_PULLUP); |
|
| 135 |
+ |
|
| 136 |
+ #if DEBUG |
|
| 137 |
+ Serial.begin(115200); |
|
| 138 |
+ #endif |
|
| 139 |
+ |
|
| 140 |
+ // Mount the filesystem |
|
| 141 |
+ bool result = SPIFFS.begin(); |
|
| 142 |
+ #if DEBUG |
|
| 143 |
+ //Serial.println("SPIFFS opened: " + result);
|
|
| 144 |
+ #endif |
|
| 145 |
+ |
|
| 146 |
+ readConfigFile(); //citesc user si devid din memorie |
|
| 147 |
+ generate_vars(); //genereaza topicurile de mqtt in baza mqtt_user si mqtt_devid |
|
| 148 |
+ |
|
| 149 |
+ // ma conectez la AP via wifi |
|
| 150 |
+ WiFiManager wifi; |
|
| 151 |
+ wifi.setConfigPortalTimeout(120); // a timeout so the ESP doesn't hang waiting to be configured, for instance after a power failure |
|
| 152 |
+ wifi.setTimeout(180); // sta AP 3 minute apoi se reseteaza din nou |
|
| 153 |
+ if (!wifi.autoConnect("ClickHome")) {
|
|
| 154 |
+ #if DEBUG |
|
| 155 |
+ Serial.println("timeout - going to sleep");
|
|
| 156 |
+ #endif |
|
| 157 |
+ } |
|
| 158 |
+ delay(200); |
|
| 159 |
+ |
|
| 160 |
+ reconectez(); |
|
| 161 |
+ client.setCallback(getMessage); //setez functia care parseaza mesajele venite prin mqtt |
|
| 162 |
+ |
|
| 163 |
+} |
|
| 164 |
+ |
|
| 165 |
+void getMessage(char* topic, byte* payload, unsigned int length) {
|
|
| 166 |
+ float t = 0; |
|
| 167 |
+ mqttMessage=""; |
|
| 168 |
+ #if DEBUG |
|
| 169 |
+ Serial.print("Message arrived in topic: ");
|
|
| 170 |
+ Serial.println(topic); |
|
| 171 |
+ Serial.print("Message:");
|
|
| 172 |
+ #endif |
|
| 173 |
+ for (int i = 0; i < length; i++) {
|
|
| 174 |
+ mqttMessage += (char)payload[i]; |
|
| 175 |
+ } |
|
| 176 |
+ #if DEBUG |
|
| 177 |
+ Serial.println(mqttMessage); |
|
| 178 |
+ #endif |
|
| 179 |
+ |
|
| 180 |
+// procedura de software update via WEB |
|
| 181 |
+ if (mqttMessage == "update") |
|
| 182 |
+ {
|
|
| 183 |
+ String msg="Software update: "; |
|
| 184 |
+ t_httpUpdate_return ret; |
|
| 185 |
+ //ESPhttpUpdate.rebootOnUpdate(false); |
|
| 186 |
+ ret = ESPhttpUpdate.update("http://update.clickhome.ro/releu/arduino.bin");
|
|
| 187 |
+ switch(ret) {
|
|
| 188 |
+ case HTTP_UPDATE_FAILED: |
|
| 189 |
+ msg.concat(" eroare:");
|
|
| 190 |
+ msg.concat(ESPhttpUpdate.getLastError()); |
|
| 191 |
+ msg.concat(" motiv:");
|
|
| 192 |
+ msg.concat(ESPhttpUpdate.getLastErrorString().c_str()); |
|
| 193 |
+ break; |
|
| 194 |
+ case HTTP_UPDATE_NO_UPDATES: |
|
| 195 |
+ msg.concat(" no update.");
|
|
| 196 |
+ break; |
|
| 197 |
+ case HTTP_UPDATE_OK: |
|
| 198 |
+ msg.concat(" success.");
|
|
| 199 |
+ break; |
|
| 200 |
+ } |
|
| 201 |
+ #if DEBUG |
|
| 202 |
+ Serial.println(msg); |
|
| 203 |
+ #endif |
|
| 204 |
+ } |
|
| 205 |
+ |
|
| 206 |
+ // reset la ESP |
|
| 207 |
+ if (mqttMessage == "reset") |
|
| 208 |
+ {
|
|
| 209 |
+ String lastwords="Am fost resetat ..."; |
|
| 210 |
+ client.publish(mqttESP, lastwords.c_str(), TRUE); |
|
| 211 |
+ delay (3000); |
|
| 212 |
+ ESP.reset(); |
|
| 213 |
+ delay (5000); |
|
| 214 |
+ } |
|
| 215 |
+ |
|
| 216 |
+} |
|
| 217 |
+ |
|
| 218 |
+bool readConfigFile() {
|
|
| 219 |
+ // this opens the config file in read-mode |
|
| 220 |
+ File f = SPIFFS.open("/config.json", "r");
|
|
| 221 |
+ |
|
| 222 |
+ if (!f) {
|
|
| 223 |
+ #if DEBUG |
|
| 224 |
+ Serial.println("Configuration file not found");
|
|
| 225 |
+ #endif |
|
| 226 |
+ return false; |
|
| 227 |
+ } else {
|
|
| 228 |
+ // we could open the file |
|
| 229 |
+ size_t size = f.size(); |
|
| 230 |
+ // Allocate a buffer to store contents of the file. |
|
| 231 |
+ std::unique_ptr<char[]> buf(new char[size]); |
|
| 232 |
+ |
|
| 233 |
+ // Read and store file contents in buf |
|
| 234 |
+ f.readBytes(buf.get(), size); |
|
| 235 |
+ // Closing file |
|
| 236 |
+ f.close(); |
|
| 237 |
+ // Using dynamic JSON buffer which is not the recommended memory model, but anyway |
|
| 238 |
+ // See https://github.com/bblanchon/ArduinoJson/wiki/Memory%20model |
|
| 239 |
+ DynamicJsonBuffer jsonBuffer; |
|
| 240 |
+ // Parse JSON string |
|
| 241 |
+ JsonObject& json = jsonBuffer.parseObject(buf.get()); |
|
| 242 |
+ // Test if parsing succeeds. |
|
| 243 |
+ if (!json.success()) {
|
|
| 244 |
+ #if DEBUG |
|
| 245 |
+ Serial.println("JSON parseObject() failed");
|
|
| 246 |
+ #endif |
|
| 247 |
+ return false; |
|
| 248 |
+ } |
|
| 249 |
+ #if DEBUG |
|
| 250 |
+ json.printTo(Serial); |
|
| 251 |
+ #endif |
|
| 252 |
+ strcpy(mqtt_user, json["mqtt_user"]); |
|
| 253 |
+ strcpy(mqtt_devid, json["mqtt_devid"]); |
|
| 254 |
+ } |
|
| 255 |
+ return true; |
|
| 256 |
+} |
|
| 257 |
+ |
|
| 258 |
+void loop() |
|
| 259 |
+{
|
|
| 260 |
+ if (!client.connected()) |
|
| 261 |
+ {
|
|
| 262 |
+ reconectez(); |
|
| 263 |
+ } |
|
| 264 |
+ client.loop(); |
|
| 265 |
+ |
|
| 266 |
+ if (digitalRead(PIR)==1){ //verific daca nu e falsa alarma
|
|
| 267 |
+ if (t_pir1==0) |
|
| 268 |
+ t_pir1=millis(); |
|
| 269 |
+ if (millis() - t_pir1 > 1500) |
|
| 270 |
+ pir1=1; //este intradevar miscare |
|
| 271 |
+ }else{
|
|
| 272 |
+ if (t_pir1>0) t_pir1=0; |
|
| 273 |
+ pir1=0; |
|
| 274 |
+ } |
|
| 275 |
+ |
|
| 276 |
+ if (pir1!=lastVAL){
|
|
| 277 |
+ lastVAL=pir1; |
|
| 278 |
+ #if DEBUG |
|
| 279 |
+ Serial.println(pir1); |
|
| 280 |
+ #endif |
|
| 281 |
+ } |
|
| 282 |
+} |
|
| 283 |
+ |