Hi guys,
I want to change this sketch to use 2pcs DS18b20 instead of temp/humidity from DHT.
I just dont have the time to learn arduino i realized.
*Temperature in Celscius
- Use DS18b20 instead of DHT
*Instead of “Heating” function it should be reverted and used as a cooler (if temp is above a set value the relay is active)
*current “temp” value should be value from DS18b20#1, current “humidity” should be value from DS18b20#2
FULL CODE https://www.instructables.com/id/ESP8266-12E-DHT-Thermostat/
/* ESP8266 Programmable Thermostat IoT
Version 1.0 1/7/2017 Version 1.0 Damon Borgnino
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <FS.h> // FOR SPIFFS
#include <ctype.h> // for isNumber check
#include <DHT.h>
#define DHTTYPE DHT22
#define DHTPIN 2 // D4
#define RELAYPIN 4 // D2
const char* ssid = "yourWiFiSSID";
const char* password = "yourWiFiPassword";
int heatOn = 69;
int heatOff = 73;
String relayState = "OFF";
const static String fName = "prop.txt";
const static String dfName = "data.txt";
int dataLines = 0;
int maxFileData = 20;
ESP8266WebServer server(80);
// Initialize DHT sensor
// This is for the ESP8266 processor on ESP-01
DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266
float humidity, temp_f; // Values read from sensor
String webString = ""; // String to display
String webMessage = "";
// Generally, you should use "unsigned long" for variables that hold time to handle rollover
unsigned long previousMillis = 0; // will store last temp was read
long interval = 20000; // interval at which to read sensor
// reads the temp and humidity from the DHT sensor and sets the global variables for both
void gettemperature() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
humidity = dht.readHumidity(); // Read humidity (percent)
temp_f = dht.readTemperature(true); // Read temperature as Fahrenheit
// Check if any reads failed and exit
if (isnan(humidity) || isnan(temp_f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// turn the relay switch Off or On depending on the temperature reading
if (temp_f <= heatOn)
{
digitalWrite(RELAYPIN, HIGH);
relayState = "ON";
}
else if (temp_f >= heatOff)
{
digitalWrite(RELAYPIN, LOW);
relayState = "OFF";
}
}
/////////////////////////////////////////////////
void clearDataFile() // deletes all the stored data
{
File f = SPIFFS.open(dfName, "w");
if (!f) {
Serial.println("data file open to clear failed");
}
else
{
Serial.println("-- Data file cleared =========");
f.close();
}
}
///////////////////////////////////////////////////////////////////////////////////
// removes the first line of a file by writing all data except the first line
// to a new file. The old file is deleted and new file is renamed.
///////////////////////////////////////////////////////////////////////////////////
void removeFileLine(String fi)
{
File original = SPIFFS.open(fi, "r");
if (!original) {
Serial.println("original data file open failed");
}
File temp = SPIFFS.open("tempfile.txt", "w");
if (!temp) {
Serial.println("temp data file open failed");
}
Serial.println("------ Removing Data Lines ------");
//Lets read line by line from the file
for (int i = 0; i < maxFileData; i++) {
String str = original.readStringUntil('\n'); // read a line
if (i > 0) { // skip writing first line to the temp file
temp.println(str);
// Serial.println(str); // uncomment to view the file data in the serial console
}
}
int origSize = original.size();
int tempSize = temp.size();
temp.close();
original.close();
Serial.print("size orig: "); Serial.print(origSize); Serial.println(" bytes");
Serial.print("size temp: "); Serial.print(tempSize); Serial.println(" bytes");
if (! SPIFFS.remove(dfName))
Serial.println("Remove file failed");
if (! SPIFFS.rename("tempfile.txt", dfName))
Serial.println("Rename file failed");
// dataLines--;
}
//////////////////////////////////////////////////////////////////////
// writes the most recent variable values to the data file //
//////////////////////////////////////////////////////////////////////
void updateDataFile()
{
Serial.println("dataLines: ");
Serial.println(dataLines);
if (dataLines >= maxFileData)
{
removeFileLine(dfName);
}
///////
File f = SPIFFS.open(dfName, "a");
if (!f) {
Serial.println("data file open failed");
}
else
{
Serial.println("====== Writing to data file =========");
f.print(relayState); f.print(":");
f.print(temp_f); f.print( ","); f.println(humidity);
Serial.println("Data file updated");
f.close();
}
Serial.print("millis: ");
Serial.println(millis());
}
//////////////////////////////////////////////////////////////////////////////
// reads the data and formats it so that it can be used by google charts //
//////////////////////////////////////////////////////////////////////////////
String readDataFile()
{
String returnStr = "";
File f = SPIFFS.open(dfName, "r");
if (!f)
{
Serial.println("Data File Open for read failed.");
}
else
{
Serial.println("----Reading Data File-----");
dataLines = 0;
while (f.available()) {
//Lets read line by line from the file
dataLines++;
String str = f.readStringUntil('\n');
String switchState = str.substring(0, str.indexOf(":") );
/*
String tempF = str.substring(str.indexOf(":") + 1, str.indexOf(",") );
// String humid = str.substring(str.indexOf(",") + 1 );
// String milliSecs = str.substring(str.indexOf("~") + 1 , str.indexOf("~"));
// Serial.println(tempF);
// Serial.println(humid);
// Serial.println(str);
*/
returnStr += ",['" + switchState + "'," + str.substring(str.indexOf(":") + 1) + "]";
}
f.close();
}
return returnStr;
}
////////////////////////////////////////////////////////////////////////////////////
// creates the HTML string to be sent to the client //
////////////////////////////////////////////////////////////////////////////////////
void setHTML()
{
webString = "<html><head>\n";
webString += "<meta http-equiv=\"refresh\" content=\"60;url=http://" + WiFi.localIP().toString() + "\"> \n";