Skip to content

Commit

Permalink
keep connecting until stopped
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Feb 11, 2022
1 parent a55e799 commit 3919f4d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 60 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ endif
ESP_IDF_VERSION := "v3.3.5"

fmt:
clang-format -i ./*.c ./*.h -style="{BasedOnStyle: Google, ColumnLimit: 120}"
clang-format -i ./test/main/*.c -style="{BasedOnStyle: Google, ColumnLimit: 120}"
clang-format -i ./*.c ./*.h -style="{BasedOnStyle: Google, ColumnLimit: 120, SortIncludes: false}"
clang-format -i ./test/main/*.c -style="{BasedOnStyle: Google, ColumnLimit: 120, SortIncludes: false}"

test/xtensa-esp32-elf:
wget https://dl.espressif.com/dl/$(XTENSA_TOOLCHAIN)
Expand Down
23 changes: 13 additions & 10 deletions esp_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <freertos/semphr.h>
#include <freertos/task.h>
#include <lwmqtt.h>
#include <stdio.h>
#include <string.h>

#if defined(CONFIG_ESP_MQTT_TLS_ENABLE)
Expand Down Expand Up @@ -274,7 +273,7 @@ static bool esp_mqtt_process_connect() {
return true;
}

static void esp_mqtt_process(void *p) {
static void esp_mqtt_process() {
// connection loop
for (;;) {
// log attempt
Expand All @@ -288,7 +287,7 @@ static void esp_mqtt_process(void *p) {
// log success
ESP_LOGI(ESP_MQTT_LOG_TAG, "esp_mqtt_process: connection attempt successful");

// set local flag
// set flag
esp_mqtt_connected = true;

// release mutex
Expand Down Expand Up @@ -411,23 +410,27 @@ static void esp_mqtt_process(void *p) {
esp_lwmqtt_network_disconnect(&esp_mqtt_network);
#endif

// set local flags
// set flags
esp_mqtt_connected = false;
esp_mqtt_running = false;
esp_mqtt_error = false;

// release mutex
ESP_MQTT_UNLOCK_MAIN();

ESP_LOGI(ESP_MQTT_LOG_TAG, "esp_mqtt_process: exit task");
// log event
ESP_LOGI(ESP_MQTT_LOG_TAG, "esp_mqtt_process: lost connection");

// call callback if existing
if (esp_mqtt_status_callback) {
esp_mqtt_status_callback(ESP_MQTT_STATUS_DISCONNECTED);
}
}

// delete task
vTaskDelete(NULL);
static void esp_mqtt_run(void *p) {
// keep processing
for (;;) {
esp_mqtt_process();
}
}

void esp_mqtt_lwt(const char *topic, const char *payload, int qos, bool retained) {
Expand Down Expand Up @@ -535,15 +538,15 @@ bool esp_mqtt_start(const char *host, const char *port, const char *client_id, c

// create mqtt thread
ESP_LOGI(ESP_MQTT_LOG_TAG, "esp_mqtt_start: create task");
BaseType_t ret = xTaskCreatePinnedToCore(esp_mqtt_process, "esp_mqtt", CONFIG_ESP_MQTT_TASK_STACK_SIZE, NULL,
BaseType_t ret = xTaskCreatePinnedToCore(esp_mqtt_run, "esp_mqtt", CONFIG_ESP_MQTT_TASK_STACK_SIZE, NULL,
CONFIG_ESP_MQTT_TASK_STACK_PRIORITY, &esp_mqtt_task, 1);
if (ret != pdPASS) {
ESP_LOGW(ESP_MQTT_LOG_TAG, "esp_mqtt_start: failed to create task");
ESP_MQTT_UNLOCK_MAIN();
return false;
}

// set local flag
// set flag
esp_mqtt_running = true;

// release mutex
Expand Down
5 changes: 3 additions & 2 deletions esp_mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ void esp_mqtt_lwt(const char *topic, const char *payload, int qos, bool retained
* The background process will attempt to connect to the specified broker once a second until a connection can be
* established. This process can be interrupted by calling `esp_mqtt_stop();`. If a connection has been established,
* the status callback will be called with `ESP_MQTT_STATUS_CONNECTED`. From that moment on the functions
* `esp_mqtt_subscribe`, `esp_mqtt_unsubscribe` and `esp_mqtt_publish` can be used to interact with the broker.
* `esp_mqtt_subscribe`, `esp_mqtt_unsubscribe` and `esp_mqtt_publish` can be used to interact with the broker. If an
* established connection fails, it will be retried until the process is stopped.
*
* @param host - The broker host.
* @param port - The broker port.
Expand Down Expand Up @@ -130,7 +131,7 @@ bool esp_mqtt_publish(const char *topic, uint8_t *payload, size_t len, int qos,
/**
* Stop the MQTT process.
*
* Will stop initial connection attempts or disconnect any active connection.
* Will stop the running MQTT process.
*/
void esp_mqtt_stop();

