Hi guys, i couldn't manage to connect my ESP8266 to my AWS IOT
I cannot understand what i have done wrong, the secrets.h file has copy paste the code from TLS aws cerificates. So i'm sure that something in my code is wrong.
It connects to the wifi network, finds the local time, but when its time to establish a connection with the aws broker, first PubSubClient errorcode -1 appears and then -2
enercost_client_tutorial.ino (3.1 KB)
Before you ask, let me mention: In AWS policies have all required permissions attached to the thing's certificate.
#include <WiFiClientSecure.h>
#include "secrets.h"
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <time.h>
// The MQTT topics that this device should publish/subscribe
#define AWS_IOT_PUBLISH_TOPIC "buildings/C-1"
#define AWS_IOT_SUBSCRIBE_TOPIC "buildings/upd/B-1"
BearSSL::X509List ca_cert(AWS_CERT_CA);
BearSSL::X509List client_crt(AWS_CERT_CRT);
BearSSL::PrivateKey client_key(AWS_CERT_PRIVATE);
WiFiClientSecure net;
PubSubClient pubSubClient(AWS_IOT_ENDPOINT, 8883 , net);
void connectAWS()
{
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.println("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
// Create a message handler
// pubSubClient.onMessage(messageHandler);
Serial.print("Connecting to AWS IOT");
while (!pubSubClient.connect(THINGNAME)) {
Serial.print(".");
delay(100);
}
if(!pubSubClient.connected()){
Serial.println("AWS IoT Timeout!");
return;
}
// Subscribe to a topic
pubSubClient.subscribe(AWS_IOT_SUBSCRIBE_TOPIC);
Serial.println("AWS IoT Connected!");
}
}
void publishMessage()
{
StaticJsonDocument<200> doc;
doc["building_id"] = "B-1";
doc["device_id"] = "B-1";
doc["device_loc"] = "Athens";
doc["device_type"] = "Mother_sensor";
doc["watt_value"] = 4.2;
doc["watt_price_value"] = 0.20;
doc["cng_value"] = 1.4;
doc["cng_price_value"] = 0.16;
doc["water_value"] = 10;
doc["water_price_value"] = 0.001;
char jsonBuffer[512];
serializeJson(doc, jsonBuffer); // print to client
pubSubClient.publish(AWS_IOT_PUBLISH_TOPIC, jsonBuffer);
}
void setCurrentTime() {
configTime(2 * 3600, 0, "pool.ntp.org", "time.nist.gov"); //UTC
Serial.print("Waiting for NTP time sync: ");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
delay(500);
Serial.print(".");
now = time(nullptr);
}
Serial.println("");
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: "); Serial.print(asctime(&timeinfo));
}
void setup() {
Serial.begin(115200);
Serial.println("client mother device");
Serial.print("Connecting to "); Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
WiFi.waitForConnectResult(2000);
Serial.print(", WiFi connected, IP address: "); Serial.println(WiFi.localIP());
setCurrentTime();//for certificates not to be shown as expired
net.setClientRSACert(&client_crt,&client_key);
net.setTrustAnchors(&ca_cert);
}
void loop() {
pubSubCheckConnect();
publishMessage();
delay(1000);
}
void pubSubCheckConnect() {
if ( ! pubSubClient.connected()) {
Serial.print("Enercost Client connecting to: "); Serial.print(AWS_IOT_ENDPOINT);
while ( ! pubSubClient.connected()) {
Serial.print(".");
Serial.println(pubSubClient.state());
pubSubClient.connect("enercost_dev_b1");
}
Serial.println(" connected");
pubSubClient.subscribe(AWS_IOT_SUBSCRIBE_TOPIC);
}
pubSubClient.loop();
}