ESP8266 keeps disconnecting with AWS IOT - Getting CLIENT_ERROR

0

Hi,

My ESP8266 keeps disconnecting from AWS IOT. It connects fine but it immediately disconnects. I have added Arduino code and logs as below. My board publishes Temperature and Humidity values to AWS IOT.

My Payload is not that big, i tried sending hello world but still it gives me CLIENT_ERROR in disconnect reason. Also i am sending connection request only once.

Below is the subscription data i receive in AWS Console for IOT Enter image description here

Logs from Serial port - Arduino

Enter image description here

Arduino Code:

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <time.h>
#include "security.h"



float h=12 ;
float t=22;
unsigned long lastMillis = 0;
unsigned long previousMillis = 0;
const long interval = 5000;

#define AWS_IOT_PUBLISH_TOPIC "esp8266/pub"
#define AWS_IOT_SUBSCRIBE_TOPIC "esp8266/sub"

WiFiClientSecure net;

BearSSL::X509List cert(cacert);
BearSSL::X509List client_crt(client_cert);
BearSSL::PrivateKey key(privkey);

PubSubClient client(net);

time_t now;
time_t nowish = 1510592825;


void NTPConnect(void)
{
Serial.print("Setting time using SNTP");
configTime(TIME_ZONE * 3600, 0 * 3600, "pool.ntp.org", "time.nist.gov");
now = time(nullptr);
while (now < nowish)
{
delay(500);
Serial.print(".");
now = time(nullptr);
}
Serial.println("done!");
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
Serial.print(asctime(&timeinfo));
}


void messageReceived(char *topic, byte *payload, unsigned int length)
{
Serial.print("Received [");
Serial.print(topic);
Serial.print("]: ");
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
}


void connectAWS()
{
delay(3000);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

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

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

NTPConnect();

net.setTrustAnchors(&cert);
net.setClientRSACert(&client_crt, &key);

client.setServer(MQTT_HOST, 8883);
client.setCallback(messageReceived);


Serial.println("Connecting to AWS IOT using thing name ");
Serial.println(THINGNAME);

while (!client.connect(THINGNAME))
{
Serial.print(".");
delay(1000);
}

if (!client.connected()) {
Serial.println("AWS IoT Timeout!");
return;
}
// Subscribe to a topic
client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC);

Serial.println("AWS IoT Connected!");
}


void publishMessage()
{
StaticJsonDocument<200> doc;
doc["time"] = millis();
doc["humidity"] = h;
doc["temperature"] = t;
char jsonBuffer[512];
serializeJson(doc, jsonBuffer); // print to client

client.publish(AWS_IOT_PUBLISH_TOPIC, jsonBuffer);
}


void setup()
{
Serial.begin(115200);
connectAWS();

}


void loop()
{



Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.println(F("°C "));
delay(2000);

now = time(nullptr);

if (!client.connected())
{
connectAWS();
}
else
{
Serial.print("in else loop");
client.loop();
Serial.print("after client loop");
if (millis() - lastMillis > 5000)
{
Serial.print("in if loop");
lastMillis = millis();
publishMessage();
}
}
}
gefragt vor 2 Jahren987 Aufrufe
3 Antworten
0

Hi. I think you may have a problem with your IoT policy. Please review it. Please post it here if you're unsure.

https://docs.aws.amazon.com/iot/latest/developerguide/connect-and-pub.html

profile pictureAWS
EXPERTE
Greg_B
beantwortet vor 2 Jahren
0

Thanks a lot. After having a closer look at my Policy, i figured out that the topic name in policy was incorrect. After correcting it is working fine.

For reference below is the Policy

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "iot:Connect",
      "Resource": "arn:aws:iot:ap-south-1:1234567890:client/${iot:Connection.Thing.ThingName}"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Subscribe",
      "Resource": "arn:aws:iot:ap-south-1:1234567890:topicfilter/esp8266/sub"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Receive",
      "Resource": "arn:aws:iot:ap-south-1:1234567890:topic/esp8266/sub"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Publish",
      "Resource": "arn:aws:iot:ap-south-1:1234567890:topic/esp8266/pub"
    }
  ]
}
beantwortet vor 2 Jahren
0

I am getting this

I am using the same code but cannot get any readings.

See my console Enter image description here

Please help me.

Sidd
beantwortet vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen