Go Down

Topic: I want to change DHT sensor to DS18b20 in script and paying 20EUR - PAYPAL (Read 3673 times) previous topic - next topic

testy123

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/





Code: [Select]

/* 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";


martinius96

That code were so trash from that instructables portal... ON/OFF regulation does not work...
There are problems with data types, for that axis cannot be draw (I think it is some like line plot).
But gauges working, also settings (from first ds18b20 sensor). Second ds18b20 is like Monitor.
It looks like that projects werent finished...
Here is version with two DS18B20 sensors at OneWire bus:
https://pastebin.com/McMTWJwH

Arduino and website programmer

testy123

Gracias Amigo,

I will test this live tomorrow.

Please send over your paypal e-mail adress in PM! =)

Thanks

That code were so trash from that instructables portal... ON/OFF regulation does not work...
There are problems with data types, for that axis cannot be draw (I think it is some like line plot).
But gauges working, also settings (from first ds18b20 sensor). Second ds18b20 is like Monitor.
It looks like that projects werent finished...
Here is version with two DS18B20 sensors at OneWire bus:
https://pastebin.com/McMTWJwH



martinius96

Hi, I have tested it now and it is working!  :)  (THAT ON/OFF regulation based on value)
Problem was, I was testing it only under and upper to CoolingOn value, not under CoolingOff value...
So It is working.
Maybe you will need to change
digitalWrite(RELAYPIN, HIGH); and digitalWrite(RELAYPIN, LOW); opposite for your purposes. Because some relays have inverted logic.
I think I haven't change code, but... there is last version. Should be same as previous.
https://pastebin.com/PjheJtyf
PM sent,
Martin.
Arduino and website programmer

martinius96

Have you tested? testy123
It should be better to remove few lines from javascript, because of that notification that show us on every refresh... Data columns axis..
Arduino and website programmer

martinius96

Ok, so this guy paid NOTHING. Doesn't replied to PMs, comments, doesn't paid for programming his project...
Please administrator / moderator ban this guy from forum.
Arduino and website programmer


Go Up