Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //**************************************************************************************
- // Code voor een wifi thermostaat met een DHT sensor (11 of 22) en touchscreen TFT **
- //**************************************************************************************
- //ESPWebtime code voor het ophalen van tijd via
- #include <Adafruit_GFX.h> // Core graphics library
- #include <Adafruit_TFTLCD.h> // Hardware-specifieke library
- #include <TouchScreen.h>
- #include <DHT.h> // DHT sensor (temp en luchtvochtigheid)
- // Fonts
- #include <Fonts/FreeMonoBold18pt7b.h>
- //#include <Fonts/FreeMonoBold12pt7b.h>
- #include <Fonts/FreeMono9pt7b.h>
- #include <Fonts/Segment10pt7b.h>
- #include <Fonts/Segment22pt7b.h>
- //DHT sensor
- #define DHTPIN 53
- #define DHTTYPE DHT11
- //TFT display en touch
- #if defined(__SAM3X8E__)
- #undef __FlashStringHelper::F(string_literal)
- #define F(string_literal) string_literal
- #endif
- #define YP A3
- #define XM A2
- #define YM 9
- #define XP 8
- #define TS_MINX 126
- #define TS_MAXX 902
- #define TS_MINY 78
- #define TS_MAXY 902
- TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
- #define LCD_CS A3
- #define LCD_CD A2
- #define LCD_WR A1
- #define LCD_RD A0
- #define LCD_RESET A4
- #include <MCUFRIEND_kbv.h>
- MCUFRIEND_kbv tft;
- //Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
- DHT dht(DHTPIN, DHTTYPE);
- // 16-bit kleuren definities: https://ee-programming-notepad.blogspot.com/2016/10/16-bit-color-generator-picker.html
- #define BLACK 0x0000
- #define LIGHT 0xF7DE
- #define MEDIUM 0x9532
- #define DARK 0x6BAD
- #define WHITE 0xFFFF
- #define ACCENT 0x53C9
- #define RED 0xD1E3
- //Drukbereik touchscreen
- #define MINPRESSURE 10
- #define MAXPRESSURE 1000
- // Variabelen
- float Tset = 19; //Ingestelde temperatuur
- long previousRefrMillis = 0; // ten behoeve van de schermrefresh
- unsigned long currentRefrMillis = millis();
- long Refrinterval = 30000; // Update elke halve minuut
- long previousTimeMillis = 0; // ten behoeve van de klok
- unsigned long currentTimeMillis = millis();
- long Timeinterval = 60000; // Update elke minuut
- int H = 112; // Uren binnen honderd (om de eerste 1 er weer vanaf te kunnen knippen)
- int M = 100; // Minuten
- String HH;
- String MM;
- String DisplayTime;
- float t = dht.readTemperature();
- int humid = dht.readHumidity();
- String RecieveFromSerial;
- String TimePartH;
- String TimePartM;
- int power1 = 0;
- int power2 = 0;
- int MenuOption = 1;
- int OldMenuOption;
- int MenuTouch = 0;
- //schermposities van de onderdelen
- int MenuposX = 20;
- int MenuposY = 10;
- int curTposX = 20;
- int curTposY = 55;
- String TitleCurT = "Huidig";
- int setTposX = 165;
- int setTposY = 55;
- String TitleSetT = "Doel";
- int UpBtnX = 225;
- int UpBtnY = 120;
- int DwnBtnX = 165;
- int DwnBtnY = 120;
- int TimeDisplX = 120;
- int TimeDisplY = 190;
- int HumDisplX = 5;
- int HumDisplY = 190;
- String TitleHumid = "lv:";
- int Power1X = 0;
- int Power2X = 0;
- void setup() {
- Serial.begin(9600);
- Serial3.begin(9600);
- dht.begin();
- tft.reset();
- initDisplay();
- tft.fillScreen(WHITE);
- MenuBtn();
- writeTemp();
- writeTempSet();
- UpBtn();
- DwnBtn();
- writeTime();
- writeHumid();
- if (t<Tset){
- VerwarmingAan();
- } else
- { VerwarmingUit();}
- }
- void RecTime()
- {
- if((Serial.read()) > (0)){
- RecieveFromSerial= String(Serial.readStringUntil('\n'));
- if((( RecieveFromSerial.substring(1, 1))==('T'))){
- TimePartH= RecieveFromSerial.substring(0, 2);
- TimePartM= RecieveFromSerial.substring(2, 4);
- H = TimePartH.toInt()+100;
- M = TimePartM.toInt()+100;
- writeTime();
- }
- }
- if((Serial3.read()) > (0)){
- RecieveFromSerial= String(Serial3.readStringUntil('\n'));
- if((( RecieveFromSerial.substring(1, 1))==('T'))){
- TimePartH= RecieveFromSerial.substring(0, 2);
- TimePartM= RecieveFromSerial.substring(2, 4);
- H = TimePartH.toInt()+100;
- M = TimePartM.toInt()+100;
- writeTime();
- }
- }
- }
- void initDisplay(){
- uint16_t identifier = tft.readID();
- if(identifier == 0x9325) {
- Serial.println(F("ILI9325 LCD driver"));
- } else if(identifier == 0x9328) {
- Serial.println(F("ILI9328 LCD driver"));
- } else if(identifier == 0x4535) {
- Serial.println(F("LGDP4535 LCD driver"));
- }else if(identifier == 0x7575) {
- Serial.println(F("HX8347G LCD driver"));
- } else if(identifier == 0x9341) {
- Serial.println(F("ILI9341 LCD driver"));
- }else if(identifier == 0x7783) {
- Serial.println(F("ST7781 LCD driver"));
- }else if(identifier == 0x8230) {
- Serial.println(F("UC8230 LCD driver"));
- }else if(identifier == 0x8357) {
- Serial.println(F("HX8357D LCD driver"));
- }else if(identifier==0x0101) {
- identifier=0x9341;
- Serial.println(F("0x9341 LCD driver"));
- }else {
- Serial.print(F("Onbekende LCD driver chip: "));
- Serial.println(identifier, HEX);
- identifier=0x9341;
- }
- tft.begin(identifier);
- tft.setRotation(3);
- }
- void writeTemp()
- {
- tft.setFont(&Segment10pt7b);
- tft.setCursor(curTposX,curTposY);
- tft.setTextColor(DARK);
- tft.print(TitleCurT);
- getTemp();
- char T[8];
- dtostrf(t,6,1,T);
- tft.fillRoundRect(curTposX, curTposY+4, 110, 45, 5, LIGHT);
- tft.drawRoundRect(curTposX, curTposY+4, 110, 45, 5, MEDIUM);
- tft.setTextColor(DARK);
- tft.setFont(&Segment22pt7b);
- tft.setCursor(curTposX-25, curTposY+42);
- tft.print(T);
- int posx = tft.getCursorX();
- int posy = tft.getCursorY();
- tft.setCursor(posx+2,posy-16);
- tft.setFont(&Segment10pt7b);
- tft.print("o");
- }
- void writeTempSet()
- {
- char TsetT[8];
- dtostrf(Tset,6,1,TsetT);
- tft.setFont(&Segment10pt7b);
- tft.setCursor(setTposX,setTposY);
- tft.setTextColor(DARK);
- tft.print(TitleSetT);
- tft.fillRoundRect(setTposX, setTposY+4, 110, 45, 5, LIGHT);
- tft.drawRoundRect(setTposX, setTposY+4, 110, 45, 5, MEDIUM);
- tft.setTextColor(DARK);
- tft.setFont(&Segment22pt7b);
- tft.setCursor(setTposX-25, setTposY+42);
- tft.print(TsetT);
- int posx = tft.getCursorX();
- int posy = tft.getCursorY();
- tft.setCursor(posx+2,posy-16);
- tft.setFont(&Segment10pt7b);
- tft.print("o");
- }
- void MenuBtn()
- {
- tft.fillRoundRect(MenuposX, MenuposY, 35, 22, 5, LIGHT);
- tft.drawRoundRect(MenuposX, MenuposY, 35, 22, 5, MEDIUM);
- tft.setCursor(MenuposX+5,MenuposY+8);
- tft.setTextColor(DARK);
- tft.setFont(&Segment22pt7b);
- tft.print("...");
- }
- void DrawMenu()
- {
- tft.fillScreen(WHITE);
- MenuBtn();
- writeTime();
- }
- void UpBtn()
- {
- tft.fillRoundRect(UpBtnX, UpBtnY, 50, 50, 5, LIGHT);
- tft.drawRoundRect(UpBtnX, UpBtnY, 50, 50, 5, MEDIUM);
- tft.fillTriangle(UpBtnX+14,UpBtnY+36,UpBtnX+36,UpBtnY+36,UpBtnX+25,UpBtnY+14,WHITE);
- tft.drawTriangle(UpBtnX+14,UpBtnY+36,UpBtnX+36,UpBtnY+36,UpBtnX+25,UpBtnY+14,MEDIUM);
- }
- void DwnBtn()
- {
- tft.fillRoundRect(DwnBtnX, DwnBtnY, 50, 50, 5, LIGHT);
- tft.drawRoundRect(DwnBtnX, DwnBtnY, 50, 50, 5, MEDIUM);
- tft.fillTriangle(DwnBtnX+14,DwnBtnY+14,DwnBtnX+36,DwnBtnY+14,DwnBtnX+25,DwnBtnY+36,WHITE);
- tft.drawTriangle(DwnBtnX+14,DwnBtnY+14,DwnBtnX+36,DwnBtnY+14,DwnBtnX+25,DwnBtnY+36,MEDIUM);
- }
- //Deze klok gaat ervan uit dat hij 1 keer per minuut wordt aangeroepen
- void Clock()
- {
- if((M) > (159)){
- M = 100;
- H += 1;
- }
- if((H) > (123)){
- H = 100;
- }
- HH= String(H);
- MM= String(M);
- if((H) > (109)){
- DisplayTime= String( HH.substring(2-1, 3)+String(":")+ MM.substring(2-1, 3));
- }else{
- DisplayTime= String( HH.substring(3-1, 3)+String(":")+ MM.substring(2-1, 3));
- }
- currentTimeMillis = millis();
- if(currentTimeMillis - previousTimeMillis > Timeinterval) {
- previousTimeMillis = currentTimeMillis;
- M += 1;}
- }
- void writeTime()
- {
- Clock();
- tft.fillRect(TimeDisplX, TimeDisplY, 80, 50, WHITE);
- tft.setCursor(TimeDisplX+20,TimeDisplY+30);
- //tft.print (H);
- //tft.print (":");
- //tft.print (M);
- tft.setTextColor(DARK);
- tft.setFont(&Segment10pt7b);
- tft.print (DisplayTime);
- }
- void writeHumid()
- {
- humid = dht.readHumidity()-1.4;
- tft.fillRect(HumDisplX, HumDisplY, 80, 50, WHITE);
- tft.setCursor(HumDisplX+20,HumDisplY+30);
- //tft.print (H);
- //tft.print (":");
- //tft.print (M);
- tft.setTextColor(DARK);
- tft.setFont(&Segment10pt7b);
- tft.print (TitleHumid);
- tft.print (" ");
- tft.print (humid);
- tft.print ("%");
- }
- void Tup()
- {
- Tset = Tset+0.5;
- }
- void Tdown()
- {
- Tset = Tset-0.5;
- }
- void VerwarmingAan()
- {
- tft.fillCircle(290,220,7,RED);
- }
- void VerwarmingUit()
- {
- tft.fillCircle(290,220,7,WHITE);
- tft.drawCircle(290,220,7,RED);
- }
- void getTemp(){
- t = dht.readTemperature()-1.4;
- }
- void loop()
- {
- RecTime();
- digitalWrite(13, HIGH);
- TSPoint p = ts.getPoint();
- digitalWrite(13, LOW);
- pinMode(XM, OUTPUT);
- pinMode(YP, OUTPUT);
- currentRefrMillis = millis();
- if((currentRefrMillis - previousRefrMillis > Refrinterval) || (MenuTouch == 1)) {
- if (MenuOption = 0)
- {
- DrawMenu();
- }
- if (MenuOption = 1)
- {
- getTemp();
- if (t<Tset){
- VerwarmingAan();
- } else
- { VerwarmingUit();}
- writeTemp();
- writeTime();
- writeHumid();
- }
- if(currentRefrMillis - previousRefrMillis > Refrinterval){
- previousRefrMillis = currentRefrMillis;
- }
- if(MenuTouch = 1){
- MenuTouch = 0;
- }
- }]4e
- //Als het scherm wordt aangeraakt
- if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
- //Vertaal de input van het touchscreen naar coordinaten op het scherm
- p.x = map(p.x, TS_MINX, TS_MAXX, tft.height(), 0);
- p.y = map(p.y, TS_MINY, TS_MAXY, tft.width(), 0);
- //Menu knop wordt aangeraakt
- if (((p.x) > (MenuposY)) && ((p.x) < (MenuposY+50)) && ((p.y) > (MenuposX)) && ((p.y) < (MenuposX+50))){
- MenuTouch = 1;
- if (MenuOption > 0){
- OldMenuOption = MenuOption;
- MenuOption = 0;
- }
- else {
- {MenuOption = OldMenuOption;
- }}
- }
- //Temp omhoog knop wordt aangeraakt
- if (((p.x) > (UpBtnY)) && ((p.x) < (UpBtnY+50)) && ((p.y) > (UpBtnX)) && ((p.y) < (UpBtnX+50)) ){
- Tup();
- writeTempSet();
- if (t<Tset){
- VerwarmingAan();
- } else
- { VerwarmingUit();}
- }
- //Temp omlaag knop wordt aangeraakt
- if (((p.x) > (DwnBtnY)) && ((p.x) < (DwnBtnY+50)) && ((p.y) > (DwnBtnX)) && ((p.y) < (DwnBtnX+50)) ){
- Tdown();
- writeTempSet();
- if (t<Tset){
- VerwarmingAan();
- } else
- { VerwarmingUit();}
- }
- //Serial.print("("); Serial.print(p.x);
- //Serial.print(", "); Serial.print(p.y);
- //Serial.println(")");
- }
- }
Add Comment
Please, Sign In to add comment