This repository was archived by the owner on Apr 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwriter.py
53 lines (43 loc) · 1.63 KB
/
writer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sqlite3 as lite
import sys
import time
import datetime
from sense_hat import SenseHat
SLEEP = 300
sense = SenseHat()
sense.clear()
def init_db():
"""Connects to the specific database."""
con = lite.connect("sensehat.db")
cur = con.cursor()
query = """CREATE TABLE IF NOT EXISTS
sensehat(epoch INT,
humidity REAL,
pressure REAL,
temp_hum REAL,
temp_prs REAL)"""
cur.execute(query)
if __name__ == "__main__":
humidity = round(sense.humidity, 1)
pressure = round(sense.get_pressure(), 2)
temperature_from_humidity = round(sense.get_temperature(), 1)
temperature_from_pressure = round(sense.get_temperature_from_pressure(), 1)
init_db()
while 1:
humidity = round(sense.humidity, 1)
pressure = round(sense.get_pressure(), 2)
temperature_from_humidity = round(sense.get_temperature(), 1)
temperature_from_pressure = round(
sense.get_temperature_from_pressure(), 1)
con = lite.connect("sensehat.db")
with con:
print("%s - Inserting data." % datetime.datetime.now().isoformat())
cur = con.cursor()
command = "INSERT INTO sensehat VALUES(%i,%0.2f,%0.2f,%0.2f,%0.2f)" % (int(
time.time()), humidity, pressure, temperature_from_humidity, temperature_from_pressure)
cur.execute(command)
print("%s - Sleeping for %i seconds." %
(datetime.datetime.now().isoformat(), SLEEP))
time.sleep(SLEEP)