Skip to content

Instantly share code, notes, and snippets.

@jameshi16
Created September 5, 2019 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameshi16/5846acdec40279028319c680fe8314b5 to your computer and use it in GitHub Desktop.
Save jameshi16/5846acdec40279028319c680fe8314b5 to your computer and use it in GitHub Desktop.
[Mine] ESP32 -> AWS IoT -> DynamoDB via MQTT
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#define LED_PIN 2
#define BUTTON_PIN 0
/* Change the following configuration options */
const char* ssid = "";
const char* password = "";
const char* aws_iot_hostname = "";
const char* aws_iot_sub_topic = "";
const char* aws_iot_pub_topic = "";
const char* aws_iot_pub_message = "{\"led_state\": %d}";
const char* client_id = "MyIoT";
const char* ca_certificate = ""; //CA certificate
const char* iot_certificate = ""; //Own certificate
const char* iot_privatekey = ""; //Own private key
#define SSID_HAS_PASSWORD //comment this line if your SSID does not have a password
/* Global Variables */
WiFiClientSecure client;
PubSubClient mqtt(client);
bool released = true;
bool led_state = LOW;
char buffer[50];
/* Functions */
void sub_callback(const char* topic, byte* payload, unsigned int length) {
Serial.print("Topic: ");
Serial.println(topic);
Serial.print("Message: ");
for (int i = 0; i < length; i++)
Serial.print((char) payload[i]);
Serial.println();
if ((char) payload[0] == '1')
led_state = HIGH;
else if ((char) payload[0] == '0')
led_state = LOW;
digitalWrite(LED_PIN, led_state);
}
void setup() {
//Initializations
Serial.begin(9600);
Serial.print("Attempting WiFi connection on SSID: ");
Serial.print(ssid);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
pinMode(BUTTON_PIN, INPUT);
// WiFi
#ifdef SSID_HAS_PASSWORD
WiFi.begin(ssid, password);
#else
WiFi.begin(ssid);
#endif
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print('.');
}
Serial.print("\nWiFi connection succeeded.\n");
client.setCACert(ca_certificate);
client.setCertificate(iot_certificate);
client.setPrivateKey(iot_privatekey);
// AWS IoT MQTT uses port 8883
mqtt.setServer(aws_iot_hostname, 8883);
mqtt.setCallback(sub_callback);
}
void loop() {
// reconnect on disconnect
while (!mqtt.connected()) {
Serial.print("Now connecting to AWS IoT: ");
if (mqtt.connect(client_id)) {
Serial.println("connected!");
mqtt.subscribe(aws_iot_sub_topic); //subscribe to the topic
} else {
Serial.print("failed with status code ");
Serial.print(mqtt.state());
Serial.println(" trying again in 5 seconds...");
delay(5000);
}
}
// check if button is pressed
if (digitalRead(BUTTON_PIN) == LOW && released) {
released = false;
sprintf(buffer, aws_iot_pub_message, led_state);
Serial.println("Button is pressed. Publishing MQTT message...");
mqtt.publish(aws_iot_pub_topic, buffer);
}
if (!released)
released = digitalRead(BUTTON_PIN);
mqtt.loop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment