Változtattam a kódon. Nincs benne órás várakozás, és a gomb figyelést egy másik szálon végzem, a kapcsolati ellenőrzés pedig "szinkron" működik. Előtte pedig még le is kérem a shelly kapcsoló relé státuszát (tehát nem kell mondjuk kétszer nyomni a gombot, hogy lekapcsolja a lámpát ha már fel van kapcsolva).
import netman
import urequests as requests
import uasyncio as asyncio
import dht11
from machine import Pin
print(asyncio.__version__)
led = Pin("LED", Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
shellyIP = '192.168.0.140'
async def blinking():
while True:
led.on()
asyncio.sleep(0.3)
led.off()
asyncio.sleep(0.3)
def toggleLamp(lamp_state):
print("button clicked to turn " + lamp_state + " lamp")
switch_lamp = 'http://' + shellyIP + '/relay/0?turn=' + lamp_state
blink_task = asyncio.create_task(blinking())
try:
resp = requests.get(switch_lamp)
resp.close()
blink_task.cancel()
except:
blink_task.cancel()
led.on()
country = 'HU'
ssid = '<SSID>'
password = '*****'
wlan = netman.connectWifi(ssid,password,country)
async def listenButton(defaultLampState):
button_state = button.value()
switch_lamp = ''
lamp_state = defaultLampState
count = 0
while True:
if button_state == 1 and lamp_state == 'on':
lamp_state = 'off'
toggleLamp(lamp_state)
await asyncio.sleep_ms(200)
button_state = button.value()
if button_state == 1 and lamp_state == 'off':
lamp_state = 'on'
toggleLamp(lamp_state)
await asyncio.sleep_ms(200)
button_state = button.value()
await asyncio.sleep_ms(200)
async def listenButtonAndCheckConnection():
wlan_next = netman.checkConnection(wlan,ssid,password,country)
defaultLampState = 'off'
try:
resp = requests.get('http://' + shellyIP + '/relay/0')
print(resp.text)
parsedResp = resp.json()
print(parsedResp["ison"])
if parsedResp["ison"] == True:
defaultLampState = 'on'
resp.close()
except:
print("couldn't fetch shelly relay status")
asyncio.create_task(listenButton(defaultLampState))
while True:
await asyncio.sleep(1)
wlan_next = netman.checkConnection(wlan_next,ssid,password,country)
temp_humidity = dht11.getTemp()
json = """{temp: %s, humidity: %s}"""
async def serve(reader, writer):
print("create temperature data to serve")
data = """none"""
try:
temp_humidity = dht11.getTemp()
data = json % temp_humidity
except:
data = """undefined"""
resp = b"HTTP/1.0 200 OK\r\nContent-type: application/json\r\n\r\n" + "{JSON}\r\n".format(JSON=data)
l = await reader.read(256)
print(l)
led.on()
await writer.awrite(resp)
await asyncio.sleep_ms(200)
await writer.wait_closed()
led.off()
print("start event loop")
loop = asyncio.get_event_loop()
loop.create_task(asyncio.start_server(serve, "0.0.0.0", 80))
loop.create_task(listenButtonAndCheckConnection())
try:
loop.run_forever()
except KeyboardInterrupt:
print("closing")
loop.close()
A netman.py kódját is kicsit alakítottam. Az ellenőrző függvény máshogy néz ki:
def checkConnection(wlan,ssid,password,country):
status = wlan.status()
print(status)
if status != 3:
print("connection lost")
wlan = tryToConnect(ssid,password,country)
return wlan
else:
print("connection ok")
time.sleep(0.2)
return wlan
[ Szerkesztve ]