Skip to content

Cant stablish mqtt connection in aws #10184

Closed
@ioviedodev

Description

@ioviedodev

Board

ESP32 DEV KIT C

Device Description

Just esp32 dev kit c and wifi connection

Hardware Configuration

no connections

Version

v3.0.4

IDE Name

ECLIPSE

Operating System

Windows 10

Flash frequency

80 MH

PSRAM enabled

yes

Upload speed

115200

Description

Using arduino-esp32 as IDF Component, i cant stablish mqtt connection the program is very simple i think that is something that i need to enable in sdkconfig.

Sketch

//file: main.cpp
#ifndef ARDUINO_H
#include <Arduino.h>
#endif

#include "WiFi.h"
#include <WiFiClientSecure.h>
#include <MQTTClient.h>
#include <ArduinoJson.h>
//C8:F0:9E:69:43:F4

#define WIFI_SSID   "SSIDEXAMPLE"
#define WIFI_PASSWORD   "passwordExample"
#define THINGNAME   "44566873-21312132132132"
#define AWS_IOT_PUBLISH_TOPIC   "44566873-21312132132132"
#define AWS_IOT_SUBSCRIBE_TOPIC "44566873-21312132132132/room"
const char AWS_IOT_ENDPOINT[] = "23232323asd-ats.iot.us-east-1.amazonaws.com";

static const char AWS_CERT_CA[] = R"EOF(
-----BEGIN CERTIFICATE-----
MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF
ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6
-----END CERTIFICATE-----
)EOF";

static const char AWS_DEV_CERT_CA[] = R"EOF(
-----BEGIN CERTIFICATE-----
MIIDWTCCAkGgAwIBAgIUAbWwZS0QcK1zSv9XqapM9dgIxpswDQYJKoZIhvcNAQEL
BQAwTTFLMEkGA1UECwxCQW1hem9uIFdlYiBTZXJ2aWNlcyBPPUFtYXpvbi5jb20g
-----END CERTIFICATE-----
)EOF";

static const char AWS_DEV_KEY_CA[] = R"EOF(
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAwl2fC9N0JhV1mgdjuLvipMC8lSZvz/HWmA7l8Zq2Cl+aPE9v
JwdKdgw1Vkvfpn0oFYtrZg4sSX15SgHrj0K6uuhpKeAEbmmnmjJlofwcEEmCQCjR
ePZbL5JNQToDfB4ujSF4/s1aQrPjn2DcyomGbSueZlPRSrIjNTihk9pGfMVEMidC
nps424lb==
-----END RSA PRIVATE KEY-----
)EOF";

WiFiClientSecure net = WiFiClientSecure();
MQTTClient client = MQTTClient(512);
void messageHandler(String &topic, String &payload);

char* create_device_info_json();
void connectAWS();

void connectAWS()
{
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  Serial.println("Connecting to Wi-Fi");

  while (WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }
  WiFi.setSleep(false);
  Serial.print("SEÑAL: ");
  Serial.println(WiFi.RSSI());

  // Configure WiFiClientSecure to use the AWS IoT device credentials
  net.setCACert(AWS_CERT_CA);
  net.setCertificate(AWS_DEV_CERT_CA);
  net.setPrivateKey(AWS_DEV_KEY_CA);
  net.setTimeout(20000);
  // Connect to the MQTT broker on the AWS endpoint we defined earlier
  client.begin(AWS_IOT_ENDPOINT, 8883, net);

  // Create a message handler
  client.onMessage(messageHandler);

  Serial.print("Connecting to AWS IOT");

  while (!client.connect(THINGNAME)) {
    Serial.print(".");
    delay(100);
  }

  if(!client.connected()){
    Serial.println("AWS IoT Timeout!");
    return;
  }

  // Subscribe to a topic
  client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC);

  Serial.println("AWS IoT Connected!");
}

char* create_device_info_json() {
    static char json_buffer[256]; // Buffer to hold the JSON string
    uint8_t mac[6]; // Array to hold the MAC address

    // Read the MAC address of the station interface
    WiFi.macAddress(mac);
    // Get the system uptime in milliseconds
    unsigned long millis = (unsigned long) (esp_timer_get_time() / 1000);

    // Get the free heap size
    uint32_t free_heap = esp_get_free_heap_size();

    // Format the MAC address and other information into a JSON string
    snprintf(json_buffer, sizeof(json_buffer),
             "{\"designator\": \"P_TEST\", \"device_mac\": \"%02X:%02X:%02X:%02X:%02X:%02X\", \"millis\": %lu, \"free_heap\": %i, \"RSSI\": %d}",
             mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], millis, (int)free_heap, WiFi.RSSI());

    Serial.println(json_buffer);
    return json_buffer;
}

