//Hariharnath Paduchuru Working Demo
#include <WiFi.h>
#include "secrets.h"
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include "WiFi.h"
#include "DHTesp.h"
#include "DHT.h"
#include <LiquidCrystal_I2C.h>
int lcdColumns = 20;
int lcdRows = 4;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
// Define your custom I2C pins
#define CUSTOM_SDA_PIN 32 // 21 // 14
#define CUSTOM_SCL_PIN 33 // 22 //15
const int DHT_PIN = 15;
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHT_PIN, DHTTYPE);
DHTesp dhtSensor;
#define AWS_IOT_PUBLISH_TOPIC "iotfrontier/pub"
#define AWS_IOT_SUBSCRIBE_TOPIC "iotfrontier/sub"
String h ;
String t;
WiFiClientSecure net = WiFiClientSecure();
PubSubClient client(net);
// Function to clear a specific row
void clearRow(int row) {
lcd.setCursor(0, row); // Set cursor to the beginning of the desired row
for (int i = 0; i < 20; i++) { // Assuming a 20x4 LCD, print 20 spaces
lcd.print(" ");
}
}
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!");
Wire.begin(CUSTOM_SDA_PIN, CUSTOM_SCL_PIN); // Initialize with custom pins
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
dht.begin();
}
void publishMessage()
{
StaticJsonDocument<200> doc;
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: ");
Serial.println(topic);
StaticJsonDocument<200> doc;
deserializeJson(doc, payload);
const char* message = doc["message"];
Serial.println(message);
}
void setup() {
Serial.begin(115200);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
connectAWS();
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
h = String(data.temperature, 2);
t = String(data.humidity, 1);
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
// LCD display ===================================
// set cursor to first column, first row
clearRow(0);
clearRow(1);
clearRow(2);
clearRow(3);
lcd.setCursor(0, 0); // First col ,first Row
lcd.print("Humd:"); // 5 character
lcd.setCursor(5, 0); //
lcd.print(h); // 5 character (10)
lcd.setCursor(10, 0); //
lcd.print(","); // 1 (12)
lcd.setCursor(11, 0); // First col ,first Row
lcd.print("Tmp:"); // 4 characters (17)
lcd.setCursor(15, 0); //
lcd.print(t); // 22
//=======================================================
lcd.setCursor(0, 1); // First col ,first Row
lcd.print("HIndx:"); // 6 character heat feel like
lcd.setCursor(6, 1); //
lcd.print(hic); // 5 character (11)
lcd.setCursor(10, 1); //
lcd.print(","); // 1 (12)
lcd.setCursor(11, 1); // First col ,first Row
lcd.print("Tmp:"); // 7 characters (17)
lcd.setCursor(15, 1); //
lcd.print(hif); // 22
//=======================================================
delay(2000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
publishMessage(); // run this to data go to aws
//messageHandler();
client.loop(); // run this to recive data from aws
delay(5000);
}