Closed
Description
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"