do work including wifi stuff in main loop instead of callback
"); "); ", readings_index); "); "); "); ");
This commit is contained in:
parent
58b102f97d
commit
9f4a163b53
1 changed files with 22 additions and 15 deletions
37
node1.c
37
node1.c
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
// 5 sec loop is for testing
|
// 5 sec loop is for testing
|
||||||
// #define LOOP_INTERVAL_MS 5000
|
// #define LOOP_INTERVAL_MS 5000
|
||||||
#define LOOP_INTERVAL_MS 10000
|
#define LOOP_INTERVAL_MS 20000
|
||||||
// #define LOOP_INTERVAL_MS 30000
|
// #define LOOP_INTERVAL_MS 30000
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -87,11 +87,22 @@ bool wifi_init(void) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Send a message over TCP to homeassistant event proxy
|
||||||
|
static void send_msg(char *msg) {
|
||||||
|
bool success = tcp_client_send_message(&tcp_config, msg);
|
||||||
|
if (success) {
|
||||||
|
printf("✓ Data sent successfully\n");
|
||||||
|
comms_led_blink();
|
||||||
|
} else {
|
||||||
|
printf("✗ Failed to send data\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Callback function called every 30 seconds
|
* Callback function called every 30 seconds
|
||||||
*/
|
*/
|
||||||
static bool cb_30(__unused struct repeating_timer *t) {
|
char msg_to_send[256];
|
||||||
comms_led_blink();
|
static bool do_work(void) {
|
||||||
printf("cb_30: %d\n", readings_index);
|
printf("cb_30: %d\n", readings_index);
|
||||||
|
|
||||||
// Read BME280
|
// Read BME280
|
||||||
|
|
@ -122,7 +133,7 @@ static bool cb_30(__unused struct repeating_timer *t) {
|
||||||
current_bem280_reading.temperature, current_bem280_reading.pressure,
|
current_bem280_reading.temperature, current_bem280_reading.pressure,
|
||||||
current_bem280_reading.humidity);
|
current_bem280_reading.humidity);
|
||||||
printf("Sending temperature, pressure, and humidity to backend server...\n");
|
printf("Sending temperature, pressure, and humidity to backend server...\n");
|
||||||
// printf("MSG: %s", msg_to_send);
|
send_msg(msg_to_send);
|
||||||
|
|
||||||
// send PM readings
|
// send PM readings
|
||||||
if (readings_index == 6) {
|
if (readings_index == 6) {
|
||||||
|
|
@ -130,13 +141,16 @@ static bool cb_30(__unused struct repeating_timer *t) {
|
||||||
current_pms5003_reading.pm1, current_pms5003_reading.pm2_5,
|
current_pms5003_reading.pm1, current_pms5003_reading.pm2_5,
|
||||||
current_pms5003_reading.pm10);
|
current_pms5003_reading.pm10);
|
||||||
printf("Sending particulate matter readings to backend server...\n");
|
printf("Sending particulate matter readings to backend server...\n");
|
||||||
// printf("MSG: %s", msg);
|
send_msg(msg_to_send);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool cb_30_trigger = true;
|
bool cb_30_trigger = true;
|
||||||
|
static bool cb_30(__unused struct repeating_timer *t) {
|
||||||
|
cb_30_trigger = true;
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
stdio_init_all();
|
stdio_init_all();
|
||||||
|
|
@ -172,16 +186,9 @@ int main() {
|
||||||
watchdog_update();
|
watchdog_update();
|
||||||
tight_loop_contents();
|
tight_loop_contents();
|
||||||
// Iterate over msgs_to_send and send them if not empty
|
// Iterate over msgs_to_send and send them if not empty
|
||||||
if (msg_to_send[0] != '\0') {
|
if (cb_30_trigger) {
|
||||||
bool success = tcp_client_send_message(&tcp_config, msg_to_send);
|
do_work();
|
||||||
if (success) {
|
cb_30_trigger = false;
|
||||||
printf("✓ Data sent successfully\n");
|
|
||||||
comms_led_blink();
|
|
||||||
// Clear the message after sending
|
|
||||||
msg_to_send[0] = '\0';
|
|
||||||
} else {
|
|
||||||
printf("✗ Failed to send data\n");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue