format and try to get tcp working

");
");
");
");
");
", WIFI_SSID);
", WIFI_SSID);
");
");
");
");
", readings_index);
");
");
");
", current_pms5003_reading.pm1);
", current_pms5003_reading.pm2_5);
", current_pms5003_reading.pm10);
");
",
",
");
");
");
");
");
",
",
");
");
");
");
");
");
");
");
", BACKEND_SERVER_IP, BACKEND_SERVER_PORT);
", err);
", err);
");
", status);
");
", status);
", len);
", len);
");
");
", err);
");
", write_err);
", err);
");
", write_err);
", output_err);
", output_err);
");
");
", err);
", err);
");
");
", p->tot_len);
", p->tot_len);
",
",
");
");
", err);
", err);
", config->server_ip, config->server_port);
", config->server_ip,
");
");
");
");
", config->server_ip);
", config->server_ip);
",
",
");
");
");
");
This commit is contained in:
Travis Shears 2025-12-26 16:27:58 +01:00
parent e0a5fef8fc
commit e2140561eb
Signed by: travisshears
GPG key ID: CB9BF1910F3F7469
2 changed files with 232 additions and 251 deletions

57
node1.c
View file

@ -1,23 +1,23 @@
#include "bme280.h" #include "bme280.h"
#include "pico/cyw43_arch.h"
#include "pico/stdlib.h" #include "pico/stdlib.h"
#include "pms5003.h" #include "pms5003.h"
#include "pico/cyw43_arch.h"
#include "tcp_client.h" #include "tcp_client.h"
#include <hardware/gpio.h> #include <hardware/gpio.h>
#include <hardware/i2c.h> #include <hardware/i2c.h>
#include <hardware/uart.h> #include <hardware/uart.h>
#include <hardware/watchdog.h>
#include <pico/time.h> #include <pico/time.h>
#include <pico/types.h> #include <pico/types.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <hardware/watchdog.h>
// 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 10000
// #define LOOP_INTERVAL_MS 30000 #define LOOP_INTERVAL_MS 30000
/** /**
* Balcony Weather Station Node 1 * Balcony Weather Station Node 1
@ -51,12 +51,13 @@ void comms_led_update() {
static bool cb_24h(__unused struct repeating_timer *t) { static bool cb_24h(__unused struct repeating_timer *t) {
printf("Restarting Pico after 24 hours...\n"); printf("Restarting Pico after 24 hours...\n");
watchdog_enable(1, 1); // 1 ms timeout, reset on next loop watchdog_enable(1, 1); // 1 ms timeout, reset on next loop
while (1) { tight_loop_contents(); } // Wait for reset while (1) {
tight_loop_contents();
} // Wait for reset
return false; // Not reached return false; // Not reached
} }
static tcp_client_config tcp_config; static tcp_client_config tcp_config;
static bool wifi_connected = false;
static pms5003_config pms_config; static pms5003_config pms_config;
static pms5003_reading current_pms5003_reading; static pms5003_reading current_pms5003_reading;
@ -67,6 +68,7 @@ static bme280_reading current_bem280_reading;
// Initialize WiFi connection // Initialize WiFi connection
bool wifi_init(void) { bool wifi_init(void) {
printf("Initializing WiFi...\n");
if (cyw43_arch_init()) { if (cyw43_arch_init()) {
printf("Failed to initialize CYW43\n"); printf("Failed to initialize CYW43\n");
return false; return false;
@ -76,7 +78,7 @@ bool wifi_init(void) {
printf("Connecting to WiFi '%s'...\n", WIFI_SSID); printf("Connecting to WiFi '%s'...\n", WIFI_SSID);
if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD,
CYW43_AUTH_WPA2_AES_PSK, 30000)) { CYW43_AUTH_WPA2_AES_PSK, 60000)) {
printf("Failed to connect to WiFi\n"); printf("Failed to connect to WiFi\n");
return false; return false;
} }
@ -92,13 +94,10 @@ static bool cb_30(__unused struct repeating_timer *t) {
comms_led_blink(); comms_led_blink();
printf("cb_30: %d\n", readings_index); printf("cb_30: %d\n", readings_index);
// Read BME280 // Read BME280
printf("Making BME280 Reading\n"); printf("Making BME280 Reading\n");
current_bem280_reading = bme280_read(&bem_config); current_bem280_reading = bme280_read(&bem_config);
if (readings_index == 2) { if (readings_index == 2) {
printf("Warming up PMSS5003\n"); printf("Warming up PMSS5003\n");
pms5003_warmup(&pms_config); pms5003_warmup(&pms_config);
@ -112,10 +111,6 @@ static bool cb_30(__unused struct repeating_timer *t) {
if (readings_index == 6) { if (readings_index == 6) {
printf("Finished reading PMSS5003\n"); printf("Finished reading PMSS5003\n");
current_pms5003_reading = pms5003_finish_reading(&pms_config); current_pms5003_reading = pms5003_finish_reading(&pms_config);
// TODO: send data to backend server if WiFi is connected
// printf("PM1: %.2f\n", current_pms5003_reading.pm1);
// printf("PM2.5: %.2f\n", current_pms5003_reading.pm2_5);
// printf("PM10: %.2f\n", current_pms5003_reading.pm10);
} }
readings_index++; readings_index++;
@ -123,16 +118,9 @@ static bool cb_30(__unused struct repeating_timer *t) {
readings_index = 0; readings_index = 0;
} }
if (!wifi_connected) {
printf("WiFi not connected, skipping send\n");
return true;
}
char msg[256]; char msg[256];
snprintf(msg, sizeof(msg), snprintf(msg, sizeof(msg), "M001,%.2f,%.2f,%2f\n",
"M001,%.2f,%.2f,%2f\n", 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); printf("MSG: %s", msg);
@ -147,10 +135,8 @@ static bool cb_30(__unused struct repeating_timer *t) {
// send PM readings // send PM readings
if (readings_index == 6) { if (readings_index == 6) {
snprintf(msg, sizeof(msg), snprintf(msg, sizeof(msg), "M02,%.2f,%.2f,%2f\n",
"M02,%.2f,%.2f,%2f\n", 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); printf("MSG: %s", msg);
@ -176,28 +162,19 @@ int main() {
comms_led_init(); comms_led_init();
// Initialize WiFi // Initialize WiFi
printf("Initializing WiFi...\n");
if (!wifi_init()) { if (!wifi_init()) {
printf("WiFi initialization failed!\n");
panic("WiFi initialization failed!"); panic("WiFi initialization failed!");
} else {
wifi_connected = true;
// Initialize TCP client
if (!tcp_client_init(&tcp_config, BACKEND_SERVER_IP,
BACKEND_SERVER_PORT, 10000)) {
printf("TCP client initialization failed\n");
panic("TCP client initialization failed!");
wifi_connected = false;
} else {
printf("TCP client ready: %s:%d\n", BACKEND_SERVER_IP, BACKEND_SERVER_PORT);
} }
if (!tcp_client_init(&tcp_config, BACKEND_SERVER_IP, BACKEND_SERVER_PORT,
20000)) {
panic("TCP client initialization failed!");
} }
// Setup BME280 // Setup BME280
bme280_init(&bem_config, i2c1, 14, 15); bme280_init(&bem_config, i2c1, 14, 15);
// Setup PMS5003 // Setup PMS5003
// pms5003_init(&pms_config, uart1, 20, 21, 18, 19); pms5003_init(&pms_config, uart1, 20, 21, 18, 19);
struct repeating_timer timer_30; struct repeating_timer timer_30;
add_repeating_timer_ms(LOOP_INTERVAL_MS, cb_30, NULL, &timer_30); add_repeating_timer_ms(LOOP_INTERVAL_MS, cb_30, NULL, &timer_30);

View file

@ -4,13 +4,14 @@
*/ */
#include "tcp_client.h" #include "tcp_client.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include "lwip/pbuf.h" #include "lwip/pbuf.h"
#include "lwip/tcp.h" #include "lwip/tcp.h"
#include "pico/cyw43_arch.h"
#include "pico/stdlib.h"
#include "pico/time.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEBUG_printf printf #define DEBUG_printf printf
#define MAX_RETRY_COUNT 3 #define MAX_RETRY_COUNT 3
@ -61,7 +62,7 @@ static err_t tcp_client_result(tcp_client_state_t *state, int status) {
} }
static err_t tcp_client_sent(void *arg, struct tcp_pcb *tpcb, u16_t len) { static err_t tcp_client_sent(void *arg, struct tcp_pcb *tpcb, u16_t len) {
tcp_client_state_t *state = (tcp_client_state_t*)arg; tcp_client_state_t *state = (tcp_client_state_t *)arg;
DEBUG_printf("tcp_client: sent %u bytes\n", len); DEBUG_printf("tcp_client: sent %u bytes\n", len);
state->sent_len += len; state->sent_len += len;
@ -74,7 +75,7 @@ static err_t tcp_client_sent(void *arg, struct tcp_pcb *tpcb, u16_t len) {
} }
static err_t tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err) { static err_t tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err) {
tcp_client_state_t *state = (tcp_client_state_t*)arg; tcp_client_state_t *state = (tcp_client_state_t *)arg;
if (err != ERR_OK) { if (err != ERR_OK) {
DEBUG_printf("tcp_client: connect failed %d\n", err); DEBUG_printf("tcp_client: connect failed %d\n", err);
return tcp_client_result(state, err); return tcp_client_result(state, err);
@ -85,7 +86,8 @@ static err_t tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err) {
// Send the message // Send the message
cyw43_arch_lwip_begin(); cyw43_arch_lwip_begin();
err_t write_err = tcp_write(tpcb, state->message, state->message_len, TCP_WRITE_FLAG_COPY); err_t write_err =
tcp_write(tpcb, state->message, state->message_len, TCP_WRITE_FLAG_COPY);
if (write_err != ERR_OK) { if (write_err != ERR_OK) {
DEBUG_printf("tcp_client: failed to write data %d\n", write_err); DEBUG_printf("tcp_client: failed to write data %d\n", write_err);
cyw43_arch_lwip_end(); cyw43_arch_lwip_end();
@ -105,13 +107,13 @@ static err_t tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err) {
} }
static err_t tcp_client_poll(void *arg, struct tcp_pcb *tpcb) { static err_t tcp_client_poll(void *arg, struct tcp_pcb *tpcb) {
tcp_client_state_t *state = (tcp_client_state_t*)arg; tcp_client_state_t *state = (tcp_client_state_t *)arg;
DEBUG_printf("tcp_client: poll timeout\n"); DEBUG_printf("tcp_client: poll timeout\n");
return tcp_client_result(state, -1); return tcp_client_result(state, -1);
} }
static void tcp_client_err(void *arg, err_t err) { static void tcp_client_err(void *arg, err_t err) {
tcp_client_state_t *state = (tcp_client_state_t*)arg; tcp_client_state_t *state = (tcp_client_state_t *)arg;
if (err != ERR_ABRT) { if (err != ERR_ABRT) {
DEBUG_printf("tcp_client: error %d\n", err); DEBUG_printf("tcp_client: error %d\n", err);
state->success = false; state->success = false;
@ -119,8 +121,9 @@ static void tcp_client_err(void *arg, err_t err) {
} }
} }
static err_t tcp_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { static err_t tcp_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p,
tcp_client_state_t *state = (tcp_client_state_t*)arg; err_t err) {
tcp_client_state_t *state = (tcp_client_state_t *)arg;
if (!p) { if (!p) {
DEBUG_printf("tcp_client: connection closed by server\n"); DEBUG_printf("tcp_client: connection closed by server\n");
@ -183,7 +186,8 @@ bool tcp_client_init(tcp_client_config *config, const char *server_ip,
config->initialized = true; config->initialized = true;
config->internal_state = NULL; config->internal_state = NULL;
DEBUG_printf("tcp_client: initialized for %s:%u\n", config->server_ip, config->server_port); DEBUG_printf("tcp_client: initialized for %s:%u\n", config->server_ip,
config->server_port);
return true; return true;
} }
@ -225,6 +229,8 @@ bool tcp_client_send_message(tcp_client_config *config, const char *message) {
// Wait for completion or timeout // Wait for completion or timeout
while (!state->complete) { while (!state->complete) {
cyw43_arch_poll();
cyw43_arch_wait_for_work_until(make_timeout_time_ms(1000));
if (time_reached(state->timeout_time)) { if (time_reached(state->timeout_time)) {
DEBUG_printf("tcp_client: operation timed out\n"); DEBUG_printf("tcp_client: operation timed out\n");
tcp_client_close(state); tcp_client_close(state);
@ -232,8 +238,6 @@ bool tcp_client_send_message(tcp_client_config *config, const char *message) {
state->success = false; state->success = false;
break; break;
} }
cyw43_arch_poll();
cyw43_arch_wait_for_work_until(make_timeout_time_ms(100));
} }
bool success = state->success; bool success = state->success;