Raspberry Pi

In this article we will try to use CommonSense to store ouput and also get input of Raspberry Pi's General Purpose Input Output (GPIO). The Raspberry Pi has 17 GPIO pins. These pins can be used as a digital input or output. We will try to use these pins as a sensors and store or retrieve data of that pin to CommonSense.

This demonstrator shows two ways in which you can use CommonSense as part of your Raspberry Pi system.

  1. Use CommonSense to store output from the Raspberry Pi
  2. Read sensor data from the outside world from CommonSense in the Raspberry Pi

Contents

  1. Requirements
  2. Install Python on Raspberry Pi
  3. Prepare CommonSense Account
  4. Store Raspberry Pi Output at CommonSense
  5. Read CommonSense as Input for Raspberry Pi

Requirements

  • Raspberry Pi with a Linux OS installed. In this case we use the Raspbian OS which is a Debian based distribution.
  • Raspberry Pi connected to the internet and accessible via SSH
  • LED
  • 200Ω, 1kΩ, 10kΩ resistors
  • Push button switch
  • A couple of jumpers
  • Account on CommonSense. Create a new account for free here.

Install Python on Raspberry Pi

We will be using python and use the wiringpi2 library to control the GPIO pin. To communicate easily with CommonSense API we will be using the python client library.

Python should already be available on the default repository. First step is to SSH into your Raspberry Pi. Let's say for example the IP address is 192.168.1.2 and the default username is piname:

$ ssh 192.168.1.2 -l piname

Now we need to install python and all of its dependencies:

$ sudo apt-get update
$ sudo apt-get install python-dev python-setuptools
$ sudo apt-get install python-pip
$ sudo pip install wiringpi2

Next step is getting the CommonSense client library for python. Note that we need the oauth library installed even though we are not using it on this experiment.

$ sudo pip install oauth
$ wget https://raw.github.com/senseobservationsystems/commonsense-python-lib/master/senseapi.py

Important:

There are a couple of ways to setup GPIO pin. The easiest way is to use wiringpi2.wiringPiSetupGpio method, but this require the python script to be run as root (sudo). If you want to run the script without the root privilegs, you may want to export the pin using wiringpi. There is an article here that guides you through the process. This article uses the first method so you need to run python with sudo rights.

Prepare CommonSense Account

Now that we prepared the Rasperry Pi, we also need to prepare the account at CommonSense. We want to create two sensors: one sensor for input from our switch and one for output to the Pi. These sensors will be linked to GPIO pin 17 for input and 18 for output.

import senseapi

api = senseapi.SenseAPI()

# login to CommonSense
api.Login('username', senseapi.MD5Hash('password'))

Now we create a new sensor called input17. For details about creating a new sensor, please refer to the documentation here.

# create a new sensor for input
sensor_parameter = {'sensor': {'display_name': 'GPIO pin 17 Input', 'name': 'input17', 'data_type': 'Integer', 'device_type': 'raspberry-pi'}}

api.SensorsPost(sensor_parameter)
# => True

input_sensor_id = api.getLocationId()
print input_sensor_id
# => 12345

# now for output
sensor_parameter = {'sensor': {'display_name': 'GPIO pin 18 output', 'name': 'output17', 'data_type': 'Integer', 'device_type': 'raspberry-pi'}}

api.SensorsPost(sensor_parameter)
# => True

output_sensor_id = api.getLocationId()
print output_sensor_id
# => 54321

Store Raspberry Pi Output at CommonSense

Okay, so we have prepare the Raspberry Pi, and set up an account on CommonSense with one sensor for input and one for output. It is time to get some data from the Raspberry Pi to CommonSense!

we will use the pin 17 that is connected to a switch (push button) and we will store the state to CommonSense. The script will send data 1 meaning that the switch is being pushed or 0 when it is not. Later we will be able to plot the data to a graph base on time at CommonSense front end. If you are not familiar with GPIO pins on the Raspberry Pi, there is an excellent introduction and tutorial provided by Adafruit.

For the circuit, we will try to follow the schema created by Computer Laboratory Faculty of Computer Science and Technology, Cambridge. It is actually quite simple. We only need to connect the push button to the Raspberry Pi with a couple of resistors. You are free to store any data you want in CommonSense, so this is the part where you substitute this boring circuit with a fancy device of your own.

schema

Now run the python interpreter. The python will need root (sudo) access to be able to control the GPIO pin. If you want to run the script as a normal user there is a tutorial in the beginning of this article.

$ sudo python

First we need to log in to CommonSense. If you do not have account already, create new account here for free.

import senseapi

api = senseapi.SenseAPI()

# login to CommonSense
api.Login('username', senseapi.MD5Hash('password'))

At the beginning of this article we have made a new sensor called output18 with sensor ID 54321. Now we want to sample the pin every second. We will write the data as 1 if the push button is pressed and 0 if its not

import time, json, wiringpi2

output_sensor_id = 54321

# setup GPIO
wiringpi2.wiringPiSetupGpio()
wiringpi2.pinMode(18, 0) # setting pin 18 mode to INPUT

sensor_id = 54321

while True:
  value = wiringpi2.digitalRead(18)
  print value
  data_on = {'data':[{'value': value, 'date': time.time()}]}
  api.SensorDataPost(output_sensor_id, data_on)
  time.sleep(0.5)

Now you can plot graph of your sensor data in the CommonSense Front end.

commonsense-output

Read CommonSense as Input for Raspberry Pi

For the final step in this demonstration, we want to get data from CommonSense and use it in our Raspberry Pi. For this step we will follow the schema from Gordon the creator or wiring-pi library. The complete schema and explanation can be found here

led circuit

We will try to use pin 17 as output to the LED and use read sensor from CommonSense to turn the lights on or off. The script will poll the data from commonSense every second and only retrieve the last data point.

To run the python interpreted with root access:

$ sudo python

First we need to log in to CommonSense. If you do not have account already, create new account here for free.

import senseapi

api = senseapi.SenseAPI()

# login to CommonSense
api.Login('username', senseapi.MD5Hash('password'))

At the beginning of this article we have created a new sensor called input17 with sensor ID 12345. Now we want to use that sensor as an input of the GPIO port 17.

import time, json, wiringpi2

input_sensor_id = 12345

# setup GPIO
wiringpi2.wiringPiSetupGpio()
wiringpi2.pinMode(17,1) # setting pin 17 mode to OUTPUT

while True:
  data_parameter = {'page':0, 'per_page':1, 'sort':'DESC'}
  api. SensorDataGet(input_sensor_id, data_parameter)
  response = json.loads(api.getResponse())

  if response['data']:
    data = response['data'][0]
    value = int(data['value'])
    wiringpi2.digitalWrite(17,value)

  time.sleep(5)

Now, on another terminal, let's open a new interpreter. You can open a new SSH session to the Raspberry Pi. We will now try post a data to sensor with ID of 12345 turn on the light. We do not need sudo this time.

$ python
import senseapi, time

api = senseapi.SenseAPI()

# login to CommonSense
api.Login('username', senseapi.MD5Hash('password'))

sensor_id = 12345

# turn on the light
data_on = {'data':[{'value':1, 'date': time.time()}]}
api.SensorDataPost(sensor_id, data_on)

# turn off the light
data_off = {'data':[{'value':0, 'date': time.time()}]}
api.SensorDataPost(sensor_id, data_off)

Congratulations! You have now connected your Raspberry Pi with CommonSense.