6#include <stm32_systime.h>
10#include "app_lorawan.h"
12#include "../user_config.h"
24static UTIL_TIMER_Object_t UploadTimer = {};
114 APP_LOG(TS_OFF, VLEVEL_M,
"WiFi app starting\r\n");
127 UTIL_SEQ_SetTask((1 << CFG_SEQ_Task_WiFiUpload), CFG_SEQ_Prio_1);
131 const size_t buffer_size = 256;
132 size_t buffer_len = 0;
133 uint8_t
buffer[buffer_size];
136 PayloadStatus payload_status = PAYLOAD_OK;
137 payload_status = FormatPayload(
buffer, buffer_size, &buffer_len);
138 if (payload_status == PAYLOAD_ERROR) {
139 APP_LOG(TS_OFF, VLEVEL_M,
"Error formatting payload\r\n");
141 }
else if (payload_status == PAYLOAD_NO_DATA) {
142 APP_LOG(TS_OFF, VLEVEL_M,
"No data to send\r\n");
147 APP_LOG(TS_ON, VLEVEL_M,
"Payload[%d]: ", buffer_len);
148 for (
int i = 0;
i < buffer_len;
i++) {
149 APP_LOG(TS_OFF, VLEVEL_M,
"%02X ",
buffer[
i]);
151 APP_LOG(TS_OFF, VLEVEL_M,
"\r\n");
154 APP_LOG(TS_ON, VLEVEL_M,
"Uploading data.");
156 APP_LOG(TS_OFF, VLEVEL_M,
"Error! Could not communicate with esp32!\r\n");
159 for (
unsigned int retries = 0;; retries++) {
161 APP_LOG(TS_OFF, VLEVEL_M,
".");
164 APP_LOG(TS_OFF, VLEVEL_M,
"(%d) \r\n", resp.
http_code);
171 APP_LOG(TS_OFF, VLEVEL_M,
172 "Error when posting data! Likely error with WiFi or response "
175 APP_LOG(TS_OFF, VLEVEL_M,
176 "Error with HTTP code! Likely error with measurement format or "
182 APP_LOG(TS_ON, VLEVEL_M,
"Max retries reached! Stopping upload.\r\n");
189 if (FramBufferLen() > 0) {
190 APP_LOG(TS_ON, VLEVEL_M,
"Buffer not empty, starting another upload\r\n");
199 APP_LOG(TS_ON, VLEVEL_M,
"Starting sensor measurements...\t");
201 APP_LOG(TS_OFF, VLEVEL_M,
"Started!\r\n");
205 UTIL_TIMER_Time_t UploadPeriod = cfg->Upload_interval * 1000;
208 APP_LOG(TS_ON, VLEVEL_M,
"Starting upload task...\t")
209 UTIL_SEQ_RegTask((1 << CFG_SEQ_Task_WiFiUpload), UTIL_SEQ_RFU,
Upload);
211 UTIL_TIMER_Create(&UploadTimer, UploadPeriod, UTIL_TIMER_PERIODIC,
213 UTIL_TIMER_Start(&UploadTimer);
214 APP_LOG(TS_OFF, VLEVEL_M,
"Started!\r\n");
219 APP_LOG(TS_ON, VLEVEL_M,
"Stopping upload task...\t");
220 UTIL_TIMER_Stop(&UploadTimer);
221 APP_LOG(TS_OFF, VLEVEL_M,
"Stopped!\r\n");
226 APP_LOG(TS_ON, VLEVEL_M,
"Starting upload task...\t")
227 UTIL_TIMER_Start(&UploadTimer);
228 APP_LOG(TS_OFF, VLEVEL_M,
"Started!\r\n");
249 const char*
ssid = cfg->WiFi_SSID;
250 const char* passwd = cfg->WiFi_Password;
252 APP_LOG(TS_ON, VLEVEL_M,
"Connecting to %s.",
ssid);
254 APP_LOG(TS_OFF, VLEVEL_M,
"Error! Could not communicate with esp32!\r\n");
258 for (
unsigned int retries = 0;; retries++) {
260 APP_LOG(TS_OFF, VLEVEL_M,
".")
267 APP_LOG(TS_OFF, VLEVEL_M,
"Error! Timeout after %d retries!\r\n",
274 APP_LOG(TS_OFF, VLEVEL_M,
"Error! No SSID available!\r\n");
277 APP_LOG(TS_OFF, VLEVEL_M,
"Error! Connect failed!\r\n");
284 APP_LOG(TS_OFF, VLEVEL_M,
"Connected!\r\n");
290 APP_LOG(TS_ON, VLEVEL_M,
"Disconnecting from WiFi.");
292 APP_LOG(TS_OFF, VLEVEL_M,
"Error! Could not communicate with esp32!\r\n");
296 for (
unsigned int retries = 0;; retries++) {
298 APP_LOG(TS_OFF, VLEVEL_M,
".")
307 APP_LOG(TS_OFF, VLEVEL_M,
"Error! Timeout after %d retries!\r\n",
313 APP_LOG(TS_OFF, VLEVEL_M,
"Done!\r\n");
319 SysTime_t ts = {.Seconds = -1, .SubSeconds = 0};
321 APP_LOG(TS_OFF, VLEVEL_M,
"Syncing time.");
323 APP_LOG(TS_OFF, VLEVEL_M,
"Error! Could not communicate with esp32!\r\n");
327 for (
unsigned int retries = 0;; retries++) {
329 APP_LOG(TS_OFF, VLEVEL_M,
".");
334 if (ts.Seconds != 0) {
338 APP_LOG(TS_OFF, VLEVEL_M,
"Error! Timeout after %d retries!\r\n",
347 APP_LOG(TS_OFF, VLEVEL_M,
"Done!\r\n");
348 APP_LOG(TS_OFF, VLEVEL_M,
"Current timestamp is %d\r\n", ts.Seconds);
359 const char* url = cfg->API_Endpoint_URL;
361 APP_LOG(TS_OFF, VLEVEL_M,
"Checking API health.");
363 APP_LOG(TS_OFF, VLEVEL_M,
"Error! Could not communicate with esp32!\r\n");
367 for (
unsigned int retries = 0;; retries++) {
368 APP_LOG(TS_OFF, VLEVEL_M,
".");
371 APP_LOG(TS_OFF, VLEVEL_M,
"(%d) \r\n", resp.
http_code);
378 APP_LOG(TS_OFF, VLEVEL_M,
379 "Error when posting data! Likely error with WiFi or response "
382 APP_LOG(TS_OFF, VLEVEL_M,
383 "Error with HTTP code! Likely error with measurement format or "
389 APP_LOG(TS_ON, VLEVEL_M,
"Max retries reached! Stopping upload.\r\n");
417 UserConfigSetupStop(60);
Controller module for stm32.
Circular buffer as FIFO data structure for buffering measurements.
bool ControllerWiFiConnect(const char *ssid, const char *passwd)
Initialize WiFi settings on the esp32.
ControllerWiFiStatus ControllerWiFiCheckWiFi(void)
Checks the current status of WiFi.
bool ControllerWiFiCheckApi(const char *url)
Queries the API endpoint to check if it is reachable.
bool ControllerWiFiDisconnect(void)
Disconnects from the WiFi network.
bool ControllerWiFiNtpSync(void)
Triggers a NTP sync with the server.
ControllerWiFiStatus
WiFi status.
uint32_t ControllerWiFiTime(void)
Get the current time from the NTP server.
bool ControllerWiFiPost(const uint8_t *data, size_t data_len)
Post data to the configured endpoint.
ControllerWiFiResponse ControllerWiFiCheckRequest(void)
Checks the status of the most recent HTTP request.
@ CONTROLLER_WIFI_CONNECTED
@ CONTROLLER_WIFI_IDLE_STATUS
@ CONTROLLER_WIFI_CONNECT_FAILED
@ CONTROLLER_WIFI_DISCONNECTED
@ CONTROLLER_WIFI_NO_SSID_AVAIL
const UserConfiguration * UserConfigGet(void)
Gets a reference to the loaded user configuration data in RAM.
bool Disconnect(void)
Disconnect from WiFi.
void StopUploads(void)
Stop upload timer.
bool TimeSync(void)
Timesync with NTP server.
const unsigned int retry_delay
Delay in milliseconds between retries.
void StartUploads(void)
Setup upload timer.
void Upload(void)
Upload data to hub.
void WiFiInit(void)
Initialize WiFi uploading method.
bool Esp32Init(void)
Call initialization functions for ESP32.
void ResumeUploads(void)
Resumes upload timer.
bool Check(void)
Check API health.
void ErrorHandler(void)
Error handler.
const unsigned int max_retries
Maximum number of retries for any event.
bool Connect(void)
Connect to WiFi.
void UploadEvent(void *context)
Function call for upload event.