Open mgabor-bp opened 5 years ago
Add some feature to handle the BLE crash: Code: import json import urllib.request import base64 import time import os from mijia.mijia_poller import MijiaPoller, \ MI_HUMIDITY, MI_TEMPERATURE, MI_BATTERY
domoticzserver = "127.0.0.1:8080" domoticzusername = "" domoticzpassword = ""
base64string = base64.encodestring(('%s:%s' % (domoticzusername, domoticzpassword)).encode()).decode().replace('\n', '')
def domoticzrequest (url): print(url) request = urllib.request.Request(url) request.add_header("Authorization", "Basic %s" % base64string) response = urllib.request.urlopen(request) return response.read()
def update(address,idx_temp,idx_err): global domoticzserver poller = MijiaPoller(address)
loop = 0
try:
temp = poller.parameter_value(MI_TEMPERATURE)
except:
temp = "Not set"
while loop < 2 and temp == "Not set":
print("Error reading value retry after 5 seconds...\n")
os.system("sudo hciconfig hci0 down")
os.system("sudo hciconfig hci0 up")
if loop == 2:
os.system("sudo sudo invoke-rc.d bluetooth restart")
time.sleep(5)
poller = MijiaPoller(address)
loop += 1
try:
temp = poller.parameter_value(MI_TEMPERATURE)
except:
temp = "Not set"
if temp == "Not set":
print("Error reading value\n")
domoticzrequest("http://" + domoticzserver + "/json.htm?type=command¶m=switchlight&idx=" + idx_err + "&switchcmd=Off")
return
print("Mi Sensor: " + address)
print("Firmware: {}".format(poller.firmware_version()))
print("Name: {}".format(poller.name()))
print("Temperature: {}°C".format(poller.parameter_value(MI_TEMPERATURE)))
print("Humidity: {}%".format(poller.parameter_value(MI_HUMIDITY)))
print("Battery: {}%".format(poller.parameter_value(MI_BATTERY)))
val_bat = "{}".format(poller.parameter_value(MI_BATTERY))
# Update temp
#val_temp = "{}".format(poller.parameter_value(MI_TEMPERATURE))
#domoticzrequest("http://" + domoticzserver + "/json.htm?type=command¶m=udevice&idx=" + idx_temp + "&nvalue=0&svalue=" + val_temp + "&battery=" + val_bat)
# Update humidity
#val_hum = "{}".format(poller.parameter_value(MI_HUMIDITY))
#domoticzrequest("http://" + domoticzserver + "/json.htm?type=command¶m=udevice&idx=" + idx_hum + "&svalue=" + val_hum + "&battery=" + val_bat)
#/json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT
val_temp = "{}".format(poller.parameter_value(MI_TEMPERATURE))
val_hum = "{}".format(poller.parameter_value(MI_HUMIDITY))
val_comfort = "0"
if float(val_hum) < 40:
val_comfort = "2"
elif float(val_hum) <= 60:
val_comfort = "1"
elif float(val_hum) > 60:
val_comfort = "3"
domoticzrequest("http://" + domoticzserver + "/json.htm?type=command¶m=udevice&idx=" + idx_temp + "&nvalue=0&svalue=" + val_temp + ";" + val_hum + ";"+ val_comfort + "&battery=" + val_bat)
domoticzrequest("http://" + domoticzserver + "/json.htm?type=command¶m=switchlight&idx=" + idx_err + "&switchcmd=On")
print("\n1: updating") update("4C:65:A8:DD:BD:5C","9","17")
update("4C:65:A8:DF:CC:03","10","17")
update("4C:65:A8:DF:C8:B2","19","17")
update("4C:65:A8:DF:C8:A9","20","17")
Easiest fix and best working is to add a usb bluetoothstick. Wifi and Bluetooth are on the same chip for pi zero w en pi 3, and i think also for the pi4. Also then disable the internal bluetooth
After the some request the bluetooth crashes Different errors e.g.: connect: Device or resource busy (16) What can I do?