This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

BLE profiles
#12
(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
Reply


Messages In This Thread
BLE profiles - by morak - 29.01.2018, 11:37
RE: BLE profiles - by 1114752670@qq.com - 13.03.2019, 07:53
RE: BLE profiles - by Daniel - 13.03.2019, 08:48
RE: BLE profiles - by gjniewenhuijse - 13.03.2019, 11:06
RE: BLE profiles - by admin - 13.03.2019, 11:19
RE: BLE profiles - by benanderson_475 - 13.01.2020, 21:17
RE: BLE profiles - by admin - 14.01.2020, 07:44
RE: BLE profiles - by benanderson_475 - 21.04.2020, 01:00
RE: BLE profiles - by admin - 21.04.2020, 06:23
RE: BLE profiles - by benanderson_475 - 22.04.2020, 00:38
RE: BLE profiles - by admin - 22.04.2020, 10:53
RE: BLE profiles - by benanderson_475 - 12.03.2022, 10:12

Forum Jump: