当在Arduino IDE中使用ESP32连接到AWS IoT时,mbedtls_net_connect返回-0x52
我尝试使用Arduino IDE帮助下我的ESP32板中的基本pubsub示例连接到AWS IoT。当在Arduino IDE中使用ESP32连接到AWS IoT时,mbedtls_net_connect返回-0x52
作为一个基本的例子它连接到AWS的IoT和发布消息,但是当我给一个静态IP到程序它连接到与所指定的IP地址的WiFi(我还分配一个静态IP到MAC在我的路由器板)的地址,但它没有公布消息,并给了我以下错误:
试图连接到SSID:RCB岩石!!!! 连接到wifi
E(37583)aws_iot:失败! mbedtls_net_connect返回-0x52
E(37583)AWS_IOT:错误(-23)连接到*********** iot.eu-west-2.amazonaws.com:8883,。
正尝试重新连接
我使用下面的代码:
#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++;
}
我发现这个AWS物联网SDK的Arduino的ESP32 here,我跟着我this website中给出的故障。
您可能想尝试查看ESP板是否可以使用该静态IP连接到互联网。你可以尝试运行这个草图。 https://learn.sparkfun.com/tutorials/esp32-thing-hookup-guide#arduino-example-wifi
我在我的主板上遇到了同样的问题,如果我通过chrome连接到无线网络,则会获得DNS_PROBE_FINISHED_NO_INTERNET。更改为其他网络可解决问题。
Attempting to connect to SSID: RCB Rocks!!!! Connected to wifi
所以你的主板能够获得网络连接。
E (37583) aws_iot: failed! mbedtls_net_connect returned -0x52
此错误
NET - Failed to get an IP address for the given hostname
无论是主机名是错误的或有什么问题您的DNS设置。假设您的程序在不使用静态IP地址的情况下运行,则问题必须与主板上的DNS设置有关。当主板从DHCP接收到动态IP地址时,DHCP服务器也会向其发送DNS设置。如果您使用静态IP地址而不是DHCP,则还需要静态设置DNS服务器。