12.03.2022, 10:12
(This post was last modified: 12.03.2022, 10:14 by benanderson_475.)
(22.04.2020, 10:53)admin Wrote: You can use a scheduled script or create a profile. See raiing-temperature.lua for a similar device.Hi admin,
working on this some more after long testing time...
I am wondering if you can help me with the BLE code below (running in scheduled script every 10min), some times i get fail to read the data from the sensor, are you able to tell me if there is some obvious error in my lua code or is it more likely hardware related i have 2x flora plant sensor BLE, but if i am successful with no errors i might add 5 or 6 more of these sensors.
Many Thanks,
Code:
local devices = {"80:EA:CA:88:xx:xx", "80:EA:CA:89:xx:xx"}
require('ble')
for i = 1, #devices do
sock = ble.sock()
ble.settimeout(sock, 10)
res = ble.connect(sock, devices[i]) --"80:EA:CA:88:xx:xx" "80:EA:CA:89:xx:xx"
--log(res)
if res == nil then -- device not found
log(devices[i]..' Read Failed')
log(devices[i] ..' Retry connecting')
os.sleep(3)
res = ble.connect(sock, devices[i])
if res == nil then
log(devices[i]..' Not Found')
end
end
if res then
cmd1, cmd2 = 0xA0, 0x1F
x, err = ble.sockwritereq(sock, 0x33, cmd1, cmd2)
--log(x, err)
os.sleep(1)
rx = ble.sockreadhnd(sock, 0x35) --it usually misses the first one
rx1 = ble.sockreadhnd(sock, 0x35)
batt = ble.sockreadhnd(sock, 0x38) --it usually misses the first one
batt1 = ble.sockreadhnd(sock, 0x38)
if rx1 then
temperature = (rx1:byte(2) * 0x100 + rx1:byte(1)) / 10
fertility = rx1:byte(10) * 0x100 + rx1:byte(9) -- conductivity in µS/cm
moisture = rx1:byte(8) -- moisture in %
sunlight = rx1:byte(5) * 0x100 + rx1:byte(4) -- Lux
end
if batt1 then
battery = batt1:byte(1)
end
log('mac ='.. devices[i]..' '.. 'temp '.. temperature, fertility, sunlight, battery, moisture)
os.sleep(2)
end
end