This repository was archived by the owner on Mar 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsense.py
85 lines (67 loc) · 2.08 KB
/
sense.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from sense_hat import SenseHat
from datetime import datetime
import boto3
import time
import json
import subprocess
try:
from local_settings import REGION
except ImportError:
from default_settings import REGION
iot = boto3.client('iot-data', region_name=REGION)
sense = SenseHat()
def get_raspid():
# Extract serial from cpuinfo file
cpuserial = "0000000000000000"
with open('/proc/cpuinfo', 'r') as f:
for line in f:
if line[0:6] == 'Serial':
cpuserial = line[10:26]
return cpuserial
sensor_id = get_raspid()
delay_s = 60
topic = sensor_id + '/sensehat/data'
# get the data from the accelerometer
def get_accelerometer():
acceleration = sense.get_accelerometer()
print("Pitch: {pitch}, Roll: {roll}, Yaw: {yaw}".format(
**acceleration
)
)
return acceleration
def get_humidity():
humidity = sense.get_humidity()
print("Humidity: %s %%rH" % humidity)
return humidity
def get_temperature():
# temperature = sense.get_temperature_from_humidity()
# print("Temperature: %s C" % temperature)
# Read the sensors
temp_c = sense.get_temperature()
cpu_temp = subprocess.check_output("vcgencmd measure_temp", shell=True)
array = cpu_temp.split("=")
array2 = array[1].split("'")
cpu_tempf = float(array2[0]) * 9.0 / 5.0 + 32.0
cpu_tempf = float("{0:.2f}".format(cpu_tempf))
# Format the data
temp_f = temp_c * 9.0 / 5.0 + 32.0
temp_f = float("{0:.2f}".format(temp_f))
temp_c = float("{0:.2f}".format(temp_c))
return temp_c
def get_pressure():
pressure = sense.get_pressure()
print("Pressure: %s Millibars" % pressure)
return pressure
while True:
payload = get_accelerometer()
payload['humidity'] = get_humidity()
payload['temperature'] = get_temperature()
payload['pressure'] = get_pressure()
payload['device_id'] = get_raspid()
payload['datetime'] = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
print(payload)
response = iot.publish(
topic=topic,
payload=json.dumps(payload)
)
time.sleep(5)