Last active
April 10, 2022 21:34
-
-
Save sebadima/9097a1fcdddbc5bc6068318147cdb24e to your computer and use it in GitHub Desktop.
esp32-sensore-gas-002.ino
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 "secrets.h" | |
#include <WiFiClientSecure.h> | |
#include <PubSubClient.h> | |
#include <ArduinoJson.h> | |
#include "WiFi.h" | |
#include "DHT.h" | |
#define DHTPIN 34 // Digital pin connected to the DHT sensor | |
#define DHTTYPE DHT11 // DHT 11 | |
#define AWS_IOT_PUBLISH_TOPIC "esp32/pub" | |
#define AWS_IOT_SUBSCRIBE_TOPIC "esp32/sub" | |
float h ; | |
float t; | |
const int potPin =34; | |
int potValue = 0; | |
//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["quality"] = h; | |
doc["temperature"] = t; | |
doc["machine"] = 1001; | |
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() | |
{ | |
//delay(1000); | |
h = analogRead(potPin); | |
//h = dht.readHumidity(); | |
//t = dht.readTemperature(); | |
t=25; | |
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("Quality: ")); | |
Serial.print(h); | |
Serial.print(F("% Temperature: ")); | |
Serial.print(t); | |
Serial.println(F("°C ")); | |
publishMessage(); | |
client.loop(); | |
delay(5000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment