kao prvo, pozdrav svima
sad o problemu. radim za fax "meterološku stanicu" sa raspberry pi-em sa anemometrom (očitavanje brzine vjetra) i dht22 (temperatura i vlaga). glavni zadatak je koristiti dretve. anemometar koristi običan reed switch koji zatvara strujni krug prolazom magneta (funkcionira ko obično tipkalo) svaki krug te se mjeri kolko dugo je strujni krug bio zatvoren te se za to koriste GPIO.add_event_detect i GPIO.add_event_callback. dht22 je običan senzor za koji postoji knjižnica i samo se pozove i vrati trenutnu temp i vlagu. problem je u tome što GPIO.add_event_detect uvijek čeka na prolaz magneta i ne znam kako da dodam još jednu dretvu k tome koja će svako određeno vrijeme pozvati funkciju od dht22, osim još jedan GPIO.add_event_detect pa bi to funkcioniralo preko tipkala što nije baš poželjno. relativno sam nov što se tiče pythona a dretve su mi potpuno nove.
ako ima koja dobra duša koja zna, može i želi pomoć bio bi zahvalan do groba a možemo se dogovoriti i za drugi način zahvale (mislim na piće )
evo i kod koji imam za sad. podaci se šalju u bazu i emoncms. sve radi kako treba osim što se temperatura i vlaga ispisuju tek kada se izađe iz skripte sa control+c
#!/usr/bin/python
import sys
import Adafruit_DHT
import time
import datetime
import httplib
import json
import MySQLdb
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #pin that the anemometer is connected to
GPIO.setup(22, GPIO.OUT) # pin that the LED is connected to
timer = time.time() # initialise the timer
fast_time = -1 # TODO: future use
domain = "localhost"
emoncmspath = "emoncms"
apikey = "-----------------------"
def db_conn(wind):
db = MySQLdb.connect("localhost","root","raspberry","anemo")
cursor = db.cursor()
sql = "INSERT INTO Brzina(brzina) VALUES(%s)" %(wind)
cursor.execute(sql)
db.commit()
db.close()
if (datetime.now().second%5 == 0):
conn = httplib.HTTPConnection(domain)
conn.request("GET", "/emoncms/input/post.json?nodeid=3&json={wind:"+wind+"}&apikey="+apikey)
def wind_ping(channel):
global timer
global fast_time
cur_time = (time.time() - timer) # time for half a revolution
wind_speed = 0.667 / cur_time # reworked from the datasheet
if wind_speed < 200: # add bounce detection here
if cur_time < fast_time:
fast_time = cur_time
#wind = "%10.4f" % wind_speed # convert the float number to a string
wind= str(round(wind_speed))
wind_speed = wind_speed * 2.236936 # apply the multiplier to calculate miles per hour
windmph_str = "%10.4f" % wind_speed
#windmph_str= str(round(wind_speed))
cur_str = "%10.4f" % cur_time
# will print the measurements to screen for each pulse detected
print "Wind Speed: " + wind + " m/s, " + windmph_str + " mph, time: " + cur_str
db_conn(wind)
timer = time.time() #reset the timer for the next revolution
GPIO.output(22, not GPIO.input(22)) # alternate the LED state
GPIO.add_event_detect(27,GPIO.FALLING, bouncetime=30) #threaded event, to detect the # voltage falling on anemometer (pin 18)
GPIO.add_event_callback(27,wind_ping) # tell the event to call procedure above
try:
while True: # loop to keep the program alive
sleep(5)
except KeyboardInterrupt:
GPIO.cleanup() # reset the GPIO pins when you press ctrl
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 4)
if humidity is not None and temperature is not None:
print 'Temperature={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity)
temp = str(round(temperature))
hum = str(round(humidity))
db = MySQLdb.connect("localhost","root","raspberry","dht")
cursor = db.cursor()
sql = "INSERT INTO Senzor(vrijeme, temperatura, vlaga) VALUES(NOW(), %s, %s)" %(temperature, humidity)
cursor.execute(sql)
db.commit()
db.close()
conn = httplib.HTTPConnection(domain)
conn.request("GET", "/"+emoncmspath+"/input/post.json?nodeid=3&json={temp:"+temp+",hum:"+hum+"}&apikey="+apikey)
else:
print 'Failed to get reading. Try again!'