-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwifi_temp_hum.py
77 lines (60 loc) · 1.84 KB
/
wifi_temp_hum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# see http://electronics.ozonejunkie.com/2014/12/opening-up-the-usr-htw-wifi-temperature-humidity-sensor/ (10.10.100.254 default)
import socket
import math
import time
TCP_ADDR = '192.168.1.107'
TCP_PORT = 8899
PACK_LEN = 11
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(30)
while True:
try:
s.connect((TCP_ADDR, TCP_PORT))
break
except:
print 'Retry connect....'
time.sleep(10)
bytes_data = [0] * PACK_LEN
b = 17.67 # see wikipedia
c = 243.5
def gamma(t,rh):
return (b*t / (c+t)) + math.log(rh/100.0)
def dewpoint(t,rh):
g = gamma(t,rh)
return c*g / (b-g)
def get_temp_hum():
try:
str_data = s.recv(PACK_LEN)
hex_data = str_data.encode('hex')
for n in range(0,PACK_LEN): #convert to array of bytes
lower = 2*n
upper = lower + 2
bytes_data[n] = int(hex_data[lower:upper],16)
humid = (((bytes_data[6])<<8)+(bytes_data[7]))/10.0
temp = (((((bytes_data[8])&0x7F)<<8)+(bytes_data[9]))/10.0)
if int(bytes_data[8]) & 0x80: #invert temp if sign bit is set
temp = -1.0* temp
# checksum = (uint(sum(bytes_data[0:10])) & 0xFF)+1
checksum = 0
for i in range(PACK_LEN-1):
checksum += bytes_data[i]
checksum &= 0xFF
checksum += 1
if checksum == bytes_data[10]:
return (temp, humid)
raise ValueError,'Invalid Checksum'
except:
raise
while True:
try:
(temp, humid) = get_temp_hum()
print "Valid!"
print "Temp: " + str(temp)
print "Hum: " + str(humid)
print "Dewpoint: " + str(dewpoint(temp, humid))
except Exception as ex:
print "exception: ", ex
print "Timed Out"
time.sleep(30)