DHT support for raspberry pi?


#1

I’m trying to use the PietteTech_DHT library to read a DHT22 on my raspberry pi 3.

I’ve verified I can read data off the sensor outside of the particle-agent using the Adafruit_DHT python lib, but am getting read timeout errors using the PIetteTech lib and particle-agent.

I’ve tried modifying the timeout for reads and changing pins to no avail.

Thanks for any help,
Scott


#2

What version of the lib are you using and how do you read (sync via dht.acquireAndWait(1000) or async via dht.acquire())?

Since that lib uses interrupts to do the async reading, and I can’t really find any docs about attachInterrupt() for RPi, I’m not sure you can read async.

Maybe @jvanier can chime in on the plans on interrupts on RPi.


Update:
As I’ve just confirmed with @jvanier, currently interrupts are not supported via the Particle framework on RPi and it’s a low priority issue too.
So you’ll have to go for a non-interrupt driven library on the RPi for now.


#3

Good to know, thanks for your help!


#4

Well I’m not sure if it helps, but I got the DHT22 working with the following workaround:

  1. I installed the DHT22 python library from https://github.com/adafruit/Adafruit_Python_DHT.git

  2. Ran the example python script to see if the sensor is attached correctly

  3. changes the output format of the example script to print temp/humid as floats by changing the print line to

if humidity is not None and temperature is not None:
print(’{0:0.1f}\n{1:0.1f}’.format(temperature, humidity))

  1. in the particle sketch i wrote a function to read an parse those values

void readDHT(){

//Run python script => https://github.com/adafruit/Adafruit_Python_DHT.git
Process proc = Process::run("/home/pi/AdafruitDHT.py 22 24");
//Wait for script to finish
proc.wait();
//parse output to variables
float temp=proc.out().parseFloat();
float humid=proc.out().parseFloat();

//Your code here

}


#5

I tryed also Adafruit_DHT, w/o success. It is always reading NaN from the sensor.
Then I tried this workaround, and … it works.

Any idea why RPI3 agent simply cannot read from digital pin while other software can?


#6

I was just reading your post as I am about to start working with a Particle Pi.
On Photons, I have used below sketch for a long time, as it needs no library and no interrupts.
It may help you to run it on the Particle Pi.

STARTUP(WiFi.selectAntenna(ANT_AUTO)); // FAVORITE: continually switches at high speed between antennas
char str[255]; // String for publishing events

// *D6 - RoomSense TEMP/HUM
  #include "math.h" // to calculate mathematical functions!

  int THpin = D6;
  double ROOMTemp2;
  double ROOMHumi;
  double ROOMTdf;     // Dewpoint temperature
  int SafeMargin = 5; // Safety margin between Dewpoint and Set Minimum temperature.
  double ROOMTout;    // We can safely let the room temperature sink to this level when nobody is home for a long time.
  unsigned int loopCnt;
  unsigned long xtime;
  int bits[40] = {0};// Store sensor output train: 40 "bits" in integer format
  
  int getTempHumInterval = 300 * 1000;
  int getTempHumLastTime = millis() - getTempHumInterval;




void setup()
{

// *D6 - RoomSense TEMP/HUM
  Particle.variable("ROOM_Temp2", &ROOMTemp2, DOUBLE);
  Particle.variable("ROOM_Humid", &ROOMHumi, DOUBLE);
  Particle.variable("ROOM_Tout", &ROOMTout, DOUBLE);
  
}



void loop()
{
// *D6 - RoomSense TEMP/HUM
 if ((millis()-getTempHumLastTime)>getTempHumInterval)
 {
  ReadTempHum();
  
  sprintf(str, "\"ROOM Temp2 + Humid + Tout\": %2.1f + %2.0f + %2.1f",ROOMTemp2,ROOMHumi,ROOMTout);
  Particle.publish("Status-ROOM", str,60,PRIVATE);
  // Put eventual actions/calculations with Temp & Hum here...
  getTempHumLastTime = millis();
 }
}



// *D6 - RoomSense TEMP/HUM
void ReadTempHum()
{
  reStart:
  
  pinMode(THpin,OUTPUT);
  digitalWrite(THpin,LOW);
  delay(20);
  digitalWrite(THpin,HIGH);
  delayMicroseconds(40);
  digitalWrite(THpin,LOW);
  
  pinMode(THpin,INPUT);
  
  loopCnt=10000;
  while(digitalRead(THpin) == LOW)
  {
    if(loopCnt-- == 0)
    {
      goto reStart;
    }
  }
  
  loopCnt=30000;
  while(digitalRead(THpin) == HIGH)
  {
    if(loopCnt-- == 0)
    {
      goto reStart;
    }
  }
  
  for(int i=0;i<40;i++)
  {
    while(digitalRead(THpin) == LOW)
    {
    }
    
    xtime = micros();
    
    while(digitalRead(THpin) == HIGH)
    {
    }
    
    if (micros() - xtime >50)
    {
      bits[i]=1;
    }
    else
    {
      bits[i]=0;
    }
  }

  ROOMHumi = bits[6]*512+bits[7]*256+bits[8]*128+bits[9]*64+bits[10]*32+bits[11]*16+bits[12]*8+bits[13]*4+bits[14]*2+bits[15];
  
  ROOMHumi = ROOMHumi/10;

  ROOMTemp2 = bits[22]*512+bits[23]*256+bits[24]*128+bits[25]*64+bits[26]*32+bits[27]*16+bits[28]*8+bits[29]*4+bits[30]*2+bits[31];
  
  if (bits[16]==1)
  {
   ROOMTemp2 = ROOMTemp2/-10;
  }
  else
  {
   ROOMTemp2 = ROOMTemp2/10;
  }
  // Dewpoint calculation, based on: http://en.wikipedia.org/wiki/Dew_point
  double a = 17.271;
  double b = 237.7;
  double calc1 = (a * (double) ROOMTemp2) / (b + (double) ROOMTemp2) + log( (double) ROOMHumi/100);
  
  ROOMTdf = (b * calc1) / (a - calc1);
  ROOMTout = ROOMTdf + SafeMargin;
}

:wave::older_man: