- Newest
- Most votes
- Most comments
Here is the reference links I have followed to make it work https://exploreembedded.com/wiki/AWS_IOT_with_Arduino_ESP32 https://exploreembedded.com/wiki/Secure_IOT_with_AWS_and_Hornbill_ESP32
Finally working. I have followed the instructions in the following links to make it work. https://exploreembedded.com/wiki/AWS_IOT_with_Arduino_ESP32 https://exploreembedded.com/wiki/Secure_IOT_with_AWS_and_Hornbill_ESP32
I don't have a DTH11 sensor so I commented it out and tried it and it worked fine.
sketch_may27a.ino
#include "secrets.h"
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include "WiFi.h"
//#include "DHT.h"
//#define DHTPIN 14 // Digital pin connected to the DHT sensor
//#define DHTTYPE DHT11 // DHT 11
#define AWS_IOT_PUBLISH_TOPIC "esp32/pub"
#define AWS_IOT_SUBSCRIBE_TOPIC "esp32/sub"
float h ;
float t;
//DHT dht(DHTPIN, DHTTYPE);
WiFiClientSecure net = WiFiClientSecure();
PubSubClient client(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(".");
}
// Configure WiFiClientSecure to use the AWS IoT device credentials
net.setCACert(AWS_CERT_CA);
net.setCertificate(AWS_CERT_CRT);
net.setPrivateKey(AWS_CERT_PRIVATE);
// 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.println("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["humidity"] = h;
doc["temperature"] = t;
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.println(topic);
StaticJsonDocument<200> doc;
deserializeJson(doc, payload);
const char* message = doc["message"];
Serial.println(message);
}
void setup()
{
Serial.begin(115200);
connectAWS();
// dht.begin();
}
void loop()
{
h = 100;//dht.readHumidity();
t = 200;//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 "));
publishMessage();
client.loop();
delay(1000);
}
Secrets.h
#include <pgmspace.h>
#define SECRET
#define THINGNAME "ESP32_DHT11" //change this
const char WIFI_SSID[] = "xxxxxxxxx"; //change this
const char WIFI_PASSWORD[] = "xxxxxxxxx"; //change this
const char AWS_IOT_ENDPOINT[] = "xxx.iot.ap-northeast-1.amazonaws.com"; //change this
// Amazon Root CA 1
static const char AWS_CERT_CA[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF
...
rqXRfboQnoZsG4q5WTP468SQvvG5
-----END CERTIFICATE-----
)EOF";
// Device Certificate //change this
static const char AWS_CERT_CRT[] PROGMEM = R"KEY(
-----BEGIN CERTIFICATE-----
MIIDWTCCAkGgAwIBAgIUfWcbwQBaVl8RnCfJY4TO4UGscAYwDQYJKoZIhvcNAQEL
...
X/XquhMaID9XyciEeI4ZNevs8PZ+wZIsP95rCOdehNb9dQLV37r/gHJ6clEO
-----END CERTIFICATE-----
)KEY";
// Device Private Key //change this
static const char AWS_CERT_PRIVATE[] PROGMEM = R"KEY(
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAo+HePx9Rc3XVmJjB3DG0idyGSlN+X86LBpbEF/OZUyqKtm1E
...
OrHl2hsHks8F8cD+rE355q6viOZxkQp+egLbM6g6a+3e1bpsUQNw
-----END RSA PRIVATE KEY-----
)KEY";
I would be very happy if you could ACCEPTE ANSWER when you solve the problem😀😀😀
Sure. Thanks for confirming that the code works. I will give it a try tomorrow again.
I have tried your code but I could not make it work. I was getting the same error.
Relevant content
- asked a year ago
- asked 2 years ago
- asked 3 years ago
- asked 3 years ago
- AWS OFFICIALUpdated 4 months ago
- AWS OFFICIALUpdated 10 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 3 years ago
I am glad it works. I just don't know why it didn't.