Next up we are going to read the temperature and humidity from the HC1000 Humidity Sensor. Each of the sensors that are accessible on the sensor tag has a GATT Service defined for it. Below is the Humidity Service.
Handle | Type | Type (text) | GATT Server | Description/Value (text) |
(hex) | (hex) | Permissions | ||
0x27 | 0x2800 | GATT Primary Service Declaration | R | Humidity Service |
0x28 | 0x2803 | GATT Characteristic Declaration | R | Humidity Data |
0x29 | 0xAA21 | Humidity Data | RN | TempLSB:TempMSB:HumidityLSB:HumidityMSB |
0x2A | 0x2902 | Client Characteristic Configuration | RW | Write "01:00" to enable notifications, "00:00" to disable |
0x2B | 0x2803 | GATT Characteristic Declaration | R | Humidity Config |
0x2C | 0xAA22 | Humidity Config | RW | Write "01" to start measurements, "00" to stop |
0x2D | 0x2803 | GATT Characteristic Declaration | R | Humidity Period |
0x2E | 0xAA23 | Humidity Period | RW | Period = [Input*10] ms, (lower limit 100 ms), default 1000 ms |
Before you can read a sensor value from the service you must turn it on. For the Humidity Service this is done by writing a 01 to the handle at 0x2C labeled in the table above as Humidity Config. Once you have turned on the Humidity Service you will read values from the handle at 0x29 labeled Humidity Data.
Referring to the table above we see that four values will be returned from left to right:
- Temp(LSB) - The least significant byte of the temperature data in hex.
- Temp(MSB) - The most significant byte of the temperature data in hex.
- Humidity(LSB) - The least significant byte of the humidity data in hex.
- Humidity(MSB) - The most significant byte of the humidity data in hex.
On page 14 of data sheet for the HC1000 Humidity Sensor are the formulas for converting this data into a temperature reading and a humidity reading.
- Temperature in Celsius = (Temp(MSB) * 0x100 + Temp(LSB)) / 65536 * 165 – 40
- Humidity = (Humidity(MSB) * 0x100 + Humidity(LSB)) / 65536 * 100
To get the values for the temperature and humidity readings follow the steps below. In this example the values returned are 64 66 0c a2.
pi@raspberrypi ~ $ gatttool -b B0:B4:48:B9:2C:82 -I |
The resulting temperature is:
(0x66 * 0x100 + 0x64) / 65536 * 165 – 40 = 26.19 C
The resulting humidity is:
(0xa2 * 0x100 + 0x0C) / 65536 * 100 = 63.30%
Below is a bash script that will perform the commands above and calculate the temperature and humidity.
getTempHumidity.sh
#!/bin/bash |
No comments:
Post a Comment