AJ-Explains-It-All
Published © GPL3+

Connecting to AWS IoT with ESP32 in an Easy & Unsecure Way

Here in this project I have written the dummy code that can be used to update the device shadow on AWS IoT core using any sensor on ESP32.

BeginnerFull instructions provided2 hours3,405
Connecting to AWS IoT with ESP32 in an Easy & Unsecure Way

Things used in this project

Hardware components

SparkFun ESP32 Thing
SparkFun ESP32 Thing
×1
Rotary Encoder with Push-Button
Rotary Encoder with Push-Button
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Desoldering Set, Tweezer
Desoldering Set, Tweezer

Story

Read more

Schematics

Shabby yet accurate schematic

Connect the Vin for +5V supply for the encoder if you have directly connected the esp32 to the USB port of your PC

Code

CODE

Arduino
The details on how to connect to AWS iot is written in the project description, read that before playing with the code
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>

//connecting to the AWS iot
void connectAWSIoT();
void mqttCallback (char* topic, byte* payload, unsigned int length);

//connecting to the local wifi
char *ssid = "XXXXXXX";
char *password = "XXXXXXXXXXXXXX";

//Assigning the REST API endopoint 
const char *endpoint = "a3gkm8nxkzxavd-ats.iot.ap-south-1.amazonaws.com";


//assigning the AWS communication port *NEVER use port 1883 
const int port = 8883;

//Assigning the topic to where the data is to be published and subscribed
char *pubTopic = "$aws/things/_________/shadow/update";
char *subTopic = "$aws/things/_________/shadow/update/delta";   //$aws/things/coffee12/shadow/update/delta subscribe to this topic with your custom topic name in the blank on your AWS test window


//Uploading the ROOTca, PRIVATE and CERTIFICATE.
const char* rootCA = \
"-----BEGIN CERTIFICATE-----\n"

"-----END CERTIFICATE-----";



const char* certificate = \
"-----BEGIN CERTIFICATE-----\n"

"-----END CERTIFICATE-----";


const char* privateKey =  \
"-----BEGIN RSA PRIVATE KEY-----\n"

"-----END RSA PRIVATE KEY-----";


WiFiClientSecure httpsClient;
PubSubClient mqttClient(httpsClient);

void setup() {
    delay(1000);
    Serial.begin(115200);

    // Start WiFi
    Serial.println("Connecting to ");
    Serial.print(ssid);
    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("\nConnected.");

    // Configuring MQTT Client
    httpsClient.setCACert(rootCA);
    httpsClient.setCertificate(certificate);
    httpsClient.setPrivateKey(privateKey);
    mqttClient.setServer(endpoint, port);
    mqttClient.setCallback(mqttCallback);

    connectAWSIoT();
}

void connectAWSIoT() {
    while (!mqttClient.connected()) {
        if (mqttClient.connect("ESP32_device")) {
            Serial.println("Connected.");
            int qos = 0; //Make sure the qos is zero in the MQTT broker of AWS
            mqttClient.subscribe(subTopic, qos);
            Serial.println("Subscribed.");
        } else {
            Serial.print("Failed. Error state=");
            Serial.print(mqttClient.state());
        
            delay(100); //change the delay here to manipulate the device shadow updation speed
        }
    }
}

long messageSentAt = 0;
int dummyValue = 0;
char pubMessage[128];

void mqttCallback (char* topic, byte* payload, unsigned int length) {
    Serial.print("Received. topic=");
    Serial.println(topic);
    for (int i = 0; i < length; i++) {
        Serial.print((char)payload[i]);
    }
    Serial.print("\n");
}

void mqttLoop() {
    if (!mqttClient.connected()) {
        connectAWSIoT();
    }
    mqttClient.loop();

    long now = millis();
    if (now - messageSentAt > 100) { //change the delay here to manipulate the device shadow updation speed
        messageSentAt = now;
        sprintf(pubMessage, "{\"state\": {\"desired\":{\"DUMMY VALUE\":\"%d\"}}}", dummyValue++);
        Serial.print("Publishing message to topic ");
        Serial.println(pubTopic);
        Serial.println(pubMessage);
        mqttClient.publish(pubTopic, pubMessage);
        Serial.println("Published.");
    }
}

void loop() {
  mqttLoop();
}

Encoder Logic

Arduino
Read the description to understand this code snippets utility
void setup(){
pinMode(encoderPin1, INPUT_PULLUP); 
  pinMode(encoderPin2, INPUT_PULLUP);

  digitalWrite(encoderPin1, HIGH); 
  digitalWrite(encoderPin2, HIGH); 

  attachInterrupt(digitalPinToInterrupt(32), updateEncoder, CHANGE);
  attachInterrupt(digitalPinToInterrupt(33), updateEncoder, CHANGE)
  }

void updateEncoder(){
  int MSB = digitalRead(encoderPin1); 
  int LSB = digitalRead(encoderPin2); 

  int encoded = (MSB << 1) |LSB; 
  int sum  = (lastEncoded << 2) | encoded; 

  if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
  if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;

  lastEncoded = encoded; 
}

Credits

AJ-Explains-It-All

AJ-Explains-It-All

12 projects • 10 followers
Experienced embedded firmware and software developer. I have a great affinity towards ARM cortex based MCU's.

Comments