• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Miernik temperatury wilgotności i ciśnienia
#1
Witam
Postanowiłem jako pierwszy projekt wykonać stację pomiarową, która będzie na wyświetlaczu LCD 20x4 wyświetlać
aktualną godzinę, datę, temperatury z 2 czujników, wilgotność i ciśnienie atmosferyczne.
Czujniki to:
Sonda wodoodporna z czujnikiem temperatury DS18B20
BME280 - czujnik wilgotności, temperatury oraz ciśnienia I2C/SPI - moduł Adafruit
W wyniku poszukiwań w sieci i wykorzystaniu kilku gotowych programów otrzymałem takie coś:

Kod:
#include <DS1307RTC.h>
#include <Time.h>
#include <OneWire.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <LiquidCrystal_I2C.h>
#define BACKLIGHT_PIN 3
LiquidCrystal_I2C  lcd(0x27,2,1,0,4,5,6,7);
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10

#define SEALEVELPRESSURE_HPA (1013.25)

OneWire  ds(10);  // on pin 10 (a 4.7K resistor is necessary)
Adafruit_BME280 bme; // I2C

const char *monthName[12] = {
 "01", "02", "03", "04", "05", "06",
 "07", "08", "09", "10", "11", "12"
};
 byte temp[8] = //ikona temperatury
{
   B00100,
   B01010,
   B01010,
   B01110,
   B01110,
   B11111,
   B11111,
   B01110
};

byte wilg[8] = //ikona wilgotnosci
{
   B00100,
   B00100,
   B01010,
   B10001,
   B10001,
   B10001,
   B01110,
};

byte stopnie[8] = //ikona stopni
{
       B01100,
       B10010,
       B10010,
       B01100,
       B00000,
       B00000,
       B00000,
       B00000
};

void setup(void) {
 Serial.begin(9600);
//  Serial.println(F("BME280 test"));

  lcd.begin(20, 4);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.createChar(0, temp);
  lcd.createChar(1, wilg);
  lcd.createChar(3, stopnie);
  lcd.createChar(4, procent);
 
 if (!bme.begin()) {
//    Serial.println("Could not find a valid BME280 sensor, check wiring!");
   while (1);
 }
}

void loop() {
 byte i;
 byte present = 0;
 byte type_s;
 byte data[12];
 byte addr[8];
 float celsius, fahrenheit;
 
 if ( !ds.search(addr)) {
//    Serial.println("No more addresses.");
//    Serial.println();
   ds.reset_search();
//  delay(1000);
   return;
 }
 
//  Serial.print("ROM =");
 for( i = 0; i < 8; i++) {
//    Serial.write(' ');
//    Serial.print(addr[i], HEX);
 }

 if (OneWire::crc8(addr, 7) != addr[7]) {
//      Serial.println("CRC is not valid!");
     return;
 }
//  Serial.println();
 
 // the first ROM byte indicates which chip
 switch (addr[0]) {
   case 0x10:
//      Serial.println("  Chip = DS18S20");  // or old DS1820
     type_s = 1;
     break;
   case 0x28:
//      Serial.println("  Chip = DS18B20");
     type_s = 0;
     break;
   case 0x22:
//     Serial.println("  Chip = DS1822");
     type_s = 0;
     break;
   default:
//      Serial.println("Device is not a DS18x20 family device.");
     return;
 }

 ds.reset();
 ds.select(addr);
 ds.write(0x44, 1);        // start conversion, with parasite power on at the end
 
// delay(1000);    
 // we might do a ds.depower() here, but the reset will take care of it.
 
 present = ds.reset();
 ds.select(addr);    
 ds.write(0xBE);         // Read Scratchpad

//  Serial.print("  Data = ");
//  Serial.print(present, HEX);
//  Serial.print(" ");
 for ( i = 0; i < 9; i++) {           // we need 9 bytes
   data[i] = ds.read();
//    Serial.print(data[i], HEX);
//    Serial.print(" ");
 }
//  Serial.print(" CRC=");
//  Serial.print(OneWire::crc8(data, 8), HEX);
//  Serial.println();

 // Convert the data to actual temperature
 // because the result is a 16 bit signed integer, it should
 // be stored to an "int16_t" type, which is always 16 bits
 // even when compiled on a 32 bit processor.
 int16_t raw = (data[1] << 8) | data[0];
 if (type_s) {
   raw = raw << 3; // 9 bit resolution default
   if (data[7] == 0x10) {
     // "count remain" gives full 12 bit resolution
     raw = (raw & 0xFFF0) + 12 - data[6];
   }
 } else {
   byte cfg = (data[4] & 0x60);
   // at lower res, the low bits are undefined, so let's zero them
   if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
   else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
   else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
   //// default is 12 bit resolution, 750 ms conversion time
 }
 celsius = (float)raw / 16.0;
 fahrenheit = celsius * 1.8 + 32.0;
//  Serial.print("  Temperature = ");
//  Serial.print(celsius);
//  Serial.print(" Celsius, ");
//  Serial.print(fahrenheit);
//  Serial.println(" Fahrenheit");
//zegar
tmElements_t tm;

 if (RTC.read(tm)) {

//    lcd.setCursor (0, 0);
//    lcd.print("Godzina");
   lcd.setCursor (6, 0);
   LCDprint2digits(tm.Hour);
   lcd.print(':');
   LCDprint2digits(tm.Minute);
   lcd.print(':');
   LCDprint2digits(tm.Second);
//    lcd.setCursor (0, 1);
//    lcd.print("Data");
   lcd.setCursor (5, 1);
   lcd.print(tm.Day);
   lcd.print('/');
   lcd.print(monthName[tm.Month-1]);
   lcd.print('/');
   lcd.print(tmYearToCalendar(tm.Year));
      //wyswietlacz
   lcd.setCursor (0, 2);
   lcd.write(byte(0));
   lcd.print("=");
   lcd.print(bme.readTemperature(), 1);
   lcd.write(byte(3));
   lcd.print("C");

   lcd.setCursor (9, 2);
   lcd.print("P=");
   lcd.print(bme.readPressure() / 100.0F, 1);
   lcd.print("hPa");

 lcd.setCursor (0, 3);
   lcd.write(byte(0));
   lcd.print("=");
   lcd.print(celsius, 1);
   lcd.write(byte(3));
   lcd.print("C");


   lcd.setCursor (9, 3);
   lcd.write(byte(1));
   lcd.write("=");
   lcd.print(bme.readHumidity(), 1);
   lcd.print("%");
   
 } else {
   if (RTC.chipPresent()) {
     lcd.println("DS1307 stoi. Uruchom Zegar");
     }
      else {
     lcd.println("DS1307 blad oczczytu!");
     lcd.println("sprawdz polaczenie");
   }
//    delay(9000);
 }
//  delay(1000);
}

//void print2digits(int number) {
//  if (number >= 0 && number < 10) {
//    Serial.write('0');
//  }
//  Serial.print(number);
//}
 
void LCDprint2digits(int number) {
 if (number >= 0 && number < 10) {
   lcd.write('0');
 }
 lcd.print(number);


     //bme280
//    Serial.print("Temperature = ");
//    Serial.print(bme.readTemperature());
//    Serial.println(" *C");

//    Serial.print("Pressure = ");

//    Serial.print(bme.readPressure() / 100.0F);
//    Serial.println(" hPa");

//    Serial.print("Approx. Altitude = ");
//    Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
//    Serial.println(" m");

//    Serial.print("Humidity = ");
//    Serial.print(bme.readHumidity());
//    Serial.println(" %");

//    Serial.println();
//    delay(500);
}

Na początku zanim dodałem wyświetlacz LCD dane czytałem w konsoli dlatego cześć kodu dotycząca wysyłania danych do komputera jest wyłączona.
Zastanawiam się też nad możliwością wykonania programu tak aby na wyświetlaczu zmieniać ekrany np żeby godzina i data były na jednym ekranie a wielkości mierzone na drugim a zmiana następowała np co 5 s ale to już bardziej skomplikowane. Na szczęście jest na forum wątek o tym:
http://forum.arduinopolska.com/watek-kilka-stron-lcd
niestety na razie nie ogarniam tego.
Proszę o weryfikację, pomoc w uporządkowaniu tego i pomoc w ustawieniu opóźnienia tak aby zegar odświeżał się co sekundę a wielkości mierzone np co 2s.
 
Odpowiedź
  


Wiadomości w tym wątku
Miernik temperatury wilgotności i ciśnienia - przez zbyszek78 - 27-09-2015, 21:22

Skocz do:


Przeglądający: 2 gości