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

uploaded esp code

parents
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 network %s' %ssid)
wlan.connect(ssid, password)
while not wlan.isconnected():
pass
print('network config:', wlan.ifconfig())
def on_message(topic, message):
print(message)
def connectToMQTT():
print(wlan.isconnected())
global mqtt_client
mqtt_client = MQTTClient(
client_id = 'bla',
server = '50ffbd7e701c480ca097875a1621692c.s1.eu.hivemq.cloud',
port = 8883,
keepalive = 60,
ssl=True
)
mqtt_client.connect()
mqtt_client.set_callback(on_message)
mqtt_client.subscribe("birdie/tx")
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)
print('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/rx', '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