Advertisement
ccarman602

DHT22 Temp & Humidity with OLED display

Jun 3rd, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. This combines the DHT22 temperature & humidity sensor code with the 0.96" OLED display  code
  3. to give you a real-time display of current conditions!
  4.  
  5. A quick review of how to connect everything:
  6.  
  7. DHT22 pins:
  8. ----------
  9. "+": 5V on Arduino (any 5v pin will do)
  10. out: pin 2 on Arduino
  11. "-": GND on Arduino (any GND pin will do)
  12.  
  13. OLED pins:
  14. ---------
  15. GND: GND on Arduino
  16. VCC: 5V on Arduino
  17. D0 : pin 10 on Arduino
  18. D1 : pin 9 on Arduino
  19. RES: pin 13 on Arduino
  20. DC : pin 11 on Arduino
  21. CS : pin 12 on Arduino
  22.  
  23. You can also see the output in the Serial Monitor - make sure the speed is set to "9600 baud"!
  24. */
  25.  
  26. #include <SPI.h>
  27. #include <Wire.h>
  28. #include <Adafruit_GFX.h>
  29. #include <Adafruit_SSD1306.h>
  30. #include <SimpleDHT.h>
  31.  
  32. // If using software SPI (the default case):
  33. #define OLED_MOSI   9
  34. #define OLED_CLK   10
  35. #define OLED_DC    11
  36. #define OLED_CS    12
  37. #define OLED_RESET 13
  38. Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
  39.  
  40. int pinDHT22 = 2;
  41. SimpleDHT22 dht22(pinDHT22);
  42.  
  43.  
  44. void setup(){
  45.   Serial.begin(9600);
  46.  
  47.   Serial.println("=================================");
  48.   Serial.println("Reading from DHT22...");
  49.  
  50.   // set up the OLED display
  51.   display.begin(SSD1306_SWITCHCAPVCC);
  52.   display.clearDisplay();
  53.   display.display();
  54. }
  55.  
  56. void loop(){
  57.   float tempC = 0;
  58.   float humidity = 0;
  59.  
  60.   // this checks to see if there are errors
  61.   int err = SimpleDHTErrSuccess;
  62.   if ((err = dht22.read2(&tempC, &humidity, NULL)) != SimpleDHTErrSuccess) {
  63.     Serial.print("Read DHT22 failed, err="); Serial.println(err);delay(2000);
  64.     return;
  65.   }
  66.  
  67.   float tempF = tempC * 1.8 + 32; // convert Celsius to Fahrenheit
  68.   float hi = calculateHeatIndex(tempF, humidity);
  69.  
  70.   // if the sample is okay, display it to the serial monitor:
  71.   Serial.print((float)tempF); // use tempC if you want Celsius
  72.   Serial.print(" *F, "); // don't forget to make sure the units match!
  73.   Serial.print("feels like ");
  74.   Serial.print((float)hi);
  75.   Serial.print(" *F, ");
  76.   Serial.print((float)humidity);
  77.   Serial.println(" RH%");
  78.  
  79.   // now display temp & RH to the OLED:
  80.   display.clearDisplay();
  81.   display.setTextSize(2);
  82.   display.setTextColor(WHITE);
  83.   display.setCursor(0,0);
  84.   display.print((float)tempF);
  85.   display.println(" F");
  86.   display.print((float)humidity);
  87.   display.println("% RH");
  88.   display.display();
  89.   delay(2500);
  90.  
  91.   // display "Feels Like" temp:
  92.   display.clearDisplay();
  93.   display.setTextSize(2);
  94.   display.setTextColor(WHITE);
  95.   display.setCursor(0,0);
  96.   display.println("Feels like");
  97.   display.print((float)hi);
  98.   display.println(" F");
  99.   display.display();
  100.   delay(2500); // DHT22 sampling rate is 0.5Hz, or once every 2 seconds
  101. }
  102.  
  103. float calculateHeatIndex(float t, float rh) {
  104.   // HI = 0.5 * {T + 61.0 + [(T-68.0)*1.2] + (RH*0.094)}
  105.   float hi = 0.5 * ( t + 61.0  + ( ( t - 68.0 ) * 1.2 ) + ( rh * 0.094 ) );
  106.   float avg = (hi + t) / 2;
  107.  
  108.   if (avg < 80) {
  109.     return avg;
  110.   } else {
  111.     // the average is greater than 80, so we have to continue on!
  112.     // Rothfusz regression:
  113.     // 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
  114.     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);
  115.     // If the RH is less than 13% and the temperature is between 80 and 112 degrees F
  116.     if (rh < 13 && t >= 80 && t <= 112) {
  117.       // the following adjustment is subtracted from HI:
  118.       // ADJUSTMENT = [(13-RH)/4]*SQRT{[17-ABS(T-95.)]/17}
  119.       float adjustment = ( ( 13 - rh ) / 4 ) * sqrt( ( 17 - abs( t - 95.0  ) ) /17 );
  120.       hi = hi - adjustment;
  121.       return hi;
  122.     } else if (rh > 85 && t >= 80 && t <= 87) {
  123.       // 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:
  124.       // ADJUSTMENT = [(RH-85)/10] * [(87-T)/5]
  125.       float adjustment = ( ( rh - 85 ) / 10 ) * ( ( 87 - t ) /5 );
  126.       hi = hi + adjustment;
  127.       return hi;
  128.     } else {
  129.       // no adjustment necessary, just use original regression
  130.       return hi;
  131.     }
  132.   }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement