jasir
Published © Apache-2.0

Sending Temperature Sensor Data to Azure DB

This project uses a Raspberry Pi B+ attached to 2 temperature sensors and sends data to an Azure data source.

BeginnerFull instructions provided2 hours2,726
Sending Temperature Sensor Data to Azure DB

Things used in this project

Story

Read more

Code

read sensor

Python
The Python program prints out the temperature in degrees C every 5 seconds
import os
import glob 
import time 
import thread 
import socket 
from datetime import datetime 

os.system('modprobe w1-gpio') 
os.system('modprobe w1-therm')

base_dir = '/sys/bus/w1/devices/' 
device1_file = glob.glob(base_dir + '28*')[0] + '/w1_slave'
device2_file = glob.glob(base_dir + '28*')[1] + '/w1_slave'

def read_temp_raw(dfile):
    f = open(dfile, 'r')
    lines = f.readlines()
    f.close()
    return lines 

def read_temp(dfile):
    lines = read_temp_raw(dfile)
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw(dfile)
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        return temp_c

host = socket.gethostname() 
while True:
    try:
	temp1 = read_temp(device1_file)
	temp2 = read_temp(device2_file)
        body = '{ \"DeviceId\": \"' + host + '\" '        
	now = datetime.now()
	body += ', \"rowid\": ' + now.strftime('%Y%m%d%H%M%S')
        body += ', \"Time\": \"' + now.strftime('%Y/%m/%d %H:%M:%S') + '\"'
	body += ', \"Temp1\":' + str(temp1) 
        body += ', \"Temp2\":' + str(temp2) + '}'
	print body
        
        time.sleep(5)
    except Exception as e:
        print "Exception - ", repr(e)

step4 code

SQL
Using SQL Server Management Studio, or some other database management tool to connect to the database and create a table. You can use the following script:
CREATE TABLE [dbo].[SensorDemo1](	
	[rowid] [int] NOT NULL,
	[DeviceId] [nchar](30) NULL,
	[Time] [datetime] NULL,
	[Temp1] [float] NULL,
	[Temp2] [float] NULL,
PRIMARY KEY CLUSTERED 
(
	[rowid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)

GO

step 6

ABAP
Install the Azure API and other prerequisite libraries by executing the following commands within a terminal window on your Pi.
$ sudo apt-get install gcc cmake uuid-dev libssl-dev
$ wget sourceware.org:/pub/libffi/libffi-3.2.1.tar.gz
$ tar -zvxf libffi-3.2.1.tar.gz 
$ cd libffi-3.2.1/
$ ./configure
$ sudo make install 
$ sudo ldconfig
$ cd ~
$ sudo apt-get install python-pip
$ sudo pip install requests[security]
$ sudo pip install certifi urllib3[secure] pyopenssl ndg-httpsclient pyasn1 azure
$ sudo apt-get install python-openssl

Credits

jasir

jasir

4 projects • 22 followers
iot develeper

Comments