Convert to C# Humidity and Temperature Sensor - RHT03 RHT-22 - General Discussion - Netduino Forums
   
Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Convert to C# Humidity and Temperature Sensor - RHT03 RHT-22


  • Please log in to reply
20 replies to this topic

#1 rex64

rex64

    Member

  • Members
  • PipPip
  • 17 posts

Posted 01 January 2012 - 09:19 PM

I am trying to convert this sample code to C# for the Humidity and Temperature Sensor - RHT03 RHT-22. Sorry I am new to NetDuino and C#, and could not find anything writting in C# for this sensor online. Please try and make it easy for me to see the reasults in the debugger :)

http://www.sparkfun.com/products/10167

This is my bad attempt. I started commenting things out but there were lots of custom data types and I do not know the best way to get this this done. If someone can help me convert this it would be helpful.
  //****************************************************************//
//	              Program for AM230x series 
//MCU  AT89S52 ,   Frequency of crystal oscillator: 11.0592MHz
//Function Transmit RH & Temp. Data via PC interface ,  Baud rate 9600 
//Connection P2.0 connected with DHT sensor   
// Company   Aosong Electronics   
//****************************************************************//

//#define uchar unsigned char
//#define uint unsigned int
//#define   Data_0_time    4

using System;
using Microsoft.SPOT;

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace NetduinoApplication1
{







//using reg51.h;

//#include <reg51.h>
//#include <intrins.h> 
//
typedef unsigned char  U8;       /* defined for unsigned 8-bits integer variable 	     */
typedef signed   char  S8;       /* defined for signed 8-bits integer variable		   */
typedef unsigned int   U16;      /* defined for unsigned 16-bits integer variable 	     */
typedef signed   int   S16;      /* defined for signed 16-bits integer variable 	     */
typedef unsigned long  U32;      /* defined for unsigned 32-bits integer variable 	  */
typedef signed   long  S32;      /* defined for signed 32-bits integer variable 	      */
typedef float          F32;      /* single precision floating point variable (32bits)    */
typedef double         F64;      /* double precision floating point variable (64bits)    */
//


//----------------------------------------------//
//----------------Definition for IO interface--------------------//
//----------------------------------------------//
sbit  P2_0  = P2^0 ;
sbit  P2_1  = P2^1 ;
sbit  P2_2  = P2^2 ;
sbit  P2_3  = P2^3 ;
//----------------------------------------------//
//----------------Definition zone--------------------//
//----------------------------------------------//
U8  U8FLAG,k;
U8  U8count,U8temp;
U8  U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;
U8  U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;
U8  U8comdata;
U8  outdata(5);  
U8  indata(5);
U8  count, count_r=0;
U8  str(5)={"RS232"};
U16 U16temp1,U16temp2;
SendData(U8 *a)
{
    outdata(0) = a(0); 
    outdata(1) = a(1);
    outdata(2) = a(2);
    outdata(3) = a(3);
    outdata(4) = a(4);
    count = 1;
    SBUF=outdata(0);
}

       void Delay(U16 j)
    {      U8 i;
        for(;j>0;j--)
      { 	
        for(i=0;i<27;i++);

      }
    }
       void  Delay_10us(void)
      {
        U8 i;
        i--;
        i--;
        i--;
        i--;
        i--;
        i--;
       }

        void  COM(void)
      {

            U8 i;
       for(i=0;i<8;i++)	   
        {

            U8FLAG=2;

        while((!P2_0)&&U8FLAG++);
            Delay_10us();
            Delay_10us();
            Delay_10us();
            U8temp=0;
         if(P2_0)U8temp=1;
            U8FLAG=2;
         while((P2_0)&&U8FLAG++);


         if(U8FLAG==1)break;


           U8comdata<<=1;
           U8comdata|=U8temp;        //0
         }//rof

    }

    //--------------------------------
    //-----Sub-program for reading %RH ------------
    //--------------------------------
    //----All the variable bellow is global variable--------
    //----Temperature's high 8bit== U8T_data_H------
    //----Temperature's low 8bit== U8T_data_L------
    //----Humidity's high 8bit== U8RH_data_H-----
    //----Humidity's low 8bit== U8RH_data_L-----
    //----Check-sum 8bit == U8checkdata-----
    //--------------------------------

    void RH(void)
    {

     P2_0=0;
       Delay(5);
       P2_0=1;
       Delay_10us();
       Delay_10us();
       Delay_10us();
       Delay_10us();
       P2_0=1;	  
       if(!P2_0)		  
       {
       U8FLAG=2;
       while((!P2_0)&&U8FLAG++);
       U8FLAG=2;
       while((P2_0)&&U8FLAG++);
       COM();
       U8RH_data_H_temp=U8comdata;
       COM();
       U8RH_data_L_temp=U8comdata;
       COM();
       U8T_data_H_temp=U8comdata;
       COM();
       U8T_data_L_temp=U8comdata;
       COM();
       U8checkdata_temp=U8comdata;
       P2_0=1;

       U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
       if(U8temp==U8checkdata_temp)
       {
          U8RH_data_H=U8RH_data_H_temp;
          U8RH_data_L=U8RH_data_L_temp;
          U8T_data_H=U8T_data_H_temp;
          U8T_data_L=U8T_data_L_temp;
          U8checkdata=U8checkdata_temp;
       }//fi
       }//fi

    }

//----------------------------------------------
//               main()
//----------------------------------------------
void main()
{
    U8  i,j;


    TMOD = 0x20;	  
    TH1 = 253;        
    TL1 = 253;
    TR1 = 1;          
    SCON = 0x50;	  
    ES = 1;
    EA = 1;           
    TI = 0;
    RI = 0;
    SendData(str) ;   
    Delay(1);         
    while(1)
    {  

       //------------------------
       //
       RH();
       //--------------------------

       str(0)=U8RH_data_H;
       str(1)=U8RH_data_L;
       str(2)=U8T_data_H;
       str(3)=U8T_data_L;
       str(4)=U8checkdata;
       SendData(str) ;  

       Delay(20000);
    }//elihw

}// main

void RSINTR() interrupt 4 using 2
{
    U8 InPut3;
    if(TI==1) 
    {
        TI=0;
        if(count!=5) 
        {
            SBUF= outdata(count);
            count++;
        }
    }

    if(RI==1)	 
    {	
        InPut3=SBUF;
        indata(count_r)=InPut3;
        count_r++;
        RI=0;								 
        if (count_r==5)
        {

            count_r=0;
        str(0)=indata(0);
         str(1)=indata(1);
           str(2)=indata(2);
             str(3)=indata(3);
                 str(4)=indata(4);
                 P0=0;
        }
    }
}

    }



#2 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 01 January 2012 - 09:50 PM

I have published sample application for DHT11/22 sensors - they should be very similar, if not the same. There are a few quirks, but usually the communication works most of the time...

#3 rex64

rex64

    Member

  • Members
  • PipPip
  • 17 posts

Posted 02 January 2012 - 06:09 PM

I am almost ready to start testing :) Is there a good free electronics simulator? I want to make sure I hook this up correctly. Do I need any resistors, diodes or anything else in-between? Does this look correct. Left is what I am hooking it up to, right is the sensor. Left to right pin order when looking at device front: 5V Duino -- VDD Power Supply Digital 0 -- Data Signal Nothing -- Null Duino Gnd -- Ground

#4 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 02 January 2012 - 07:21 PM

Do I need any resistors, diodes or anything else in-between?

Yes, you need a pull-up resistor for the sensor data line (connected from the data line to VDD). However, the sensors I have work fine with Netduino's internal pull-up resistor; I've provided pullUp parameter in the Dht11/22Sensor class constructors where you can pass PullUpResistor.Internal to select the built-in one or PullUpResistor.External in case you've connected the external one.

#5 rex64

rex64

    Member

  • Members
  • PipPip
  • 17 posts

Posted 03 January 2012 - 04:22 AM

The USB power light (white) goes dim after about 1 second of plugging in. I think Something must be wrong. I do not know where to hook up the resistor yet, so I just used the built in. Am I supposed to use digital? And is it ok to use the gnd and 5v on the Duino to power?

#6 boyd bridges

boyd bridges

    New Member

  • Members
  • Pip
  • 1 posts

Posted 03 January 2012 - 07:18 AM

is this website compadable with a mac?

#7 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 03 January 2012 - 08:21 AM

The USB power light (white) goes dim after about 1 second of plugging in. I think Something must be wrong.

Could you please show the circuit?

#8 rex64

rex64

    Member

  • Members
  • PipPip
  • 17 posts

Posted 03 January 2012 - 03:53 PM

See attached. The left to right goes from bottom to top starting at the 4th row. :)

Attached Files



#9 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 03 January 2012 - 03:55 PM

is this website compadable with a mac?

Which site do you refer to?
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#10 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 03 January 2012 - 04:24 PM

Hm, the circuit seems to be fine, unless I am missing something. Does the power LED go dim when you have the sensor plugged in? Does Netduino work normally without the sensor attached?

#11 rex64

rex64

    Member

  • Members
  • PipPip
  • 17 posts

Posted 04 January 2012 - 03:16 AM

I get this error (I had the 5v reversed earlier :)) An unhandled exception of type 'System.InvalidOperationException' occurred in DhtSensorApp.exe Additional information: DHT sensor pins are not connected together.

#12 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 04 January 2012 - 08:07 AM

Additional information: DHT sensor pins are not connected together.

Ok - I had to use kind of 'trick' with two pins connected together to overcome .NET MF limitations, so you'd need to connect yet another pin to data line - it is D1 in the sample application, just add a wire from D1 to D0 (or the sensor data line on breadboard). For the explanation please see the original forum post.

#13 rex64

rex64

    Member

  • Members
  • PipPip
  • 17 posts

Posted 04 January 2012 - 01:41 PM

Debug.Assert(data[1] == 0, "Humidity decimal part should be zero."); The data inside data is: 5 222 66 201 Debug window: DHT sensor Read() failed DHT sensor data has invalid checksum. DHT sensor Read() failed DHT sensor Read() ok, RH = 0.0%, Temp = 0.0°C DHT sensor data has invalid checksum.

#14 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 04 January 2012 - 02:56 PM

Do you use Dht22Sensor class? RHT-03 should be the same as DHT-22.

#15 rex64

rex64

    Member

  • Members
  • PipPip
  • 17 posts

Posted 04 January 2012 - 11:02 PM

Ok, I changed to Dht22 and I made sure internal was selected. Anything else? I am still getting the same errors.

var dhtSensor = new Dht22Sensor(Pins.GPIO_PIN_D0, Pins.GPIO_PIN_D1, PullUpResistor.Internal);

Debug Window:

DHT sensor data timeout.
DHT sensor Read() failed
DHT sensor Read() ok, RH = 0.0%, Temp = 0.0°C
DHT sensor data has invalid checksum.
DHT sensor Read() failed
DHT sensor data has invalid checksum.
DHT sensor Read() failed



#16 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 05 January 2012 - 08:57 AM

Ok, I changed to Dht22 and I made sure internal was selected. Anything else? I am still getting the same errors.

Well, it seems that your sensor may have slightly different communication timing - I've observed during development that the devices do not follow the datasheet exactly, there are certain variations, which can cause the code to fail due to .NET MF time measurement precision limitations. You'd probably need either to use a logic analyzer to see the actual pulse duration and tweak the code accordingly, or try the firmware with increased timer resolution.

#17 rex64

rex64

    Member

  • Members
  • PipPip
  • 17 posts

Posted 05 January 2012 - 01:54 PM

Ok I will try that. Can you post a compiled firmware to make it easier? Do I just make the modifications that you mentioned in the first post? Also, maybe my sensor is fried, can you give me a few tips so I can play with my 2nd sensor? http://forums.netdui...h__1#entry22357

#18 rex64

rex64

    Member

  • Members
  • PipPip
  • 17 posts

Posted 16 January 2012 - 02:01 AM

What language is the firmware written in? Do I use this compiler: Visual C++ 2010 Express? Also, are these the files I would be editing? \netduinofirmware\netduinofirmware_4.1.0.6\Solutions\NetduinoPlus Also, I do not understand what this step means. Am I changing the Micro Framework or the Duino firmware? Are they the same? 2. Copy these files over the Platform Kit (typically into C:\MicroFrameworkPK_v4_1) If you get a chance, it would be great to have your built firmware, since I am new at these things. Thanks!

#19 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 10 February 2013 - 09:41 PM

Hi rex64, where you able to get the RH03 to work properly? Mine only runs on debug mode, very strange.



#20 Arjun

Arjun

    New Member

  • Members
  • Pip
  • 1 posts

Posted 06 April 2013 - 10:38 AM

i am also want same thing i want to develop it in window form and want to display RH and Temp in 2 diffrent difrent monitar and save the RH and Tem in our sys in CSV formate pls help me






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.