#include <DallasTemperature.h>
#include <OneWire.h>
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ThingSpeak.h>
#define LED_BULB 4
#define ONE_WIRE_BUS 0 //D1 pin of nodemcu
#define DHTPIN 5
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
const char* ssid = "XXXXX";
const char* password = "XXXXX";
WiFiClient client;
unsigned long myChannelNumber = XXXXX;
const char * myWriteAPIKey = "XXXXX";
uint8_t temperature, humidity, dsb;
void startup()
{
pinMode(LED_BULB, OUTPUT);
digitalWrite(LED_BULB, LOW);
Serial.begin(115200);
dht.begin();
delay(10);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Print the IP address
Serial.println(WiFi.localIP());
ThingSpeak.begin(client);
sensors.begin();
}
void setup()
{
}
void loop()
{
startup();
Serial.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
sensors.requestTemperatures();
Serial.print("DSB Temperature is: ");
Serial.println(sensors.getTempCByIndex(0));
dsb = sensors.getTempCByIndex(0);
temperature = dht.readTemperature();
humidity = dht.readHumidity();
Serial.print("Temperature Value is :");
Serial.print(temperature);
Serial.println("C");
Serial.print("Humidity Value is :");
Serial.print(humidity);
Serial.println("%");
digitalWrite(LED_BULB, HIGH);
Serial.println("Uploading DHT temp");
ThingSpeak.writeField(myChannelNumber, 1, temperature, myWriteAPIKey);
delay(30000);
Serial.println("Uploading DHT hum");
ThingSpeak.writeField(myChannelNumber, 2, humidity, myWriteAPIKey);
delay(30000);
Serial.println("Uploading DS temp");
ThingSpeak.writeField(myChannelNumber, 3, dsb, myWriteAPIKey);
Serial.println("Done uploading");
delay(30000);
digitalWrite(LED_BULB, LOW);
delay(500);
digitalWrite(LED_BULB, HIGH);
delay(500);
digitalWrite(LED_BULB, LOW);
delay(500);
digitalWrite(LED_BULB, HIGH);
delay(500);
digitalWrite(LED_BULB, LOW);
delay(500);
Serial.println("Sleeping");
ESP.deepSleep(120000000); //sleeping for 120 seconds
Serial.println("Waking up");
}
The output on the serial monitor is as shown below.
Connecting to XXXXX
..............................................................
WiFi connected
XXXXXX
DSB Temperature is: 26.56
Temperature Value is :25C
Humidity Value is :92%
Uploading DHT temp
Uploading DHT hum
Uploading DS temp
Done uploading
Sleeping
After printing 'sleeping', some mixed up jargon will be printed and that's it.