mirror of
https://github.com/espressif/esp-idf.git
synced 2026-05-19 23:45:28 +02:00
AWS IoT Device SDK Support
Use device cert/key embedded in firmware, or loaded from filesystem.
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := aws_iot_subpub
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
# Amazon Web Services IoT MQTT Subscribe/Publish Example
|
||||
|
||||
This is an adaptation of the [AWS IoT C SDK](https://github.com/aws/aws-iot-device-sdk-embedded-C) "subscribe_publish" example for ESP-IDF.
|
||||
|
||||
# Configuration
|
||||
|
||||
See the README.md in the parent directory for information about configuring the AWS IoT examples.
|
||||
|
||||
# Monitoring MQTT Data from the device
|
||||
|
||||
After flashing the example to your ESP32, it should connect to Amazon and start subscribing/publishing MQTT data.
|
||||
|
||||
The example code publishes MQTT data to the topic `test_topic/esp32`. Amazon provides a web interface to subscribe to MQTT topics for testing:
|
||||
|
||||
* On the AWS IoT console, click "MQTT Client" near the top-right.
|
||||
* Click "Generate Client ID" to generate a random client ID.
|
||||
* Click "Connect"
|
||||
|
||||
One connection succeeds, you can subscribe to the data published by the ESP32:
|
||||
|
||||
* Click "Subscribe to Topic"
|
||||
* Enter "Subscription Topic" `test_topic/esp32`
|
||||
* Click "Subscribe"
|
||||
|
||||
... you should see MQTT data published from the running example.
|
||||
|
||||
To publish data back to the device:
|
||||
|
||||
* Click "Publish to Topic"
|
||||
* Enter "Publish Topic" `test_topic/esp32`
|
||||
* Enter a message in the payload field
|
||||
* Click Publish
|
||||
@@ -0,0 +1,60 @@
|
||||
menu "Example Configuration"
|
||||
|
||||
config WIFI_SSID
|
||||
string "WiFi SSID"
|
||||
default "myssid"
|
||||
help
|
||||
SSID (network name) for the example to connect to.
|
||||
|
||||
config WIFI_PASSWORD
|
||||
string "WiFi Password"
|
||||
default "myssid"
|
||||
help
|
||||
WiFi password (WPA or WPA2) for the example to use.
|
||||
|
||||
Can be left blank if the network has no security set.
|
||||
|
||||
config AWS_EXAMPLE_CLIENT_ID
|
||||
string "AWS IoT Client ID"
|
||||
default "myesp32"
|
||||
help
|
||||
AWS IoT Client ID for the example. Should be unique for every device.
|
||||
|
||||
choice EXAMPLE_CERT_SOURCE
|
||||
prompt "AWS IoT Certificate Source"
|
||||
default EXAMPLE_EMBEDDED_CERTS
|
||||
help
|
||||
AWS IoT requires loading of a device-specific certificate and private key,
|
||||
and a common Root CA Certificate. These can be compiled into the example
|
||||
app, or they can be loaded via the filesystem from an SD card.
|
||||
|
||||
config EXAMPLE_EMBEDDED_CERTS
|
||||
bool "Embed into app"
|
||||
config EXAMPLE_SDCARD_CERTS
|
||||
bool "Load from SD card"
|
||||
select EXAMPLE_FILESYSTEM_CERTS
|
||||
endchoice
|
||||
|
||||
# Currently this is equivalent to EXAMPLE_SDCARD_CERTS,
|
||||
# however eventually we want to support more filesystem
|
||||
# sources (SPIFFS, etc.) So this hidden config item
|
||||
# is selected whenever the item should load from filesystem.
|
||||
config EXAMPLE_FILESYSTEM_CERTS
|
||||
bool
|
||||
|
||||
config EXAMPLE_CERTIFICATE_PATH
|
||||
string "Device Certificate Path"
|
||||
depends on EXAMPLE_FILESYSTEM_CERTS
|
||||
default "/sdcard/certificate.pem.crt"
|
||||
|
||||
config EXAMPLE_PRIVATE_KEY_PATH
|
||||
string "Device Private Key Path"
|
||||
depends on EXAMPLE_FILESYSTEM_CERTS
|
||||
default "/sdcard/private.pem.key"
|
||||
|
||||
config EXAMPLE_ROOT_CA_PATH
|
||||
string "Root CA Certificate Path"
|
||||
depends on EXAMPLE_FILESYSTEM_CERTS
|
||||
default "/sdcard/aws-root-ca.pem"
|
||||
|
||||
endmenu
|
||||
@@ -0,0 +1,3 @@
|
||||
Copy certificate files for AWS IoT SDK example here
|
||||
|
||||
See README.md in main example directory for details.
|
||||
@@ -0,0 +1,28 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIE0zCCA7ugAwIBAgIQGNrRniZ96LtKIVjNzGs7SjANBgkqhkiG9w0BAQUFADCB
|
||||
yjELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQL
|
||||
ExZWZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTowOAYDVQQLEzEoYykgMjAwNiBWZXJp
|
||||
U2lnbiwgSW5jLiAtIEZvciBhdXRob3JpemVkIHVzZSBvbmx5MUUwQwYDVQQDEzxW
|
||||
ZXJpU2lnbiBDbGFzcyAzIFB1YmxpYyBQcmltYXJ5IENlcnRpZmljYXRpb24gQXV0
|
||||
aG9yaXR5IC0gRzUwHhcNMDYxMTA4MDAwMDAwWhcNMzYwNzE2MjM1OTU5WjCByjEL
|
||||
MAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQLExZW
|
||||
ZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTowOAYDVQQLEzEoYykgMjAwNiBWZXJpU2ln
|
||||
biwgSW5jLiAtIEZvciBhdXRob3JpemVkIHVzZSBvbmx5MUUwQwYDVQQDEzxWZXJp
|
||||
U2lnbiBDbGFzcyAzIFB1YmxpYyBQcmltYXJ5IENlcnRpZmljYXRpb24gQXV0aG9y
|
||||
aXR5IC0gRzUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCvJAgIKXo1
|
||||
nmAMqudLO07cfLw8RRy7K+D+KQL5VwijZIUVJ/XxrcgxiV0i6CqqpkKzj/i5Vbex
|
||||
t0uz/o9+B1fs70PbZmIVYc9gDaTY3vjgw2IIPVQT60nKWVSFJuUrjxuf6/WhkcIz
|
||||
SdhDY2pSS9KP6HBRTdGJaXvHcPaz3BJ023tdS1bTlr8Vd6Gw9KIl8q8ckmcY5fQG
|
||||
BO+QueQA5N06tRn/Arr0PO7gi+s3i+z016zy9vA9r911kTMZHRxAy3QkGSGT2RT+
|
||||
rCpSx4/VBEnkjWNHiDxpg8v+R70rfk/Fla4OndTRQ8Bnc+MUCH7lP59zuDMKz10/
|
||||
NIeWiu5T6CUVAgMBAAGjgbIwga8wDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8E
|
||||
BAMCAQYwbQYIKwYBBQUHAQwEYTBfoV2gWzBZMFcwVRYJaW1hZ2UvZ2lmMCEwHzAH
|
||||
BgUrDgMCGgQUj+XTGoasjY5rw8+AatRIGCx7GS4wJRYjaHR0cDovL2xvZ28udmVy
|
||||
aXNpZ24uY29tL3ZzbG9nby5naWYwHQYDVR0OBBYEFH/TZafC3ey78DAJ80M5+gKv
|
||||
MzEzMA0GCSqGSIb3DQEBBQUAA4IBAQCTJEowX2LP2BqYLz3q3JktvXf2pXkiOOzE
|
||||
p6B4Eq1iDkVwZMXnl2YtmAl+X6/WzChl8gGqCBpH3vn5fJJaCGkgDdk+bW48DW7Y
|
||||
5gaRQBi5+MHt39tBquCWIMnNZBU4gcmU7qKEKQsTb47bDN0lAtukixlE0kF6BWlK
|
||||
WE9gyn6CagsCqiUXObXbf+eEZSqVir2G3l6BFoMtEMze/aiCKm0oHw0LxOXnGiYZ
|
||||
4fQRbxC1lfznQgUy286dUV4otp6F01vvpX1FQHKOtw5rDgb7MzVIcbidJ4vEZV8N
|
||||
hnacRHr2lVz2XTIIM6RUthg/aFzyQkqFOFSDX9HoLPKsEdao7WNq
|
||||
-----END CERTIFICATE-----
|
||||
@@ -0,0 +1,22 @@
|
||||
#
|
||||
# Main Makefile. This is basically the same as a component makefile.
|
||||
#
|
||||
|
||||
ifdef CONFIG_EXAMPLE_EMBEDDED_CERTS
|
||||
# Certificate files. certificate.pem.crt & private.pem.key must be downloaded
|
||||
# from AWS, see README for details.
|
||||
COMPONENT_EMBED_TXTFILES := certs/aws-root-ca.pem certs/certificate.pem.crt certs/private.pem.key
|
||||
|
||||
ifndef IDF_CI_BUILD
|
||||
# Print an error if the certificate/key files are missing
|
||||
$(COMPONENT_PATH)/certs/certificate.pem.crt $(COMPONENT_PATH)/certs/private.pem.key:
|
||||
@echo "Missing PEM file $@. This file identifies the ESP32 to AWS for the example, see README for details."
|
||||
exit 1
|
||||
else # IDF_CI_BUILD
|
||||
# this case is for the internal Continuous Integration build which
|
||||
# compiles all examples. Add some dummy certs so the example can
|
||||
# compile (even though it won't work)
|
||||
$(COMPONENT_PATH)/certs/certificate.pem.crt $(COMPONENT_PATH)/certs/private.pem.key:
|
||||
echo "Dummy certificate data for continuous integration" > $@
|
||||
endif
|
||||
endif
|
||||
@@ -0,0 +1,326 @@
|
||||
/*
|
||||
* Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* Additions Copyright 2016 Espressif Systems (Shanghai) PTE LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License").
|
||||
* You may not use this file except in compliance with the License.
|
||||
* A copy of the License is located at
|
||||
*
|
||||
* http://aws.amazon.com/apache2.0
|
||||
*
|
||||
* or in the "license" file accompanying this file. This file is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
||||
* express or implied. See the License for the specific language governing
|
||||
* permissions and limitations under the License.
|
||||
*/
|
||||
/**
|
||||
* @file subscribe_publish_sample.c
|
||||
* @brief simple MQTT publish and subscribe on the same topic
|
||||
*
|
||||
* This example takes the parameters from the build configuration and establishes a connection to the AWS IoT MQTT Platform.
|
||||
* It subscribes and publishes to the same topic - "test_topic/esp32"
|
||||
*
|
||||
* Some setup is required. See example README for details.
|
||||
*
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <unistd.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event_loop.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_vfs_fat.h"
|
||||
#include "driver/sdmmc_host.h"
|
||||
|
||||
#include "aws_iot_config.h"
|
||||
#include "aws_iot_log.h"
|
||||
#include "aws_iot_version.h"
|
||||
#include "aws_iot_mqtt_client_interface.h"
|
||||
|
||||
static const char *TAG = "subpub";
|
||||
|
||||
/* The examples use simple WiFi configuration that you can set via
|
||||
'make menuconfig'.
|
||||
|
||||
If you'd rather not, just change the below entries to strings with
|
||||
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
|
||||
*/
|
||||
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
|
||||
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
|
||||
|
||||
/* FreeRTOS event group to signal when we are connected & ready to make a request */
|
||||
static EventGroupHandle_t wifi_event_group;
|
||||
|
||||
/* The event group allows multiple bits for each event,
|
||||
but we only care about one event - are we connected
|
||||
to the AP with an IP? */
|
||||
const int CONNECTED_BIT = BIT0;
|
||||
|
||||
|
||||
/* CA Root certificate, device ("Thing") certificate and device
|
||||
* ("Thing") key.
|
||||
|
||||
Example can be configured one of two ways:
|
||||
|
||||
"Embedded Certs" are loaded from files in "certs/" and embedded into the app binary.
|
||||
|
||||
"Filesystem Certs" are loaded from the filesystem (SD card, etc.)
|
||||
|
||||
See example README for more details.
|
||||
*/
|
||||
#if defined(CONFIG_EXAMPLE_EMBEDDED_CERTS)
|
||||
|
||||
extern const uint8_t aws_root_ca_pem_start[] asm("_binary_aws_root_ca_pem_start");
|
||||
extern const uint8_t aws_root_ca_pem_end[] asm("_binary_aws_root_ca_pem_end");
|
||||
extern const uint8_t certificate_pem_crt_start[] asm("_binary_certificate_pem_crt_start");
|
||||
extern const uint8_t certificate_pem_crt_end[] asm("_binary_certificate_pem_crt_end");
|
||||
extern const uint8_t private_pem_key_start[] asm("_binary_private_pem_key_start");
|
||||
extern const uint8_t private_pem_key_end[] asm("_binary_private_pem_key_end");
|
||||
|
||||
#elif defined(CONFIG_EXAMPLE_FILESYSTEM_CERTS)
|
||||
|
||||
static const char * DEVICE_CERTIFICATE_PATH = CONFIG_EXAMPLE_CERTIFICATE_PATH;
|
||||
static const char * DEVICE_PRIVATE_KEY_PATH = CONFIG_EXAMPLE_PRIVATE_KEY_PATH;
|
||||
static const char * ROOT_CA_PATH = CONFIG_EXAMPLE_ROOT_CA_PATH;
|
||||
|
||||
#else
|
||||
#error "Invalid method for loading certs"
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Default MQTT HOST URL is pulled from the aws_iot_config.h
|
||||
*/
|
||||
char HostAddress[255] = AWS_IOT_MQTT_HOST;
|
||||
|
||||
/**
|
||||
* @brief Default MQTT port is pulled from the aws_iot_config.h
|
||||
*/
|
||||
uint32_t port = AWS_IOT_MQTT_PORT;
|
||||
|
||||
|
||||
static esp_err_t event_handler(void *ctx, system_event_t *event)
|
||||
{
|
||||
switch(event->event_id) {
|
||||
case SYSTEM_EVENT_STA_START:
|
||||
esp_wifi_connect();
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||
/* This is a workaround as ESP32 WiFi libs don't currently
|
||||
auto-reassociate. */
|
||||
esp_wifi_connect();
|
||||
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void iot_subscribe_callback_handler(AWS_IoT_Client *pClient, char *topicName, uint16_t topicNameLen,
|
||||
IoT_Publish_Message_Params *params, void *pData) {
|
||||
ESP_LOGI(TAG, "Subscribe callback");
|
||||
ESP_LOGI(TAG, "%.*s\t%.*s", topicNameLen, topicName, (int) params->payloadLen, (char *)params->payload);
|
||||
}
|
||||
|
||||
void disconnectCallbackHandler(AWS_IoT_Client *pClient, void *data) {
|
||||
ESP_LOGW(TAG, "MQTT Disconnect");
|
||||
IoT_Error_t rc = FAILURE;
|
||||
|
||||
if(NULL == pClient) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(aws_iot_is_autoreconnect_enabled(pClient)) {
|
||||
ESP_LOGI(TAG, "Auto Reconnect is enabled, Reconnecting attempt will start now");
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Auto Reconnect not enabled. Starting manual reconnect...");
|
||||
rc = aws_iot_mqtt_attempt_reconnect(pClient);
|
||||
if(NETWORK_RECONNECTED == rc) {
|
||||
ESP_LOGW(TAG, "Manual Reconnect Successful");
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Manual Reconnect Failed - %d", rc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void aws_iot_task(void *param) {
|
||||
char cPayload[100];
|
||||
|
||||
int32_t i = 0;
|
||||
|
||||
IoT_Error_t rc = FAILURE;
|
||||
|
||||
AWS_IoT_Client client;
|
||||
IoT_Client_Init_Params mqttInitParams = iotClientInitParamsDefault;
|
||||
IoT_Client_Connect_Params connectParams = iotClientConnectParamsDefault;
|
||||
|
||||
IoT_Publish_Message_Params paramsQOS0;
|
||||
IoT_Publish_Message_Params paramsQOS1;
|
||||
|
||||
ESP_LOGI(TAG, "AWS IoT SDK Version %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG);
|
||||
|
||||
mqttInitParams.enableAutoReconnect = false; // We enable this later below
|
||||
mqttInitParams.pHostURL = HostAddress;
|
||||
mqttInitParams.port = port;
|
||||
|
||||
#if defined(CONFIG_EXAMPLE_EMBEDDED_CERTS)
|
||||
mqttInitParams.pRootCALocation = (const char *)aws_root_ca_pem_start;
|
||||
mqttInitParams.pDeviceCertLocation = (const char *)certificate_pem_crt_start;
|
||||
mqttInitParams.pDevicePrivateKeyLocation = (const char *)private_pem_key_start;
|
||||
|
||||
#elif defined(CONFIG_EXAMPLE_FILESYSTEM_CERTS)
|
||||
mqttInitParams.pRootCALocation = ROOT_CA_PATH;
|
||||
mqttInitParams.pDeviceCertLocation = DEVICE_CERTIFICATE_PATH;
|
||||
mqttInitParams.pDevicePrivateKeyLocation = DEVICE_PRIVATE_KEY_PATH;
|
||||
#endif
|
||||
|
||||
mqttInitParams.mqttCommandTimeout_ms = 20000;
|
||||
mqttInitParams.tlsHandshakeTimeout_ms = 5000;
|
||||
mqttInitParams.isSSLHostnameVerify = true;
|
||||
mqttInitParams.disconnectHandler = disconnectCallbackHandler;
|
||||
mqttInitParams.disconnectHandlerData = NULL;
|
||||
|
||||
#ifdef CONFIG_EXAMPLE_SDCARD_CERTS
|
||||
ESP_LOGI(TAG, "Mounting SD card...");
|
||||
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
|
||||
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
|
||||
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
|
||||
.format_if_mount_failed = false,
|
||||
.max_files = 3,
|
||||
};
|
||||
sdmmc_card_t* card;
|
||||
esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to mount SD card VFAT filesystem.");
|
||||
abort();
|
||||
}
|
||||
#endif
|
||||
|
||||
rc = aws_iot_mqtt_init(&client, &mqttInitParams);
|
||||
if(SUCCESS != rc) {
|
||||
ESP_LOGE(TAG, "aws_iot_mqtt_init returned error : %d ", rc);
|
||||
abort();
|
||||
}
|
||||
|
||||
/* Wait for WiFI to show as connected */
|
||||
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
|
||||
false, true, portMAX_DELAY);
|
||||
|
||||
connectParams.keepAliveIntervalInSec = 10;
|
||||
connectParams.isCleanSession = true;
|
||||
connectParams.MQTTVersion = MQTT_3_1_1;
|
||||
/* Client ID is set in the menuconfig of the example */
|
||||
connectParams.pClientID = CONFIG_AWS_EXAMPLE_CLIENT_ID;
|
||||
connectParams.clientIDLen = (uint16_t) strlen(CONFIG_AWS_EXAMPLE_CLIENT_ID);
|
||||
connectParams.isWillMsgPresent = false;
|
||||
|
||||
ESP_LOGI(TAG, "Connecting to AWS...");
|
||||
do {
|
||||
rc = aws_iot_mqtt_connect(&client, &connectParams);
|
||||
if(SUCCESS != rc) {
|
||||
ESP_LOGE(TAG, "Error(%d) connecting to %s:%d", rc, mqttInitParams.pHostURL, mqttInitParams.port);
|
||||
vTaskDelay(1000 / portTICK_RATE_MS);
|
||||
}
|
||||
} while(SUCCESS != rc);
|
||||
|
||||
/*
|
||||
* Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h
|
||||
* #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL
|
||||
* #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL
|
||||
*/
|
||||
rc = aws_iot_mqtt_autoreconnect_set_status(&client, true);
|
||||
if(SUCCESS != rc) {
|
||||
ESP_LOGE(TAG, "Unable to set Auto Reconnect to true - %d", rc);
|
||||
abort();
|
||||
}
|
||||
|
||||
const char *TOPIC = "test_topic/esp32";
|
||||
const int TOPIC_LEN = strlen(TOPIC);
|
||||
|
||||
ESP_LOGI(TAG, "Subscribing...");
|
||||
rc = aws_iot_mqtt_subscribe(&client, TOPIC, TOPIC_LEN, QOS0, iot_subscribe_callback_handler, NULL);
|
||||
if(SUCCESS != rc) {
|
||||
ESP_LOGE(TAG, "Error subscribing : %d ", rc);
|
||||
abort();
|
||||
}
|
||||
|
||||
sprintf(cPayload, "%s : %d ", "hello from SDK", i);
|
||||
|
||||
paramsQOS0.qos = QOS0;
|
||||
paramsQOS0.payload = (void *) cPayload;
|
||||
paramsQOS0.isRetained = 0;
|
||||
|
||||
paramsQOS1.qos = QOS1;
|
||||
paramsQOS1.payload = (void *) cPayload;
|
||||
paramsQOS1.isRetained = 0;
|
||||
|
||||
while((NETWORK_ATTEMPTING_RECONNECT == rc || NETWORK_RECONNECTED == rc || SUCCESS == rc)) {
|
||||
|
||||
//Max time the yield function will wait for read messages
|
||||
rc = aws_iot_mqtt_yield(&client, 100);
|
||||
if(NETWORK_ATTEMPTING_RECONNECT == rc) {
|
||||
// If the client is attempting to reconnect we will skip the rest of the loop.
|
||||
continue;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "-->sleep");
|
||||
vTaskDelay(1000 / portTICK_RATE_MS);
|
||||
sprintf(cPayload, "%s : %d ", "hello from ESP32 (QOS0)", i++);
|
||||
paramsQOS0.payloadLen = strlen(cPayload);
|
||||
rc = aws_iot_mqtt_publish(&client, TOPIC, TOPIC_LEN, ¶msQOS0);
|
||||
|
||||
sprintf(cPayload, "%s : %d ", "hello from ESP32 (QOS1)", i++);
|
||||
paramsQOS1.payloadLen = strlen(cPayload);
|
||||
rc = aws_iot_mqtt_publish(&client, TOPIC, TOPIC_LEN, ¶msQOS1);
|
||||
if (rc == MQTT_REQUEST_TIMEOUT_ERROR) {
|
||||
ESP_LOGW(TAG, "QOS1 publish ack not received.");
|
||||
rc = SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGE(TAG, "An error occurred in the main loop.");
|
||||
abort();
|
||||
}
|
||||
|
||||
static void initialise_wifi(void)
|
||||
{
|
||||
tcpip_adapter_init();
|
||||
wifi_event_group = xEventGroupCreate();
|
||||
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
|
||||
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {
|
||||
.ssid = EXAMPLE_WIFI_SSID,
|
||||
.password = EXAMPLE_WIFI_PASS,
|
||||
},
|
||||
};
|
||||
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
|
||||
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
|
||||
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
|
||||
ESP_ERROR_CHECK( esp_wifi_start() );
|
||||
}
|
||||
|
||||
|
||||
void app_main()
|
||||
{
|
||||
initialise_wifi();
|
||||
#ifdef CONFIG_MBEDTLS_DEBUG
|
||||
const size_t stack_size = 36*1024;
|
||||
#else
|
||||
const size_t stack_size = 36*1024;
|
||||
#endif
|
||||
xTaskCreatePinnedToCore(&aws_iot_task, "aws_iot_task", stack_size, NULL, 5, NULL, 1);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
# Enable AWS IoT SDK support
|
||||
CONFIG_AWS_IOT_SDK=y
|
||||
|
||||
# Enable FATFS read only with long filename support
|
||||
# for loading Cert/CA/etc from filesystem
|
||||
# (if enabled in config)
|
||||
CONFIG_FATFS_READONLY=y
|
||||
CONFIG_FATFS_CODEPAGE_437=y
|
||||
CONFIG_FATFS_LFN_HEAP=y
|
||||
Reference in New Issue
Block a user