-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsensor_ws2300.py
140 lines (104 loc) · 3.82 KB
/
sensor_ws2300.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
###########################################################################
# Sint Wind PI
# Copyright 2012 by Tonino Tarsi <[email protected]>
#
# Please refer to the LICENSE file for conditions
# Visit http://www.vololiberomontecucco.it
#
##########################################################################
"""This module defines the base sensors Sensor_WS2300 ."""
import threading
import time
import config
import random
import datetime
import sqlite3
import sys
import subprocess
import globalvars
import meteodata
import sensor_thread
import sensor
from TTLib import *
import logging
import platform
import datetime
import serial
import string
import ws2300
import serial
def log(message) :
print datetime.datetime.now().strftime("[%d/%m/%Y-%H:%M:%S]") , message
windDirMap = { 0:"N", 1:"NNE", 2:"NE", 3:"ENE", 4:"E", 5:"ESE", 6:"SE", 7:"SSE",
8:"S", 9:"SSW", 10:"SW", 11:"WSW", 12:"W", 13:"WNW", 14:"NW", 15:"NWN" }
def get_wind_dir_text():
"""Return an array to convert wind direction integer to a string."""
return ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N']
class Sensor_WS2300(sensor.Sensor):
'''
Station driver for the Oregon Scientific WS2300.
'''
def __init__(self,cfg ):
self.cfg = cfg
def GetData(self):
log("Thread started")
#print "GetData"
serialPort = ws2300.LinuxSerialPort(self.cfg.sensor_serial_port)
#print serialPort
#print "opened"
ws = ws2300.Ws2300(serialPort)
measures = [
ws2300.Measure.IDS["pa"], # pressure absolute
ws2300.Measure.IDS["it"], # in temp
ws2300.Measure.IDS["ih"], # in humidity
ws2300.Measure.IDS["ot"], # out temp"
ws2300.Measure.IDS["oh"], # out humidity"
ws2300.Measure.IDS["rt"], # rain total
ws2300.Measure.IDS["ws"], # "wind speed"
ws2300.Measure.IDS["w0"], # "wind direction"
ws2300.Measure.IDS["ws"], # "wind speed gust ???"
ws2300.Measure.IDS["wsu"], # wind speed units
#ws2300.Measure.IDS["rh"], # rain 1h
#ws2300.Measure.IDS["wsh"], # "wind speed max ??????????????"
]
while True:
seconds = datetime.datetime.now().second
if ( seconds < 30 ):
time.sleep(30-seconds)
else:
time.sleep(90-seconds)
try:
raw_data = ws2300.read_measurements(ws, measures)
data = [ m.conv.binary2value(d) for m, d in zip(measures, raw_data)]
print "***************DUBUG********************"
print data
print "***************DUBUG********************"
globalvars.meteo_data.status = 0
globalvars.meteo_data.last_measure_time = datetime.datetime.now()
globalvars.meteo_data.idx = globalvars.meteo_data.last_measure_time
globalvars.meteo_data.abs_pressure = float(data[0])
globalvars.meteo_data.temp_in = float(data[1])
globalvars.meteo_data.hum_in = float(data[2])
globalvars.meteo_data.temp_out = float(data[3])
globalvars.meteo_data.hum_out = float(data[4])
globalvars.meteo_data.rain = float(data[5])
globalvars.meteo_data.wind_ave = (float(data[6])*1.609344)*self.cfg.windspeed_gain + self.cfg.windspeed_offset
globalvars.meteo_data.wind_gust = (float(data[8])*1.609344)*self.cfg.windspeed_gain + self.cfg.windspeed_offset
wind_dir = data[7]
globalvars.meteo_data.wind_dir = wind_dir
val=int((wind_dir/22.5)+.5)
globalvars.meteo_data.wind_dir_code = get_wind_dir_text()[val]
globalvars.meteo_data.illuminance = None
globalvars.meteo_data.uv = None
sensor.Sensor.GetData(self)
except Exception, err:
print sys.exc_info()[0]
log("ERROR with WS2300 %s " % err)
serialPort.close()
if __name__ == '__main__':
"""Main only for testing"""
configfile = 'swpi.cfg'
cfg = config.config(configfile)
globalvars.meteo_data = meteodata.MeteoData(cfg)
ws = Sensor_WS2300(cfg)
ws.GetData()