Announcement

Collapse
No announcement yet.

DHT11 DHT22 Humidity sensors API

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    DHT11 DHT22 Humidity sensors API

    Anyone got any API code or tips on how to read these cheap humidity sensors. I've got dreams of controlling my bathroom extractors.

    Thanks.

    #2
    There's a good multi-page tutorial here: https://learn.adafruit.com/dht. It describes the differences between the two, how to hook-up to the Arduino and some sample code.

    Cheers
    Al
    HS 4.2.8.0: 2134 Devices 1252 Events
    Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

    Comment


      #3
      This is untested code but it should send you in the correct direction.


      PHP Code:
      //For serial set to 0 and for Ethernet set to 1
      #define ISIP 0

      //Do NOT modify these
      #if ISIP == 1
      #include <EEPROM.h>
      #include <SPI.h>       
      #include <Ethernet.h>
      #include <EthernetUdp.h> 
      #endif
      /************************************************************
       *Arduino to Homeseer 3 Plugin API writen by Enigma Theatre.*
       * V1.0.0.36                                                *
       *                                                          *
       *******Change the values below only*************************
       */


      //Address of the board.
      const byte BoardAdd 1;

      #if ISIP == 1
      // Enter a MAC address and IP address for your board below.
      byte mac[] = {0xDE0xAD0xBE0xEF0xFE0xED};

      // The IP address will be dependent on your local network.
      IPAddress ip(192,168,0,100);     //IP entered in HS config.
      const unsigned int localPort 9000;      //port entered in HS config.
      IPAddress HomeseerIP(192,168,0,123); //Homeseer IP address
      IPAddress ServerIP(EEPROM.read(2),EEPROM.read(3),EEPROM.read(4),EEPROM.read(5));
      byte EEpromVersion EEPROM.read(250);
      #endif


      //************Do not change anything in Here*****************
      int FromHS[10];                                          // *
      boolean IsConnected false;                             // *
      #if ISIP == 1                                            // *
      char packetBuffer[UDP_TX_PACKET_MAX_SIZE];               // *
      EthernetUDP Udp;                                         // *
      const unsigned int ServerPort 8888;                    // *
      #endif                                                   // *
      void(* resetFunc) (void) = 0;                            // *
      //***********************************************************

      #include "DHT.h"
      #define DHTPIN 2     // what pin we're connected to
      // Uncomment whatever type you're using!
      //#define DHTTYPE DHT11   // DHT 11 
      #define DHTTYPE DHT22   // DHT 22  (AM2302)
      //#define DHTTYPE DHT21   // DHT 21 (AM2301)

      DHT dht(DHTPINDHTTYPE);

      int SendDelay 3000;
      long LastSend;

      void setup() {
        
      HSSetup();
        
      //************************
        //Add YOUR SETUP HERE;
        //************************


      dht.begin();

      }




      void loop() {
      #if ISIP == 1
        
      IsUDP();
      #endif

          //************************
          //Add YOUR CODE HERE;
          //************************
          /* To Send Data to Homeseer use SendToHS(Device,Value)
           Eg.. SendToHS(1,200); where 1 is the API device in homeseer and 200 is the value to send
           To Recieve data from Homeseer look up the FromHS array that is updated when the device value changes.
           Eg.. FromHS[5] would be the data from API Output device 5
           All code that is located just below this block will execute regardless of connection status!
           You can include SendToHS() calls, however when there isn't an active connection, it will just return and continue.
           If you only want code to execute when HomeSeer is connected, put it inside the if statement below.
           */

      /*Execute regardless of connection status*/


       
      if (IsConnected == true) {
         
         
          if(
      millis() - LastSend SendDelay){
            
         
      SendToHS(1,dht.readHumidity());
         
      SendToHS(2,dht.readTemperature());
         
          
      LastSend=millis();
          }
         
         
         
      /*Execute ONLY when HomeSeer is connected*/

        
      }
      }


























































      const 
      charVersion "API1.0.0.36";

      byte Byte1,Byte2,Byte3;
      int Byte4,Byte5;


      void HSSetup() {

      #if ISIP == 1
          
      if (EEpromVersion!=22) {
          
      ServerIP=HomeseerIP;
          
      EEPROM.write(2,ServerIP[0]);
          
      EEPROM.write(3,ServerIP[1]);
          
      EEPROM.write(4,ServerIP[2]);
          
      EEPROM.write(5,ServerIP[3]);
          
      EEPROM.write(250,22); //Store the version where the eeprom data layout was last changed
          
      EEpromVersion=22;
        }
      Ethernet.begin(mac,ip);
        
      Udp.begin(localPort);
        
      Udp.setTimeout(0);
        
      delay(1000);
      SendConnect();
      #else
        
      Serial.begin(115200);
        
      Serial.flush();
        
      Serial.setTimeout(0);
        
      delay(1000);
        
      Serial.print("Connect ");
        
      Serial.println(BoardAdd);
      #endif

        
      IsConnected false;

      }

      void SendConnect()
      {
      #if ISIP == 0
        
      Serial.print("Connect ");
        
      Serial.println(BoardAdd);
      #else
          
      Udp.beginPacket(ServerIP,ServerPort);  //First send a connect packet to the dynamic IP stored in eeprom
          
      Udp.print("Connect ");
          
      Udp.print(BoardAdd);
          
      Udp.endPacket();
          if (
      ServerIP!=HomeseerIP) {
            
      Udp.beginPacket(HomeseerIP,ServerPort);  //Then if the stored value doesn't match the pre-specified one, send a connect packet there also
            
      Udp.print("Connect ");
            
      Udp.print(BoardAdd);
            
      Udp.endPacket();
          }
       
      #endif
      }


      #if ISIP == 1
      void IsUDP(){
        
      int packetSize Udp.parsePacket();
        if(
      packetSize)
        {
          
      IPAddress remote Udp.remoteIP();
          
      Byte1 =Udp.parseInt();
          
      Udp.read(); 
          
      Byte2 =Udp.read(); 
          
      Byte3 =Udp.parseInt();
          
      Byte4 =Udp.parseInt();
          
      Byte5 =Udp.parseInt();
          
      DataEvent();
        }
      }

      #else
      void serialEvent() {
        while (
      Serial.available() > 0) {
          
      delay(17);


          
      Byte1 Serial.parseInt();
          
      Serial.read();  
          
      Byte2 Serial.read(); 
          
      Byte3 Serial.parseInt();
          
      Byte4 Serial.parseInt();
          
      Byte5 Serial.parseInt();
          
      DataEvent();
        }
      }
      #endif


      /*

      Used Data Input Cases
      D Disconnect
      r reset
      K Keepalive
      O PinMode Output Set
      d Input debounce time set
      C Connect request
      c Connection established - report current status
      */
      void DataEvent() {



        if (
      Byte1 == BoardAdd) {
          switch (
      Byte2) {

          case 
      'c':
            
      IsConnected true;
      #if ISIP == 1
            
      if (Udp.remoteIP() != ServerIP) {
              
      ServerIP=Udp.remoteIP();
              
      EEPROM.write(2,ServerIP[0]);
              
      EEPROM.write(3,ServerIP[1]);
              
      EEPROM.write(4,ServerIP[2]);
              
      EEPROM.write(5,ServerIP[3]);
            }     
      #endif

            
      break;

          case 
      'C':   
      #if ISIP == 1
            
      Udp.beginPacket(Udp.remoteIP(), ServerPort);
            
      Udp.print("Version ");
            
      Udp.print(BoardAdd);
            
      Udp.print(" ");
            
      Udp.print(Version);
            
      Udp.println(" HS3");
            
      Udp.endPacket();

            
      Udp.beginPacket(Udp.remoteIP(), ServerPort);
            
      delay(100);
            
      Udp.print("Connected ");
            
      Udp.println(BoardAdd);
            
      Udp.endPacket();
      #else
            
      Serial.print("Version ");
            
      Serial.print(BoardAdd);
            
      Serial.print(" ");
            
      Serial.print(Version);
            
      Serial.println(" HS3"); 
            
      delay(100);
            
      Serial.print("Connected ");
            
      Serial.println(BoardAdd);
      #endif
            
      delay(100);
            
      IsConnected false;
            break;

          case 
      'K':
            
      delay(200);
      #if ISIP == 1
            
      Udp.beginPacket(Udp.remoteIP(), ServerPort);
            
      Udp.print("Alive ");
            
      Udp.println(BoardAdd);
            
      Udp.endPacket();
            if (
      Udp.remoteIP() != ServerIP) {
              
      ServerIP=Udp.remoteIP();
              
      EEPROM.write(2,ServerIP[0]);
              
      EEPROM.write(3,ServerIP[1]);
              
      EEPROM.write(4,ServerIP[2]);
              
      EEPROM.write(5,ServerIP[3]);
            }     
      #else     
            
      Serial.print("Alive ");
            
      Serial.println(BoardAdd);
      #endif
            
      break; 
            
            case 
      'r':
            
      delay(200);
            
      resetFunc();  //call reset
            
      break; 

          case 
      'O':
            
      FromHS[Byte3] = Byte4;
            break; 

          case 
      'D':
            
      IsConnected false;
            break;   
          }
        }
      }

      void SendToHS(byte Devicelong Data){
      if (
      IsConnected == true) {
      #if ISIP == 1
        
      Udp.beginPacket(Udp.remoteIP(), ServerPort);
        
      Udp.print(BoardAdd);
        
      Udp.print(" API ");
        
      Udp.print(Device);
        
      Udp.print(" ");
        
      Udp.print(Data);
        
      Udp.endPacket();
      #else
        
      Serial.print(BoardAdd);
        
      Serial.print(" API ");
        
      Serial.print(Device);
        
      Serial.print(" ");
        
      Serial.println(Data);
      #endif
      }


      Greig.
      Zwave = Z-Stick, 3xHSM100� 7xACT ZDM230, 1xEverspring SM103, 2xACT HomePro ZRP210.
      X10 = CM12U, 2xAM12, 1xAW10, 1 x TM13U, 1xMS13, 2xHR10, 2xSS13
      Other Hardware = ADI Ocelot + secu16, Global Cache GC100, RFXtrx433, 3 x Foscams.
      Plugings = RFXcom, ActiveBackup, Applied Digital Ocelot, BLDeviceMatrix, BLGarbage, BLLAN, Current Cost, Global Cache GC100,HSTouch Android, HSTouch Server, HSTouch Server Unlimited, NetCAM, PowerTrigger, SageWebcamXP, SqueezeBox, X10 CM11A/CM12U.
      Scripts =
      Various

      Comment


        #4
        Thanks - it was more of a question if anyone has coded it for this api into homeseer, before I go and try it code it myself.

        Edit - my reply was in response to Sparkman. I'll look at your example Greig - thanks.

        Comment


          #5
          Received my Ethernet shield today, so could test the code - Happy to report that it works as posted. I've now ordered 4 X DH22's and will adapt the code to drive those sensors and report back.

          Many thanks.

          Comment


            #6
            Good news. Thanks for the info. I think I will move this to a API code thread some time and we can add more examples.

            Sent from my HTC One using Tapatalk
            Zwave = Z-Stick, 3xHSM100� 7xACT ZDM230, 1xEverspring SM103, 2xACT HomePro ZRP210.
            X10 = CM12U, 2xAM12, 1xAW10, 1 x TM13U, 1xMS13, 2xHR10, 2xSS13
            Other Hardware = ADI Ocelot + secu16, Global Cache GC100, RFXtrx433, 3 x Foscams.
            Plugings = RFXcom, ActiveBackup, Applied Digital Ocelot, BLDeviceMatrix, BLGarbage, BLLAN, Current Cost, Global Cache GC100,HSTouch Android, HSTouch Server, HSTouch Server Unlimited, NetCAM, PowerTrigger, SageWebcamXP, SqueezeBox, X10 CM11A/CM12U.
            Scripts =
            Various

            Comment


              #7
              May of spoke too soon, not sure if this was doing this from the outset or just today, but I'm getting two entries of this:

              Feb-20 20:21:31Arduino PluginException in RecievedText: Object reference not set to an instance of an object.Feb-20 20:21:31Arduino PluginException in RecievedText: Object reference not set to an instance of an object.
              Every couple seconds, i.e every time it sends an update. The virtual devices are updating with the values and they correspond to what i'm seeing via the serial monitor.

              Any idea what this is?

              Comment


                #8
                Originally posted by ecuboss View Post
                May of spoke too soon, not sure if this was doing this from the outset or just today, but I'm getting two entries of this:

                Feb-20 20:21:31Arduino PluginException in RecievedText: Object reference not set to an instance of an object.Feb-20 20:21:31Arduino PluginException in RecievedText: Object reference not set to an instance of an object.
                Every couple seconds, i.e every time it sends an update. The virtual devices are updating with the values and they correspond to what i'm seeing via the serial monitor.

                Any idea what this is?
                can you send me a debug log so I can see what it is.
                Enable debug logging at the top of the plugin config page then connect the board until you get the error then got back to the config page and disable debug logging and click the download link that is created. Send this to me at my personal address that is also available by clicking my name at the top of the config page.

                Greig.
                Zwave = Z-Stick, 3xHSM100� 7xACT ZDM230, 1xEverspring SM103, 2xACT HomePro ZRP210.
                X10 = CM12U, 2xAM12, 1xAW10, 1 x TM13U, 1xMS13, 2xHR10, 2xSS13
                Other Hardware = ADI Ocelot + secu16, Global Cache GC100, RFXtrx433, 3 x Foscams.
                Plugings = RFXcom, ActiveBackup, Applied Digital Ocelot, BLDeviceMatrix, BLGarbage, BLLAN, Current Cost, Global Cache GC100,HSTouch Android, HSTouch Server, HSTouch Server Unlimited, NetCAM, PowerTrigger, SageWebcamXP, SqueezeBox, X10 CM11A/CM12U.
                Scripts =
                Various

                Comment


                  #9
                  All sent over to you.

                  Comment


                    #10
                    Originally posted by ecuboss View Post
                    All sent over to you.
                    I have replied to both you addresses.

                    Greig.
                    Zwave = Z-Stick, 3xHSM100� 7xACT ZDM230, 1xEverspring SM103, 2xACT HomePro ZRP210.
                    X10 = CM12U, 2xAM12, 1xAW10, 1 x TM13U, 1xMS13, 2xHR10, 2xSS13
                    Other Hardware = ADI Ocelot + secu16, Global Cache GC100, RFXtrx433, 3 x Foscams.
                    Plugings = RFXcom, ActiveBackup, Applied Digital Ocelot, BLDeviceMatrix, BLGarbage, BLLAN, Current Cost, Global Cache GC100,HSTouch Android, HSTouch Server, HSTouch Server Unlimited, NetCAM, PowerTrigger, SageWebcamXP, SqueezeBox, X10 CM11A/CM12U.
                    Scripts =
                    Various

                    Comment


                      #11
                      Also receiving:- Exception in RecievedText:

                      Hi Greig,
                      I am receiving this error in my logs:-
                      No idea at all, the plugin seems to be running OK although it does restart all of its own accord for no apparent reason. I have not added anything to the boards (2 x Mega) and they are only used for inputs, they have been used since the very early versions of the plugins without any problem until recently.

                      Peter
                      Exception in RecievedText: Object reference not set to an instance of an object.

                      Comment


                        #12
                        Greig has fixed this in a newer version of the code (beta) - I've been running it for about a week now and it seems fine.

                        I just fitted the my dht22 into the bathroom yesterday and its controling the extractor fan via another arduino with a relay module.

                        I'll post up all mode code and hs scripts once Greig releases the updated code.
                        Last edited by ecuboss; March 1, 2015, 04:34 AM.

                        Comment


                          #13
                          Yes I have a fix and have asked for it to be in the updater but have not had a reply. Drop me an e-mail and I can send it to you or wait and I will post here when it is in the updater.

                          Sent from my HTC One using Tapatalk
                          Zwave = Z-Stick, 3xHSM100� 7xACT ZDM230, 1xEverspring SM103, 2xACT HomePro ZRP210.
                          X10 = CM12U, 2xAM12, 1xAW10, 1 x TM13U, 1xMS13, 2xHR10, 2xSS13
                          Other Hardware = ADI Ocelot + secu16, Global Cache GC100, RFXtrx433, 3 x Foscams.
                          Plugings = RFXcom, ActiveBackup, Applied Digital Ocelot, BLDeviceMatrix, BLGarbage, BLLAN, Current Cost, Global Cache GC100,HSTouch Android, HSTouch Server, HSTouch Server Unlimited, NetCAM, PowerTrigger, SageWebcamXP, SqueezeBox, X10 CM11A/CM12U.
                          Scripts =
                          Various

                          Comment


                            #14
                            No Worry - No hurry

                            Thanks Greig & ecuboss,
                            Not in a hurry here, will be lazy and wait until it is in the updater.
                            No idea what causes this error as far as I tell everything is OK my end!
                            Peter

                            Comment


                              #15
                              Hello.

                              Pervez2602, you managed to make it work?
                              If yes, is possible send me the arduino code for DHT11 for HS?

                              Thanks.

                              Comment

                              Working...
                              X