send messages from main loop

This commit is contained in:
Travis Shears 2025-12-27 21:33:20 +01:00
parent e2140561eb
commit 58b102f97d
Signed by: travisshears
GPG key ID: CB9BF1910F3F7469

43
node1.c
View file

@ -16,8 +16,8 @@
// 5 sec loop is for testing
// #define LOOP_INTERVAL_MS 5000
// #define LOOP_INTERVAL_MS 10000
#define LOOP_INTERVAL_MS 30000
#define LOOP_INTERVAL_MS 10000
// #define LOOP_INTERVAL_MS 30000
/**
* Balcony Weather Station Node 1
@ -118,41 +118,26 @@ static bool cb_30(__unused struct repeating_timer *t) {
readings_index = 0;
}
char msg[256];
snprintf(msg, sizeof(msg), "M001,%.2f,%.2f,%2f\n",
snprintf(msg_to_send, sizeof(msg_to_send), "M001,%.2f,%.2f,%2f\n",
current_bem280_reading.temperature, current_bem280_reading.pressure,
current_bem280_reading.humidity);
printf("Sending temperature, pressure, and humidity to backend server...\n");
printf("MSG: %s", 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");
return false;
}
// printf("MSG: %s", msg_to_send);
// send PM readings
if (readings_index == 6) {
snprintf(msg, sizeof(msg), "M02,%.2f,%.2f,%2f\n",
snprintf(msg_to_send, sizeof(msg_to_send), "M02,%.2f,%.2f,%2f\n",
current_pms5003_reading.pm1, current_pms5003_reading.pm2_5,
current_pms5003_reading.pm10);
printf("Sending particulate matter readings to backend server...\n");
printf("MSG: %s", 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");
return false;
}
// printf("MSG: %s", msg);
}
return true;
};
bool cb_30_trigger = true;
int main() {
stdio_init_all();
watchdog_enable(60000, 1);
@ -186,5 +171,17 @@ int main() {
comms_led_update();
watchdog_update();
tight_loop_contents();
// Iterate over msgs_to_send and send them if not empty
if (msg_to_send[0] != '\0') {
bool success = tcp_client_send_message(&tcp_config, msg_to_send);
if (success) {
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");
}
}
}
}