Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
gusgonnet
Published © CC BY-NC-SA

Bluz Wireless Temperature Sensor

Add that much needed remote temperature sensor wherever you want, no wall outlet required.

AdvancedFull instructions provided12 hours1,335
Bluz Wireless Temperature Sensor

Things used in this project

Hardware components

Bluz DK
Bluz DK
×1
Bluz gateway shield
×1
Particle Photon
this one is required for the Bluz gateway shield
×1
Adafruit 10K Precision Epoxy Thermistor - 3950 NTC
×1
Bluz Battery Shield
×1
Coin Cell Battery CR2032
Coin Cell Battery CR2032
×1
DFRobot Mini Bread Board Self Adhesive
×1

Story

Read more

Schematics

sche_sQpra2Pqb3.jpg

Code

bluz_thermistor.ino

Arduino
copy paste in your Particle Build interface
//**************************************************************************************
// Author: Gustavo Gonnet
// Contact: gusgonnet@gmail.com
// Projects: https://www.hackster.io/gusgonnet/
//
// Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
// This is a human-readable summary of (and not a substitute for) the license.
// Disclaimer
//
// You are free to:
// Share β€” copy and redistribute the material in any medium or format
// Adapt β€” remix, transform, and build upon the material
// The licensor cannot revoke these freedoms as long as you follow the license terms.
//
// Under the following terms:
// Attribution β€” You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
// NonCommercial β€” You may not use the material for commercial purposes.
// ShareAlike β€” If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.
// No additional restrictions β€” You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.
//
// Notices:
// You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation.
// No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material.
//
// Free for personal use.
//
// https://creativecommons.org/licenses/by-nc-sa/4.0/

//**************************************************************************************
// Notes:
// when I started this app with a cheap cr 2032 battery it lasted one week
//**************************************************************************************
#include <math.h>
#include "application.h"

#define APP_NAME "BLUZ_thermistor"
String VERSION = "Version 0.01";
int version = 1;

#define EVENT_NAME "BLUZ_TEMP"

/*******************************************************************************
 * changes in version 0.01:
       * initial commit
*******************************************************************************/

// by default, the temperature will be displayed in degrees celsius,
//  but if you prefer farenheit please set this to true
bool useFahrenheit = false;

// this is the thermistor used
// https://www.adafruit.com/products/372
// resistance at 25 degrees C
#define THERMISTORNOMINAL 10000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 10000

// A7 is not available in bluz!
// source: http://docs.bluz.io/hardware/dk/
// A6 and A7 cannot read analog data even though they are still labeled A6 and A7 (which was done for compatible code). There simple weren't enough ADC pins on the nrf51.
int THERMISTOR = A5;
// this gives power to the thermistor
int VCC_FOR_THERMISTOR = A0;

int LED = D7;

// this variable helps you match the measurement to another source (eg: your thermostat)
// hum, this value is pretty high, I guess is a consecuence of me not using a 1% 10k resistor
// but a less precise 10k resistor
float temperatureCorrection = -5.9;

// store the previous measured temp to check if current measurement needs to be transmited
float previousTemperature;

float currentTemperature;

unsigned long lastTime = 0;

// polling period: 20 mins == 1200000
// adjust to your liking
unsigned long checkPeriod = 10000;

// this variable defines the range where the temp is not updated (not published)
// adjust to your liking
float deltaTemperature = 0.5;


/*******************************************************************************
 * Function Name  : setup
 * Description    : this function runs once at system boot
 *******************************************************************************/
void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(VCC_FOR_THERMISTOR, OUTPUT);
  digitalWrite(VCC_FOR_THERMISTOR, LOW);
  pinMode(THERMISTOR, INPUT);

  Particle.publish(APP_NAME, VERSION, PRIVATE);

  // turn off led to save power
  // comment these two lines below while regular development
//   RGB.control(true);
//   RGB.color(0, 0, 0);

  Particle.function("getTemp", getTemperature);
  Particle.function("getVersion", getVersion);
  Particle.function("pulseLed", pulseLed);
}

/*******************************************************************************
 * Function Name  : loop
 * Description    : this function runs all the time
 *******************************************************************************/
void loop()
{

  System.sleep(SLEEP_MODE_CPU);

  // source: https://community.particle.io/t/millis-and-rollover-tutorial/20429
  unsigned long now = millis();

  if ((now - lastTime) >= checkPeriod)
  {
    lastTime = now;

    // power on the thermistor
    digitalWrite(VCC_FOR_THERMISTOR, HIGH);

    calculateTemperature();

    // power off the thermistor
    digitalWrite(VCC_FOR_THERMISTOR, LOW);
  }
}

/*******************************************************************************
 * Function Name  : calculateTemperature
 * Description    : read the value of the thermistor and convert it to degrees
 * Return         : none
 *******************************************************************************/
void calculateTemperature()
{

  // read and convert the value to resistance
  float average = analogRead(THERMISTOR);
  average = (1023 / average) - 1;
  average = SERIESRESISTOR / average;

  float steinhart;
  steinhart = average / THERMISTORNOMINAL;          // (R/Ro)
  steinhart = log(steinhart);                       // ln(R/Ro)
  steinhart /= BCOEFFICIENT;                        // 1/B * ln(R/Ro)
  steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  steinhart = 1.0 / steinhart;                      // Invert
  steinhart -= 273.15;                              // convert to C

  // Convert Celsius to Fahrenheit
  // source: http://playground.arduino.cc/ComponentLib/Thermistor2#TheSimpleCode
  if (useFahrenheit)
  {
    steinhart = (steinhart * 9.0) / 5.0 + 32.0;
  }

  currentTemperature = steinhart + temperatureCorrection;

//   if (abs(currentTemperature - previousTemperature) > deltaTemperature)
//   {
//     Particle.publish(EVENT_NAME, float2string(currentTemperature), 60, PRIVATE);
//   }

     Particle.publish(EVENT_NAME, float2string(currentTemperature), 60, PRIVATE);

  // keep track of this value
  previousTemperature = currentTemperature;
}

String float2string(float floatNumber)
{
  String stringNumber = String(floatNumber);

  //return only 2 decimals
  // Example: show 19.00 instead of 19.000000
  stringNumber = stringNumber.substring(0, stringNumber.length() - 4);

  return stringNumber;
}

int getTemperature(String dummy)
{
  return (int)(currentTemperature * 100);
}

int getVersion(String dummy)
{
  return version;
}

/*******************************************************************************
 * Function Name  : pulseLed
 * Description    : light up the led
 * Return         : 0
 *******************************************************************************/
int pulseLed(String args)
{
  digitalWrite(LED, HIGH); // sets the LED on
  delay(500);              // waits a bit
  digitalWrite(LED, LOW);  // sets the LED off
  return 0;
}

Credits

gusgonnet

gusgonnet

37 projects β€’ 306 followers
With a decade of Software Engineering experience in IoT, I focus on creating Particle IoT solutions coupled with mobile and web applications

Comments