Commit d23e157a authored by Daniel Kolibár's avatar Daniel Kolibár
Browse files

Merge branch 'esp' into 'dockeres'

Esp

See merge request tomas.gordon/hackathon-2021!1
parents ff2ade6b 773e4906
from machine import Pin
from time import sleep
import dht
import uasyncio
import network
import ubinascii
from umqtt.robust import MQTTClient
dht_sensor = dht.DHT11(Pin(14))
pir = Pin(13, Pin.IN)
mqtt_client = None
wlan = None
def connectToNetwork(ssid, password):
global wlan
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to wlan network: %s' %ssid)
wlan.connect(ssid, password)
while not wlan.isconnected():
pass
print('connected! network config:', wlan.ifconfig())
def on_message(topic, message):
print(message)
def connectToMQTT():
global mqtt_client
SSL_PARAMS = {'server_hostname': '50ffbd7e701c480ca097875a1621692c.s1.eu.hivemq.cloud'}
mqtt_client = MQTTClient(
client_id = 'birdie_house_' + ubinascii.hexlify(network.WLAN().config('mac'),'_').decode(),
server = '50ffbd7e701c480ca097875a1621692c.s1.eu.hivemq.cloud',
port = 8883,
keepalive = 30,
ssl=True,
user='birdie',
password='tjhpnHMQb2021',
ssl_params=SSL_PARAMS
)
mqtt_client.connect()
async def readTemperatrue():
while True:
try:
await uasyncio.sleep(2)
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
print('Temperature: %3.1f C; ' %temp, 'Humidity: %3.1f %%' %hum)
except OSError as e:
print('Failed to read from DHT11 sensor: %s' %e)
async def detectMotion():
while True:
await uasyncio.sleep(1)
if pir.value():
mqtt_client.publish('birdie/rxtx/raspberry', 'TAKE_PHOTO')
await uasyncio.sleep_ms(40)
while pir.value():
pass
await uasyncio.sleep_ms(40)
else:
print('No motion detected!')
connectToNetwork("wifi-lab", "12345wifilabhouse54321")
connectToMQTT()
event_loop = uasyncio.get_event_loop()
event_loop.create_task(readTemperatrue())
event_loop.create_task(detectMotion())
event_loop.run_forever()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment