Last active
October 5, 2020 02:48
-
-
Save IdrisCytron/981570cd6c6d25f039f7cadf7554c0d3 to your computer and use it in GitHub Desktop.
Log sensor data from Sense HAT to Google Sheets using Raspberry Pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sense_hat import SenseHat | |
from time import time | |
import datetime | |
import requests | |
sense = SenseHat() | |
sense.clear() | |
send = False | |
interval = 60 # 1 minute | |
prevSec = 0 | |
print("Send sensor data from Sense HAT to Google Sheets using Raspberry Pi") | |
print() | |
while True: | |
for event in sense.stick.get_events(): | |
if event.direction == "left": | |
send = False | |
sense.show_letter("0", text_colour=(100,0,0)) | |
print("Stop!") | |
print() | |
if event.direction == "right": | |
send = True | |
sense.show_letter("1", text_colour=(0,100,0)) | |
print("Sending data...") | |
print() | |
if send == True: | |
if time() - prevSec > interval: | |
prevSec = time() | |
data_to_send = {} | |
data_to_send["date"] = str(datetime.datetime.now()) | |
data_to_send["temperature"] = sense.get_temperature() | |
data_to_send["humidity"] = sense.get_humidity() | |
data_to_send["pressure"] = sense.get_pressure() | |
data_to_send["accelerometer"] = sense.get_accelerometer_raw() | |
data_to_send["gyroscope"] = sense.get_gyroscope_raw() | |
data_to_send["magnetometer"] = sense.get_compass() | |
print("JSON data:") | |
print(data_to_send) | |
# Replace your webhook URL here | |
r = requests.post("https://your.webhook.url", json = data_to_send) | |
print(r.status_code) | |
print() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment