Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.8 kB
1
Indexable
Never
#include <Mouse.h>

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.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;

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

//define OLED 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 0

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

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

void setup() {
  Serial.begin(9600);
  Wire.begin();
    
  //Start OLED display
  bool oled_success = display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  if (oled_success) {
    Serial.println("oled init success");  
  } else if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  
  //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
  display.clearDisplay();
  display.display();

  //display Initializing for 5 sec and then clear to the loop
  displaySettings(top);
  #if DEBUG == 1
  Serial.println("Initializing...");
  display.println("Initializing...");
  #else
  output.println("Initializing...");
  #endif
  
  display.display();
  delay(5000);
  display.clearDisplay();
  //this is mostly for Serial output
  output.println();
}

void loop() {
  //Display on OLED that it's in DEBUG mode, otherwise output to OLED
  #if DEBUG == 1
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0,0);
    display.println("Debug Mode");
    display.display();
    #endif
    
  //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();

  //refresh output on screen every 1 second
  output.println();
  display.display();
  delay(1000);
  display.clearDisplay();
}

//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;
}

//OLED display settings
void displaySettings(int x,int y){
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(x,y);
}