Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Raspberry Pi Home Automation with Arduino - Second Edition

You're reading from  Raspberry Pi Home Automation with Arduino - Second Edition

Product type Book
Published in Feb 2015
Publisher
ISBN-13 9781784399207
Pages 148 pages
Edition 1st Edition
Languages
Author (1):
Andrew K. Dennis Andrew K. Dennis
Profile icon Andrew K. Dennis

Table of Contents (16) Chapters

Raspberry Pi Home Automation with Arduino Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. An Introduction to the Raspberry Pi, Arduino, and Home Automation 2. Getting Started – Setting Up Your Raspberry Pi and Arduino 3. Central Air and Heating Thermostat 4. Temperature Storage – Setting Up a Database to Store Your Results 5. Parcel Delivery Detector 6. Curtain Automation – Open and Close the Curtains Based on the Ambient Light 7. Water/Damp Detection – Check for Damp/Flooding in Sheds and Basements 8. Wrapping Up References 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...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}