Trying to build a wireless DHT22 sensor that logs data to PLX-DAQ



  • I have a DHT22 Temperature/Humidity sensor. I can connect it wirelessly (And give it power), and read Temp and Humidity off it if I load some code from MySensors.org and use their version of the DHT library.

    If I flash my sensor with some code from Adafruit and use their DHT library, I can connect the sensor via usb, run PLC-DAQ on the computer it is connected to, and the Temp and Humidity values are passed into an Excel spreadheet.

    I am looking for some code to allow my sensor to connect wirelessly, then pass the data to PLC-DAQ.

    I got the code below to compile by copying and pasting the relevant parts of the Setup and Void functions (Again, terminology may be off) from the Mysensors.org and Adafruit code, then renaming all instances of the Adafruit DHT to DHTP. If you look in the code below, all the lines that end with // are the ones I edited. Except the ones where it's obviously a comment from the developer.

    I also had to rename one of the classes (Adafruit version of DHT.h, may be using the wrong terminology) to DHTP.h and DHTP.cpp.

    I have included the code I reworked below. Here is a link to the working Adafruit code http://108.51.175.78:8069/shelli-rae/sensor_plx_daq.ino , and here is a link to the working Mysensors.org code http://108.51.175.78:8069/shelli-rae/sensor_humidity_vera.ino

    If I flash my Arduino with either one and use their version of the DHT.h library either respective function works fine.

    Thank you for any help/suggestions.

    #include <Sleep_n0m1.h>
    #include <SPI.h>
    #include <EEPROM.h>
    #include <RF24.h>
    #include <Sensor.h>
    #include <DHT.h>
    #include <DHTP.h>//

    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    #define HUMIDITY_SENSOR_DIGITAL_PIN 3
    unsigned long SLEEP_TIME = 30; // Sleep time between reads (in seconds)

    #define DHTPIN 3//
    #define DHTTYPE DHT22//

    Sensor gw;
    DHT dht;
    Sleep sleep;
    float lastTemp;
    float lastHum;
    boolean metric = true;

    DHTP dhtp(DHTPIN, DHTTYPE);//

    void setup()
    {
    gw.begin();
    dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);

    // Send the Sketch Version Information to the Gateway
    gw.sendSketchInfo("Humidity", "1.0");

    // Register all sensors to gw (they will be created as child devices)
    gw.sendSensorPresentation(CHILD_ID_HUM, S_HUM);
    gw.sendSensorPresentation(CHILD_ID_TEMP, S_TEMP);

    metric = gw.isMetricSystem();{

    Serial.begin(38400);//
    Serial.println("LABEL,Time,Temperature (F), Humidity (%)"); //

    dhtp.begin();//
    }
    }

    void loop()
    {
    delay(dht.getMinimumSamplingPeriod());

    float temperature = dht.getTemperature();
    if (isnan(temperature)) {
    Serial.println("Failed reading temperature from DHT");
    } else if (temperature != lastTemp) {
    lastTemp = temperature;
    if (!metric) {
    temperature = dht.toFahrenheit(temperature);
    }
    gw.sendVariable(CHILD_ID_TEMP, V_TEMP, temperature, 1);
    Serial.print("T: ");
    Serial.println(temperature);
    }

    float humidity = dht.getHumidity();
    if (isnan(humidity)) {
    Serial.println("Failed reading humidity from DHT");
    } else if (humidity != lastHum) {
    lastHum = humidity;
    gw.sendVariable(CHILD_ID_HUM, V_HUM, humidity, 1);
    Serial.print("H: ");
    Serial.println(humidity);
    }

    {//
    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float h = dhtp.readHumidity();//
    float t = dhtp.readTemperature();//
    t = ((t*1.8)+32);//Converted from Celsius to Farenheit
    delay(60000);//

    // check if returns are valid, if they are NaN (not a number) then something went wrong!
    if (isnan(t) || isnan(h)) {//
    Serial.println("Failed to read from DHT");//
    } else {
    Serial.print("DATA,TIME,"); Serial.print(t); Serial.print(","); Serial.println(h);
    }//
    }//

    // Power down the radio. Note that the radio will get powered back up
    // on the next write() call.
    delay(1000); //delay to allow serial to fully print before sleep
    gw.powerDown();
    sleep.pwrDownMode(); //set sleep mode
    sleep.sleepDelay(SLEEP_TIME * 1000); //sleep for: sleepTime
    }


Log in to reply
 

Suggested Topics

  • 8
  • 1
  • 3
  • 2
  • 90
  • 29

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts