| 1 | 1 |
new file mode 100755 |
| ... | ... |
@@ -0,0 +1,258 @@ |
| 0 |
+#include <ESP8266WiFi.h> |
|
| 1 |
+#include <PubSubClient.h> |
|
| 2 |
+#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager |
|
| 3 |
+#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson pentru salvarea credentialelor in eprom |
|
| 4 |
+#include <WiFiClient.h> |
|
| 5 |
+#include <OneWire.h> |
|
| 6 |
+#include <DallasTemperature.h> |
|
| 7 |
+ |
|
| 8 |
+//ESP8266-01 nu are suficienta memorie pt update http |
|
| 9 |
+const char* mqtt_user="64x35up"; |
|
| 10 |
+const char* mqtt_devid="1790"; |
|
| 11 |
+#define ONE_WIRE_BUS 2 // pinul de date |
|
| 12 |
+#define DEBUG 0 //0=off 1=on |
|
| 13 |
+ |
|
| 14 |
+/********** NU edita mai jos ******************/ |
|
| 15 |
+char mqttPassword[15]; //urmatoarele le va genera cu functia gen |
|
| 16 |
+char mqttSUB[22]; |
|
| 17 |
+char espName[13]; |
|
| 18 |
+char mqttESP[22]; |
|
| 19 |
+char mqttTEMP[23]; |
|
| 20 |
+char mqttLWT[22]; |
|
| 21 |
+ |
|
| 22 |
+const String model = "ESP8266 Dallas"; |
|
| 23 |
+const String ver = "v1.0.0"; |
|
| 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 |
+// senzor de temperatura DALLAS |
|
| 32 |
+OneWire oneWire(ONE_WIRE_BUS); |
|
| 33 |
+DallasTemperature sensors(&oneWire); |
|
| 34 |
+ |
|
| 35 |
+//********************************************************************* |
|
| 36 |
+ |
|
| 37 |
+WiFiClient espClient; |
|
| 38 |
+PubSubClient client(espClient); |
|
| 39 |
+ |
|
| 40 |
+void generate_vars(){
|
|
| 41 |
+ strcpy (mqttPassword, "8219CH"); |
|
| 42 |
+ strcat (mqttPassword, mqtt_user); |
|
| 43 |
+ |
|
| 44 |
+ strcpy (espName, mqtt_user); |
|
| 45 |
+ strcat (espName, "-"); |
|
| 46 |
+ strcat (espName, mqtt_devid); |
|
| 47 |
+ |
|
| 48 |
+ strcpy (mqttSUB, "cmnd/"); |
|
| 49 |
+ strcat (mqttSUB, espName); |
|
| 50 |
+ strcat (mqttSUB, "/ESP"); |
|
| 51 |
+ |
|
| 52 |
+ strcpy (mqttESP, "stat/"); |
|
| 53 |
+ strcat (mqttESP, espName); |
|
| 54 |
+ strcat (mqttESP, "/ESP"); |
|
| 55 |
+ |
|
| 56 |
+ strcpy (mqttTEMP, "stat/"); |
|
| 57 |
+ strcat (mqttTEMP, espName); |
|
| 58 |
+ strcat (mqttTEMP, "/TEMP"); |
|
| 59 |
+ |
|
| 60 |
+ strcpy (mqttLWT, "tele/"); |
|
| 61 |
+ strcat (mqttLWT, espName); |
|
| 62 |
+ strcat (mqttLWT, "/LWT"); |
|
| 63 |
+} |
|
| 64 |
+ |
|
| 65 |
+String ipToString(IPAddress ip){
|
|
| 66 |
+ String s=""; |
|
| 67 |
+ for (int i=0; i<4; i++) |
|
| 68 |
+ s += i ? "." + String(ip[i]) : String(ip[i]); |
|
| 69 |
+ return s; |
|
| 70 |
+} |
|
| 71 |
+ |
|
| 72 |
+String getMacAddress() {
|
|
| 73 |
+ byte mac[6]; |
|
| 74 |
+ WiFi.macAddress(mac); |
|
| 75 |
+ String cMac = ""; |
|
| 76 |
+ for (int i = 0; i < 6; ++i) {
|
|
| 77 |
+ cMac += String(mac[i],HEX); |
|
| 78 |
+ if(i<5) |
|
| 79 |
+ cMac += "-"; |
|
| 80 |
+ } |
|
| 81 |
+ cMac.toUpperCase(); |
|
| 82 |
+ return cMac; |
|
| 83 |
+} |
|
| 84 |
+ |
|
| 85 |
+void reconectez() {
|
|
| 86 |
+ |
|
| 87 |
+ // ma conectez la mqtt server |
|
| 88 |
+ while (!client.connected()) {
|
|
| 89 |
+ client.setServer(mqttServer, mqttPort); |
|
| 90 |
+ #if DEBUG |
|
| 91 |
+ Serial.print("AtloopTempting MQTT connection...");
|
|
| 92 |
+ #endif |
|
| 93 |
+ // Incerc sa ma reconectez cu LWT din 5 in 5 secunde |
|
| 94 |
+ if (client.connect(espName, mqtt_user, mqttPassword, mqttLWT, 1, 1, "Offline")) {
|
|
| 95 |
+ #if DEBUG |
|
| 96 |
+ Serial.println("connected");
|
|
| 97 |
+ #endif |
|
| 98 |
+ client.publish(mqttLWT,"Online",TRUE); |
|
| 99 |
+ // trimit informatii utile cand ma conectez |
|
| 100 |
+ String esp_info = "{\"ESPMac\":\"";
|
|
| 101 |
+ esp_info += getMacAddress(); |
|
| 102 |
+ esp_info += "\", \"IPAddress\":\""; |
|
| 103 |
+ esp_info += ipToString(WiFi.localIP()); |
|
| 104 |
+ esp_info += "\"}"; |
|
| 105 |
+ String netinfo = "{\"Module\":\"";
|
|
| 106 |
+ netinfo += String (model); |
|
| 107 |
+ netinfo += "\", \"Version\":\""; |
|
| 108 |
+ netinfo += String (ver); |
|
| 109 |
+ netinfo += "\"}"; |
|
| 110 |
+ |
|
| 111 |
+ client.publish(mqttESP, netinfo.c_str(),TRUE); |
|
| 112 |
+ #if DEBUG |
|
| 113 |
+ Serial.println(netinfo); |
|
| 114 |
+ #endif |
|
| 115 |
+ client.publish(mqttESP, esp_info.c_str(),TRUE); |
|
| 116 |
+ #if DEBUG |
|
| 117 |
+ Serial.println(esp_info); |
|
| 118 |
+ #endif |
|
| 119 |
+ client.publish(mqttESP, "15 minute",TRUE); // trimit intervalul default |
|
| 120 |
+ client.subscribe(mqttSUB); |
|
| 121 |
+ } else {
|
|
| 122 |
+ #if DEBUG |
|
| 123 |
+ Serial.print("failed, rc=");
|
|
| 124 |
+ Serial.print(client.state()); |
|
| 125 |
+ Serial.println(" try again in 60 seconds");
|
|
| 126 |
+ #endif |
|
| 127 |
+ // Wait 60 seconds before retrying |
|
| 128 |
+ delay(60000); |
|
| 129 |
+ } |
|
| 130 |
+ } |
|
| 131 |
+} |
|
| 132 |
+ |
|
| 133 |
+void setup() |
|
| 134 |
+{
|
|
| 135 |
+ #if DEBUG |
|
| 136 |
+ Serial.begin(115200); |
|
| 137 |
+ #endif |
|
| 138 |
+ |
|
| 139 |
+ generate_vars(); //genereaza topicurile de mqtt in baza mqtt_user si mqtt_devid |
|
| 140 |
+ |
|
| 141 |
+ // ma conectez la AP via wifi |
|
| 142 |
+ WiFiManager wifi; |
|
| 143 |
+ wifi.setConfigPortalTimeout(120); // a timeout so the ESP doesn't hang waiting to be configured, for instance after a power failure |
|
| 144 |
+ wifi.setTimeout(180); // sta AP 3 minute apoi se reseteaza din nou |
|
| 145 |
+ if (!wifi.autoConnect("ClickHome")) {
|
|
| 146 |
+ #if DEBUG |
|
| 147 |
+ Serial.println("timeout - going to sleep");
|
|
| 148 |
+ #endif |
|
| 149 |
+ } |
|
| 150 |
+ delay(200); |
|
| 151 |
+ |
|
| 152 |
+ reconectez(); |
|
| 153 |
+ client.setCallback(getMessage); //setez functia care parseaza mesajele venite prin mqtt |
|
| 154 |
+ |
|
| 155 |
+ sensors.begin(); |
|
| 156 |
+} |
|
| 157 |
+ |
|
| 158 |
+void getMessage(char* topic, byte* payload, unsigned int length) {
|
|
| 159 |
+ float t = 0; |
|
| 160 |
+ mqttMessage=""; |
|
| 161 |
+ #if DEBUG |
|
| 162 |
+ Serial.print("Message arrived in topic: ");
|
|
| 163 |
+ Serial.println(topic); |
|
| 164 |
+ Serial.print("Message:");
|
|
| 165 |
+ #endif |
|
| 166 |
+ for (int i = 0; i < length; i++) {
|
|
| 167 |
+ mqttMessage += (char)payload[i]; |
|
| 168 |
+ } |
|
| 169 |
+ #if DEBUG |
|
| 170 |
+ Serial.println(mqttMessage); |
|
| 171 |
+ #endif |
|
| 172 |
+ |
|
| 173 |
+ // daca a primit sendTemp trimite temperatura |
|
| 174 |
+ if (mqttMessage == "temp") |
|
| 175 |
+ {
|
|
| 176 |
+ #if DEBUG |
|
| 177 |
+ Serial.println("Trimit temperatura");
|
|
| 178 |
+ #endif |
|
| 179 |
+ sensors.setResolution(12); |
|
| 180 |
+ sensors.requestTemperatures(); // Send the command to get Temperatures |
|
| 181 |
+ t = sensors.getTempCByIndex(0); |
|
| 182 |
+ String json = "{\"Temp\":";
|
|
| 183 |
+ json += String (t); |
|
| 184 |
+ json += "}"; |
|
| 185 |
+ client.publish(mqttTEMP, json.c_str(),TRUE); |
|
| 186 |
+ #if DEBUG |
|
| 187 |
+ Serial.println(json); |
|
| 188 |
+ #endif |
|
| 189 |
+ } |
|
| 190 |
+// reset la ESP |
|
| 191 |
+ if (mqttMessage == "reset") |
|
| 192 |
+ {
|
|
| 193 |
+ String lastwords="Am fost resetat ..."; |
|
| 194 |
+ client.publish(mqttESP, lastwords.c_str(), TRUE); |
|
| 195 |
+ delay (3000); |
|
| 196 |
+ ESP.reset(); |
|
| 197 |
+ delay (5000); |
|
| 198 |
+ } |
|
| 199 |
+ // daca a primit valoarea in minute seteaza loopTimer si-l trimite in DB |
|
| 200 |
+ |
|
| 201 |
+ if (mqttMessage == "debug") |
|
| 202 |
+ {
|
|
| 203 |
+ loopTimer=30000; // setez sa trimita valori la 30 de secunde ca sa fac debug |
|
| 204 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"30 secunde\"}",TRUE);
|
|
| 205 |
+ } |
|
| 206 |
+ if (mqttMessage == "10") |
|
| 207 |
+ {
|
|
| 208 |
+ loopTimer=600000; // setez sa trimita valori la 10 minute |
|
| 209 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"10 minute\"}",TRUE);
|
|
| 210 |
+ } |
|
| 211 |
+ if (mqttMessage == "15") |
|
| 212 |
+ {
|
|
| 213 |
+ loopTimer=900000; // setez sa trimita valori la 15 minute |
|
| 214 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"15 minute\"}",TRUE);
|
|
| 215 |
+ } |
|
| 216 |
+ if (mqttMessage == "30") |
|
| 217 |
+ {
|
|
| 218 |
+ loopTimer=1800000; // setez sa trimita valori la 30 de minute |
|
| 219 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"30 de minute\"}",TRUE);
|
|
| 220 |
+ } |
|
| 221 |
+ if (mqttMessage == "60") |
|
| 222 |
+ {
|
|
| 223 |
+ loopTimer=3600000; // setez sa trimita valori la o ora |
|
| 224 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"o ora\"}",TRUE);
|
|
| 225 |
+ } |
|
| 226 |
+ if (mqttMessage == "stop") |
|
| 227 |
+ {
|
|
| 228 |
+ loopTimer=3600000000; // setez sa nu trimita valori |
|
| 229 |
+ client.publish(mqttESP, "{\"LoopInterval\":\"oprit\"}",TRUE);
|
|
| 230 |
+ } |
|
| 231 |
+} |
|
| 232 |
+ |
|
| 233 |
+void loop() |
|
| 234 |
+{
|
|
| 235 |
+ if (!client.connected()) {
|
|
| 236 |
+ reconectez(); |
|
| 237 |
+ } |
|
| 238 |
+ client.loop(); |
|
| 239 |
+ long now = millis(); |
|
| 240 |
+ if (now - lastMsg > loopTimer) {
|
|
| 241 |
+ lastMsg = now; |
|
| 242 |
+ sensors.setResolution(12); |
|
| 243 |
+ sensors.requestTemperatures(); // Send the command to get loopTemperatures |
|
| 244 |
+ loopTemp = sensors.getTempCByIndex(0); |
|
| 245 |
+ #if DEBUG |
|
| 246 |
+ Serial.println(loopTemp); |
|
| 247 |
+ #endif |
|
| 248 |
+ if((loopTemp > -20) && (loopTemp <60)) |
|
| 249 |
+ {
|
|
| 250 |
+ String val = "{\"Temp\":";
|
|
| 251 |
+ val += String (loopTemp); |
|
| 252 |
+ val += "}"; |
|
| 253 |
+ client.publish(mqttESP, val.c_str(),TRUE); |
|
| 254 |
+ } |
|
| 255 |
+ } |
|
| 256 |
+} |
|
| 257 |
+ |
| ... | ... |
@@ -1,4 +1,3 @@ |
| 1 |
-#include <FS.h> //this needs to be first, e folosit de salvarea credentialelor pe FS |
|
| 2 | 1 |
#include <ESP8266WiFi.h> |
| 3 | 2 |
#include <PubSubClient.h> |
| 4 | 3 |
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager |
| ... | ... |
@@ -14,7 +13,7 @@ const char* mqtt_devid="1790"; |
| 14 | 14 |
// senzor de temperatura DHT |
| 15 | 15 |
#define DHTPIN 2 // pinul de date |
| 16 | 16 |
#define DHTTYPE DHT22 // DHT11 (albastru) DHT22 (alb) |
| 17 |
-#define DEBUG 1 //0=off 1=on |
|
| 17 |
+#define DEBUG 0 //0=off 1=on |
|
| 18 | 18 |
|
| 19 | 19 |
/********** NU edita mai jos ******************/ |
| 20 | 20 |
|
| ... | ... |
@@ -49,8 +48,6 @@ void dht_wrapper() |
| 49 | 49 |
|
| 50 | 50 |
WiFiClient espClient; |
| 51 | 51 |
PubSubClient client(espClient); |
| 52 |
-bool readConfigFile(); |
|
| 53 |
- |
|
| 54 | 52 |
|
| 55 | 53 |
void generate_vars(){
|
| 56 | 54 |
strcpy (mqttPassword, "8219CH"); |
| ... | ... |
@@ -98,13 +95,7 @@ String getMacAddress() {
|
| 98 | 98 |
} |
| 99 | 99 |
|
| 100 | 100 |
void reconectez() {
|
| 101 |
- while (String(mqtt_user)==""){
|
|
| 102 |
- #if DEBUG |
|
| 103 |
- Serial.println("Invalid user!");
|
|
| 104 |
- #endif |
|
| 105 |
- delay(99999999); |
|
| 106 |
- } |
|
| 107 |
- |
|
| 101 |
+ |
|
| 108 | 102 |
// ma conectez la mqtt server |
| 109 | 103 |
while (!client.connected()) {
|
| 110 | 104 |
client.setServer(mqttServer, mqttPort); |
| ... | ... |
@@ -159,12 +150,6 @@ void setup() |
| 159 | 159 |
Serial.begin(115200); |
| 160 | 160 |
#endif |
| 161 | 161 |
|
| 162 |
- // Mount the filesystem |
|
| 163 |
- bool result = SPIFFS.begin(); |
|
| 164 |
- #if DEBUG |
|
| 165 |
- //Serial.println("SPIFFS opened: " + result);
|
|
| 166 |
- #endif |
|
| 167 |
- |
|
| 168 | 162 |
generate_vars(); //genereaza topicurile de mqtt in baza mqtt_user si mqtt_devid |
| 169 | 163 |
|
| 170 | 164 |
// ma conectez la AP via wifi |
| ... | ... |
@@ -27,7 +27,6 @@ const int mqttPort = 1883; |
| 27 | 27 |
long loopTimer = 900000; // miliseconds - by default trimite la 15 minute |
| 28 | 28 |
long lastMsg = 0; |
| 29 | 29 |
float loopTemp = 0; |
| 30 |
-int inPin = 5; |
|
| 31 | 30 |
String mqttMessage; |
| 32 | 31 |
|
| 33 | 32 |
// senzor de temperatura DALLAS |
| ... | ... |
@@ -170,7 +169,6 @@ void setup() |
| 170 | 170 |
reconectez(); |
| 171 | 171 |
client.setCallback(getMessage); //setez functia care parseaza mesajele venite prin mqtt |
| 172 | 172 |
|
| 173 |
- pinMode(inPin, INPUT); |
|
| 174 | 173 |
sensors.begin(); |
| 175 | 174 |
} |
| 176 | 175 |
|