Advertisement
LeventeDaradici

Meteo station with Dallas DS18B20, DHT11, BMP280

Sep 2nd, 2021
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include <Wire.h>             // include Wire library, required for I2C devices
  2. #include <Adafruit_Sensor.h>  // include Adafruit sensor library
  3. #include <Adafruit_BMP280.h>  // include adafruit library for BMP280 sensor
  4. #include <LiquidCrystal_I2C.h>
  5. #include <DHT.h>
  6. #include <OneWire.h>
  7. #include <DallasTemperature.h>
  8.  
  9. #define DHTPIN 2
  10. #define DHTTYPE DHT11
  11. DHT dht(DHTPIN, DHTTYPE);
  12.  
  13.  
  14.  
  15. #define ONE_WIRE_BUS 3
  16. OneWire oneWire(ONE_WIRE_BUS);
  17. DallasTemperature sensors(&oneWire);
  18.  
  19.  
  20.  
  21. // define device I2C address: 0x76 or 0x77 (0x77 is library default address)
  22. #define BMP280_I2C_ADDRESS  0x76
  23.  
  24. Adafruit_BMP280 bmp280;
  25. LiquidCrystal_I2C lcd(0x27,16,2);
  26.  
  27. void setup() {
  28.   Serial.begin(9600);
  29.   dht.begin();
  30.   lcd.init();
  31.   lcd.backlight();
  32.   lcd.clear();
  33.   lcd.setCursor(0,0);
  34.   lcd.print("Daradici Levente");
  35.   lcd.setCursor(2,1);
  36.   lcd.print("Statie Meteo");
  37.   delay(3000);
  38.   lcd.clear();
  39.  
  40.   if (!bmp280.begin(BMP280_I2C_ADDRESS))
  41.   {  
  42.     Serial.println("Could not find a valid BMP280 sensor, check wiring!");
  43.     while (1);
  44.   }
  45.  
  46. }
  47.  
  48. char text[14];
  49.  
  50. // main loop
  51. void loop()
  52. {
  53.  
  54.   sensors.requestTemperatures(); // Send the command to get temperature readings
  55.  
  56.   int h = dht.readHumidity();
  57.   // Read temperature as Celsius (the default)
  58.   float t = dht.readTemperature();
  59.   // Read temperature as Fahrenheit (isFahrenheit = true)
  60.   float f = dht.readTemperature(true);
  61.  
  62.   // Check if any reads failed and exit early (to try again).
  63.   if (isnan(h) || isnan(t) || isnan(f)) {
  64.     Serial.println(F("Failed to read from DHT sensor!"));
  65.     return;
  66.   }
  67.    // Compute heat index in Fahrenheit (the default)
  68.   float hif = dht.computeHeatIndex(f, h);
  69.   // Compute heat index in Celsius (isFahreheit = false)
  70.   float hic = dht.computeHeatIndex(t, h, false);
  71.  
  72.   // get temperature, pressure and altitude from library
  73.   float temperature = bmp280.readTemperature();  // get temperature
  74.   float pressure    = bmp280.readPressure();     // get pressure
  75.   int altitude_   = bmp280.readAltitude(1013.25); // get altitude (this should be adjusted to your local forecast)
  76.  
  77.   // print data on the serial monitor software
  78.   // 1: print temperature
  79.   Serial.print("Temperature = ");
  80.   Serial.print(temperature);
  81.   Serial.println(" °C");
  82.   // 2: print pressure
  83.   Serial.print("Pressure    = ");
  84.   Serial.print(pressure/100);
  85.   Serial.println(" hPa");
  86.   // 3: print altitude
  87.   Serial.print("Approx Altitude = ");
  88.   Serial.print(altitude_);
  89.   Serial.println(" m");
  90.   Serial.println();  
  91.  
  92.   lcd.clear();
  93.   lcd.setCursor(0,0);
  94.   lcd.print("Temperatura");
  95.   lcd.setCursor(9,1);
  96.   lcd.print(sensors.getTempCByIndex(0));
  97.  
  98.   lcd.setCursor(14,1);
  99.   lcd.print((char)223);
  100.   lcd.setCursor(15,1);
  101.   lcd.print("C");
  102.   delay(3000);
  103.  
  104.   lcd.clear();
  105.   lcd.setCursor(0,0);
  106.   lcd.print("Umiditate");  
  107.   lcd.setCursor(12,1);
  108.   lcd.print(h);  
  109.   lcd.setCursor(14,1);
  110.   lcd.print(" %");
  111.   delay(3000);
  112.  
  113.   lcd.clear();
  114.   lcd.setCursor(0,0);
  115.   lcd.print("Presiune");
  116.   lcd.setCursor(5,1);
  117.   lcd.print(pressure/100);  
  118.   lcd.setCursor(13,1);
  119.   lcd.print("hPa");
  120.   delay(3000);  
  121.  
  122.   //lcd.clear();
  123.   //lcd.setCursor(0,0);
  124.   //lcd.print("Altitudine");
  125.   //lcd.setCursor(10,1);
  126.   //lcd.print(altitude_);  
  127.   //lcd.setCursor(15,1);
  128.   //lcd.print("m");
  129.   //delay(3000);
  130.  
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement