AWS IOT core and ESP8266 unable to connect

0

I have tried to connect aws iot core thing with ESP8266 but it is not working

code for esp:
code:#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <time.h>
#include "secrets.h"
#include "DHT.h"
#define TIME_ZONE -5
 
#define DHTPIN 4        // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11   // DHT 11
 
DHT dht(DHTPIN, DHTTYPE);
 
float h ;
float t;
unsigned long lastMillis = 0;
unsigned long previousMillis = 0;
const long interval = 5000;
                                 
#define AWS_IOT_PUBLISH_TOPIC   "ESP1/pub"
#define AWS_IOT_SUBSCRIBE_TOPIC "ESP1/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");
 
  while (!client.connect(THINGNAME))
  {
    Serial.print(THINGNAME);
    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();
  dht.begin();
}
 
 
void loop()
{
  h = dht.readHumidity();
  t = dht.readTemperature();
 
  // if (isnan(h) || isnan(t) )  // Check if any reads failed and exit early (to try again).
  // {
  //   Serial.println(F("Failed to read from DHT sensor!"));
  //   return;
  // }
 
  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
  {
    client.loop();
    if (millis() - lastMillis > 5000)
    {
      lastMillis = millis();
      publishMessage();
    }
  }
}

secrets are in other file and i have configured properly.

Policies:{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "iot:Connect", "Resource": "arn:aws:iot:ap-south-1:779522177923:client/ESP1" }, { "Effect": "Allow", "Action": "iot:Publish", "Resource": "arn:aws:iot:ap-south-1:779522177923:topic/ESP1/pub" }, { "Effect": "Allow", "Action": "iot:Subscribe", "Resource": "arn:aws:iot:ap-south-1:779522177923:topicfilter/ESP1/sub" }, { "Effect": "Allow", "Action": "iot:Receive", "Resource": "arn:aws:iot:ap-south-1:779522177923:topic/ESP1/sub" } ] }Enter image description here

akhil
질문됨 3달 전92회 조회
2개 답변
0

Hi - What is the error you are getting?

profile pictureAWS
전문가
답변함 3달 전
0

Per Nitin's question, it would be useful to know what output you're seeing from your firmware. For now, I'll presume it's stuck at "Connecting to AWS IOT".

Your policy and cloud configuration look OK. Nonetheless, I recommend you run the openssl command described here: https://docs.aws.amazon.com/iot/latest/developerguide/diagnosing-connectivity-issues.html#troubleshooting-authentication.

I also recommend you use another MQTT client, such as mosquitto_pub, like this:

mosquitto_pub -h YOUR_ENDPOINT -p 8883 -t ESP1/pub -m "hello" -i ESP1 --cert cert.pem --key key.pem --cafile CA.pem -d

If both of those commands work, then we know your cloud configuration (and your local network) is not preventing connection. So then it must be an issue with your device code.

Is THING_NAMEset to ESP1? Is MQTT_HOST set to your ATS endpoint and is cacert set to AmazonRootCA1? Is your policy attached to the certificate?

profile pictureAWS
전문가
Greg_B
답변함 3달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인