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

Issue with Simultaneous Connections to AWS IoT Core and Arduino Cloud #516

Closed
djom202 opened this issue Nov 17, 2024 · 8 comments
Closed
Labels
conclusion: invalid Issue/PR not valid topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project

Comments

@djom202
Copy link

djom202 commented Nov 17, 2024

I’m currently using the MQTT library to connect my ESP32 to AWS IoT Core for sending information. However, I’m having trouble keeping the connection open to both AWS IoT Core and Arduino Cloud simultaneously, as the connections seem to overlap at some point.

My question is: Is it possible to maintain both connections at the same time? Are there any strategies or libraries that could help me resolve this issue?

My code:

https://gist.github.com/djom202/d1660f4c9c5b9fa038b753dc6057c7bf

ESP32_AWS_oct24a.ino

#include "arduino_secrets.h"
#include "thingProperties.h"
#include <ArduinoJson.h>
#include <WiFiClientSecure.h>
#include <MQTT.h>
#include "AwsManager.h"

#define LEVEL_LOGS 2

WiFiClientSecure wifiClient = WiFiClientSecure();
MQTTClient mqttClient = MQTTClient(256);

void setup() {
  Serial.begin(19200);
  delay(1500);

  initProperties();
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(LEVEL_LOGS);
  ArduinoCloud.printDebugInfo();

  isDeviceConnected = false;
  isAwsConnected = false;
}

void loop() {
  ArduinoCloud.update();
  mqttClient.loop();

  if (ArduinoCloud.connected()) {
    Serial.print("IP: ");
    Serial.println(WiFi.localIP());

    setCretifications(wifiClient);
    setupAws(mqttClient, wifiClient);
  }
}

AwsManager.ino

#include "AwsManager.h"
#include <ESPping.h>
#include <MQTTClient.h>

// Topic MQTT
const char AWS_IOT_PUBLISH_TOPIC[] = "esp32/test";
const char AWS_IOT_SUBSCRIBE_TOPIC[] = "esp32/test";

// AWS MQTT Info
const char AWS_ENDPOINT[] = "NAME.iot.us-east-1.amazonaws.com";
const int AWS_PORT = 8883;

static const char AWS_ROOT_CA[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
)EOF";

static const char AWS_CLIENT_CRT[] PROGMEM = R"KEY(
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
)KEY";

static const char AWS_PRIVATE_KEY[] PROGMEM = R"KEY(
-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----
)KEY";

void setupAws(MQTTClient &client, WiFiClientSecure &net) {
  Serial.println("Setting up the server for AWS");

  setCretifications(net);
  client.begin(AWS_ENDPOINT, AWS_PORT, wifiClient);
  client.onMessage(messageReceived);

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

  while (!client.connect(getSerial())) {
    isAwsConnected = false;
    Serial.print("Error al conectar a AWS IoT Core: ");
    Serial.println(client.returnCode());
    Serial.println(client.lastError());
    delay(10000);
  }
  Serial.println();

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

  client.subscribe(AWS_IOT_PUBLISH_TOPIC);
  Serial.println("AWS IoT Connected!");
  isAwsConnected = true;
}

void setCretifications(WiFiClientSecure &net) {
  net.setCACert(AWS_ROOT_CA);
  net.setCertificate(AWS_CLIENT_CRT);
  net.setPrivateKey(AWS_PRIVATE_KEY);
}

char* getStateConnection(int state) {
  if (state == -4){
    return "Timeout";
  } else if (state == -3){
    return "ConnectionLost";
  } else if (state == -2){
    return "ConnectionFailed";
  } else if (state == -1){
    return "Disconnected";
  } else if (state == 0){
    return "Connected";
  } else if (state == 1){
    return "BadProtocol";
  } else if (state == 2){
    return "BadClientId";
  } else if (state == 3){
    return "Unavailable";
  } else if (state == 4){
    return "BadCredentials";
  } else if (state == 5){
    return "Unauthorized";
  }
}

void messageReceived(String &topic, String &payload) {
  Serial.println("Message received!");
  Serial.println("Topic: " + topic);
  Serial.println("Payload: " + payload);

  if (topic == INCOMMING_ATTACK_SUBSCRIBE_TOPIC) {
    Serial.println("Will be attacked");
  } else if (topic == CHANGE_TURN_SUBSCRIBE_TOPIC) {
    Serial.println("Now it your turn or the opponent");
  } else if (topic == LIFE_POINTS_SUBSCRIBE_TOPIC) {
    Serial.println("new life points to show");
  }
}

const char* getSerial() {
  String clientId = "ESP32Client-";
  clientId += DEVICE_LOGIN_NAME;
  return clientId.c_str();
}

void subscribeTopic(MQTTClient &client) {
  if (!client.connected()) return;

  if (client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC)) {
    Serial.println("Successful subscription.");
  } else {
    Serial.println("Subscription error.");
  }
}

void publishMessage(MQTTClient &client, String msg) {
  static unsigned long lastMsg = 0;
  unsigned long now = millis();

  if (now - lastMsg > 5000) {
    lastMsg = now;
    Serial.print("Publishing message: ");
    Serial.println(msg);
    client.publish(AWS_IOT_PUBLISH_TOPIC, msg.c_str());
  }
}

AwsManager.h

#ifndef AwsManager_h
#define AwsManager_h

void setupAws(MQTTClient &client, WiFiClientSecure &net);
void subscribeTopic(MQTTClient &client);
void setCretifications(WiFiClientSecure &net);
void publishMessage(MQTTClient &client, String payload);
void messageHandler(String &topic, String &payload);
const char* getSerial();

#endif

Below are the logs showing the problem:

17:08:37.686 -> [846532][E][NetworkClient.cpp:319] setSocketOption(): fail on 0, errno: 9, "Bad file number"
17:08:37.719 -> [846582][E][NetworkClient.cpp:319] setSocketOption(): fail on 0, errno: 9, "Bad file number"
17:08:37.913 -> [846774][E][ssl_client.cpp:213] start_ssl_client(): useRootCABundle is set, but attach_ssl_certificate_bundle(ssl, true); was not called!
17:08:38.526 -> [847390][E][ssl_client.cpp:36] _handle_error(): [ssl_starttls_handshake():313]: (-30336) SSL - No CA Chain is set, but required to operate
17:08:38.590 -> [847463][E][NetworkClientSecure.cpp:159] connect(): start_ssl_client: connect failed: -30336
17:08:38.654 -> ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8884 Error: -2
17:08:38.686 -> [847514][E][NetworkClient.cpp:319] setSocketOption(): fail on 0, errno: 9, "Bad file number"
17:08:38.750 -> [847614][E][NetworkClient.cpp:319] setSocketOption(): fail on 0, errno: 9, "Bad file number"
@pennam
Copy link
Collaborator

pennam commented Nov 18, 2024

@djom202 i've never tried with an ESP32 but it should be totally feasible. With the opta we use ArduinoIoTCloud and ArduinoBearSSL but it should word also with ESP32 WiFiClientSecure. Maybe a full connection log can give you more information about what is failing.

@djom202
Copy link
Author

djom202 commented Nov 20, 2024

Hi @pennam,

I'm sharing with you the logs, I just removed the repetitive information over the connection status in order to reduce the size file. Also I've to test it without ArduinoCloud and it's works by hours with not issues.

@pennam
Copy link
Collaborator

pennam commented Nov 21, 2024

@djom202 I see from the logs you are using a 3.x.x version of the ESP32 core. > 3.x.x have introduced a lot of changes and we did not have the chance to test them extensively with ArduinoIoTCloud.

Would you test the same sketch with core 2.0.17 ?

https://github.com/espressif/arduino-esp32/releases/tag/2.0.17

Thanks

@djom202
Copy link
Author

djom202 commented Nov 22, 2024

Sure. Let me check it.

@djom202
Copy link
Author

djom202 commented Nov 24, 2024

Hi @pennam,

Yeah you are right, I tested by hours with the version 2.0.17 and its works without issues.

17:55:50.019 -> Wifi CONNECTED
17:55:50.019 -> RRSI: -85
17:55:50.051 -> AWS: CONNECTED
17:55:50.051 -> Successful subscription.

So, what I’m asking is if a newer version within the 2.X.X range could work for this case?

@pennam
Copy link
Collaborator

pennam commented Nov 25, 2024

@djom202 goot news.

So, what I’m asking is if a newer version within the 2.X.X range could work for this case?

sorry but i did not get your last question. A newer version of what?

@djom202
Copy link
Author

djom202 commented Nov 25, 2024

@pennam, sorry for the confusion. I might tell you that which others versions for the 2.X.X of ESP32 core could works also this case?

@pennam
Copy link
Collaborator

pennam commented Nov 26, 2024

2.0.17 should be the latests 2.x.x release of the esp core and is the one we have installed in our cloud builder.

@djom202 djom202 closed this as completed Nov 29, 2024
@per1234 per1234 added type: imperfection Perceived defect in any part of project conclusion: invalid Issue/PR not valid topic: code Related to content of the project itself labels Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conclusion: invalid Issue/PR not valid topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

3 participants