-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkService.cpp
More file actions
115 lines (98 loc) · 2.93 KB
/
NetworkService.cpp
File metadata and controls
115 lines (98 loc) · 2.93 KB
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
#include "config.h"
#include "NetworkService.h"
#include <WiFi.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
const char* ssid = CONFIG_SSID;
const char* password = CONFIG_WIFI_PASSWORD;
WiFiClient espClient;
PubSubClient client(espClient);
char messageBuffer[64];
size_t NetworkService::publish(const char * topic, String s) {
client.publish(topic, s.c_str());
}
size_t NetworkService::publish(const char * topic, const char *format, ...) {
char loc_buf[64];
char * temp = loc_buf;
va_list arg;
va_list copy;
va_start(arg, format);
va_copy(copy, arg);
int len = vsnprintf(temp, sizeof(loc_buf), format, copy);
va_end(copy);
if(len < 0) {
va_end(arg);
return 0;
};
if(len >= sizeof(loc_buf)){
temp = (char*) malloc(len+1);
if(temp == NULL) {
va_end(arg);
return 0;
}
len = vsnprintf(temp, len+1, format, arg);
}
va_end(arg);
len = client.publish(topic, (uint8_t*)temp, len);
if(temp != loc_buf){
free(temp);
}
return len;
}
void NetworkService::setup()
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
// Port defaults to 3232
// ArduinoOTA.setPort(3232);
// Hostname defaults to esp3232-[MAC]
// ArduinoOTA.setHostname("myesp32");
ArduinoOTA.setPassword(CONFIG_OTA_PASSWORD);
ArduinoOTA
.onStart([&]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
publish(MQTT_TOPIC, "Start updating " + type);
})
.onEnd([&]() {
publish(MQTT_TOPIC, "\nEnd");
})
.onProgress([&](unsigned int progress, unsigned int total) {
publish(MQTT_TOPIC, "Progress: %u%%\r", (progress / (total / 100)));
})
.onError([&](ota_error_t error) {
publish(MQTT_TOPIC, "Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) publish(MQTT_TOPIC, "Auth Failed");
else if (error == OTA_BEGIN_ERROR) publish(MQTT_TOPIC, "Begin Failed");
else if (error == OTA_CONNECT_ERROR) publish(MQTT_TOPIC, "Connect Failed");
else if (error == OTA_RECEIVE_ERROR) publish(MQTT_TOPIC, "Receive Failed");
else if (error == OTA_END_ERROR) publish(MQTT_TOPIC, "End Failed");
});
ArduinoOTA.begin();
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Connecting to MQTT");
client.setServer(CONFIG_MQTT_SERVER, CONFIG_MQTT_PORT);
if (client.connect("cycle"))
{
Serial.println("Connected to MQTT");
}
else {
Serial.println("Failed to connect to MQTT");
Serial.printf("State %d", client.state());
}
}
void NetworkService::loop()
{
ArduinoOTA.handle();
client.loop();
}