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...