#include "secrets.h"
#include <HTTPClient.h> //Download: https://electronoobs.com/eng_arduino_httpclient.php
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <WiFi.h>
#include <NTPClient.h>
#include <DHT.h>
#include <Wire.h>
#define DHTPIN 15 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 11
#define AWS_IOT_PUBLISH_TOPIC "esp32/pub"
#define AWS_IOT_SUBSCRIBE_TOPIC "esp32/sub"
float h ;
float t;
DHT dht(DHTPIN, DHTTYPE);
WiFiClientSecure net;
PubSubClient client(net);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "id.pool.ntp.org");
String data_to_send = "";
void connectAWS()
{
WiFi.mode(WIFI_STA);
WiFi.begin("Wokwi-GUEST", "");
Serial.println("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("Connected!");
//Configuring the DHT22
//Serial.begin(115200);
//connectAWS();
dht.begin();
//Wire.begin();
//timeClient.begin();
//timeClient.setTimeOffset(0);
h = dht.readHumidity();
t = dht.readTemperature();
HTTPClient http;
while (true){
data_to_send = "";
data_to_send += dht.readHumidity();
data_to_send +=" : ";
data_to_send += dht.readTemperature();
//Begin new connection to website
http.begin("https://ufkngg6bu1.execute-api.us-east-1.amazonaws.com/palavra"); //Indicate the destination webpage
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Prepare the header
int response_code = http.GET();//POST(data_to_send); //Send the POST. This will giveg us a response code
//If the code is higher than 0, it means we received a response
if(response_code > 0){
Serial.println("HTTP code " + String(response_code)); //Print return code
if(response_code == 200){ //If code is 200, we received a good response and we can read the echo data
String response_body = http.getString(); //Save the data comming from the website
Serial.print("Server reply: "); //Print data to the monitor for debug
Serial.println(response_body);
}//End of response_code = 200
}//END of response_code > 0
Serial.println(data_to_send);
delay(1000);
}
// 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.connected())
{
Serial.print(".");
if (client.connect(THINGNAME))
{
Serial.println("Connected to AWS IoT");
client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC);
}
else
{
Serial.println("AWS IoT Connection Failed! Retrying...");
delay(1000);
}
}
}
void publishMessage()
{
StaticJsonDocument<200> doc;
doc["timestamp"] = timeClient.getEpochTime() + (7 * 3600);
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 message from topic: ");
Serial.println(topic);
StaticJsonDocument<200> doc;
deserializeJson(doc, payload, length);
const char* message = doc["message"];
Serial.println(message);
}
void setup()
{
Serial.begin(115200);
connectAWS();
dht.begin();
Wire.begin();
timeClient.begin();
timeClient.setTimeOffset(0);
}
void loop()
{
h = dht.readHumidity();
t = dht.readTemperature();
timeClient.update();
if (isnan(h) || isnan(t))
{
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 "));
// Serial.println(timeClient);
publishMessage();
client.loop();
delay(5000);
}