-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add: Add Arduino code for AWS IoT integration * refactor: Refactor Arduino code structure and file organization * add: Add .gitignore file to exclude AWS data and ArduinoCode/secrets.h * add: readme Guide for arduino code * refactor: Update readme * refactor: Update AWS IoT credentials in ArduinoCode/secrets.h * refactor: readme
- Loading branch information
1 parent
654913a
commit 0eec569
Showing
7 changed files
with
306 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
AWS data | ||
ArduinoCode/secrets.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#include "DHT.h" | ||
#include "secrets.h" | ||
#include <WiFiClientSecure.h> | ||
#include <PubSubClient.h> | ||
#include <ArduinoJson.h> | ||
#include "WiFi.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 = 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 ")); | ||
|
||
publishMessage(); | ||
client.loop(); | ||
delay(1000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# ESP32 DHT11 Sensor with AWS IoT Core: Documentation and Setup Guide | ||
|
||
This code provides a basic structure for connecting an ESP32 development board with a DHT11 sensor to AWS IoT Core for publishing temperature and humidity readings. | ||
|
||
## Components Used | ||
|
||
* ESP32 Development Board | ||
* DHT11 Sensor | ||
* Breadboard and Jumper Wires | ||
* WiFi Connection (SSID and Password) | ||
* AWS Account (Free Tier Available) | ||
|
||
## Software Required | ||
|
||
* Arduino IDE ([https://support.arduino.cc/hc/en-us/articles/360019833020-Download-and-install-Arduino-IDE](https://support.arduino.cc/hc/en-us/articles/360019833020-Download-and-install-Arduino-IDE)) | ||
* ArduinoJson Library ([https://github.com/bblanchon/ArduinoJson](https://github.com/bblanchon/ArduinoJson)) | ||
* WiFiClientSecure Library (included in Arduino IDE) | ||
* PubSubClient Library ([https://github.com/knolleary/pubsubclient/blob/master/src/PubSubClient.h](https://github.com/knolleary/pubsubclient/blob/master/src/PubSubClient.h)) | ||
|
||
## AWS IoT Core Setup | ||
|
||
1. Create an AWS Account if you don't have one already. | ||
2. Go to the AWS IoT Core service ([https://aws.amazon.com/iot/](https://aws.amazon.com/iot/)). | ||
3. Create a "Thing" which represents your ESP32 device. | ||
4. Download the certificate and private key files associated with the thing. | ||
5. Note down the AWS IoT endpoint address. | ||
|
||
## Code Explanation | ||
|
||
### Libraries | ||
|
||
* `DHT.h`: Provides functions to interact with the DHT sensor. | ||
* `secrets.h` (**Replace with your actual file**): Stores sensitive information like WiFi credentials and AWS certificates (placeholders included in the provided code). | ||
* `WiFiClientSecure.h`: Enables secure communication with AWS IoT Core using certificates. | ||
* `PubSubClient.h`: Manages communication with the MQTT broker on AWS IoT Core. | ||
* `ArduinoJson.h`: Enables working with JSON data format for sending sensor readings. | ||
* `WiFi.h`: Provides WiFi connection functionalities. | ||
|
||
### Constants | ||
|
||
* `DHTPIN`: Defines the pin connected to the DHT sensor (change if using a different pin). | ||
* `DHTTYPE`: Specifies the DHT sensor type (DHT11 in this case). | ||
* `AWS_IOT_PUBLISH_TOPIC`: Topic used to publish sensor data to AWS IoT Core. | ||
* `AWS_IOT_SUBSCRIBE_TOPIC` (**Optional**): Topic for subscribing to messages from AWS (not used in this example). | ||
|
||
### Global Variables | ||
|
||
* `h`: Stores humidity reading. | ||
* `t`: Stores temperature reading. | ||
* `dht`: Instance of the DHT library for interacting with the sensor. | ||
* `net`: Instance of WiFiClientSecure for secure communication. | ||
* `client`: Instance of PubSubClient for MQTT communication. | ||
|
||
### Functions | ||
|
||
* `connectAWS()`: Connects to the WiFi network and then to the AWS IoT Core endpoint using the provided credentials. | ||
* `publishMessage()`: Creates a JSON document with sensor readings and publishes it to the defined topic on AWS IoT Core. | ||
* `messageHandler()`: (**Optional**) Handles incoming messages on the subscribed topic (not used in this example). | ||
* `setup()`: Initializes serial communication, connects to AWS, and starts the DHT sensor. | ||
* `loop()`: Reads sensor data, checks for validity, publishes data to AWS, and loops with a delay. | ||
|
||
### Setup Guide | ||
|
||
1. Install the required libraries in the Arduino IDE. Library managers can be found under **Tools > Manage Libraries**. | ||
2. In `secrets.h`, define the following constants replacing placeholders with your actual values: | ||
* `#define WIFI_SSID "your_wifi_ssid"` | ||
* `#define WIFI_PASSWORD "your_wifi_password"` | ||
* `#define THINGNAME "your_thing_name"` (replace with the name you created in AWS IoT Core) | ||
* Update the certificate definition strings (`AWS_CERT_CA`, `AWS_CERT_CRT`, and `AWS_CERT_PRIVATE`) by replacing the placeholder content with your downloaded certificates and private key. You can use online tools to convert the downloaded PEM files to a format suitable for the code (check online resources for specific methods). | ||
3. Connect the DHT11 sensor to the ESP32 board according to the pin definition (`DHTPIN`) in the code. | ||
4. Connect the ESP32 board to your computer using a USB cable. | ||
5. In the Arduino IDE, select the appropriate board type for your ESP32 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <pgmspace.h> | ||
|
||
#define SECRET | ||
#define THINGNAME "ESP32_DHT11" //change this | ||
|
||
const char WIFI_SSID[] = ""; //change this | ||
const char WIFI_PASSWORD[] = ""; //change this | ||
const char AWS_IOT_ENDPOINT[] = ""; //change this | ||
|
||
// Amazon Root CA 1 | ||
static const char AWS_CERT_CA[] PROGMEM = R"EOF( | ||
-----BEGIN CERTIFICATE----- | ||
|
||
-----END CERTIFICATE----- | ||
)EOF"; | ||
// Device Certificate //change this | ||
static const char AWS_CERT_CRT[] PROGMEM = R"KEY( | ||
-----BEGIN CERTIFICATE----- | ||
|
||
-----END CERTIFICATE----- | ||
|
||
|
||
)KEY"; | ||
// Device Private Key //change this | ||
static const char AWS_CERT_PRIVATE[] PROGMEM = R"KEY( | ||
-----BEGIN RSA PRIVATE KEY----- | ||
|
||
-----END RSA PRIVATE KEY----- | ||
|
||
|
||
|
||
)KEY"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters