init llm assisted tcp client

This commit is contained in:
Travis Shears 2025-12-21 17:32:42 +01:00
parent 908d563bc4
commit 3f54203246
Signed by: travisshears
GPG key ID: CB9BF1910F3F7469
12 changed files with 584 additions and 394 deletions

94
node1.c
View file

@ -1,6 +1,8 @@
#include "bme280.h"
#include "pico/stdlib.h"
#include "pms5003.h"
#include "pico/cyw43_arch.h"
#include "tcp_client.h"
#include <hardware/gpio.h>
#include <hardware/i2c.h>
#include <hardware/uart.h>
@ -13,10 +15,19 @@
#include <hardware/watchdog.h>
// 5 sec loop is for testing
// #define LOOP_INTERVAL_MS 5000
#define LOOP_INTERVAL_MS 10000
#define LOOP_INTERVAL_MS 5000
// #define LOOP_INTERVAL_MS 10000
// #define LOOP_INTERVAL_MS 30000
// WiFi and Backend Server Configuration
#ifndef BACKEND_SERVER_IP
#define BACKEND_SERVER_IP "192.168.1.153"
#endif
#ifndef BACKEND_SERVER_PORT
#define BACKEND_SERVER_PORT 8080
#endif
/**
* Balcony Weather Station Node 1
* record sensor data and send it to home assistant every 5 minutes
@ -28,6 +39,7 @@ void comms_led_init() {
gpio_set_dir(16, GPIO_OUT);
}
absolute_time_t comms_led_off_time;
int16_t comms_led_blink_count = 0;
bool comms_led_state = false;
@ -55,6 +67,9 @@ static bool cb_24h(__unused struct repeating_timer *t) {
return false; // Not reached
}
static tcp_client_config tcp_config;
static bool wifi_connected = false;
static pms5003_config pms_config;
static pms5003_reading current_pms5003_reading;
@ -78,6 +93,28 @@ static bme280_reading calculate_average_bme280_reading() {
return average_reading;
}
/**
* Initialize WiFi connection
*/
bool wifi_init(void) {
if (cyw43_arch_init()) {
printf("Failed to initialize CYW43\n");
return false;
}
cyw43_arch_enable_sta_mode();
printf("Connecting to WiFi '%s'...\n", WIFI_SSID);
if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD,
CYW43_AUTH_WPA2_AES_PSK, 30000)) {
printf("Failed to connect to WiFi\n");
return false;
}
printf("WiFi connected successfully\n");
return true;
}
/**
* Callback function called every 30 seconds
*/
@ -97,14 +134,33 @@ static bool cb_30(__unused struct repeating_timer *t) {
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);
// char msg[100];
// char msg[200];
// snprintf(msg, sizeof(msg), "{\"temp\": %.2f, \"pressure\": %.2f, \"humidity\": %.2f, \"pm1\": %.2f, \"pm2_5\": %.2f, \"pm10\": %.2f}\n",
// current_bem280_reading.temperature, current_bem280_reading.pressure, current_bem280_reading.humidity,
// current_pms5003_reading.pm1, current_pms5003_reading.pm2_5, current_pms5003_reading.pm10);
printf("Sending data to home assistant...\n");
// printf
// mqtt_client_pub_message(&mqtt_config, msg);
// Send to backend server if WiFi is connected
if (wifi_connected) {
char msg[256];
snprintf(msg, sizeof(msg),
"{\"temperature\":%.2f,\"pressure\":%.2f,\"humidity\":%.2f,"
"\"pm1\":%.2f,\"pm2_5\":%.2f,\"pm10\":%.2f}\n",
current_bem280_reading.temperature,
current_bem280_reading.pressure,
current_bem280_reading.humidity,
current_pms5003_reading.pm1,
current_pms5003_reading.pm2_5,
current_pms5003_reading.pm10);
printf("Sending data to backend server...\n");
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");
}
} else {
printf("WiFi not connected, skipping send\n");
}
return true;
}
@ -135,10 +191,28 @@ static bool cb_30(__unused struct repeating_timer *t) {
int main() {
stdio_init_all();
watchdog_enable(60000, 1);
sleep_ms(2000); // Give time for USB serial
// Initialize communication LED
comms_led_init();
// Initialize WiFi
printf("Initializing WiFi...\n");
if (!wifi_init()) {
printf("WiFi initialization failed!\n");
// Continue anyway for sensor readings
} 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");
wifi_connected = false;
} else {
printf("TCP client ready: %s:%d\n", BACKEND_SERVER_IP, BACKEND_SERVER_PORT);
}
}
// Setup BME280
bme280_init(&bem_config, i2c1, 14, 15);