| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,159 @@ |
| 0 |
+#include <FS.h> //this needs to be first, or it all crashes and burns... |
|
| 1 |
+#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino |
|
| 2 |
+#include <DNSServer.h> |
|
| 3 |
+#include <ESP8266WebServer.h> |
|
| 4 |
+#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager |
|
| 5 |
+#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson pentru salvarea credentialelor in eprom |
|
| 6 |
+ |
|
| 7 |
+ |
|
| 8 |
+ |
|
| 9 |
+const char* comanda="save"; //valori posibile: save, erase |
|
| 10 |
+char* set_mqtt_user="exwrgq"; |
|
| 11 |
+char* set_mqtt_devid="2638"; |
|
| 12 |
+ |
|
| 13 |
+ |
|
| 14 |
+ |
|
| 15 |
+/******************************* |
|
| 16 |
+* NU EDITA MAI JOS * |
|
| 17 |
+*******************************/ |
|
| 18 |
+ |
|
| 19 |
+char mqtt_user[9]; |
|
| 20 |
+char mqtt_devid[5]; |
|
| 21 |
+ |
|
| 22 |
+void setup() {
|
|
| 23 |
+ Serial.begin(115200); |
|
| 24 |
+ |
|
| 25 |
+ //clean FS, for testing |
|
| 26 |
+ if (comanda == "erase"){
|
|
| 27 |
+ SPIFFS.format(); |
|
| 28 |
+ Serial.println("am formatat !");
|
|
| 29 |
+ delay(1000000); |
|
| 30 |
+ } |
|
| 31 |
+ readFS(); |
|
| 32 |
+ |
|
| 33 |
+ // The extra parameters to be configured (can be either global or just in the setup) |
|
| 34 |
+ // After connecting, parameter.getValue() will get you the configured value |
|
| 35 |
+ // id/name placeholder/prompt default length |
|
| 36 |
+ WiFiManagerParameter custom_mqtt_user("user", "mqtt user", mqtt_user, 9);
|
|
| 37 |
+ WiFiManagerParameter custom_mqtt_devid("devid", "mqtt devid", mqtt_devid, 5);
|
|
| 38 |
+ |
|
| 39 |
+ //WiFiManager |
|
| 40 |
+ //Local intialization. Once its business is done, there is no need to keep it around |
|
| 41 |
+ WiFiManager wifiManager; |
|
| 42 |
+ |
|
| 43 |
+ //set config save notify callback |
|
| 44 |
+ //wifiManager.setSaveConfigCallback(saveConfigCallback); |
|
| 45 |
+ |
|
| 46 |
+ //add all your parameters here |
|
| 47 |
+ //sters wifiManager.addParameter(&custom_mqtt_user); //apare campul in interfata web alaturi de SSID si pass de la WIfi |
|
| 48 |
+ //sters wifiManager.addParameter(&custom_mqtt_devid); |
|
| 49 |
+ |
|
| 50 |
+ //reset settings - for testing |
|
| 51 |
+ //wifiManager.resetSettings(); |
|
| 52 |
+ |
|
| 53 |
+ //set minimu quality of signal so it ignores AP's under that quality |
|
| 54 |
+ //defaults to 8% |
|
| 55 |
+ //wifiManager.setMinimumSignalQuality(); |
|
| 56 |
+ |
|
| 57 |
+ //sets timeout until configuration portal gets turned off |
|
| 58 |
+ //useful to make it all retry or go to sleep |
|
| 59 |
+ //in seconds |
|
| 60 |
+ //wifiManager.setTimeout(120); |
|
| 61 |
+ |
|
| 62 |
+ //fetches ssid and pass and tries to connect |
|
| 63 |
+ //if it does not connect it starts an access point with the specified name |
|
| 64 |
+ //here "AutoConnectAP" |
|
| 65 |
+ //and goes into a blocking loop awaiting configuration |
|
| 66 |
+ /* |
|
| 67 |
+ if (!wifiManager.autoConnect("ClickHOME")) {
|
|
| 68 |
+ Serial.println("failed to connect and hit timeout");
|
|
| 69 |
+ delay(3000); |
|
| 70 |
+ //reset and try again, or maybe put it to deep sleep |
|
| 71 |
+ ESP.reset(); |
|
| 72 |
+ delay(5000); |
|
| 73 |
+ } |
|
| 74 |
+ */ |
|
| 75 |
+ |
|
| 76 |
+ //read updated parameters |
|
| 77 |
+ strcpy(mqtt_user, custom_mqtt_user.getValue()); |
|
| 78 |
+ strcpy(mqtt_devid, custom_mqtt_devid.getValue()); |
|
| 79 |
+ |
|
| 80 |
+ Serial.print("setup read updated mqtt_user:");
|
|
| 81 |
+ Serial.print(mqtt_user); |
|
| 82 |
+ Serial.println("read mqtt_devid:");
|
|
| 83 |
+ Serial.println(mqtt_devid); |
|
| 84 |
+ |
|
| 85 |
+ if (comanda == "save"){
|
|
| 86 |
+ strcpy(mqtt_user, set_mqtt_user); |
|
| 87 |
+ strcpy(mqtt_devid, set_mqtt_devid); |
|
| 88 |
+ saveFS(); |
|
| 89 |
+ } |
|
| 90 |
+} |
|
| 91 |
+ |
|
| 92 |
+void saveFS(){
|
|
| 93 |
+ //save the custom parameters to FS |
|
| 94 |
+ Serial.println("saving config");
|
|
| 95 |
+ DynamicJsonBuffer jsonBuffer; |
|
| 96 |
+ JsonObject& json = jsonBuffer.createObject(); |
|
| 97 |
+ json["mqtt_user"] = mqtt_user; |
|
| 98 |
+ json["mqtt_devid"] = mqtt_devid; |
|
| 99 |
+ |
|
| 100 |
+ File configFile = SPIFFS.open("/config.json", "w");
|
|
| 101 |
+ if (!configFile) {
|
|
| 102 |
+ Serial.println("failed to open config file for writing");
|
|
| 103 |
+ } |
|
| 104 |
+ |
|
| 105 |
+ json.printTo(Serial); |
|
| 106 |
+ json.printTo(configFile); |
|
| 107 |
+ configFile.close(); |
|
| 108 |
+ //end save |
|
| 109 |
+} |
|
| 110 |
+ |
|
| 111 |
+void readFS(){
|
|
| 112 |
+ //read configuration from FS json |
|
| 113 |
+ Serial.println("mounting FS...");
|
|
| 114 |
+ |
|
| 115 |
+ if (SPIFFS.begin()) {
|
|
| 116 |
+ Serial.println("mounted file system");
|
|
| 117 |
+ if (SPIFFS.exists("/config.json")) {
|
|
| 118 |
+ //file exists, reading and loading |
|
| 119 |
+ Serial.println("reading config file");
|
|
| 120 |
+ File configFile = SPIFFS.open("/config.json", "r");
|
|
| 121 |
+ if (configFile) {
|
|
| 122 |
+ Serial.println("opened config file");
|
|
| 123 |
+ |
|
| 124 |
+ size_t size = configFile.size(); |
|
| 125 |
+ // Allocate a buffer to store contents of the file. |
|
| 126 |
+ std::unique_ptr<char[]> buf(new char[size]); |
|
| 127 |
+ |
|
| 128 |
+ configFile.readBytes(buf.get(), size); |
|
| 129 |
+ DynamicJsonBuffer jsonBuffer; |
|
| 130 |
+ JsonObject& json = jsonBuffer.parseObject(buf.get()); |
|
| 131 |
+ json.printTo(Serial); |
|
| 132 |
+ if (json.success()) {
|
|
| 133 |
+ Serial.println("\nparsed json");
|
|
| 134 |
+ |
|
| 135 |
+ strcpy(mqtt_user, json["mqtt_user"]); |
|
| 136 |
+ strcpy(mqtt_devid, json["mqtt_devid"]); |
|
| 137 |
+ |
|
| 138 |
+ Serial.print("read from FS updated mqtt_user:");
|
|
| 139 |
+ Serial.println(mqtt_user); |
|
| 140 |
+ Serial.print("read mqtt_devid:");
|
|
| 141 |
+ Serial.println(mqtt_devid); |
|
| 142 |
+ |
|
| 143 |
+ |
|
| 144 |
+ } else {
|
|
| 145 |
+ Serial.println("failed to load json config");
|
|
| 146 |
+ } |
|
| 147 |
+ } |
|
| 148 |
+ } |
|
| 149 |
+ } else {
|
|
| 150 |
+ Serial.println("failed to mount FS");
|
|
| 151 |
+ } |
|
| 152 |
+ //end read |
|
| 153 |
+} |
|
| 154 |
+ |
|
| 155 |
+void loop() {
|
|
| 156 |
+ Serial.println("gata!");
|
|
| 157 |
+ delay(100000); |
|
| 158 |
+} |
| 0 | 159 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,219 @@ |
| 0 |
+#include <ESP8266WiFi.h> |
|
| 1 |
+#include <PubSubClient.h> |
|
| 2 |
+#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager |
|
| 3 |
+#include <WiFiClient.h> |
|
| 4 |
+#include <OneWire.h> |
|
| 5 |
+#include <ESP8266HTTPClient.h> |
|
| 6 |
+#include <ESP8266httpUpdate.h> |
|
| 7 |
+ |
|
| 8 |
+#define RELAY D2 // pinul de date (la mine am pus releu pe D2) |
|
| 9 |
+ |
|
| 10 |
+// Modifica cu datele tale |
|
| 11 |
+const char* mqttUser = "qbzesh"; |
|
| 12 |
+const char* mqttPassword = "8219CHqbzesh"; |
|
| 13 |
+const char* mqttSUB = "cmnd/qbzesh-1432/ESP"; |
|
| 14 |
+const char* mqttESP = "stat/qbzesh-1432/ESP"; |
|
| 15 |
+const char* mqttRELAY = "stat/qbzesh-1432/RELAY"; |
|
| 16 |
+const char* mqttLWT = "tele/qbzesh-1432/LWT"; |
|
| 17 |
+const char* espName ="qbzesh-1432"; |
|
| 18 |
+ |
|
| 19 |
+//####################################################################### |
|
| 20 |
+// NU EDITA MAI JOS |
|
| 21 |
+//####################################################################### |
|
| 22 |
+const String model = "NodeMCU Releu"; |
|
| 23 |
+const String ver = "v1.0.5"; |
|
| 24 |
+const char* mqttServer = "mqtt.clickhome.ro"; |
|
| 25 |
+const int mqttPort = 1883; |
|
| 26 |
+long loopTimer = 900000; // miliseconds - by default trimite la 15 minute |
|
| 27 |
+long lastMsg = 0; |
|
| 28 |
+float loopTemp = 0; |
|
| 29 |
+String mqttMessage; |
|
| 30 |
+ |
|
| 31 |
+WiFiClient espClient; |
|
| 32 |
+PubSubClient client(espClient); |
|
| 33 |
+ |
|
| 34 |
+String ipToString(IPAddress ip){
|
|
| 35 |
+ String s=""; |
|
| 36 |
+ for (int i=0; i<4; i++) |
|
| 37 |
+ s += i ? "." + String(ip[i]) : String(ip[i]); |
|
| 38 |
+ return s; |
|
| 39 |
+} |
|
| 40 |
+String getMacAddress() {
|
|
| 41 |
+ byte mac[6]; |
|
| 42 |
+ WiFi.macAddress(mac); |
|
| 43 |
+ String cMac = ""; |
|
| 44 |
+ for (int i = 0; i < 6; ++i) {
|
|
| 45 |
+ cMac += String(mac[i],HEX); |
|
| 46 |
+ if(i<5) |
|
| 47 |
+ cMac += "-"; |
|
| 48 |
+ } |
|
| 49 |
+ cMac.toUpperCase(); |
|
| 50 |
+ return cMac; |
|
| 51 |
+} |
|
| 52 |
+ |
|
| 53 |
+void reconnect() |
|
| 54 |
+{
|
|
| 55 |
+ while (!client.connected()) |
|
| 56 |
+ {
|
|
| 57 |
+ Serial.print("AtloopTempting MQTT connection...");
|
|
| 58 |
+ // Incerc sa ma reconectez cu LWT din 60 in 60 secunde |
|
| 59 |
+ if (client.connect(espName, mqttUser, mqttPassword, mqttLWT, 1, 1, "Offline")) |
|
| 60 |
+ {
|
|
| 61 |
+ Serial.println("Releu reconectat la MQTT.");
|
|
| 62 |
+ client.publish(mqttLWT,"Online",TRUE); |
|
| 63 |
+ // trimit informatii utile cand ma conectez |
|
| 64 |
+ String esp_info = "{\"ESPMac\":\"";
|
|
| 65 |
+ esp_info += getMacAddress(); |
|
| 66 |
+ esp_info += "\", \"IPAddress\":\""; |
|
| 67 |
+ esp_info += ipToString(WiFi.localIP()); |
|
| 68 |
+ esp_info += "\"}"; |
|
| 69 |
+ String netinfo = "{\"Module\":\"";
|
|
| 70 |
+ netinfo += String (model); |
|
| 71 |
+ netinfo += "\", \"Version\":\""; |
|
| 72 |
+ netinfo += String (ver); |
|
| 73 |
+ netinfo += "\"}"; |
|
| 74 |
+ |
|
| 75 |
+ client.publish(mqttESP, netinfo.c_str(),TRUE); |
|
| 76 |
+ Serial.println(netinfo); |
|
| 77 |
+ client.publish(mqttESP, esp_info.c_str(),TRUE); |
|
| 78 |
+ Serial.println(esp_info); |
|
| 79 |
+ client.subscribe(mqttSUB); |
|
| 80 |
+ } |
|
| 81 |
+ else {
|
|
| 82 |
+ Serial.println("Reconectare MQTT esuata: ");
|
|
| 83 |
+ Serial.println(client.state()); |
|
| 84 |
+ Serial.println("fac o alta incercare in 60 de secunde");
|
|
| 85 |
+ delay(60000); |
|
| 86 |
+ } |
|
| 87 |
+ } |
|
| 88 |
+} |
|
| 89 |
+ |
|
| 90 |
+void setup() |
|
| 91 |
+{
|
|
| 92 |
+ Serial.begin(115200); |
|
| 93 |
+ pinMode(RELAY,OUTPUT); |
|
| 94 |
+ |
|
| 95 |
+ // ma conectez la AP via wifi |
|
| 96 |
+ WiFiManager wifi; |
|
| 97 |
+ wifi.setTimeout(180); // sta AP 3 minute apoi se reseteaza din nou |
|
| 98 |
+ if (!wifi.autoConnect("ClickHome")) {
|
|
| 99 |
+ Serial.println("timeout - going to sleep");
|
|
| 100 |
+ } |
|
| 101 |
+ delay(200); |
|
| 102 |
+ |
|
| 103 |
+ // ma conectez la mqtt server |
|
| 104 |
+ client.setServer(mqttServer, mqttPort); |
|
| 105 |
+ while (!client.connected()) {
|
|
| 106 |
+ Serial.println("Connecting to MQTT...");
|
|
| 107 |
+ // fac connect cu LWT |
|
| 108 |
+ if (client.connect(espName, mqttUser, mqttPassword, mqttLWT, 1, 1, "Offline")) {
|
|
| 109 |
+ // trimit starea Online pe topic LWT cand ma conectez |
|
| 110 |
+ client.publish(mqttLWT,"Online",TRUE); |
|
| 111 |
+ // trimit starea OFF cand il aprind |
|
| 112 |
+ digitalWrite(RELAY,LOW); |
|
| 113 |
+ client.publish(mqttRELAY, "{\"Relay\":\"0\"}", TRUE);
|
|
| 114 |
+ Serial.println("connected");
|
|
| 115 |
+ // trimit informatii utile cand ma conectez |
|
| 116 |
+ String esp_info = "{\"ESPMac\":\"";
|
|
| 117 |
+ esp_info += getMacAddress(); |
|
| 118 |
+ esp_info += "\", \"IPAddress\":\""; |
|
| 119 |
+ esp_info += ipToString(WiFi.localIP()); |
|
| 120 |
+ esp_info += "\"}"; |
|
| 121 |
+ String netinfo = "{\"Module\":\"";
|
|
| 122 |
+ netinfo += String (model); |
|
| 123 |
+ netinfo += "\", \"Version\":\""; |
|
| 124 |
+ netinfo += String (ver); |
|
| 125 |
+ netinfo += "\"}"; |
|
| 126 |
+ |
|
| 127 |
+ client.publish(mqttESP, netinfo.c_str(),TRUE); |
|
| 128 |
+ Serial.println(netinfo); |
|
| 129 |
+ client.publish(mqttESP, esp_info.c_str(),TRUE); |
|
| 130 |
+ Serial.println(esp_info); |
|
| 131 |
+ client.subscribe(mqttSUB); |
|
| 132 |
+ } |
|
| 133 |
+ else {
|
|
| 134 |
+ Serial.print("Conectare MQTT esuata: ");
|
|
| 135 |
+ Serial.print(client.state()); |
|
| 136 |
+ delay(2000); |
|
| 137 |
+ } |
|
| 138 |
+ } |
|
| 139 |
+ // astept mesajul de la serverul MQTT |
|
| 140 |
+ client.setCallback(getMessage); |
|
| 141 |
+ // pinMode(RELAY,OUTPUT); |
|
| 142 |
+} |
|
| 143 |
+ |
|
| 144 |
+void getMessage(char* topic, byte* payload, unsigned int length) {
|
|
| 145 |
+ float t = 0; |
|
| 146 |
+ mqttMessage=""; |
|
| 147 |
+ Serial.print("Message arrived in topic: ");
|
|
| 148 |
+ Serial.println(topic); |
|
| 149 |
+ Serial.print("Message:");
|
|
| 150 |
+ for (int i = 0; i < length; i++) {
|
|
| 151 |
+ mqttMessage += (char)payload[i]; |
|
| 152 |
+ } |
|
| 153 |
+ Serial.println(mqttMessage); |
|
| 154 |
+ |
|
| 155 |
+ // daca a primit on |
|
| 156 |
+ if (mqttMessage == "ON") |
|
| 157 |
+ {
|
|
| 158 |
+ Serial.println("Pornesc releu");
|
|
| 159 |
+ digitalWrite(RELAY,HIGH); |
|
| 160 |
+ String json = "{\"Relay\":\"1\"}";
|
|
| 161 |
+ client.publish(mqttRELAY, json.c_str(),TRUE); |
|
| 162 |
+ } |
|
| 163 |
+ if (mqttMessage == "OFF") |
|
| 164 |
+ {
|
|
| 165 |
+ long now = millis(); |
|
| 166 |
+ Serial.println("Opresc releu");
|
|
| 167 |
+ Serial.println(now); |
|
| 168 |
+ digitalWrite(RELAY,LOW); |
|
| 169 |
+ String json = "{\"Relay\":\"0\"}";
|
|
| 170 |
+ client.publish(mqttRELAY, json.c_str(),TRUE); |
|
| 171 |
+ } |
|
| 172 |
+ // procedura de software update via WEB |
|
| 173 |
+ if (mqttMessage == "update") |
|
| 174 |
+ {
|
|
| 175 |
+ String msg="Software update: "; |
|
| 176 |
+ t_httpUpdate_return ret; |
|
| 177 |
+ //ESPhttpUpdate.rebootOnUpdate(false); |
|
| 178 |
+ ret = ESPhttpUpdate.update("http://update.clickhome.ro/releu/arduino.bin");
|
|
| 179 |
+ switch(ret) {
|
|
| 180 |
+ case HTTP_UPDATE_FAILED: |
|
| 181 |
+ msg.concat(" eroare:");
|
|
| 182 |
+ msg.concat(ESPhttpUpdate.getLastError()); |
|
| 183 |
+ msg.concat(" motiv:");
|
|
| 184 |
+ msg.concat(ESPhttpUpdate.getLastErrorString().c_str()); |
|
| 185 |
+ break; |
|
| 186 |
+ case HTTP_UPDATE_NO_UPDATES: |
|
| 187 |
+ msg.concat(" no update.");
|
|
| 188 |
+ break; |
|
| 189 |
+ case HTTP_UPDATE_OK: |
|
| 190 |
+ msg.concat(" success.");
|
|
| 191 |
+ break; |
|
| 192 |
+ } |
|
| 193 |
+ Serial.println(msg); |
|
| 194 |
+ } |
|
| 195 |
+ |
|
| 196 |
+ if (mqttMessage == "stare") |
|
| 197 |
+ {
|
|
| 198 |
+ // citesc starea si o trimit |
|
| 199 |
+ String json = "{\"Relay\":\"";
|
|
| 200 |
+ json += String (digitalRead(RELAY)); |
|
| 201 |
+ json += "\"}"; |
|
| 202 |
+ client.publish(mqttRELAY, json.c_str(),TRUE); |
|
| 203 |
+ } |
|
| 204 |
+} |
|
| 205 |
+void loop() |
|
| 206 |
+{
|
|
| 207 |
+ if (!client.connected()) |
|
| 208 |
+ {
|
|
| 209 |
+ reconnect(); |
|
| 210 |
+ } |
|
| 211 |
+ client.loop(); |
|
| 212 |
+ long now = millis(); |
|
| 213 |
+ if (now - lastMsg > loopTimer) |
|
| 214 |
+ {
|
|
| 215 |
+ lastMsg = now; |
|
| 216 |
+ Serial.println("Nu fac nimic in bucla");
|
|
| 217 |
+ } |
|
| 218 |
+} |
| 0 | 219 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,228 @@ |
| 0 |
+#include <ESP8266WiFi.h> |
|
| 1 |
+#include <PubSubClient.h> |
|
| 2 |
+#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager |
|
| 3 |
+#include <WiFiClient.h> |
|
| 4 |
+#include <OneWire.h> |
|
| 5 |
+#include <DallasTemperature.h> |
|
| 6 |
+ |
|
| 7 |
+// Modifica cu datele tale |
|
| 8 |
+const char* mqttUser = "rqx958t"; |
|
| 9 |
+const char* mqttPassword = "8219CHrqx958t"; |
|
| 10 |
+const char* mqttSUB = "cmnd/rqx958t-1460/ESP"; |
|
| 11 |
+const char* mqttESP = "stat/rqx958t-1460/ESP"; |
|
| 12 |
+const char* mqttTEMP = "stat/rqx958t-1460/TEMP"; |
|
| 13 |
+const char* mqttLWT = "tele/rqx958t-1460/LWT"; |
|
| 14 |
+const char* espName ="rqx958t-1460"; |
|
| 15 |
+ |
|
| 16 |
+//####################################################################### |
|
| 17 |
+// NU EDITA MAI JOS |
|
| 18 |
+//####################################################################### |
|
| 19 |
+const String model = "NodeMCU Dallas"; |
|
| 20 |
+const String ver = "v1.0.5"; |
|
| 21 |
+const char* mqttServer = "mqtt.clickhome.ro"; |
|
| 22 |
+const int mqttPort = 1883; |
|
| 23 |
+long loopTimer = 900000; // miliseconds - by default trimite la 15 minute |
|
| 24 |
+long lastMsg = 0; |
|
| 25 |
+float loopTemp = 0; |
|
| 26 |
+int inPin = 5; |
|
| 27 |
+String mqttMessage; |
|
| 28 |
+ |
|
| 29 |
+// senzor de temperatura DALLAS |
|
| 30 |
+#define ONE_WIRE_BUS D4 // pinul de date (la mine am pus senzor pe D4) |
|
| 31 |
+OneWire oneWire(ONE_WIRE_BUS); |
|
| 32 |
+DallasTemperature sensors(&oneWire); |
|
| 33 |
+ |
|
| 34 |
+//********************************************************************* |
|
| 35 |
+ |
|
| 36 |
+WiFiClient espClient; |
|
| 37 |
+PubSubClient client(espClient); |
|
| 38 |
+ |
|
| 39 |
+ |
|
| 40 |
+String ipToString(IPAddress ip){
|
|
| 41 |
+ String s=""; |
|
| 42 |
+ for (int i=0; i<4; i++) |
|
| 43 |
+ s += i ? "." + String(ip[i]) : String(ip[i]); |
|
| 44 |
+ return s; |
|
| 45 |
+} |
|
| 46 |
+ |
|
| 47 |
+String getMacAddress() {
|
|
| 48 |
+ byte mac[6]; |
|
| 49 |
+ WiFi.macAddress(mac); |
|
| 50 |
+ String cMac = ""; |
|
| 51 |
+ for (int i = 0; i < 6; ++i) {
|
|
| 52 |
+ cMac += String(mac[i],HEX); |
|
| 53 |
+ if(i<5) |
|
| 54 |
+ cMac += "-"; |
|
| 55 |
+ } |
|
| 56 |
+ cMac.toUpperCase(); |
|
| 57 |
+ return cMac; |
|
| 58 |
+} |
|
| 59 |
+ |
|
| 60 |
+void reconnect() {
|
|
| 61 |
+ // Loop until we're reconnected |
|
| 62 |
+ while (!client.connected()) {
|
|
| 63 |
+ Serial.print("AtloopTempting MQTT connection...");
|
|
| 64 |
+ // Incerc sa ma reconectez cu LWT din 5 in 5 secunde |
|
| 65 |
+ if (client.connect(espName, mqttUser, mqttPassword, mqttLWT, 1, 1, "Offline")) {
|
|
| 66 |
+ Serial.println("connected");
|
|
| 67 |
+ client.publish(mqttLWT,"Online",TRUE); |
|
| 68 |
+ // trimit informatii utile cand ma conectez |
|
| 69 |
+ String esp_info = "{\"ESPMac\":\"";
|
|
| 70 |
+ esp_info += getMacAddress(); |
|
| 71 |
+ esp_info += "\", \"IPAddress\":\""; |
|
| 72 |
+ esp_info += ipToString(WiFi.localIP()); |
|
| 73 |
+ esp_info += "\"}"; |
|
| 74 |
+ String netinfo = "{\"Module\":\"";
|
|
| 75 |
+ netinfo += String (model); |
|
| 76 |
+ netinfo += "\", \"Version\":\""; |
|
| 77 |
+ netinfo += String (ver); |
|
| 78 |
+ netinfo += "\"}"; |
|
| 79 |
+ |
|
| 80 |
+ client.publish(mqttESP, netinfo.c_str(),TRUE); |
|
| 81 |
+ Serial.println(netinfo); |
|
| 82 |
+ client.publish(mqttESP, esp_info.c_str(),TRUE); |
|
| 83 |
+ Serial.println(esp_info); |
|
| 84 |
+ client.publish(mqttESP, "15 minute",TRUE); // trimit intervalul default |
|
| 85 |
+ client.subscribe(mqttSUB); |
|
| 86 |
+ } else {
|
|
| 87 |
+ Serial.print("failed, rc=");
|
|
| 88 |
+ Serial.print(client.state()); |
|
| 89 |
+ Serial.println(" try again in 60 seconds");
|
|
| 90 |
+ // Wait 5 seconds before retrying |
|
| 91 |
+ delay(60000); |
|
| 92 |
+ } |
|
| 93 |
+ } |
|
| 94 |
+} |
|
| 95 |
+ |
|
| 96 |
+void setup() |
|
| 97 |
+{
|
|
| 98 |
+ Serial.begin(115200); |
|
| 99 |
+ |
|
| 100 |
+ // ma conectez la AP via wifi |
|
| 101 |
+ WiFiManager wifi; |
|
| 102 |
+ wifi.setTimeout(180); // sta AP 3 minute apoi se reseteaza din nou |
|
| 103 |
+ if (!wifi.autoConnect("ClickHome")) {
|
|
| 104 |
+ Serial.println("timeout - going to sleep");
|
|
| 105 |
+ } |
|
| 106 |
+ delay(200); |
|
| 107 |
+ |
|
| 108 |
+ // ma conectez la mqtt server |
|
| 109 |
+ client.setServer(mqttServer, mqttPort); |
|
| 110 |
+ while (!client.connected()) {
|
|
| 111 |
+ Serial.println("Connecting to MQTT...");
|
|
| 112 |
+ // fac connect cu LWT |
|
| 113 |
+ if (client.connect(espName, mqttUser, mqttPassword, mqttLWT, 1, 1, "Offline")) {
|
|
| 114 |
+ // trimit starea Online pe topic LWT cand ma conectez |
|
| 115 |
+ client.publish(mqttLWT,"Online",TRUE); |
|
| 116 |
+ // trimit informatii utile cand ma conectez |
|
| 117 |
+ String esp_info = "{\"ESPMac\":\"";
|
|
| 118 |
+ esp_info += getMacAddress(); |
|
| 119 |
+ esp_info += "\", \"IPAddress\":\""; |
|
| 120 |
+ esp_info += ipToString(WiFi.localIP()); |
|
| 121 |
+ esp_info += "\"}"; |
|
| 122 |
+ String netinfo = "{\"Module\":\"";
|
|
| 123 |
+ netinfo += String (model); |
|
| 124 |
+ netinfo += "\", \"Version\":\""; |
|
| 125 |
+ netinfo += String (ver); |
|
| 126 |
+ netinfo += "\"}"; |
|
| 127 |
+ client.publish(mqttESP, netinfo.c_str(),TRUE); |
|
| 128 |
+ Serial.println(netinfo); |
|
| 129 |
+ client.publish(mqttESP, esp_info.c_str(),TRUE); |
|
| 130 |
+ Serial.println(esp_info); |
|
| 131 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"15 minute\"}",TRUE);
|
|
| 132 |
+ Serial.println("connected");
|
|
| 133 |
+ client.subscribe(mqttSUB); |
|
| 134 |
+ } |
|
| 135 |
+ else {
|
|
| 136 |
+ Serial.print("failed with state ");
|
|
| 137 |
+ Serial.print(client.state()); |
|
| 138 |
+ delay(2000); |
|
| 139 |
+ } |
|
| 140 |
+ } |
|
| 141 |
+ // astept mesajul de la serverul MQTT |
|
| 142 |
+ client.setCallback(getMessage); |
|
| 143 |
+ pinMode(inPin, INPUT); |
|
| 144 |
+ sensors.begin(); |
|
| 145 |
+} |
|
| 146 |
+ |
|
| 147 |
+void getMessage(char* topic, byte* payload, unsigned int length) {
|
|
| 148 |
+ float t = 0; |
|
| 149 |
+ mqttMessage=""; |
|
| 150 |
+ Serial.print("Message arrived in topic: ");
|
|
| 151 |
+ Serial.println(topic); |
|
| 152 |
+ Serial.print("Message:");
|
|
| 153 |
+ for (int i = 0; i < length; i++) {
|
|
| 154 |
+ mqttMessage += (char)payload[i]; |
|
| 155 |
+ } |
|
| 156 |
+ Serial.println(mqttMessage); |
|
| 157 |
+ |
|
| 158 |
+ // daca a primit sendTemp trimite temperatura |
|
| 159 |
+ if (mqttMessage == "temp") |
|
| 160 |
+ {
|
|
| 161 |
+ Serial.println("Trimit temperatura");
|
|
| 162 |
+ sensors.setResolution(12); |
|
| 163 |
+ sensors.requestTemperatures(); // Send the command to get Temperatures |
|
| 164 |
+ t = sensors.getTempCByIndex(0); |
|
| 165 |
+ String json = "{\"Temp\":";
|
|
| 166 |
+ json += String (t); |
|
| 167 |
+ json += "}"; |
|
| 168 |
+ client.publish(mqttTEMP, json.c_str(),TRUE); |
|
| 169 |
+ Serial.println(json); |
|
| 170 |
+ } |
|
| 171 |
+ |
|
| 172 |
+ // daca a primit valoarea in minute seteaza loopTimer si-l trimite in DB |
|
| 173 |
+ |
|
| 174 |
+ if (mqttMessage == "debug") |
|
| 175 |
+ {
|
|
| 176 |
+ loopTimer=30000; // setez sa trimita valori la 30 de secunde ca sa fac debug |
|
| 177 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"30 secunde\"}",TRUE);
|
|
| 178 |
+ } |
|
| 179 |
+ if (mqttMessage == "10") |
|
| 180 |
+ {
|
|
| 181 |
+ loopTimer=600000; // setez sa trimita valori la 10 minute |
|
| 182 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"10 minute\"}",TRUE);
|
|
| 183 |
+ } |
|
| 184 |
+ if (mqttMessage == "15") |
|
| 185 |
+ {
|
|
| 186 |
+ loopTimer=900000; // setez sa trimita valori la 15 minute |
|
| 187 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"15 minute\"}",TRUE);
|
|
| 188 |
+ } |
|
| 189 |
+ if (mqttMessage == "30") |
|
| 190 |
+ {
|
|
| 191 |
+ loopTimer=1800000; // setez sa trimita valori la 30 de minute |
|
| 192 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"30 de minute\"}",TRUE);
|
|
| 193 |
+ } |
|
| 194 |
+ if (mqttMessage == "60") |
|
| 195 |
+ {
|
|
| 196 |
+ loopTimer=3600000; // setez sa trimita valori la o ora |
|
| 197 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"o ora\"}",TRUE);
|
|
| 198 |
+ } |
|
| 199 |
+ if (mqttMessage == "stop") |
|
| 200 |
+ {
|
|
| 201 |
+ loopTimer=3600000000; // setez sa nu trimita valori |
|
| 202 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"oprit\"}",TRUE);
|
|
| 203 |
+ } |
|
| 204 |
+} |
|
| 205 |
+ |
|
| 206 |
+void loop() |
|
| 207 |
+{
|
|
| 208 |
+ if (!client.connected()) {
|
|
| 209 |
+ reconnect(); |
|
| 210 |
+ } |
|
| 211 |
+ client.loop(); |
|
| 212 |
+ long now = millis(); |
|
| 213 |
+ if (now - lastMsg > loopTimer) {
|
|
| 214 |
+ lastMsg = now; |
|
| 215 |
+ sensors.setResolution(12); |
|
| 216 |
+ sensors.requestTemperatures(); // Send the command to get loopTemperatures |
|
| 217 |
+ loopTemp = sensors.getTempCByIndex(0); |
|
| 218 |
+ Serial.println(loopTemp); |
|
| 219 |
+ if((loopTemp > -20) && (loopTemp <60)) |
|
| 220 |
+ {
|
|
| 221 |
+ String val = "{\"Temp\":";
|
|
| 222 |
+ val += String (loopTemp); |
|
| 223 |
+ val += "}"; |
|
| 224 |
+ client.publish(mqttESP, val.c_str(),TRUE); |
|
| 225 |
+ } |
|
| 226 |
+ } |
|
| 227 |
+} |
| 0 | 228 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,278 @@ |
| 0 |
+#include <ESP8266WiFi.h> |
|
| 1 |
+#include <PubSubClient.h> |
|
| 2 |
+#include <WiFiManager.h> |
|
| 3 |
+#include <WiFiClient.h> |
|
| 4 |
+#include <OneWire.h> |
|
| 5 |
+#include <PietteTech_DHT.h> |
|
| 6 |
+#include <ESP8266HTTPClient.h> |
|
| 7 |
+#include <ESP8266httpUpdate.h> |
|
| 8 |
+ |
|
| 9 |
+// Modifica cu datele tale |
|
| 10 |
+const char* mqttUser = "qbzesh"; |
|
| 11 |
+const char* mqttPassword = "8219CHqbzesh"; |
|
| 12 |
+const char* mqttSUB = "cmnd/qbzesh-1250/ESP"; |
|
| 13 |
+const char* mqttESP = "stat/qbzesh-1250/ESP"; |
|
| 14 |
+const char* mqttTEMP = "stat/qbzesh-1250/TEMP"; |
|
| 15 |
+const char* mqttLWT = "tele/qbzesh-1250/LWT"; |
|
| 16 |
+const char* espName ="qbzesh-1250"; |
|
| 17 |
+ |
|
| 18 |
+//####################################################################### |
|
| 19 |
+// NU EDITA MAI JOS |
|
| 20 |
+//####################################################################### |
|
| 21 |
+const String model = "NodeMCU DHT22"; |
|
| 22 |
+const String ver = "v1.0.6"; |
|
| 23 |
+const char* mqttServer = "mqtt.clickhome.ro"; |
|
| 24 |
+const int mqttPort = 1883; |
|
| 25 |
+long loopTimer = 900000; // miliseconds - by default trimite la 15 minute |
|
| 26 |
+long lastMsg = 0; |
|
| 27 |
+float loopTemp = 0; |
|
| 28 |
+float t = 0; |
|
| 29 |
+float loopHumi = 0; |
|
| 30 |
+float h = 0; |
|
| 31 |
+String mqttMessage; |
|
| 32 |
+bool bDHTstarted; |
|
| 33 |
+int acquireresult; |
|
| 34 |
+ |
|
| 35 |
+// senzor de temperatura DHT |
|
| 36 |
+#define DHTPIN D4 // pinul de date (la mine am pus senzor pe D4) |
|
| 37 |
+#define DHTTYPE DHT22 // DHT 11 |
|
| 38 |
+void dht_wrapper(); |
|
| 39 |
+PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper); |
|
| 40 |
+void dht_wrapper() |
|
| 41 |
+{
|
|
| 42 |
+ DHT.isrCallback(); |
|
| 43 |
+} |
|
| 44 |
+//********************************************************************* |
|
| 45 |
+ |
|
| 46 |
+WiFiClient espClient; |
|
| 47 |
+PubSubClient client(espClient); |
|
| 48 |
+ |
|
| 49 |
+ |
|
| 50 |
+String ipToString(IPAddress ip){
|
|
| 51 |
+ String s=""; |
|
| 52 |
+ for (int i=0; i<4; i++) |
|
| 53 |
+ s += i ? "." + String(ip[i]) : String(ip[i]); |
|
| 54 |
+ return s; |
|
| 55 |
+} |
|
| 56 |
+ |
|
| 57 |
+String getMacAddress() {
|
|
| 58 |
+ byte mac[6]; |
|
| 59 |
+ WiFi.macAddress(mac); |
|
| 60 |
+ String cMac = ""; |
|
| 61 |
+ for (int i = 0; i < 6; ++i) {
|
|
| 62 |
+ cMac += String(mac[i],HEX); |
|
| 63 |
+ if(i<5) |
|
| 64 |
+ cMac += "-"; |
|
| 65 |
+ } |
|
| 66 |
+ cMac.toUpperCase(); |
|
| 67 |
+ return cMac; |
|
| 68 |
+} |
|
| 69 |
+ |
|
| 70 |
+void reconnect() |
|
| 71 |
+{
|
|
| 72 |
+ while (!client.connected()) |
|
| 73 |
+ {
|
|
| 74 |
+ //Serial.println("Reconectare la MQTT ...");
|
|
| 75 |
+ // Incerc sa ma reconectez cu LWT din 60 in 60 secunde |
|
| 76 |
+ if (client.connect(espName, mqttUser, mqttPassword, mqttLWT, 1, 1, "Offline")) |
|
| 77 |
+ {
|
|
| 78 |
+ Serial.println("Senzor reconectat la MQTT.");
|
|
| 79 |
+ client.publish(mqttLWT,"Online",TRUE); |
|
| 80 |
+ // trimit informatii utile cand ma conectez |
|
| 81 |
+ String esp_info = "{\"ESPMac\":\"";
|
|
| 82 |
+ esp_info += getMacAddress(); |
|
| 83 |
+ esp_info += "\", \"IPAddress\":\""; |
|
| 84 |
+ esp_info += ipToString(WiFi.localIP()); |
|
| 85 |
+ esp_info += "\"}"; |
|
| 86 |
+ String netinfo = "{\"Module\":\"";
|
|
| 87 |
+ netinfo += String (model); |
|
| 88 |
+ netinfo += "\", \"Version\":\""; |
|
| 89 |
+ netinfo += String (ver); |
|
| 90 |
+ netinfo += "\"}"; |
|
| 91 |
+ |
|
| 92 |
+ client.publish(mqttESP, netinfo.c_str(),TRUE); |
|
| 93 |
+ Serial.println(netinfo); |
|
| 94 |
+ client.publish(mqttESP, esp_info.c_str(),TRUE); |
|
| 95 |
+ Serial.println(esp_info); |
|
| 96 |
+ client.publish(mqttESP, "15 minute",TRUE); // trimit intervalul default |
|
| 97 |
+ client.subscribe(mqttSUB); |
|
| 98 |
+ } |
|
| 99 |
+ else |
|
| 100 |
+ {
|
|
| 101 |
+ Serial.println("Reconectare MQTT esuata: ");
|
|
| 102 |
+ Serial.println(client.state()); |
|
| 103 |
+ Serial.println("fac o alta incercare in 60 de secunde");
|
|
| 104 |
+ delay(60000); |
|
| 105 |
+ } |
|
| 106 |
+ } |
|
| 107 |
+} |
|
| 108 |
+ |
|
| 109 |
+void setup() |
|
| 110 |
+{
|
|
| 111 |
+ Serial.begin(115200); |
|
| 112 |
+ |
|
| 113 |
+ // ma conectez la AP via wifi |
|
| 114 |
+ WiFiManager wifi; |
|
| 115 |
+ wifi.setTimeout(180); // sta AP 3 minute apoi se reseteaza din nou |
|
| 116 |
+ if (!wifi.autoConnect("ClickHome")) {
|
|
| 117 |
+ Serial.println("timeout - going to sleep");
|
|
| 118 |
+ } |
|
| 119 |
+ delay(200); |
|
| 120 |
+ |
|
| 121 |
+ // ma conectez la mqtt server |
|
| 122 |
+ client.setServer(mqttServer, mqttPort); |
|
| 123 |
+ while (!client.connected()) {
|
|
| 124 |
+ Serial.println("Conectare la MQTT ...");
|
|
| 125 |
+ // fac connect cu LWT |
|
| 126 |
+ if (client.connect(espName, mqttUser, mqttPassword, mqttLWT, 1, 1, "Offline")) {
|
|
| 127 |
+ Serial.println("Senzor conectat la MQTT.");
|
|
| 128 |
+ // trimit starea Online pe topic LWT cand ma conectez |
|
| 129 |
+ client.publish(mqttLWT,"Online",TRUE); |
|
| 130 |
+ // trimit informatii utile cand ma conectez |
|
| 131 |
+ String esp_info = "{\"ESPMac\":\"";
|
|
| 132 |
+ esp_info += getMacAddress(); |
|
| 133 |
+ esp_info += "\", \"IPAddress\":\""; |
|
| 134 |
+ esp_info += ipToString(WiFi.localIP()); |
|
| 135 |
+ esp_info += "\"}"; |
|
| 136 |
+ String netinfo = "{\"Module\":\"";
|
|
| 137 |
+ netinfo += String (model); |
|
| 138 |
+ netinfo += "\", \"Version\":\""; |
|
| 139 |
+ netinfo += String (ver); |
|
| 140 |
+ netinfo += "\"}"; |
|
| 141 |
+ client.publish(mqttESP, netinfo.c_str(),TRUE); |
|
| 142 |
+ Serial.println(netinfo); |
|
| 143 |
+ client.publish(mqttESP, esp_info.c_str(),TRUE); |
|
| 144 |
+ Serial.println(esp_info); |
|
| 145 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"15 minute\"}",TRUE);
|
|
| 146 |
+ client.subscribe(mqttSUB); |
|
| 147 |
+ } |
|
| 148 |
+ else {
|
|
| 149 |
+ Serial.print("Conectare MQTT esuata: ");
|
|
| 150 |
+ Serial.print(client.state()); |
|
| 151 |
+ delay(2000); |
|
| 152 |
+ } |
|
| 153 |
+ } |
|
| 154 |
+ client.setCallback(getMessage); |
|
| 155 |
+} |
|
| 156 |
+ |
|
| 157 |
+void getMessage(char* topic, byte* payload, unsigned int length) {
|
|
| 158 |
+ mqttMessage=""; |
|
| 159 |
+ Serial.print("Message arrived in topic: ");
|
|
| 160 |
+ Serial.println(topic); |
|
| 161 |
+ Serial.print("Message:");
|
|
| 162 |
+ for (int i = 0; i < length; i++) {
|
|
| 163 |
+ mqttMessage += (char)payload[i]; |
|
| 164 |
+ } |
|
| 165 |
+ Serial.println(mqttMessage); |
|
| 166 |
+ |
|
| 167 |
+ // daca a primit temp va trimite temperatura |
|
| 168 |
+ if (mqttMessage == "temp") |
|
| 169 |
+ {
|
|
| 170 |
+ Serial.println("Trimit temperatura si umiditatea");
|
|
| 171 |
+ acquireresult = DHT.acquireAndWait(2000); |
|
| 172 |
+ if ( acquireresult == 0 ) |
|
| 173 |
+ {
|
|
| 174 |
+ t = DHT.getCelsius(); |
|
| 175 |
+ h = DHT.getHumidity(); |
|
| 176 |
+ delay(10); |
|
| 177 |
+ String json = "{\"Temp\":";
|
|
| 178 |
+ json += String (t); |
|
| 179 |
+ json += ", \"Humi\":"; |
|
| 180 |
+ json += String (h); |
|
| 181 |
+ json += "}"; |
|
| 182 |
+ client.publish(mqttTEMP, json.c_str(),TRUE); |
|
| 183 |
+ Serial.println(json); |
|
| 184 |
+ } |
|
| 185 |
+ else |
|
| 186 |
+ {
|
|
| 187 |
+ t = h = 0; |
|
| 188 |
+ Serial.println("Nu am putut citi temperatura si umiditatea");
|
|
| 189 |
+ } |
|
| 190 |
+ } |
|
| 191 |
+ |
|
| 192 |
+ // procedura de software update via WEB |
|
| 193 |
+ if (mqttMessage == "update") |
|
| 194 |
+ {
|
|
| 195 |
+ String msg="Software update: "; |
|
| 196 |
+ t_httpUpdate_return ret; |
|
| 197 |
+ //ESPhttpUpdate.rebootOnUpdate(false); |
|
| 198 |
+ ret = ESPhttpUpdate.update("http://update.clickhome.ro/senzor/dht/arduino.bin");
|
|
| 199 |
+ //ret = ESPhttpUpdate.update("update.clickhome.ro", 80, "/senzor/dht/arduino.bin");
|
|
| 200 |
+ switch(ret) {
|
|
| 201 |
+ case HTTP_UPDATE_FAILED: |
|
| 202 |
+ msg.concat(" eroare:");
|
|
| 203 |
+ msg.concat(ESPhttpUpdate.getLastError()); |
|
| 204 |
+ msg.concat(" motiv:");
|
|
| 205 |
+ msg.concat(ESPhttpUpdate.getLastErrorString().c_str()); |
|
| 206 |
+ break; |
|
| 207 |
+ case HTTP_UPDATE_NO_UPDATES: |
|
| 208 |
+ msg.concat(" no update.");
|
|
| 209 |
+ break; |
|
| 210 |
+ case HTTP_UPDATE_OK: |
|
| 211 |
+ msg.concat(" success.");
|
|
| 212 |
+ break; |
|
| 213 |
+ } |
|
| 214 |
+ Serial.println(msg); |
|
| 215 |
+ } |
|
| 216 |
+ |
|
| 217 |
+ // daca a primit valoarea in minute seteaza loopTimer si-l trimite in DB |
|
| 218 |
+ if (mqttMessage == "debug") |
|
| 219 |
+ {
|
|
| 220 |
+ loopTimer=30000; // setez sa trimita valori la 30 de secunde ca sa fac debug |
|
| 221 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"30 secunde\"}",TRUE);
|
|
| 222 |
+ } |
|
| 223 |
+ if (mqttMessage == "10") |
|
| 224 |
+ {
|
|
| 225 |
+ loopTimer=600000; // setez sa trimita valori la 10 minute |
|
| 226 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"10 minute\"}",TRUE);
|
|
| 227 |
+ } |
|
| 228 |
+ if (mqttMessage == "15") |
|
| 229 |
+ {
|
|
| 230 |
+ loopTimer=900000; // setez sa trimita valori la 15 minute |
|
| 231 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"15 minute\"}",TRUE);
|
|
| 232 |
+ } |
|
| 233 |
+ if (mqttMessage == "30") |
|
| 234 |
+ {
|
|
| 235 |
+ loopTimer=1800000; // setez sa trimita valori la 30 de minute |
|
| 236 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"30 de minute\"}",TRUE);
|
|
| 237 |
+ } |
|
| 238 |
+ if (mqttMessage == "60") |
|
| 239 |
+ {
|
|
| 240 |
+ loopTimer=3600000; // setez sa trimita valori la o ora |
|
| 241 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"o ora\"}",TRUE);
|
|
| 242 |
+ } |
|
| 243 |
+ if (mqttMessage == "stop") |
|
| 244 |
+ {
|
|
| 245 |
+ loopTimer=3600000000; // setez sa nu trimita valori |
|
| 246 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"oprit\"}",TRUE);
|
|
| 247 |
+ } |
|
| 248 |
+} |
|
| 249 |
+ |
|
| 250 |
+void loop() |
|
| 251 |
+{
|
|
| 252 |
+ if (!client.connected()) {
|
|
| 253 |
+ reconnect(); |
|
| 254 |
+ } |
|
| 255 |
+ client.loop(); |
|
| 256 |
+ long now = millis(); |
|
| 257 |
+ if (now - lastMsg > loopTimer) {
|
|
| 258 |
+ lastMsg = now; |
|
| 259 |
+ acquireresult = DHT.acquireAndWait(2000); |
|
| 260 |
+ if ( acquireresult == 0 ) |
|
| 261 |
+ {
|
|
| 262 |
+ loopTemp = DHT.getCelsius(); |
|
| 263 |
+ loopHumi = DHT.getHumidity(); |
|
| 264 |
+ delay(10); |
|
| 265 |
+ String val = "{\"Temp\":";
|
|
| 266 |
+ val += String (loopTemp); |
|
| 267 |
+ val += ", \"Humi\":"; |
|
| 268 |
+ val += String (loopHumi); |
|
| 269 |
+ val += "}"; |
|
| 270 |
+ client.publish(mqttESP, val.c_str(),TRUE); |
|
| 271 |
+ } |
|
| 272 |
+ else |
|
| 273 |
+ {
|
|
| 274 |
+ loopTemp = loopHumi = 0; |
|
| 275 |
+ } |
|
| 276 |
+ } |
|
| 277 |
+} |