Temperature & Humidity Sensor Pro

From Nearwiki
Jump to: navigation, search


  • Technical Spec: Humidytly: 5% RH - 99% RH - Temperature -40°C - 80°C - Acuracy: 2% RH / 0.5°C



Temperature & Humidity Sensor Pro


Grove Tem Humid.jpg



MyNbios Code - Example


In order to run this code you should copy the following code into the area delimited by the MY_NBIOS CUSTOM CODE banners in the Hello_World_xxx.ino example.


void Nearbus::MyNbios_0( byte portId, UINT setValue, ULONG* pRetValue, byte vmcuMethod, PRT_CNTRL_STRCT* pPortControlStruct  )
{ 
UINT data[6];
UINT _pin;
UINT _count = 6;
ULONG _lastreadtime = 0;
boolean firstreading = true;
float h;
float t;  
byte ret;
UINT laststate = HIGH;
UINT counter = 0;
UINT j = 0;
UINT  i;
ULONG currenttime;
        
        //************************************
        // Reconfiguring Ports as I/O
        //************************************
        if( pPortControlStruct->portMode != MYDRIVER_MODE ) 
        {
            PortModeConfig( portId, MYDRIVER_MODE );        
        }
        
        //************************************
        // Custom Function
        //************************************
          _pin = pPortControlStruct->pinId;
         
          // pull the pin high and wait 250 milliseconds
          digitalWrite(_pin, HIGH);
          delay(250);

          currenttime = millis();
          if (currenttime < _lastreadtime) {
              // ie there was a rollover
              _lastreadtime = 0;
          }
          if (!firstreading && ((currenttime - _lastreadtime) < 2000)) {
              ret = 0; // return last correct measurement
              //delay(2000 - (currenttime - _lastreadtime));
          }
          else {
              firstreading = false;
              _lastreadtime = millis();
            
              data[0] = data[1] = data[2] = data[3] = data[4] = 0;
              
              // now pull it low for ~20 milliseconds
              pinMode(_pin, OUTPUT);
              digitalWrite(_pin, LOW);
              delay(20);
              cli();
              digitalWrite(_pin, HIGH);
              delayMicroseconds(40);
              pinMode(_pin, INPUT);
            
              // read in timings
              for ( i=0; i< 85; i++) {   // #define MAXTIMINGS = 85 // how many timing transitions we need to keep track of. 2 * number bits + extra
                  counter = 0;
                  while (digitalRead(_pin) == laststate) {
                      counter++;
                      delayMicroseconds(1);
                      if (counter == 255) {
                          break;
                      }
                  }
                  laststate = digitalRead(_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 > _count)
                          data[j/8] |= 1;
                      j++;
                  }
              }
              sei();
            
              // check we read 40 bits and that the checksum matches
              if ((j >= 40) && 
                  (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) {
                  ret = 0;
              }
              else{
                  ret = 1;
              }
          }
          if (ret == 0) {
              h = data[0];
              h *= 256;
              h += data[1];
              h *= 10;

              t = data[2] & 0x7F;
              t *= 256;
              t += data[3];
              t *= 10;
              if (data[2] & 0x80)
              t *= -1;
          }
          else {
              t = 0;
              h = 0;    
          }  
          // delay(250); 
                
        //************************************
        // Updating register 
        //************************************         
        * pRetValue = (ULONG) t;                
}