1#include <SPI.h>
2#include <Wire.h>
3#include <Adafruit_GFX.h>
4#include <Adafruit_ST7735.h>
5#include <Adafruit_BMP280.h>
6#include "DHT.h"
7#define DHTPIN 2
8#define DHTTYPE DHT11
9#define TFT_RST 8
10#define TFT_CS 10
11#define TFT_DC 9
12
13Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
14
15
16#define BMP280_I2C_ADDRESS 0x76
17
18Adafruit_BMP280 bmp280;
19DHT dht(DHTPIN, DHTTYPE);
20
21void setup(void)
22{
23 dht.begin();
24 tft.initR(INITR_144GREENTAB);
25 tft.fillScreen(ST77XX_BLACK);
26 tft.drawFastHLine(0, 15 , tft.width(), ST77XX_CYAN);
27
28 tft.setTextColor(ST77XX_CYAN, ST77XX_BLACK);
29 tft.setTextSize(1);
30
31
32 tft.setCursor(25, 5);
33 tft.print("WEATHER BUDDY");
34
35
36 if( bmp280.begin(BMP280_I2C_ADDRESS) == 0 )
37 {
38 tft.setTextColor(ST77XX_RED, ST77XX_CYAN);
39 tft.setTextSize(2);
40 tft.setCursor(5, 76);
41 tft.print("Connection");
42 tft.setCursor(35, 100);
43 tft.print("Error");
44 while(1);
45 }
46
47 tft.drawFastHLine(0, 55, tft.width(), ST77XX_CYAN);
48 tft.drawFastHLine(0, 95, tft.width(), ST77XX_CYAN);
49 tft.setTextColor(ST77XX_RED, ST77XX_BLACK);
50 tft.setCursor(30, 20);
51 tft.print("TEMPERATURE ");
52 tft.setTextColor(ST77XX_CYAN, ST77XX_BLACK);
53 tft.setCursor(40, 60);
54 tft.print("HUMIDITY ");
55 tft.setTextColor(ST77XX_GREEN, ST7735_BLACK);
56 tft.setCursor(40, 100);
57 tft.print("PRESSURE ");
58 tft.setTextSize(2);
59}
60
61
62void loop()
63 {
64
65 char _buffer[8];
66
67 float temp = bmp280.readTemperature();
68 float hum = dht.readHumidity();
69 float pres = bmp280.readPressure();
70
71
72 if(temp < 0)
73 sprintf( _buffer, "-%02u.%02u", (int)abs(temp), (int)(abs(temp) * 100) % 100 );
74 else
75 sprintf( _buffer, " %02u.%02u", (int)temp, (int)(temp * 100) % 100 );
76 tft.setTextColor(ST77XX_YELLOW, ST77XX_BLACK);
77 tft.setCursor(11, 34);
78 tft.print(_buffer);
79 tft.drawCircle(89, 34, 2, ST77XX_YELLOW);
80 tft.setCursor(95, 34);
81 tft.print("C");
82
83
84 sprintf( _buffer, "%02u ", (int)(hum));
85 tft.setTextColor(ST77XX_MAGENTA, ST77XX_BLACK);
86 tft.setCursor(45, 74);
87 tft.print(_buffer);
88 tft.setCursor(75, 74);
89 tft.print("%");
90
91
92
93 sprintf( _buffer, "%04u.%02u", (int)(pres/100), (int)((uint32_t)pres % 100) );
94 tft.setTextColor(ST77XX_ORANGE, ST77XX_BLACK);
95 tft.setCursor(3, 112);
96 tft.print(_buffer);
97 tft.setCursor(91, 112);
98 tft.print("hPa");
99
100 delay(1000);
101
102}