Hallo zusammen,
seit dem letzten Update (vermutlich auf Raspbian Stretch) kann ich seltsamerweise einen an den RPI3 angeschlossenen DHT22 nicht mehr mit u.g. c-Skript auslesen.
Allerdings funktioniert das komischerweise noch über pilight: dort ist der Sensor konfiguriert und wird korrekt ausgelesen.
C
/*
* dht.c:
* read temperature and humidity from DHT11 or DHT22 sensor
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#define MAX_TIMINGS 25
#define DHT_PIN 3 /* GPIO-22 */
int data[5] = { 0, 0, 0, 0, 0 };
int read_dht_data()
{
uint8_t laststate = HIGH;
uint8_t counter = 0;
uint8_t j = 0, i;
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
/* pull pin down for 18 milliseconds */
pinMode( DHT_PIN, OUTPUT );
digitalWrite( DHT_PIN, LOW );
delay( 18 );
/* prepare to read the pin */
pinMode( DHT_PIN, INPUT );
/* detect change and read data */
for ( i = 0; i < MAX_TIMINGS; i++ )
{
counter = 0;
while ( digitalRead( DHT_PIN ) == laststate )
{
counter++;
delayMicroseconds( 1 );
if ( counter == 255 )
{
break;
}
}
laststate = digitalRead( DHT_PIN );
if ( counter == 255 )
break;
/* ignore first 3 transitions */
if ( (i >= 4) && (i % 2 == 0) )
{
/* shove each bit into the storage bytes */
data[j / 8] <<= 1;
if ( counter > 16 )
data[j / 8] |= 1;
j++;
}
}
float ccc = (float)(((data[2] & 0x7F) << 8) + data[3]) / 10;
printf( "debug Resdata: %.1f \n", ccc );
/*
* check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
* print it out if data is good
*/
if ( (j >= 40) &&
(data[4] == ( (data[0] + data[1] + data[2] + data[3]) & 0xFF) ) )
{
float h = (float)((data[0] << 8) + data[1]) / 10;
if ( h > 100 )
{
h = data[0]; // for DHT11
}
float c = (float)(((data[2] & 0x7F) << 8) + data[3]) / 10;
if ( c > 125 )
{
c = data[2]; // for DHT11
}
if ( data[2] & 0x80 )
{
c = -c;
}
//float f = c * 1.8f + 32;
printf( "Humidity = %.1f %%; Temperature = %.1f *C \n", h, c );
char buff[100];
time_t now = time (0);
strftime (buff, 100, "%Y-%m-%d %H:%M:%S", localtime (&now));
printf ("%s;%.1f;%.1f\n", buff, c, h);
return 1;
}else {
//printf( "Data not good, skip\n" );
return -1;
}
}
int main( void )
{
printf( "Raspberry Pi DHT11/DHT22 temperature/humidity test\n" );
if ( wiringPiSetup() == -1 )
exit( 1 );
while ( 1 )
{
int i = read_dht_data();
printf( "Raspberry Pi DHT11/DHT22 temperature/humidity test. Result %d \n",i);
delay(1000);
if(i == 1){
delay( 10000 ); /* wait 10 seconds before next read */ //only if i==1, which means: successfully read before
}
}
return(0);
}
Alles anzeigen
Ausgabe stets:
debug Resdata: 0.0
Raspberry Pi DHT11/DHT22 temperature/humidity test
Ich habe nichts am Hardware-Aufbau geändert. Prinzipiell kann der Sensor ja auch ausgelesen werden, aber komischerweise nicht mehr mit o.g. Skript, was zuvor funktioniert hat. Habt ihr eine Idee, woran das liegen kann?
wiringPi Bibliothek ist wohl weiterhin installiert?!