Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add esp32 mqtts capability #4

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions targets/esp32/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.pio
.vscode
1 change: 1 addition & 0 deletions targets/esp32/mqtts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
9 changes: 9 additions & 0 deletions targets/esp32/mqtts/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 21 additions & 0 deletions targets/esp32/mqtts/include/cnetwork.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef C_NETWORK_H
#define C_NETWORK_H

#include <string.h>
#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
13 changes: 13 additions & 0 deletions targets/esp32/mqtts/include/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef CONFIG_H
#define CONFIG_H

#define TOPIC_BUFFER_SIZE 128

const char *mfThingId = " ";
const char *mfThingPass = " ";
const char *mfChannelId = " ";
char mfTopic[TOPIC_BUFFER_SIZE];

const char *server = " ";

#endif
14 changes: 14 additions & 0 deletions targets/esp32/mqtts/platformio.ini
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions targets/esp32/mqtts/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*)

idf_component_register(SRCS ${app_sources})
1 change: 1 addition & 0 deletions targets/esp32/mqtts/src/client.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Client certificate
1 change: 1 addition & 0 deletions targets/esp32/mqtts/src/client.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
client key
69 changes: 69 additions & 0 deletions targets/esp32/mqtts/src/cnetwork.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
Implements a network interface for 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

// Wi-Fi 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 init
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
}
141 changes: 141 additions & 0 deletions targets/esp32/mqtts/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#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
void app_main(void)
{
// 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
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();
}
1 change: 1 addition & 0 deletions targets/esp32/mqtts/src/mainflux.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mainflux certificate