start adding PMS5003 back in
"); "); "); "); "); ", "); "); "); "); "); ", "); "); "); "); "); ", link_status); "); "); "); "); ", link_status);
This commit is contained in:
parent
ca48e9a039
commit
9dd4ee4163
1 changed files with 64 additions and 33 deletions
97
node1.c
97
node1.c
|
|
@ -1,20 +1,26 @@
|
|||
#include "bme280.h"
|
||||
#include "pico/cyw43_arch.h"
|
||||
#include "pico/stdlib.h"
|
||||
#include <hardware/watchdog.h>
|
||||
#include "pms5003.h"
|
||||
#include "tcp_client.h"
|
||||
#include <hardware/watchdog.h>
|
||||
#include <pico/time.h>
|
||||
#include <pico/types.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define SENSOR_READING_INTERVAL_US (1000000 * 10) // 10 seconds
|
||||
#define SENSOR_READING_INTERVAL_US (1000000 * 15) // 15 seconds
|
||||
#define WIFI_CHECK_INTERVAL_US (1000000 * 10) // 10 seconds
|
||||
#define WIFI_STATUS_LED_PIN 16
|
||||
|
||||
static tcp_client_config tcp_config;
|
||||
static bme280_config bem_config;
|
||||
static bme280_reading current_bem280_reading;
|
||||
static pms5003_config pms_config;
|
||||
static pms5003_reading current_pms5003_reading;
|
||||
char msg_to_send[256];
|
||||
|
||||
static int8_t readings_index = 0;
|
||||
|
||||
static void send_msg(char *msg) {
|
||||
bool success = tcp_client_send_message(&tcp_config, msg);
|
||||
if (success) {
|
||||
|
|
@ -24,9 +30,6 @@ static void send_msg(char *msg) {
|
|||
}
|
||||
}
|
||||
|
||||
void comms_led_init() {
|
||||
}
|
||||
|
||||
int main() {
|
||||
stdio_init_all();
|
||||
watchdog_enable(120000, 1); // 120 second watchdog
|
||||
|
|
@ -40,11 +43,14 @@ int main() {
|
|||
gpio_set_dir(WIFI_STATUS_LED_PIN, GPIO_OUT);
|
||||
gpio_put(WIFI_STATUS_LED_PIN, false);
|
||||
|
||||
|
||||
printf("Initializing BME280...\n");
|
||||
bme280_init(&bem_config, i2c1, 14, 15);
|
||||
sleep_ms(1000);
|
||||
|
||||
printf("Initializing PMS5003...\n");
|
||||
pms5003_init(&pms_config, uart1, 20, 21, 18, 19);
|
||||
sleep_ms(1000);
|
||||
|
||||
printf("Initializing CYW43...\n");
|
||||
if (cyw43_arch_init()) {
|
||||
printf("FATAL: Failed to initialize CYW43\n");
|
||||
|
|
@ -93,18 +99,44 @@ int main() {
|
|||
|
||||
absolute_time_t now = get_absolute_time();
|
||||
|
||||
if (absolute_time_diff_us(last_sensor_reading, now) >= SENSOR_READING_INTERVAL_US) {
|
||||
if (absolute_time_diff_us(last_sensor_reading, now) >=
|
||||
SENSOR_READING_INTERVAL_US) {
|
||||
last_sensor_reading = now;
|
||||
printf("Making BME280 Reading\n");
|
||||
current_bem280_reading = bme280_read(&bem_config);
|
||||
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");
|
||||
send_msg(msg_to_send);
|
||||
current_bem280_reading.temperature,
|
||||
current_bem280_reading.pressure,
|
||||
current_bem280_reading.humidity);
|
||||
printf(
|
||||
"Sending temperature, pressure, and humidity to backend server...\n");
|
||||
send_msg(msg_to_send);
|
||||
watchdog_update();
|
||||
if (readings_index == 2) {
|
||||
printf("Warming up PMS5003\n");
|
||||
pms5003_warmup(&pms_config);
|
||||
}
|
||||
|
||||
// if (readings_index == 4) {
|
||||
// printf("Starting reads on PMSS5003\n");
|
||||
// pms5003_start_reading(&pms_config);
|
||||
// }
|
||||
|
||||
// if (readings_index == 6) {
|
||||
// printf("Finished reading PMSS5003\n");
|
||||
// current_pms5003_reading = pms5003_finish_reading(&pms_config);
|
||||
// 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);
|
||||
// }
|
||||
|
||||
readings_index++;
|
||||
if (readings_index >= 10) {
|
||||
readings_index = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (absolute_time_diff_us(last_check, now) >= 5000000) {
|
||||
if (absolute_time_diff_us(last_check, now) >= WIFI_CHECK_INTERVAL_US) {
|
||||
last_check = now;
|
||||
|
||||
// Check connection status
|
||||
|
|
@ -113,28 +145,27 @@ int main() {
|
|||
printf("[WiFi Status Check]\n");
|
||||
printf(" Link Status: ");
|
||||
switch (link_status) {
|
||||
case CYW43_LINK_DOWN:
|
||||
printf("DOWN\n");
|
||||
gpio_put(WIFI_STATUS_LED_PIN, false);
|
||||
break;
|
||||
case CYW43_LINK_JOIN:
|
||||
printf("JOINING\n");
|
||||
gpio_put(WIFI_STATUS_LED_PIN, false);
|
||||
break;
|
||||
case CYW43_LINK_NOIP:
|
||||
printf("NO IP\n");
|
||||
gpio_put(WIFI_STATUS_LED_PIN, false);
|
||||
break;
|
||||
case CYW43_LINK_UP:
|
||||
printf("UP\n");
|
||||
gpio_put(WIFI_STATUS_LED_PIN, true);
|
||||
break;
|
||||
default:
|
||||
printf("UNKNOWN (%d)\n", link_status);
|
||||
gpio_put(WIFI_STATUS_LED_PIN, false);
|
||||
break;
|
||||
case CYW43_LINK_DOWN:
|
||||
printf("DOWN\n");
|
||||
gpio_put(WIFI_STATUS_LED_PIN, false);
|
||||
break;
|
||||
case CYW43_LINK_JOIN:
|
||||
printf("JOINING\n");
|
||||
gpio_put(WIFI_STATUS_LED_PIN, false);
|
||||
break;
|
||||
case CYW43_LINK_NOIP:
|
||||
printf("NO IP\n");
|
||||
gpio_put(WIFI_STATUS_LED_PIN, false);
|
||||
break;
|
||||
case CYW43_LINK_UP:
|
||||
printf("UP\n");
|
||||
gpio_put(WIFI_STATUS_LED_PIN, true);
|
||||
break;
|
||||
default:
|
||||
printf("UNKNOWN (%d)\n", link_status);
|
||||
gpio_put(WIFI_STATUS_LED_PIN, false);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
sleep_ms(100);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue