Menyiapkan Program di Arduino
Contents
Buat sketch baru, isi dengan kode berikut, dan sesuaikan semua bagian mulai dari DHTPIN, DHTTYPE (jika perlu), dan setting lain yang diperlukan.
#include <WiFiClientSecure.h> #include <PubSubClient.h> #include <ArduinoJson.h> #include "WiFi.h" #include "DHT.h" #include <pgmspace.h> #define DHTPIN 4 // nomor digital pin #define DHTTYPE DHT22 // tipe sensor #define AWS_IOT_PUBLISH_TOPIC "esp32/pub" #define AWS_IOT_SUBSCRIBE_TOPIC "esp32/sub" #define SECRET #define THINGNAME "ESP32" // Thing name di AWS. Sesuaikan jika berbeda const char WIFI_SSID[] = ""; // isi dengan WiFi SSID const char WIFI_PASSWORD[] = ""; // WiFi password const char AWS_IOT_ENDPOINT[] = ""; // AWS Endpoint di AWS IoT, di bagian Settings // 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"; 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); }
Paste semua isi dari file certificate yg sudah didownload, sesuai dengan tempatnya di kode program tersebut.
Setelah semua bagian yang perlu dilakukan penyesuaian kita sesuaikan, lakukan Verify, lalu Upload ke ESP32. Pantau datanya di Serial Monitor, lalu cek di AWS IoT Core pada menu Test->MQTT Test Client. Masukkan ESP32/pub (pastikan sama dengan yang kita gunakan di setting pada Arduino IDE), lalu Subscribe. Jika data sudah tampil, maka dipastikan pengiriman dari perangat IoT ke AWS sudah lancar.