mirror of
https://github.com/0015/ThatProject.git
synced 2026-01-12 01:07:44 +03:00
AWS IoT | How to install Mosquitto Broker on Amazon EC2
This commit is contained in:
89
ESP32_MQTT/0_AWS_Mosquitto_Test/0_AWS_Mosquitto_Test.ino
Normal file
89
ESP32_MQTT/0_AWS_Mosquitto_Test/0_AWS_Mosquitto_Test.ino
Normal file
@@ -0,0 +1,89 @@
|
||||
// Source: https://aws.amazon.com/blogs/compute/building-an-aws-iot-core-device-using-aws-serverless-and-an-esp32/
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
/*
|
||||
AWS IoT | How to install Mosquitto Broker on Amazon EC2
|
||||
Video Tutorial: https://youtu.be/SDrkv2hUzAc
|
||||
Created by Eric N. (ThatProject)
|
||||
*/
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "secrets.h"
|
||||
#include <WiFiClientSecure.h>
|
||||
#include <MQTTClient.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include "WiFi.h"
|
||||
|
||||
// The MQTT topics that this device should publish/subscribe
|
||||
#define AWS_IOT_PUBLISH_TOPIC "esp32/pub"
|
||||
#define AWS_IOT_SUBSCRIBE_TOPIC "esp32/sub"
|
||||
|
||||
WiFiClientSecure net = WiFiClientSecure();
|
||||
MQTTClient client = MQTTClient(256);
|
||||
|
||||
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.begin(AWS_IOT_ENDPOINT, 8883, net);
|
||||
|
||||
// Create a message handler
|
||||
client.onMessage(messageHandler);
|
||||
|
||||
Serial.print("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["time"] = millis();
|
||||
doc["sensor_a0"] = random(1000);
|
||||
char jsonBuffer[512];
|
||||
serializeJson(doc, jsonBuffer); // print to client
|
||||
|
||||
client.publish(AWS_IOT_PUBLISH_TOPIC, jsonBuffer);
|
||||
}
|
||||
|
||||
void messageHandler(String &topic, String &payload) {
|
||||
Serial.println("incoming: " + topic + " - " + payload);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
connectAWS();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
publishMessage();
|
||||
client.loop();
|
||||
delay(1000);
|
||||
}
|
||||
26
ESP32_MQTT/0_AWS_Mosquitto_Test/secrets.h
Normal file
26
ESP32_MQTT/0_AWS_Mosquitto_Test/secrets.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <pgmspace.h>
|
||||
|
||||
#define SECRET
|
||||
#define THINGNAME "MyESP32"
|
||||
|
||||
const char WIFI_SSID[] = "ThatProject";
|
||||
const char WIFI_PASSWORD[] = "California";
|
||||
const char AWS_IOT_ENDPOINT[] = "xxxxxxxxxxxxxx-ats.iot.us-west-1.amazonaws.com";
|
||||
|
||||
// Amazon Root CA 1
|
||||
static const char AWS_CERT_CA[] PROGMEM = R"EOF(
|
||||
-----BEGIN CERTIFICATE-----
|
||||
-----END CERTIFICATE-----
|
||||
)EOF";
|
||||
|
||||
// Device Certificate
|
||||
static const char AWS_CERT_CRT[] PROGMEM = R"KEY(
|
||||
-----BEGIN CERTIFICATE-----
|
||||
-----END CERTIFICATE-----
|
||||
)KEY";
|
||||
|
||||
// Device Private Key
|
||||
static const char AWS_CERT_PRIVATE[] PROGMEM = R"KEY(
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
-----END RSA PRIVATE KEY-----
|
||||
)KEY";
|
||||
Reference in New Issue
Block a user