Hirdetés
- Xiaomi 15T - reakció nélkül nincs egyensúly
- Megbüntették, ezért feloszlatná az EU-t Elon Musk
- Samsung Galaxy Z Fold7 - ezt vártuk, de…
- Honor Magic7 Pro - kifinomult, költséges képalkotás
- Vivo X300 - kicsiben jobban megéri
- Apple Watch
- Samsung Galaxy S25 Ultra - titán keret, acélos teljesítmény
- Samsung Galaxy Watch5 Pro - kerek, de nem tekerek
- Bemutatkozott a Poco X7 és X7 Pro
- Xiaomi 14 Ultra - Leica hercegnő
Új hozzászólás Aktív témák
-
saja
csendes tag
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
Új hozzászólás Aktív témák
- DJI SPARK SUNRISE YELLOW DRONE SOK KIEGÉSZÍTŐVEL ELADÓ
- WD Blacck SN8100 4 TB M.2 NVME PCI-E 5.0 x4 - Új - 14.900-14.000 MBs - Eladó!
- WD Black SN850X 4TB M.2 NVME PCI-E 4.0 x4 - Új - 7300-6600 MBs - Eladó!
- Asus T.U.F gaming monitor 34"
- 9900k + 48GB DDR 4 + RTX 3060ti konfig eladó kérésre monitorokkal, hangrendszerrel
- BESZÁMÍTÁS! ASROCK B650M R7 8700F 32GB DDR4 512GB SSD RX 6800XT 16GB Zalman Z1 PLUS 750W
- ÚJ Lenovo ThinkPad X13 Gen 5 - 13.3" WUXGA IPS - Ultra 5 135U - 16GB - 512GB - Win11 - 2,5 év gari
- Dell Latitude 7420 Core i7-1185 G7, 16GB RAM, SSD, jó akku, számla, 6 hó gar
- ASUS VivoBook S16 S3607QA-PL018W
- GYÖNYÖRŰ iPhone 13 Mini 128GB Pink-1 ÉV GARANCIA -Kártyafüggetlen, MS3823, 100% Akkumulátor
Állásajánlatok
Cég: Laptopszaki Kft.
Város: Budapest
Cég: BroadBit Hungary Kft.
Város: Budakeszi


