forked from Brunas/esp32_p1meter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp32_p1meter.ino
130 lines (108 loc) · 3.2 KB
/
esp32_p1meter.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
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#include <esp_wifi.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#include <ArduinoJson.h>
#include <tiny-collections.h>
#include "settings.h"
#include "dsmr_map.h"
WiFiClient espClient;
PubSubClient mqttClient(espClient);
/***********************************
Main Setup
***********************************/
void setup() {
// Initialize pins and blink once for setup start
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
blinkLed(1, 500);
// Force CPU frequency if constant is specified
if (CPU_FREQ > 0) {
setCpuFrequencyMhz(CPU_FREQ);
}
Serial.begin(BAUD_RATE);
#ifdef DEBUG
// Blinking 2 times long to indicate DEBUG mode
debug("Booting - DEBUG mode on");
blinkLed(2, 2000);
#endif
makeSureWiFiConnected(true);
MDNS.begin(String(HOSTNAME).c_str());
delay(1000);
setupOTA();
mqttClient.setServer(MQTT_HOST, atoi(MQTT_PORT));
makeSureMqttConnected();
setupDataReadout();
#ifndef TEST
Serial2.begin(BAUD_RATE, SERIAL_8N1, RXD2, TXD2, true);
#endif
debug("System initialised successfully!");
blinkLed(5, 500); // Blink 5 times to indicate end of setup
}
/***********************************
Main Loop
***********************************/
void loop() {
long now = millis();
makeSureWiFiConnected(false);
ArduinoOTA.handle();
makeSureMqttConnected();
mqttClient.loop();
// Check if we want a full update of all the data including the unchanged data.
if (now - LAST_FULL_UPDATE_SENT > UPDATE_FULL_INTERVAL) {
for (int i = 0; i < telegramObjects.size(); i++) {
telegramObjects[i].sendData = true;
LAST_FULL_UPDATE_SENT = millis();
}
}
if (now - LAST_UPDATE_SENT > UPDATE_INTERVAL) {
#ifdef TEST
if (readTestSerial()) {
#else
if (readP1Serial()) {
#endif
LAST_UPDATE_SENT = millis();
sendDataToBroker();
}
}
}
/***********************************
Setup Methods
***********************************/
/**
Over the Air update setup
*/
void setupOTA() {
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()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.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.setHostname(String(HOSTNAME).c_str());
ArduinoOTA.setPasswordHash(String(OTA_PASSWORD_HASH).c_str());
ArduinoOTA.begin();
}