wifi: squash commit of wifi implementation
Squashed commit of the following:
commit 7684f6a159896d44ec7dc865aeb382ed674f5764
Author: A.M. Rowsell <amr@frzn.dev>
Date: Wed Jun 24 02:53:46 2026 -0400
wireless: these files also need to be present to build, oops.
commit 3b5760f4ffa6e0162fad54552a7274d3209334d8
Author: A.M. Rowsell <amr@frzn.dev>
Date: Tue Jun 23 10:57:55 2026 -0400
wifi: working setup with MQTT, plus averaging of readings
This commit is contained in:
parent
b1224528aa
commit
c937638cad
8 changed files with 547 additions and 26 deletions
|
|
@ -17,16 +17,20 @@ include(pico_sdk_import.cmake)
|
||||||
if (PICO_SDK_VERSION_STRING VERSION_LESS "1.4.0")
|
if (PICO_SDK_VERSION_STRING VERSION_LESS "1.4.0")
|
||||||
message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.4.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
|
message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.4.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
project(ensaht C CXX ASM)
|
|
||||||
|
|
||||||
# Initialise the Raspberry Pi Pico SDK
|
# Initialise the Raspberry Pi Pico SDK
|
||||||
pico_sdk_init()
|
pico_sdk_init()
|
||||||
|
|
||||||
|
project(ensaht C CXX ASM)
|
||||||
|
|
||||||
# Add executable. Default name is the project name, version 0.1
|
# Add executable. Default name is the project name, version 0.1
|
||||||
|
|
||||||
add_executable(ensaht ensaht.c )
|
add_executable(ensaht ensaht.c mqtt.c)
|
||||||
|
|
||||||
|
# Add the standard include files to the build
|
||||||
|
target_include_directories(ensaht PRIVATE
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/.. # for our common lwipopts or any other standard includes, if required
|
||||||
|
)
|
||||||
pico_set_program_name(ensaht "ensaht")
|
pico_set_program_name(ensaht "ensaht")
|
||||||
pico_set_program_version(ensaht "0.1")
|
pico_set_program_version(ensaht "0.1")
|
||||||
pico_enable_stdio_usb(ensaht 1)
|
pico_enable_stdio_usb(ensaht 1)
|
||||||
|
|
@ -37,23 +41,19 @@ target_link_libraries(ensaht
|
||||||
hardware_gpio
|
hardware_gpio
|
||||||
hardware_divider
|
hardware_divider
|
||||||
pico_double
|
pico_double
|
||||||
|
pico_cyw43_arch_lwip_threadsafe_background
|
||||||
|
pico_lwip_mqtt
|
||||||
|
pico_lwip_mbedtls
|
||||||
|
pico_mbedtls
|
||||||
pico_stdlib)
|
pico_stdlib)
|
||||||
if (PICO_CYW43_SUPPORTED)
|
|
||||||
target_link_libraries(ensaht pico_cyw43_arch_none)
|
|
||||||
endif()
|
|
||||||
# Add the standard include files to the build
|
|
||||||
target_include_directories(ensaht PRIVATE
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/.. # for our common lwipopts or any other standard includes, if required
|
|
||||||
)
|
|
||||||
|
|
||||||
pico_add_extra_outputs(ensaht)
|
pico_add_extra_outputs(ensaht)
|
||||||
|
|
||||||
add_custom_target(flash
|
add_custom_target(flash
|
||||||
COMMAND openocd
|
COMMAND openocd
|
||||||
-f interface/cmsis-dap.cfg
|
-f interface/cmsis-dap.cfg
|
||||||
-f target/rp2350.cfg
|
-f target/rp2040.cfg
|
||||||
-c "adapter speed 5000"
|
-c "adapter speed 10000"
|
||||||
-c "program ${CMAKE_CURRENT_BINARY_DIR}/ensaht.elf verify reset exit"
|
-c "program ${CMAKE_CURRENT_BINARY_DIR}/ensaht.elf verify reset exit"
|
||||||
DEPENDS ensaht
|
DEPENDS ensaht
|
||||||
)
|
)
|
||||||
|
|
|
||||||
153
ensaht.c
153
ensaht.c
|
|
@ -1,13 +1,16 @@
|
||||||
#include "hardware/gpio.h"
|
#include "hardware/gpio.h"
|
||||||
#include "hardware/i2c.h"
|
#include "hardware/i2c.h"
|
||||||
|
#include "mqtt.h"
|
||||||
#include "pico/cyw43_arch.h"
|
#include "pico/cyw43_arch.h"
|
||||||
#include "pico/stdlib.h"
|
#include "pico/stdlib.h"
|
||||||
#include <boards/pico_w.h>
|
#include <boards/pico_w.h>
|
||||||
#include <hardware/structs/io_bank0.h>
|
#include <hardware/structs/io_bank0.h>
|
||||||
#include <pico/binary_info/code.h>
|
#include <pico/binary_info/code.h>
|
||||||
#include <pico/double.h>
|
#include <pico/double.h>
|
||||||
|
#include <pico/error.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// I2C device defines
|
||||||
#define AHT_ADDR 0x38
|
#define AHT_ADDR 0x38
|
||||||
#define ENS_ADDR 0x53
|
#define ENS_ADDR 0x53
|
||||||
uint8_t AHT_MEASURE[3] = {0xAC, 0x33, 0x00};
|
uint8_t AHT_MEASURE[3] = {0xAC, 0x33, 0x00};
|
||||||
|
|
@ -22,6 +25,48 @@ uint8_t ENS_IDLE[2] = {0x10, 0x01};
|
||||||
#define ENS_ECO2 0x24
|
#define ENS_ECO2 0x24
|
||||||
#define ENS_ETOH 0x26
|
#define ENS_ETOH 0x26
|
||||||
|
|
||||||
|
#define AVERAGES 16
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
||||||
|
// MQTT Topics
|
||||||
|
char temp_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.temperature";
|
||||||
|
char humi_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.humidity";
|
||||||
|
char aqi_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.aqi";
|
||||||
|
char tvoc_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.tvoc";
|
||||||
|
char etoh_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.etoh";
|
||||||
|
char eco2_topic[] = "ADAFRUIT_IO_USERNAME/feeds/ensaht.eco2";
|
||||||
|
|
||||||
|
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 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 / 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 + count / 2) / count);
|
||||||
|
}
|
||||||
|
|
||||||
void getAHTData(double *t, double *h) {
|
void getAHTData(double *t, double *h) {
|
||||||
uint8_t result[6];
|
uint8_t result[6];
|
||||||
uint32_t temperature, humidity;
|
uint32_t temperature, humidity;
|
||||||
|
|
@ -54,7 +99,6 @@ uint8_t getENSData(double *t, double *h, uint8_t *AQI, uint16_t *TVOC,
|
||||||
(uint8_t)(RH & 0x00FF), (uint8_t)(RH >> 8)};
|
(uint8_t)(RH & 0x00FF), (uint8_t)(RH >> 8)};
|
||||||
uint8_t result[7], dataReg = ENS_AQI, valid;
|
uint8_t result[7], dataReg = ENS_AQI, valid;
|
||||||
i2c_write_blocking(i2c_default, ENS_ADDR, inputData, 5, false);
|
i2c_write_blocking(i2c_default, ENS_ADDR, inputData, 5, false);
|
||||||
activateENS();
|
|
||||||
sleep_ms(100);
|
sleep_ms(100);
|
||||||
i2c_write_blocking(i2c_default, ENS_ADDR, &dataReg, 1, true);
|
i2c_write_blocking(i2c_default, ENS_ADDR, &dataReg, 1, true);
|
||||||
i2c_read_blocking(i2c_default, ENS_ADDR, result, 7, false);
|
i2c_read_blocking(i2c_default, ENS_ADDR, result, 7, false);
|
||||||
|
|
@ -65,19 +109,62 @@ uint8_t getENSData(double *t, double *h, uint8_t *AQI, uint16_t *TVOC,
|
||||||
dataReg = 0x20; // status register
|
dataReg = 0x20; // status register
|
||||||
i2c_write_blocking(i2c_default, ENS_ADDR, &dataReg, 1, true);
|
i2c_write_blocking(i2c_default, ENS_ADDR, &dataReg, 1, true);
|
||||||
i2c_read_blocking(i2c_default, ENS_ADDR, result, 1, false);
|
i2c_read_blocking(i2c_default, ENS_ADDR, result, 1, false);
|
||||||
printf("Status register is 0x%0X\n", result[0]);
|
|
||||||
valid = (result[0] & ENS_STATUS_MASK) == 0
|
valid = (result[0] & ENS_STATUS_MASK) == 0
|
||||||
? 1
|
? 1
|
||||||
: 0; // check if we are in warm-up or normal mode
|
: 0; // check if we are in warm-up or normal mode
|
||||||
// deactivateENS();
|
|
||||||
return valid;
|
return valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
uint16_t tvoc, eco2, etoh;
|
uint16_t tvoc, eco2, etoh, avg_tvoc, avg_eco2, avg_etoh;
|
||||||
uint8_t aqi, validData;
|
uint8_t aqi, validData;
|
||||||
double t, h;
|
double t, h, avg_t, avg_h;
|
||||||
|
uint16_t tvoc_avg[AVERAGES], eco2_avg[AVERAGES], etoh_avg[AVERAGES];
|
||||||
|
double t_avg[AVERAGES], h_avg[AVERAGES];
|
||||||
|
char payload[32];
|
||||||
|
// 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();
|
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, 100 * 1000); // start i2c at 100khz
|
i2c_init(i2c_default, 100 * 1000); // start i2c at 100khz
|
||||||
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
|
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
|
||||||
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
|
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
|
||||||
|
|
@ -86,16 +173,56 @@ int main() {
|
||||||
|
|
||||||
bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN,
|
bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN,
|
||||||
GPIO_FUNC_I2C));
|
GPIO_FUNC_I2C));
|
||||||
|
printf("\x1b[2J"); // clear screen
|
||||||
|
uint8_t i = 0;
|
||||||
|
uint8_t sample_count = 0;
|
||||||
|
activateENS();
|
||||||
while (1) {
|
while (1) {
|
||||||
|
// cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
|
||||||
getAHTData(&t, &h);
|
getAHTData(&t, &h);
|
||||||
printf("Temperature %f Humidity %f\n", t, h);
|
t_avg[i] = t;
|
||||||
validData = getENSData(&t, &h, &aqi, &tvoc, &eco2, &etoh);
|
h_avg[i] = h;
|
||||||
if (!validData) {
|
if (sample_count < AVERAGES) {
|
||||||
printf("ENS Data not yet valid\n");
|
sample_count++;
|
||||||
}
|
}
|
||||||
printf("AQI: %d TVOC: %d eCO2: %d EtOH: %d\n", aqi, tvoc, eco2, etoh);
|
avg_t = average_double(t_avg, sample_count);
|
||||||
sleep_ms(2000);
|
avg_h = average_double(h_avg, sample_count);
|
||||||
|
printf("\x1b[J\x1b[HTemperature %0.2f Humidity %0.2f\n", avg_t, avg_h);
|
||||||
|
validData = getENSData(&avg_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), "%.2f", avg_t);
|
||||||
|
ensaht_mqtt_publish_client(&mqtt, temp_topic, payload, strlen(payload),
|
||||||
|
NULL, NULL);
|
||||||
|
snprintf(payload, sizeof(payload), "%.2f", 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);
|
||||||
|
sleep_ms(30000);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
33
lwipopts.h
Normal file
33
lwipopts.h
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
#ifndef _LWIPOPTS_H
|
||||||
|
#define _LWIPOPTS_H
|
||||||
|
|
||||||
|
// Generally you would define your own explicit list of lwIP options
|
||||||
|
// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html)
|
||||||
|
//
|
||||||
|
// This example uses a common include to avoid repetition
|
||||||
|
#include "lwipopts_examples_common.h"
|
||||||
|
|
||||||
|
/* TCP WND must be at least 16 kb to match TLS record size
|
||||||
|
or you will get a warning "altcp_tls: TCP_WND is smaller than the RX decrypion buffer, connection RX might stall!" */
|
||||||
|
#undef TCP_WND
|
||||||
|
#define TCP_WND 16384
|
||||||
|
|
||||||
|
#undef MEMP_NUM_SYS_TIMEOUT
|
||||||
|
#define MEMP_NUM_SYS_TIMEOUT (LWIP_NUM_SYS_TIMEOUT_INTERNAL + 8)
|
||||||
|
|
||||||
|
#undef MQTT_REQ_MAX_IN_FLIGHT
|
||||||
|
#define MQTT_REQ_MAX_IN_FLIGHT 8
|
||||||
|
|
||||||
|
#define LWIP_ALTCP 1
|
||||||
|
|
||||||
|
// If you don't want to use TLS (just a http request) you can avoid linking to mbedtls and remove the following
|
||||||
|
#define LWIP_ALTCP_TLS 1
|
||||||
|
#define LWIP_ALTCP_TLS_MBEDTLS 1
|
||||||
|
|
||||||
|
// Note bug in lwip with LWIP_ALTCP and LWIP_DEBUG
|
||||||
|
// https://savannah.nongnu.org/bugs/index.php?62159
|
||||||
|
//#define LWIP_DEBUG 1
|
||||||
|
#undef LWIP_DEBUG
|
||||||
|
#define ALTCP_MBEDTLS_DEBUG LWIP_DBG_ON
|
||||||
|
|
||||||
|
#endif
|
||||||
92
lwipopts_examples_common.h
Normal file
92
lwipopts_examples_common.h
Normal file
|
|
@ -0,0 +1,92 @@
|
||||||
|
#ifndef _LWIPOPTS_EXAMPLE_COMMONH_H
|
||||||
|
#define _LWIPOPTS_EXAMPLE_COMMONH_H
|
||||||
|
|
||||||
|
|
||||||
|
// Common settings used in most of the pico_w examples
|
||||||
|
// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html for details)
|
||||||
|
|
||||||
|
// allow override in some examples
|
||||||
|
#ifndef NO_SYS
|
||||||
|
#define NO_SYS 1
|
||||||
|
#endif
|
||||||
|
// allow override in some examples
|
||||||
|
#ifndef LWIP_SOCKET
|
||||||
|
#define LWIP_SOCKET 0
|
||||||
|
#endif
|
||||||
|
#if PICO_CYW43_ARCH_POLL
|
||||||
|
#define MEM_LIBC_MALLOC 1
|
||||||
|
#else
|
||||||
|
// MEM_LIBC_MALLOC is incompatible with non polling versions
|
||||||
|
#define MEM_LIBC_MALLOC 0
|
||||||
|
#endif
|
||||||
|
#define MEM_ALIGNMENT 4
|
||||||
|
#ifndef MEM_SIZE
|
||||||
|
#define MEM_SIZE 4000
|
||||||
|
#endif
|
||||||
|
#define MEMP_NUM_TCP_SEG 32
|
||||||
|
#define MEMP_NUM_ARP_QUEUE 10
|
||||||
|
#define PBUF_POOL_SIZE 24
|
||||||
|
#define LWIP_ARP 1
|
||||||
|
#define LWIP_ETHERNET 1
|
||||||
|
#define LWIP_ICMP 1
|
||||||
|
#define LWIP_RAW 1
|
||||||
|
#define TCP_WND (8 * TCP_MSS)
|
||||||
|
#define TCP_MSS 1460
|
||||||
|
#define TCP_SND_BUF (8 * TCP_MSS)
|
||||||
|
#define TCP_SND_QUEUELEN ((4 * (TCP_SND_BUF) + (TCP_MSS - 1)) / (TCP_MSS))
|
||||||
|
#define LWIP_NETIF_STATUS_CALLBACK 1
|
||||||
|
#define LWIP_NETIF_LINK_CALLBACK 1
|
||||||
|
#define LWIP_NETIF_HOSTNAME 1
|
||||||
|
#define LWIP_NETCONN 0
|
||||||
|
#define MEM_STATS 0
|
||||||
|
#define SYS_STATS 0
|
||||||
|
#define MEMP_STATS 0
|
||||||
|
#define LINK_STATS 0
|
||||||
|
// #define ETH_PAD_SIZE 2
|
||||||
|
#define LWIP_CHKSUM_ALGORITHM 3
|
||||||
|
#define LWIP_DHCP 1
|
||||||
|
#define LWIP_IPV4 1
|
||||||
|
#define LWIP_TCP 1
|
||||||
|
#define LWIP_UDP 1
|
||||||
|
#define LWIP_DNS 1
|
||||||
|
#define LWIP_TCP_KEEPALIVE 1
|
||||||
|
#define LWIP_NETIF_TX_SINGLE_PBUF 1
|
||||||
|
#define DHCP_DOES_ARP_CHECK 0
|
||||||
|
#define LWIP_DHCP_DOES_ACD_CHECK 0
|
||||||
|
|
||||||
|
#ifndef NDEBUG
|
||||||
|
#define LWIP_DEBUG 1
|
||||||
|
#define LWIP_STATS 1
|
||||||
|
#define LWIP_STATS_DISPLAY 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define ETHARP_DEBUG LWIP_DBG_OFF
|
||||||
|
#define NETIF_DEBUG LWIP_DBG_OFF
|
||||||
|
#define PBUF_DEBUG LWIP_DBG_OFF
|
||||||
|
#define API_LIB_DEBUG LWIP_DBG_OFF
|
||||||
|
#define API_MSG_DEBUG LWIP_DBG_OFF
|
||||||
|
#define SOCKETS_DEBUG LWIP_DBG_OFF
|
||||||
|
#define ICMP_DEBUG LWIP_DBG_OFF
|
||||||
|
#define INET_DEBUG LWIP_DBG_OFF
|
||||||
|
#define IP_DEBUG LWIP_DBG_OFF
|
||||||
|
#define IP_REASS_DEBUG LWIP_DBG_OFF
|
||||||
|
#define RAW_DEBUG LWIP_DBG_OFF
|
||||||
|
#define MEM_DEBUG LWIP_DBG_OFF
|
||||||
|
#define MEMP_DEBUG LWIP_DBG_OFF
|
||||||
|
#define SYS_DEBUG LWIP_DBG_OFF
|
||||||
|
#define TCP_DEBUG LWIP_DBG_OFF
|
||||||
|
#define TCP_INPUT_DEBUG LWIP_DBG_OFF
|
||||||
|
#define TCP_OUTPUT_DEBUG LWIP_DBG_OFF
|
||||||
|
#define TCP_RTO_DEBUG LWIP_DBG_OFF
|
||||||
|
#define TCP_CWND_DEBUG LWIP_DBG_OFF
|
||||||
|
#define TCP_WND_DEBUG LWIP_DBG_OFF
|
||||||
|
#define TCP_FR_DEBUG LWIP_DBG_OFF
|
||||||
|
#define TCP_QLEN_DEBUG LWIP_DBG_OFF
|
||||||
|
#define TCP_RST_DEBUG LWIP_DBG_OFF
|
||||||
|
#define UDP_DEBUG LWIP_DBG_OFF
|
||||||
|
#define TCPIP_DEBUG LWIP_DBG_OFF
|
||||||
|
#define PPP_DEBUG LWIP_DBG_OFF
|
||||||
|
#define SLIP_DEBUG LWIP_DBG_OFF
|
||||||
|
#define DHCP_DEBUG LWIP_DBG_OFF
|
||||||
|
|
||||||
|
#endif /* __LWIPOPTS_H__ */
|
||||||
6
mbedtls_config.h
Normal file
6
mbedtls_config.h
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef MBEDTLS_CONFIG_TLS_CLIENT_H
|
||||||
|
#define MBEDTLS_CONFIG_TLS_CLIENT_H
|
||||||
|
|
||||||
|
#include "mbedtls_config_examples_common.h"
|
||||||
|
|
||||||
|
#endif
|
||||||
75
mbedtls_config_examples_common.h
Normal file
75
mbedtls_config_examples_common.h
Normal file
|
|
@ -0,0 +1,75 @@
|
||||||
|
#ifndef MBEDTLS_CONFIG_EXAMPLES_COMMON_H
|
||||||
|
#define MBEDTLS_CONFIG_EXAMPLES_COMMON_H
|
||||||
|
|
||||||
|
/* Workaround for some mbedtls source files using INT_MAX without including limits.h */
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
#define MBEDTLS_NO_PLATFORM_ENTROPY
|
||||||
|
#define MBEDTLS_ENTROPY_HARDWARE_ALT
|
||||||
|
|
||||||
|
#define MBEDTLS_SSL_OUT_CONTENT_LEN 2048
|
||||||
|
|
||||||
|
#define MBEDTLS_ALLOW_PRIVATE_ACCESS
|
||||||
|
#define MBEDTLS_HAVE_TIME
|
||||||
|
#define MBEDTLS_PLATFORM_MS_TIME_ALT
|
||||||
|
|
||||||
|
#define MBEDTLS_CIPHER_MODE_CBC
|
||||||
|
#define MBEDTLS_ECP_DP_SECP192R1_ENABLED
|
||||||
|
#define MBEDTLS_ECP_DP_SECP224R1_ENABLED
|
||||||
|
#define MBEDTLS_ECP_DP_SECP256R1_ENABLED
|
||||||
|
#define MBEDTLS_ECP_DP_SECP384R1_ENABLED
|
||||||
|
#define MBEDTLS_ECP_DP_SECP521R1_ENABLED
|
||||||
|
#define MBEDTLS_ECP_DP_SECP192K1_ENABLED
|
||||||
|
#define MBEDTLS_ECP_DP_SECP224K1_ENABLED
|
||||||
|
#define MBEDTLS_ECP_DP_SECP256K1_ENABLED
|
||||||
|
#define MBEDTLS_ECP_DP_BP256R1_ENABLED
|
||||||
|
#define MBEDTLS_ECP_DP_BP384R1_ENABLED
|
||||||
|
#define MBEDTLS_ECP_DP_BP512R1_ENABLED
|
||||||
|
#define MBEDTLS_ECP_DP_CURVE25519_ENABLED
|
||||||
|
#define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
|
||||||
|
#define MBEDTLS_PKCS1_V15
|
||||||
|
#define MBEDTLS_SHA256_SMALLER
|
||||||
|
#define MBEDTLS_SSL_SERVER_NAME_INDICATION
|
||||||
|
#define MBEDTLS_AES_C
|
||||||
|
#define MBEDTLS_ASN1_PARSE_C
|
||||||
|
#define MBEDTLS_BIGNUM_C
|
||||||
|
#define MBEDTLS_CIPHER_C
|
||||||
|
#define MBEDTLS_CTR_DRBG_C
|
||||||
|
#define MBEDTLS_ENTROPY_C
|
||||||
|
#define MBEDTLS_ERROR_C
|
||||||
|
#define MBEDTLS_MD_C
|
||||||
|
#define MBEDTLS_MD5_C
|
||||||
|
#define MBEDTLS_OID_C
|
||||||
|
#define MBEDTLS_PKCS5_C
|
||||||
|
#define MBEDTLS_PK_C
|
||||||
|
#define MBEDTLS_PK_PARSE_C
|
||||||
|
#define MBEDTLS_PLATFORM_C
|
||||||
|
#define MBEDTLS_RSA_C
|
||||||
|
#define MBEDTLS_SHA1_C
|
||||||
|
#define MBEDTLS_SHA224_C
|
||||||
|
#define MBEDTLS_SHA256_C
|
||||||
|
#define MBEDTLS_SHA512_C
|
||||||
|
#define MBEDTLS_SSL_CLI_C
|
||||||
|
#define MBEDTLS_SSL_SRV_C
|
||||||
|
#define MBEDTLS_SSL_TLS_C
|
||||||
|
#define MBEDTLS_X509_CRT_PARSE_C
|
||||||
|
#define MBEDTLS_X509_USE_C
|
||||||
|
#define MBEDTLS_AES_FEWER_TABLES
|
||||||
|
|
||||||
|
/* TLS 1.2 */
|
||||||
|
#define MBEDTLS_SSL_PROTO_TLS1_2
|
||||||
|
#define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
|
||||||
|
#define MBEDTLS_GCM_C
|
||||||
|
#define MBEDTLS_ECDH_C
|
||||||
|
#define MBEDTLS_ECP_C
|
||||||
|
#define MBEDTLS_ECDSA_C
|
||||||
|
#define MBEDTLS_ASN1_WRITE_C
|
||||||
|
|
||||||
|
// The following is needed to parse a certificate
|
||||||
|
#define MBEDTLS_PEM_PARSE_C
|
||||||
|
#define MBEDTLS_BASE64_C
|
||||||
|
|
||||||
|
// The following significantly speeds up mbedtls due to NIST optimizations.
|
||||||
|
#define MBEDTLS_ECP_NIST_OPTIM
|
||||||
|
|
||||||
|
#endif
|
||||||
157
mqtt.c
Normal file
157
mqtt.c
Normal file
|
|
@ -0,0 +1,157 @@
|
||||||
|
#include "mqtt.h"
|
||||||
|
|
||||||
|
#include "lwip/altcp.h"
|
||||||
|
#include "lwip/altcp_tls.h"
|
||||||
|
#include "lwip/apps/mqtt_priv.h"
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
|
||||||
|
err_t ensaht_mqtt_pingreq(mqtt_client_t *client) {
|
||||||
|
static const u8_t pingreq[] = {0xC0, 0x00};
|
||||||
|
err_t err;
|
||||||
|
|
||||||
|
LWIP_ASSERT_CORE_LOCKED();
|
||||||
|
if (client == NULL || client->conn == NULL ||
|
||||||
|
!mqtt_client_is_connected(client)) {
|
||||||
|
return ERR_CONN;
|
||||||
|
}
|
||||||
|
|
||||||
|
err =
|
||||||
|
altcp_write(client->conn, pingreq, sizeof(pingreq), TCP_WRITE_FLAG_COPY);
|
||||||
|
if (err == ERR_OK) {
|
||||||
|
client->cyclic_tick = 0;
|
||||||
|
err = altcp_output(client->conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
err_t ensaht_mqtt_pingreq_client(ensaht_mqtt_t *mqtt) {
|
||||||
|
err_t err;
|
||||||
|
|
||||||
|
if (mqtt == NULL || mqtt->client == NULL) {
|
||||||
|
return ERR_CONN;
|
||||||
|
}
|
||||||
|
|
||||||
|
cyw43_arch_lwip_begin();
|
||||||
|
err = ensaht_mqtt_pingreq(mqtt->client);
|
||||||
|
cyw43_arch_lwip_end();
|
||||||
|
|
||||||
|
return err;
|
||||||
|
}
|
||||||
31
mqtt.h
Normal file
31
mqtt.h
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
#ifndef ENSAHT_MQTT_H
|
||||||
|
#define ENSAHT_MQTT_H
|
||||||
|
|
||||||
|
#include "lwip/apps/mqtt.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
mqtt_client_t *client;
|
||||||
|
ip_addr_t server_addr;
|
||||||
|
const char *hostname;
|
||||||
|
u16_t port;
|
||||||
|
struct mqtt_connect_client_info_t client_info;
|
||||||
|
mqtt_connection_cb_t connect_cb;
|
||||||
|
void *connect_arg;
|
||||||
|
} ensaht_mqtt_t;
|
||||||
|
|
||||||
|
void ensaht_mqtt_init(ensaht_mqtt_t *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);
|
||||||
|
void ensaht_mqtt_disconnect(ensaht_mqtt_t *mqtt);
|
||||||
|
|
||||||
|
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);
|
||||||
|
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 ensaht_mqtt_pingreq(mqtt_client_t *client);
|
||||||
|
err_t ensaht_mqtt_pingreq_client(ensaht_mqtt_t *mqtt);
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Add table
Add a link
Reference in a new issue