Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SSID, PASSWORD = 'ssid', 'pass'
- PRESS_TTL = 0.01
- ALERT_ENDPOINT = 'https://ya.ru/foo/bar/baz/ni'
- DEVICE_ID = 'some_dummy_device_id'
- HASH_SALT = '$y$j9T$edgJeH8xqTYX6/NTUBML70$oq6bfszMECZZCib5ADrj8BkAxsWtw1Nd54L.gE2OXE5'
- def network_config(ssid, password):
- """ setup wifi connection """
- import network
- sta_if = network.WLAN(network.STA_IF)
- if not sta_if.isconnected():
- print('connecting to network...')
- sta_if.active(True)
- sta_if.connect(ssid, password)
- while not sta_if.isconnected():
- pass
- print('network config:', sta_if.ifconfig())
- def get_secret():
- """ generate secret for request """
- from datetime import datetime
- from hashlib import sha256
- raw_secret = ''.join((
- DEVICE_ID, HASH_SALT, datetime.now().minute
- ))
- return sha256(_)
- def post_alert():
- """ post http request to alert server """
- import urequests as requests
- r = requests.post(ALERT_ENDPOINT, json={
- 'device_id': DEVICE_ID,
- 'secret': get_secret(),
- })
- def monitor_emerg_button(buutton):
- """ monitor targer button"""
- first = button.value()
- time.sleep(PRESS_TTL)
- second = button value()
- status_pressed = first and not second
- status_released = not first and second
- if status_pressed:
- post_alert()
- def main():
- """ app entry point """
- import machine
- network_config(SSID, PASSWORD)
- button = machine.Pin(12, machine.Pin.IN, machine.Pin.PULL_UP)
- while True:
- monitor_emerg_button(button)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement