Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This combines the DHT22 temperature & humidity sensor code with the 0.96" OLED display code
- to give you a real-time display of current conditions!
- A quick review of how to connect everything:
- DHT22 pins:
- ----------
- "+": 5V on Arduino (any 5v pin will do)
- out: pin 2 on Arduino
- "-": GND on Arduino (any GND pin will do)
- OLED pins:
- ---------
- GND: GND on Arduino
- VCC: 5V on Arduino
- D0 : pin 10 on Arduino
- D1 : pin 9 on Arduino
- RES: pin 13 on Arduino
- DC : pin 11 on Arduino
- CS : pin 12 on Arduino
- You can also see the output in the Serial Monitor - make sure the speed is set to "9600 baud"!
- */
- #include <SPI.h>
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #include <SimpleDHT.h>
- // If using software SPI (the default case):
- #define OLED_MOSI 9
- #define OLED_CLK 10
- #define OLED_DC 11
- #define OLED_CS 12
- #define OLED_RESET 13
- Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
- int pinDHT22 = 2;
- SimpleDHT22 dht22(pinDHT22);
- void setup(){
- Serial.begin(9600);
- Serial.println("=================================");
- Serial.println("Reading from DHT22...");
- // set up the OLED display
- display.begin(SSD1306_SWITCHCAPVCC);
- display.clearDisplay();
- display.display();
- }
- void loop(){
- 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 to the serial monitor:
- Serial.print((float)tempF); // use tempC if you want Celsius
- Serial.print(" *F, "); // don't forget to make sure the units match!
- Serial.print("feels like ");
- Serial.print((float)hi);
- Serial.print(" *F, ");
- Serial.print((float)humidity);
- Serial.println(" RH%");
- // now display temp & RH to the OLED:
- display.clearDisplay();
- display.setTextSize(2);
- display.setTextColor(WHITE);
- display.setCursor(0,0);
- display.print((float)tempF);
- display.println(" F");
- display.print((float)humidity);
- display.println("% RH");
- display.display();
- delay(2500);
- // display "Feels Like" temp:
- display.clearDisplay();
- display.setTextSize(2);
- display.setTextColor(WHITE);
- display.setCursor(0,0);
- display.println("Feels like");
- display.print((float)hi);
- display.println(" F");
- display.display();
- delay(2500); // DHT22 sampling rate is 0.5Hz, or once every 2 seconds
- }
- 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