interim commit

This commit is contained in:
Thomas Bishop 2026-03-01 15:21:45 +00:00
parent 59c31f1159
commit 5aea2f7ae2
3 changed files with 63 additions and 44 deletions

View file

@ -1,2 +1,2 @@
BasedOnStyle: LLVM BasedOnStyle: LLVM
BreakBeforeBraces: Linux BreakBeforeBraces: Allman

View file

@ -49,9 +49,9 @@ esp_mqtt_client_handle_t mqtt_connect()
esp_mqtt_client_config_t mqtt_config = {.broker.address.uri = esp_mqtt_client_config_t mqtt_config = {.broker.address.uri =
SECRETS_MQTT_BROKER}; SECRETS_MQTT_BROKER};
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_config); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_config);
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, ESP_ERROR_CHECK(esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID,
client); mqtt_event_handler, client));
esp_mqtt_client_start(client); ESP_ERROR_CHECK(esp_mqtt_client_start(client));
return client; return client;
} }
@ -60,11 +60,11 @@ void mqtt_publish(esp_mqtt_client_handle_t client, char *topic,
{ {
if (MQTT_CONNECTED) { if (MQTT_CONNECTED) {
ESP_LOGI(TAG, "Publishing payload %s to topic %s", payload, topic); ESP_LOGI(TAG, "Publishing payload %s to topic %s", payload, topic);
esp_mqtt_client_publish(client, topic, payload, strlen(payload), 0, 0); ESP_ERROR_CHECK(
esp_mqtt_client_publish(client, topic, payload, strlen(payload), 0, 0));
} else { } else {
ESP_LOGI(TAG, "Could not publish to MQTT topic %s. Not connected to broker", ESP_LOGI(TAG,
"Could not publish to MQTT topic %s. Not connected to broker.",
topic); topic);
} }
} }
// https://github.com/ESP32Tutorials/esp32-mqtt-pub-sub-esp-idf/blob/main/main/app_main.c

View file

@ -1,57 +1,70 @@
#include "esp_event.h" #include "esp_event.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_wifi.h" #include "esp_wifi.h"
#include "freertos/event_groups.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "secrets.h" #include "secrets.h"
// `TAG` is common naming convention for logs in ESP-IDF static const char *TAG = "wifi"; // Logs are coming from this wifi module
static const char *TAG = "wifi"; // Logs are coming from this wifi module. TAG = static uint32_t WIFI_CONNECTED = 0;
// common naming convention for logs in ESP-IDF
static EventGroupHandle_t s_wifi_event_group;
static int s_retry_num = 0; static int s_retry_num = 0;
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
#define MAX_RETRY 5 #define MAX_RETRY 5
static void wifi_event_handler(void *arg, esp_event_base_t event_base, static void wifi_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data) { int32_t event_id, void *event_data)
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { {
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
{
esp_wifi_connect(); esp_wifi_connect();
} else if (event_base == WIFI_EVENT && }
event_id == WIFI_EVENT_STA_DISCONNECTED) {
if (s_retry_num < MAX_RETRY) { if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
{
if (s_retry_num < MAX_RETRY)
{
esp_wifi_connect(); esp_wifi_connect();
s_retry_num++; s_retry_num++;
ESP_LOGI(TAG, "Retrying connection..."); ESP_LOGI(TAG, "Trying to reconnect to WiFi.");
} else {
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
} }
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { else
ESP_LOGI(TAG, "Connected! Got IP"); {
ESP_LOGI(TAG, "Could not connect to WiFi. Giving up.");
}
}
if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
{
s_retry_num = 0; s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); WIFI_CONNECTED = 1;
} }
} }
void wifi_connect(void) { void wifi_connect(void)
s_wifi_event_group = xEventGroupCreate(); {
ESP_LOGI(TAG, "Attempting to connect to WiFi");
// Initialise network interface layer (Internet Layer) once Link Layer
// established (in standard TCP/IP architecture)
ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_netif_init());
// Create default event dispatcher
ESP_ERROR_CHECK(esp_event_loop_create_default()); ESP_ERROR_CHECK(esp_event_loop_create_default());
// Having established interface layer, create an interface instance
esp_netif_create_default_wifi_sta(); esp_netif_create_default_wifi_sta();
// Initialise WiFi driver (Link Layer)
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_init(&cfg));
esp_event_handler_instance_t instance_any_id; ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID,
esp_event_handler_instance_t instance_got_ip; &wifi_event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_instance_register( ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID,
WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &wifi_event_handler, NULL));
&instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(
IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL,
&instance_got_ip));
wifi_config_t wifi_config = { wifi_config_t wifi_config = {
.sta = .sta =
@ -60,18 +73,24 @@ void wifi_connect(void) {
.password = SECRETS_WIFI_PASS, .password = SECRETS_WIFI_PASS,
}, },
}; };
// Set WiFi mode to 'station' (basically a client connecting to a WiFi access
// point)
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
// Give connection config to WiFi driver
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
// Begin the connection which triggers WIFI_EVENT_STA_START (the event which
// is the basis for the actual connection defined in the event handler)
ESP_ERROR_CHECK(esp_wifi_start()); ESP_ERROR_CHECK(esp_wifi_start());
// Wait for connection // Block main execution until WiFi connection est
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE, pdFALSE, portMAX_DELAY);
if (bits & WIFI_CONNECTED_BIT) { ESP_LOGI(TAG, "Waiting for WiFi connection...");
ESP_LOGI(TAG, "WiFi connected successfully");
} else { while (!WIFI_CONNECTED)
ESP_LOGE(TAG, "WiFi connection failed"); {
vTaskDelay(pdMS_TO_TICKS(100));
} }
} }