The Spark Core IOT Temperature Sensor with the DS18b20


Spark Core wired to the DS18b20 Temperature sensor

Spark Core and the DS18b20 OneWire sensor

This project outlines how to create a simple Temperature Monitor with the Spark Core Microcontroller using just a DS18b20 sensor, a 4.7kOhm resistor, a few wires, and a breadboard.  It is extended using the web service IFTTT (IF This Then That) to create a real-time temperature alerting system.

The Spark Core works well as an IOT (Internet of things) Temperature monitor because it has a simple RESTful interface so you can easily “ask” it a question from any other internet enabled device (such as computer, phone or another IOT device).  It has 2 onboard processors. One processor maintains the internet connection and the other runs your code as a separate thread, so you dont have to worry about maintaining the connection to the web in code.

I am using the OneWire DS18b20 to measure the temperature.  The datasheet can be found here:

http://datasheets.maximintegrated.com/en/ds/DS18B20.pdf

The DS18b20 is a very accurate sensor and can be used with multiple sensors all sharing the same digital pin.  This means that a microcontroller like the Arduino can monitor several sensors with just one data pin.  You need to connect just the power pins (3v and ground) and the signal pin to the D2 pin on the Spark Core.  You also need a 4.7K resistor as a pullup resistor between the VDD and the signal pin (pin 2 or D).  Each sensor outputs its address and basic info on the signal line and a low level request can be made to “ask” the sensor for its temperature.

ds18b20

the lower image on the right show the sensor from the bottom, think of the flat edge as the front of the sensor

taken from the wiring to arduino examples, but it shows how the resistor must be attached

*if you wire the sensor in backwards it will quickly heat up and burn your skin, but I have found that they work even after that, your mileage may vary.  Double check your wiring before powering the Spark Core.

The basic wiring for the Spark Core looks like this:

SparkCore  -> DS18b20

GND ———-> GND (pin 1)

D2 ————> D (pin 2)

3v ————> VDD (pin 3)

You also need to connect the D and VDD pins on the DS18b20 with a 4.7kOhm resistor (as a pull-up resistor, see images above).  The wiring is very simple, the only caveat is that you should keep the sensor away from the Spark Core because it does give off a small amount of heat that could affect your results.


 

Code 

My code is a fork from another repo so that I could add the ability to do event publishing and output a DOUBLE so that the IFTTT value comparisons would work.  My code is available here:

https://github.com/contractorwolf/SparkCoreDS18B20

The code is using a Spark Core public “variable” to allow values that are seen in the code to be publicly available through the RESTful interface.  This means that if you define a variable like this:

Spark.variable(temperature, &temperature, DOUBLE)

you can then access it remotely using this sort of HTTP request:

https://api.spark.io/v1/devices/{DEVICE_ID}/temperature?access_token={ACCESS_TOKEN}

(you will need to replace the {DEVICE_ID} and {ACCESS_TOKEN} with real values from your microcontroller, find them in your Settings and Cores section on http://spark.io)

I also am creating a published event in code to allow for external devices to subscribe to.   When external device are subscribing to the event they will receive a message from the device every time the event occurs.

In my code you will see this:

Spark.publish(“temperatureInfo”, temperatureInfo);

when the device receives new temperature data every 5 seconds.  To subscribe to this event you can use cURL and listen on the command line with a cURL command like this:

curl -H “Authorization: Bearer {ACCESS_TOKEN}” https://api.spark.io/v1/events/temperatureInfo

(again, you must replace the {ACCESS_TOKEN} with your own, found in settings on the web IDE)


 

IFTTT

If you dont already know about IFTTT it is a web service that nicely connects to thousands of other services.  Everything from from your email to the Spark Core can have events and actions.  My idea was to make a web based IOT temperature monitor that could alert you when a threshold was reached.  IFTTT can quickly be attached to your Spark Devices so that when one of your public endpoints did something an action could be triggered.

For this project I figured a threshold would fire an SMS message when it was crossed would be a good IOT project.  I created an IFTTT recipe that sent me a text message when the threshold of 50 fahrenheit.  With just a few clicks I had alerting temperature monitor that was accessible from anywhere and could send sophisticated alerts when custom thresholds were reached.  All for basically no money.

The IFTTT Recipe is here:

IFTTT Recipe: Spark Core SMS Temperature Alert System connects spark to sms

To use this recipe you simple need to install the IFTTT app on your phone, connect IFTTT to your Spark account, select the spark device from your list, choose the threshold, and set the SMS message.  It takes about 20 seconds to setup and it is all menu driven, so it is very easy.


 

Hardware

http://spark.io

http://www.amazon.com/gp/product/B00FR1BUUG

Github repo:

https://github.com/contractorwolf/SparkCoreDS18B20

IFTTT:

https://ifttt.com/recipes/267433-spark-core-sms-temperature-alert-system

Datasheet:
http://datasheets.maximintegrated.com/en/ds/DS18B20.pdf

 

 

6 Responses to The Spark Core IOT Temperature Sensor with the DS18b20

  1. Joan Rowe says:

    Thank you for posting this! I am a complete NOOB and have not been able to find any information on how to flash multiple applications (.cpp) onto the spark core via build.spark.io

    Would you kindly explain how I would flash my spark core with your multiple .cpp’s please?

    • Joan Rowe says:

      It seems I was confusing .cpp’s with .ino and I was able to compile and flash the application to my spark core.

      I am unsure where in the code to define the variable:

      Spark.variable(“temperature“, &temperature, DOUBLE)

    • james wolf says:

      I believe that there is an icon in the upper right of the web ide that lets you add additional files. Add the .h and .cpp files using that. I was trying to create the necessary libraries needed to communicate with the DS18b20 so that the main .ino file was clean.

  2. james wolf says:

    I am sorry that I just noticed this comment. You would initialize the above temperature Spark variable in the setup() function. And yes in that github repo I also have the other libraries, but you really only need to pay attention to the onewire-ds18b20.ino file.

  3. Derek says:

    hello
    I tried to use multiple DS18B20 sensors with a Spark Core but not with good results!
    Please, if you know, you can take some minutes to explain how do that?
    How make the request for each sensors’ “id”?
    Thx
    Derek

    • james wolf says:

      Take a look at the GitHub code I have on the post. Make sure you are grabbing all the files. The code looks for the first sensor it finds on that data pin. I have 3 of these running in my house 24/7. Try my code running a single temp sensor first.

Leave a reply to Joan Rowe Cancel reply