Book Image

Raspberry Pi Home Automation with Arduino - Second Edition

By : Andrew K. Dennis
Book Image

Raspberry Pi Home Automation with Arduino - Second Edition

By: Andrew K. Dennis

Overview of this book

Table of Contents (16 chapters)
Raspberry Pi Home Automation with Arduino Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
4
Temperature Storage – Setting Up a Database to Store Your Results
Index

Python code


The following Python code is a modified version of the request.py code that we used previously.

Create a new file, damp.py, in a text editor of your choice and add the following code. Remember to change the IP address given here to that of your Arduino:

#!/usr/bin/env python
import sqlite3
import urllib2
import json

def main():
    req = urllib2.Request('http://192.168.3.6/')
    req.add_header('Content-Type', 'application/json;charset=utf-8')
    r = urllib2.urlopen(req)
    result = json.load(r)
    room = result['thermostat'][0]['location']
    temperature = result['thermostat'][1]['temperature']
    humidity = result['thermostat'][1]['humidity']
    my_query = 'INSERT INTO temperature(roomid,temperaturec,datetime, humidity) \
                VALUES(%s,%s,CURRENT_TIMESTAMP);' %(room,temperature, humidity)

Here, we can see that we are storing the humidity value returned in the json object in a variable called humidity.

Next, we insert this value into the query that writes the...