interim commit
This commit is contained in:
parent
59c31f1159
commit
5aea2f7ae2
3 changed files with 63 additions and 44 deletions
|
|
@ -1,2 +1,2 @@
|
|||
BasedOnStyle: LLVM
|
||||
BreakBeforeBraces: Linux
|
||||
BreakBeforeBraces: Allman
|
||||
|
|
|
|||
|
|
@ -49,9 +49,9 @@ esp_mqtt_client_handle_t mqtt_connect()
|
|||
esp_mqtt_client_config_t mqtt_config = {.broker.address.uri =
|
||||
SECRETS_MQTT_BROKER};
|
||||
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,
|
||||
client);
|
||||
esp_mqtt_client_start(client);
|
||||
ESP_ERROR_CHECK(esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID,
|
||||
mqtt_event_handler, client));
|
||||
ESP_ERROR_CHECK(esp_mqtt_client_start(client));
|
||||
return client;
|
||||
}
|
||||
|
||||
|
|
@ -60,11 +60,11 @@ void mqtt_publish(esp_mqtt_client_handle_t client, char *topic,
|
|||
{
|
||||
if (MQTT_CONNECTED) {
|
||||
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 {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// https://github.com/ESP32Tutorials/esp32-mqtt-pub-sub-esp-idf/blob/main/main/app_main.c
|
||||
|
|
|
|||
|
|
@ -1,57 +1,70 @@
|
|||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "freertos/task.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. TAG =
|
||||
// common naming convention for logs in ESP-IDF
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
static const char *TAG = "wifi"; // Logs are coming from this wifi module
|
||||
static uint32_t WIFI_CONNECTED = 0;
|
||||
|
||||
static int s_retry_num = 0;
|
||||
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
#define MAX_RETRY 5
|
||||
|
||||
static void wifi_event_handler(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void *event_data) {
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
||||
int32_t event_id, void *event_data)
|
||||
{
|
||||
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
|
||||
{
|
||||
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();
|
||||
s_retry_num++;
|
||||
ESP_LOGI(TAG, "Retrying connection...");
|
||||
} else {
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
|
||||
ESP_LOGI(TAG, "Trying to reconnect to WiFi.");
|
||||
}
|
||||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
ESP_LOGI(TAG, "Connected! Got IP");
|
||||
else
|
||||
{
|
||||
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;
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
WIFI_CONNECTED = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_connect(void) {
|
||||
s_wifi_event_group = xEventGroupCreate();
|
||||
void wifi_connect(void)
|
||||
{
|
||||
|
||||
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());
|
||||
|
||||
// Create default event dispatcher
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
||||
// Having established interface layer, create an interface instance
|
||||
esp_netif_create_default_wifi_sta();
|
||||
|
||||
// Initialise WiFi driver (Link Layer)
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
esp_event_handler_instance_t instance_any_id;
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(
|
||||
WIFI_EVENT, ESP_EVENT_ANY_ID, &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));
|
||||
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID,
|
||||
&wifi_event_handler, NULL));
|
||||
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID,
|
||||
&wifi_event_handler, NULL));
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.sta =
|
||||
|
|
@ -60,18 +73,24 @@ void wifi_connect(void) {
|
|||
.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));
|
||||
|
||||
// Give connection config to WiFi driver
|
||||
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());
|
||||
|
||||
// Wait for connection
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||
pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
// Block main execution until WiFi connection est
|
||||
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
ESP_LOGI(TAG, "WiFi connected successfully");
|
||||
} else {
|
||||
ESP_LOGE(TAG, "WiFi connection failed");
|
||||
ESP_LOGI(TAG, "Waiting for WiFi connection...");
|
||||
|
||||
while (!WIFI_CONNECTED)
|
||||
{
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue