Browse code

ESP8266 nu se poate updata decat programat via cablu pt ca nu are memorie suficienta

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