chore: reorganize source organization to a more modern style
This commit is contained in:
parent
21cb610d31
commit
0425836642
12 changed files with 16 additions and 10 deletions
24
src/aht.c
Normal file
24
src/aht.c
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include "aht.h"
|
||||
|
||||
#include "hardware/i2c.h"
|
||||
#include "pico/stdlib.h" // IWYU pragma: keep
|
||||
|
||||
static const uint8_t AHT_MEASURE[3] = {0xAC, 0x33, 0x00};
|
||||
|
||||
void getAHTData(i2c_inst_t *i2c, double *t, double *h) {
|
||||
uint8_t result[6];
|
||||
uint32_t temperature;
|
||||
uint32_t humidity;
|
||||
|
||||
i2c_write_blocking(i2c, AHT_ADDR, AHT_MEASURE, 3,
|
||||
false); // trigger measurement
|
||||
// now we must delay ~100ms
|
||||
sleep_ms(100);
|
||||
i2c_read_blocking(i2c, AHT_ADDR, result, 6,
|
||||
false); // read 6 bytes back
|
||||
humidity = (result[1] << 12) | (result[2] << 4) | (result[3] & 0xF0) >> 4;
|
||||
temperature = (result[3] & 0x0F) << 16 | (result[4] << 8) | result[5];
|
||||
*t = (((double)temperature / 1048576.0f) * 200.0f) - 50.0f;
|
||||
*h = ((double)humidity / 1048576.0f) * 100.0f;
|
||||
return;
|
||||
}
|
||||
122
src/bmp.c
Normal file
122
src/bmp.c
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
// Copyright 2026 A.M. Rowsell <amr@frzn.dev>
|
||||
// SPDX-License-Identifier: MIT
|
||||
#include "bmp.h"
|
||||
|
||||
float bmp_cal_data_fp[15];
|
||||
|
||||
int getBMPData(i2c_inst_t *i2c, float *temperature, float *pressure) {
|
||||
uint8_t rawMeasurements[7];
|
||||
uint32_t rawTemp;
|
||||
uint32_t rawPress;
|
||||
i2c_write_blocking(i2c, BMP_ADDR, &BMP_STATUS, 1, true);
|
||||
i2c_read_blocking(i2c, BMP_ADDR, rawMeasurements, 7,
|
||||
false); // read status & 6 data bytes
|
||||
if ((rawMeasurements[0] & 0x60) != 0x60) {
|
||||
// no measurements ready
|
||||
return 1;
|
||||
}
|
||||
rawPress = (rawMeasurements[3] << 16) | (rawMeasurements[2] << 8) |
|
||||
rawMeasurements[1];
|
||||
rawTemp = (rawMeasurements[6] << 16) | (rawMeasurements[5] << 8) |
|
||||
rawMeasurements[4];
|
||||
// now for the crazy math
|
||||
|
||||
float partial_data1;
|
||||
float partial_data2;
|
||||
float t_lin;
|
||||
|
||||
partial_data1 = (float)(rawTemp - bmp_cal_data_fp[PAR_T1]);
|
||||
partial_data2 = (float)(partial_data1 * bmp_cal_data_fp[PAR_T2]);
|
||||
t_lin =
|
||||
partial_data2 + (partial_data1 * partial_data1) * bmp_cal_data_fp[PAR_T3];
|
||||
// t_lin is compensated temperature
|
||||
|
||||
float comp_press;
|
||||
float partial_data3;
|
||||
float partial_data4;
|
||||
float partial_out1;
|
||||
float partial_out2;
|
||||
|
||||
partial_data1 = bmp_cal_data_fp[PAR_P6] * t_lin;
|
||||
partial_data2 = bmp_cal_data_fp[PAR_P7] * (t_lin * t_lin);
|
||||
partial_data3 = bmp_cal_data_fp[PAR_P8] * (t_lin * t_lin * t_lin);
|
||||
partial_out1 =
|
||||
bmp_cal_data_fp[PAR_P5] + partial_data1 + partial_data2 + partial_data3;
|
||||
|
||||
partial_data1 = bmp_cal_data_fp[PAR_P2] * t_lin;
|
||||
partial_data2 = bmp_cal_data_fp[PAR_P3] * (t_lin * t_lin);
|
||||
partial_data3 = bmp_cal_data_fp[PAR_P4] * (t_lin * t_lin * t_lin);
|
||||
partial_out2 = (float)rawPress * (bmp_cal_data_fp[PAR_P1] + partial_data1 +
|
||||
partial_data2 + partial_data3);
|
||||
|
||||
partial_data1 = (float)rawPress * (float)rawPress;
|
||||
partial_data2 = bmp_cal_data_fp[PAR_P9] + bmp_cal_data_fp[PAR_P10] * t_lin;
|
||||
partial_data3 = partial_data1 * partial_data2;
|
||||
partial_data4 =
|
||||
partial_data3 + ((float)rawPress * (float)rawPress * (float)rawPress) *
|
||||
bmp_cal_data_fp[PAR_P11];
|
||||
comp_press = partial_out1 + partial_out2 + partial_data4;
|
||||
|
||||
*temperature = t_lin;
|
||||
*pressure = comp_press;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int getBMPCalData(i2c_inst_t *i2c, bmp_cal_data_t *calData) {
|
||||
uint8_t rawValues[21];
|
||||
i2c_write_blocking(i2c, BMP_ADDR, &BMP_CAL_ADDR, 1, true);
|
||||
i2c_read_blocking(i2c, BMP_ADDR, rawValues, BMP_CAL_LEN, false);
|
||||
|
||||
calData->NVM_PAR_T1 = (uint16_t)((rawValues[1] << 8) | rawValues[0]);
|
||||
calData->NVM_PAR_T2 = (uint16_t)((rawValues[3] << 8) | rawValues[2]);
|
||||
calData->NVM_PAR_T3 = (int8_t)(rawValues[4]);
|
||||
calData->NVM_PAR_P1 = ((int16_t)(rawValues[6] << 8) | (int16_t)rawValues[5]);
|
||||
calData->NVM_PAR_P2 = ((int16_t)(rawValues[8] << 8) | (int16_t)rawValues[7]);
|
||||
calData->NVM_PAR_P3 = (int8_t)(rawValues[9]);
|
||||
calData->NVM_PAR_P4 = (int8_t)(rawValues[10]);
|
||||
calData->NVM_PAR_P5 = (uint16_t)((rawValues[12] << 8) | rawValues[11]);
|
||||
calData->NVM_PAR_P6 = (uint16_t)((rawValues[14] << 8) | rawValues[13]);
|
||||
calData->NVM_PAR_P7 = (int8_t)(rawValues[15]);
|
||||
calData->NVM_PAR_P8 = (int8_t)(rawValues[16]);
|
||||
calData->NVM_PAR_P9 =
|
||||
((int16_t)(rawValues[18] << 8) | (int16_t)rawValues[17]);
|
||||
calData->NVM_PAR_P10 = (int8_t)(rawValues[19]);
|
||||
calData->NVM_PAR_P11 = (int8_t)(rawValues[20]);
|
||||
|
||||
// convert cal values to floating point, adjusted values
|
||||
bmp_cal_data_fp[PAR_T1] = (float)calData->NVM_PAR_T1 * 256.0f;
|
||||
bmp_cal_data_fp[PAR_T2] = (float)calData->NVM_PAR_T2 / 1073741824.0f;
|
||||
bmp_cal_data_fp[PAR_T3] = (float)calData->NVM_PAR_T3 / 281474976710656.0f;
|
||||
bmp_cal_data_fp[PAR_P1] =
|
||||
((float)calData->NVM_PAR_P1 - 16384.0f) / 1048576.0f;
|
||||
bmp_cal_data_fp[PAR_P2] =
|
||||
((float)calData->NVM_PAR_P2 - 16384.0f) / 536870912.0f;
|
||||
bmp_cal_data_fp[PAR_P3] = (float)calData->NVM_PAR_P3 / 4294967296.0f;
|
||||
bmp_cal_data_fp[PAR_P4] = (float)calData->NVM_PAR_P4 / 137438953472.0f;
|
||||
bmp_cal_data_fp[PAR_P5] = (float)calData->NVM_PAR_P5 * 8.0f;
|
||||
bmp_cal_data_fp[PAR_P6] = (float)calData->NVM_PAR_P6 / 64.0f;
|
||||
bmp_cal_data_fp[PAR_P7] = (float)calData->NVM_PAR_P7 / 256.0f;
|
||||
bmp_cal_data_fp[PAR_P8] = (float)calData->NVM_PAR_P8 / 32768.0f;
|
||||
bmp_cal_data_fp[PAR_P9] = (float)calData->NVM_PAR_P9 / 281474976710656.0f;
|
||||
bmp_cal_data_fp[PAR_P10] = (float)calData->NVM_PAR_P10 / 281474976710656.0f;
|
||||
bmp_cal_data_fp[PAR_P11] =
|
||||
(float)calData->NVM_PAR_P11 / 36893488147419103232.0f;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int initBMP(i2c_inst_t *i2c) {
|
||||
static const uint8_t BMP_RESET[2] = {0x7E, 0xB6};
|
||||
static const uint8_t BMP_SETUP[6] = {0x1C, 0x0B, 0x1D, 0x08, 0x1F, 0x0A};
|
||||
static const uint8_t BMP_START[2] = {0x1B, 0x33};
|
||||
|
||||
bmp_cal_data_t bmpRawCalValues;
|
||||
i2c_write_blocking(i2c, BMP_ADDR, BMP_RESET, 2, false); // send reset
|
||||
sleep_ms(2); // wait for reset to complete
|
||||
getBMPCalData(i2c, &bmpRawCalValues);
|
||||
i2c_write_blocking(i2c, BMP_ADDR, BMP_SETUP, 6,
|
||||
false); // set up sampling parameters
|
||||
i2c_write_blocking(i2c, BMP_ADDR, BMP_START, 2,
|
||||
false); // start automatic measurements
|
||||
return 0;
|
||||
}
|
||||
41
src/ens.c
Normal file
41
src/ens.c
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#include "ens.h"
|
||||
|
||||
#include "hardware/i2c.h"
|
||||
#include "pico/stdlib.h" // IWYU pragma: keep
|
||||
|
||||
static const uint8_t ENS_ACTIVATE[2] = {0x10, 0x02};
|
||||
static const uint8_t ENS_IDLE[2] = {0x10, 0x01};
|
||||
|
||||
void activateENS(i2c_inst_t *i2c) {
|
||||
i2c_write_blocking(i2c, ENS_ADDR, ENS_ACTIVATE, 2, false);
|
||||
return;
|
||||
}
|
||||
void deactivateENS(i2c_inst_t *i2c) {
|
||||
i2c_write_blocking(i2c, ENS_ADDR, ENS_IDLE, 2, false);
|
||||
return;
|
||||
}
|
||||
uint8_t getENSData(i2c_inst_t *i2c, double *t, double *h, uint8_t *AQI, uint16_t *TVOC,
|
||||
uint16_t *ECO2, uint16_t *ETOH) {
|
||||
uint16_t K = (uint16_t)((*t + 273.15) * 64.0); // convert to ENS format
|
||||
uint16_t RH = (uint16_t)(*h * 512.0);
|
||||
uint8_t inputData[5] = {0x13, (uint8_t)(K & 0x00FF), (uint8_t)(K >> 8),
|
||||
(uint8_t)(RH & 0x00FF), (uint8_t)(RH >> 8)};
|
||||
uint8_t result[7];
|
||||
uint8_t dataReg = ENS_AQI;
|
||||
uint8_t valid;
|
||||
i2c_write_blocking(i2c, ENS_ADDR, inputData, 5, false);
|
||||
sleep_ms(100);
|
||||
i2c_write_blocking(i2c, ENS_ADDR, &dataReg, 1, true);
|
||||
i2c_read_blocking(i2c, ENS_ADDR, result, 7, false);
|
||||
*AQI = result[0];
|
||||
*TVOC = (result[2] << 8) | result[1];
|
||||
*ECO2 = (result[4] << 8) | result[3];
|
||||
*ETOH = (result[6] << 8) | result[5];
|
||||
dataReg = 0x20; // status register
|
||||
i2c_write_blocking(i2c, ENS_ADDR, &dataReg, 1, true);
|
||||
i2c_read_blocking(i2c, ENS_ADDR, result, 1, false);
|
||||
valid = (result[0] & ENS_STATUS_MASK) == 0
|
||||
? 1
|
||||
: 0; // check if we are in warm-up or normal mode
|
||||
return valid;
|
||||
}
|
||||
223
src/main.c
Normal file
223
src/main.c
Normal file
|
|
@ -0,0 +1,223 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
// SPDX-FileCopyrightText: 2026 A.M. Rowsell <amr@frzn.dev>
|
||||
/******************************************************************************
|
||||
* _ _
|
||||
* ___ _ __ ___ __ _| |__ | |_
|
||||
* / _ \ '_ \/ __|/ _` | '_ \| __|
|
||||
* | __/ | | \__ \ (_| | | | | |_
|
||||
* \___|_| |_|___/\__,_|_| |_|\__|
|
||||
*
|
||||
* Program Name: ensaht
|
||||
* Author: A.M Rowsell <amr@frzn.dev>
|
||||
* License: MIT License (Free use, modification, and distribution permitted
|
||||
* as long as the original copyright notice is included. Provided
|
||||
* "AS IS" without warranty).
|
||||
*
|
||||
* Description: Reads environmental data (temperature, humidity, AQI, TVOC,
|
||||
* eCO2, EtOH) via I2C from AHT and ENS sensors and publishes
|
||||
* the readings to Adafruit IO over MQTT using the Pico W.
|
||||
******************************************************************************/
|
||||
|
||||
#include "aht.h"
|
||||
#include "bmp.h"
|
||||
#include "ens.h"
|
||||
#include "mqtt.h"
|
||||
#include "hardware/gpio.h"
|
||||
#include "hardware/i2c.h"
|
||||
#include "pico/cyw43_arch.h"
|
||||
#include "pico/stdlib.h" // IWYU pragma: keep
|
||||
#include <boards/pico_w.h>
|
||||
#include <hardware/structs/io_bank0.h>
|
||||
#include <pico/binary_info/code.h>
|
||||
#include <pico/double.h>
|
||||
#include <pico/error.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define AVERAGES 8
|
||||
#define I2C1_SCL_PIN 19
|
||||
#define I2C1_SDA_PIN 18
|
||||
// MQTT and WiFi defines
|
||||
#define ADAFRUIT_IO_HOST "io.adafruit.com"
|
||||
#define ADAFRUIT_IO_USERNAME "ADAFRUIT_IO_USERNAME"
|
||||
#define ADAFRUIT_IO_PASSWORD "ADAFRUIT_IO_KEY"
|
||||
#define WIFI_SSID "Meosjin"
|
||||
#define WIFI_PASSWORD "WIFI_PASSWORD"
|
||||
#define MQTT_KEEP_ALIVE_SECONDS 60
|
||||
|
||||
static void mqtt_connection_cb(mqtt_client_t *client, void *arg,
|
||||
mqtt_connection_status_t status) {
|
||||
LWIP_UNUSED_ARG(client);
|
||||
LWIP_UNUSED_ARG(arg);
|
||||
|
||||
printf("MQTT status: %d\n", status);
|
||||
}
|
||||
|
||||
static float average_float(const float *samples, uint8_t count) {
|
||||
float sum = 0.0;
|
||||
for (uint8_t i = 0; i < count; i++) {
|
||||
sum += samples[i];
|
||||
}
|
||||
return sum / (float)count;
|
||||
}
|
||||
|
||||
static double average_double(const double *samples, uint8_t count) {
|
||||
double sum = 0.0;
|
||||
for (uint8_t i = 0; i < count; i++) {
|
||||
sum += samples[i];
|
||||
}
|
||||
return sum / (double)count;
|
||||
}
|
||||
|
||||
static uint16_t average_uint16(const uint16_t *samples, uint8_t count) {
|
||||
uint32_t sum = 0;
|
||||
for (uint8_t i = 0; i < count; i++) {
|
||||
sum += samples[i];
|
||||
}
|
||||
return (uint16_t)((sum + (uint32_t)count / 2UL) / (uint32_t)count);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
// MQTT Topics
|
||||
static const char temp_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.temperature";
|
||||
static const char humi_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.humidity";
|
||||
static const char aqi_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.aqi";
|
||||
static const char tvoc_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.tvoc";
|
||||
static const char etoh_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.etoh";
|
||||
static const char eco2_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.eco2";
|
||||
static const char prs_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.pressure";
|
||||
uint16_t tvoc, eco2, etoh, avg_tvoc, avg_eco2, avg_etoh;
|
||||
uint8_t aqi, validData;
|
||||
double t, h, avg_t, avg_h, avg_bmp_t, avg_bmp_p;
|
||||
uint16_t tvoc_avg[AVERAGES], eco2_avg[AVERAGES], etoh_avg[AVERAGES];
|
||||
double t_avg[AVERAGES], h_avg[AVERAGES];
|
||||
float bmpt_avg[AVERAGES];
|
||||
float bmpp_avg[AVERAGES];
|
||||
char payload[32];
|
||||
float bmpT = 0.0;
|
||||
float bmpP = 0.0;
|
||||
// MQTT setup
|
||||
ensaht_mqtt_t mqtt;
|
||||
struct mqtt_connect_client_info_t mqtt_client_info = {
|
||||
.client_id = ADAFRUIT_IO_USERNAME,
|
||||
.client_user = ADAFRUIT_IO_USERNAME,
|
||||
.client_pass = ADAFRUIT_IO_PASSWORD,
|
||||
.keep_alive = MQTT_KEEP_ALIVE_SECONDS,
|
||||
};
|
||||
// setup stdio
|
||||
stdio_init_all();
|
||||
// wifi chip initialization
|
||||
if (cyw43_arch_init_with_country(CYW43_COUNTRY_CANADA)) {
|
||||
printf("failed to initialise\n");
|
||||
return 1;
|
||||
}
|
||||
cyw43_arch_enable_sta_mode();
|
||||
int wifierr = 1;
|
||||
|
||||
wifierr = cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD,
|
||||
CYW43_AUTH_WPA2_AES_PSK, 30000);
|
||||
if (wifierr) {
|
||||
printf("failed to connect to Wi-Fi, reason %d\n", wifierr);
|
||||
switch (wifierr) {
|
||||
case PICO_ERROR_BADAUTH:
|
||||
printf("Password incorrect.\n");
|
||||
break;
|
||||
case PICO_ERROR_TIMEOUT:
|
||||
printf("Timeout ran out before connection.\n");
|
||||
break;
|
||||
case PICO_ERROR_CONNECT_FAILED:
|
||||
printf("A damn generic error code.\n");
|
||||
break;
|
||||
default:
|
||||
printf("Not a known error code.\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
ensaht_mqtt_init(&mqtt);
|
||||
err_t mqtt_err =
|
||||
ensaht_mqtt_connect(&mqtt, ADAFRUIT_IO_HOST, MQTT_PORT, &mqtt_client_info,
|
||||
mqtt_connection_cb, NULL);
|
||||
if (mqtt_err != ERR_OK) {
|
||||
printf("MQTT connect start failed: %d\n", mqtt_err);
|
||||
}
|
||||
i2c_init(i2c_default, 400 * 1000); // start i2c0 at 400khz
|
||||
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
|
||||
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
|
||||
gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
|
||||
gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
|
||||
|
||||
bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN,
|
||||
GPIO_FUNC_I2C));
|
||||
|
||||
i2c_init(i2c1,
|
||||
100 * 1000); // start i2c1 at 100kHz -- this is the stemma chain
|
||||
gpio_set_function(I2C1_SDA_PIN, GPIO_FUNC_I2C);
|
||||
gpio_set_function(I2C1_SCL_PIN, GPIO_FUNC_I2C);
|
||||
gpio_pull_up(I2C1_SDA_PIN);
|
||||
gpio_pull_up(I2C1_SCL_PIN);
|
||||
bi_decl(bi_2pins_with_func(I2C1_SDA_PIN, I2C1_SCL_PIN, GPIO_FUNC_I2C));
|
||||
initBMP(i2c1);
|
||||
printf("\x1b[2J"); // clear screen
|
||||
uint8_t i = 0;
|
||||
uint8_t sample_count = 0;
|
||||
activateENS(i2c_default);
|
||||
while (1) {
|
||||
// cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
|
||||
getAHTData(i2c_default, &t, &h);
|
||||
getBMPData(i2c1, &bmpT, &bmpP);
|
||||
t_avg[i] = t;
|
||||
h_avg[i] = h;
|
||||
bmpt_avg[i] = bmpT;
|
||||
bmpp_avg[i] = bmpP;
|
||||
if (sample_count < AVERAGES) {
|
||||
sample_count++;
|
||||
}
|
||||
avg_t = average_double(t_avg, sample_count);
|
||||
avg_h = average_double(h_avg, sample_count);
|
||||
avg_bmp_t = average_float(bmpt_avg, sample_count);
|
||||
avg_bmp_p = average_float(bmpp_avg, sample_count);
|
||||
printf("\x1b[H\x1b[JTemperature %0.2f C Humidity %0.2f%%\n", avg_t, avg_h);
|
||||
printf("BMP Temperature %0.2f C Pressure %0.2f kPa\n", avg_bmp_t,
|
||||
avg_bmp_p / 1000.0);
|
||||
validData =
|
||||
getENSData(i2c_default, &avg_bmp_t, &avg_h, &aqi, &tvoc, &eco2, &etoh);
|
||||
tvoc_avg[i] = tvoc;
|
||||
eco2_avg[i] = eco2;
|
||||
etoh_avg[i] = etoh;
|
||||
avg_tvoc = average_uint16(tvoc_avg, sample_count);
|
||||
avg_eco2 = average_uint16(eco2_avg, sample_count);
|
||||
avg_etoh = average_uint16(etoh_avg, sample_count);
|
||||
if (!validData) {
|
||||
printf("!! ");
|
||||
}
|
||||
printf("AQI: %d TVOC: %d eCO2: %d EtOH: %d\n", aqi, avg_tvoc, avg_eco2,
|
||||
avg_etoh);
|
||||
// cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
|
||||
i++;
|
||||
if (i == AVERAGES)
|
||||
i = 0;
|
||||
snprintf(payload, sizeof(payload), "%.4f", avg_bmp_t);
|
||||
ensaht_mqtt_publish_client(&mqtt, temp_topic, payload, strlen(payload),
|
||||
NULL, NULL);
|
||||
snprintf(payload, sizeof(payload), "%.4f", avg_h);
|
||||
ensaht_mqtt_publish_client(&mqtt, humi_topic, payload, strlen(payload),
|
||||
NULL, NULL);
|
||||
snprintf(payload, sizeof(payload), "%d", avg_tvoc);
|
||||
ensaht_mqtt_publish_client(&mqtt, tvoc_topic, payload, strlen(payload),
|
||||
NULL, NULL);
|
||||
snprintf(payload, sizeof(payload), "%d", avg_eco2);
|
||||
ensaht_mqtt_publish_client(&mqtt, eco2_topic, payload, strlen(payload),
|
||||
NULL, NULL);
|
||||
snprintf(payload, sizeof(payload), "%d", avg_etoh);
|
||||
ensaht_mqtt_publish_client(&mqtt, etoh_topic, payload, strlen(payload),
|
||||
NULL, NULL);
|
||||
snprintf(payload, sizeof(payload), "%d", aqi);
|
||||
ensaht_mqtt_publish_client(&mqtt, aqi_topic, payload, strlen(payload), NULL,
|
||||
NULL);
|
||||
snprintf(payload, sizeof(payload), "%.4f", avg_bmp_p / 1000.0);
|
||||
ensaht_mqtt_publish_client(&mqtt, prs_topic, payload, strlen(payload), NULL,
|
||||
NULL);
|
||||
sleep_ms(30000);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
123
src/mqtt.c
Normal file
123
src/mqtt.c
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
#include "mqtt.h"
|
||||
|
||||
#include "lwip/altcp.h" // IWYU pragma: keep
|
||||
#include "lwip/altcp_tls.h"
|
||||
#include "lwip/apps/mqtt_priv.h" // IWYU pragma: keep
|
||||
#include "lwip/dns.h"
|
||||
#include "pico/cyw43_arch.h"
|
||||
#include <string.h>
|
||||
|
||||
static void ensaht_mqtt_default_connect_cb(mqtt_client_t *client, void *arg,
|
||||
mqtt_connection_status_t status) {
|
||||
LWIP_UNUSED_ARG(client);
|
||||
LWIP_UNUSED_ARG(arg);
|
||||
LWIP_UNUSED_ARG(status);
|
||||
}
|
||||
|
||||
static err_t ensaht_mqtt_start(ensaht_mqtt_t *mqtt) {
|
||||
err_t err;
|
||||
|
||||
if (mqtt->client == NULL) {
|
||||
mqtt->client = mqtt_client_new();
|
||||
if (mqtt->client == NULL) {
|
||||
return ERR_MEM;
|
||||
}
|
||||
}
|
||||
|
||||
err = mqtt_client_connect(mqtt->client, &mqtt->server_addr, mqtt->port,
|
||||
mqtt->connect_cb, mqtt->connect_arg,
|
||||
&mqtt->client_info);
|
||||
#if LWIP_ALTCP && LWIP_ALTCP_TLS
|
||||
if (err == ERR_OK && mqtt->client_info.tls_config != NULL) {
|
||||
mbedtls_ssl_set_hostname(altcp_tls_context(mqtt->client->conn),
|
||||
mqtt->hostname);
|
||||
}
|
||||
#endif
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static void ensaht_mqtt_dns_found(const char *hostname, const ip_addr_t *ipaddr,
|
||||
void *arg) {
|
||||
ensaht_mqtt_t *mqtt = (ensaht_mqtt_t *)arg;
|
||||
LWIP_UNUSED_ARG(hostname);
|
||||
|
||||
if (ipaddr == NULL) {
|
||||
if (mqtt->connect_cb != NULL) {
|
||||
mqtt->connect_cb(mqtt->client, mqtt->connect_arg, MQTT_CONNECT_TIMEOUT);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
mqtt->server_addr = *ipaddr;
|
||||
cyw43_arch_lwip_begin();
|
||||
err_t err = ensaht_mqtt_start(mqtt);
|
||||
cyw43_arch_lwip_end();
|
||||
|
||||
if (err != ERR_OK && mqtt->connect_cb != NULL) {
|
||||
mqtt->connect_cb(mqtt->client, mqtt->connect_arg,
|
||||
MQTT_CONNECT_DISCONNECTED);
|
||||
}
|
||||
}
|
||||
|
||||
void ensaht_mqtt_init(ensaht_mqtt_t *mqtt) { memset(mqtt, 0, sizeof(*mqtt)); }
|
||||
|
||||
err_t ensaht_mqtt_connect(ensaht_mqtt_t *mqtt, const char *hostname, u16_t port,
|
||||
const struct mqtt_connect_client_info_t *client_info,
|
||||
mqtt_connection_cb_t cb, void *arg) {
|
||||
err_t err;
|
||||
|
||||
if (mqtt == NULL || hostname == NULL || client_info == NULL ||
|
||||
client_info->client_id == NULL) {
|
||||
return ERR_ARG;
|
||||
}
|
||||
|
||||
mqtt->hostname = hostname;
|
||||
mqtt->port = port != 0 ? port : MQTT_PORT;
|
||||
mqtt->client_info = *client_info;
|
||||
mqtt->connect_cb = cb != NULL ? cb : ensaht_mqtt_default_connect_cb;
|
||||
mqtt->connect_arg = arg;
|
||||
|
||||
cyw43_arch_lwip_begin();
|
||||
err = dns_gethostbyname(hostname, &mqtt->server_addr, ensaht_mqtt_dns_found,
|
||||
mqtt);
|
||||
if (err == ERR_OK) {
|
||||
err = ensaht_mqtt_start(mqtt);
|
||||
}
|
||||
cyw43_arch_lwip_end();
|
||||
|
||||
return err == ERR_INPROGRESS ? ERR_OK : err;
|
||||
}
|
||||
|
||||
void ensaht_mqtt_disconnect(ensaht_mqtt_t *mqtt) {
|
||||
if (mqtt == NULL || mqtt->client == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
cyw43_arch_lwip_begin();
|
||||
mqtt_disconnect(mqtt->client);
|
||||
cyw43_arch_lwip_end();
|
||||
}
|
||||
|
||||
err_t ensaht_mqtt_publish(mqtt_client_t *client, const char *topic,
|
||||
const void *payload, u16_t payload_length,
|
||||
mqtt_request_cb_t cb, void *arg) {
|
||||
return mqtt_publish(client, topic, payload, payload_length, 1, 0, cb, arg);
|
||||
}
|
||||
|
||||
err_t ensaht_mqtt_publish_client(ensaht_mqtt_t *mqtt, const char *topic,
|
||||
const void *payload, u16_t payload_length,
|
||||
mqtt_request_cb_t cb, void *arg) {
|
||||
err_t err;
|
||||
|
||||
if (mqtt == NULL || mqtt->client == NULL) {
|
||||
return ERR_CONN;
|
||||
}
|
||||
|
||||
cyw43_arch_lwip_begin();
|
||||
err = ensaht_mqtt_publish(mqtt->client, topic, payload, payload_length, cb,
|
||||
arg);
|
||||
cyw43_arch_lwip_end();
|
||||
|
||||
return err;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue