Created
February 18, 2017 00:28
-
-
Save wolfeidau/6388e209ab458ea9d637c91e0437d788 to your computer and use it in GitHub Desktop.
ESP32 weather station
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <SPI.h> | |
#include <Adafruit_Sensor.h> | |
#include <Adafruit_BME280.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_ILI9341.h> | |
#define SEALEVELPRESSURE_HPA (1013.25) | |
// For the ESP-WROVER_KIT, these are the default. | |
#define TFT_CS 22 | |
#define TFT_DC 21 | |
#define TFT_MOSI 23 | |
#define TFT_CLK 19 | |
#define TFT_RST 18 | |
#define TFT_MISO 25 | |
#define TFT_LED 5 // GPIO not managed by library | |
#define I2C_SDA 12 | |
#define I2C_SCL 13 | |
Adafruit_BME280 bme; // I2C | |
unsigned long delayTime; | |
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST); | |
void backlighting(bool state) { | |
if (state) { | |
digitalWrite(TFT_LED, LOW); | |
} | |
else { | |
digitalWrite(TFT_LED, HIGH); | |
} | |
} | |
void setupTft() { | |
pinMode(TFT_LED, OUTPUT); | |
backlighting(true); | |
SPI.begin(TFT_CLK, TFT_MISO, TFT_MOSI, -1); | |
tft.begin(); | |
tft.fillScreen(ILI9341_BLACK); | |
} | |
void setup() { | |
Serial.begin(115200); | |
Serial.println("Weather Station Project"); | |
// adjust pins to match wiring and avoid overlap with screen | |
Wire.begin(I2C_SDA, I2C_SCL); | |
if (! bme.begin(0x76)) { | |
Serial.println("Could not find a valid BME280 sensor, check wiring!"); | |
while (1); | |
} | |
// weather monitoring | |
Serial.println("-- BME280 Weather Station Scenario --"); | |
Serial.println("forced mode, 1x temperature / 1x humidity / 1x pressure oversampling,"); | |
Serial.println("filter off"); | |
bme.setSampling(Adafruit_BME280::MODE_FORCED, | |
Adafruit_BME280::SAMPLING_X1, // temperature | |
Adafruit_BME280::SAMPLING_X1, // pressure | |
Adafruit_BME280::SAMPLING_X1, // humidity | |
Adafruit_BME280::FILTER_OFF ); | |
// suggested rate is 1/60Hz (1m) | |
delayTime = 60000; // in milliseconds | |
Serial.println("-- Setup TFT --"); | |
setupTft(); | |
// read diagnostics (optional but can help debug problems) | |
uint8_t x = tft.readcommand8(ILI9341_RDMODE); | |
Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX); | |
x = tft.readcommand8(ILI9341_RDMADCTL); | |
Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX); | |
x = tft.readcommand8(ILI9341_RDPIXFMT); | |
Serial.print("Pixel Format: 0x"); Serial.println(x, HEX); | |
x = tft.readcommand8(ILI9341_RDIMGFMT); | |
Serial.print("Image Format: 0x"); Serial.println(x, HEX); | |
x = tft.readcommand8(ILI9341_RDSELFDIAG); | |
Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX); | |
} | |
void loop() { | |
// Only needed in forced mode! In normal mode, you can remove the next line. | |
bme.takeForcedMeasurement(); // has no effect in normal mode | |
printValues(); | |
delay(delayTime); | |
} | |
void printValues() { | |
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment