Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import RPi.GPIO as GPIO
- import time
- import urllib,urllib2,base64
- import math
- P_ANEMO = 18
- P_CLK = 23
- P_MOSI = 19
- P_MISO = 21
- P_CS = 24
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(P_ANEMO, GPIO.IN)
- GPIO.setup(P_CLK, GPIO.OUT)
- GPIO.setup(P_MOSI, GPIO.OUT)
- GPIO.setup(P_MISO, GPIO.IN)
- GPIO.setup(P_CS, GPIO.OUT)
- def anemo_pulse(channel):
- global anemo_count
- anemo_count += 1
- def read_sensor(ch_no = 0):
- if (ch_no == 0):
- cmd = 0x6 # CH0
- else:
- cmd = 0x7 # CH1
- # init pins low
- GPIO.output(P_CLK, False)
- GPIO.output(P_CS, False)
- # write first 3 bits to MOSI
- cmd <<= 5
- for bno in range(3):
- if (cmd & 0x80):
- GPIO.output(P_MOSI, True)
- else:
- GPIO.output(P_MOSI, False)
- cmd <<= 1
- GPIO.output(P_CLK, True)
- GPIO.output(P_CLK, False)
- # read 2(empty and null bit) + 10 ADC bits from MISO
- adc_result = 0
- for bit_no in range(12):
- GPIO.output(P_CLK, True)
- GPIO.output(P_CLK, False)
- adc_result <<= 1
- if (GPIO.input(P_MISO)):
- adc_result |= 0x1
- GPIO.output(P_CS, True)
- adc_result /= 2 # first bit is 'null' so drop it
- return adc_result
- anemo_count = 0.
- GPIO.add_event_detect(P_ANEMO, GPIO.FALLING, callback=anemo_pulse)
- try:
- while True:
- vitesseVentMoy = 0.
- vitesseVentMin = 999999.
- vitesseVentMax = 0.
- xVector = 0.
- yVector = 0.
- for i in range(0,30):
- anemo_count=0.
- time.sleep(10)
- vitesse = anemo_count
- vitesse *= 0.1006 * 3.6 # 1.006m/s sur 10 secondes
- dir_sensor = read_sensor(0)/1023.
- direction = dir_sensor*360.
- direction_rad = dir_sensor*2*math.pi
- xVector += vitesse * math.cos(direction_rad)
- yVector += vitesse * math.sin(direction_rad)
- if vitesse < vitesseVentMin:
- vitesseVentMin = vitesse
- if vitesse > vitesseVentMax:
- vitesseVentMax = vitesse
- vitesseVentMoy += vitesse
- #print "#", i, " : ", vitesse, "km/h ", direction, "°"
- vitesseVentMoy /= 30.;
- dirVentMoy = math.atan2( yVector , xVector) / math.pi * 180.
- #print "vitesse inst:", vitesse, "km/h"
- #print "vitesse moy :", vitesseVentMoy, "km/h"
- #print "vitesse max :", vitesseVentMax, "km/h"
- #print "vitesse min :", vitesseVentMin, "km/h"
- #print "dir inst :", direction, "°"
- #print "dir moy :", dirVentMoy, "°"
- params = urllib.urlencode({
- 'idBalise': 6000,
- 'vitesseVentMoy': vitesseVentMoy,
- 'vitesseVentMin': vitesseVentMin,
- 'vitesseVentMax': vitesseVentMax,
- 'directVentMoy': dirVentMoy,
- 'directVentInst': direction
- })
- req = urllib2.Request('http://URL-UPLOAD-FFVL', params)
- try:
- response = urllib2.urlopen(req)
- data=response.read()
- print data
- except urllib2.URLError, e:
- print "error", e
- del req
- except KeyboardInterrupt:
- GPIO.cleanup()
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment