Browse code

Varianta ESP8266 pt RELEU

Andrei Bucur authored on 03/03/2018 19:52:19
Showing 1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,222 @@
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
+
7
+//ESP8266-01 nu are suficienta memorie pt update http
8
+const char* mqtt_user="64x35up";
9
+const char* mqtt_devid="5566";
10
+#define DEBUG 1 //0=off 1=on
11
+#define RELAY 2 //  pinul de date 
12
+
13
+/********** NU edita mai jos ******************/
14
+char mqttPassword[15]; //urmatoarele le va genera cu functia gen
15
+char mqttSUB[22];
16
+char espName[13];
17
+char mqttESP[22];
18
+char mqttRELAY[23];
19
+char mqttLWT[22];
20
+
21
+const String model = "ESP8266 Releu";
22
+const String ver = "v1.0.0";
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
+String mqttMessage;
29
+
30
+WiFiClient espClient;
31
+PubSubClient client(espClient);
32
+
33
+void generate_vars(){
34
+  strcpy (mqttPassword, "8219CH");
35
+  strcat (mqttPassword, mqtt_user);
36
+
37
+  strcpy (espName, mqtt_user);
38
+  strcat (espName, "-");
39
+  strcat (espName, mqtt_devid);
40
+
41
+  strcpy (mqttSUB, "cmnd/");
42
+  strcat (mqttSUB, espName);
43
+  strcat (mqttSUB, "/ESP");
44
+
45
+  strcpy (mqttESP, "stat/");
46
+  strcat (mqttESP, espName);
47
+  strcat (mqttESP, "/ESP");
48
+
49
+  strcpy (mqttRELAY, "stat/");
50
+  strcat (mqttRELAY, espName);
51
+  strcat (mqttRELAY, "/RELAY");
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
+  
79
+  // ma conectez la mqtt server
80
+  while (!client.connected()) {
81
+    client.setServer(mqttServer, mqttPort);
82
+    #if DEBUG
83
+    Serial.print("AtloopTempting MQTT connection...");
84
+    #endif
85
+    // Incerc sa ma reconectez cu LWT din 5 in 5 secunde
86
+    if (client.connect(espName, mqtt_user, mqttPassword, mqttLWT, 1, 1, "Offline")) {
87
+      #if DEBUG
88
+      Serial.println("connected");
89
+      #endif
90
+      client.publish(mqttLWT,"Online",TRUE);
91
+        // trimit informatii utile cand ma conectez
92
+        String esp_info = "{\"ESPMac\":\"";
93
+        esp_info += getMacAddress();
94
+        esp_info += "\", \"IPAddress\":\"";
95
+        esp_info += ipToString(WiFi.localIP());    
96
+        esp_info += "\"}";       
97
+        String netinfo = "{\"Module\":\"";
98
+        netinfo += String (model);
99
+        netinfo += "\", \"Version\":\"";
100
+        netinfo += String (ver);
101
+        netinfo += "\"}";            
102
+        
103
+        client.publish(mqttESP, netinfo.c_str(),TRUE);
104
+        #if DEBUG
105
+        Serial.println(netinfo);
106
+        #endif
107
+        client.publish(mqttESP, esp_info.c_str(),TRUE);
108
+        #if DEBUG
109
+        Serial.println(esp_info);
110
+        #endif
111
+    
112
+        // citesc starea si o trimit cand se reconecteaza si la boot
113
+        String json = "{\"Relay\":\"";
114
+        json += String (digitalRead(RELAY));
115
+        json += "\"}";
116
+        client.publish(mqttRELAY, json.c_str(),TRUE);
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(RELAY,OUTPUT);
135
+
136
+  #if DEBUG
137
+  Serial.begin(115200);
138
+  #endif
139
+
140
+  generate_vars(); //genereaza topicurile de mqtt in baza mqtt_user si mqtt_devid
141
+
142
+  // ma conectez la AP via wifi
143
+   WiFiManager wifi;
144
+   wifi.setConfigPortalTimeout(120); // a timeout so the ESP doesn't hang waiting to be configured, for instance after a power failure
145
+   wifi.setTimeout(180); // sta AP 3 minute apoi se reseteaza din nou
146
+   if (!wifi.autoConnect("ClickHome")) {
147
+     #if DEBUG
148
+     Serial.println("timeout - going to sleep");
149
+     #endif
150
+  }
151
+  delay(200);
152
+
153
+  reconectez();
154
+  client.setCallback(getMessage); //setez functia care parseaza mesajele venite prin mqtt
155
+
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 on
174
+  if (mqttMessage == "ON")
175
+  {
176
+    #if DEBUG
177
+    Serial.println("Pornesc releu");
178
+    #endif
179
+    digitalWrite(RELAY,HIGH);
180
+    String json = "{\"Relay\":\"1\"}";
181
+    client.publish(mqttRELAY, json.c_str(),TRUE);
182
+  }  
183
+  if (mqttMessage == "OFF")
184
+  {
185
+     long now = millis(); 
186
+    #if DEBUG
187
+    Serial.println("Opresc releu");
188
+    Serial.println(now);
189
+    #endif
190
+    digitalWrite(RELAY,LOW);
191
+    String json = "{\"Relay\":\"0\"}";
192
+    client.publish(mqttRELAY, json.c_str(),TRUE);
193
+  }
194
+
195
+  if (mqttMessage == "stare")
196
+  {
197
+    // citesc starea si o trimit
198
+    String json = "{\"Relay\":\"";
199
+    json += String (digitalRead(RELAY));
200
+    json += "\"}";
201
+    client.publish(mqttRELAY, json.c_str(),TRUE);
202
+  }
203
+}
204
+
205
+void loop()
206
+{
207
+  if (!client.connected()) 
208
+  {
209
+    reconectez();
210
+  }
211
+  client.loop();
212
+  long now = millis(); 
213
+  if (now - lastMsg > loopTimer) 
214
+  {
215
+    lastMsg = now;
216
+    #if DEBUG
217
+    Serial.println("Nu fac nimic in bucla");
218
+    #endif
219
+  }
220
+}
221
+