Files
IoTManager/src/main.cpp

184 lines
3.4 KiB
C++
Raw Normal View History

2020-06-18 23:43:06 +02:00
#include "Global.h"
2020-06-17 23:30:48 +03:00
2020-06-22 03:11:02 +03:00
void not_async_actions();
static const char* MODULE = "Main";
void setup() {
WiFi.setAutoConnect(false);
WiFi.persistent(false);
Serial.begin(115200);
Serial.flush();
Serial.println();
Serial.println("--------------started----------------");
setChipId();
pm.info("FS");
fileSystemInit();
pm.info("Config");
loadConfig();
pm.info("Commands");
CMD_init();
pm.info("Sensors");
sensors_init();
pm.info("Init");
All_init();
pm.info("Network");
startSTAMode();
pm.info("Uptime");
uptime_init();
2020-06-22 14:01:12 +03:00
if (!TELEMETRY_UPDATE_INTERVAL) {
pm.info("Telemetry: Disabled");
}
2020-06-22 03:11:02 +03:00
telemetry_init();
pm.info("Updater");
init_updater();
pm.info("WebServer");
Web_server_init();
pm.info("WebAdmin");
web_init();
pm.info("TimeSync");
ts.add(
TIME_SYNC, 30000, [&](void*) {
startTimeSync();
},
nullptr, true);
#ifdef UDP_ENABLED
UDP_init();
pm.info("Broadcast");
#endif
ts.add(
TEST, 10000, [&](void*) {
printMemoryStatus();
},
nullptr, true);
just_load = false;
}
2020-06-17 23:30:48 +03:00
void saveConfig() {
2020-06-19 14:50:34 +02:00
writeFile("config.json", configSetupJson);
2020-06-17 23:30:48 +03:00
}
2020-06-22 03:11:02 +03:00
void loop() {
#ifdef OTA_UPDATES_ENABLED
ArduinoOTA.handle();
#endif
#ifdef WS_enable
ws.cleanupClients();
#endif
not_async_actions();
2020-06-24 01:16:00 +03:00
MqttClient::loop();
2020-06-22 17:40:29 +03:00
loopCmd();
loopButton();
loopScenario();
2020-06-22 03:11:02 +03:00
#ifdef UDP_ENABLED
2020-06-22 17:40:29 +03:00
loopUdp();
2020-06-22 03:11:02 +03:00
#endif
ts.update();
}
void not_async_actions() {
2020-06-24 01:16:00 +03:00
if (mqttParamsChanged) {
MqttClient::reconnect();
mqttParamsChanged = false;
}
2020-06-22 03:11:02 +03:00
do_upgrade_url();
do_upgrade();
#ifdef UDP_ENABLED
do_udp_data_parse();
do_mqtt_send_settings_to_udp();
#endif
do_i2c_scanning();
}
2020-06-19 07:48:31 +03:00
String getURL(const String& urls) {
2020-06-19 07:27:06 +03:00
String res = "";
2020-06-17 23:30:48 +03:00
HTTPClient http;
2020-06-19 07:27:06 +03:00
http.begin(urls);
2020-06-17 23:30:48 +03:00
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
2020-06-19 07:27:06 +03:00
res = http.getString();
2020-06-17 23:30:48 +03:00
} else {
2020-06-19 07:27:06 +03:00
res = "error";
2020-06-17 23:30:48 +03:00
}
http.end();
2020-06-19 07:27:06 +03:00
return res;
2020-06-17 23:30:48 +03:00
}
2020-06-19 07:27:06 +03:00
2020-06-18 23:43:06 +02:00
void safeDataToFile(String data, String Folder) {
2020-06-17 23:30:48 +03:00
String fileName;
fileName.toLowerCase();
fileName = deleteBeforeDelimiter(fileName, " ");
fileName.replace(" ", ".");
fileName.replace("..", ".");
fileName = Folder + "/" + fileName + ".txt";
2020-06-22 17:40:29 +03:00
2020-06-19 14:50:34 +02:00
jsonWriteStr(configLiveJson, "test", fileName);
2020-06-17 23:30:48 +03:00
}
2020-06-18 23:43:06 +02:00
2020-06-22 14:01:12 +03:00
void sendConfig(String topic, String widgetConfig, String key, String date) {
2020-06-17 23:30:48 +03:00
yield();
2020-06-22 14:01:12 +03:00
topic = jsonReadStr(configSetupJson, "mqttPrefix") + "/" + chipId + "/" + topic + "/status";
2020-06-17 23:30:48 +03:00
String outer = "{\"widgetConfig\":";
String inner = "{\"";
inner = inner + key;
inner = inner + "\":\"";
inner = inner + date;
inner = inner + "\"";
inner = inner + "}}";
String t = outer + inner;
yield();
}
2020-06-21 15:20:40 +03:00
void setChipId() {
chipId = getChipId();
Serial.println(chipId);
}
2020-06-17 23:30:48 +03:00
#ifdef ESP8266
2020-06-21 15:20:40 +03:00
#ifdef LED_PIN
2020-06-22 03:11:02 +03:00
void setLedStatus(LedStatus_t status) {
2020-06-21 03:43:15 +03:00
pinMode(LED_PIN, OUTPUT);
2020-06-22 03:11:02 +03:00
switch (status) {
case LED_OFF:
noTone(LED_PIN);
digitalWrite(LED_PIN, HIGH);
break;
case LED_ON:
noTone(LED_PIN);
digitalWrite(LED_PIN, LOW);
break;
case LED_SLOW:
tone(LED_PIN, 1);
break;
case LED_FAST:
tone(LED_PIN, 20);
break;
default:
break;
2020-06-17 23:30:48 +03:00
}
2020-06-20 22:51:14 +03:00
}
2020-06-17 23:30:48 +03:00
#endif
#endif