forked from mruettgers/SMLReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
135 lines (112 loc) · 3.54 KB
/
main.cpp
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
#include <list>
#include "config.h"
#include "debug.h"
#include <sml/sml_file.h>
#include "Sensor.h"
#include <IotWebConf.h>
#include "MqttPublisher.h"
#include "EEPROM.h"
#include <ESP8266WiFi.h>
std::list<Sensor*> *sensors = new std::list<Sensor*>();
void wifiConnected();
void configSaved();
DNSServer dnsServer;
WebServer server(80);
HTTPUpdateServer httpUpdater;
WiFiClient net;
MqttConfig mqttConfig;
MqttPublisher publisher;
IotWebConf iotWebConf(WIFI_AP_SSID, &dnsServer, &server, WIFI_AP_DEFAULT_PASSWORD, CONFIG_VERSION);
IotWebConfParameter params[] = {
IotWebConfParameter("MQTT server", "mqttServer", mqttConfig.server, sizeof(mqttConfig.server), "text", NULL, mqttConfig.server, NULL, true),
IotWebConfParameter("MQTT port", "mqttPort", mqttConfig.port, sizeof(mqttConfig.port), "text", NULL, mqttConfig.port, NULL, true),
IotWebConfParameter("MQTT username", "mqttUsername", mqttConfig.username, sizeof(mqttConfig.username), "text", NULL, mqttConfig.username, NULL, true),
IotWebConfParameter("MQTT password", "mqttPassword", mqttConfig.password, sizeof(mqttConfig.password), "password", NULL, mqttConfig.password, NULL, true),
IotWebConfParameter("MQTT topic", "mqttTopic", mqttConfig.topic, sizeof(mqttConfig.topic), "text", NULL, mqttConfig.topic, NULL, true)};
boolean needReset = false;
boolean connected = false;
void process_message(byte *buffer, size_t len, Sensor *sensor)
{
// Parse
sml_file *file = sml_file_parse(buffer + 8, len - 16);
DEBUG_SML_FILE(file);
publisher.publish(sensor, file);
// free the malloc'd memory
sml_file_free(file);
}
void setup()
{
// Setup debugging stuff
SERIAL_DEBUG_SETUP(115200);
#ifdef DEBUG
// Delay for getting a serial console attached in time
delay(2000);
#endif
// Setup reading heads
DEBUG("Setting up %d configured sensors...", NUM_OF_SENSORS);
const SensorConfig *config = SENSOR_CONFIGS;
for (uint8_t i = 0; i < NUM_OF_SENSORS; i++, config++)
{
Sensor *sensor = new Sensor(config, process_message);
sensors->push_back(sensor);
}
DEBUG("Sensor setup done.");
// Initialize publisher
// Setup WiFi and config stuff
DEBUG("Setting up WiFi and config stuff.");
for (uint8_t i = 0; i < sizeof(params) / sizeof(params[0]); i++)
{
DEBUG("Adding parameter %s.", params[i].label);
iotWebConf.addParameter(¶ms[i]);
}
iotWebConf.setConfigSavedCallback(&configSaved);
iotWebConf.setWifiConnectionCallback(&wifiConnected);
iotWebConf.setupUpdateServer(&httpUpdater);
boolean validConfig = iotWebConf.init();
if (!validConfig)
{
DEBUG("Missing or invalid config. MQTT publisher disabled.");
MqttConfig defaults;
// Resetting to default values
strcpy(mqttConfig.server, defaults.server);
strcpy(mqttConfig.port, defaults.port);
strcpy(mqttConfig.username, defaults.username);
strcpy(mqttConfig.password, defaults.password);
strcpy(mqttConfig.topic, defaults.topic);
}
else
{
// Setup MQTT publisher
publisher.setup(mqttConfig);
}
server.on("/", [] { iotWebConf.handleConfig(); });
server.onNotFound([]() { iotWebConf.handleNotFound(); });
DEBUG("Setup done.");
}
void loop()
{
if (needReset)
{
// Doing a chip reset caused by config changes
DEBUG("Rebooting after 1 second.");
delay(1000);
ESP.restart();
}
// Execute sensor state machines
for (std::list<Sensor*>::iterator it = sensors->begin(); it != sensors->end(); ++it){
(*it)->loop();
}
iotWebConf.doLoop();
yield();
}
void configSaved()
{
DEBUG("Configuration was updated.");
needReset = true;
}
void wifiConnected()
{
DEBUG("WiFi connection established.");
connected = true;
publisher.connect();
}