#include "DHTesp.h"
#include <avr/wdt.h>
#include <avr/sleep.h>
#include <SoftwareSerial.h>
const int DHT_PIN = 13;
const int sensorPin = A0;
DHTesp dhtSensor; // initialize sensor instance
SoftwareSerial esp32(7, 6); // initialize software serial
#define DEBUG true
String espmode = "3";
String ssid = "Wokwi-GUEST";
String password = "";
String server_ip = "184.106.153.149";
String apikey = "BPT4SHRMJPL5AH8F"; // write api key of thingspeak
void wifiSetup(String mode, String ssid, String pass);
void wifidata(float temp, float hum, String s_ip, int Idrval, String apikey);
void myWatchdogEnable(const byte interval);
void setup()
{
wdt_enable(WDTO_8S); // enable the watchdog timer
Serial.begin(9600); // set esp baud rate
esp32.begin(9600); // set software serial baud rate
wdt_reset(); // reset the watchdog timer
pinMode(10, OUTPUT); // configure pin 10 as output
dhtSensor.setup(DHT_PIN, DHTesp::DHT22); // setup dht sensor
}
//function to send data
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp32.print(command); // send the read character to the esp32
long int time = millis();
wdt_reset ();
while ((time + timeout) > millis()) //until timeout time is met
{
// wdt_reset ();
while (esp32.available())
{
wdt_reset ();
char c = esp32.read(); // read the next character.
response += c;
}
}
if (debug)
{
wdt_reset ();
Serial.print(response);
}
wdt_reset();
return response;
}
// WDT vector Function
ISR(WDT_vect)
{
wdt_disable(); // disable the watchdog timer
}
// Watchdog Function
void myWatchdogEnable(const byte interval)
{
// reset various flags
MCUSR =0;
WDTCSR |= 0b00011000; // see docs, set WDCE, WOE
WDTCSR = 0b01000000 | interval;
wdt_reset();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_mode();
}
void wifiSetup(String mode, String ssid, String pass){
wdt_reset();
sendData("AT\r\n",2000,DEBUG);
String mode0="AT+CWMODE+";
mode0 += mode;
mode0 += "\r\n";
wdt_reset();
sendData(mode0,2000,DEBUG);
wdt_reset();
String ssidset="AT+CWJAP=\"";
ssidset +=ssid;
ssidset +=pass;
ssidset +="\"";
ssidset+="\r\n";
sendData(ssidset,10000,DEBUG);
wdt_reset();
return;
}
void wifidata(float temp,float hum,String s_ip,int IdrvaI, String apikey){
wdt_reset ();
char buffer[10];
String HumiF = dtostrf(hum, 4, 1, buffer);
// convert floating point value to string
String tempF = dtostrf(temp, 4, 1, buffer);
String LDRval = dtostrf(temp, 4, 1, buffer);
wdt_reset ();
String api ="https://api.thingspeak.com/update?api_key=" ;
api += apikey;
String Data1=api+'&'+"field1="+tempF+'&'+"field2="+HumiF+'&'+"field3="+LDRval;
// formulate the url that will be sent
Data1+="\r\n";
wdt_reset();
String scnt="AT+CIPSTART=\"TCP\",\"";
scnt += s_ip ;
scnt += "\",8-\r\n" ;
sendData(scnt,1000,DEBUG);
String cipsend ="AT+cipsend=";
wdt_reset ();
cipsend +=Data1.length();
cipsend +="\r\n";
wdt_reset ();
sendData(cipsend,2000,DEBUG);
Serial.println(Data1);
sendData(Data1,5000,DEBUG);
wdt_reset ();
String closeCommand ="AT+CIPCLOSE";
closeCommand+="\r\n";
sendData(closeCommand,1000,DEBUG);
wdt_reset ();
return ;
}
void loop()
{
digitalWrite(10, LOW);
myWatchdogEnable (0b100001); // 8 seconds
wdt_reset ();
wifiSetup(espmode, ssid, password);
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float tem = data.temperature;
float hum = data.humidity;
wdt_reset (); // read temperature and humidity from the sensor
int Idr = analogRead(sensorPin); //get the interval
wifidata(tem, hum, server_ip, Idr, apikey); //send the new readings
digitalWrite(10, HIGH);
myWatchdogEnable (0b100001); // 8 seconds
for (int a = 0; a <= 75; a++)
{
myWatchdogEnable (0b100001); // 8 seconds
}
myWatchdogEnable (0b100001); // 8 seconds
myWatchdogEnable (0b100001); // 4 seconds
}