1
votes

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.

3

3 Answers

1
votes

Attempting to connect to SSID: RCB Rocks!!!! Connected to wifi

So your board is able to get a network connection.

E (37583) aws_iot: failed! mbedtls_net_connect returned -0x52

This error means

NET - Failed to get an IP address for the given hostname

Either the host name is wrong or there's something wrong with your DNS setup. Given that your program works when you don't use a static IP address, the problem must be with the DNS setup on the board. When the board receives a dynamic IP address from DHCP, the DHCP server also sends it DNS settings. If you use a static IP address instead of DHCP, you also need to set up a DNS server statically.

0
votes

You might want to try seeing if the ESP board can even connect to the internet using that static IP. You could try running this sketch. https://learn.sparkfun.com/tutorials/esp32-thing-hookup-guide#arduino-example-wifi

I had the same issue on my board and was getting the DNS_PROBE_FINISHED_NO_INTERNET if I connected to the wireless from chrome. Changing to a different network fixed the issue.

0
votes

I know this is old, but moving Wifi.begin() outside the for loop did the trick for me:

WiFi.config(ip,gateway,subnet);
WiFi.mode(WIFI_STA);

Serial.print("Attempting to connect to SSID: ");
Serial.println(WIFI_SSID);

// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

while (WiFi.status() != WL_CONNECTED) {
  Serial.print(".");
  delay(500);
}

Serial.print("connected.");