/*
ANKIT PATRAS TOPNO
IoT & Robotics - Projects
Krutanic July 2024
1. Design an IoT System to perform following tasks
> Interface ESP32 with DHT11 Sensor to read temperature and Humidity.
> Print this data on LCD screen
> Send this data to ThingSpeak cloud
> Create 2 Lamps indicator on cloud and turn on the lamps if the sensed
valued crosses a threshold level.
*/
#include <WiFi.h> //Wifi library
#include <HTTPClient.h> //HTTP library
const char* ssid = "Wokwi-GUEST"; //wifi name
const char* password = ""; //wifi password (here, it is an open network)
String talkbacks_id = "52717"; //talkback ID
String command_id = "44779177"; //command ID
String talkbacks_api = "PBV1XSJMEW6CY29J"; //talkback API key
String command_string=""; //empty command
void GETcommand()
{
HTTPClient http;
Serial.print("[HTTP] begin...\n");
//"http://api.thingspeak.com/talkbacks/35233/commands/16599915.json?api_key=0E1160BEX9CSNU6V"
String url = "http://api.thingspeak.com/talkbacks/";
url += talkbacks_id;
url += "/commands/";
url += command_id;
url += ".json?api_key=";
url += talkbacks_api;
http.begin(url); //HTTP
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
int index_start = payload.indexOf("command_string\":\"") + 17;
int index_stop = payload.indexOf("\",\"position");
command_string = payload.substring(index_start, index_stop);
Serial.println(command_string);
}
}
else
{
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
delay(1000);
}
void connectWifi (const char* ssid, const char* password)
{
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to WiFi");
}
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
connectWifi(ssid, password);
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
}
// I = V/R
// 20mA = 3.3/R
void loop() {
GETcommand();
if(command_string == "HI") //here, "HI" is the reset condition
{
digitalWrite(2, LOW);//turn off the LED
digitalWrite(4, LOW);//turn off the ERROR LED
}
else if(command_string == "LEDON")
{
digitalWrite(4, HIGH);// turn on red LED (i.e. alert)
digitalWrite(2, LOW);//turn off the green LED (i.e. normal)
}
else
{
digitalWrite(2, HIGH);// turn OFF red LED
digitalWrite(4, LOW);//turn ON green LED
}
delay(1000);
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4