-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.cpp
More file actions
160 lines (134 loc) · 4.63 KB
/
monitor.cpp
File metadata and controls
160 lines (134 loc) · 4.63 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
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
#include <stdio.h>
#include <stdarg.h>
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include "pico/binary_info.h"
#include "hardware/gpio.h"
#include "hardware/adc.h"
#include "hardware/watchdog.h"
#include "lwip/timeouts.h"
#include "mqtt_client.h"
#include "httpserver/httpserver.h"
#include "button.h"
#include "setting_defaults.h"
#include "settings.h"
#include "flash_memory.h"
#include "utils.h"
#include "wifi.h"
#include "debug.h"
#include "ADCLogger.h"
#define MPB_PIN 13 // GPIO pin to Momentary Push Button
#define ADC_PIN 26 // ADC0 GPIO pin
volatile bool bailout = false;
MPButton mpButton; // Momentary Push button
WifiConn wifi; // WiFi connection
ADCLogger ADCLog; // Buffer to store ADC readings
extern "C" uint get_wifi_json(char *sbuf, uint sz) {
return wifi.scan(sbuf, sz);
}
extern "C" uint get_data_json(char *sbuf, uint sz) {
return (ADCLog.toJSON(sbuf,sz));
}
void extInterrupt(uint gpio, uint32_t hw_e) {
if (gpio == MPB_PIN) {
mpButton.interrupt(hw_e); // notify the button object
} else DEBUG_printf("Unknown interrupt on gpio: %u", gpio);
}
void buttonEvent(Event e) {
DEBUG_printf("Button Event:%d\n",e);
// We are inside an interrupt here so dont do anything fancy
// Let the ilde loop do the work
switch (e) {
case CLICK:
break;
case DBCLICK:
break;
case LPRESS:
bailout= true;
break;
}
}
int main() {
stdio_init_all();
// Add metadata
bi_decl(bi_program_version_string(BUILD_VERSION));
bi_decl(bi_program_description("Monitor voltage on ADC0."));
bi_decl(bi_1pin_with_name(ADC_PIN, "ADC input"));
bi_decl(bi_1pin_with_name(LED_PIN, "Status LED")); // see utils.h for pin definition
bi_decl(bi_1pin_with_name(MPB_PIN, "Reset Button"));
// Initialise ADC and GPIO pins
adc_init();
adc_set_temp_sensor_enabled(true);
adc_gpio_init(ADC_PIN); // Set GPIO26 (channel 0) high-impedance for analog input
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
// Setup the button handler
mpButton.gpioSetup(MPB_PIN);
mpButton.eventHandler(&buttonEvent);
gpio_set_irq_enabled_with_callback(MPB_PIN, GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE, true, &extInterrupt);
// Initialise wifi and depending on settings start STA or AP modes
wifi.init(CYW43_COUNTRY_AUSTRALIA);
// Read Flash memory settings
if (settings_init()) {
onLED(); // this must not be called before cyw43 init()
wifi.start(WIFI_STA, &bailout);
offLED();
} else {
// If no settings available boot into AP mode
wifi.start(WIFI_AP, &bailout);
}
// Start web server
http_server_init();
// Start MQTT if needed
int publish_timer = get_settings()->mqtt_pint;
ADCLog.setSampleInterval(get_settings()->data_lint); // minutes
if (publish_timer > 0) {
mqtt_client_init(); // only start mqtt if the pub interval non zero
}
// Init idle settings
absolute_time_t set_time = get_absolute_time();
if (wifi.getMode() == WIFI_STA) watchdog_enable(60000, true);
bool status_led = false;
//
// Idle loop
//
while(true) {
sys_check_timeouts();
cyw43_arch_poll();
watchdog_update();
if (to_ms_since_boot(get_absolute_time()) - to_ms_since_boot(set_time) > 1000) {
//
// Do timer related tasks
//
if (wifi.getMode() == WIFI_AP) flashLED(2); // flash LED in AP mode
else {
if (status_led = !status_led) onLED(); else offLED(); // toggle LED in WIFI_STA mode
if (!wifi.isConnected()) {
wifi.deinit();
watchdog_reboot(0, 0, 200); // start over
}
}
if (publish_timer > 0) {
if (publish_timer == 1) {
mqtt_update();
publish_timer = get_settings()->mqtt_pint;
} else publish_timer -=1;
}
if (ADCLog.timeToNext() > 0) {
if (ADCLog.timeToNext() == 1) {
adc_select_input(0); // Select ADC input 0 (GPIO26)
ADCLog.writeSample(adc_read());
ADCLog.resetTimeToNext();
} else ADCLog.decTimeToNext();
}
set_time = get_absolute_time();
}
if (bailout) {
// User wants to reset with a new configuration.
// Make sure the idle loop only sees a bailout once.
bailout = false;
reset_settings();
}
sleep_ms(10); // Yield CPU to lwIP and background processes
}
}