Skip to content

Instantly share code, notes, and snippets.

@wiomoc wiomoc/sketch.ino
Created Apr 19, 2020

Embed
What would you like to do?
Sample Code using AWS IoT with an ESP32 for alerting
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <DHT.h>
// Retrieve using aws iot describe-endpoint --endpoint-type iot:Data-ATS
#define ENDPOINT "....-ats.iot.---.amazonaws.com"
#define SSID "YOUR_SSID"
#define PSK "YOUR_PASSWORD"
#define DEVICE_NAME "sensor01"
static const char AWS_ROOT_CA[] = "-----BEGIN CERTIFICATE-----\n\
MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF\n\
ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6\n\
b24gUm9vdCBDQSAxMB4XDTE1MDUyNjAwMDAwMFoXDTM4MDExNzAwMDAwMFowOTEL\n\
MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv\n\
b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj\n\
ca9HgFB0fW7Y14h29Jlo91ghYPl0hAEvrAIthtOgQ3pOsqTQNroBvo3bSMgHFzZM\n\
9O6II8c+6zf1tRn4SWiw3te5djgdYZ6k/oI2peVKVuRF4fn9tBb6dNqcmzU5L/qw\n\
IFAGbHrQgLKm+a/sRxmPUDgH3KKHOVj4utWp+UhnMJbulHheb4mjUcAwhmahRWa6\n\
VOujw5H5SNz/0egwLX0tdHA114gk957EWW67c4cX8jJGKLhD+rcdqsq08p8kDi1L\n\
93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm\n\
jgSubJrIqg0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC\n\
AYYwHQYDVR0OBBYEFIQYzIU07LwMlJQuCFmcx7IQTgoIMA0GCSqGSIb3DQEBCwUA\n\
A4IBAQCY8jdaQZChGsV2USggNiMOruYou6r4lK5IpDB/G/wkjUu0yKGX9rbxenDI\n\
U5PMCCjjmCXPI6T53iHTfIUJrU6adTrCC2qJeHZERxhlbI1Bjjt/msv0tadQ1wUs\n\
N+gDS63pYaACbvXy8MWy7Vu33PqUXHeeE6V/Uq2V8viTO96LXFvKWlJbYK8U90vv\n\
o/ufQJVtMVT8QtPHRh8jrdkPSHCa2XV4cdFyQzR1bldZwgJcJmApzyMZFo6IQ6XU\n\
5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy\n\
rqXRfboQnoZsG4q5WTP468SQvvG5\n\
-----END CERTIFICATE-----\n";
static const char CERTIFICATE[] = "-----BEGIN CERTIFICATE-----\n\
.....
-----END CERTIFICATE-----\n";
static const char PRIVATE_KEY[] = "-----BEGIN RSA PRIVATE KEY-----\n\
...
-----END RSA PRIVATE KEY-----\n";
DHT dht(23, DHT22);
WiFiClientSecure net = WiFiClientSecure();
PubSubClient mqtt(ENDPOINT, 8883, net);
void setup() {
Serial.begin(115200);
delay(100);
Serial.print("Attempting to connect to WIFI: ");
WiFi.begin(SSID, PSK);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println(WiFi.localIP());
Serial.println(PRIVATE_KEY);
Serial.println("Connected to WIFI");
net.setCACert(AWS_ROOT_CA);
net.setCertificate(CERTIFICATE);
net.setPrivateKey(PRIVATE_KEY);
while (!mqtt.connected()) {
Serial.print("Attempting MQTT connection...");
if (mqtt.connect(DEVICE_NAME)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(mqtt.state());
delay(5000); // Retry after 5sec
}
}
dht.begin();
}
void loop() {
mqtt.loop();
delay(2000);
float temperature = dht.readTemperature();
Serial.print("Temperature:");
Serial.println(temperature);
static boolean sentAlert = false;
if(temperature > 30) {
if(!sentAlert) { // Send alert only once per temperature exceedance
mqtt.publish(DEVICE_NAME "/status", "{\"status\":\"alert\"}");
sentAlert = true;
}
} else {
sentAlert = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.