Getting wifi connected ESP8266 to connect to AWS_IOT

0

I've been following a combination of the following two guides:

  1. This one from AWS which details using IOT with a ESP32
  2. And this one that if specific to an esp8266

But despite following each guide as well as I can, my esp8266 connects to WIFI but never connects to IOT

Here's my sketch

#include "secrets.h"
#include <PubSubClient.h>
#include <WiFiClientSecure.h>
//#include <MQTTClient.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>


// The MQTT topics that this device should publish/subscribe
#define AWS_IOT_PUBLISH_TOPIC   "esp8266/pub"
#define AWS_IOT_SUBSCRIBE_TOPIC "esp8266/sub"

WiFiClientSecure net; 

BearSSL::X509List cert(AWS_CERT_CA);
BearSSL::X509List client_crt(AWS_CERT_CRT);
BearSSL::PrivateKey key(AWS_CERT_PRIVATE);
//MQTTClient client = MQTTClient(256);

PubSubClient client(net);

void connectAWS()
{
  
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  Serial.println("Connecting to Wi-Fi");

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

  // Configure WiFiClientSecure to use the AWS IoT device credentials
  
  
  //net.setCACert(AWS_CERT_CA);
  //net.setCertificate(AWS_CERT_CRT);
  //net.setPrivateKey(AWS_CERT_PRIVATE);
  
  net.setTrustAnchors(&cert);
  net.setClientRSACert(&client_crt, &key);
  
  // Connect to the MQTT broker on the AWS endpoint we defined earlier
  client.setServer(AWS_IOT_ENDPOINT, 8883);

  // Create a message handler
  client.setCallback(messageHandler);

  Serial.print("Connecting to AWS IOT");

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

  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["sensor_a0"] = analogRead(0);
  char jsonBuffer[512];
  serializeJson(doc, jsonBuffer); // print to client

  client.publish(AWS_IOT_PUBLISH_TOPIC, jsonBuffer);
}

void messageHandler(char *topic, byte *payload, unsigned int length) {
  Serial.print("incoming: ");
  Serial.print(topic);
  Serial.print(" - ");
  for (int i = 0; i < length; i++)
  {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  
//  StaticJsonDocument<200> doc;
//  deserializeJson(doc, payload);
//  const char* message = doc["message"];
}

void setup() {
  Serial.begin(9600);
  connectAWS();
}

void loop() {
  publishMessage();
  client.loop();
  delay(1000);
}

Additionally I confirmed that my thingname is correct and that my thing has an active and attached certificate with an attached policy that contains what I believe to be the right information. I've tried all the similar suggestions on repost but none have solved my problem and I am out of ideas of what could be wrong.

Edit: Here is my policy

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "iot:Connect",
      "Resource": "arn:aws:iot:us-east-1:1234567890:client/testESP8266"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Subscribe",
      "Resource": "arn:aws:iot:us-east-1:1234567890:topicfilter/esp826/sub"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Receive",
      "Resource": "arn:aws:iot:us-east-1:1234567890:topic/esp8266/sub"
    },
    {
      "Effect": "Allow",
      "Action": "iot:Publish",
      "Resource": "arn:aws:iot:us-east-1:1234567890:topic/esp8266/pub"
    }
  ]
}
KWC
asked 9 months ago313 views
1 Answer
0

Hi. I presume you mean the connection attempt keeps terminating with "AWS IoT Timeout!"?

There's not so much that can go wrong. It's all about your certificates, the private key, the endpoint, the thing and client name, and the IoT policy. So I would suggest you review all of those again, accepting that it's very likely that there's a mistake in at least one of them. It could be useful to share your policy here, redacting the account ID.

Following the steps here could also be useful: https://docs.aws.amazon.com/iot/latest/developerguide/diagnosing-connectivity-issues.html

And/or trying another client like mosquitto_pub (to validate your certificates, endpoint and policy):

mosquitto_pub -h AWS_IOT_ENDPOINT -p 8883 -t AWS_IOT_PUBLISH_TOPIC -m "hello" -i THINGNAME --cert AWS_CERT_CRT --key AWS_CERT_PRIVATE --cafile AWS_CERT_CA -d
profile pictureAWS
EXPERT
Greg_B
answered 9 months ago
  • I’ll make sure to check that all again and I’ll post the policy, however I never recieve the timeout message, despite the program running for over 20 mins it just keeps printing “.”

  • Just edited to include the policy

  • So is THINGNAME in your sketch testESP8266? You do have an error in your IoT policy for the Subscribe resource, but since you don't yet get as far as subscribing, it should not yet be an issue. I really encourage you to follow the troubleshooting link I shared, and use another client like I described. This will remove your device code from the equation and confirm or deny whether your cloud configuration and credentials are correct.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions