Raspberry Pi to the PI System

Blog Post created by dng on Nov 12, 2015

Yesterday, Barry posted a blog post about writing to the PI System using the Arduino Yun. I figured I’ll join in this Internet of Things fun by posting my experience on writing to PI using Raspberry Pi.




Similar to Barry’s set up, I am collecting temperature data via a temperature sensor. The temperature data is then sent to the PI System by posting to a public PI Web API endpoint. Instead of Arduino Yun, we are using Raspberry Pi here.


Here is the setup:


  • Sensor: DS18B20 digital temperature sensor
    • The sensor was set up according to this tutorial with the help of James Dryden (thanks James!)
  • Development system: Raspberry Pi
    • I am using an older model of Raspberry Pi (Model B) running Raspbian.
  • Gateway device: Digi TransPort WR21 wireless router
    • This is a cellular router, allowing us to send data to the PI server via the cellular LTE network. This potentially allow us to install our sensor at any location as long as cellular network is available!
  • PI System: PI Web API with PI Data Archive 2015.
    • The PI Data Archive, as well as the PI Web API instance, is hosted on Azure. The PI Web API endpoint is publicly exposed.



Python Script

Once everything is set up, I used a simple Python script to write the temperature data to PI every 5 seconds.


WriteTemp.py (GitHub gist here):


import requests
import time
import os
import glob
import re

base_url = '
https://<PI Web API server>/piwebapi

webId = '<WebId to PI Tag>'
base_dir = '/sys/bus/w1/devices/'
yes_pattern = re.compile(r'YES')
temp_pattern = re.compile('t=(\d+)')

def initialize_sensor():
os.system('sudo modprobe w1-gpio')
os.system('sudo modprobe w1-therm')

def get_sensor_file():
device_folder = glob.glob(base_dir + '28-*')[0]
return device_folder + '/w1_slave'

def read_temp_raw(file):
f = open(device_file,'r')
lines = f.read()
return lines

def read_temp(file):
lines = read_temp_raw(file)
while not yes_pattern.search(lines):
lines = read_temp_raw(file)
temp_string = temp_pattern.search(lines).group(1)
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_f

def post_pi_value(value):
data = {'Value': value}
headers = {'Content-Type': 'application/json'}
response = requests.post(base_url + '/streams/' + webId + '/value', json=data, headers=headers, verify=False)
return response

if __name__ == '__main__':
device_file = get_sensor_file()
while True:
value = read_temp(device_file)
response = post_pi_value(value)


In this script, I’m posting the temperature value to the PI Web API Stream controller > UpdateValue action every 5 seconds (timestamp is current time by default). The post_pi_value function is all we need to write to PI Web API! For more information on how to work with HTTP requests in Python, check out the Python library Requests. Regarding the code on on how to get temperature reading from the sensor, check out the tutorial for the DS18B20 temperature sensor.



Running on startup

Finally, I set this Python script to run on startup by editing the rc.local file at /etc/rc.local by including this line:


sudo python3 /path/to/WriteTemp.py > /dev/null 2>&1 &




Running the script (which occurs automatically after rebooting the Raspberry Pi), we can see live temperature readings in the PI Data Archive!


On a normal day in the office:


On a particular warm day in the office:


We can clearly see the temperature going up at the beginning of business day! (heater kicking off? when people starting to come into the office?)



Through these simple set ups, we hope to show you the possibility of integrating the PI System with these IoT devices. Let us know your experience with IoT + PI!