Created
August 4, 2022 15:26
-
-
Save sebadima/c25396e5f32d206b458a53609bf992c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <esp_wifi.h> | |
#include <WiFiClientSecure.h> | |
#include <PubSubClient.h> | |
#include <ArduinoJson.h> | |
#include "WiFi.h" | |
#include "DHT.h" | |
#include "secrets.h" | |
#define AWS_IOT_PUBLISH_TOPIC "esp32/pub" | |
#define AWS_IOT_SUBSCRIBE_TOPIC "esp32/sub" | |
#define DHTTYPE DHT11 // DHT 11 | |
#define DHTPIN 21 // Usa lo zoccolo BMP_1, sostituisci con 15 per usare DHT_1 | |
float h; | |
float t; | |
float g; | |
DHT dht(DHTPIN, DHTTYPE); | |
WiFiClientSecure net = WiFiClientSecure(); | |
PubSubClient client(net); | |
int Gas = 14; | |
int Led1 = 26; | |
int Led2 = 27; | |
void connectAWS() | |
{ | |
Serial.println("In collegamento al Wi-Fi"); | |
digitalWrite(Led1, HIGH); | |
delay(25); | |
digitalWrite(Led1, LOW); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(500); | |
Serial.println("."); | |
} | |
// Configura WiFiClientSecure per usare le credenziali AWS IoT | |
net.setCACert(AWS_CERT_CA); | |
net.setCertificate(AWS_CERT_CRT); | |
net.setPrivateKey(AWS_CERT_PRIVATE); | |
// Collegamento al broker MQTT sull' endpoint AWS di robotdazeo.it | |
client.setServer(AWS_IOT_ENDPOINT, 8883); | |
// Crea un message handler | |
client.setCallback(messageHandler); | |
Serial.println("In collegamento a AWS IOT"); | |
while (!client.connect(THINGNAME)) | |
{ | |
Serial.print("."); | |
delay(100); | |
} | |
if (!client.connected()) | |
{ | |
Serial.println("AWS IoT Timeout!"); | |
return; | |
} | |
// Subscribe al topic di AWS | |
client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC); | |
Serial.println("AWS IoT Connesso!"); | |
digitalWrite(Led1, HIGH); | |
delay(100); | |
digitalWrite(Led1, LOW); | |
} | |
void publishMessage() | |
{ | |
StaticJsonDocument<200> doc; | |
doc["humidity"] = h; | |
doc["temperature"] = t; | |
doc["customer"] = 8020; | |
doc["machine"] = 8021; | |
doc["airquality"] = g; | |
char jsonBuffer[512]; | |
serializeJson(doc, jsonBuffer); | |
Serial.println("Dati da inviare via JSON:"); | |
digitalWrite(Led2, HIGH); | |
delay(100); | |
digitalWrite(Led2, LOW); | |
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); | |
pinMode(Led1, OUTPUT); | |
pinMode(Led2, OUTPUT); | |
pinMode(Gas, INPUT); | |
dht.begin(); | |
connectAWS(); | |
} | |
void loop() | |
{ | |
client.disconnect(); | |
WiFi.disconnect(); | |
WiFi.disconnect(true); // Scollegati dal WIFI | |
WiFi.mode(WIFI_OFF); | |
esp_wifi_stop(); | |
h = dht.readHumidity(); | |
t = dht.readTemperature(); | |
g = analogRead(Gas); | |
if (isnan(t) ) | |
{ | |
Serial.println(F("Non riesco a leggere dal sensore DHT!")); | |
return; | |
} | |
if (isnan(g) ) | |
{ | |
Serial.println(F("Non riesco a leggere dal sensore di GAS!")); | |
return; | |
} | |
Serial.println(""); | |
Serial.println("*****************************************************"); | |
Serial.print(F(" Umidita:")); | |
Serial.print(h); | |
Serial.print(F(" Temperatura:")); | |
Serial.print(t); | |
Serial.print(F(" Gas:")); | |
Serial.print(g); | |
Serial.println(""); | |
Serial.println("*****************************************************"); | |
Serial.println(""); | |
connectAWS(); | |
publishMessage(); | |
delay(30000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment