mc_workshop

meteo tft1.8 DHT11

Apr 16th, 2021
412
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. I collegamenti
  2. VCC  5V
  3. GND GND
  4.  
  5. DHT 11 al pin 7 come da sketch  #define DHTPIN 7
  6.  
  7. RST 8
  8. RS 9
  9. CS 10
  10. SDA 11
  11. CLK 13
  12.  
  13. i collegamenti definiti nello scketc
  14. #define TFT_RST 8      (RST)
  15. #define TFT_DC 9        (RS)
  16. #define TFT_CS 10       (CS)
  17. #define TFT_MOSI 11  (SDA)
  18. #define TFT_SCLK 13  (CLK)
  19.  
  20. -------
  21.  
  22. /*Stazione meteo
  23.   16-4-2021
  24.   display tft 1.8
  25.   DHT11
  26.   open code mcworkshop
  27.   Visualizzazione su monitor seriale attiva
  28.   Interrogazione del DHT11 ogni 65 secondi
  29. */
  30. #define TFT_CS 10
  31. #define TFT_DC 9
  32. #define TFT_RST 8
  33. #define TFT_SCLK 13
  34. #define TFT_MOSI 11
  35. //
  36. #include <Adafruit_GFX.h>
  37. #include <Adafruit_ST7735.h>
  38. #include <SPI.h>
  39. #include <stdio.h>
  40. //
  41. #include <DHT.h>
  42. #define DHTPIN 7 // DHT11 data pin is connected to Arduino 7 pin.
  43. #define DHTTYPE DHT11
  44. DHT dht(DHTPIN, DHTTYPE);
  45. unsigned long last = 0;
  46. //
  47. #if defined(__SAM3X8E__)
  48. #undef __FlashStringHelper::F(string_literal)
  49. #define F(string_literal) string_literal
  50. #endif
  51. //Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
  52. Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
  53. //
  54. #define COLOR1 ST7735_WHITE
  55. #define COLOR2 ST7735_BLACK
  56. //White theme
  57. //#define COLOR1 ST7735_BLACK
  58. //#define COLOR2 ST7735_WHITE
  59. int text_color_humidex;
  60. float humidity, temperature, humidex;
  61. String message;
  62. //
  63. void setup(void)
  64. {
  65.  Serial.begin(9600);
  66.  dht.begin();
  67.  Serial.println("DHT Sensor Initalized");
  68.  tft.initR(INITR_BLACKTAB);
  69.  tft.fillScreen(COLOR2);
  70. }
  71. void testdrawtext(char* text, uint16_t color)
  72. {
  73.  tft.setCursor(0, 0);
  74.  tft.setTextColor(color);
  75.  tft.setTextWrap(true);
  76.  tft.print(text);
  77. }
  78. void loop()
  79. {
  80.  unsigned long current = millis();
  81.  if ((current - last) >= 65000) {
  82.    humidity = dht.readHumidity();
  83.    temperature = dht.readTemperature();
  84.    Serial.print("umidità:   "); Serial.println(humidity);
  85.    Serial.print("temperatura:   "); Serial.println(temperature);
  86.    //humidex
  87.    humidex = calculate_humidex(temperature, humidity);
  88.    //
  89.    tft.drawRect(0, 0, 128, 160, COLOR1);
  90.    tft.drawLine(0, 50, 128, 50, COLOR1);
  91.    tft.drawLine(0, 100, 128, 100, COLOR1);
  92.    //
  93.    temperature_to_lcd(temperature, 4);
  94.    humidity_to_lcd(humidity, 55);
  95.    humidex_to_lcd(humidex, 105);
  96.    if ((current - last) >= 65000)
  97.    {
  98.      last = current;
  99.    }
  100.  }
  101. }
  102. //tft
  103. void temperature_to_lcd(float temperature, unsigned char text_position)
  104. {
  105.  int text_color;
  106.  tft.setCursor(4, text_position);
  107.  tft.setTextColor(COLOR1, COLOR2);
  108.  tft.setTextSize(1);
  109.  tft.print("Temperatura:");
  110.  tft.setTextSize(3);
  111.  if (temperature > 0)
  112.  {
  113.    text_color = ST7735_BLUE;
  114.  }
  115.  else
  116.  {
  117.    text_color = ST7735_BLUE;
  118.  }
  119.  tft.setCursor(1, text_position + 20);
  120.  fix_number_position(temperature);
  121.  tft.setTextColor(text_color, COLOR2);
  122.  tft.print(temperature, 1);
  123.  tft.setCursor(108, text_position + 20);
  124.  tft.print("C");
  125.  tft.drawChar(90, text_position + 20, 247, text_color, COLOR2, 2);
  126. }
  127. //humidity tft
  128. void humidity_to_lcd(float humidity, unsigned char text_position)
  129. {
  130.  tft.setTextColor(COLOR1, COLOR2);
  131.  tft.setCursor(4, text_position);
  132.  tft.setTextSize(1);
  133.  tft.println("Umidita':");
  134.  tft.setTextSize(3);
  135.  tft.setCursor(1, text_position + 20);
  136.  fix_number_position(humidity);
  137.  tft.print(humidity, 1);
  138.  tft.print(" %");
  139. }
  140. //Humidex tft indice di calore
  141. void humidex_to_lcd(float humidex, unsigned char text_position)
  142. {
  143.  tft.setCursor(4, text_position);
  144.  tft.setTextSize(1);
  145.  tft.println("Humidex [6 livelli]:");
  146.  tft.setTextSize(3);
  147.  tft.setCursor(1, text_position + 17);
  148.  if ((humidex >= 21) && (temperature < 44))
  149.  {
  150.    fix_number_position(humidex);
  151.    get_humidex_color_warning_message(humidex);
  152.    tft.setTextColor(text_color_humidex, COLOR2);
  153.    tft.print(humidex, 1);
  154.    tft.setCursor(108, text_position + 17);
  155.    tft.print("C");
  156.    tft.drawChar(90, text_position + 17, 247, text_color_humidex, COLOR2, 2); //degree symbol
  157.    tft.setCursor(3, text_position + 43);
  158.    tft.setTextSize(1);
  159.    tft.print(message);
  160.  }
  161.  else
  162.  {
  163.    tft.print(" --.-");
  164.    tft.setCursor(108, text_position + 17);
  165.    tft.print("C");
  166.    tft.drawChar(90, text_position + 17, 247, COLOR1, COLOR2, 2); //degree symbol
  167.    tft.setCursor(1, text_position + 43);
  168.    tft.setTextSize(1);
  169.    tft.println(" ");
  170.  }
  171.  ;
  172. }
  173. void fix_number_position(float number)
  174. {
  175.  if ((number >= -40) && (number < -9.9))
  176.  {
  177.    ;
  178.  }
  179.  if ((number >= -9.9) && (number < 0.0))
  180.  {
  181.    tft.print(" ");
  182.  }
  183.  if ((number >= 0.0) && (number < 9.9))
  184.  {
  185.    tft.print(" ");
  186.  }
  187.  if ((number >= 9.9) && (number < 99.9))
  188.  {
  189.    tft.print(" ");
  190.  }
  191.  if ((number >= 99.9) && (number < 151))
  192.  {
  193.    tft.print("");
  194.  }
  195. }
  196. uint16_t Color565(uint8_t r, uint8_t g, uint8_t b)
  197. {
  198.  return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
  199. }
  200. //function to calculete Humidex
  201. float calculate_humidex(float temperature, float humidity)
  202. {
  203.  float e;
  204.  e = (6.112 * pow(10, (7.5 * temperature / (237.7 + temperature))) * humidity / 100); //vapor pressure
  205.  float humidex = temperature + 0.55555555 * (e - 10.0); //humidex
  206.  return humidex;
  207. }
  208. void get_humidex_color_warning_message(float humidex)
  209. {
  210.  if ((humidex >= 21) && (humidex < 27))
  211.  {
  212.    text_color_humidex = Color565(0, 255, 0);
  213.    message = "LIV 1 OK ";
  214.  }
  215.  if ((humidex >= 27) && (humidex < 30))
  216.  {
  217.    text_color_humidex = Color565(255, 255, 0);
  218.    message = "LIV 2 ";
  219.  }
  220.  if ((humidex >= 30) && (humidex < 35))
  221.  {
  222.    text_color_humidex = Color565(255, 255, 0);
  223.    message = "LIV 3 ";
  224.  }
  225.  if ((humidex >= 35) && (humidex < 40))
  226.  {
  227.    text_color_humidex = Color565(255, 0, 0);
  228.    message = " ATTENZIONE LIV 4 ";
  229.  }
  230.  if ((humidex >= 40) && (humidex < 55))
  231.  {
  232.    text_color_humidex = Color565(255, 0, 0);
  233.    message = "PERICOLO LIV 5 ";
  234.  }
  235.  if ((humidex >= 55))
  236.  {
  237.    text_color_humidex = Color565(255, 0, 0);
  238.    message = "ALLARME LIV MAX ";
  239.  }
  240. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×