I am trying to connect to AWS IoT using a basic pubsub example in my ESP32 board with the help of the Arduino IDE.
As a basic example it does connect to AWS IoT and publishes messages, but when I give a static IP to the program it does connect to the wifi with the specified IP address (I have also assigned a static IP to the MAC address of the board in my router), but it fails to publish the messages and gives me the following error:
Attempting to connect to SSID: RCB Rocks!!!! Connected to wifi
E (37583) aws_iot: failed! mbedtls_net_connect returned -0x52
E (37583) AWS_IOT: Error(-23) connecting to ***********.iot.eu-west-2.amazonaws.com:8883,
Trying to reconnect
I am using the following code:
#include <AWS_IOT.h>
#include <WiFi.h>
AWS_IOT hornbill;
char WIFI_SSID[]="RCB Rocks!!!!";
char WIFI_PASSWORD[]="********";
char HOST_ADDRESS[]="************.iot.eu-west-2.amazonaws.com";
char CLIENT_ID[]= "1008";
char TOPIC_NAME[]= "smk";
IPAddress ip(192, 168, 0, 20);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
int status = WL_IDLE_STATUS;
int tick=0,msgCount=0,msgReceived = 0;
char payload[512];
char rcvdPayload[512];
void mySubCallBackHandler (char *topicName, int payloadLen, char *payLoad) {
strncpy(rcvdPayload,payLoad,payloadLen);
rcvdPayload[payloadLen] = 0;
msgReceived = 1;
}
void setup() {
Serial.begin(115200);
delay(2000);
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(WIFI_SSID);
WiFi.config(ip,gateway,subnet);
WiFi.mode(WIFI_STA);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
// wait 5 seconds for connection:
delay(5000);
}
Serial.println("Connected to wifi");
if(hornbill.connect(HOST_ADDRESS,CLIENT_ID)== 0) {
Serial.println("Connected to AWS");
delay(1000);
if(0==hornbill.subscribe(TOPIC_NAME,mySubCallBackHandler)) {
Serial.println("Subscribe Successfull");
} else {
Serial.println("Subscribe Failed, Check the Thing Name and Certificates");
while(1);
}
} else {
Serial.println("AWS connection failed, Check the HOST Address");
while(1);
}
delay(2000);
}
void loop() {
if(msgReceived == 1) {
msgReceived = 0;
Serial.print("Received Message:");
Serial.println(rcvdPayload);
}
if(tick >= 5) {
// publish to topic every 5seconds
tick=0;
sprintf(payload,"Hello from hornbill ESP32 : %d",msgCount++);
if(hornbill.publish(TOPIC_NAME,payload) == 0) {
Serial.print("Publish Message:");
Serial.println(payload);
} else {
Serial.println("Publish failed");
}
}
vTaskDelay(1000 / portTICK_RATE_MS);
tick++;
}
I have found this AWS IoT SDK for Arduino ESP32 here, and I followed the instructions given in this website.