Luftfeuchtigkeitssensor mit Python abrufen
This commit is contained in:
parent
8056ae4ded
commit
2cf38cbff5
6 changed files with 61 additions and 154 deletions
Binary file not shown.
|
@ -1,148 +0,0 @@
|
|||
// How to access GPIO registers from C-code on the Raspberry-Pi
|
||||
// Example program
|
||||
// 15-January-2012
|
||||
// Dom and Gert
|
||||
//
|
||||
|
||||
|
||||
// Access from ARM Running Linux
|
||||
|
||||
#define BCM2708_PERI_BASE 0x20000000
|
||||
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <bcm2835.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define MAXTIMINGS 100
|
||||
|
||||
//#define DEBUG
|
||||
|
||||
#define DHT11 11
|
||||
#define DHT22 22
|
||||
#define AM2302 22
|
||||
|
||||
int readDHT(int type, int pin);
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (!bcm2835_init())
|
||||
return 1;
|
||||
|
||||
if (argc != 3) {
|
||||
printf("usage: %s [11|22|2302] GPIOpin#\n", argv[0]);
|
||||
printf("example: %s 2302 4 - Read from an AM2302 connected to GPIO #4\n", argv[0]);
|
||||
return 2;
|
||||
}
|
||||
int type = 0;
|
||||
if (strcmp(argv[1], "11") == 0) type = DHT11;
|
||||
if (strcmp(argv[1], "22") == 0) type = DHT22;
|
||||
if (strcmp(argv[1], "2302") == 0) type = AM2302;
|
||||
if (type == 0) {
|
||||
printf("Select 11, 22, 2302 as type!\n");
|
||||
return 3;
|
||||
}
|
||||
|
||||
int dhtpin = atoi(argv[2]);
|
||||
|
||||
if (dhtpin <= 0) {
|
||||
printf("Please select a valid GPIO pin #\n");
|
||||
return 3;
|
||||
}
|
||||
|
||||
|
||||
printf("Using pin #%d\n", dhtpin);
|
||||
readDHT(type, dhtpin);
|
||||
return 0;
|
||||
|
||||
} // main
|
||||
|
||||
|
||||
int bits[250], data[100];
|
||||
int bitidx = 0;
|
||||
|
||||
int readDHT(int type, int pin) {
|
||||
int counter = 0;
|
||||
int laststate = HIGH;
|
||||
int j=0;
|
||||
|
||||
// Set GPIO pin to output
|
||||
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP);
|
||||
|
||||
bcm2835_gpio_write(pin, HIGH);
|
||||
usleep(500000); // 500 ms
|
||||
bcm2835_gpio_write(pin, LOW);
|
||||
usleep(20000);
|
||||
|
||||
bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);
|
||||
|
||||
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
|
||||
|
||||
// wait for pin to drop?
|
||||
while (bcm2835_gpio_lev(pin) == 1) {
|
||||
usleep(1);
|
||||
}
|
||||
|
||||
// read data!
|
||||
for (int i=0; i< MAXTIMINGS; i++) {
|
||||
counter = 0;
|
||||
while ( bcm2835_gpio_lev(pin) == laststate) {
|
||||
counter++;
|
||||
//nanosleep(1); // overclocking might change this?
|
||||
if (counter == 1000)
|
||||
break;
|
||||
}
|
||||
laststate = bcm2835_gpio_lev(pin);
|
||||
if (counter == 1000) break;
|
||||
bits[bitidx++] = counter;
|
||||
|
||||
if ((i>3) && (i%2 == 0)) {
|
||||
// shove each bit into the storage bytes
|
||||
data[j/8] <<= 1;
|
||||
if (counter > 200)
|
||||
data[j/8] |= 1;
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
for (int i=3; i<bitidx; i+=2) {
|
||||
printf("bit %d: %d\n", i-3, bits[i]);
|
||||
printf("bit %d: %d (%d)\n", i-2, bits[i+1], bits[i+1] > 200);
|
||||
}
|
||||
#endif
|
||||
|
||||
printf("Data (%d): 0x%x 0x%x 0x%x 0x%x 0x%x\n", j, data[0], data[1], data[2], data[3], data[4]);
|
||||
|
||||
if ((j >= 39) &&
|
||||
(data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) {
|
||||
// yay!
|
||||
if (type == DHT11)
|
||||
printf("Temp = %d *C, Hum = %d \%\n", data[2], data[0]);
|
||||
if (type == DHT22) {
|
||||
float f, h;
|
||||
h = data[0] * 256 + data[1];
|
||||
h /= 10;
|
||||
|
||||
f = (data[2] & 0x7F)* 256 + data[3];
|
||||
f /= 10.0;
|
||||
if (data[2] & 0x80) f *= -1;
|
||||
printf("Temp = %.1f *C, Hum = %.1f \%\n", f, h);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
40
Fremddateien/Adafruit_DHT.py
Normal file
40
Fremddateien/Adafruit_DHT.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding:utf-8 -*-
|
||||
|
||||
import sys
|
||||
import dhtreader
|
||||
|
||||
DHT11 = 11
|
||||
DHT22 = 22
|
||||
AM2302 = 22
|
||||
|
||||
|
||||
dhtreader.init()
|
||||
|
||||
if len(sys.argv) != 3:
|
||||
print("usage: {0} [11|22|2302] GPIOpin#".format(sys.argv[0]))
|
||||
print("example: {0} 2302 Read from an AM2302 connected to GPIO #4".format(sys.argv[0]))
|
||||
sys.exit(2)
|
||||
|
||||
dev_type = None
|
||||
if sys.argv[1] == "11":
|
||||
dev_type = DHT11
|
||||
elif sys.argv[1] == "22":
|
||||
dev_type = DHT22
|
||||
elif sys.argv[1] == "2302":
|
||||
dev_type = AM2302
|
||||
else:
|
||||
print("invalid type, only 11, 22 and 2302 are supported for now!")
|
||||
sys.exit(3)
|
||||
|
||||
dhtpin = int(sys.argv[2])
|
||||
if dhtpin <= 0:
|
||||
print("invalid GPIO pin#")
|
||||
sys.exit(3)
|
||||
|
||||
#print("using pin #{0}".format(dhtpin))
|
||||
t, h = dhtreader.read(dev_type, dhtpin)
|
||||
if t and h:
|
||||
print "%.3f; %.3f" % (t, h)
|
||||
else:
|
||||
print("Failed to read from sensor, maybe try again?")
|
|
@ -1,3 +1,4 @@
|
|||
Für alle Dateien von Adafruit
|
||||
============
|
||||
Copyright (c) 2012-2013 Limor Fried, Kevin Townsend and Mikey Sklar for Adafruit Industries.
|
||||
All rights reserved.
|
||||
|
@ -23,3 +24,15 @@ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
Für airsensor/airsensor.c
|
||||
============
|
||||
https://code.google.com/p/usb-sensors-linux/
|
||||
|
||||
Für send.py
|
||||
============
|
||||
(c) hakermania http://ubuntuforums.org/showthread.php?t=1472520&p=12104667#post12104667
|
||||
|
||||
Für transpose.py
|
||||
===========
|
||||
(c) Peter Reutemann: http://www.cs.waikato.ac.nz/~fracpete/programming/csv2gnuplot/
|
||||
|
|
BIN
Fremddateien/dhtreader.so
Executable file
BIN
Fremddateien/dhtreader.so
Executable file
Binary file not shown.
14
export.sh
14
export.sh
|
@ -57,15 +57,17 @@ do
|
|||
temp4=$(echo "scale=3; $(grep 't=' /sys/bus/w1/devices/w1_bus_master1/10-00080277a5db/w1_slave | awk -F 't=' '{print $2}') / 1000" | bc -l)
|
||||
done
|
||||
|
||||
luft_roh=$(sudo /home/pi/Temperaturmessung/Fremddateien/Adafruit_DHT 2302 17 |grep Hum ) # Rohdaten des Luftfeuchtigkeits-Sensors
|
||||
luft_temp=$(echo $luft_roh | cut -c 8,9,10,11) # Luftfeuchtigkeit-Sensor auftrennen
|
||||
luft_feucht=$(echo $luft_roh | cut -c 23,24,25,26)
|
||||
luft_roh=$(sudo python /home/pi/Temperaturmessung/Fremddateien/Adafruit_DHT.py 2302 17) # Rohdaten des Luftfeuchtigkeits-Sensors
|
||||
set -- $luft_roh
|
||||
luft_temp=$1
|
||||
luft_feucht=$2
|
||||
while [ -z "$luft_roh" ] || [ "$(echo $luft_temp '>' 40 | bc -l)" -eq 1 ] || [ "$(echo $luft_temp '<' -20 | bc -l)" -eq 1 ]
|
||||
do
|
||||
echo "----Luft: $luft_roh"
|
||||
luft_roh=$(sudo /home/pi/Temperaturmessung/Fremddateien/Adafruit_DHT 2302 17 |grep Hum )
|
||||
luft_temp=$(echo $luft_roh | cut -c 8,9,10,11) # Luftfeuchtigkeit-Sensor auftrennen
|
||||
luft_feucht=$(echo $luft_roh | cut -c 23,24,25,26)
|
||||
luft_roh=$(sudo python /home/pi/Temperaturmessung/Fremddateien/Adafruit_DHT.py 2302 17) # Rohdaten des Luftfeuchtigkeits-Sensors
|
||||
set -- $luft_roh
|
||||
luft_temp=$1
|
||||
luft_feucht=$2
|
||||
done
|
||||
druck_roh=$(sudo python /home/pi/Temperaturmessung/Fremddateien/Adafruit_BMP085_auswertung.py) # Rohdaten des Luftdruck-Sensors
|
||||
set -- $druck_roh #Zerlegen mithilfe von IFS (siehe ganz oben)
|
||||
|
|
Reference in a new issue