From 17909ded76695fec472cc96f8f8c154fe74ec24c Mon Sep 17 00:00:00 2001 From: 1998-felix Date: Wed, 23 Aug 2023 13:57:46 +0300 Subject: [PATCH 1/4] Add esp32 mqtts capability Signed-off-by: 1998-felix --- targets/esp32/.gitignore | 2 + targets/esp32/mqtts/.gitignore | 1 + targets/esp32/mqtts/CMakeLists.txt | 9 ++ targets/esp32/mqtts/include/cnetwork.h | 21 ++++ targets/esp32/mqtts/include/config.h | 11 ++ targets/esp32/mqtts/platformio.ini | 14 +++ targets/esp32/mqtts/src/CMakeLists.txt | 4 + targets/esp32/mqtts/src/client.crt | 1 + targets/esp32/mqtts/src/client.key | 1 + targets/esp32/mqtts/src/cnetwork.c | 69 ++++++++++++ targets/esp32/mqtts/src/main.c | 141 +++++++++++++++++++++++++ targets/esp32/mqtts/src/mainflux.crt | 1 + 12 files changed, 275 insertions(+) create mode 100644 targets/esp32/.gitignore create mode 100644 targets/esp32/mqtts/.gitignore create mode 100644 targets/esp32/mqtts/CMakeLists.txt create mode 100644 targets/esp32/mqtts/include/cnetwork.h create mode 100644 targets/esp32/mqtts/include/config.h create mode 100644 targets/esp32/mqtts/platformio.ini create mode 100644 targets/esp32/mqtts/src/CMakeLists.txt create mode 100644 targets/esp32/mqtts/src/client.crt create mode 100644 targets/esp32/mqtts/src/client.key create mode 100644 targets/esp32/mqtts/src/cnetwork.c create mode 100644 targets/esp32/mqtts/src/main.c create mode 100644 targets/esp32/mqtts/src/mainflux.crt diff --git a/targets/esp32/.gitignore b/targets/esp32/.gitignore new file mode 100644 index 0000000..b9f3806 --- /dev/null +++ b/targets/esp32/.gitignore @@ -0,0 +1,2 @@ +.pio +.vscode diff --git a/targets/esp32/mqtts/.gitignore b/targets/esp32/mqtts/.gitignore new file mode 100644 index 0000000..722d5e7 --- /dev/null +++ b/targets/esp32/mqtts/.gitignore @@ -0,0 +1 @@ +.vscode diff --git a/targets/esp32/mqtts/CMakeLists.txt b/targets/esp32/mqtts/CMakeLists.txt new file mode 100644 index 0000000..c13608e --- /dev/null +++ b/targets/esp32/mqtts/CMakeLists.txt @@ -0,0 +1,9 @@ + +cmake_minimum_required(VERSION 3.16) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(mqtts) + +target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "src/client.crt" TEXT) +target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "src/client.key" TEXT) +target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "src/mainflux.crt" TEXT) \ No newline at end of file diff --git a/targets/esp32/mqtts/include/cnetwork.h b/targets/esp32/mqtts/include/cnetwork.h new file mode 100644 index 0000000..7b2470c --- /dev/null +++ b/targets/esp32/mqtts/include/cnetwork.h @@ -0,0 +1,21 @@ +#ifndef C_NETWORK_H +#define C_NETWORK_H + +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_mac.h" +#include "esp_wifi.h" +#include "esp_event.h" +#include "esp_log.h" +#include "nvs_flash.h" + +#include "lwip/err.h" +#include "lwip/sys.h" + +static void wifi_event_handler(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data); + +void wifi_init_softap(void); + +#endif \ No newline at end of file diff --git a/targets/esp32/mqtts/include/config.h b/targets/esp32/mqtts/include/config.h new file mode 100644 index 0000000..3437fce --- /dev/null +++ b/targets/esp32/mqtts/include/config.h @@ -0,0 +1,11 @@ +#ifndef CONFIG_H +#define CONFIG_H + +const char *mfThingId = " "; +const char *mfThingPass = " "; +const char *mfChannelId = " "; +char mfTopic[150]; + +const char *server = " "; + +#endif diff --git a/targets/esp32/mqtts/platformio.ini b/targets/esp32/mqtts/platformio.ini new file mode 100644 index 0000000..c4d17d9 --- /dev/null +++ b/targets/esp32/mqtts/platformio.ini @@ -0,0 +1,14 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32-c3-devkitc-02] +platform = espressif32 +board = esp32-c3-devkitc-02 +framework = espidf diff --git a/targets/esp32/mqtts/src/CMakeLists.txt b/targets/esp32/mqtts/src/CMakeLists.txt new file mode 100644 index 0000000..af2cb2f --- /dev/null +++ b/targets/esp32/mqtts/src/CMakeLists.txt @@ -0,0 +1,4 @@ + +FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*) + +idf_component_register(SRCS ${app_sources}) diff --git a/targets/esp32/mqtts/src/client.crt b/targets/esp32/mqtts/src/client.crt new file mode 100644 index 0000000..73d9658 --- /dev/null +++ b/targets/esp32/mqtts/src/client.crt @@ -0,0 +1 @@ +Client certificate \ No newline at end of file diff --git a/targets/esp32/mqtts/src/client.key b/targets/esp32/mqtts/src/client.key new file mode 100644 index 0000000..a09d13e --- /dev/null +++ b/targets/esp32/mqtts/src/client.key @@ -0,0 +1 @@ +client key \ No newline at end of file diff --git a/targets/esp32/mqtts/src/cnetwork.c b/targets/esp32/mqtts/src/cnetwork.c new file mode 100644 index 0000000..0d7198e --- /dev/null +++ b/targets/esp32/mqtts/src/cnetwork.c @@ -0,0 +1,69 @@ +/***** +Implements a network interface fpr the esp. +Current interface is wifi. +**/ + +#include "cnetwork.h" + +#define ESP_WIFI_SSID " " // Enter the wifi ssid +#define ESP_WIFI_PASS " " // Enter the wifi password + +static const char *TAG = "wifi softAP"; // tag for esp logging + +// Wifi event handler +static void wifi_event_handler(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + if (event_id == WIFI_EVENT_AP_STACONNECTED) + { + wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *)event_data; + ESP_LOGI(TAG, "station " MACSTR " join, AID=%d", + MAC2STR(event->mac), event->aid); + } + else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) + { + wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data; + ESP_LOGI(TAG, "station " MACSTR " leave, AID=%d", + MAC2STR(event->mac), event->aid); + } +} + +// Wi-fi initial +void wifi_init_softap(void) +{ + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + esp_netif_create_default_wifi_ap(); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + + ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, + ESP_EVENT_ANY_ID, + &wifi_event_handler, + NULL, + NULL)); + + wifi_config_t wifi_config = { + .ap = { + .ssid = ESP_WIFI_SSID, + .ssid_len = strlen(ESP_WIFI_SSID), + .password = ESP_WIFI_PASS, + .authmode = WIFI_AUTH_WPA2_PSK, + .pmf_cfg = { + .required = true, + }, + }, + }; + if (strlen(ESP_WIFI_PASS) == 0) + { + wifi_config.ap.authmode = WIFI_AUTH_OPEN; + } + + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP)); + ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config)); + ESP_ERROR_CHECK(esp_wifi_start()); + + ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s", + ESP_WIFI_SSID, ESP_WIFI_PASS); // Success log +} diff --git a/targets/esp32/mqtts/src/main.c b/targets/esp32/mqtts/src/main.c new file mode 100644 index 0000000..38192b6 --- /dev/null +++ b/targets/esp32/mqtts/src/main.c @@ -0,0 +1,141 @@ +#include +#include +#include +#include +#include "esp_wifi.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include "esp_event.h" +#include "esp_netif.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "freertos/queue.h" + +#include "lwip/sockets.h" +#include "lwip/dns.h" +#include "lwip/netdb.h" + +#include "esp_log.h" +#include "mqtt_client.h" +#include "cnetwork.h" +#include "config.h" + +#define CLIENT_ID "ESP32" + +static const char *TAG = "MQTTS_MAINFLUX"; + +extern const uint8_t client_cert_pem_start[] asm("_binary_client_crt_start"); +extern const uint8_t client_cert_pem_end[] asm("_binary_client_crt_end"); +extern const uint8_t client_key_pem_start[] asm("_binary_client_key_start"); +extern const uint8_t client_key_pem_end[] asm("_binary_client_key_end"); +extern const uint8_t server_cert_pem_start[] asm("_binary_mainflux_crt_start"); +extern const uint8_t server_cert_pem_end[] asm("_binaty_mainflux_crt_end"); + +static void log_error_if_nonzero(const char *message, int error_code) +{ + if (error_code != 0) + { + ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code); + } +} +void create_mainflux_channel(void) +{ + const char *_preId = "channels/"; + const char *_postId = "/messages"; + strcpy(mfTopic, _preId); + strcat(mfTopic, mfChannelId); + strcat(mfTopic, _postId); +} + +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) +{ + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32 "", base, event_id); + esp_mqtt_event_handle_t event = event_data; + esp_mqtt_client_handle_t client = event->client; + int msg_id; + create_mainflux_channel(); + switch ((esp_mqtt_event_id_t)event_id) + { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + msg_id = esp_mqtt_client_publish(client, mfTopic, "'{'message':'hello mainflux'}", 0, 0, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_subscribe(client, mfTopic, 0); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); + break; + case MQTT_EVENT_SUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + msg_id = esp_mqtt_client_publish(client, mfTopic, "'{'message':'hello mainflux'}", 0, 0, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_UNSUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_PUBLISHED: + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_DATA: + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); + printf("DATA=%.*s\r\n", event->data_len, event->data); + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) + { + log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err); + log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err); + log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno); + ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno)); + } + break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; + } +} + +static void mqtt_app_start(void) +{ + esp_mqtt_client_config_t mqtt_cfg = { + .broker.address.uri = server, + .broker.verification.certificate = (const char *)server_cert_pem_start, + .credentials.client_id = CLIENT_ID, + .credentials.username = mfThingId, + .credentials.authentication.password = mfThingPass, + .credentials.authentication.certificate = (const char*)client_cert_pem_start, + .credentials.authentication.key = (const char*) client_key_pem_start, + }; + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); + esp_mqtt_client_start(client); +} + +// main app called by rtos +void app_main(void) +{ + // Defualt start up logs for ESP -IDF + ESP_LOGI(TAG, "[APP] Startup.."); + ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); + // set log level + esp_log_level_set("*", ESP_LOG_INFO); + esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); + esp_log_level_set("esp-tls", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); + esp_log_level_set("outbox", ESP_LOG_VERBOSE); + // Handle error on start up + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + // Run main task + wifi_init_softap(); + mqtt_app_start(); +} diff --git a/targets/esp32/mqtts/src/mainflux.crt b/targets/esp32/mqtts/src/mainflux.crt new file mode 100644 index 0000000..fc596f8 --- /dev/null +++ b/targets/esp32/mqtts/src/mainflux.crt @@ -0,0 +1 @@ +mainflux certificate \ No newline at end of file From df4640439a986f699a2bc3a71eb8f79ff26cbb04 Mon Sep 17 00:00:00 2001 From: 1998-felix Date: Fri, 15 Sep 2023 16:40:20 +0300 Subject: [PATCH 2/4] Cleaning Up Signed-off-by: 1998-felix --- targets/esp32/mqtts/src/cnetwork.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/targets/esp32/mqtts/src/cnetwork.c b/targets/esp32/mqtts/src/cnetwork.c index 0d7198e..78ad6bf 100644 --- a/targets/esp32/mqtts/src/cnetwork.c +++ b/targets/esp32/mqtts/src/cnetwork.c @@ -28,7 +28,7 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base, } } -// Wi-fi initial +// Wi-Fi initial void wifi_init_softap(void) { ESP_ERROR_CHECK(esp_netif_init()); From 11e6e97ac73e014a9784c0bfab79409e67c9da72 Mon Sep 17 00:00:00 2001 From: 1998-felix Date: Fri, 15 Sep 2023 17:13:43 +0300 Subject: [PATCH 3/4] Remove magic numbers Signed-off-by: 1998-felix --- targets/esp32/mqtts/include/config.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/targets/esp32/mqtts/include/config.h b/targets/esp32/mqtts/include/config.h index 3437fce..c60280b 100644 --- a/targets/esp32/mqtts/include/config.h +++ b/targets/esp32/mqtts/include/config.h @@ -1,10 +1,12 @@ #ifndef CONFIG_H #define CONFIG_H +#define TOPIC_BUFFER_SIZE 128 + const char *mfThingId = " "; const char *mfThingPass = " "; const char *mfChannelId = " "; -char mfTopic[150]; +char mfTopic[TOPIC_BUFFER_SIZE]; const char *server = " "; From 6fe38b315b306dc41015fd163cdac03c30758c57 Mon Sep 17 00:00:00 2001 From: 1998-felix Date: Sun, 17 Sep 2023 13:42:05 +0300 Subject: [PATCH 4/4] Clean up comments Signed-off-by: 1998-felix --- targets/esp32/mqtts/src/cnetwork.c | 10 +++++----- targets/esp32/mqtts/src/main.c | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/targets/esp32/mqtts/src/cnetwork.c b/targets/esp32/mqtts/src/cnetwork.c index 78ad6bf..1ba9e0e 100644 --- a/targets/esp32/mqtts/src/cnetwork.c +++ b/targets/esp32/mqtts/src/cnetwork.c @@ -1,5 +1,5 @@ -/***** -Implements a network interface fpr the esp. +/** +Implements a network interface for the esp. Current interface is wifi. **/ @@ -8,9 +8,9 @@ Current interface is wifi. #define ESP_WIFI_SSID " " // Enter the wifi ssid #define ESP_WIFI_PASS " " // Enter the wifi password -static const char *TAG = "wifi softAP"; // tag for esp logging +static const char *TAG = "wifi softAP"; // Tag for esp logging -// Wifi event handler +// Wi-Fi event handler static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { @@ -28,7 +28,7 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base, } } -// Wi-Fi initial +// Wi-Fi init void wifi_init_softap(void) { ESP_ERROR_CHECK(esp_netif_init()); diff --git a/targets/esp32/mqtts/src/main.c b/targets/esp32/mqtts/src/main.c index 38192b6..322d7f5 100644 --- a/targets/esp32/mqtts/src/main.c +++ b/targets/esp32/mqtts/src/main.c @@ -117,14 +117,14 @@ static void mqtt_app_start(void) esp_mqtt_client_start(client); } -// main app called by rtos +// Main app void app_main(void) { - // Defualt start up logs for ESP -IDF + // Default start up logs for ESP-IDF ESP_LOGI(TAG, "[APP] Startup.."); ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size()); ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); - // set log level + // Set log level esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE);