Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This is lightly modified from the built-in SimpleDHT example code for the DHT22 humidity & temperature sensor,
- * "DHT22Default".
- *
- * First, use male-to-female wires or a male-to-male and a breadboard to connect the following pins on the DHT22
- * to the Arduino:
- *
- * pin "+" on DHT22: connects to 5V on Arduino (any 5v pin will do)
- * pin "out" on DHT22: connects to pin 2 on Arduino
- * pin "-" on DHT22: connects to GND on Arduino (any GND pin will do)
- *
- * Finally, use the Serial Monitor to see the output! Go to Tools > Serial Monitor, and make sure the speed
- * in the lower right corner of the window is on "9600 baud" (long story for another day).
- *
- * To calculate the "heat index", which combines temperature and humidity for what it "feels like", it's kind of complicated.
- * Modified from https://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml:
- * First, we do this calculation:
- * HI (heat index) = 0.5 * {T + 61.0 + [(T-68.0)*1.2] + (RH*0.094)}
- * (T is temperature in degrees F, and RH is % relative humidity)
- *
- * Average HI with the temperature to find the heat index:
- * AVERAGE = (HI + T) / 2
- * If the average is less than 80, we use that number. This is sometimes called the "humiture" (ha!).
- *
- * If HI is greater than 80, we need to use the following equation, called the "Rothfusz regression":
- * HI = -42.379 + 2.04901523*T + 10.14333127*RH - .22475541*T*RH - .00683783*T*T - .05481717*RH*RH + .00122874*T*T*RH + .00085282*T*RH*RH - .00000199*T*T*RH*RH
- *
- * Wow, that's ugly! Also, if the RH is less than 13% and the temperature is between 80 and 112 degrees F,
- * then the following adjustment is subtracted from HI:
- *
- * ADJUSTMENT = [(13-RH)/4]*SQRT{[17-ABS(T-95.)]/17}
- * where ABS and SQRT are the absolute value and square root functions, respectively.
- *
- * On the other hand, if the RH is greater than 85% and the temperature is between 80 and 87 degrees F,
- * then the following adjustment is added to HI:
- * ADJUSTMENT = [(RH-85)/10] * [(87-T)/5]
- *
- * What does this all mean? Just use the calculateHeatIndex function that I wrote for you. :)
- *
- */
- #include <SimpleDHT.h>
- // for DHT22,
- // VCC: 5V or 3V
- // GND: GND
- // DATA: 2
- int pinDHT22 = 2; // if you don't want to connect it to pin 2, you can change that here, just don't use 0 or 1!
- SimpleDHT22 dht22(pinDHT22);
- void setup() {
- Serial.begin(9600);
- Serial.println("=================================");
- Serial.println("Reading from DHT22...");
- }
- void loop() {
- // read without samples.
- // @remark We use read2 to get a float data, such as 10.1*C
- // if user doesn't care about the accurate data, use read to get a byte data, such as 10*C.
- float tempC = 0;
- float humidity = 0;
- // this checks to see if there are errors
- int err = SimpleDHTErrSuccess;
- if ((err = dht22.read2(&tempC, &humidity, NULL)) != SimpleDHTErrSuccess) {
- Serial.print("Read DHT22 failed, err="); Serial.println(err);delay(2000);
- return;
- }
- float tempF = tempC * 1.8 + 32; // convert Celsius to Fahrenheit
- float hi = calculateHeatIndex(tempF, humidity);
- // if the sample is okay, display it:
- // Serial.print((float)tempC); // Americans don't use Celsius! :P
- // Serial.print(" *C, ");
- Serial.print((float)tempF);
- Serial.print(" *F, ");
- Serial.print("feels like ");
- Serial.print((float)hi);
- Serial.print(" *F, ");
- Serial.print((float)humidity);
- Serial.println(" RH%");
- // DHT22 sampling rate is 0.5Hz, or once every 2 seconds
- delay(2500);
- }
- float calculateHeatIndex(float t, float rh) {
- // HI = 0.5 * {T + 61.0 + [(T-68.0)*1.2] + (RH*0.094)}
- float hi = 0.5 * ( t + 61.0 + ( ( t - 68.0 ) * 1.2 ) + ( rh * 0.094 ) );
- float avg = (hi + t) / 2;
- if (avg < 80) {
- return avg;
- } else {
- // the average is greater than 80, so we have to continue on!
- // Rothfusz regression:
- // HI = -42.379 + 2.04901523*T + 10.14333127*RH - .22475541*T*RH - .00683783*T*T - .05481717*RH*RH + .00122874*T*T*RH + .00085282*T*RH*RH - .00000199*T*T*RH*RH
- hi = -42.379 + (2.04901523 * t) + (10.14333127 * rh) - (0.22475541 * t * rh) - (0.00683783 * t * t) - (0.05481717 * rh * rh) + (0.00122874 * t * t * rh) + (0.00085282 * t * rh * rh) - (0.00000199 * t * t * rh * rh);
- // If the RH is less than 13% and the temperature is between 80 and 112 degrees F
- if (rh < 13 && t >= 80 && t <= 112) {
- // the following adjustment is subtracted from HI:
- // ADJUSTMENT = [(13-RH)/4]*SQRT{[17-ABS(T-95.)]/17}
- float adjustment = ( ( 13 - rh ) / 4 ) * sqrt( ( 17 - abs( t - 95.0 ) ) /17 );
- hi = hi - adjustment;
- return hi;
- } else if (rh > 85 && t >= 80 && t <= 87) {
- // if the RH is greater than 85% and the temperature is between 80 and 87 degrees F, then the following adjustment is added to HI:
- // ADJUSTMENT = [(RH-85)/10] * [(87-T)/5]
- float adjustment = ( ( rh - 85 ) / 10 ) * ( ( 87 - t ) /5 );
- hi = hi + adjustment;
- return hi;
- } else {
- // no adjustment necessary, just use original regression
- return hi;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement