wluijkx

Thermostat (Dutch)

Nov 1st, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.73 KB | None | 0 0
  1. //**************************************************************************************
  2. // Code voor een wifi thermostaat met een DHT sensor (11 of 22) en touchscreen TFT    **
  3. //**************************************************************************************
  4.  
  5. //ESPWebtime code voor het ophalen van tijd via
  6.  
  7. #include <Adafruit_GFX.h>    // Core graphics library
  8. #include <Adafruit_TFTLCD.h> // Hardware-specifieke library
  9. #include <TouchScreen.h>    
  10. #include <DHT.h>             // DHT sensor (temp en luchtvochtigheid)
  11.  
  12. // Fonts
  13. #include <Fonts/FreeMonoBold18pt7b.h>
  14. //#include <Fonts/FreeMonoBold12pt7b.h>
  15. #include <Fonts/FreeMono9pt7b.h>
  16. #include <Fonts/Segment10pt7b.h>
  17. #include <Fonts/Segment22pt7b.h>
  18.  
  19.  
  20. //DHT sensor
  21. #define DHTPIN 53
  22. #define DHTTYPE    DHT11
  23.  
  24. //TFT display en touch
  25. #if defined(__SAM3X8E__)
  26.     #undef __FlashStringHelper::F(string_literal)
  27.     #define F(string_literal) string_literal
  28. #endif
  29.  
  30. #define YP A3  
  31. #define XM A2
  32. #define YM 9
  33. #define XP 8
  34.  
  35. #define TS_MINX 126
  36. #define TS_MAXX 902
  37.  
  38. #define TS_MINY 78
  39. #define TS_MAXY 902
  40.  
  41. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
  42.  
  43. #define LCD_CS A3
  44. #define LCD_CD A2
  45. #define LCD_WR A1
  46. #define LCD_RD A0
  47. #define LCD_RESET A4
  48.  
  49. #include <MCUFRIEND_kbv.h>
  50. MCUFRIEND_kbv tft;
  51. //Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
  52.  
  53. DHT dht(DHTPIN, DHTTYPE);
  54.  
  55. // 16-bit kleuren definities: https://ee-programming-notepad.blogspot.com/2016/10/16-bit-color-generator-picker.html
  56. #define BLACK   0x0000
  57. #define LIGHT   0xF7DE
  58. #define MEDIUM  0x9532
  59. #define DARK    0x6BAD
  60. #define WHITE   0xFFFF
  61. #define ACCENT  0x53C9
  62. #define RED     0xD1E3
  63.  
  64. //Drukbereik touchscreen
  65. #define MINPRESSURE 10
  66. #define MAXPRESSURE 1000
  67.  
  68.  
  69. // Variabelen
  70. float Tset = 19;          //Ingestelde temperatuur
  71. long previousRefrMillis = 0;  // ten behoeve van de schermrefresh
  72. unsigned long currentRefrMillis = millis();
  73. long Refrinterval = 30000;    // Update elke halve minuut
  74. long previousTimeMillis = 0;  // ten behoeve van de klok
  75. unsigned long currentTimeMillis = millis();
  76. long Timeinterval = 60000;    // Update elke minuut
  77. int H = 112;                // Uren binnen honderd (om de eerste 1 er weer vanaf te kunnen knippen)
  78. int M = 100;                // Minuten
  79. String HH;
  80. String MM;
  81. String DisplayTime;
  82. float t = dht.readTemperature();
  83. int humid = dht.readHumidity();
  84. String RecieveFromSerial;
  85. String TimePartH;
  86. String TimePartM;
  87. int power1 = 0;
  88. int power2 = 0;
  89. int MenuOption = 1;
  90. int OldMenuOption;
  91. int MenuTouch = 0;
  92.  
  93.  
  94. //schermposities van de onderdelen
  95. int MenuposX = 20;
  96. int MenuposY = 10;
  97. int curTposX = 20;
  98. int curTposY = 55;
  99. String  TitleCurT = "Huidig";
  100. int setTposX = 165;
  101. int setTposY = 55;
  102. String TitleSetT = "Doel";
  103. int UpBtnX = 225;
  104. int UpBtnY = 120;
  105. int DwnBtnX = 165;
  106. int DwnBtnY = 120;
  107. int TimeDisplX = 120;
  108. int TimeDisplY = 190;
  109. int HumDisplX = 5;
  110. int HumDisplY = 190;
  111. String TitleHumid = "lv:";
  112. int Power1X = 0;
  113. int Power2X = 0;
  114.  
  115.  
  116. void setup() {
  117.   Serial.begin(9600);
  118.   Serial3.begin(9600);
  119.  
  120.   dht.begin();
  121.   tft.reset();
  122.  
  123.   initDisplay();  
  124.   tft.fillScreen(WHITE);
  125.   MenuBtn();
  126.   writeTemp();
  127.   writeTempSet();
  128.   UpBtn();
  129.   DwnBtn();
  130.   writeTime();
  131.   writeHumid();
  132.   if (t<Tset){
  133.     VerwarmingAan();
  134.   }    else
  135.   {  VerwarmingUit();}
  136. }
  137.  
  138. void RecTime()
  139. {
  140.     if((Serial.read()) > (0)){
  141.          RecieveFromSerial= String(Serial.readStringUntil('\n'));
  142.         if((( RecieveFromSerial.substring(1, 1))==('T'))){
  143.              TimePartH=  RecieveFromSerial.substring(0, 2);
  144.              TimePartM=  RecieveFromSerial.substring(2, 4);
  145.             H = TimePartH.toInt()+100;
  146.             M = TimePartM.toInt()+100;
  147.             writeTime();
  148.          }
  149.     }
  150.     if((Serial3.read()) > (0)){
  151.          RecieveFromSerial= String(Serial3.readStringUntil('\n'));
  152.         if((( RecieveFromSerial.substring(1, 1))==('T'))){
  153.              TimePartH=  RecieveFromSerial.substring(0, 2);
  154.              TimePartM=  RecieveFromSerial.substring(2, 4);
  155.             H = TimePartH.toInt()+100;
  156.             M = TimePartM.toInt()+100;
  157.             writeTime();
  158.          }
  159.     }
  160. }
  161.  
  162. void initDisplay(){  
  163.   uint16_t identifier = tft.readID();
  164.   if(identifier == 0x9325) {
  165.     Serial.println(F("ILI9325 LCD driver"));
  166.   } else if(identifier == 0x9328) {
  167.     Serial.println(F("ILI9328 LCD driver"));
  168.   } else if(identifier == 0x4535) {
  169.     Serial.println(F("LGDP4535 LCD driver"));
  170.   }else if(identifier == 0x7575) {
  171.     Serial.println(F("HX8347G LCD driver"));
  172.   } else if(identifier == 0x9341) {
  173.     Serial.println(F("ILI9341 LCD driver"));
  174.   }else if(identifier == 0x7783) {
  175.     Serial.println(F("ST7781 LCD driver"));
  176.   }else if(identifier == 0x8230) {
  177.     Serial.println(F("UC8230 LCD driver"));
  178.   }else if(identifier == 0x8357) {
  179.     Serial.println(F("HX8357D LCD driver"));
  180.   }else if(identifier==0x0101)  {    
  181.       identifier=0x9341;
  182.        Serial.println(F("0x9341 LCD driver"));
  183.   }else {
  184.     Serial.print(F("Onbekende LCD driver chip: "));
  185.     Serial.println(identifier, HEX);
  186.     identifier=0x9341;
  187.   }
  188.   tft.begin(identifier);
  189.   tft.setRotation(3);
  190. }
  191.  
  192. void writeTemp()
  193. {
  194.   tft.setFont(&Segment10pt7b);
  195.   tft.setCursor(curTposX,curTposY);
  196.   tft.setTextColor(DARK);
  197.   tft.print(TitleCurT);
  198.   getTemp();
  199.   char T[8];
  200.   dtostrf(t,6,1,T);
  201.   tft.fillRoundRect(curTposX, curTposY+4, 110, 45, 5, LIGHT);
  202.   tft.drawRoundRect(curTposX, curTposY+4, 110, 45, 5, MEDIUM);
  203.   tft.setTextColor(DARK);
  204.   tft.setFont(&Segment22pt7b);
  205.   tft.setCursor(curTposX-25, curTposY+42);
  206.   tft.print(T);
  207.   int posx = tft.getCursorX();
  208.   int posy = tft.getCursorY();
  209.   tft.setCursor(posx+2,posy-16);
  210.   tft.setFont(&Segment10pt7b);
  211.   tft.print("o");
  212. }
  213.  
  214. void writeTempSet()
  215. {
  216.   char TsetT[8];
  217.   dtostrf(Tset,6,1,TsetT);
  218.   tft.setFont(&Segment10pt7b);
  219.   tft.setCursor(setTposX,setTposY);
  220.   tft.setTextColor(DARK);
  221.   tft.print(TitleSetT);
  222.   tft.fillRoundRect(setTposX, setTposY+4, 110, 45, 5, LIGHT);
  223.   tft.drawRoundRect(setTposX, setTposY+4, 110, 45, 5, MEDIUM);
  224.   tft.setTextColor(DARK);
  225.   tft.setFont(&Segment22pt7b);
  226.   tft.setCursor(setTposX-25, setTposY+42);
  227.   tft.print(TsetT);
  228.   int posx = tft.getCursorX();
  229.   int posy = tft.getCursorY();
  230.   tft.setCursor(posx+2,posy-16);
  231.   tft.setFont(&Segment10pt7b);
  232.   tft.print("o");
  233. }
  234.  
  235. void MenuBtn()
  236. {
  237.   tft.fillRoundRect(MenuposX, MenuposY, 35, 22, 5, LIGHT);
  238.   tft.drawRoundRect(MenuposX, MenuposY, 35, 22, 5, MEDIUM);
  239.   tft.setCursor(MenuposX+5,MenuposY+8);
  240.   tft.setTextColor(DARK);
  241.   tft.setFont(&Segment22pt7b);
  242.   tft.print("...");
  243. }
  244.  
  245. void DrawMenu()
  246. {
  247.   tft.fillScreen(WHITE);
  248.   MenuBtn();
  249.   writeTime();
  250.  
  251. }
  252. void UpBtn()
  253. {
  254.   tft.fillRoundRect(UpBtnX, UpBtnY, 50, 50, 5, LIGHT);
  255.   tft.drawRoundRect(UpBtnX, UpBtnY, 50, 50, 5, MEDIUM);
  256.   tft.fillTriangle(UpBtnX+14,UpBtnY+36,UpBtnX+36,UpBtnY+36,UpBtnX+25,UpBtnY+14,WHITE);
  257.   tft.drawTriangle(UpBtnX+14,UpBtnY+36,UpBtnX+36,UpBtnY+36,UpBtnX+25,UpBtnY+14,MEDIUM);
  258. }
  259. void DwnBtn()
  260. {
  261.   tft.fillRoundRect(DwnBtnX, DwnBtnY, 50, 50, 5, LIGHT);
  262.   tft.drawRoundRect(DwnBtnX, DwnBtnY, 50, 50, 5, MEDIUM);
  263.   tft.fillTriangle(DwnBtnX+14,DwnBtnY+14,DwnBtnX+36,DwnBtnY+14,DwnBtnX+25,DwnBtnY+36,WHITE);
  264.   tft.drawTriangle(DwnBtnX+14,DwnBtnY+14,DwnBtnX+36,DwnBtnY+14,DwnBtnX+25,DwnBtnY+36,MEDIUM);
  265. }
  266.  
  267. //Deze klok gaat ervan uit dat hij 1 keer per minuut wordt aangeroepen
  268. void Clock()
  269. {
  270.     if((M) > (159)){
  271.         M = 100;
  272.         H += 1;
  273.     }
  274.     if((H) > (123)){
  275.         H = 100;
  276.     }
  277.      HH= String(H);
  278.      MM= String(M);
  279.     if((H) > (109)){
  280.          DisplayTime= String( HH.substring(2-1, 3)+String(":")+ MM.substring(2-1, 3));
  281.     }else{
  282.          DisplayTime= String( HH.substring(3-1, 3)+String(":")+ MM.substring(2-1, 3));
  283.     }
  284.     currentTimeMillis = millis();
  285.     if(currentTimeMillis - previousTimeMillis > Timeinterval) {
  286.     previousTimeMillis = currentTimeMillis;
  287.     M += 1;}
  288. }
  289.  
  290. void writeTime()
  291. {
  292.     Clock();
  293.     tft.fillRect(TimeDisplX, TimeDisplY, 80, 50, WHITE);
  294.     tft.setCursor(TimeDisplX+20,TimeDisplY+30);
  295.     //tft.print (H);
  296.     //tft.print (":");
  297.     //tft.print (M);
  298.     tft.setTextColor(DARK);
  299.     tft.setFont(&Segment10pt7b);
  300.     tft.print (DisplayTime);
  301. }
  302.  
  303. void writeHumid()
  304. {
  305.     humid = dht.readHumidity()-1.4;
  306.     tft.fillRect(HumDisplX, HumDisplY, 80, 50, WHITE);
  307.     tft.setCursor(HumDisplX+20,HumDisplY+30);
  308.     //tft.print (H);
  309.     //tft.print (":");
  310.     //tft.print (M);
  311.     tft.setTextColor(DARK);
  312.     tft.setFont(&Segment10pt7b);
  313.     tft.print (TitleHumid);
  314.     tft.print (" ");
  315.     tft.print (humid);
  316.     tft.print ("%");
  317. }
  318.  
  319. void Tup()
  320. {
  321.   Tset = Tset+0.5;
  322. }
  323.  
  324. void Tdown()
  325. {
  326.   Tset = Tset-0.5;
  327. }
  328.  
  329. void VerwarmingAan()
  330. {
  331.   tft.fillCircle(290,220,7,RED);
  332. }
  333.  
  334. void VerwarmingUit()
  335. {
  336.   tft.fillCircle(290,220,7,WHITE);
  337.   tft.drawCircle(290,220,7,RED);
  338. }
  339. void getTemp(){
  340.   t = dht.readTemperature()-1.4;
  341. }
  342.  
  343. void loop()
  344. {
  345.   RecTime();
  346.   digitalWrite(13, HIGH);
  347.   TSPoint p = ts.getPoint();
  348.   digitalWrite(13, LOW);
  349.  
  350.   pinMode(XM, OUTPUT);
  351.   pinMode(YP, OUTPUT);
  352.  
  353. currentRefrMillis = millis();
  354.  
  355. if((currentRefrMillis - previousRefrMillis > Refrinterval) || (MenuTouch == 1)) {
  356. if (MenuOption = 0)
  357. {
  358.     DrawMenu();
  359.  
  360. }
  361. if (MenuOption = 1)
  362. {
  363.  
  364.     getTemp();
  365.     if (t<Tset){
  366.     VerwarmingAan();
  367.   }    else
  368.   {  VerwarmingUit();}
  369.  
  370.     writeTemp();
  371.     writeTime();
  372.     writeHumid();
  373.    }
  374.     if(currentRefrMillis - previousRefrMillis > Refrinterval){
  375.     previousRefrMillis = currentRefrMillis;
  376.     }
  377.     if(MenuTouch = 1){
  378.       MenuTouch = 0;
  379.     }
  380.  
  381. }]4e
  382.  
  383. //Als het scherm wordt aangeraakt
  384.   if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
  385.  
  386. //Vertaal de input van het touchscreen naar coordinaten op het scherm
  387.     p.x = map(p.x, TS_MINX, TS_MAXX, tft.height(), 0);
  388.     p.y = map(p.y, TS_MINY, TS_MAXY, tft.width(), 0);
  389.  
  390. //Menu knop wordt aangeraakt
  391.     if (((p.x) > (MenuposY)) && ((p.x) < (MenuposY+50))  && ((p.y) > (MenuposX)) && ((p.y) < (MenuposX+50))){
  392.     MenuTouch = 1;
  393.  
  394.     if (MenuOption > 0){
  395.     OldMenuOption = MenuOption;
  396.     MenuOption = 0;
  397.     }
  398.     else {
  399.     {MenuOption = OldMenuOption;
  400.     }}
  401.     }
  402.  
  403.  
  404.  
  405. //Temp omhoog knop wordt aangeraakt
  406.     if (((p.x) > (UpBtnY)) && ((p.x) < (UpBtnY+50))  && ((p.y) > (UpBtnX)) && ((p.y) < (UpBtnX+50)) ){
  407.       Tup();
  408.       writeTempSet();
  409.         if (t<Tset){
  410.     VerwarmingAan();
  411.   }    else
  412.   {  VerwarmingUit();}
  413.  
  414.       }
  415.  
  416. //Temp omlaag knop wordt aangeraakt      
  417.     if (((p.x) > (DwnBtnY)) && ((p.x) < (DwnBtnY+50))  && ((p.y) > (DwnBtnX)) && ((p.y) < (DwnBtnX+50)) ){
  418.       Tdown();
  419.       writeTempSet();
  420.   if (t<Tset){
  421.     VerwarmingAan();
  422.   }    else
  423.   {  VerwarmingUit();}
  424.     }
  425.     //Serial.print("("); Serial.print(p.x);
  426.     //Serial.print(", "); Serial.print(p.y);
  427.     //Serial.println(")");
  428.    
  429.     }
  430.  
  431.   }
  432.  
  433.  
Add Comment
Please, Sign In to add comment