DS18B20 Temperature Measurement with Spark Core

In our blog post Spark Core Introduction and First Impressions we introduced Spark Core – a Wi-Fi enabled Internet of Things device which can be programmed like an Arduino and accessed via the internet.

Spark Core Wi-Fi Open Source IoT development board

Of most interest to us at REUK is using Spark Core to enhance our range of solar water heating controller adding datalogging and internet functionality. Therefore we want to access temperature readings from DS18B20 digital temperature sensors of the type used in our 2014 Solar Water Heating Pump Controller connected to Spark Core.

As a test we connected a DS18B20 temperature sensor to the Spark Core. Pin 1 of the sensor connects to GND, Pin 3 to 3.3V, and Pin 2 to a digital pin on Spark Core – we randomly chose D2. Finally we connected a 4K7 resistor across Pins 1 and 3 of the sensor and entered the following code via the Spark IDE to flash to the Spark Core:

#include "spark-dallas-temperature/spark-dallas-temperature.h"
#include "OneWire/OneWire.h"
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensor(&oneWire);

float temperature = 1.0;
char myStr[10];

void setup() {
 Spark.variable("read", &myStr, STRING);
 sensor.begin();
 sensor.setResolution(12);
}

void loop() {
 sensor.requestTemperatures();
 temperature= sensor.getTempCByIndex(0);
 sprintf(myStr,"%.3f",temperature);
}

At the time of writing (August 2014) it is not possible to have a Spark.variable which is a float – the code just will not compile – so the temperature measurement from the sensor (which is a float/double) must either be saved as an integer (losing accuracy due to rounding) or be converted into a string (which is what we did above to three decimal places with the sprintf function) so it can be accessed remotely.

We then wrote the following Python script on an internet connected Raspberry Pi to grab the temperature measurement once every minute and to append it to a text file for datalogging and later analysis:

#!/usr/bin/python

import urllib2
import json
import time

var = 1
while var == 1:
   response = urllib2.urlopen('https://api.spark.io/v1/devices/YOURDEVICEID/read?access_token=YOURACCESSTOKEN')
   html = response.read()
   reading = json.loads(html)
   temperature = reading['result']
   with open("core-temp-log.txt", "a") as myfile:
      myfile.write(temperature)
      myfile.write('\n')
   myfile.close();
   time.sleep(60)

The string variable read is the string conversion of the value read in by the temperature sensor.

Having got the Spark Core successfully reading data from a DS18B20 it is possible to fully replicate our Arduino based solar water heating pump controllers with the added benefit of internet connectivity and effective remote datalogging.

See our Raspberry Pi related articles Publish Temperature Sensor Readings to Twitter and Temperature Logger with Xively to find out how to automatically publish your collected data to the internet – either as a Twitter feed or with Xively as an online datalogger with graph plotting etc.

Spark Core Introduction and First Impressions

Spark Core is described as an Open Source IoT (Internet of Things)Toolkit.  It is a small Wi-Fi development board which connects automatically to servers in the cloud, and can be programmed and controlled remotely over the internet and also send data to the cloud where you can access it.

Spark Core Wi-Fi Open Source IoT development board

The Spark Core board is programmed using Wiring – the same programming language used with Arduino – but via a browser based IDE. Therefore you do not physically connect the board to your PC. Instead you just power it, it connects to your Wi-Fi automatically (with credentials entered during a one off setup process), and then automatically connects to the Spark servers. You then write your Wiring code in your web browser, it is checked and compiled on the Spark servers and the code is then flashed to your board over Wi-Fi and starts running.

Each Spark Core has a unique device ID with an associated secret access code so no-one else can take over your Core or access the data from it.

To try out Spark Core, we put together a very simple setup just to measure the ambient light level.

Spark Core light detector test circuit

Spark Core is supplied with its own prototyping breadboard. We connected a light dependent resistor (LDR) to one of the regulated 3.3V output pins, and to one of the ground pins via a 10K resistor. This creates a voltage divider (where the LDR meets the resistor), the output of which we connected to analog pin A4. (In the photograph above, we also have an LED connected via a current limiting resistor to digital pin D0).

The analog pins on the Spark Core are 12-bit analog to digital converters (ADC). Therefore, they measure the voltage on the pin and give it a proportional digital value from 0 to 4095 where 0 is 0V and 4095 is 3.3V.

const int ldrpin = A4;
int lightlevel = 0;

void setup(){
   pinMode(ldrpin, INPUT);
   Spark.variable("lightlevel", &lightlevel, INT);
}

void loop(){
   lightlevel = analogRead(ldrpin);
}

Above is the Wiring code we wrote to continuously save the digital conversion of the measured voltage on pin A4 (here called ldrpin), as a variable lightlevel. Defining the Spark.variable lightlevel in setup makes it accessible via the Spark servers.

With that code flashed to the Spark Core and running, you can now instruct the Spark servers to grab that variable (with the Spark API). The simplest way to grab the lightlevel variable is to enter a URL in your web browser like this:

https://api.spark.io/v1/devices/YOURDEVICEID/lightlevel?access_token=YOURACCESSCODE

…obviously substituting in the device ID and access code for your own Spark Core. The browser will then display something like this:

{
  "cmd": "VarReturn",
  "name": "lightlevel",
  "result": 2961,
  "coreInfo": {
    "last_app": "",
    "last_heard": "2014-08-03T11:21:32.577Z",
    "connected": true,
    "deviceID": "YOURDEVICEID"
  }
}

So, in this example the light level was measured by the Spark Core to be 2961 (meaning that the voltage on the pin was 3.3*(2961/4095) Volts).

Instead of measuring a light level, we could have connected any other digital or analog sensors – temperature sensors for example – pre-processed the collected data on the Spark Core board to be saved as useful values which we could view from anywhere in the world.

For a final test, we wrote a very short Python script on an internet connected Raspberry Pi to grab just the value of lightlevel out of the file returned by Spark and to print it out.

#!/usr/bin/python
import urllib2
import json
response = urllib2.urlopen('https://api.spark.io/v1/devices/YOURDEVICEID/lightlevel?access_token=YOURACCESSCODE
html = response.read()
reading = json.loads(html)
lightlevel = reading['result']
print lightlevel

This was saved as file core.py and run using the command sudo python core.py in the terminal. In under one second, the value of the light level measured on the Spark Core was displayed. With a slightly more complex Python script or using cron the light level could be checked every 5 minutes or other interval and logged to a file for later analysis etc.

All in all, first impressions of Spark Core are very favourable. While we have previously used ethernet shields with Arduino to enable remote control and monitoring over the internet, this has necessitated messing around with broadband router settings and firewalls etc. With Spark Core everything happens automagically which makes things a lot simpler for the average user and opens up many Internet of Things possibilities.

Click here to visit the Spark.io website for more information about Spark Core.

Size Comparison of Pyboard with Raspberry Pi and Arduino UNO

We have just received our Pyboard – a prototyping platform that runs Micropython which is an implementation of the popular Python programming lanuage.

Micropython PyboardAbove is the Pyboard in the hard shell padded case in which it arrived. It looks to be very well made, sturdy, and best of all, physically very small.

Size comparison between Pyboard, Raspberry Pi B+ and Arduino UNOThe photo above shows just how small the Pyboard is in comparison with the Raspberry Pi Model B+, and an Arduino UNO.

We have lots of projects in the pipeline for which we would previously have used a Raspberry Pi, but the simplicity, size, and much lower power consumption of the Pyboard will often make it the obvious option.

Nabduino Remote Access Board

Pictured below is Nabduino – an “open source peer-to-peer remote access embedded board with a user modifiable web-interface developed by Nabto“.

Nabduino board

This small board (available to purchase here: buy Nabduino) can be accessed via a direct encrypted connection through a firewall without the need to change the firewall settings of your router.

If for example you set up a server on a Raspberry Pi – that server will only be visible on your local network unless you change settings on your router to make it visible world wide. If your that server is not set up correctly, then your whole network is at risk – therefore this is best left to experts. Having set up that server, you then need either a static IP address (not usually free of charge) or set up some web services (sometimes free, sometimes not), so that you can find your server when you are outside the local network.

The Nabduino board makes things a lot easier and safer than all that trouble. You simply connect an ethernet cable connected to your local network to the board, connect power to the board, and that is it. Each Nabduino board has a unique ID, and there is a simple web interface which gives you access to the board and its sensors etc accessible from anywhere in the world at XXXX.nabduino.net where XXXX is the ID of your board.

web interface for nabduino board

Via the simple web interface (pictured above), the state of the on board button can be viewed, an on board LED can be controlled, the temperature of the microprocessor can be monitored, five PWM outputs can be set with values from 0-255 for dimming lights or motor control, the status of six analog inputs can be viewed, and thirteen digital pins can be set to input or output and be controlled remotely.

As it stands, Nabduino could be used for simple remote control of a home via relays connected to digital outputs – for example, turning on some lights when you are away from home using your mobile phone or PC. Similarly, very basic home monitoring could be achieved with sensor switches connected to digital inputs. However, much more powerful things can be achieved connecting the Nabduino to an Arduino board so that the Arduino can respond to digital and PWM signals from the Nabduino and carry out any complex logic.

We will be looking at the Nabduino board in much more detail soon. Until then, click here to find out more about Nabto.com.

Pyboard Python for Microcontrollers

Pyboard python for microcontrollersPictured above is the Pyboard – an open source prototyping platform designed and manufactured in the UK. This board with its ARM microcontroller (STM32F405 clocked at 168MHz) is programmed using micropython a low memory usage version of the Python 3 scripting language.

The board has LEDs, microswitches, a built in accelerometer, and 30 general purpose IO connections (including 4 PWM, 14 ADC, I2C, and SPI pins) for connection to external components and analogue/digital sensors for your projects.

The board has 1MB of on board flash memory, 192KB of RAM, and also a micro SD card slot which can be used to store scripts and hold project generated data. It has a built in USB interface.

Pyboard fits in the marketplace somewhere between Raspberry Pi and Arduino. A Raspberry Pi is a full computer which means that it can be complicated to use, power hungry, and large in size. An Arduino is simple to use, has lots of useful GPIO and shields, and they are available in small versions, but they are not very fast and scripts need to be compiled on a PC before loading them to the Arduino. Pyboard is perfect for processor intensive stand alone projects – particularly for anyone who already has experience programming with Python.

Pyboard is just 33 x 40mm in size and weighs just 6g.

The official Micro Python website is here, and the tutorial which shows how to get strarted with Pyboard and Micro Python is here: Micro Python Tutorial.