Advertisement
ccarman602

DHT22 Temp & Humidity with heat index

Jun 3rd, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * This is lightly modified from the built-in SimpleDHT example code for the DHT22 humidity & temperature sensor,
  3.  * "DHT22Default".
  4.  *
  5.  * First, use male-to-female wires or a male-to-male and a breadboard to connect the following pins on the DHT22
  6.  * to the Arduino:
  7.  *
  8.  * pin "+" on DHT22:   connects to 5V on Arduino (any 5v pin will do)
  9.  * pin "out" on DHT22: connects to pin 2 on Arduino
  10.  * pin "-" on DHT22:   connects to GND on Arduino (any GND pin will do)
  11.  *
  12.  * Finally, use the Serial Monitor to see the output! Go to Tools > Serial Monitor, and make sure the speed
  13.  * in the lower right corner of the window is on "9600 baud" (long story for another day).
  14.  *
  15.  * To calculate the "heat index", which combines temperature and humidity for what it "feels like", it's kind of complicated.
  16.  * Modified from https://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml:
  17.  * First, we do this calculation:
  18.  *   HI (heat index) = 0.5 * {T + 61.0 + [(T-68.0)*1.2] + (RH*0.094)}
  19.  *   (T is temperature in degrees F, and RH is % relative humidity)
  20.  *
  21.  * Average HI with the temperature to find the heat index:
  22.  *   AVERAGE = (HI + T) / 2
  23.  * If the average is less than 80, we use that number. This is sometimes called the "humiture" (ha!).
  24.  *
  25.  * If HI is greater than 80, we need to use the following equation, called the "Rothfusz regression":
  26.  *   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
  27.  *
  28.  * Wow, that's ugly! Also, if the RH is less than 13% and the temperature is between 80 and 112 degrees F,
  29.  * then the following adjustment is subtracted from HI:
  30.  *
  31.  *   ADJUSTMENT = [(13-RH)/4]*SQRT{[17-ABS(T-95.)]/17}
  32.  * where ABS and SQRT are the absolute value and square root functions, respectively.
  33.  *
  34.  * On the other hand, if the RH is greater than 85% and the temperature is between 80 and 87 degrees F,
  35.  * then the following adjustment is added to HI:
  36.  *   ADJUSTMENT = [(RH-85)/10] * [(87-T)/5]
  37.  *
  38.  * What does this all mean? Just use the calculateHeatIndex function that I wrote for you. :)
  39.  *
  40.  */
  41.  
  42. #include <SimpleDHT.h>
  43.  
  44. // for DHT22,
  45. //      VCC: 5V or 3V
  46. //      GND: GND
  47. //      DATA: 2
  48. 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!
  49. SimpleDHT22 dht22(pinDHT22);
  50.  
  51. void setup() {
  52.   Serial.begin(9600);
  53.   Serial.println("=================================");
  54.   Serial.println("Reading from DHT22...");
  55. }
  56.  
  57. void loop() {
  58.   // read without samples.
  59.   // @remark We use read2 to get a float data, such as 10.1*C
  60.   //    if user doesn't care about the accurate data, use read to get a byte data, such as 10*C.
  61.   float tempC = 0;
  62.   float humidity = 0;
  63.  
  64.   // this checks to see if there are errors
  65.   int err = SimpleDHTErrSuccess;
  66.   if ((err = dht22.read2(&tempC, &humidity, NULL)) != SimpleDHTErrSuccess) {
  67.     Serial.print("Read DHT22 failed, err="); Serial.println(err);delay(2000);
  68.     return;
  69.   }
  70.  
  71.   float tempF = tempC * 1.8 + 32; // convert Celsius to Fahrenheit
  72.   float hi = calculateHeatIndex(tempF, humidity);
  73.  
  74.   // if the sample is okay, display it:
  75.   // Serial.print((float)tempC); // Americans don't use Celsius! :P
  76.   // Serial.print(" *C, ");
  77.   Serial.print((float)tempF);
  78.   Serial.print(" *F, ");
  79.   Serial.print("feels like ");
  80.   Serial.print((float)hi);
  81.   Serial.print(" *F, ");
  82.   Serial.print((float)humidity);
  83.   Serial.println(" RH%");
  84.  
  85.   // DHT22 sampling rate is 0.5Hz, or once every 2 seconds
  86.   delay(2500);
  87. }
  88.  
  89. float calculateHeatIndex(float t, float rh) {
  90.   // HI = 0.5 * {T + 61.0 + [(T-68.0)*1.2] + (RH*0.094)}
  91.   float hi = 0.5 * ( t + 61.0  + ( ( t - 68.0 ) * 1.2 ) + ( rh * 0.094 ) );
  92.   float avg = (hi + t) / 2;
  93.  
  94.   if (avg < 80) {
  95.     return avg;
  96.   } else {
  97.     // the average is greater than 80, so we have to continue on!
  98.     // Rothfusz regression:
  99.     // 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
  100.     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);
  101.     // If the RH is less than 13% and the temperature is between 80 and 112 degrees F
  102.     if (rh < 13 && t >= 80 && t <= 112) {
  103.       // the following adjustment is subtracted from HI:
  104.       // ADJUSTMENT = [(13-RH)/4]*SQRT{[17-ABS(T-95.)]/17}
  105.       float adjustment = ( ( 13 - rh ) / 4 ) * sqrt( ( 17 - abs( t - 95.0  ) ) /17 );
  106.       hi = hi - adjustment;
  107.       return hi;
  108.     } else if (rh > 85 && t >= 80 && t <= 87) {
  109.       // 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:
  110.       // ADJUSTMENT = [(RH-85)/10] * [(87-T)/5]
  111.       float adjustment = ( ( rh - 85 ) / 10 ) * ( ( 87 - t ) /5 );
  112.       hi = hi + adjustment;
  113.       return hi;
  114.     } else {
  115.       // no adjustment necessary, just use original regression
  116.       return hi;
  117.     }
  118.   }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement