Advertisement
wluijkx

Thermostaat met FlappyBird

Nov 12th, 2020 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 46.46 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. #include <NTPClient.h>
  7.  
  8. #include <ESP8266WiFi.h>
  9.  
  10. #include <WiFiUdp.h>
  11.  
  12. #include <EEPROM.h>
  13.  
  14. #include "Adafruit_MQTT.h"
  15. #include "Adafruit_MQTT_Client.h"
  16. #define AIO_SERVER "192.168.2.33"
  17. #define AIO_SERVERPORT 1883 // use 8883 for SSL
  18. uint32_t x = 0;
  19.  
  20. WiFiClient client;
  21. Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT);
  22.  
  23. Adafruit_MQTT_Publish ToggleSwitch1 = Adafruit_MQTT_Publish(&mqtt, "cmnd/Schakelaar01/power");
  24. Adafruit_MQTT_Subscribe ToggleSwitch1Status = Adafruit_MQTT_Subscribe(&mqtt, "stat/Schakelaar01/POWER");
  25. Adafruit_MQTT_Publish ToggleSwitch2 = Adafruit_MQTT_Publish(&mqtt, "cmnd/Schakelaar02/power");
  26. Adafruit_MQTT_Subscribe ToggleSwitch2Status = Adafruit_MQTT_Subscribe(&mqtt, "stat/Schakelaar02/POWER");
  27.  
  28. #include "flappyBird.h"
  29.  
  30. const char* ssid = "";
  31. const char* password = "";
  32.  
  33. const long utcOffsetInSeconds = 3600;
  34.  
  35. // Define NTP Client to get time
  36. WiFiUDP ntpUDP;
  37. NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
  38.  
  39. #include <SPI.h>
  40.  
  41. #include <Adafruit_GFX.h>
  42.  
  43. #include <Adafruit_ILI9341.h>
  44.  
  45. #include <XPT2046_Touchscreen.h>
  46.  
  47. #define TFT_CS D0 //for D1 mini or TFT I2C Connector Shield (V1.1.0 or later)
  48. #define TFT_DC D8 //for D1 mini or TFT I2C Connector Shield (V1.1.0 or later)
  49. #define TFT_RST -1 //for D1 mini or TFT I2C Connector Shield (V1.1.0 or later)
  50. #define TS_CS D3 //for D1 mini or TFT I2C Connector Shield (V1.1.0 or later)
  51.  
  52. // #define TFT_CS 14  //for D32 Pro
  53. // #define TFT_DC 27  //for D32 Pro
  54. // #define TFT_RST 33 //for D32 Pro
  55. // #define TS_CS  12 //for D32 Pro
  56.  
  57. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
  58. XPT2046_Touchscreen ts(TS_CS);
  59.  
  60. #include <DHT.h> // DHT sensor (temp en luchtvochtigheid)
  61.  
  62. // Fonts https://rop.nl/truetype2gfx/
  63. #include "Segment10pt7b.h"
  64.  
  65. #include "Segment18pt7b.h"
  66.  
  67. #include "Segment22pt7b.h"
  68.  
  69. #include "Segment48pt7b.h"
  70.  
  71. #include "icons14pt7b.h"
  72.  
  73. #include "icons30pt7b.h"
  74.  
  75. //DHT sensor
  76. #define DHTPIN D4
  77. #define DHTTYPE DHT11
  78.  
  79. //Vertalen
  80. #define TS_MINX 230
  81. #define TS_MAXX 3600
  82.  
  83. #define TS_MINY 330
  84. #define TS_MAXY 3700
  85.  
  86. //https://github.com/wemos/D1_mini_Examples/blob/master/examples/04.Shields/TFT_2.4_Touch_Shield/TFT_TouchTest/TFT_TouchTest.ino
  87.  
  88. DHT dht(DHTPIN, DHTTYPE);
  89.  
  90. // 16-bit kleuren definities: https://ee-programming-notepad.blogspot.com/2016/10/16-bit-color-generator-picker.html
  91. #define BLACK 0x0000
  92. #define WHITE 0xFFFF
  93. #define BUTTONBG 0xFFFF
  94. #define BUTTONBGACT 0xDEFB
  95. #define BUTTONEDGE 0x9492
  96. #define TEXTCOLOR 0x0000
  97. #define SCREENCOLOR 0xFFFF
  98. #define TOGGLEON 0x53C9
  99. #define TOGGLEOFF 0xDEFB
  100. #define HEATON 0xD1E3
  101. #define HEATOFF 0xDEFB
  102. #define WIFION 0x0000
  103. #define WIFIOFF 0xD1E3
  104. #define FLAPPYSKY 0xAE7F
  105. #define FLAPPYGRASS 0x3E29
  106. #define FLAPPYBOX 0x558B
  107.  
  108. //Drukbereik touchscreen
  109. #define MINPRESSURE 400
  110. #define MAXPRESSURE 3000
  111.  
  112. //eeprom adressen van de opgeslagen variabelen
  113.  
  114. // Variabelen
  115. unsigned long currentTimeMillis = millis();
  116. unsigned long previousTimeMillis;
  117. unsigned long currentGetTempMillis = millis();
  118. unsigned long previousGetTempMillis;
  119. unsigned long currentScreenTimOuteMillis = millis();
  120. unsigned long previousScreenTimeOutMillis;
  121. int TimeOutPeriod = 20000; //Timeout voor het dimmen van het scherm
  122. int DimMode = 1; //1= helder, 2 = gedimd, 3 = nachtscherm, 4 = uit
  123. float TempSet = 14; //Ingestelde temperatuur
  124. float PrevTempSet;
  125. int D = 1;
  126. String DayNames[7] = { "Zo", "Ma", "Di", "Wo", "Do", "Vr", "Za" };
  127. String LongDayNames[7] = { "Zondag", "Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag" };
  128. String DD = DayNames[1];
  129. String DDD = LongDayNames[1];
  130. int H = 112; // Uren binnen honderd (om de eerste 1 er weer vanaf te kunnen knippen)
  131. int M = 100; // Minuten
  132. int HM = 1200; // UrenMinuten
  133. String HH;
  134. String MM;
  135. String DisplayTime;
  136. String PrevDisplayTime;
  137. float Temp = dht.readTemperature();
  138. float PrevTemp;
  139. float SchemaTempH = 18.5;
  140. float SchemaTempL = 14;
  141. int SchemaTempHint = 185;
  142. int SchemaTempLint = 140;
  143. float PrevSchemaTempH = 0;
  144. float PrevSchemaTempL = 0;
  145. int Humid = dht.readHumidity();
  146. int PrevHumid;
  147. String RecieveFromSerial;
  148. String TimePartH;
  149. String TimePartM;
  150. int HeatState = 0;
  151. int PrevHeatState = 2;
  152. int power1 = 0;
  153. int power2 = 0;
  154. int DisplayScreen = 1;
  155. int PrevDisplayScreen = 0;
  156. int ScreenTouch = 0;
  157. int Toggle1 = 0;
  158. int Toggle2 = 0;
  159. int PrevToggle1 = 2;
  160. int PrevToggle2 = 2;
  161. int WifiStatus = 0;
  162. int PrevWifiStatus = 2;
  163. int HeatingMode = 3; //1=automatisch 2=uitschakeltimer 3=handmatig
  164. String HeatingModeIcon1 = "Z";
  165. String HeatingModeIcon2 = "k";
  166. String HeatingModeIcon3 = "b";
  167. int PrevHeatingMode = 9;
  168. //String HeatingModeIcon[] = {"Z","k","b"};
  169. int NightModeTime = 2230;
  170. int DayModeTime = 700;
  171. int DimBrightness = 100;
  172. int PrevDimBrightness = 0;
  173. int WifiTimeOutCounter = 0;
  174.  
  175. //Schema variabelen
  176. int thermostatProg[42];
  177. int EEPROMAddrTempHigh;
  178. int EEPROMAddrTempLow;
  179. int EEPROMAddrTimeHigh;
  180. int EEPROMAddrTimeLow;
  181. int SchemaNumber = 1;
  182. int PrevSchemaNumber = 4;
  183. int TimeHighH = 7;
  184. int TimeHighM = 0;
  185. int TimeHighHM = 7;
  186. int TimeLowH = 18;
  187. int TimeLowM = 0;
  188. int TimeLowHM = 18;
  189. int PrevTimeHighH = 99;
  190. int PrevTimeHighM = 99;
  191. int PrevTimeLowH = 99;
  192. int PrevTimeLowM = 99;
  193. int SetDay = 1;
  194. int PrevSetDay = 9;
  195.  
  196. //schermconfiguratie
  197. //Icoontjes: z Power, O sun high, Q Sun low, k clock, = Wifi, ! error, V checkbox checked, W checkbox empty, U Unlocked, L Locked, d Settings, F WaterDrop, 4 up, ~ down, € €, B Menu btn, h Menu open, , arrow < , . arrow >, Y Pin
  198. int MenuBtnX = 140;
  199. int MenuBtnY = 200;
  200. int CurTempX = 5;
  201. int CurTempY = 50;
  202. String TitleCurTemp = "";
  203. int SetTempX = 0;
  204. int SetTempY = 100;
  205. String TitleSetT = "";
  206. int CurTemp2X = 110;
  207. int CurTemp2Y = 50;
  208. String TitleCurTemp2 = "";
  209. int SetTemp2X = 62;
  210. int SetTemp2Y = 100;
  211. String TitleSetT2 = "";
  212. int UpBtnX = 260;
  213. int UpBtnY = 120;
  214. char UpBtnIcon = '+';
  215. int DwnBtnX = 10;
  216. int DwnBtnY = 120;
  217. char DwnBtnIcon = '~';
  218. int TimeDisplX = 100;
  219. int TimeDisplY = 5;
  220. int HumidX = 5;
  221. int HumidY = 210;
  222. String HumidIcon = "F";
  223. int Toggle1X = 210;
  224. int Toggle1Y = 30;
  225. char Toggle1Icon = 'z';
  226. String Toggle1Title = "";
  227. int Toggle2X = 210;
  228. int Toggle2Y = 120;
  229. char Toggle2Icon = 'z';
  230. int HeatStateX = 285;
  231. int HeatStateY = 232;
  232. int HeatModeX = 240;
  233. int HeatModeY = 232;
  234.  
  235. char HeatOnIcon = 'O';
  236. char HeatOffIcon = 'Q';
  237. int WifiPosX = 10;
  238. int WifiPosY = 25;
  239. char WifiOnIcon = '=';
  240. char WifiOffIcon = '!';
  241.  
  242. String SaveIcon = "j";
  243. String MenuIcon = "H";
  244. String MenuOption1 = "Automatisch";
  245. String MenuOption2 = "Uitschakeltimer";
  246. String MenuOption3 = "Handmatig";
  247. String MenuOption4 = "Schema";
  248. String MenuOption5 = "Schakelaarklok";
  249. String MenuOption6 = "Andere ruimte";
  250.  
  251. String MenuOption7 = "Instellingen";
  252. String MenuOption8 = "Scherm";
  253. String MenuOption9 = "Nachtscherm";
  254. String MenuOption10 = "FlappyBird";
  255.  
  256. void setup()
  257. {
  258.     Serial.begin(9600);
  259.     EEPROM.begin(512);
  260.     EEPROM.get(0, DimBrightness);
  261.     timeClient.begin();
  262.     timeClient.setUpdateInterval(3600000); //Elk uur updaten i.p.v. elke minuut
  263.     dht.begin();
  264.     initDisplay();
  265.     pinMode(D1, OUTPUT);
  266.     digitalWrite(D1, 1);
  267.     tft.fillScreen(SCREENCOLOR);
  268.     tft.setTextColor(TEXTCOLOR);
  269.     tft.setFont(&Segment10pt7b);
  270.     tft.setCursor(50, 150);
  271.     tft.print("Verbinden");
  272.  
  273.     WiFi.begin(ssid, password);
  274.  
  275.     while ((WiFi.status() != WL_CONNECTED) && (WifiTimeOutCounter < 30)) {
  276.         delay(500);
  277.         tft.print(".");
  278.         WifiTimeOutCounter += 1;
  279.     }
  280.     GetWifiStatus();
  281.     tft.fillScreen(SCREENCOLOR);
  282.     GetTime();
  283.     delay(500);
  284.     mqtt.subscribe(&ToggleSwitch1Status);
  285. }
  286.  
  287. void MQTT_connect();
  288.  
  289. void initDisplay()
  290. {
  291.     ts.begin();
  292.     ts.setRotation(3);
  293.     tft.begin();
  294.     tft.setRotation(3);
  295. }
  296.  
  297. void ScreenTimeOut()
  298. {
  299.     currentScreenTimOuteMillis = millis();
  300.     if ((previousScreenTimeOutMillis + TimeOutPeriod < currentScreenTimOuteMillis) && (DisplayScreen != 8)) {
  301.         if (((H - 100) * 100 + (M - 100) > NightModeTime) || ((H - 100) * 100 + (M - 100) < DayModeTime)) {
  302.             digitalWrite(D1, 0);
  303.             DisplayScreen = -1;
  304.         }
  305.         else {
  306.             analogWrite(D1, DimBrightness);
  307.         }
  308.     }
  309.     if (previousScreenTimeOutMillis + TimeOutPeriod + 2000 < currentScreenTimOuteMillis) {
  310.         if (((H - 100) * 100 + (M - 100) > NightModeTime) || ((H - 100) * 100 + (M - 100) < DayModeTime)) {
  311.         }
  312.         else {
  313.             DisplayScreen = 1;
  314.         }
  315.     }
  316. }
  317.  
  318. void GetWifiStatus()
  319. {
  320.     if (WiFi.status() == WL_CONNECTED) {
  321.         WifiStatus = 1;
  322.     }
  323.     else {
  324.         WifiStatus = 0;
  325.         WiFi.reconnect();
  326.     }
  327. }
  328.  
  329. void DrawWifiStatus(int posX, int posY)
  330. {
  331.     if ((WifiStatus != PrevWifiStatus) || (DisplayScreen != PrevDisplayScreen)) {
  332.         tft.fillRect(posX - 4, posY - 20, 35, 35, SCREENCOLOR);
  333.         tft.setCursor(posX, posY);
  334.         tft.setFont(&icons14pt7b);
  335.         if (WifiStatus == 1) {
  336.             tft.setTextColor(WIFION);
  337.             tft.print(WifiOnIcon);
  338.         }
  339.         else {
  340.             tft.setTextColor(WIFIOFF);
  341.             tft.print(WifiOffIcon);
  342.         }
  343.         PrevWifiStatus = WifiStatus;
  344.     }
  345. }
  346. void GetTime()
  347. {
  348.     timeClient.update();
  349.  
  350.     H = timeClient.getHours() + 100;
  351.     M = timeClient.getMinutes() + 100;
  352.     D = timeClient.getDay();
  353.     DD = DayNames[D];
  354.     DDD = LongDayNames[D];
  355. }
  356.  
  357. void DrawHeatState(int posX, int posY)
  358. {
  359.     if (DisplayScreen != PrevDisplayScreen) {
  360.         PrevHeatState = 2;
  361.     }
  362.     tft.setCursor(posX, posY);
  363.     tft.setFont(&icons14pt7b);
  364.     if (Temp < TempSet) {
  365.         HeatState = 1;
  366.         tft.setTextColor(HEATON);
  367.         if (HeatState != PrevHeatState) {
  368.             tft.fillRect(posX - 4, posY - 20, 35, 35, SCREENCOLOR);
  369.             tft.print(HeatOnIcon);
  370.             PrevHeatState = 1;
  371.         }
  372.     }
  373.     else {
  374.         HeatState = 0;
  375.         tft.setTextColor(HEATOFF);
  376.         if (HeatState != PrevHeatState) {
  377.             tft.fillRect(posX - 4, posY - 20, 35, 35, SCREENCOLOR);
  378.             tft.print(HeatOffIcon);
  379.             PrevHeatState = 0;
  380.         }
  381.     }
  382. }
  383.  
  384. void DrawHeatingMode(int posX, int posY)
  385. {
  386.     if ((DisplayScreen != PrevDisplayScreen) || (HeatingMode != PrevHeatingMode)) {
  387.         tft.setCursor(posX, posY);
  388.         tft.setFont(&icons14pt7b);
  389.         tft.setTextColor(TEXTCOLOR);
  390.         tft.fillRect(posX - 4, posY - 20, 35, 35, SCREENCOLOR);
  391.         if (HeatingMode == 1) {
  392.             tft.print(HeatingModeIcon1);
  393.         }
  394.         else if (HeatingMode == 2) {
  395.             tft.print(HeatingModeIcon2);
  396.         }
  397.         else if (HeatingMode == 3) {
  398.             tft.print(HeatingModeIcon3);
  399.         }
  400.         PrevHeatingMode = HeatingMode;
  401.     }
  402. }
  403. void ToggleHeating()
  404. {
  405.     if (DisplayScreen != PrevDisplayScreen) {
  406.         //        getTemp();
  407.         DrawHeatState(HeatStateX, HeatStateY);
  408.     }
  409. }
  410.  
  411. void RecTime()
  412. {
  413.     if ((Serial.read()) > (0)) {
  414.         RecieveFromSerial = String(Serial.readStringUntil('\n'));
  415.         if (((RecieveFromSerial.substring(1, 1)) == ("T"))) {
  416.             TimePartH = RecieveFromSerial.substring(0, 2);
  417.             TimePartM = RecieveFromSerial.substring(2, 4);
  418.             H = TimePartH.toInt() + 100;
  419.             M = TimePartM.toInt() + 100;
  420.             DrawTime(TimeDisplX, TimeDisplY);
  421.         }
  422.     }
  423. }
  424.  
  425. void DrawCurTemp(int posX, int posY)
  426. {
  427.     getTemp();
  428.     if ((Temp != PrevTemp) || (DisplayScreen != PrevDisplayScreen)) {
  429.         tft.setFont(&Segment10pt7b);
  430.         tft.setCursor(posX, posY - 4);
  431.         tft.setTextColor(TEXTCOLOR);
  432.         tft.print(TitleCurTemp);
  433.         getTemp();
  434.         char T[8];
  435.         dtostrf(Temp, 6, 1, T);
  436.         tft.fillRoundRect(posX, posY, 110, 45, 5, BUTTONBG);
  437.         tft.setTextColor(TEXTCOLOR);
  438.         tft.setFont(&Segment22pt7b);
  439.         tft.setCursor(posX - 25, posY + 42);
  440.         tft.print(T);
  441.         int curposx = tft.getCursorX();
  442.         int curposy = tft.getCursorY();
  443.         tft.setCursor(curposx + 2, curposy - 16);
  444.         tft.setFont(&Segment10pt7b);
  445.         tft.print("o");
  446.         PrevTemp = Temp;
  447.     }
  448. }
  449.  
  450. void DrawSetTemp(int posX, int posY)
  451. {
  452.     if ((TempSet != PrevTempSet) || (DisplayScreen != PrevDisplayScreen)) {
  453.         char TsetT[8];
  454.         dtostrf(TempSet, 6, 1, TsetT);
  455.         String TempString = String(TsetT);
  456.         String TempString2 = String(TsetT);
  457.         TempString.replace("1", "");
  458.         tft.setFont(&Segment10pt7b);
  459.         tft.setCursor(SetTempX, SetTempY);
  460.         tft.setTextColor(TEXTCOLOR);
  461.         tft.print(TitleSetT);
  462.         tft.fillRoundRect(posX, posY + 4, 190, 82, 5, BUTTONBG);
  463.         //   tft.drawRoundRect(setTposX, setTposY + 4, 190, 82, 5, BUTTONEDGE);
  464.         tft.setTextColor(TEXTCOLOR);
  465.         tft.setFont(&Segment48pt7b);
  466.         //als er een 1 in e temperatuur zit dan is er een andere uitlijning nodig
  467.         if (TempString == TempString2) {
  468.             tft.setCursor(posX - 86, posY + 75);
  469.         }
  470.         else {
  471.             tft.setCursor(posX - 70, posY + 75);
  472.         }
  473.         tft.print(TsetT);
  474.         int curposx = tft.getCursorX();
  475.         int curposy = tft.getCursorY();
  476.         tft.setCursor(curposx + 2, curposy - 35);
  477.         tft.setFont(&Segment22pt7b);
  478.         tft.print("o");
  479.         PrevTempSet = TempSet;
  480.     }
  481. }
  482.  
  483. void DrawSaveBtn(int posX, int posY)
  484. {
  485.     if (DisplayScreen != PrevDisplayScreen) {
  486.         tft.fillRoundRect(posX, posY, 35, 35, 5, BUTTONBG);
  487.         tft.drawRoundRect(posX, posY, 35, 35, 5, BUTTONEDGE);
  488.         tft.setCursor(posX + 7, posY + 26);
  489.         tft.setTextColor(TEXTCOLOR);
  490.         tft.setFont(&icons14pt7b);
  491.         tft.print(SaveIcon);
  492.     }
  493. }
  494. void DrawMenuBtn(int posX, int posY)
  495. {
  496.     if (DisplayScreen != PrevDisplayScreen) {
  497.         tft.fillRoundRect(posX, posY, 35, 35, 5, BUTTONBG);
  498.         tft.drawRoundRect(posX, posY, 35, 35, 5, BUTTONEDGE);
  499.         tft.setCursor(posX + 7, posY + 26);
  500.         tft.setTextColor(TEXTCOLOR);
  501.         tft.setFont(&icons14pt7b);
  502.         tft.print(MenuIcon);
  503.     }
  504. }
  505.  
  506. void DrawMenu()
  507. {
  508.     DrawMenuBtn(MenuBtnX, MenuBtnY);
  509.     tft.setTextColor(TEXTCOLOR);
  510.     tft.setFont(&Segment10pt7b);
  511.     int xpos = 5;
  512.     int ypos = 10;
  513.  
  514.     if (HeatingMode == 1) {
  515.         tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBGACT);
  516.     }
  517.     else {
  518.         tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBG);
  519.     }
  520.     tft.drawRoundRect(xpos, ypos, 150, 50, 5, BUTTONEDGE);
  521.     tft.setCursor(xpos + 10, ypos + 32);
  522.     tft.print(MenuOption1);
  523.     ypos += 65;
  524.  
  525.     if (HeatingMode == 2) {
  526.         tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBGACT);
  527.     }
  528.     else {
  529.         tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBG);
  530.     }
  531.     tft.drawRoundRect(xpos, ypos, 150, 50, 5, BUTTONEDGE);
  532.     tft.setCursor(xpos + 10, ypos + 32);
  533.     tft.print(MenuOption2);
  534.     ypos += 65;
  535.  
  536.     if (HeatingMode == 3) {
  537.         tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBGACT);
  538.     }
  539.     else {
  540.         tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBG);
  541.     }
  542.     tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBG);
  543.     tft.drawRoundRect(xpos, ypos, 150, 50, 5, BUTTONEDGE);
  544.     tft.setCursor(xpos + 10, ypos + 32);
  545.     tft.print(MenuOption3);
  546.     ypos += 65;
  547.  
  548.     xpos = 165;
  549.     ypos = 10;
  550.  
  551.     tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBG);
  552.     tft.drawRoundRect(xpos, ypos, 150, 50, 5, BUTTONEDGE);
  553.     tft.setCursor(xpos + 10, ypos + 32);
  554.     tft.print(MenuOption4);
  555.     ypos += 65;
  556.  
  557.     tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBG);
  558.     tft.drawRoundRect(xpos, ypos, 150, 50, 5, BUTTONEDGE);
  559.     tft.setCursor(xpos + 10, ypos + 32);
  560.     tft.print(MenuOption5);
  561.     ypos += 65;
  562.  
  563.     tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBG);
  564.     tft.drawRoundRect(xpos, ypos, 150, 50, 5, BUTTONEDGE);
  565.     tft.setCursor(xpos + 10, ypos + 32);
  566.     tft.print(MenuOption6);
  567.     ypos += 65;
  568. }
  569.  
  570. void DrawMenu2()
  571. {
  572.     DrawMenuBtn(MenuBtnX, MenuBtnY);
  573.     tft.setTextColor(TEXTCOLOR);
  574.     tft.setFont(&Segment10pt7b);
  575.     int xpos = 5;
  576.     int ypos = 10;
  577.  
  578.     tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBG);
  579.     tft.drawRoundRect(xpos, ypos, 150, 50, 5, BUTTONEDGE);
  580.     tft.setCursor(xpos + 10, ypos + 32);
  581.     tft.print(MenuOption7);
  582.     ypos += 65;
  583.  
  584.     tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBG);
  585.     tft.drawRoundRect(xpos, ypos, 150, 50, 5, BUTTONEDGE);
  586.     tft.setCursor(xpos + 10, ypos + 32);
  587.     tft.print(MenuOption8);
  588.  
  589.     xpos = 165;
  590.     ypos = 10;
  591.  
  592.     tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBG);
  593.     tft.drawRoundRect(xpos, ypos, 150, 50, 5, BUTTONEDGE);
  594.     tft.setCursor(xpos + 10, ypos + 32);
  595.     tft.print(MenuOption9);
  596.     ypos += 65;
  597.  
  598.     tft.fillRoundRect(xpos, ypos, 150, 50, 5, BUTTONBG);
  599.     tft.drawRoundRect(xpos, ypos, 150, 50, 5, BUTTONEDGE);
  600.     tft.setCursor(xpos + 10, ypos + 32);
  601.     tft.print(MenuOption10);
  602.     ypos += 65;
  603. }
  604.  
  605. void DrawToggle1(int PosX, int PosY)
  606. {
  607.     if ((Toggle1 != PrevToggle1) || (DisplayScreen != PrevDisplayScreen)) {
  608.         tft.fillRoundRect(PosX, PosY, 80, 80, 15, BUTTONBG);
  609.         tft.drawRoundRect(PosX, PosY, 80, 80, 15, BUTTONEDGE);
  610.         tft.setFont(&icons30pt7b);
  611.         tft.setCursor(PosX + 16, PosY + 55);
  612.         if (Toggle1 == 0) {
  613.             tft.setTextColor(TOGGLEOFF);
  614.             PrevToggle1 = 0;
  615.             tft.print(Toggle1Icon);
  616.         }
  617.         else {
  618.             tft.setTextColor(TOGGLEON);
  619.             PrevToggle1 = 1;
  620.             tft.print(Toggle1Icon);
  621.         }
  622.     }
  623. }
  624. void DrawToggle1Dark(int PosX, int PosY)
  625. {
  626.     if ((Toggle1 != PrevToggle1) || (DisplayScreen != PrevDisplayScreen)) {
  627.         tft.fillRoundRect(PosX, PosY, 80, 80, 15, BLACK);
  628.         tft.drawRoundRect(PosX, PosY, 80, 80, 15, WHITE);
  629.         tft.setFont(&icons30pt7b);
  630.         tft.setCursor(PosX + 16, PosY + 55);
  631.         if (Toggle1 == 0) {
  632.             tft.setTextColor(TOGGLEOFF);
  633.             PrevToggle1 = 0;
  634.             tft.print(Toggle1Icon);
  635.         }
  636.         else {
  637.             tft.setTextColor(TOGGLEON);
  638.             PrevToggle1 = 1;
  639.             tft.print(Toggle1Icon);
  640.         }
  641.     }
  642. }
  643.  
  644. void SetToggle1()
  645. {
  646.     if (Toggle1 == 0) {
  647.         Serial.print(F("cmnd/Schakelaar02/power"));
  648.         Serial.print(x);
  649.         Serial.print("...");
  650.         if (!ToggleSwitch1.publish(1)) {
  651.             Serial.println(F("Failed"));
  652.         }
  653.         else {
  654.             Serial.println(F("OK!"));
  655.         }
  656.  
  657.         Toggle1 = 1;
  658.     }
  659.     else {
  660.         Toggle1 = 0;
  661.         if (!ToggleSwitch1.publish(0)) {
  662.             Serial.println(F("Failed"));
  663.         }
  664.         else {
  665.             Serial.println(F("OK!"));
  666.         }
  667.     }
  668.     DrawToggle1(Toggle1X, Toggle1Y);
  669. }
  670. void DrawToggle2(int PosX, int PosY)
  671. {
  672.     if ((Toggle2 != PrevToggle2) || (DisplayScreen != PrevDisplayScreen)) {
  673.         tft.fillRoundRect(PosX, PosY, 80, 80, 15, BUTTONBG);
  674.         tft.drawRoundRect(PosX, PosY, 80, 80, 15, BUTTONEDGE);
  675.         tft.setFont(&icons30pt7b);
  676.         tft.setCursor(PosX + 16, PosY + 55);
  677.         if (Toggle2 == 0) {
  678.             tft.setTextColor(TOGGLEOFF);
  679.             PrevToggle2 = 0;
  680.             tft.print(Toggle2Icon);
  681.         }
  682.         else {
  683.             tft.setTextColor(TOGGLEON);
  684.             PrevToggle2 = 1;
  685.             tft.print(Toggle2Icon);
  686.         }
  687.     }
  688. }
  689. void DrawToggle2Dark(int PosX, int PosY)
  690. {
  691.     if ((Toggle2 != PrevToggle2) || (DisplayScreen != PrevDisplayScreen)) {
  692.         tft.fillRoundRect(PosX, PosY, 80, 80, 15, BLACK);
  693.         tft.drawRoundRect(PosX, PosY, 80, 80, 15, WHITE);
  694.         tft.setFont(&icons30pt7b);
  695.         tft.setCursor(PosX + 16, PosY + 55);
  696.         if (Toggle2 == 0) {
  697.             tft.setTextColor(TOGGLEOFF);
  698.             PrevToggle2 = 0;
  699.             tft.print(Toggle2Icon);
  700.         }
  701.         else {
  702.             tft.setTextColor(TOGGLEON);
  703.             PrevToggle2 = 1;
  704.             tft.print(Toggle2Icon);
  705.         }
  706.     }
  707. }
  708.  
  709. void SetToggle2()
  710. {
  711.     if (Toggle2 == 0) {
  712.         ToggleSwitch2.publish(1);
  713.         Toggle2 = 1;
  714.     }
  715.     else {
  716.         ToggleSwitch2.publish(0);
  717.         Toggle2 = 0;
  718.     }
  719.     DrawToggle2(Toggle2X, Toggle2Y);
  720. }
  721.  
  722. void DrawUpBtn(int posX, int posY)
  723. {
  724.     if (DisplayScreen != PrevDisplayScreen) {
  725.         tft.fillRoundRect(posX, posY, 50, 50, 5, BUTTONBG);
  726.         tft.drawRoundRect(posX, posY, 50, 50, 5, BUTTONEDGE);
  727.         tft.setCursor(posX + 12, posY + 37);
  728.         tft.setTextColor(TEXTCOLOR);
  729.         tft.setFont(&Segment22pt7b);
  730.         tft.print("+");
  731.     }
  732. }
  733. void DrawDwnBtn(int posX, int posY)
  734. {
  735.     if (DisplayScreen != PrevDisplayScreen) {
  736.         tft.fillRoundRect(posX, posY, 50, 50, 5, BUTTONBG);
  737.         tft.drawRoundRect(posX, posY, 50, 50, 5, BUTTONEDGE);
  738.         tft.setCursor(posX + 15, posY + 40);
  739.         tft.setTextColor(TEXTCOLOR);
  740.         tft.setFont(&Segment22pt7b);
  741.         tft.print("-");
  742.     }
  743. }
  744. void DrawUpBtnSm(int posX, int posY)
  745. {
  746.     if (DisplayScreen != PrevDisplayScreen) {
  747.         tft.fillRoundRect(posX, posY, 35, 35, 5, BUTTONBG);
  748.         tft.drawRoundRect(posX, posY, 35, 35, 5, BUTTONEDGE);
  749.         tft.setCursor(posX + 6, posY + 30);
  750.         tft.setTextColor(TEXTCOLOR);
  751.         tft.setFont(&Segment22pt7b);
  752.         tft.print("+");
  753.     }
  754. }
  755. void DrawDwnBtnSm(int posX, int posY)
  756. {
  757.     if (DisplayScreen != PrevDisplayScreen) {
  758.         tft.fillRoundRect(posX, posY, 35, 35, 5, BUTTONBG);
  759.         tft.drawRoundRect(posX, posY, 35, 35, 5, BUTTONEDGE);
  760.         tft.setCursor(posX + 9, posY + 34);
  761.         tft.setTextColor(TEXTCOLOR);
  762.         tft.setFont(&Segment22pt7b);
  763.         tft.print("-");
  764.     }
  765. }
  766.  
  767. void Clock()
  768. {
  769.     currentTimeMillis = millis();
  770.     if ((M) > (159)) {
  771.         M = 100;
  772.         H += 1;
  773.     }
  774.     if ((H) > (123)) {
  775.         H = 100;
  776.     }
  777.     HH = String(H);
  778.     MM = String(M);
  779.     if ((H) > (109)) {
  780.         DisplayTime = String(HH.substring(2 - 1, 3) + String(":") + MM.substring(2 - 1, 3));
  781.     }
  782.     else {
  783.         DisplayTime = String(HH.substring(3 - 1, 3) + String(":") + MM.substring(2 - 1, 3));
  784.     }
  785.     //    DisplayTime = DD + " "+DisplayTime;
  786.     if (currentTimeMillis - previousTimeMillis > 60000) {
  787.         previousTimeMillis = previousTimeMillis + 60000;
  788.         M += 1;
  789.     }
  790. }
  791.  
  792. void DrawTime(int posX, int posY)
  793. {
  794.     Clock();
  795.     if ((DisplayTime != PrevDisplayTime) || (DisplayScreen != PrevDisplayScreen)) {
  796.         tft.fillRect(posX, posY, 100, 35, SCREENCOLOR);
  797.         tft.setCursor(posX + 7, posY + 30);
  798.         tft.setTextColor(TEXTCOLOR);
  799.         tft.setFont(&Segment22pt7b);
  800.         tft.print(DisplayTime);
  801.         PrevDisplayTime = DisplayTime;
  802.     }
  803. }
  804.  
  805. void DrawSchemaTimeH(int posX, int posY)
  806. {
  807.     String DisplM;
  808.     if ((TimeHighH != PrevTimeHighH) || (TimeHighM != PrevTimeHighM) || (DisplayScreen != PrevDisplayScreen)) {
  809.         DisplM = String(TimeHighM + 100);
  810.         DisplM = String(DisplM.substring(1, 3));
  811.         tft.fillRect(posX, posY, 95, 50, BUTTONBGACT);
  812.         tft.setCursor(posX + 4, posY + 40);
  813.         tft.setTextColor(TEXTCOLOR);
  814.         tft.setFont(&Segment18pt7b);
  815.         tft.print(TimeHighH);
  816.         tft.print(":");
  817.         tft.print(DisplM);
  818.         //        TimeHighHM = TimeHighH * 100 + TimeHighM;
  819.         PrevTimeHighH = TimeHighH;
  820.         PrevTimeHighM = TimeHighM;
  821.     }
  822. }
  823. void DrawSchemaTimeL(int posX, int posY)
  824. {
  825.     String DisplM;
  826.     if ((TimeLowH != PrevTimeLowH) || (TimeLowM != PrevTimeLowM) || (DisplayScreen != PrevDisplayScreen)) {
  827.         DisplM = String(TimeLowM + 100);
  828.         DisplM = String(DisplM.substring(1, 3));
  829.         tft.fillRect(posX, posY, 95, 50, BUTTONBGACT);
  830.         tft.setCursor(posX + 4, posY + 40);
  831.         tft.setTextColor(TEXTCOLOR);
  832.         tft.setFont(&Segment18pt7b);
  833.         tft.print(TimeLowH);
  834.         tft.print(":");
  835.         tft.print(DisplM);
  836.         PrevTimeLowH = TimeLowH;
  837.         PrevTimeLowM = TimeLowM;
  838.     }
  839. }
  840.  
  841. void DrawSchemaTempH(int posX, int posY)
  842. {
  843.     if ((SchemaTempH != PrevSchemaTempH) || (DisplayScreen != PrevDisplayScreen)) {
  844.         tft.fillRect(posX, posY, 65, 50, BUTTONBGACT);
  845.         tft.setCursor(posX + 2, posY + 40);
  846.         tft.setTextColor(TEXTCOLOR);
  847.         tft.setFont(&Segment18pt7b);
  848.         tft.printf("%.1f", SchemaTempH);
  849.         PrevSchemaTempH = SchemaTempH;
  850.     }
  851. }
  852.  
  853. void DrawSchemaTempL(int posX, int posY)
  854. {
  855.     if ((SchemaTempL != PrevSchemaTempL) || (DisplayScreen != PrevDisplayScreen)) {
  856.         tft.fillRect(posX, posY, 65, 50, BUTTONBGACT);
  857.         tft.setCursor(posX + 2, posY + 40);
  858.         tft.setTextColor(TEXTCOLOR);
  859.         tft.setFont(&Segment18pt7b);
  860.         tft.printf("%.1f", SchemaTempL);
  861.         PrevSchemaTempL = SchemaTempL;
  862.     }
  863. }
  864.  
  865. void DrawHumid(int posX, int posY)
  866. {
  867.     if ((Humid != PrevHumid) || (DisplayScreen != PrevDisplayScreen)) {
  868.         Humid = dht.readHumidity() - 1.4;
  869.         tft.fillRect(posX, posY, 80, 35, SCREENCOLOR);
  870.         tft.setCursor(posX + 6, posY + 22);
  871.         tft.setTextColor(TEXTCOLOR);
  872.         tft.setFont(&icons14pt7b);
  873.         tft.print(HumidIcon);
  874.         tft.setFont(&Segment10pt7b);
  875.         tft.print(" ");
  876.         tft.print(Humid);
  877.         tft.print("%");
  878.         PrevHumid = Humid;
  879.     }
  880. }
  881.  
  882. void TempUp()
  883. {
  884.     TempSet = TempSet + 0.5;
  885. }
  886.  
  887. void TempDown()
  888. {
  889.     TempSet = TempSet - 0.5;
  890. }
  891.  
  892. void DrawBackLightValue(int posX, int posY)
  893. {
  894.     if ((DimBrightness != PrevDimBrightness) || (DisplayScreen != PrevDisplayScreen)) {
  895.         int DimBrightnessPerc = float(DimBrightness) / 260 * 100;
  896.         tft.setFont(&Segment10pt7b);
  897.         tft.setCursor(posX, posY);
  898.         tft.setTextColor(TEXTCOLOR);
  899.         tft.print("Helderheid gedimd scherm");
  900.         tft.fillRoundRect(posX, posY + 4, 190, 82, 5, BUTTONBG);
  901.         //   tft.drawRoundRect(setTposX, setTposY + 4, 190, 82, 5, BUTTONEDGE);
  902.         tft.setTextColor(TEXTCOLOR);
  903.         tft.setFont(&Segment48pt7b);
  904.         tft.setCursor(posX + 10, posY + 75);
  905.         tft.print(DimBrightnessPerc);
  906.         int curposx = tft.getCursorX();
  907.         int curposy = tft.getCursorY();
  908.         tft.setCursor(curposx + 2, curposy);
  909.         tft.setFont(&Segment22pt7b);
  910.         tft.print("%");
  911.         analogWrite(D1, DimBrightness);
  912.         PrevDimBrightness = DimBrightness;
  913.     }
  914. }
  915.  
  916. void StoreBackLight()
  917. {
  918.     EEPROM.put(0, DimBrightness);
  919.     EEPROM.commit();
  920. }
  921.  
  922. void BackLightUp()
  923. {
  924.     if (DimBrightness < 255) {
  925.         DimBrightness += 10;
  926.         DrawBackLightValue(SetTemp2X, SetTemp2Y);
  927.     }
  928. }
  929.  
  930. void BackLightDwn()
  931. {
  932.     if (DimBrightness > 0) {
  933.         DimBrightness += -10;
  934.         DrawBackLightValue(SetTemp2X, SetTemp2Y);
  935.     }
  936. }
  937.  
  938. void getTemp()
  939. {
  940.     Temp = dht.readTemperature() - 1.4;
  941. }
  942.  
  943. void StoreSchema()
  944. {
  945.     EEPROM.put(EEPROMAddrTimeHigh, TimeHighHM);
  946.     EEPROM.put(EEPROMAddrTimeLow, TimeLowHM);
  947.     EEPROM.put(EEPROMAddrTempHigh, SchemaTempHint);
  948.     EEPROM.put(EEPROMAddrTempLow, SchemaTempLint);
  949.  
  950.     //    TimeHighH = (floor(float(TimeHighHM) / 100));
  951.     //    TimeHighM = TimeHighHM - (TimeHighH * 100);
  952.     EEPROM.commit();
  953.     delay(150);
  954. }
  955.  
  956. void GetSchemaTemp()
  957. {
  958.     if ((HeatingMode == 1) && (DisplayScreen == 1)) {
  959.         if (DisplayTime != PrevDisplayTime) {
  960.             EEPROMAddrTimeHigh = ((D * 42) + (3 * 16) - 12);
  961.             EEPROMAddrTimeLow = ((D * 42) + (3 * 16) - 8);
  962.             EEPROMAddrTempHigh = ((D * 42) + (3 * 16) - 4);
  963.             EEPROMAddrTempLow = ((D * 42) + (3 * 16));
  964.             EEPROM.get(EEPROMAddrTimeHigh, TimeHighHM);
  965.             EEPROM.get(EEPROMAddrTimeLow, TimeLowHM);
  966.             EEPROM.get(EEPROMAddrTempHigh, SchemaTempHint);
  967.             EEPROM.get(EEPROMAddrTempLow, SchemaTempLint);
  968.         }
  969.     }
  970. }
  971. void GetSchema()
  972. {
  973.     EEPROMAddrTimeHigh = ((SetDay * 42) + (SchemaNumber * 16) - 12);
  974.     EEPROMAddrTimeLow = ((SetDay * 42) + (SchemaNumber * 16) - 8);
  975.     EEPROMAddrTempHigh = ((SetDay * 42) + (SchemaNumber * 16) - 4);
  976.     EEPROMAddrTempLow = ((SetDay * 42) + (SchemaNumber * 16));
  977.     EEPROM.get(EEPROMAddrTimeHigh, TimeHighHM);
  978.     EEPROM.get(EEPROMAddrTimeLow, TimeLowHM);
  979.     EEPROM.get(EEPROMAddrTempHigh, SchemaTempHint);
  980.     EEPROM.get(EEPROMAddrTempLow, SchemaTempLint);
  981.     delay(10);
  982.     if ((TimeHighHM > 2359) || (TimeHighHM < 0)) {
  983.         TimeHighHM = 0;
  984.     }
  985.     if ((TimeLowHM > 2359) || (TimeLowHM < 0)) {
  986.         TimeLowHM = 0;
  987.     }
  988.     TimeHighH = (floor(float(TimeHighHM) / 100));
  989.     TimeHighM = TimeHighHM - (TimeHighH * 100);
  990.     TimeLowH = (floor(float(TimeLowHM) / 100));
  991.     TimeLowM = TimeLowHM - (TimeLowH * 100);
  992.     if ((SchemaTempHint > 240) || (SchemaTempHint < 0)) {
  993.         SchemaTempHint = 0;
  994.     }
  995.     if ((SchemaTempLint > 240) || (SchemaTempLint < 0)) {
  996.         SchemaTempLint = 0;
  997.     }
  998.     SchemaTempH = float(SchemaTempHint) / 10;
  999.     SchemaTempL = float(SchemaTempLint) / 10;
  1000. }
  1001.  
  1002. void PlayFalppyBird()
  1003. {
  1004.     int UpDown = 0;
  1005.     int GameOver = 0;
  1006.     int BirdY = 30;
  1007.     int TopY = -50;
  1008.     int BottomY = 170;
  1009.     int GameSpeed = 50;
  1010.     int BoxX = 380;
  1011.     int BoxY = 100;
  1012.     int BoxSpeed = 7;
  1013.     int GapSize = 90;
  1014.     int FBScore = 0;
  1015.     unsigned long gameMillis = millis();
  1016.     unsigned long PrevGameMillis = gameMillis;
  1017.     tft.fillScreen(FLAPPYSKY);
  1018.     tft.fillRect(0, 210, 320, 30, FLAPPYGRASS);
  1019.     tft.fillRect(200, 210, 120, 30, FLAPPYGRASS);
  1020.     tft.setCursor(205, 235);
  1021.     tft.setFont(&Segment10pt7b);
  1022.     tft.setTextColor(WHITE);
  1023.     tft.print("Score: ");
  1024.     tft.setCursor(265, 235);
  1025.     tft.print(FBScore);
  1026.  
  1027.     while (GameOver == 0) {
  1028.         gameMillis = millis();
  1029.  
  1030.         if ((ts.touched()) && (UpDown <= 15)) {
  1031.             UpDown += -5;
  1032.         }
  1033.         if ((UpDown >= -15) && (PrevGameMillis + GameSpeed < gameMillis)) {
  1034.             UpDown += 1;
  1035.         }
  1036.         BirdY += UpDown;
  1037.  
  1038.         if ((UpDown >= 5) && (PrevGameMillis + GameSpeed < gameMillis)) {
  1039.             tft.fillRect(30, BirdY - 100, 50, 100, FLAPPYSKY);
  1040.             tft.drawRGBBitmap(30, BirdY, flappyBird2, 50, 50);
  1041.             if (BirdY > 100) {
  1042.                 tft.fillRect(30, 210, 50, 30, FLAPPYGRASS);
  1043.             }
  1044.         }
  1045.         else if ((UpDown < 5) && (UpDown > -5) && (PrevGameMillis + GameSpeed < gameMillis)) {
  1046.             tft.fillRect(30, BirdY - 6, 50, 6, FLAPPYSKY);
  1047.             tft.fillRect(30, BirdY + 56, 50, 6, FLAPPYSKY);
  1048.             tft.drawRGBBitmap(30, BirdY, flappyBird1, 50, 50);
  1049.             if (BirdY > 100) {
  1050.                 tft.fillRect(30, 210, 50, 30, FLAPPYGRASS);
  1051.             }
  1052.         }
  1053.         else {
  1054.             tft.fillRect(30, BirdY + 50, 50, 50, FLAPPYSKY);
  1055.             tft.drawRGBBitmap(30, BirdY, flappyBird3, 50, 50);
  1056.             if (BirdY > 100) {
  1057.                 tft.fillRect(30, 210, 50, 30, FLAPPYGRASS);
  1058.             }
  1059.         }
  1060.         if (PrevGameMillis + GameSpeed < gameMillis) {
  1061.             //onderbox
  1062.             tft.fillRect(BoxX, BoxY, 50, 210 - BoxY, FLAPPYBOX);
  1063.             tft.fillRect(BoxX + 50, BoxY, BoxSpeed * 2, 210 - BoxY, FLAPPYSKY);
  1064.             //bovenbox
  1065.             tft.fillRect(BoxX, 0, 50, BoxY - GapSize, FLAPPYBOX);
  1066.             tft.fillRect(BoxX + 50, 0, BoxSpeed * 2, BoxY - GapSize, FLAPPYSKY);
  1067.             BoxX = BoxX - BoxSpeed;
  1068.             BoxX = BoxX - BoxSpeed;
  1069.             if (BoxX < -100) {
  1070.                 BoxX = 380;
  1071.                 BoxY = random(80, 160 + 1);
  1072.                 BoxSpeed += 1;
  1073.                 FBScore += 5;
  1074.                 tft.fillRect(260, 210, 60, 30, FLAPPYGRASS);
  1075.                 tft.setFont(&Segment10pt7b);
  1076.                 tft.setTextColor(WHITE);
  1077.                 tft.setCursor(265, 235);
  1078.                 tft.print(FBScore);
  1079.             }
  1080.         }
  1081.         if ((BirdY > BottomY) || (BirdY < TopY) || (((BirdY + 40 > BoxY) || (BirdY + 10 + GapSize < BoxY)) && (BoxX > 0) && (BoxX < 50))) {
  1082.             tft.setCursor(50, 130);
  1083.             tft.setFont(&Segment22pt7b);
  1084.             tft.setTextColor(WHITE);
  1085.             tft.print("Game Over");
  1086.             delay(5000);
  1087.             previousScreenTimeOutMillis = millis();
  1088.             GameOver = 1;
  1089.         }
  1090.  
  1091.         if (PrevGameMillis + GameSpeed < gameMillis) {
  1092.             PrevGameMillis = gameMillis;
  1093.         }
  1094.     }
  1095. }
  1096. void MQTT_connect()
  1097. {
  1098.     int8_t ret;
  1099.  
  1100.     // Stop if already connected.
  1101.     if (mqtt.connected()) {
  1102.         return;
  1103.     }
  1104.  
  1105.     Serial.print("Connecting to MQTT... ");
  1106.  
  1107.     uint8_t retries = 3;
  1108.     while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
  1109.         Serial.println(mqtt.connectErrorString(ret));
  1110.         Serial.println("Retrying MQTT connection in 5 seconds...");
  1111.         mqtt.disconnect();
  1112.         delay(5000); // wait 5 seconds
  1113.         retries--;
  1114.         if (retries == 0) {
  1115.             // basically die and wait for WDT to reset me
  1116.             while (1)
  1117.                 ;
  1118.         }
  1119.     }
  1120.     Serial.println("MQTT Connected!");
  1121. }
  1122.  
  1123. void loop()
  1124. {
  1125.     MQTT_connect();
  1126.     /*      Adafruit_MQTT_Subscribe *subscription;
  1127.   while ((subscription = mqtt.readSubscription(5000))) {
  1128.       if (subscription == &ToggleSwitch1Status) {
  1129.       Serial.print(F("Got: "));
  1130.       Serial.println((char *)ToggleSwitch1Status.lastread);
  1131.     }
  1132.   }
  1133. */
  1134.     RecTime();
  1135.     GetTime();
  1136.     GetSetTemp();
  1137.     ToggleHeating();
  1138.     GetWifiStatus();
  1139.     ScreenTimeOut();
  1140.     if (DisplayScreen != PrevDisplayScreen) {
  1141.         tft.fillScreen(SCREENCOLOR);
  1142.     }
  1143.  
  1144.     unsigned long currentMillis = millis();
  1145.     unsigned long previousMillis;
  1146.     if (DisplayScreen == 10) {
  1147.         if (DisplayScreen != PrevDisplayScreen) {
  1148.             DrawMenu();
  1149.             PrevDisplayScreen = 10;
  1150.         }
  1151.     }
  1152.  
  1153.     else if (DisplayScreen == 11) {
  1154.         if (DisplayScreen != PrevDisplayScreen) {
  1155.             DrawMenu2();
  1156.             PrevDisplayScreen = 11;
  1157.         }
  1158.     }
  1159.  
  1160.     else if (DisplayScreen == 1) {
  1161.         GetWifiStatus();
  1162.         DrawWifiStatus(WifiPosX, WifiPosY);
  1163.         DrawMenuBtn(MenuBtnX, MenuBtnY);
  1164.         DrawCurTemp(CurTempX, CurTempY);
  1165.         DrawSetTemp(SetTempX, SetTempY);
  1166.         //         DrawUpBtn(UpBtnX,UpBtnY);
  1167.         //         DrawDwnBtn(DwnBtnX,DwnBtnY);
  1168.         DrawToggle1(Toggle1X, Toggle1Y);
  1169.         DrawToggle2(Toggle2X, Toggle2Y);
  1170.         DrawTime(TimeDisplX, TimeDisplY);
  1171.         DrawHumid(HumidX, HumidY);
  1172.         DrawHeatState(HeatStateX, HeatStateY);
  1173.         DrawHeatingMode(HeatModeX, HeatModeY);
  1174.         PrevDisplayScreen = 1;
  1175.     }
  1176.  
  1177.     else if (DisplayScreen == 2) {
  1178.         GetWifiStatus();
  1179.         DrawWifiStatus(WifiPosX, WifiPosY);
  1180.         //        DrawMenuBtn(MenuBtnX, MenuBtnY);
  1181.         DrawCurTemp(CurTemp2X, CurTemp2Y);
  1182.         DrawSetTemp(SetTemp2X, SetTemp2Y);
  1183.         DrawUpBtn(UpBtnX, UpBtnY);
  1184.         DrawDwnBtn(DwnBtnX, DwnBtnY);
  1185.         //         DrawToggle1(Toggle1X,Toggle1Y);
  1186.         //         DrawToggle2(Toggle2X,Toggle2Y);
  1187.         DrawTime(TimeDisplX, TimeDisplY);
  1188.         DrawHumid(HumidX, HumidY);
  1189.         DrawHeatState(HeatStateX, HeatStateY);
  1190.         PrevDisplayScreen = 2;
  1191.     }
  1192.  
  1193.     else if (DisplayScreen == 3) {
  1194.         if (SchemaNumber != PrevSchemaNumber) {
  1195.  
  1196.             tft.setCursor(60, 48);
  1197.             tft.setFont(&Segment18pt7b);
  1198.             tft.setTextColor(TEXTCOLOR);
  1199.             tft.fillRect(50, 10, 220, 50, BUTTONBGACT);
  1200.             tft.print(LongDayNames[SetDay]);
  1201.             tft.print(" ");
  1202.             tft.print(SchemaNumber);
  1203.             PrevSchemaNumber = SchemaNumber;
  1204.         }
  1205.         DrawDwnBtnSm(10, 18);
  1206.         DrawUpBtnSm(278, 18);
  1207.  
  1208.         DrawSchemaTimeH(45, 70);
  1209.         DrawDwnBtnSm(10, 80);
  1210.         DrawUpBtnSm(140, 80);
  1211.  
  1212.         DrawSchemaTempH(212, 70);
  1213.         DrawDwnBtnSm(177, 80);
  1214.         DrawUpBtnSm(278, 80);
  1215.  
  1216.         DrawSchemaTimeL(45, 140);
  1217.         DrawDwnBtnSm(10, 150);
  1218.         DrawUpBtnSm(140, 150);
  1219.  
  1220.         DrawSchemaTempL(212, 140);
  1221.         DrawDwnBtnSm(177, 150);
  1222.         DrawUpBtnSm(278, 150);
  1223.         DrawSaveBtn(MenuBtnX, MenuBtnY);
  1224.         PrevDisplayScreen = 3;
  1225.     }
  1226.  
  1227.     else if (DisplayScreen == 8) {
  1228.         GetWifiStatus();
  1229.         DrawWifiStatus(WifiPosX, WifiPosY);
  1230.         //        DrawMenuBtn(MenuBtnX, MenuBtnY);
  1231.         DrawBackLightValue(SetTemp2X, SetTemp2Y);
  1232.         DrawUpBtn(UpBtnX, UpBtnY);
  1233.         DrawDwnBtn(DwnBtnX, DwnBtnY);
  1234.         //         DrawToggle1(Toggle1X,Toggle1Y);
  1235.         //         DrawToggle2(Toggle2X,Toggle2Y);
  1236.         DrawTime(TimeDisplX, TimeDisplY);
  1237.         DrawHumid(HumidX, HumidY);
  1238.         DrawHeatState(HeatStateX, HeatStateY);
  1239.         PrevDisplayScreen = 8;
  1240.     }
  1241.     else if (DisplayScreen == 20) {
  1242.         PlayFalppyBird();
  1243.         DisplayScreen = 1;
  1244.     }
  1245.  
  1246.     //Als het scherm wordt aangeraakt
  1247.     if (ts.touched()) {
  1248.         TS_Point p = ts.getPoint();
  1249.         //Vertaal de input van het touchscreen naar coordinaten op het scherm
  1250.         p.x = map(p.x, TS_MAXX, TS_MINX, 0, tft.width());
  1251.         p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
  1252.  
  1253.         previousScreenTimeOutMillis = millis();
  1254.         if (DisplayScreen != 8) {
  1255.             digitalWrite(D1, 1);
  1256.         }
  1257.  
  1258.         delay(150);
  1259.  
  1260.         //Knoppen van de Menuopties
  1261.  
  1262.         if (DisplayScreen == 10) {
  1263.             int xpos = 5;
  1264.             int ypos = 10;
  1265.  
  1266.             if (((p.x) > (xpos)) && ((p.x) < (xpos + 150)) && ((p.y) > (ypos)) && ((p.y) < (ypos + 50))) {
  1267.                 DisplayScreen = 1;
  1268.                 HeatingMode = 1;
  1269.             }
  1270.  
  1271.             ypos += 65;
  1272.             if (((p.x) > (xpos)) && ((p.x) < (xpos + 150)) && ((p.y) > (ypos)) && ((p.y) < (ypos + 50))) {
  1273.                 DisplayScreen = 1;
  1274.                 HeatingMode = 2;
  1275.             }
  1276.  
  1277.             ypos += 65;
  1278.             if (((p.x) > (xpos)) && ((p.x) < (xpos + 150)) && ((p.y) > (ypos)) && ((p.y) < (ypos + 50))) {
  1279.                 DisplayScreen = 1;
  1280.                 HeatingMode = 3;
  1281.             }
  1282.             xpos = 165;
  1283.             ypos = 10;
  1284.  
  1285.             if (((p.x) > (xpos)) && ((p.x) < (xpos + 150)) && ((p.y) > (ypos)) && ((p.y) < (ypos + 50))) {
  1286.                 DisplayScreen = 3;
  1287.                 PrevSetDay = 9;
  1288.                 SetDay = 1;
  1289.                 PrevSchemaNumber = 4;
  1290.                 SchemaNumber = 1;
  1291.                 GetSchema();
  1292.             }
  1293.  
  1294.             ypos += 65;
  1295.             if (((p.x) > (xpos)) && ((p.x) < (xpos + 150)) && ((p.y) > (ypos)) && ((p.y) < (ypos + 50))) {
  1296.                 DisplayScreen = 1;
  1297.             }
  1298.  
  1299.             ypos += 65;
  1300.             if (((p.x) > (xpos)) && ((p.x) < (xpos + 150)) && ((p.y) > (ypos)) && ((p.y) < (ypos + 50))) {
  1301.                 DisplayScreen = 1;
  1302.             }
  1303.  
  1304.             if (((p.x) > (MenuBtnX)) && ((p.x) < (MenuBtnX + 50)) && ((p.y) > (MenuBtnY)) && ((p.y) < (MenuBtnY + 50))) {
  1305.                 DisplayScreen = 11;
  1306.             }
  1307.         }
  1308.         else if (DisplayScreen == 11) {
  1309.             int xpos = 5;
  1310.             int ypos = 10;
  1311.  
  1312.             if (((p.x) > (xpos)) && ((p.x) < (xpos + 150)) && ((p.y) > (ypos)) && ((p.y) < (ypos + 50))) {
  1313.                 DisplayScreen = 1;
  1314.             }
  1315.  
  1316.             ypos += 65;
  1317.             if (((p.x) > (xpos)) && ((p.x) < (xpos + 150)) && ((p.y) > (ypos)) && ((p.y) < (ypos + 50))) {
  1318.                 DisplayScreen = 8;
  1319.             }
  1320.             xpos = 165;
  1321.             ypos = 10;
  1322.  
  1323.             ypos += 65;
  1324.             if (((p.x) > (xpos)) && ((p.x) < (xpos + 150)) && ((p.y) > (ypos)) && ((p.y) < (ypos + 50))) {
  1325.                 DisplayScreen = 3;
  1326.             }
  1327.  
  1328.             if (((p.x) > (xpos)) && ((p.x) < (xpos + 150)) && ((p.y) > (ypos)) && ((p.y) < (ypos + 50))) {
  1329.                 DisplayScreen = 20;
  1330.             }
  1331.  
  1332.             if (((p.x) > (MenuBtnX)) && ((p.x) < (MenuBtnX + 50)) && ((p.y) > (MenuBtnY)) && ((p.y) < (MenuBtnY + 50))) {
  1333.                 DisplayScreen = 10;
  1334.             }
  1335.         }
  1336.  
  1337.         //Startscherm met temperatuur en Toggle knoppen
  1338.         else if (DisplayScreen == 1) {
  1339.  
  1340.             if (((p.x) > (SetTempX)) && ((p.x) < (SetTempX + 200)) && ((p.y) > (SetTempY)) && ((p.y) < (SetTempY + 100))) {
  1341.                 DisplayScreen = 2;
  1342.             }
  1343.  
  1344.             if (((p.x) > (Toggle1X)) && ((p.x) < (Toggle1X + 80)) && ((p.y) > (Toggle1Y)) && ((p.y) < (Toggle1Y + 80))) {
  1345.                 SetToggle1();
  1346.             }
  1347.             if (((p.x) > (Toggle2X)) && ((p.x) < (Toggle2X + 80)) && ((p.y) > (Toggle2Y)) && ((p.y) < (Toggle2Y + 80))) {
  1348.                 SetToggle2();
  1349.             }
  1350.  
  1351.             if (((p.x) > (MenuBtnX)) && ((p.x) < (MenuBtnX + 50)) && ((p.y) > (MenuBtnY)) && ((p.y) < (MenuBtnY + 50))) {
  1352.                 DisplayScreen = 10;
  1353.             }
  1354.         }
  1355.  
  1356.         //Knoppen scherm 2
  1357.         else if (DisplayScreen == 2) {
  1358.             //Temp omhoog knop wordt aangeraakt
  1359.             if (((p.x) > (SetTemp2X)) && ((p.x) < (SetTemp2X + 200)) && ((p.y) > (SetTemp2Y + 20)) && ((p.y) < (SetTemp2Y + 100))) {
  1360.                 DisplayScreen = 1;
  1361.             }
  1362.  
  1363.             if (((p.x) > (UpBtnX)) && ((p.x) < (UpBtnX + 50)) && ((p.y) > (UpBtnY)) && ((p.y) < (UpBtnY + 50))) {
  1364.                 TempUp();
  1365.                 DrawSetTemp(SetTemp2X, SetTemp2Y);
  1366.                 DrawHeatState(HeatStateX, HeatStateY);
  1367.             }
  1368.  
  1369.             //Temp omlaag knop wordt aangeraakt
  1370.             if (((p.x) > (DwnBtnX)) && ((p.x) < (DwnBtnX + 50)) && ((p.y) > (DwnBtnY)) && ((p.y) < (DwnBtnY + 50))) {
  1371.                 TempDown();
  1372.                 DrawSetTemp(SetTemp2X, SetTemp2Y);
  1373.                 DrawHeatState(HeatStateX, HeatStateY);
  1374.             }
  1375.         }
  1376.         //Schema instelscherm
  1377.         else if (DisplayScreen == 3) {
  1378.             //DagSchema's + en -
  1379.             // Schema vooruit
  1380.             if (((p.x) > (260)) && ((p.x) < (310)) && ((p.y) > (10)) && ((p.y) < (60))) {
  1381.                 // delay(10);
  1382.                 StoreSchema();
  1383.  
  1384.                 if (SchemaNumber >= 3) {
  1385.                     SetDay += 1;
  1386.                     SchemaNumber = 1;
  1387.                 }
  1388.                 else {
  1389.                     SchemaNumber += 1;
  1390.                 }
  1391.                 if (SetDay > 6) {
  1392.                     SetDay = 0;
  1393.                 }
  1394.                 GetSchema();
  1395.             }
  1396.  
  1397.             // Schema achteruit
  1398.             if (((p.x) > (10)) && ((p.x) < (60)) && ((p.y) > (10)) && ((p.y) < (60))) {
  1399.                 StoreSchema();
  1400.                 if (SchemaNumber <= 1) {
  1401.                     SetDay += -1;
  1402.                     SchemaNumber = 3;
  1403.                 }
  1404.                 else {
  1405.                     SchemaNumber += -1;
  1406.                 }
  1407.                 if (SetDay < 0) {
  1408.                     SetDay = 6;
  1409.                 }
  1410.                 GetSchema();
  1411.             }
  1412.  
  1413.             // Tijd1 omhoog
  1414.             if (((p.x) > (130)) && ((p.x) < (170)) && ((p.y) > (75)) && ((p.y) < (120))) {
  1415.                 if (TimeHighM >= 45) {
  1416.                     TimeHighH += 1;
  1417.                     TimeHighM = 0;
  1418.                 }
  1419.                 else {
  1420.                     TimeHighM += 15;
  1421.                 }
  1422.                 if (TimeHighH > 23) {
  1423.                     TimeHighH = 0;
  1424.                 }
  1425.                 TimeHighHM = TimeHighH * 100 + TimeHighM;
  1426.             }
  1427.             // Tijd1 omlaag
  1428.             if (((p.x) > (0)) && ((p.x) < (95)) && ((p.y) > (75)) && ((p.y) < (120))) {
  1429.                 if (TimeHighM <= 0) {
  1430.                     TimeHighH += -1;
  1431.                     TimeHighM = 45;
  1432.                 }
  1433.                 else {
  1434.                     TimeHighM += -15;
  1435.                 }
  1436.                 if (TimeHighH > 23) {
  1437.                     TimeHighH = 0;
  1438.                 }
  1439.                 TimeHighHM = TimeHighH * 100 + TimeHighM;
  1440.             }
  1441.  
  1442.             // Temp1 omhoog
  1443.             if (((p.x) > (270)) && ((p.x) < (320)) && ((p.y) > (75)) && ((p.y) < (120))) {
  1444.                 SchemaTempH += 0.5;
  1445.                 SchemaTempHint = int(SchemaTempH * 10);
  1446.             }
  1447.             // Temp1 omlaag
  1448.             if (((p.x) > (170)) && ((p.x) < (210)) && ((p.y) > (75)) && ((p.y) < (120))) {
  1449.                 SchemaTempH += -0.5;
  1450.                 SchemaTempHint = int(SchemaTempH * 10);
  1451.             }
  1452.             // Temp2 omhoog
  1453.             if (((p.x) > (270)) && ((p.x) < (320)) && ((p.y) > (140)) && ((p.y) < (180))) {
  1454.                 SchemaTempL += 0.5;
  1455.                 SchemaTempLint = int(SchemaTempL * 10);
  1456.             }
  1457.             // Temp2 omlaag
  1458.             if (((p.x) > (170)) && ((p.x) < (210)) && ((p.y) > (140)) && ((p.y) < (180))) {
  1459.                 SchemaTempL += -0.5;
  1460.                 SchemaTempLint = int(SchemaTempL * 10);
  1461.             }
  1462.  
  1463.             // Tijd2 omhoog
  1464.             if (((p.x) > (130)) && ((p.x) < (170)) && ((p.y) > (140)) && ((p.y) < (180))) {
  1465.                 if (TimeLowM >= 45) {
  1466.                     TimeLowH += 1;
  1467.                     TimeLowM = 0;
  1468.                 }
  1469.                 else {
  1470.                     TimeLowM += 15;
  1471.                 }
  1472.                 if (TimeLowH > 23) {
  1473.                     TimeLowH = 0;
  1474.                 }
  1475.                 TimeLowHM = TimeLowH * 100 + TimeLowM;
  1476.             }
  1477.             // Tijd2 omlaag
  1478.             if (((p.x) > (0)) && ((p.x) < (95)) && ((p.y) > (140)) && ((p.y) < (180))) {
  1479.                 if (TimeLowM <= 0) {
  1480.                     TimeLowH += -1;
  1481.                     TimeLowM = 45;
  1482.                 }
  1483.                 else {
  1484.                     TimeLowM += -15;
  1485.                 }
  1486.                 if (TimeLowH > 23) {
  1487.                     TimeLowH = 0;
  1488.                 }
  1489.                 TimeLowHM = TimeLowH * 100 + TimeLowM;
  1490.             }
  1491.  
  1492.             if (((p.x) > (MenuBtnX)) && ((p.x) < (MenuBtnX + 50)) && ((p.y) > (MenuBtnY)) && ((p.y) < (MenuBtnY + 50))) {
  1493.                 DisplayScreen = 10;
  1494.             }
  1495.         }
  1496.         else if (DisplayScreen == 8) {
  1497.             //BackLightWaarde wordt aangeraakt
  1498.             if (((p.x) > (SetTemp2X + 20)) && ((p.x) < (SetTemp2X + 200)) && ((p.y) > (SetTemp2Y)) && ((p.y) < (SetTemp2Y + 100))) {
  1499.                 StoreBackLight();
  1500.                 digitalWrite(D1, 1);
  1501.                 DisplayScreen = 1;
  1502.             }
  1503.  
  1504.             if (((p.x) > (UpBtnX)) && ((p.x) < (UpBtnX + 50)) && ((p.y) > (UpBtnY)) && ((p.y) < (UpBtnY + 50))) {
  1505.                 BackLightUp();
  1506.             }
  1507.  
  1508.             //Temp omlaag knop wordt aangeraakt
  1509.             if (((p.x) > (DwnBtnX)) && ((p.x) < (DwnBtnX + 50)) && ((p.y) > (DwnBtnY)) && ((p.y) < (DwnBtnY + 50))) {
  1510.                 BackLightDwn();
  1511.             }
  1512.         }
  1513.  
  1514.         else if (DisplayScreen == -1) {
  1515.             DisplayScreen = 1;
  1516.             PrevDisplayScreen = -1;
  1517.         }
  1518.  
  1519.         Serial.print("(");
  1520.         Serial.print(p.x);
  1521.         Serial.print(", ");
  1522.         Serial.print(p.y);
  1523.         Serial.println(")");
  1524.     }
  1525. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement