Raspberry-Pi Tutorial

Connecting a temperature sensor

Introduction

This tutorial introduces the basics of how to quickly deploy an Internet of Things project with a working example of a Connected Product.

The example illustrates how to connect a temperature sensor to the CloudPlugs service through a Raspberry Pi board using the internal Ethernet controller.

You will need to open an account at CloudPlugs so you can create a prototype in your account. The example shows how to configure the Raspberry Pi with a connectivity library to connect to CloudPlugs. Once all the software is configured and the sensor is connected, the BeagleBone Black will upload and publish data every minute.

This guide has the same objectives as the Arduino tutorial.

Requirements

You will need to set up your Raspberry Pi and sensor hardware, your CloudPlugs account and your Raspberry Pi software so you can monitor published data from the CloudPlugs web desktop.

Hardware

The Raspberry Pi does not have an ADC (Analog-to-Digital Converter). Therefore, to extract data from an analog sensor such as the TMP35/36/37, we need to use an ADC such as the Microchip MCP3008. This is a low cost and easy to connect device which does not require additional components. It uses the SPI bus protocol which iso fully supported by the Pi’s GPIO header.

mcp3008

The following image shows the circuit already wired.

raspberry_pi_tmp36

Boards

  • Raspberry Pi model B;

Components

Enable Hardware SPI Support on the Raspberry Pi

The first step required to start building your things using the SPI interface is to enable the hardware SPI on the Raspberry PI. This exercise will use a Python SPI wrapper. To learn more about the SPI bus, please visit the Serial Peripheral Interface Bus Wikipedia page.

  1. Enabling the SPI kernel module

To enable hardware SPI on the Raspberry Pi we need to modify one of the system files:

sudo nano /etc/modprobe.d/raspi-blacklist.conf

Now proceed as follows:

  • Move to the line containing the string "spi-bcm2708;
  • Add a ‘#’ character in front of this line;
  • User CTRL-X, then Y to save and exit from the nano editor;

Now issue a reboot command:

sudo reboot

At the end of the reboot process, login to the Raspberry Pi and, to check if the change is operational, run this command:

sudo lsmod

You should be able to see “spi_bcm2708” listed in the output of the command.

  1. Install the Python SPI Wrapper

To read data from the SPI bus using Python, please follow the steps below in the order listed:

  1. Install ‘python-dev’;
  2. Install the ‘py-spidev’ library;
sudo apt-get install python-dev

The to finish we can download ‘py-spidev’ and compile it ready for use:

mkdir py-spidev

cd py-spidev

wget https://raw.github.com/doceme/py-spidev/master/setup.py

wget https://raw.github.com/doceme/py-spidev/master/spidev_module.c

sudo python setup.py install

Software

  1. Use the CloudPlugs Python library.
  2. Use the CloudPlugs Web Desktop to configure your device in the cloud.

Connecting the Raspberry Pi, the ADC and the sensor using the Python Library

There are a few simple steps to follow to connect the Raspberry Pi and to start publishing data:

  1. Register at www.cloudplugs.com;
  2. Enable the hardware SPI support on your Raspberry Pi as indicated above.
  3. Login to the CloudPlugs platform and create a Prototype.
  4. Write down or copy to your clipboard the Prototype’s Plug-ID displayed in General->Device Profile.
  5. Download the Python library and uncompress it into your preferred folder on the Raspberry Pi.
  6. Use one of the examples included in the Python library. In the Raspberry Pi root shell go to the folder where you downloaded the CloudPlugs Python library and use your preferred command-line editor to edit the file sample.py
  7. Edit the source code with the changes shown below
  1. Change AUTH_PLUGID to the Plug-ID provided by the platform and listed in the Prototype->General->Device Profile->General Properties panel.
  2. Either change the authentication password AUTH_PASS to a new password if you set it on the platform Prototype->General->Device Profile->Permissions->Set password, or leave the default which is your CloudPlugs login password (only in this case set the AUTH_MASTER variable to True).
  1. Connect your hardware
  1. Connect the TMP36 to the analog input of the ADC (follow the connections shown in the breadboard picture above) and then connect it to the Raspberry Pi digital IO ports (GPIO);
  2. Connect the Raspberry Pi board to your network using an ethernet cable.  You can power it using a micro USB cable connected to your computer or by using the board’s external power adapter;
  1. From the command-line run:
root@pi:~# python ./sample.py
  1. Temperature data will be published every minute in the Published Data panel under Prototype->Publish data. To change temperature values easily, lightly press the sensor between your fingers.
  2. Congratulations! you have successfully connected a Thing (temp sensor) through your Raspberry Pi to the CloudPlugs platform;

Source Code

Use the following source code:

1.    #!/usr/bin/python
2.    
3.    import spidev
4.    import time
5.    import os
6.    import sys
7.    import json
8.    
9.    # Importing CloudPlugs python library
10.   from cloudplugs import CloudPlugs
11.    
12.   # Open SPI bus
13.   spi = spidev.SpiDev()
14.   spi.open(0,0)
15.    
16.   AUTH_PLUGID = 'dev-00112233445566778899AABB'    # Enter your Plug-ID here
17.   AUTH_PASS = 'password'                     # Enter your authentication password for the device here 
18.   AUTH_MASTER = False # change to True, if you want to authenticate the device using your login password.
19.    
20.   # Define MCP3008 analog reading channel, Define readings delay
21.   TEMP_CHANNEL  = 0
22.   DELAY = 5
23.    
24.   # Initializing CloudPlugs stuff
25.   cps = CloudPlugs()
26.   cps.set_auth(AUTH_PLUGID, AUTH_PASS, AUTH_MASTER)
27.    
28.   # Function to read SPI data from MCP3008 chip
29.   # Channel must be an integer 0-7
30.   def ReadChannel(channel):
31.     adc = spi.xfer2([1,(8+channel)< <4,0])
32.     data = ((adc[1]&3) << 8) + adc[2]
33.     return data
34.    
35.   # Function to calculate temperature from
36.   # TMP36 data, rounded to specified
37.   def ConvertTemp(data,places):
38.     # tmp36 conversion from [0-1023] to [-50 -> 280 Celsius Deg.]
39.     temp = ((data * 330)/float(1023))-50
40.     temp = round(temp,places)
41.     print 'temp [%d]C\r'%temp
42.     return temp
43.    
44.   while True:
45.     # Read the temperature sensor data
46.     temp       = ConvertTemp(ReadChannel(TEMP_CHANNEL),2)
47.     cp_res = cps.publish_data("weather", { "data":{ "temperature":temp }})
48.     # Wait before repeating loop
49.     time.sleep(DELAY)

The MCP3008 is a 10bit ADC and it returns numbers ranging from 0 to 1,023. This interval is linearly proportional to the voltage. A reading of 0 means that we’re reading 0V and a reading of 1,023 means the input is 3.3V. According to the TMP36 specifications, these values would equate to a temperature range of -50 to 280 degrees Celsius.

Still need help? Get in touch!
Last updated on 23rd May 2019