-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP8266Switch.ino
238 lines (194 loc) · 6.57 KB
/
ESP8266Switch.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//ESP8266-01
//spinac rele
#include "Configuration.h"
//for LED status
Ticker ticker;
auto timer = timer_create_default(); // create a timer with default settings
Timer<> default_timer; // save as above
DoubleResetDetector drd(DRD_TIMEOUT, DRD_ADDRESS);
void tick()
{
//toggle state
int state = digitalRead(STATUSLED); // get the current state of GPIO1 pin
digitalWrite(STATUSLED, !state); // set pin to the opposite state
}
uint32_t heartBeat = 0;
//gets called when WiFiManager enters configuration mode
void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println("Entered config mode");
Serial.println(WiFi.softAPIP());
//if you used auto generated SSID, print it
Serial.println(myWiFiManager->getConfigPortalSSID());
//entered config mode, make led toggle faster
ticker.attach(0.2, tick);
}
//MQTT callback
void callback(char* topic, byte* payload, unsigned int length) {
char * pEnd;
long int valL;
String val = String();
DEBUG_PRINT("Message arrived [");
DEBUG_PRINT(topic);
DEBUG_PRINT("] ");
for (int i=0;i<length;i++) {
DEBUG_PRINT((char)payload[i]);
val += (char)payload[i];
}
DEBUG_PRINTLN();
if (strcmp(topic, (String(mqtt_base) + "/" + String(mqtt_topic_restart)).c_str())==0) {
DEBUG_PRINT("RESTART");
ESP.restart();
} else if (strcmp(topic, (String(mqtt_base) + "/" + String(mqtt_topic_netinfo)).c_str())==0) {
DEBUG_PRINT("NET INFO");
sendNetInfoMQTT();
} else if (strcmp(topic, (String(mqtt_base) + "/" + String(mqtt_topic_set)).c_str())==0) {
DEBUG_PRINT("SET ");
if (val=="0") {
DEBUG_PRINT("OFF");
digitalWrite(RELE,0);
} else {
DEBUG_PRINT("ON");
digitalWrite(RELE,1);
}
sendStatus();
}
}
ADC_MODE(ADC_VCC);
WiFiClient espClient;
PubSubClient client(espClient);
void setup(void) {
SERIAL_BEGIN;
DEBUG_PRINT(F(SW_NAME));
DEBUG_PRINT(F(" "));
DEBUG_PRINTLN(F(VERSION));
pinMode(RELE, OUTPUT);
ticker.attach(1, tick);
WiFiManager wifiManager;
//set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
wifiManager.setAPCallback(configModeCallback);
wifiManager.setConfigPortalTimeout(CONFIG_PORTAL_TIMEOUT);
wifiManager.setConnectTimeout(CONNECT_TIMEOUT);
if (drd.detectDoubleReset()) {
DEBUG_PRINTLN("Double reset detected, starting config portal...");
ticker.attach(0.2, tick);
if (!wifiManager.startConfigPortal(HOSTNAMEOTA)) {
DEBUG_PRINTLN("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(5000);
}
}
rst_info *_reset_info = ESP.getResetInfoPtr();
uint8_t _reset_reason = _reset_info->reason;
DEBUG_PRINT("Boot-Mode: ");
DEBUG_PRINTLN(_reset_reason);
heartBeat = _reset_reason;
/*
REASON_DEFAULT_RST = 0 normal startup by power on
REASON_WDT_RST = 1 hardware watch dog reset
REASON_EXCEPTION_RST = 2 exception reset, GPIO status won't change
REASON_SOFT_WDT_RST = 3 software watch dog reset, GPIO status won't change
REASON_SOFT_RESTART = 4 software restart ,system_restart , GPIO status won't change
REASON_DEEP_SLEEP_AWAKE = 5 wake up from deep-sleep
REASON_EXT_SYS_RST = 6 external system reset
*/
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
WiFi.printDiag(Serial);
if (!wifiManager.autoConnect(AUTOCONNECTNAME, AUTOCONNECTPWD)) {
DEBUG_PRINTLN("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(5000);
}
sendNetInfoMQTT();
#ifdef ota
ArduinoOTA.setHostname(HOSTNAMEOTA);
ArduinoOTA.onStart([]() {
DEBUG_PRINTLN("Start updating ");
});
ArduinoOTA.onEnd([]() {
DEBUG_PRINTLN("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
DEBUG_PRINTF("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
DEBUG_PRINTF("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
#endif
void * a;
timer.every(SEND_DELAY, sendDataMQTT);
timer.every(SENDSTAT_DELAY, sendStatisticMQTT);
sendStatisticMQTT(a);
ticker.detach();
}
void loop(void) {
timer.tick(); // tick the timer
#ifdef ota
ArduinoOTA.handle();
reconnect();
client.loop();
#endif
}
bool sendStatisticMQTT(void *) {
DEBUG_PRINTLN(F(" - I am sending statistic"));
SenderClass sender;
sender.add("VersionSW", VERSION);
sender.add("HeartBeat", heartBeat++);
sender.add("RSSI", WiFi.RSSI());
sender.add("Voltage", ESP.getVcc());
DEBUG_PRINTLN(F("Calling MQTT"));
sender.sendMQTT(mqtt_server, mqtt_port, mqtt_username, mqtt_key, mqtt_base);
return true;
}
bool sendDataMQTT(void *) {
SenderClass sender;
sender.sendMQTT(mqtt_server, mqtt_port, mqtt_username, mqtt_key, mqtt_base);
return true;
}
void sendNetInfoMQTT() {
digitalWrite(BUILTIN_LED, LOW);
//printSystemTime();
DEBUG_PRINTLN(F("Net info"));
SenderClass sender;
sender.add("IP", WiFi.localIP().toString().c_str());
sender.add("MAC", WiFi.macAddress());
DEBUG_PRINTLN(F("Calling MQTT"));
sender.sendMQTT(mqtt_server, mqtt_port, mqtt_username, mqtt_key, mqtt_base);
digitalWrite(BUILTIN_LED, HIGH);
return;
}
void sendStatus(void) {
DEBUG_PRINTLN(F(" - I am sending status"));
SenderClass sender;
sender.add("status", digitalRead(RELE));
DEBUG_PRINTLN(F("Calling MQTT"));
sender.sendMQTT(mqtt_server, mqtt_port, mqtt_username, mqtt_key, mqtt_base);
}
void reconnect() {
// Loop until we're reconnected
if (!client.connected()) {
if (lastConnectAttempt == 0 || lastConnectAttempt + connectDelay < millis()) {
DEBUG_PRINT("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(mqtt_base, mqtt_username, mqtt_key)) {
DEBUG_PRINTLN("connected");
// Once connected, publish an announcement...
client.subscribe((String(mqtt_base) + "/#").c_str());
} else {
lastConnectAttempt = millis();
DEBUG_PRINT("failed, rc=");
DEBUG_PRINTLN(client.state());
}
}
}
}