Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Raspberry Pi Home Automation with Arduino - Second Edition
  • Table Of Contents Toc
Raspberry Pi Home Automation with Arduino - Second Edition

Raspberry Pi Home Automation with Arduino - Second Edition - Second Edition

By : Andrew K. Dennis
3 (5)
close
close
Raspberry Pi Home Automation with Arduino - Second Edition

Raspberry Pi Home Automation with Arduino - Second Edition

3 (5)
By: Andrew K. Dennis

Overview of this book

If you are new to the Raspberry Pi, the Arduino, or home automation and wish to develop some amazing projects using these tools, then this book is for you. Any experience in using the Raspberry Pi would be an added advantage.
Table of Contents (11 chapters)
close
close
4
4. Temperature Storage – Setting Up a Database to Store Your Results
10
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...

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Raspberry Pi Home Automation with Arduino - Second Edition
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon