#include <OneWire.h>
#include <DallasTemperature.h>
#include <DHT.h>;
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <ESP8266WiFi.h>
#include "ThingSpeak.h"
#include <BlynkSimpleEsp8266.h>
#define ONE_WIRE_BUS D5 // Temperature sensor data wire is plugged into port D2 on the ESP8266
#define DHTPIN D4 // DHT 22 pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define RELAY 0 //Connect GPIO0 to RELAY (D3)
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define BLYNK_PRINT Serial
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
float tempSensor1, tempSensor2;
float hum; //Stores humidity value
float temp; //Stores temperature value
float tempDiff;
unsigned long previousMillis = 0;
const long interval = 30000; //Thinkspeak send interval. Dont put too low or your account will be locked
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Blynk authorisation code
int pinButton = D7; //the pin where we connect the button (virtual button)
int LED = D6; //the pin we connect the Relay, blynk contolled
uint8_t sensor1[8] = { 0x28, 0x22, 0x10, 0x66, 0x04, 0x00, 0x00, 0xBF }; // Enter your HEX address here, inside temp sensor
uint8_t sensor2[8] = { 0x28, 0x7D, 0xFE, 0x65, 0x04, 0x00, 0x00, 0x8C }; // Enter your HEX address here, outside temp sensor
/*Put your SSID & Password*/
char ssid[] = "YOUR SSID"; // your network SSID (name)
char pass[] = "YOUR PWD"; // your network password
int status = WL_IDLE_STATUS;
WiFiClient client;
unsigned long myChannelNumber = xxxxxxxxx; //your Thingspeak channel without commas
const char * myWriteAPIKey = "xxxxxxxxxxxxxx"; //your Thingspeak writeAPIKey with commas
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, LOW);
delay(100);
sensors.begin();
// OLED
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
// End OLED
WiFi.begin(ssid, pass);
ThingSpeak.begin(client);
Blynk.begin(auth, ssid, pass);
}
void loop() {
sensors.requestTemperatures();
tempSensor1 = sensors.getTempC(sensor1); // Gets the values of the temperature
tempSensor2 = sensors.getTempC(sensor2); // Gets the values of the temperature
tempDiff = (tempSensor1 - tempSensor2);
temp= dht.readTemperature();
hum = dht.readHumidity();
Blynk.run();
delay(3000);
// Heater relay ON/OFF function
if (tempDiff > 4) {
digitalWrite(RELAY, LOW);
}
// else {
// digitalWrite(RELAY, HIGH);
// }
if(tempDiff<2) {
digitalWrite(RELAY, HIGH);
}
// End of relay funct.
// debug mode
Serial.print("T1 ");
Serial.print(tempSensor1);
Serial.print(" C");
Serial.println("");
Serial.print("T2 ");
Serial.print(tempSensor2);
Serial.print(" C");
Serial.println("");
Serial.println(tempDiff);
Serial.print("DHT Temp ");
Serial.print(temp);
Serial.print(" C");
Serial.println("");
Serial.print("DHT Hum ");
Serial.print(hum);
Serial.print(" %");
Serial.println("");
Serial.println(tempDiff);
Serial.println(digitalRead (RELAY));
// OLED Print mode
if (isnan(hum) || isnan(tempDiff)) {
Serial.println("Failed to read from DHT sensor!");
}
//clear display
display.clearDisplay();
// display temperature
display.setTextSize(1);
display.setCursor(0,0);
display.print("TempDiff: ");
display.setTextSize(2);
display.setCursor(0,10);
display.print(tempDiff);
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("C");
// display humidity
display.setTextSize(1);
display.setCursor(0, 35);
display.print("Humidity: ");
display.setTextSize(2);
display.setCursor(0, 45);
display.print(hum);
display.print(" %");
display.display();
Blynk.run();
//Function to send to Thingspeak on defined interval
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
Serial.println("Sending to ThingSpeak");
previousMillis = currentMillis;
ThingSpeak.setField(1, tempSensor1);
ThingSpeak.setField(2, tempSensor2);
ThingSpeak.setField(3, digitalRead(RELAY));
ThingSpeak.setField(4, tempDiff);
ThingSpeak.setField(5, hum);
ThingSpeak.setField(6, temp);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
}
}