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:
parent
e0a5fef8fc
commit
e2140561eb
2 changed files with 232 additions and 251 deletions
121
node1.c
121
node1.c
|
|
@ -1,23 +1,23 @@
|
|||
#include "bme280.h"
|
||||
#include "pico/cyw43_arch.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>
|
||||
#include <hardware/watchdog.h>
|
||||
#include <pico/time.h>
|
||||
#include <pico/types.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <hardware/watchdog.h>
|
||||
|
||||
// 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 30000
|
||||
#define LOOP_INTERVAL_MS 30000
|
||||
|
||||
/**
|
||||
* Balcony Weather Station Node 1
|
||||
|
|
@ -41,22 +41,23 @@ void comms_led_blink() {
|
|||
gpio_put(16, comms_led_state);
|
||||
}
|
||||
void comms_led_update() {
|
||||
if (time_reached(comms_led_off_time)) {
|
||||
comms_led_state = false;
|
||||
gpio_put(16, comms_led_state);
|
||||
}
|
||||
if (time_reached(comms_led_off_time)) {
|
||||
comms_led_state = false;
|
||||
gpio_put(16, comms_led_state);
|
||||
}
|
||||
}
|
||||
|
||||
// Callback to reset the Pico after 24 hours
|
||||
static bool cb_24h(__unused struct repeating_timer *t) {
|
||||
printf("Restarting Pico after 24 hours...\n");
|
||||
watchdog_enable(1, 1); // 1 ms timeout, reset on next loop
|
||||
while (1) { tight_loop_contents(); } // Wait for reset
|
||||
return false; // Not reached
|
||||
printf("Restarting Pico after 24 hours...\n");
|
||||
watchdog_enable(1, 1); // 1 ms timeout, reset on next loop
|
||||
while (1) {
|
||||
tight_loop_contents();
|
||||
} // Wait for reset
|
||||
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;
|
||||
|
|
@ -67,22 +68,23 @@ static bme280_reading current_bem280_reading;
|
|||
|
||||
// Initialize WiFi connection
|
||||
bool wifi_init(void) {
|
||||
if (cyw43_arch_init()) {
|
||||
printf("Failed to initialize CYW43\n");
|
||||
return false;
|
||||
}
|
||||
printf("Initializing WiFi...\n");
|
||||
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);
|
||||
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;
|
||||
}
|
||||
if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD,
|
||||
CYW43_AUTH_WPA2_AES_PSK, 60000)) {
|
||||
printf("Failed to connect to WiFi\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
printf("WiFi connected successfully\n");
|
||||
return true;
|
||||
printf("WiFi connected successfully\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -92,13 +94,10 @@ static bool cb_30(__unused struct repeating_timer *t) {
|
|||
comms_led_blink();
|
||||
printf("cb_30: %d\n", readings_index);
|
||||
|
||||
|
||||
|
||||
// Read BME280
|
||||
printf("Making BME280 Reading\n");
|
||||
current_bem280_reading = bme280_read(&bem_config);
|
||||
|
||||
|
||||
if (readings_index == 2) {
|
||||
printf("Warming up PMSS5003\n");
|
||||
pms5003_warmup(&pms_config);
|
||||
|
|
@ -112,10 +111,6 @@ static bool cb_30(__unused struct repeating_timer *t) {
|
|||
if (readings_index == 6) {
|
||||
printf("Finished reading PMSS5003\n");
|
||||
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++;
|
||||
|
|
@ -123,44 +118,35 @@ static bool cb_30(__unused struct repeating_timer *t) {
|
|||
readings_index = 0;
|
||||
}
|
||||
|
||||
if (!wifi_connected) {
|
||||
printf("WiFi not connected, skipping send\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
char msg[256];
|
||||
snprintf(msg, sizeof(msg),
|
||||
"M001,%.2f,%.2f,%2f\n",
|
||||
current_bem280_reading.temperature,
|
||||
current_bem280_reading.pressure,
|
||||
current_bem280_reading.humidity);
|
||||
snprintf(msg, sizeof(msg), "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();
|
||||
printf("✓ Data sent successfully\n");
|
||||
comms_led_blink();
|
||||
} else {
|
||||
printf("✗ Failed to send data\n");
|
||||
return false;
|
||||
printf("✗ Failed to send data\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
// send PM readings
|
||||
if (readings_index == 6) {
|
||||
snprintf(msg, sizeof(msg),
|
||||
"M02,%.2f,%.2f,%2f\n",
|
||||
current_pms5003_reading.pm1,
|
||||
current_pms5003_reading.pm2_5,
|
||||
current_pms5003_reading.pm10);
|
||||
snprintf(msg, sizeof(msg), "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();
|
||||
printf("✓ Data sent successfully\n");
|
||||
comms_led_blink();
|
||||
} else {
|
||||
printf("✗ Failed to send data\n");
|
||||
return false;
|
||||
printf("✗ Failed to send data\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -170,34 +156,25 @@ 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
|
||||
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");
|
||||
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);
|
||||
}
|
||||
panic("WiFi initialization failed!");
|
||||
}
|
||||
if (!tcp_client_init(&tcp_config, BACKEND_SERVER_IP, BACKEND_SERVER_PORT,
|
||||
20000)) {
|
||||
panic("TCP client initialization failed!");
|
||||
}
|
||||
|
||||
// Setup BME280
|
||||
bme280_init(&bem_config, i2c1, 14, 15);
|
||||
|
||||
// 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;
|
||||
add_repeating_timer_ms(LOOP_INTERVAL_MS, cb_30, NULL, &timer_30);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue