forked from ToninoTarsi/swpi
/
sensor_argent80422.py
217 lines (157 loc) · 6.32 KB
/
sensor_argent80422.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
###########################################################################
# Sint Wind PI
# Copyright 2012 by Tonino Tarsi <tony.tarsi@gmail.com>
#
# Please refer to the LICENSE file for conditions
# Visit http://www.vololiberomontecucco.it
#
##########################################################################
"""This module defines the base sensor Nevio ."""
import threading
import time
import config
import random
import datetime
import sqlite3
from TTLib import *
import sys
import subprocess
import globalvars
import meteodata
import sensor_thread
import sensor
import RPi.GPIO as GPIO
import TTLib
import thread
from ctypes import *
import intervalmap
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']
class Sensor_Argent80422(sensor.Sensor):
__MEASURETIME = 2 # Number of seconds for pulse recording
# Connections PIN - USING BCM numbering convention !!!!!!
__PIN_A = 23 #Anemometer
def __init__(self,cfg ):
threading.Thread.__init__(self)
sensor.Sensor.__init__(self,cfg )
self.libMCP = cdll.LoadLibrary('./mcp3002/libMCP3002.so')
if ( self.libMCP.init() != 0 ):
log("Error initializing mcp3002 library.Try to continue")
self.cfg = cfg
self.bTimerRun = 0
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(self.__PIN_A, GPIO.IN) # wind Speed
self.rb_WindSpeed = TTLib.RingBuffer(self.cfg.number_of_measure_for_wind_average_gust_calculation)
self.map = intervalmap.intervalmap()
self.map[0:75] = 5
self.map[75:89] = 3
self.map[89:111] = 4
self.map[111:156] = 7
self.map[156:214] = 6
self.map[214:264] = 9
self.map[264:342] = 8
self.map[342:424] = 1
self.map[424:514] = 2
self.map[514:593] = 11
self.map[593:640] = 10
self.map[640:712] = 15
self.map[712:769] = 0
self.map[769:815] = 13
self.map[815:870] = 14
self.map[870:1024]= 12
self.active = True
self.start()
def run(self):
sleeptime = self.cfg.windmeasureinterval - self.__MEASURETIME
if sleeptime < 0 : sleeptime = 0
while self.active :
currentWind = self.GetCurretWindSpeed()
#TTLib.log( "currentWind : " + str(currentWind))
self.rb_WindSpeed.append(currentWind)
time.sleep(sleeptime)
def Detect(self):
return True
def SetTimer(self):
self.bTimerRun = 0
def GetCurretWindDir(self):
"""Get wind direction reading MCP3002 channel 0."""
ch0 = -1
while ch0 == -1 :
ch0 = self.libMCP.read_channel(0)
if ( ch0 == -1 ) :
log("Error reading mcp3002 channel 0. Retrying ")
time.sleep(0.1)
#print "ch0",ch0
wind_dir = self.map[ch0]
winddir_code = get_wind_dir_text()[wind_dir]
return wind_dir*22.5, winddir_code
def GetCurretWindSpeed(self):
"""Get wind speed pooling __PIN_A ( may be an interrupt version later )."""
self.bTimerRun = 1
t = threading.Timer(self.__MEASURETIME, self.SetTimer)
t.start()
i = 0
o = GPIO.input(self.__PIN_A)
while self.bTimerRun:
n = GPIO.input(self.__PIN_A)
if ( n != o):
i = i+1
o = n
time.sleep(0.005)
time.sleep(0.0005)
return (( i / ( self.__MEASURETIME * 2 )) * 2.4 ) * self.cfg.windspeed_gain + self.cfg.windspeed_offset
def GetCurretWindSpeedOld(self):
"""Get wind speed pooling __PIN_A ( may be an interrupt version later )."""
self.bTimerRun = 1
t = threading.Timer(self.__MEASURETIME, self.SetTimer)
t.start()
i = 0
o = GPIO.input(self.__PIN_A)
while self.bTimerRun:
#time.sleep(0.010)
n = GPIO.input(self.__PIN_A)
if ( n != o):
i = i+1
o = n
return (( i / ( self.__MEASURETIME * 2 )) * 2.4 ) * self.cfg.windspeed_gain + self.cfg.windspeed_offset
def GetData(self):
seconds = datetime.datetime.now().second
if ( seconds < 30 ):
time.sleep(30-seconds)
else:
time.sleep(90-seconds)
wind_ave,wind_gust = self.rb_WindSpeed.getMeanMax()
if ( wind_ave != None) :
wind_dir, wind_dir_code = self.GetCurretWindDir()
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.wind_ave = wind_ave
globalvars.meteo_data.wind_gust = wind_gust
globalvars.meteo_data.wind_dir = wind_dir
globalvars.meteo_data.wind_dir_code = wind_dir_code
if ( self.cfg.use_tmp36 ):
ch1 = self.libMCP.read_channel(1)
v1 = ch1 * (3300.0/1024.0)
temp = (v1 - 500.0) / 10.0
globalvars.meteo_data.temp_out = temp
sensor.Sensor.GetData(self)
if __name__ == '__main__':
configfile = 'swpi.cfg'
cfg = config.config(configfile)
ss = Sensor_Argent80422(cfg)
ss.active = False
while ( 1 ) :
speed = ss.GetCurretWindSpeed()
dir = ss.GetCurretWindDir()
temp = None
if ( cfg.use_tmp36 ):
ch1 = ss.libMCP.read_channel(1)
v1 = ch1 * (3300.0/1024.0)
temp = (v1 - 500.0) / 10.0
print "Speed:",speed,"Dir:",dir,"Temp;",temp
# ss.GetData()
# log( "Meteo Data - D : " + globalvars.meteo_data.wind_dir_code + " S : " + str(globalvars.meteo_data.wind_ave) + + " G : " + str(globalvars.meteo_data.wind_gust) )
# #print logData("http://localhost/swpi_logger.php")