The rht03.c
C program is a driver for the MaxDetect series RHT03 sensor, designed to run on a Raspberry Pi device, and it continuously reads and displays temperature and humidity values from the sensor. The program is distributed under the GNU Lesser General Public License (LGPL) version 3 or later, written by Gordon Henderson and part of the wiringPi project.
/*
* rht03.c:
* Driver for the MaxDetect series sensors
*
* Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published b y
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <time.h>
#include <wiringPi.h>
#include <maxdetect.h>
#define RHT03_PIN 7
/*
***********************************************************************
* The main program
***********************************************************************
*/
int main (void)
{
int temp, rh ;
float temp2 ;
int newTemp, newRh ;
temp = rh = newTemp = newRh = 0 ;
wiringPiSetup () ;
piHiPri (55) ;
for(;;)
{
if (!readRHT03 (RHT03_PIN, &newTemp, &newRh))
{
delay(57);
continue;
}
if ((temp != newTemp) || (rh != newRh))
{
temp = newTemp ;
temp2 = (9.0 / 5) * (newTemp / 10) + 32;
rh = newRh ;
fprintf (stdout, "celsius=%.2f&fahrenheit=%.2f&humidity=%.2f&time=%u\n", temp / 10.0, temp2, rh / 10.0, (unsigned)time(NULL)) ;
fflush(stdout);
}
delay(23000);
}
return 0 ;
}
/**
* @file rht03.c
* @brief Driver for the MaxDetect series sensors
*
* @copyright 2012-2013 Gordon Henderson
*
* @note This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*/
#include
#include
#include
#include
#define RHT03_PIN 7
#define DELAY_MS 23000
#define SERIAL_DELAY_MS 57
#define MAX_TEMPERATURE 100
#define MAX_HUMIDITY 100
/**
* @brief Setup function for wiringPi
*/
void setup(void) {
wiringPiSetup();
piHiPri(55);
}
/**
* @brief Main program loop
*/
void loop(void) {
int newTemp, newRh;
float temp2;
if (readRHT03(RHT03_PIN, &newTemp, &newRh)) {
if ((temp!= newTemp) || (rh!= newRh)) {
temp = newTemp;
temp2 = (9.0 / 5) * (newTemp / 10) + 32;
rh = newRh;
printf("celsius=%.2f&fahrenheit=%.2f&humidity=%.2f&time=%u\n", temp / 10.0, temp2, rh / 10.0, (unsigned)time(NULL));
fflush(stdout);
}
} else {
// Serial interface is not available, wait and try again
usleep(SERIAL_DELAY_MS * 1000);
}
usleep(DELAY_MS * 1000);
}
int main(void) {
int temp, rh;
float temp2;
temp = rh = 0;
setup();
while (1) {
loop();
}
return 0;
}
This is a C program named rht03.c
that serves as a driver for the MaxDetect series sensors, specifically the RHT03 sensor. The program is designed to run on a Raspberry Pi device.
The program includes the following header files:
stdio.h
: for standard input/output operationstime.h
: for time-related functionswiringPi.h
: for wiringPi library functionsmaxdetect.h
: for MaxDetect sensor library functionsThe program defines a single constant:
RHT03_PIN
: the pin number on the Raspberry Pi where the RHT03 sensor is connected (pin 7)The main
function is the entry point of the program. It performs the following tasks:
wiringPiSetup
.piHiPri
.readRHT03
.fprintf
. The output format includes the temperature in Celsius and Fahrenheit, humidity, and the current time.delay(57)
if an error occurs while reading the sensor values.delay(23000)
.The program is distributed under the GNU Lesser General Public License (LGPL) version 3 or later. The copyright notice indicates that the program was written by Gordon Henderson and is part of the wiringPi project.