Creating profiles can range from very easy to virtually impossible
BLE does not specify data exchange formats so each manufacturer can do whatever they like including custom authentication/encryption. There are mobile apps that can connect to BLE devices and show available services. Best case scenario that you can find color/brightness control service number and there's no special pairing procedure required. Have a look at awox-aromalightcolor.lua which is an RGB LED lamp with BLE.
Im working on a BLE profile for the xiaome Mi Flora Flower care sensor, this is as far as i have got, i can read the data but not sure how to extract the data i want from the bytes,
there is available, light level, soil moisture level and temperature, the location of the data is as per the link below. https://www.open-homeautomation.com/2016...nt-sensor/
I am having trouble receiving the correct data from lua code, when i use gatttool all works as below although i need to be fast to send the --char-read request before i get disconnected otherwise response is AA BB CC DD EE FF 99 88 77 66 00 00 00 00 00 00
gatttool --device=80:EA:CA:89:1D:78 --char-write-req -a 0x33 -n A01F
gatttool --device=80:EA:CA:89:1D:78 --char-read -a 0x35
response is 03 01 00 4a 03 00 00 00 00 00 02 3c 00 fb 34 9b this is example of expected response
from my lua script, result in rx is always AA BB CC DD EE FF 99 88 77 66 00 00 00 00 00 00
i think im not sending the ble.sockreadhnd(sock, 0x35) command fast enough? or sending it before the ble.sockwritereq(sock, 0x33, cmd) has returned or maybe there is something else wrong?
The result from log(x, err) is *arg 1 number: 4 * arg: 2* number: 114 is there any documentation for these codes? or for the ble.sockwritereq() commands? Many Thanks
Code:
require('ble')
sock = ble.sock()
ble.settimeout(sock, 10)
res = ble.connect(sock, "80:EA:CA:89:1D:78")
if res then
cmd = 0xA0, 0x1F
(21.04.2020, 06:23)admin Wrote: This line is incorrect (only 0xA0 is assigned to cmd):
Code:
cmd = 0xA0, 0x1F
Thanks, i have it working correctly now (in resident script with 0 delay) and returning the correct values as below,
but if i put a longer delay than 0 on the resident script the correct values are not returned,
what could be the reason for this?
how should this code run, in a ble profile? or it is ok in resident script?
also i want to only ask this device what its values are every 15 min or so, as it is a battery operated device...
should i do a scheduled script every x minutes?
Code:
require('ble')
sock = ble.sock()
ble.settimeout(sock, 10)
res = ble.connect(sock, "80:EA:CA:89:1D:78")
if res then
cmd1, cmd2 = 0xA0, 0x1F
x, err = ble.sockwritereq(sock, 0x33, cmd1, cmd2)
log(x, err)
rx = ble.sockreadhnd(sock, 0x35)
loghex(rx)
temperature = (rx:byte(2) + rx:byte(1)) / 10
log(temperature)
end
You can use a scheduled script or create a profile. See raiing-temperature.lua for a similar device.
I'm not sure that this conversion is correct:
Code:
temperature = (rx:byte(2) + rx:byte(1)) / 10
The temperature value is probably two bytes, so you need to shift one byte with multiply by 256. Try both ways as byte order is not known.
Code:
temperature = (rx:byte(2) * 256 + rx:byte(1)) / 10
temperature = (rx:byte(2) + rx:byte(1) * 256) / 10
All write functions like ble.sockwritereq return two values:
1. number of bytes written, -1 = error
2. last error number (errno), applicable only when first value is -1, see this for errno number description: https://www.thegeekstuff.com/2010/10/linux-error-codes/
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"}