void publishMessage(){
  client.publish(AWS_IOT_PUBLISH_TOPIC, create_device_info_json());
}

void messageHandler(String &topic, String &payload) {
  Serial.println("incoming: " + topic + " - " + payload);
}

void setup() {
  Serial.begin(115200);
  pinMode(27, OUTPUT);
  digitalWrite(27, HIGH);
  connectAWS();
}

void loop() {
  if(!client.loop()){
    Serial.println("LOST MQTT");
    while (!client.connect(THINGNAME)) {
      Serial.print(".");
      delay(100);
    }
    // Subscribe to a topic
    client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC);
  }
  else{
    publishMessage();
  }
  delay(5000);
}

Debug Message

←[0;33m--- esp-idf-monitor 1.4.0 on \\.\COM4 115200 ---←[0m
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 153911750, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:7112
load:0x40078000,len:15644
ho 0 tail 12 room 4
load:0x40080400,len:4
0x40080400: _init at ??:?

load:0x40080404,len:3876
entry 0x4008064c
I (32) boot: ESP-IDF v5.1.4 2nd stage bootloader
I (32) boot: compile time Aug 16 2024 13:06:18
I (32) boot: Multicore bootloader
I (36) boot: chip revision: v3.0
I (40) boot.esp32: SPI Speed      : 80MHz
I (45) boot.esp32: SPI Mode       : DIO
I (49) boot.esp32: SPI Flash Size : 16MB
I (54) boot: Enabling RNG early entropy source...
I (59) boot: Partition Table:
I (63) boot: ## Label            Usage          Type ST Offset   Length
I (70) boot:  0 nvs              WiFi data        01 02 0000a000 00003000
I (77) boot:  1 nvscerts         WiFi data        01 02 0000d000 00020000
I (85) boot:  2 otadata          OTA data         01 00 0002d000 00002000
I (92) boot:  3 phy_init         RF data          01 01 0002f000 00001000
I (100) boot:  4 app1             OTA app          00 11 00030000 00400000
I (107) boot:  5 app0             OTA app          00 10 00430000 00400000
I (115) boot:  6 factory          factory app      00 00 00830000 00400000
I (122) boot:  7 spiffs           Unknown data     01 82 00c30000 003d0000
I (130) boot: End of partition table
I (134) boot: Defaulting to factory image
I (139) esp_image: segment 0: paddr=00830020 vaddr=3f400020 size=39408h (234504) map
I (182) esp_image: segment 1: paddr=00869430 vaddr=3ffb0000 size=03cc0h ( 15552) load
I (185) esp_image: segment 2: paddr=0086d0f8 vaddr=40080000 size=02f20h ( 12064) load
I (190) esp_image: segment 3: paddr=00870020 vaddr=400d0020 size=c2d18h (797976) map
I (313) esp_image: segment 4: paddr=00932d40 vaddr=40082f20 size=155ech ( 87532) load
I (343) boot: Loaded app from partition at offset 0x830000
I (343) boot: Disabling RNG early entropy source...
I (354) cpu_start: Multicore app
V (355) mmap: after coalescing, 3 regions are left
I (355) quad_psram: This chip is ESP32-D0WDR2-V3
I (358) esp_psram: Found 2MB PSRAM device
I (362) esp_psram: Speed: 40MHz
I (366) esp_psram: PSRAM initialized, cache is in low/high (2-core) mode.
V (373) mmap: found laddr is 0x1800000
V (377) esp_psram: 8bit-aligned-range: 0x200000 B, starting from: 0x3f800000
I (384) cpu_start: Pro cpu up.
I (388) cpu_start: Starting app cpu, entry point is 0x400815a8
0x400815a8: call_start_cpu1 at C:/Users/IOFamilia/espidf514/components/esp_system/port/cpu_start.c:159

I (0) cpu_start: App cpu up.
I (856) esp_psram: SPI SRAM memory test OK
D (864) clk: RTC_SLOW_CLK calibration value: 3571648
I (869) cpu_start: Pro cpu start user code
I (869) cpu_start: cpu freq: 240000000 Hz
I (869) cpu_start: Application information:
I (874) cpu_start: Project name:     scan
I (879) cpu_start: App version:      1
I (883) cpu_start: Compile time:     Aug 16 2024 13:10:48
I (889) cpu_start: ELF file SHA256:  cf4ea01fd65e65c3...
Warning: checksum mismatch between flashed and built applications. Checksum of built application is 0840d0164f0df6460a2476b4d037460061d9bbb7f6cc017c97fc474fd3823288
I (895) cpu_start: ESP-IDF:          v5.1.4
I (900) cpu_start: Min chip rev:     v3.0
I (905) cpu_start: Max chip rev:     v3.99 
I (910) cpu_start: Chip rev:         v3.0

[ 21644][E][NetworkClientSecure.cpp:159] connect(): start_ssl_client: connect failed: -1
.[ 21752][E][NetworkClient.cpp:315] setSocketOption(): fail on 0, errno: 9, "Bad file number"
[ 40262][E][ssl_client.cpp:146] start_ssl_client(): socket error on fd 54, errno: 113, "Software caused connection abort"
[ 40273][E][NetworkClientSecure.cpp:159] connect(): start_ssl_client: connect failed: -1
.[ 40380][E][NetworkClient.cpp:315] setSocketOption(): fail on 0, errno: 9, "Bad file number"
[ 58890][E][ssl_client.cpp:146] start_ssl_client(): socket error on fd 54, errno: 113, "Software caused connection abort"
[ 58901][E][NetworkClientSecure.cpp:159] connect(): start_ssl_client: connect failed: -1
.[ 59008][E][NetworkClient.cpp:315] setSocketOption(): fail on 0, errno: 9, "Bad file number"
D (60751) wifi:eb is dhcp or dns sport = 15736, dport = 53
[ 77794][E][ssl_client.cpp:146] start_ssl_client(): socket error on fd 54, errno: 113, "Software caused connection abort"
[ 77805][E][NetworkClientSecure.cpp:159] connect(): start_ssl_client: connect failed: -1
.[ 77912][E][NetworkClient.cpp:315] setSocketOption(): fail on 0, errno: 9, "Bad file number"
[ 96422][E][ssl_client.cpp:146] start_ssl_client(): socket error on fd 54, errno: 113, "Software caused connection abort"
[ 96434][E][NetworkClientSecure.cpp:159] connect(): start_ssl_client: connect failed: -1
.[ 96541][E][NetworkClient.cpp:315] setSocketOption(): fail on 0, errno: 9, "Bad file number"
[115050][E][ssl_client.cpp:146] start_ssl_client(): socket error on fd 54, errno: 113, "Software caused connection abort"
[115062][E][NetworkClientSecure.cpp:159] connect(): start_ssl_client: connect failed: -1
.[115169][E][NetworkClient.cpp:315] setSocketOption(): fail on 0, errno: 9, "Bad file number"
[133678][E][ssl_client.cpp:146] start_ssl_client(): socket error on fd 54, errno: 113, "Software caused connection abort"
[133690][E][NetworkClientSecure.cpp:159] connect(): start_ssl_client: connect failed: -1
.[133797][E][NetworkClient.cpp:315] setSocketOption(): fail on 0, errno: 9, "Bad file number"
D (135540) wifi:eb is dhcp or dns sport = 31478, dport = 53
D (136540) wifi:eb is dhcp or dns sport = 31478, dport = 53
[153571][E][ssl_client.cpp:146] start_ssl_client(): socket error on fd 54, errno: 113, "Software caused connection abort"
[153582][E][NetworkClientSecure.cpp:159] connect(): start_ssl_client: connect failed: -1
.[153690][E][NetworkClient.cpp:315] setSocketOption(): fail on 0, errno: 9, "Bad file number"
[172199][E][ssl_client.cpp:146] start_ssl_client(): socket error on fd 54, errno: 113, "Software caused connection abort"
[172211][E][NetworkClientSecure.cpp:159] connect(): start_ssl_client: connect failed: -1
.[172318][E][NetworkClient.cpp:315] setSocketOption(): fail on 0, errno: 9, "Bad file number"

Other Steps to Reproduce

.

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions