Advertisement
RaspBar

ESP8266_DHT22_ST7735.ino

Jul 6th, 2022 (edited)
685
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**************************************************************************
  2.  *
  3.  * ESP8266 NodeMCU (ESP-12E)mit ST7735S TFT Display (128x160 Pixel)
  4.  * und DHT22 Luftdruck-, Temperatursensor.
  5.  *
  6.  *************************************************************************/
  7.  
  8. #include <Adafruit_GFX.h>      // include Adafruit graphics library
  9. #include <Adafruit_ST7735.h>   // include Adafruit ST7735 TFT library
  10. #include <DHT.h>               // include Adafruit DHT library code
  11.  
  12. //LED Setup
  13. int LEDrot = D0; // -> GPIO-16
  14. int LEDblau = D3; // -> GPIO-0
  15.  
  16.  
  17. // ST7735 TFT module connections
  18. #define TFT_RST   -1     // TFT RST pin is connected to NodeMCU pin RST
  19. #define TFT_CS    D8     // TFT CS  pin is connected to NodeMCU pin D8
  20. #define TFT_DC    D4     // TFT DC  pin is connected to NodeMCU pin D4
  21. // initialize ST7735 TFT library with hardware SPI module
  22. // SCK (CLK) ---> NodeMCU pin D5 (GPIO14)
  23. // MOSI(DIN) ---> NodeMCU pin D7 (GPIO13)
  24. Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
  25.  
  26. #define DHTPIN  D6           // DHT11 data pin is connected to NodeMCU pin D6 (GPIO5)
  27. #define DHTTYPE DHT22        // DHT11 sensor is used
  28. DHT dht22(DHTPIN, DHTTYPE);  // configure DHT library
  29.  
  30. void setup(void)
  31. {
  32.   //LED initialisieren -> LED aus
  33.   pinMode(LEDrot, OUTPUT); // Pin D0 ist ein Ausgang.
  34.   pinMode(LEDblau,OUTPUT); // Pin D3 ist ein Ausgang.
  35.   digitalWrite(LEDrot, LOW); // Schalte die LED an Pin D0 aus. D0 ist beim Start des ESP8266 auf HIGH.
  36.   digitalWrite(LEDblau, LOW); // Schalte die LED an Pin D3 aus.  
  37.  
  38.   tft.initR(INITR_BLACKTAB);     // initialize a ST7735S chip, black tab
  39.   tft.fillScreen(ST7735_BLACK);  // fill screen with black color
  40.   tft.drawFastHLine(0, 50,  tft.width(), ST7735_BLUE);   // draw horizontal blue line at position (0, 50)
  41.   tft.drawFastHLine(0, 102,  tft.width(), ST7735_BLUE);  // draw horizontal blue line at position (0, 102)
  42.  
  43.   tft.setTextColor(ST7735_WHITE, ST7735_BLACK);  // set text color to white with black background
  44.   tft.setTextSize(1);                 // text size = 1
  45.   tft.setCursor(4, 16);               // move cursor to position (4, 16) pixel
  46.   tft.print("ESP8266 + ST7735 TFT");
  47.   tft.setCursor(22, 33);              // move cursor to position (22, 33) pixel
  48.   tft.print("+ DHT22 SENSOR");
  49.   tft.setTextColor(ST7735_GREEN, ST7735_BLACK);     // set text color to green with black background
  50.   tft.setCursor(25, 61);              // move cursor to position (25, 61) pixel
  51.   tft.print("TEMPERATURE =");
  52.   tft.setTextColor(ST7735_YELLOW, ST7735_BLACK);  // set text color to yellow with black background
  53.   tft.setCursor(34, 113);              // move cursor to position (34, 113) pixel
  54.   tft.print("HUMIDITY =");
  55.   tft.setTextSize(2);                     // text size = 2
  56.   tft.drawCircle(83, 80, 2, ST7735_RED);  // print degree symbol ( ° )
  57.   tft.setTextColor(ST7735_RED, ST7735_BLACK);  // set text color to red with black background
  58.   tft.setCursor(89, 78);
  59.   tft.print("C");
  60.  
  61.   // initialize DHT11 sensor
  62.   dht22.begin();
  63. }
  64.  
  65. // main loop
  66. void loop()
  67. {
  68.   // read humidity in rH%
  69.   int Humi = dht22.readHumidity() * 10;
  70.   // read temperature in degree Celsius
  71.   int Temp = dht22.readTemperature() * 10;
  72.  
  73.   // print temperature (in °C)
  74.   tft.setTextColor(ST7735_RED, ST7735_BLACK);  // set text color to red with black background
  75.   tft.setCursor(17, 78);
  76.   if(Temp < 0)    // if temperature < 0
  77.     tft.printf("-%02u.%1u", (abs(Temp)/10)%100, abs(Temp) % 10);
  78.   else            // temperature >= 0
  79.     tft.printf(" %02u.%1u", (Temp/10)%100, Temp % 10);
  80.  
  81.   // print humidity (in %)
  82.   tft.setTextColor(ST7735_CYAN, ST7735_BLACK);  // set text color to cyan with black background
  83.   tft.setCursor(29, 130);
  84.   tft.printf("%02u.%1u %%", (Humi/10)%100, Humi % 10);
  85.  
  86.   delay(1000);    // wait a second
  87. }
  88.  
  89. // end of code.
  90.  
Advertisement
RAW Paste Data Copied
Advertisement