Untitled

 avatar
unknown
plain_text
2 years ago
4.3 kB
5
Indexable
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include "Adafruit_ILI9341.h"
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085.h>
#include <DHT.h>

#define DHTTYPE DHT11   // DHT 11
#define DHTPIN 2     // what pin we're connected to(DHT10 and DHT20 don't need define it)
DHT dht(DHTPIN, DHTTYPE);   //   DHT11 DHT21 DHT22

Adafruit_BMP085 bmp;

// For the Adafruit shield, these are the default.
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 8

// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// If using the breakout, change pins as desired
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);

//define TFT text position on screen from top left, down
#define top 0,0
#define middle 0,10
#define bottom 0,20
 
//###############################
//### Debug Mode ###############
//##############################

//Change between Serial and display output
//Enter 1 for Debug, 0 for normal display output
#define DEBUG 1

#if DEBUG == 1
#define output Serial
#else
#define output tft
#endif

//###############################
//### End Debug Mode ###############
//##############################

void setup() {
  Serial.begin(9600);

  Wire.begin();
  tft.begin();
  displayRotation();
  displaySettings(70,120);
  tft.setTextColor(ILI9341_WHITE);
  tft.fillScreen(ILI9341_BLACK);
  //display initialization message
  initialize();

  //Start Temp/Press Sensor
  dht.begin();
  //initialize the bmp180 temperature sensor
  bool success = bmp.begin();
  if (success) {
    Serial.println("BMP180 init success");
  }   else if (!bmp.begin()) {
    Serial.println("Could not find a valid BMP180 sensor, check wiring!");
    while (1) {}
  }

  //output to Serial that setup completed
  Serial.println("Setup Complete!");

  // Clear the buffer
  tft.fillScreen(ILI9341_BLACK);

  tft.fillScreen(ILI9341_BLACK);
  //this is mostly for Serial output
  output.println();

  delay(2000);
}

void loop(){
  displayRotation();
  tft.println("hello world");
  
  //output current temp reading
  getTemp();

  //output current pressure reading
  getPress();

  //output current humidity reading
  getHumidity();

  //used basically only for debug in serial mode
  output.println();

  //output temp reading message
  tempRange();
}

//get message for current temperature
char tempRange() {
  char result;
  if (bmp.readTemperature() >= 25) {
    //displaySettings(1);
    output.println ("it's pretty warm");
  } else  {
    //displaySettings(1);
    output.println ("it's not that bad");
  }
  return result;
}

//get current humidity
float getHumidity() {
  float temp_hum_val[2] = {0};
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)

  if (!dht.readTempAndHumidity(temp_hum_val)) {
    displaySettings(bottom);
    output.print("Humidity: ");
    output.print(temp_hum_val[0] + 1);
    output.println("%");
  } else {
    output.println("Failed to get humidity value.");
  }
  return temp_hum_val[0];
}

//get current temperature
double getTemp() {
  double readTemp;
  //read temperature sensor
  if (bmp.readTemperature()) {
    displaySettings(top);
    output.print("Temp: ");
    output.print(bmp.readTemperature() - 1);
    output.println("*C");
  } else  {
    output.println ("Temp Unavail");
  }
  return readTemp;
}

//get current pressure
double getPress() {
  double readPress;
  //read pressure sensor
  if (bmp.readPressure()) {
    displaySettings(middle);
    output.print("Pressure:");
    output.print(bmp.readPressure() / 1000);
    output.println("KPA");
  } else  {
      output.println ("Press Unavail");
    }
  return readPress;
}

//TFT display settings
void displaySettings(int x,int y){
    tft.setTextSize(2);
    tft.setTextColor(ILI9341_BLUE);
    tft.setCursor(x,y);
}

//set TFT display rotation
void displayRotation(){
  for(uint8_t rotation=0; rotation<4; rotation++) {
      tft.setRotation(rotation);  }
}

//initialize message
void initialize(){
  output.print("Initializing");
  delay(800);
  output.print(".");
  delay(800);
  output.print(".");
  delay(800);
  output.println(".");
  delay(800);
  output.println("Initialization Done!");
}
Editor is loading...