Expand Down
44 changes: 21 additions & 23 deletions test/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
#define MQTT_PORT "1883"
#define MQTTS_PORT "8883"

// openssl s_client -showcerts -connect public.cloud.shiftr.io:8883 -servername public.cloud.shiftr.io
#define PUBLISH_INTERVAL 1000
#define RESTART_INTERVAL 20000

// openssl s_client -showcerts -connect garage.cloud.shiftr.io:8883 -servername garage.cloud.shiftr.io
extern const uint8_t server_root_cert_pem_start[] asm("_binary_server_root_cert_pem_start");
extern const uint8_t server_root_cert_pem_end[] asm("_binary_server_root_cert_pem_end");

Expand All @@ -34,16 +37,19 @@ static void connect() {

static void process(void *p) {
for (;;) {
// publish roughly every second
// publish every second
esp_mqtt_publish("/hello", (uint8_t *)"world", 5, 2, false);
vTaskDelay(1000 / portTICK_PERIOD_MS);
vTaskDelay(PUBLISH_INTERVAL / portTICK_PERIOD_MS);
}
}

static void restart(void *_) {
// initial start
connect();

for (;;) {
// stop and start mqtt 15s
vTaskDelay(15000 / portTICK_PERIOD_MS);
// restart periodically
vTaskDelay(RESTART_INTERVAL / portTICK_PERIOD_MS);
esp_mqtt_stop();
connect();
}
Expand All @@ -53,22 +59,16 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) {
switch (event->event_id) {
case SYSTEM_EVENT_STA_START:
// connect to ap
esp_wifi_connect();
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_connect());

break;

case SYSTEM_EVENT_STA_GOT_IP:
// start mqtt
connect();

break;

case SYSTEM_EVENT_STA_DISCONNECTED:
// stop mqtt
esp_mqtt_stop();

// reconnect wifi
esp_wifi_connect();
// reconnect Wi-Fi
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_connect());

break;

Expand All @@ -88,9 +88,7 @@ static void status_callback(esp_mqtt_status_t status) {
break;

case ESP_MQTT_STATUS_DISCONNECTED:
// reconnect
connect();

default:
break;
}
}
Expand All @@ -100,30 +98,30 @@ static void message_callback(const char *topic, uint8_t *payload, size_t len) {
}

void app_main() {
// initialize nvs flash
// initialize NVS flash
ESP_ERROR_CHECK(nvs_flash_init());

// initialize tcp/ip adapter
// initialize TCP/IP adapter
tcpip_adapter_init();

// register event handler
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));

// initialize wifi
// initialize Wi-Fi
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));

// set wifi storage to ram
// set Wi-Fi storage to ram
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));

// set wifi mode
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));

// prepare wifi config
// prepare Wi-Fi config
wifi_config_t wifi_config = {.sta = {.ssid = WIFI_SSID, .password = WIFI_PASS}};
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));

// start wifi
// start Wi-Fi
ESP_ERROR_CHECK(esp_wifi_start());

// initialize mqtt
Expand Down
51 changes: 28 additions & 23 deletions test/main/server_root_cert.pem
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
-----BEGIN CERTIFICATE-----
MIIEZTCCA02gAwIBAgIQQAF1BIMUpMghjISpDBbN3zANBgkqhkiG9w0BAQsFADA/
MIIFYDCCBEigAwIBAgIQQAF3ITfU6UK47naqPGQKtzANBgkqhkiG9w0BAQsFADA/
MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT
DkRTVCBSb290IENBIFgzMB4XDTIwMTAwNzE5MjE0MFoXDTIxMDkyOTE5MjE0MFow
MjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxCzAJBgNVBAMT
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