Last active
October 23, 2020 10:18
-
-
Save uosotm/e47f5244b818c28dc521a927de7762e9 to your computer and use it in GitHub Desktop.
【ツクレル勉強会】ラズパイでスマート温湿度計を作ろう - サンプルコード
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
import time | |
from sense_hat import SenseHat | |
sense = SenseHat() | |
def main(): | |
sense.clear(128, 128, 128) | |
while True: | |
temp = sense.get_temperature() | |
hum = sense.get_humidity() | |
pres = sense.get_pressure() | |
print(f'====={time.ctime()}=====') | |
print(f'Temperature: {temp}') | |
print(f'Humidity: {hum}') | |
print(f'Pressure: {pres}') | |
events = [] | |
for event in sense.stick.get_events(): | |
events.append([event.action, event.direction]) | |
print(f'Events: {events}') | |
time.sleep(2) | |
if __name__ == '__main__': | |
try: | |
main() | |
except KeyboardInterrupt: | |
sense.clear() |
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
def get_vh(temp, hum): | |
return 217*(6.1078*10**(7.5*temp/(temp+237.3)))/(temp+273.15)*hum/100 | |
def get_flu_level(vh): | |
if vh > 17: | |
return 1, "非常に安全" | |
if vh > 11: | |
return 2, "安全" | |
if vh > 7: | |
return 3, "要注意" | |
return 4, "警戒" | |
if __name__ == '__main__': | |
from sense_hat import SenseHat | |
#from sense_emu import SenseHat | |
TEMP_DELTA = 0 | |
sense = SenseHat() | |
temp = sense.get_temperature() - TEMP_DELTA | |
hum = sense.get_humidity() | |
vh = get_vh(temp, hum) | |
level, msg = get_flu_level(vh) | |
print(f'Temperature: {temp}, Humidity: {hum}') | |
print(f'Volumetric humidity: {vh}') | |
print(f'Level: {level}, Message: {msg}') |
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
import requests | |
class Webhook: | |
def __init__(self, event_name, ifttt_key): | |
self.event_name = event_name | |
self.ifttt_key = ifttt_key | |
self.update_url() | |
def update_url(self, event=None): | |
if event: | |
self.event_name = event | |
self.url = "https://maker.ifttt.com/trigger/" + \ | |
self.event_name + "/with/key/" + self.ifttt_key | |
def post(self, *, event=None, payload={}): | |
if event: | |
self.update_url(event) | |
response = requests.post(self.url, data=payload) | |
return response |
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 sense_emu import SenseHat | |
from ifttt import Webhook | |
from flu import * | |
import time | |
IFTTT_KEY = '' | |
TEMP_DELTA = 0 | |
CHECK_SPAN = 60 * 10 | |
sense = SenseHat() | |
# イベント名は後で変更するので最初は何でも良い | |
webhook = Webhook('demo', IFTTT_KEY) | |
def alert(message): | |
payload = { | |
"value1": message | |
} | |
res = webhook.post(event='send_message', payload=payload) | |
if not res.ok: | |
print('Request failed with status code', res.status_code) | |
def main(): | |
vh_level = 0 | |
while True: | |
temp = sense.get_temperature() - TEMP_DELTA | |
hum = sense.get_humidity() | |
''' | |
インフルエンザ警戒度が3以上に変化したらアラートを出す | |
''' | |
vh = get_vh(temp, hum) | |
level, msg = get_flu_level(vh) | |
if (vh_level < 3 and level >= 3) or (vh_level < 4 and level >= 4): | |
alert(f'【{msg}】インフルエンザにご注意ください。現在の絶対湿度は{round(vh)}g/m3です。加湿しましょう。') | |
vh_level = level | |
time.sleep(CHECK_SPAN) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment