Saltar al contenido

Proyecto Arduino Nano Estación Meteorológica: CÓDIGO C/C++

#include <SPI.h>               //include Serial Peripheral Interface library
#include <Wire.h>              //include Two Wire Interface library
#include <Adafruit_GFX.h>      // include Adafruit graphics library
#include <Adafruit_ST7735.h>   // include Adafruit ST7735 TFT library
#include <Adafruit_BMP280.h>   // include Adafruit BMP280 sensor library
#include "DHT.h"               //include DHTXX sensor library
#define DHTPIN 2               //DHT11 data pin connected to arduino pin D2
#define DHTTYPE DHT11          //specifying the type of DHT sensor used
#define TFT_RST   8            // TFT RST pin is connected to arduino pin D8
#define TFT_CS    10           // TFT CS  pin is connected to arduino pin D9
#define TFT_DC    9            // TFT DC  pin is connected to arduino pin D10
// initialize ST7735 SERIES TFT library
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
 
// define device I2C address: 0x76 or 0x77 (0x77 is the library default address)
#define BMP280_I2C_ADDRESS  0x76
 
Adafruit_BMP280  bmp280;       // initialize Adafruit BMP280 library
DHT dht(DHTPIN, DHTTYPE);      // initialize DHT sensor
 
void setup(void)
{
  dht.begin();                                         // synchronizing DHT sensor
  tft.initR(INITR_144GREENTAB);                        // initialize a ST7735S chip, black tab
  tft.fillScreen(ST77XX_BLACK);                        // setting black background
  tft.drawFastHLine(0, 15 ,  tft.width(), ST77XX_CYAN);// draw horizontal seperation line at position (0, 15)
 
  tft.setTextColor(ST77XX_CYAN, ST77XX_BLACK);         // set text color to white and black background
  tft.setTextSize(1);                                  // setting text size to 1
  //tft.setCursor(4, 0);                               // move cursor to position (4, 0) pixel
  //tft.print("ARDUINO + ST7735 TFT");
  tft.setCursor(25, 5);                                // move cursor to position (25, 5) pixel
  tft.print("WEATHER BUDDY");
 
  // initialize the BMP280 sensor
  if( bmp280.begin(BMP280_I2C_ADDRESS) == 0 )
  {  // connection error or device address wrong!
    tft.setTextColor(ST77XX_RED, ST77XX_CYAN);         // set text color to red and black background
    tft.setTextSize(2);                                // setting text size to 2
    tft.setCursor(5, 76);                              // move cursor to position (5, 76) pixel
    tft.print("Connection");
    tft.setCursor(35, 100);                            // move cursor to position (35, 100) pixel
    tft.print("Error");
    while(1);  // stay here
  }
 
  tft.drawFastHLine(0, 55,  tft.width(), ST77XX_CYAN);  // draw horizontal seperation line at position (0, 55)pixel
  tft.drawFastHLine(0, 95,  tft.width(), ST77XX_CYAN);  // draw horizontal seperation line at position (0, 195)pixel
  tft.setTextColor(ST77XX_RED, ST77XX_BLACK);           // set text color to red and black background
  tft.setCursor(30, 20);                                // move cursor to position (30, 20) pixel
  tft.print("TEMPERATURE ");                            // setting heading for first section
  tft.setTextColor(ST77XX_CYAN, ST77XX_BLACK);          // set text color to cyan and black background
  tft.setCursor(40, 60);                                // move cursor to position (40, 60) pixel
  tft.print("HUMIDITY ");                               // setting heading for second section
  tft.setTextColor(ST77XX_GREEN, ST7735_BLACK);         // set text color to green and black background
  tft.setCursor(40, 100);                               // move cursor to position (40, 100) pixel
  tft.print("PRESSURE ");                               // setting heading for third section 
  tft.setTextSize(2);                                   // setting text size to 2 
}
 
// main loop
void loop()
  {

  char _buffer[8];
  // read temperature, humidity and pressure from the BMP280 sensor
  float temp = bmp280.readTemperature();    // get temperature in °C
  float hum = dht.readHumidity();           // get humidity in rH%
  float pres = bmp280.readPressure();       // get pressure in hPa
 
  // print temperature (in °C)
  if(temp < 0)                              // if temperature < 0
    sprintf( _buffer, "-%02u.%02u", (int)abs(temp), (int)(abs(temp) * 100) % 100 );
  else                                      // if temperature >= 0
    sprintf( _buffer, " %02u.%02u", (int)temp, (int)(temp * 100) % 100 );// setting the value approximation 
  tft.setTextColor(ST77XX_YELLOW, ST77XX_BLACK);  // set text color to yellow and black background
  tft.setCursor(11, 34);                          // move cursor to position (11,34) pixel 
  tft.print(_buffer);                             // print temperature from BMP-280 sensor
  tft.drawCircle(89, 34, 2, ST77XX_YELLOW);       // print the degree symbol ( ° )(can be omitted if * is used instead)
  tft.setCursor(95, 34);                          // move cursor to position (95,34) pixel 
  tft.print("C");                                 // print the Celcius symbol     
 
  // 2: print humidity (in %)
  sprintf( _buffer, "%02u ", (int)(hum));          // setting the value approximation 
  tft.setTextColor(ST77XX_MAGENTA, ST77XX_BLACK);  // set text color to magenta and black background
  tft.setCursor(45, 74);                           // move cursor to position (45,74) pixel
  tft.print(_buffer);                              // print humidity from DHT-11 sensor
  tft.setCursor(75, 74);                           // move cursor to position (75,74) pixel
  tft.print("%");                                  // print the percentage symbol     
 
 
  // 3: print pressure (in hPa)
  sprintf( _buffer, "%04u.%02u", (int)(pres/100), (int)((uint32_t)pres % 100) ); // setting the value approximation 
  tft.setTextColor(ST77XX_ORANGE, ST77XX_BLACK);  // set text color to orange and black background
  tft.setCursor(3, 112);                          // move cursor to position (3,112)pixel      
  tft.print(_buffer);                             // print atmospheric pressure from BMP-280
  tft.setCursor(91, 112);                         // move cursor to position (91,112)pixel
  tft.print("hPa");                               // print unit of atmospheric pressure as hecto pascal
 
  delay(1000);                                    // wait 1 second before taking next sensor reading
 
}

Si continuas utilizando este sitio aceptas el uso de cookies. más información

Los ajustes de cookies de esta web están configurados para «permitir cookies» y así ofrecerte la mejor experiencia de navegación posible. Si sigues utilizando esta web sin cambiar tus ajustes de cookies o haces clic en «Aceptar» estarás dando tu consentimiento a esto.

Cerrar