Cette page fournit une interface de navigation pour trouver toutes les valeurs d’une propriété et une page donnée. D’autres interfaces de recherche disponibles incluent la recherche de propriété, et le constructeur de requête ask.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect",
"iot:Publish",
"iot:Receive",
"iot:Subscribe"
],
"Resource": [
"*"
]
}
]
}
#include "SPIFFS.h"
#include
#include
// Enter your Device Configuration
const char* ssid = " "; // Provide your SSID
const char* password = " "; // Provide Password
const char* mqtt_server = " "; // Replace with your MQTT END point
String thingname = " ";
String shadowname = " ";
const int mqtt_port = 8883;
String publish_topic = "$aws/things/"+thingname+"/shadow/name/"+shadowname+"/update";
String subscribe_topic = "$aws/things/"+thingname+"/shadow/name/"+shadowname+"/get/accepted";
String Read_rootca, Read_cert, Read_privatekey;
#define BUFFER_LEN 256
long lastMsg = 0;
char msg[BUFFER_LEN];
int Value = 0;
byte mac[6];
char mac_Id[18];
int count = 1;
WiFiClientSecure espClient;
PubSubClient client(espClient);
float temperature = 0;
float humidity = 0;
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(2, LOW);
delay(500);
Serial.print(".");
digitalWrite(2,HIGH);
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP32-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
WiFi.mode(WIFI_STA);
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish(publish_topic.c_str(), "hello world");
// ... and resubscribe
client.subscribe(subscribe_topic.c_str());
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
// initialize digital pin LED_BUILTIN as an output.
pinMode(2, OUTPUT);
setup_wifi();
delay(1000);
//=============================================================
if (!SPIFFS.begin(true)) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
//=======================================
//Root CA File Reading.
File file2 = SPIFFS.open("/AmazonRootCA1.pem", "r");
if (!file2) {
Serial.println("Failed to open file for reading");
return;
}
//Serial.println("Root CA File Content:");
Serial.println("Root CA File Read");
while (file2.available()) {
Read_rootca = file2.readString();
//Serial.println(Read_rootca);
}
//=============================================
// Cert file reading
File file4 = SPIFFS.open("/device-certificate.pem.crt", "r");
if (!file4) {
Serial.println("Failed to open file for reading");
return;
}
//Serial.println("Cert File Content:");
Serial.println("Cert File Read");
while (file4.available()) {
Read_cert = file4.readString();
//Serial.println(Read_cert);
}
//=================================================
//Privatekey file reading
File file6 = SPIFFS.open("/device-private.pem.key", "r");
if (!file6) {
Serial.println("Failed to open file for reading");
return;
}
//Serial.println("privateKey File Content:");
Serial.println("privateKey File Read");
while (file6.available()) {
Read_privatekey = file6.readString();
//Serial.println(Read_privatekey);
}
//=====================================================
char* pRead_rootca;
pRead_rootca = (char *)malloc(sizeof(char) * (Read_rootca.length() + 1));
strcpy(pRead_rootca, Read_rootca.c_str());
char* pRead_cert;
pRead_cert = (char *)malloc(sizeof(char) * (Read_cert.length() + 1));
strcpy(pRead_cert, Read_cert.c_str());
char* pRead_privatekey;
pRead_privatekey = (char *)malloc(sizeof(char) * (Read_privatekey.length() + 1));
strcpy(pRead_privatekey, Read_privatekey.c_str());
espClient.setCACert(pRead_rootca);
espClient.setCertificate(pRead_cert);
espClient.setPrivateKey(pRead_privatekey);
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
delay(2000);
}
void loop() {
float h = random(25,30); // Reading Temperature
float t = random(70,80); // Reading Humidity
float tF = (t * 1.8) + 32;
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
//=============================================================================================
String Temperature = String(t);
String Humidity = String(h);
snprintf (msg, BUFFER_LEN, "{\"state\":{\"reported\":{\"Temperature\":\"%s\",\"Moisture\":\"%s\"}, \"desired\":{\"Temperature\":\"30\",\"Moisture\":\"80\"}}}", Temperature.c_str(), Humidity.c_str());
Serial.print("Publish message: ");
Serial.print(count);
Serial.println(msg);
client.publish(publish_topic.c_str(), msg);
count = count + 1;
//================================================================================================
}
}
float h = random(25,30);
float t = random(70,80);
snprintf (msg, BUFFER_LEN, "{\"state\":{\"reported\":{\"Temperature\":\"%s\",\"Moisture\":\"%s\"}, \"desired\":{\"Temperature\":\"30\",\"Moisture\":\"80\"}}}", Temperature.c_str(), Humidity.c_str());
[EXAMPLE] When a '''user1''' publishes an image on social media, then only the '''user2''' subscribed to '''user1''' can view/receive the image. Here, the '''user1''' is the '''PUBLISHER''', '''user2''' is the '''SUBSCRIBER''', and the '''user1's account''' is the '''BROKER'''.According to the above analogy, the image that is published is the data, that was '''transferred from user1 to user2''' 📤. And that is the exact scenario in an MQTT Pub/Sub model. We have a more secure layer 🔒 to make sure the data is sha'''red through a specific path, we ca'''ll that 'topic', Whe'''n''' user1 publishes data on topic, the subscriber automatically receives if already connected to the broker. Hen'''ce''', the LOW latency.ady connected to the broker. Hen'''ce''', the LOW latency. +
Vous avez entré un nom de page invalide, avec un ou plusieurs caractères suivants :
< > @ ~ : * € £ ` + = / \ | [ ] { } ; ? #