Created
July 9, 2017 06:11
-
-
Save electronut/4e8f04ee20116d6c25cb701ece7a9f1a to your computer and use it in GitHub Desktop.
bluey-beacon - parse sensor data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# constant | |
pow_16 = 65536.0 | |
# decode temperature | |
def decodeT(temp_val): | |
return ((temp_val / pow_16) * 165 - 40) | |
# decode humidity | |
def decodeH(humid_val): | |
return ((humid_val / pow_16) * 100) | |
# decode ambient light | |
def decodeL(adc_ch0, adc_ch1): | |
result = 999.99 | |
channelRatio = (adc_ch1)/(float)(adc_ch0); | |
# below formula is from datasheet | |
if(channelRatio >= 0 and channelRatio <= 0.52): | |
result = (0.0315 * adc_ch0) - (0.0593 * adc_ch0 * pow(channelRatio, 1.4)) | |
elif(channelRatio > 0.52 and channelRatio <= 0.65): | |
result = (0.0229 * adc_ch0) - (0.0291 * adc_ch1) | |
elif(channelRatio > 0.65 and channelRatio <= 0.80): | |
result = (0.0157 * adc_ch0) - (0.0180 * adc_ch1) | |
elif(channelRatio > 0.80 and channelRatio <= 1.30): | |
result = (0.00338 * adc_ch0) - (0.00260 * adc_ch1) | |
elif(channelRatio > 1.30): | |
result = 0; | |
return result | |
# decode T/H/L data | |
def decodeData(x1, x2, x3, x4): | |
T = decodeT(x1) | |
H = decodeH(x2) | |
L = decodeL(x3, x4) | |
return (T, H, L) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment