Jordan Burch
Created April 25, 2016

Improving Home Efficiency One DS18B20 At A Time

This project aims to isolate inefficiencies in my home as well as the HVAC system.

EasyWork in progress4 hours49
Improving Home Efficiency One DS18B20 At A Time

Things used in this project

Hardware components

Photon
Particle Photon
×2
Temperature Sensor DS18B20 (Waterproof)
×2
Resistor 4.75k ohm
Resistor 4.75k ohm
*Pull up resistor
×2
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
Particle Power Sheild
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Circuit Diagram

Code

DS18B20

C/C++
This is the code for the DS18B20
//It will be required that you add the DS18B20 library to your application after copying this code.****IMPORTANT****
#include "DS18B20/Particle-OneWire.h"
#include "DS18B20/DS18B20.h"

DS18B20 ds18b20 = DS18B20(D2); //Sets Pin D2 for Thermostat Temp Sensor
int led = D7;
char szInfo[64];
float pubTemp;
double celsius;
double fahrenheit;
unsigned int Metric_Publish_Rate = 30000;
unsigned int MetricnextPublishTime = 10000;
int DS18B20nextSampleTime;
int DS18B20_SAMPLE_INTERVAL = 2500;
int dsAttempts = 0;

void setup() {
    Time.zone(-5);
    Particle.syncTime();
    pinMode(D2, INPUT);
    Particle.variable("Control", &fahrenheit, DOUBLE);//"Your Variable name"
    Serial.begin(115200);//Be sure that this serial number matches that of the device you are using
}

void loop() {

if (millis() > DS18B20nextSampleTime){
  getTemp();
  }

  if (millis() > MetricnextPublishTime){
    Serial.println("Publishing now.");
    publishData();
  }

}


void publishData(){
  if(!ds18b20.crcCheck()){
    return;
  }
  sprintf(szInfo, "%2.2f", fahrenheit);
  Particle.publish("Thermostat", szInfo, PRIVATE);//"Your event name"
  MetricnextPublishTime = millis() + Metric_Publish_Rate;
}

void getTemp(){
    if(!ds18b20.search()){
      ds18b20.resetsearch();
      celsius = ds18b20.getTemperature();
      Serial.println(celsius);
      while (!ds18b20.crcCheck() && dsAttempts < 4){
        Serial.println("Caught bad value.");
        dsAttempts++;
        Serial.print("Attempts to Read: ");
        Serial.println(dsAttempts);
        if (dsAttempts == 3){
          delay(1000);
        }
        ds18b20.resetsearch();
        celsius = ds18b20.getTemperature();
        continue;
      }
      dsAttempts = 0;
      fahrenheit = ds18b20.convertToFahrenheit(celsius);
      DS18B20nextSampleTime = millis() + DS18B20_SAMPLE_INTERVAL;
      Serial.println(fahrenheit);
    }
}

Credits

Jordan Burch

Jordan Burch

1 project • 1 follower

Comments