Elizabeth Simon
Published © CC BY-NC

Low temperature heater control with warning

It monitors the temperature under the sink of the guest bathroom, turns a heater on and sends a message if the temperature gets too low.

IntermediateFull instructions provided2 hours641
Low temperature heater control with warning

Things used in this project

Hardware components

SparkFun ESP8266 Thing - Dev Board
SparkFun ESP8266 Thing - Dev Board
×1
SparkFun RHT03 Temperature/Humidity Sensor
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Powerswitch tail II
This has been replaced by the IoT Power Relay
×1

Software apps and online services

Cayenne
myDevices Cayenne

Story

Read more

Schematics

Temperature Sensing and Control

Plug the PowerSwitch Tail into the wall outlet and the heater into the PowerSwitch Tail

Code

ESP8266_LoTemp_Cayenne.ino

Arduino
Use SparkFun ESP8266 thing Dev board and Cayenne App to detect low temperature and turn on heater
// Low temperature sensor, detects low temperature and turns on heater to prevent freezing
// Uses the following:
// SparkFun ESP8266 Thing Dev board
// RHT03 Temperature/Humidity sensor
// PowerSwitch tail II
// Cayenne App

/** 
 * Temperature/Humidity sensor RHT03
 * Connections:
 *   Thing Dev |  RHT03
 *  -----------|---------
 *      3V3    | 1 (VDD) 
 *        4    | 2 (DATA)
 *      GND    | 4 (GND)
 *      
 */
//include RHT03 library
#include <SparkFun_RHT03.h>

/***
 * PowerSwitch tail connections
 *  Thing Dev | PowerSwitch Tail
 * -----------|-----------------
 *     16     |  1 (+in)
 *     GND    |  2 (-in)
 * 
 */

// include Cayenne library and defines
//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>


/****
* connection and authentication info for WiFi and Cayenne
* change this to your information
*/
/// WiFi network info.
char ssid[] = "ssid";
char wifiPassword[] = "password";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "Cayenne username";
char password[] = "Cayenne password";
char clientID[] = "Cayenne clientID";

// general definitions
#define TRUE 1
#define FALSE 0

// Pin definitions
const int RHT03_DATA_PIN = 4;
const int LED_PIN = 5;  // on-board LED
const int SWITCH_PIN = 16;

// widget channel numbers
#define TEMP_F_CHAN 2
//#define TEMP_C_CHAN 2 uncoment this if you want to use celsius instead of fahrenheit
#define SWITCH_OUT_CHAN 5
#define THRESH_CHAN 6

// Global variables
RHT03 rht;

// This flag is used so the sync only happens on the first connection.
bool isFirstConnect = true;

unsigned long lastMillis = 0;
//int counter = 20;
int threshold = 45; // change default threshold for celsius
bool switchOn = FALSE;



// setup code runs once
void setup() {
  // Set up LED for debugging
  pinMode(LED_PIN, OUTPUT);
  // set up switch output
  pinMode(SWITCH_PIN, OUTPUT);
  // set up for debug output to serial port
  Serial.begin(9600);
  // start Cayenne (includes connecting to WiFi)
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  
  // Call rht.begin() to initialize the sensor and our data pin
  rht.begin(RHT03_DATA_PIN);
}

// loop code runs repeatedly
void loop() {
  // put your main code here, to run repeatedly:
  Cayenne.loop();

  //Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
  if (millis() - lastMillis > 10000) 
  {
    lastMillis = millis();
    // Call rht.update() to get new humidity and temperature values from the sensor.
    int updateRet = rht.update();

    // If successful, the update() function will return 1.
    if (updateRet == 1)
    {
  
      // The tempC(), tempF(), and humidity() functions can be 
      // called after a successful update()
//      float temp_c = rht.tempC(); uncoment this if you want to use celsius
      float temp_f = rht.tempF();
     
      //Check if temperatur is less than threshold
      if (temp_f <= threshold)
      {
        switchOn = TRUE;
      }
      else
      {
        switchOn = FALSE;
      }

      digitalWrite(SWITCH_PIN, switchOn);

      //Write data to Cayenne here. Send out the temperature and switch state.
//      Cayenne.celsiusWrite(TEMP_C_CHAN, temp_c); uncomment this if you want to use celsius
      Cayenne.fahrenheitWrite(TEMP_F_CHAN, temp_f);
      Cayenne.virtualWrite(SWITCH_OUT_CHAN, switchOn, "digital_sensor", "d");
    }
    else
    {
      // If the update failed, try delaying for some time
      delay(RHT_READ_INTERVAL_MS);
    }

  // Turn LED off when we've posted the data
  digitalWrite(LED_PIN, HIGH);
  }
}

// This function will run every time the Cayenne connection is established.
CAYENNE_CONNECTED()
{
  CAYENNE_LOG("Connection established");
  if (isFirstConnect)
  {
    CAYENNE_LOG("First Connection");
    // This causes Cayenne to resend data for any virtual pins.
    Cayenne.syncAll();
    isFirstConnect = false;
    CAYENNE_LOG("threshold %d", threshold);
    Cayenne.virtualWrite(THRESH_CHAN, threshold);
  }
}

//Default function for processing actuator commands from the Cayenne Dashboard.
//You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
  CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");

  //set threshold
  if (request.channel == THRESH_CHAN)
  {
    threshold = getValue.asInt();
    CAYENNE_LOG("threshold %d", threshold);
  }

}

Credits

Elizabeth Simon

Elizabeth Simon

1 project • 0 followers

Comments