-
Notifications
You must be signed in to change notification settings - Fork 77
/
src.ino
234 lines (193 loc) · 6.38 KB
/
src.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
/*
* -------------------------------------------------------------------
* EmonESP Serial to Emoncms gateway
* -------------------------------------------------------------------
* Adaptation of Chris Howells OpenEVSE ESP Wifi
* by Trystan Lea, Glyn Hudson, OpenEnergyMonitor
* All adaptation GNU General Public License as below.
*
* -------------------------------------------------------------------
*
* This file is part of OpenEnergyMonitor.org project.
* EmonESP is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
* EmonESP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with EmonESP; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "emonesp.h"
#include "app_config.h"
#include "wifi.h"
#include "web_server.h"
#include "ota.h"
#include "input.h"
#include "emoncms.h"
#include "mqtt.h"
#include "http.h"
#include "autoauth.h"
#include <NTPClient.h>
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP,"europe.pool.ntp.org",time_offset,60000);
unsigned long last_ctrl_update = 0;
unsigned long last_pushbtn_check = 0;
bool pushbtn_action = 0;
bool pushbtn_state = 0;
bool last_pushbtn_state = 0;
static uint32_t last_mem = 0;
static uint32_t start_mem = 0;
static unsigned long mem_info_update = 0;
// -------------------------------------------------------------------
// SETUP
// -------------------------------------------------------------------
void setup() {
debug_setup();
DEBUG.println();
DEBUG.println();
DEBUG.print("EmonESP ");
DEBUG.println(ESP.getChipId());
DEBUG.println("Firmware: " + currentfirmware);
DEBUG.printf("Free: %d\n", ESP.getFreeHeap());
DBUG("Node type: ");
DBUGLN(node_type);
// Read saved settings from the config
config_load_settings();
DBUGF("After config_load_settings: %d", ESP.getFreeHeap());
timeClient.setTimeOffset(time_offset);
DBUG("Node name: ");
DBUGLN(node_name);
// ---------------------------------------------------------
// pin setup
pinMode(WIFI_LED, OUTPUT);
digitalWrite(WIFI_LED, !WIFI_LED_ON_STATE);
pinMode(CONTROL_PIN, OUTPUT);
digitalWrite(CONTROL_PIN, !CONTROL_PIN_ON_STATE);
// custom: analog output pin
#ifdef VOLTAGE_OUT_PIN
pinMode(4, OUTPUT);
#endif
// ---------------------------------------------------------
// Initial LED on
led_flash(3000, 100);
// Initialise the WiFi
wifi_setup();
DBUGF("After wifi_setup: %d", ESP.getFreeHeap());
led_flash(50, 50);
// Bring up the web server
web_server_setup();
DBUGF("After web_server_setup: %d", ESP.getFreeHeap());
led_flash(50, 50);
// Start the OTA update systems
ota_setup();
DBUGF("After ota_setup: %d", ESP.getFreeHeap());
// Start auto auth
auth_setup();
DBUGF("After auth_setup: %d", ESP.getFreeHeap());
// Time
timeClient.begin();
DBUGF("After timeClient.begin: %d", ESP.getFreeHeap());
delay(100);
start_mem = last_mem = ESP.getFreeHeap();
} // end setup
void led_flash(int ton, int toff) {
digitalWrite(WIFI_LED, WIFI_LED_ON_STATE);
delay(ton);
digitalWrite(WIFI_LED, WIFI_LED_ON_STATE);
delay(toff);
}
// -------------------------------------------------------------------
// LOOP
// -------------------------------------------------------------------
void loop()
{
if (millis() > mem_info_update) {
mem_info_update = millis() + 2000;
uint32_t current = ESP.getFreeHeap();
int32_t diff = (int32_t)(last_mem - current);
if(diff != 0) {
DEBUG.printf("Free memory %u - diff %d %d\n", current, diff, start_mem - current);
last_mem = current;
}
}
ota_loop();
web_server_loop();
wifi_loop();
timeClient.update();
StaticJsonDocument<512> data;
boolean gotInput = input_get(data);
if (wifi_client_connected())
{
mqtt_loop();
if(gotInput) {
emoncms_publish(data);
event_send(data);
}
}
auth_loop();
// --------------------------------------------------------------
// CONTROL UPDATE
// --------------------------------------------------------------
if ((millis()-last_ctrl_update)>1000 || ctrl_update) {
last_ctrl_update = millis();
ctrl_update = 0;
ctrl_state = 0; // default off
// 1. Timer
int timenow = timeClient.getHours()*100+timeClient.getMinutes();
if (timer_stop1>=timer_start1 && (timenow>=timer_start1 && timenow<timer_stop1)) ctrl_state = 1;
if (timer_stop2>=timer_start2 && (timenow>=timer_start2 && timenow<timer_stop2)) ctrl_state = 1;
if (timer_stop1<timer_start1 && (timenow>=timer_start1 || timenow<timer_stop1)) ctrl_state = 1;
if (timer_stop2<timer_start2 && (timenow>=timer_start2 || timenow<timer_stop2)) ctrl_state = 1;
// 2. On/Off
if (ctrl_mode=="On") ctrl_state = 1;
if (ctrl_mode=="Off") ctrl_state = 0;
// 3. Apply
if (ctrl_state) {
// ON
digitalWrite(CONTROL_PIN, CONTROL_PIN_ON_STATE);
} else {
digitalWrite(CONTROL_PIN, !CONTROL_PIN_ON_STATE);
}
#ifdef VOLTAGE_OUT_PIN
analogWrite(VOLTAGE_OUT_PIN, voltage_output);
#endif
}
// --------------------------------------------------------------
if ((millis()-last_pushbtn_check)>100) {
last_pushbtn_check = millis();
last_pushbtn_state = pushbtn_state;
pushbtn_state = !digitalRead(0);
if (pushbtn_state && last_pushbtn_state && !pushbtn_action) {
pushbtn_action = 1;
if (ctrl_mode=="On") ctrl_mode = "Off"; else ctrl_mode = "On";
if (mqtt_server!=0) mqtt_publish("out/ctrlmode",String(ctrl_mode));
}
if (!pushbtn_state && !last_pushbtn_state) pushbtn_action = 0;
}
} // end loop
String getTime() {
return timeClient.getFormattedTime();
}
void setTimeOffset() {
timeClient.setTimeOffset(time_offset);
}
void event_send(String &json)
{
StaticJsonDocument<512> event;
deserializeJson(event, json);
event_send(event);
}
void event_send(JsonDocument &event)
{
#ifdef ENABLE_DEBUG
serializeJson(event, DEBUG_PORT);
DBUGLN("");
#endif
web_server_event(event);
mqtt_publish(event);
}