Advertisement
LeventeDaradici

ESP32 8048S070C - HomeAssistant Energy Dashboard - Huawei Solar

Jul 14th, 2024 (edited)
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 28.01 KB | Source Code | 0 0
  1. //
  2. //     A video of this project on youtube: https://youtu.be/iKTeAz46-Ds
  3. //
  4. // Board Manager --> ESP32 ver.3.02
  5. //                   Arduino ESP 32 Boards 2.0.13  
  6. #include <Arduino_GFX_Library.h>        //ver. 1.4.7
  7. #include <Adafruit_GFX.h>               //ver. 1.11.9
  8. #include <Fonts/FreeSans12pt7b.h>
  9. #include <Fonts/FreeSans18pt7b.h>
  10. #include <Fonts/FreeSans24pt7b.h>
  11. #include <Fonts/FreeSans9pt7b.h>
  12. #include <SPI.h>
  13. #include <ArduinoJson.h>
  14. #include <WiFi.h>
  15. #include <HTTPClient.h>
  16. #include "time.h"
  17.  
  18. #define ESP32_8048S070
  19.  
  20. #if defined(ESP32_8048S070)
  21. #define TFT_BL 2
  22. Arduino_ESP32RGBPanel* bus = new Arduino_ESP32RGBPanel(
  23.   GFX_NOT_DEFINED /* CS */, GFX_NOT_DEFINED /* SCK */, GFX_NOT_DEFINED /* SDA */,
  24.   41 /* DE */, 40 /* VSYNC */, 39 /* HSYNC */, 42 /* PCLK */,
  25.   14 /* R0 */, 21 /* R1 */, 47 /* R2 */, 48 /* R3 */, 45 /* R4 */,
  26.   9 /* G0 */, 46 /* G1 */, 3 /* G2 */, 8 /* G3 */, 16 /* G4 */, 1 /* G5 */,
  27.   15 /* B0 */, 7 /* B1 */, 6 /* B2 */, 5 /* B3 */, 4 /* B4 */
  28. );
  29.  
  30. Arduino_RPi_DPI_RGBPanel* gfx = new Arduino_RPi_DPI_RGBPanel(
  31.   bus,
  32.   800 /* width */, 0 /* hsync_polarity */, 210 /* hsync_front_porch */, 30 /* hsync_pulse_width */, 16 /* hsync_back_porch */,
  33.   480 /* height */, 0 /* vsync_polarity */, 22 /* vsync_front_porch */, 13 /* vsync_pulse_width */, 10 /* vsync_back_porch */,
  34.   1 /* pclk_active_neg */, 16000000 /* prefer_speed */, true /* auto_flush */
  35. );
  36. #endif
  37.  
  38. const char* ssid = "WIFI SSID";   // Your Wifi network name
  39. const char* password = "*WIFI PASSWORD";  // Your Wifi network password
  40. //                 Your Home Assistant server IP        Your Home Assistant Sensor    
  41. const char* haApiUrl = "http://192.168.xxx.xxx:8123/api/states/sensor.xxxxxxxxxx";
  42. const char* haApiUrl2 = "http://192.168.xxx.xxx:8123/api/states/sensor.xxxxxxxxxx";
  43. const char* haApiUrl3 = "http://192.168.xxx.xxx:8123/api/states/sensor.xxxxxxxxxx";
  44. const char* haApiUrl4 = "http://192.168.xxx.xxx:8123/api/states/sensor.xxxxxxxxxx";
  45. const char* haApiUrl5 = "http://192.168.xxx.xxx:8123/api/states/sensor.xxxxxxxxxx";
  46. const char* haApiUrl6 = "http://192.168.xxx.xxx:8123/api/states/sensor.xxxxxxxxxx";
  47. // Example:               I used these sensors, with these links.
  48. //const char* haApiUrl = "http://192.168.0.103:8123/api/states/sensor.power_meter_active_power";
  49. //const char* haApiUrl2 = "http://192.168.0.103:8123/api/states/sensor.inverter_active_power";
  50. //const char* haApiUrl3 = "http://192.168.0.103:8123/api/states/sensor.difference_power_entities";
  51. //const char* haApiUrl4 = "http://192.168.0.103:8123/api/states/sensor.inverter_daily_yield";
  52. //const char* haApiUrl5 = "http://192.168.0.103:8123/api/states/sensor.inverter_internal_temperature";
  53. //const char* haApiUrl6 = "http://192.168.0.103:8123/api/states/sensor.power_meter_tensiune";
  54.  
  55. const char* haApiToken = "xxxxxxxxxxxxxxxxxxxx";   //Your Home Assistant token, details in this video: https://youtu.be/p7ZOHrjYuNU
  56.  
  57. int activePower;
  58. int solarPower;
  59. int houseConsumption;
  60. int ProdusAstazi;
  61. int Temperatura;
  62. int Tensiune;
  63.  
  64. float ProdusAstazi2;
  65. float Temperatura2;
  66. float Tensiune2;
  67. float houseConsumption2;
  68. float currentSolarPower;
  69. float currentProduction;
  70.  
  71. uint16_t mainColor = (activePower >= 0) ? GREEN : RED;
  72. uint16_t houseColor = (activePower >= 0) ? GREEN : RED;
  73. uint16_t solarColor = (solarPower > 0) ? GREEN : BLACK;
  74.  
  75. int centerX = gfx->width() / 2;
  76. int centerY = gfx->height() / 2;
  77. int radius = 50;
  78. int mainRadius = radius * 2;
  79.  
  80. unsigned long previousMillis = 0;
  81. const long interval = 15000;
  82.  
  83. unsigned long currentSageataMillis;
  84. unsigned long previousSageataMillis;
  85. int SageataInterval;
  86. int SageataX;
  87. int SageataY;
  88. int SageataDistanta;
  89. uint16_t SageataCuloare;
  90. int SageataViteza;
  91. int SageataDimensiune;
  92. int i;
  93.  
  94. unsigned long currentSageataMillis2;
  95. unsigned long previousSageataMillis2;
  96. int SageataInterval2;
  97. int SageataX2;
  98. int SageataY2;
  99. int SageataDistanta2;
  100. uint16_t SageataCuloare2;
  101. int SageataViteza2;
  102. int SageataDimensiune2;
  103. int i2;
  104.  
  105. unsigned long currentSageataMillis3;
  106. unsigned long previousSageataMillis3;
  107. int SageataInterval3;
  108. int SageataX3;
  109. int SageataY3;
  110. int SageataDistanta3;
  111. uint16_t SageataCuloare3;
  112. int SageataViteza3;
  113. int SageataDimensiune3;
  114. int i3;
  115.  
  116. unsigned long currentSageataMillis4;
  117. unsigned long previousSageataMillis4;
  118. int SageataInterval4;
  119. int SageataX4;
  120. int SageataY4;
  121. int SageataDistanta4;
  122. uint16_t SageataCuloare4;
  123. int SageataViteza4;
  124. int SageataDimensiune4;
  125. int i4;
  126.  
  127. unsigned long currentSageataMillis5;
  128. unsigned long previousSageataMillis5;
  129. int SageataInterval5;
  130. int SageataX5;
  131. int SageataY5;
  132. int SageataDistanta5;
  133. uint16_t SageataCuloare5;
  134. int SageataViteza5;
  135. int SageataDimensiune5;
  136. int i5;
  137.  
  138. int VitezaInjectie;
  139. int VitezaRetea;
  140. int VitezaConsum;
  141.  
  142. const char* ntpServer = "pool.ntp.org";
  143. const long gmtOffset_sec = 7200;
  144. const int daylightOffset_sec = 3600;
  145. unsigned long currentTimp = 0;
  146. unsigned long previousTimp = 0;
  147. int IntervalTimp = 1000;
  148.  
  149. int Cadran = 1;
  150.  
  151. int prevHour = -1;
  152. int prevMinute = -1;
  153. int prevSecond = -1;
  154.  
  155. int StergeEcranulOdata = 0;
  156.  
  157. int currentHour = 0;
  158. int currentMinute = 0;
  159. int currentSecond = 0;
  160.  
  161. void setup()
  162.    {
  163.       Serial.begin(115200);
  164.       gfx->begin();
  165.       #ifdef TFT_BL
  166.       pinMode(TFT_BL, OUTPUT);
  167.       analogWrite(TFT_BL, 100);
  168.       #endif
  169.       gfx->setTextSize(2);
  170.       gfx->fillScreen(BLACK);
  171.       gfx->setTextColor(YELLOW);
  172.       gfx->setCursor(10,110);
  173.       gfx->print("Display --> OK!");
  174.       connectToWiFi();
  175.       configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  176.       CitesteDatele();
  177.       AfiseazaDatele();
  178.    }
  179.  
  180. void loop()
  181.    {
  182.       unsigned long currentMillis = millis();
  183.       if (currentMillis - previousMillis >= interval)
  184.         {
  185.           previousMillis = currentMillis;
  186.           Cadran++;
  187.           if (Cadran > 2) Cadran = 1;
  188.           if (solarPower == 0 ) Cadran = 2;
  189.          
  190.           if (Cadran == 1)
  191.             {
  192.                FadeDown();
  193.                checkWiFiConnection();
  194.                CitesteDatele();
  195.                StergeCompletEcranul();
  196.                AfiseazaDatele();
  197.                AfiseazaOra();
  198.                FadeIn();
  199.                StergeEcranulOdata = 0;
  200.             }
  201.           if (Cadran == 2)
  202.             {  
  203.                if(solarPower > 0)
  204.                  {
  205.                     FadeDown();
  206.                  }
  207.                prevHour = -1;
  208.                prevMinute = -1;
  209.                prevSecond = -1;
  210.                checkWiFiConnection();
  211.                CitesteProductiaSolara();
  212.                if (solarPower > 0)
  213.                  {
  214.                     StergeCompletEcranul();
  215.                  } else
  216.                       {
  217.                         if (StergeEcranulOdata == 0)
  218.                            {
  219.                               StergeCompletEcranul();
  220.                               StergeEcranulOdata = 1;
  221.                            }
  222.                       }
  223.                AfiseazaDatele2();
  224.                if(solarPower > 0)
  225.                  {
  226.                     FadeIn();
  227.                  }
  228.             }
  229.         }
  230.       if (Cadran == 1)
  231.         {
  232.           AfiseazaAnimatiiCadran1();
  233.           AfiseazaOra();    
  234.         }
  235.       if (Cadran == 2)
  236.         {
  237.           AfiseazaDatele2();
  238.         }
  239.   }
  240.  
  241. void CitesteProductiaSolara()
  242.     {
  243.        delay(50);
  244.        HTTPClient http;
  245.        http.begin(haApiUrl2);
  246.        http.addHeader("Authorization", "Bearer " + String(haApiToken));
  247.        int httpCode = http.GET();
  248.        if (httpCode > 0)
  249.          {
  250.             String payload = http.getString();
  251.             DynamicJsonDocument doc(1024);
  252.             deserializeJson(doc, payload);
  253.             float currentSolarPower = doc["state"].as<float>();
  254.             solarPower = currentSolarPower;
  255.             Serial.print("Productie Solara: ");
  256.             Serial.println(currentSolarPower);
  257.          } else
  258.               {
  259.                  Serial.println("Nu am reusit sa citesc Productia Solara!");
  260.               }
  261.          http.end();
  262.     }
  263. void FadeDown()
  264.     {
  265.        for(int i = 100; i >= 0; i--)
  266.          {
  267.            analogWrite(TFT_BL, i);
  268.            delay(10);
  269.          }    
  270.     }
  271. void FadeIn()
  272.     {
  273.        for(int i = 0; i <= 100; i++)
  274.          {
  275.            analogWrite(TFT_BL, i);
  276.            delay(10);
  277.          }    
  278.     }
  279. void AfiseazaAnimatiiCadran1()
  280.     {
  281.        VitezaInjectie = 55 - activePower / 100;
  282.        VitezaConsum = 55 - houseConsumption / 100;
  283.        VitezaRetea = 55 + activePower / 100;
  284.  
  285.        if (solarPower > 0)
  286.          {
  287.             AfiseazaSageata(425, 325, 135, GREEN, VitezaConsum, 10);
  288.             AfiseazaSageata5(415, 230, 90, GREEN, VitezaConsum, 10);
  289.          }
  290.  
  291.        if (activePower > 0)
  292.          {
  293.             AfiseazaSageata2(235, 325, 135, GREEN, VitezaInjectie, 10);
  294.             AfiseazaSageata4(385, 230, 90, GREEN, VitezaInjectie, 10);
  295.          }
  296.        if (activePower < 0)
  297.          {
  298.             AfiseazaSageata3(235, 355, 335, RED, VitezaRetea, 10);
  299.          }
  300.        
  301.        if (currentTimp - previousTimp >= IntervalTimp)
  302.           {
  303.             previousTimp = currentTimp;
  304.             AfiseazaOra();
  305.           }
  306.      }
  307.  
  308. void AfiseazaDatele2()
  309.     {
  310.       char buffer[3];
  311.       struct tm timeinfo;
  312.        if (getLocalTime(&timeinfo))
  313.          {
  314.             currentHour = timeinfo.tm_hour;
  315.             currentMinute = timeinfo.tm_min;
  316.             currentSecond = timeinfo.tm_sec;
  317.            
  318.             if (currentHour != prevHour)
  319.                {
  320.                   Serial.printf("Ora: %02d\n", currentHour);
  321.                   prevHour = currentHour;
  322.                   gfx->fillRect(0 , 110, 365, 230, BLACK);
  323.                   gfx->setCursor(0, 320);
  324.                   gfx->setTextColor(BLUE);
  325.                   gfx->setTextSize(6);
  326.                   //gfx->print(currentHour);
  327.                   sprintf(buffer, "%02d:", currentHour);
  328.                   gfx->print(buffer);  
  329.                }
  330.             if (currentMinute != prevMinute)
  331.                {
  332.                   Serial.printf("Minute: %02d\n", currentMinute);
  333.                   prevMinute = currentMinute;
  334.                   gfx->fillRect(365 , 110, 335, 230, BLACK);
  335.                   gfx->setCursor(365, 320);
  336.                   gfx->setTextColor(BLUE);
  337.                   gfx->setTextSize(6);
  338.                   sprintf(buffer, "%02d", currentMinute);
  339.                   gfx->print(buffer);  
  340.                }
  341.             if (currentSecond != prevSecond)
  342.                {
  343.                   Serial.printf("Secunde: %02d\n", currentSecond);
  344.                   prevSecond = currentSecond;
  345.                   gfx->fillRect(685 , 110, 120, 100, BLACK);
  346.                   gfx->setCursor(685, 185);
  347.                   gfx->setTextColor(RED);
  348.                   //gfx->setTextColor(gfx->color565(255, 120, 35));
  349.                   gfx->setTextSize(2);
  350.                   sprintf(buffer, "%02d", currentSecond);
  351.                   gfx->print(buffer);            
  352.                   delay(500);      
  353.                }
  354.           } else
  355.                {
  356.                   Serial.println("Eroare la obtinerea timpului.");
  357.                }
  358.                
  359.     }
  360.  
  361. void StergeCompletEcranul()
  362.     {
  363.       gfx->fillRect(0 , 0, centerX * 2, centerY * 2, BLACK);
  364.     }
  365.  
  366. void AfiseazaOra()
  367.    {
  368.       currentTimp = millis();
  369.       if (currentTimp - previousTimp >= IntervalTimp)
  370.         {
  371.            previousTimp = currentTimp;
  372.            struct tm timeinfo;
  373.            getLocalTime(&timeinfo);
  374.            gfx->setTextSize(1);
  375.            gfx->fillRect(308 , 398, 200, 50, BLACK);
  376.            gfx->setTextColor(WHITE);
  377.            gfx->setCursor(320, 440);
  378.            gfx->print(&timeinfo, "%H:%M:%S");
  379.            Serial.print(".");
  380.         }
  381. }
  382.  
  383. void AfiseazaSageata5(int SageataX5, int SageataY5, int SageataDistanta5, uint16_t SageataCuloare5, int SageataViteza5, int SageataDimensiune5)
  384.     {
  385.        currentSageataMillis5 = millis();
  386.        if (currentSageataMillis5 - previousSageataMillis5 >= SageataViteza5)
  387.           {
  388.              if (i5 > SageataDistanta5)
  389.                 {
  390.                    gfx->fillTriangle(SageataX5, SageataY5 + SageataDistanta5, SageataX5 - SageataDimensiune5, SageataY5 + SageataDistanta5 - SageataDimensiune5, SageataX5 + SageataDimensiune5, SageataY5 + SageataDistanta5 - SageataDimensiune5, BLACK);
  391.                    i5 = 0;
  392.                 }
  393.               gfx->fillTriangle(SageataX5, SageataY5 + i5, SageataX5 - SageataDimensiune5, SageataY5 - SageataDimensiune5 + i5, SageataX5 + SageataDimensiune5, SageataY5 - SageataDimensiune5 + i5, SageataCuloare5);
  394.               gfx->fillRect(SageataX5 - SageataDimensiune5 - 2, SageataY5 - SageataDimensiune5 - 1 + i5, SageataDimensiune5 * 2 + 4, 2, BLACK);
  395.               i5++;
  396.               previousSageataMillis5 = currentSageataMillis5;
  397.           }
  398.        gfx->drawLine(centerX + 15, centerY + 85, centerX + 15, centerY - 25, WHITE);
  399.     }
  400.  
  401. void AfiseazaSageata4(int SageataX4, int SageataY4, int SageataDistanta4, uint16_t SageataCuloare4, int SageataViteza4, int SageataDimensiune4)
  402.     {
  403.        currentSageataMillis4 = millis();
  404.        if (currentSageataMillis4 - previousSageataMillis4 >= SageataViteza4)
  405.           {
  406.              if (i4 > SageataDistanta4)
  407.                 {
  408.                    gfx->fillTriangle(SageataX4, SageataY4 + SageataDistanta4, SageataX4 - SageataDimensiune4, SageataY4 + SageataDistanta4 - SageataDimensiune4, SageataX4 + SageataDimensiune4, SageataY4 + SageataDistanta4 - SageataDimensiune4, BLACK);
  409.                    i4 = 0;
  410.                 }
  411.              gfx->fillTriangle(SageataX4, SageataY4 + i4, SageataX4 - SageataDimensiune4, SageataY4 - SageataDimensiune4 + i4, SageataX4 + SageataDimensiune4, SageataY4 - SageataDimensiune4 + i4, SageataCuloare4);
  412.              gfx->fillRect(SageataX4 - SageataDimensiune4 - 2, SageataY4 - SageataDimensiune4 - 1 + i4, SageataDimensiune4 * 2 + 4, 2, BLACK);
  413.              i4++;
  414.              previousSageataMillis4 = currentSageataMillis4;
  415.           }
  416.        gfx->drawLine(centerX - 15, centerY + 85, centerX - 15, centerY - 25, WHITE);
  417.     }
  418.  
  419. void AfiseazaSageata3(int SageataX3, int SageataY3, int SageataDistanta3, uint16_t SageataCuloare3, int SageataViteza3, int SageataDimensiune3)
  420.     {
  421.        currentSageataMillis3 = millis();
  422.        if (currentSageataMillis3 - previousSageataMillis3 >= SageataViteza3)
  423.           {
  424.              if (i3 > SageataDistanta3)
  425.                 {
  426.                    gfx->fillTriangle(SageataX3 + SageataDistanta3 + 1, SageataY3 - SageataDimensiune3, SageataX3 + SageataDistanta3 + 1, SageataY3 + SageataDimensiune3, SageataX3 + SageataDimensiune3 + i3 + 1, SageataY3, BLACK);
  427.                    i3 = 0;
  428.                 }
  429.              gfx->fillTriangle(SageataX3 + i3, SageataY3 - SageataDimensiune3, SageataX3 + i3, SageataY3 + SageataDimensiune3, SageataX3 + SageataDimensiune3 + i3, SageataY3, SageataCuloare3);
  430.              gfx->fillRect(SageataX3 + i3 - 1, SageataY3 - SageataDimensiune3, 2, SageataDimensiune3 * 2 + 1, BLACK);
  431.              i3++;
  432.              previousSageataMillis3 = currentSageataMillis3;
  433.           }
  434.        gfx->drawLine(centerX / 2 + 15, centerY + 115, centerX / 2 + centerX - 15, centerY + 115, WHITE);
  435.     }
  436.  
  437. void AfiseazaSageata2(int SageataX2, int SageataY2, int SageataDistanta2, uint16_t SageataCuloare2, int SageataViteza2, int SageataDimensiune2)
  438.     {
  439.        currentSageataMillis2 = millis();
  440.        if (currentSageataMillis2 - previousSageataMillis2 >= SageataViteza2)
  441.           {
  442.              if (i2 > SageataDistanta2)
  443.                 {
  444.                    gfx->fillTriangle(SageataX2 - SageataDimensiune2, SageataY2, SageataX2, SageataY2 - SageataDimensiune2, SageataX2, SageataY2 + SageataDimensiune2, BLACK);
  445.                    i2 = 0;
  446.                 }
  447.              gfx->fillTriangle(SageataX2 + SageataDistanta2 - SageataDimensiune2 - i2, SageataY2, SageataX2 + SageataDistanta2 - i2, SageataY2 - SageataDimensiune2, SageataX2 + SageataDistanta2 - i2, SageataY2 + SageataDimensiune2, GREEN);
  448.              gfx->fillRect(SageataX2 + SageataDistanta2 - i2 + 1, SageataY2 - SageataDimensiune2, 2, SageataDimensiune2 * 2 + 1, BLACK);
  449.              i2++;
  450.              previousSageataMillis2 = currentSageataMillis2;
  451.           }
  452.        gfx->drawLine(centerX / 2 + 15, centerY + 85, centerX - 15, centerY + 85, WHITE);
  453.     }
  454.  
  455. void AfiseazaSageata(int SageataX, int SageataY, int SageataDistanta, uint16_t SageataCuloare, int SageataViteza, int SageataDimensiune)
  456.     {
  457.        currentSageataMillis = millis();
  458.        if (currentSageataMillis - previousSageataMillis >= SageataViteza)
  459.           {
  460.              if (i > SageataDistanta)
  461.                 {
  462.                    gfx->fillTriangle(SageataX + SageataDistanta + 1, SageataY - SageataDimensiune, SageataX + SageataDistanta + 1, SageataY + SageataDimensiune, SageataX + SageataDimensiune + i + 1, SageataY, BLACK);
  463.                    i = 0;
  464.                 }
  465.              gfx->fillTriangle(SageataX + i, SageataY - SageataDimensiune, SageataX + i, SageataY + SageataDimensiune, SageataX + SageataDimensiune + i, SageataY, GREEN);
  466.              gfx->fillRect(SageataX + i - 1, SageataY - SageataDimensiune, 2, SageataDimensiune * 2 + 1, BLACK);
  467.              i++;
  468.              previousSageataMillis = currentSageataMillis;
  469.           }
  470.        gfx->drawLine(centerX + 15, centerY + 85, centerX / 2 + centerX - 15, centerY + 85, WHITE);
  471.     }
  472.  
  473. void CitesteDatele()
  474.     {
  475.        HTTPClient http;
  476.        http.begin(haApiUrl);
  477.        http.addHeader("Authorization", "Bearer " + String(haApiToken));
  478.        int httpCode = http.GET();
  479.        if (httpCode > 0)
  480.           {
  481.              String payload = http.getString();
  482.              DynamicJsonDocument doc(1024);
  483.              deserializeJson(doc, payload);
  484.              currentProduction = doc["state"].as<float>();
  485.              activePower = currentProduction;
  486.              Serial.print("Putere Activa: ");
  487.              Serial.println(currentProduction);
  488.           } else
  489.                 {
  490.                    Serial.println("Nu am reusit sa citesc Putere Activa!");
  491.                 }
  492.         delay(50);   //   If it fails to read the data from Home Assistant, try to increase the duration of this delay.
  493.         http.begin(haApiUrl2);
  494.         http.addHeader("Authorization", "Bearer " + String(haApiToken));
  495.         httpCode = http.GET();
  496.         if (httpCode > 0)
  497.           {
  498.              String payload = http.getString();
  499.              DynamicJsonDocument doc(1024);
  500.              deserializeJson(doc, payload);
  501.              currentSolarPower = doc["state"].as<float>();
  502.              solarPower = currentSolarPower;
  503.              Serial.print("Productie Solara: ");
  504.              Serial.println(currentSolarPower);
  505.           } else
  506.                {
  507.                   Serial.println("Nu am reusit sa citesc Productia Solara!");
  508.                }
  509.         delay(50);   //   If it fails to read the data from Home Assistant, try to increase the duration of this delay.
  510.         http.begin(haApiUrl3);
  511.         http.addHeader("Authorization", "Bearer " + String(haApiToken));
  512.         httpCode = http.GET();
  513.         if (httpCode > 0)
  514.           {
  515.              String payload = http.getString();
  516.              DynamicJsonDocument doc(1024);
  517.              deserializeJson(doc, payload);
  518.              houseConsumption2 = doc["state"].as<float>();
  519.              houseConsumption = houseConsumption2;
  520.              Serial.print("Consum casa: ");
  521.              Serial.println(houseConsumption);
  522.           } else
  523.                {
  524.                   Serial.println("Nu am reusit sa citesc Consumul casei!");
  525.                }
  526.         delay(50);   //   If it fails to read the data from Home Assistant, try to increase the duration of this delay.
  527.         http.begin(haApiUrl4);
  528.         http.addHeader("Authorization", "Bearer " + String(haApiToken));
  529.         httpCode = http.GET();
  530.         if (httpCode > 0)
  531.           {
  532.              String payload = http.getString();
  533.              DynamicJsonDocument doc(1024);
  534.              deserializeJson(doc, payload);
  535.              ProdusAstazi2 = doc["state"].as<float>();
  536.              ProdusAstazi = ProdusAstazi2;
  537.              Serial.print("Produs Astazi: ");
  538.              Serial.println(ProdusAstazi2);
  539.           } else
  540.                {
  541.                   Serial.println("Nu am reusit sa citesc Productia pe Astazi!");
  542.                }
  543.         delay(50);   //   If it fails to read the data from Home Assistant, try to increase the duration of this delay.
  544.         http.begin(haApiUrl5);
  545.         http.addHeader("Authorization", "Bearer " + String(haApiToken));
  546.         httpCode = http.GET();
  547.         if (httpCode > 0)
  548.           {
  549.              String payload = http.getString();
  550.              DynamicJsonDocument doc(1024);
  551.              deserializeJson(doc, payload);
  552.              Temperatura2 = doc["state"].as<float>();
  553.              Temperatura = Temperatura2;
  554.              Serial.print("Temperatura inverter: ");
  555.              Serial.println(Temperatura2);
  556.           } else
  557.                {
  558.                   Serial.println("Nu am reusit sa citesc Temperatura Inverter!");
  559.                }
  560.          delay(50);   //   If it fails to read the data from Home Assistant, try to increase the duration of this delay.
  561.          http.begin(haApiUrl6);
  562.          http.addHeader("Authorization", "Bearer " + String(haApiToken));
  563.          httpCode = http.GET();
  564.          if (httpCode > 0)
  565.            {
  566.               String payload = http.getString();
  567.               DynamicJsonDocument doc(1024);
  568.               deserializeJson(doc, payload);
  569.               Tensiune2 = doc["state"].as<float>();
  570.               Tensiune = Tensiune2;
  571.               Serial.print("Tensiune retea: ");
  572.               Serial.println(Tensiune2);
  573.            } else
  574.                 {
  575.                    Serial.println("Nu am reusit sa citesc Tensiune Retea!");
  576.                 }
  577.         //delay(50);   //   If it fails to read the data from Home Assistant, try to increase the duration of this delay.  
  578.          http.end();
  579.      }
  580.  
  581. void AfiseazaDatele()
  582.     {
  583.        AcoperaSagetile();
  584.        gfx->drawCircle(centerX, centerY - 130, mainRadius + 1, WHITE);
  585.        gfx->drawCircle(centerX, centerY - 130, mainRadius + 2, WHITE);
  586.        
  587.        if (solarPower > 0) solarColor = GREEN;
  588.        else solarColor = BLACK;
  589.        drawCircleWithValue(centerX, centerY - 130, mainRadius + 0, solarPower, solarColor);
  590.        gfx->drawCircle(centerX / 2 - 90, centerY + 100, mainRadius + 1, WHITE);
  591.        gfx->drawCircle(centerX / 2 - 90, centerY + 100, mainRadius + 2, WHITE);
  592.        
  593.        if (activePower >= 0) mainColor = GREEN;
  594.        else mainColor = RED;
  595.        drawCircleWithValue(centerX / 2 - 90, centerY + 100, mainRadius + 0, activePower, mainColor);
  596.        gfx->drawCircle(centerX / 2 + centerX + 90, centerY + 100, mainRadius + 1, WHITE);
  597.        gfx->drawCircle(centerX / 2 + centerX + 90, centerY + 100, mainRadius + 2, WHITE);
  598.  
  599.        if (activePower >= 0) houseColor = GREEN;
  600.        else houseColor = RED;
  601.        drawCircleWithValue(centerX / 2 + centerX + 90, centerY + 100, mainRadius + 0, houseConsumption, houseColor);
  602.        String ProdusAstaziS = String(ProdusAstazi2, 2) + " kWh";
  603.        drawTextWithOutline(800, 50, ProdusAstaziS, WHITE, GREEN);
  604.        String TensiuneS = String(Tensiune2, 1) + " V";
  605.        drawTextWithOutline(800, 100, TensiuneS, WHITE, GREEN);
  606.        String TemperaturaS = String(Temperatura2, 1) + " *C";
  607.        drawTextWithOutline(800, 150, TemperaturaS, WHITE, GREEN);
  608.        // linii intre cercuri
  609.        gfx->drawLine(centerX + 15, centerY + 85, centerX + 15, centerY - 25, WHITE);
  610.        gfx->drawLine(centerX - 15, centerY + 85, centerX - 15, centerY - 25, WHITE);
  611.        gfx->drawLine(centerX + 15, centerY + 85, centerX / 2 + centerX - 15, centerY + 85, WHITE);
  612.        gfx->drawLine(centerX / 2 + 15, centerY + 85, centerX - 15, centerY + 85, WHITE);
  613.        gfx->drawLine(centerX / 2 + 15, centerY + 115, centerX / 2 + centerX - 15, centerY + 115, WHITE);
  614.        // casa
  615.        gfx->fillTriangle(485, 245, 530, 185, 580, 245, RED);
  616.        gfx->fillRect(500, 246, 65, 60, YELLOW);
  617.        //stalp
  618.        gfx->fillRect(220, 210, 5, 100, WHITE);
  619.        gfx->fillRect(210, 220, 25, 5, WHITE);
  620.        gfx->fillRect(210, 230, 25, 5, WHITE);
  621.        gfx->fillRect(210, 240, 25, 5, WHITE);
  622.        //panouri
  623.        gfx->drawLine(105, 10, 305, 10, WHITE);
  624.        gfx->drawLine(80, 60, 280, 60, WHITE);
  625.        gfx->drawLine(80, 60, 105, 10, WHITE);
  626.        gfx->drawLine(120, 60, 145, 10, WHITE);
  627.        gfx->drawLine(160, 60, 185, 10, WHITE);
  628.        gfx->drawLine(200, 60, 225, 10, WHITE);
  629.        gfx->drawLine(240, 60, 265, 10, WHITE);
  630.        gfx->drawLine(280, 60, 305, 10, WHITE);
  631.     }
  632.  
  633. void AcoperaSagetile()
  634.     {
  635.        gfx->fillRect(centerX / 2 + 15, centerY + 74, 160, 24, BLACK);
  636.        gfx->fillRect(centerX + 25, centerY + 74, 160, 24, BLACK);
  637.        gfx->fillRect(centerX / 2 + 15, centerY + 104, 370, 24, BLACK);
  638.        gfx->fillRect(centerX - 25, centerY - 25, 24, 120, BLACK);
  639.        gfx->fillRect(centerX + 5, centerY - 25, 24, 120, BLACK);
  640.     }
  641.  
  642. void drawCircleWithValue(int x, int y, int radius, int value, uint16_t color)
  643.     {
  644.        gfx->fillCircle(x, y, radius, color);
  645.  
  646.        gfx->setTextColor(BLACK);
  647.        gfx->setFont(&FreeSans24pt7b);
  648.        gfx->setTextSize(1);
  649.  
  650.        char valueStr[10];
  651.        sprintf(valueStr, "%d", value);
  652.  
  653.        int16_t x1, y1;
  654.        uint16_t textWidth, textHeight;
  655.        gfx->getTextBounds(valueStr, 0, 0, &x1, &y1, &textWidth, &textHeight);
  656.  
  657.        int16_t textX = x - textWidth / 2;
  658.        int16_t textY = y + textHeight / 2;
  659.  
  660.        gfx->setCursor(textX, textY);
  661.        gfx->print(valueStr);
  662.     }
  663.  
  664. void drawTextWithOutline(int x, int y, String text, uint16_t color, uint16_t outlineColor)
  665.     {
  666.        gfx->setFont(&FreeSans24pt7b);
  667.        gfx->setTextSize(1);
  668.        int16_t x1, y1;
  669.        uint16_t textWidth, textHeight;
  670.        gfx->getTextBounds(text, x, y, &x1, &y1, &textWidth, &textHeight);
  671.        int16_t textX = x - textWidth;
  672.        int16_t textY = y + textHeight / 2;
  673.        gfx->fillRect(textX - 80, y - textHeight / 2, textWidth + 80, textHeight + 3, BLACK);
  674.        gfx->setTextColor(color);
  675.        gfx->setCursor(textX - 20, textY);
  676.        gfx->print(text);
  677.     }
  678.  
  679. void checkWiFiConnection()
  680.     {
  681.        if (WiFi.status() != WL_CONNECTED)
  682.          {
  683.             Serial.println("WiFi connection lost. Reconnecting...");
  684.             connectToWiFi();
  685.             gfx->setTextColor(YELLOW);
  686.             gfx->setCursor(10,50);
  687.             gfx->print("WiFi connection lost. Reconnecting...");
  688.          } else
  689.               {
  690.                  Serial.println("CHECK WIFI --> OK!");
  691.               }
  692.     }
  693.  
  694. void connectToWiFi()
  695.     {
  696.        gfx->setTextColor(YELLOW);
  697.        gfx->setCursor(10, 130);
  698.        gfx->print("WIFI");
  699.        WiFi.begin(ssid, password);
  700.        int attempt = 0;
  701.        while (WiFi.status() != WL_CONNECTED && attempt < 10)
  702.             {
  703.                delay(500);
  704.                Serial.print(".");
  705.                gfx->print(".");
  706.                attempt++;
  707.             }
  708.  
  709.        if (WiFi.status() == WL_CONNECTED)
  710.           {
  711.              Serial.println("\nConnected to WiFi");
  712.              gfx->print("OK!");
  713.              IPAddress ip = WiFi.localIP();
  714.              Serial.print("IP Address: ");
  715.              Serial.println(ip);
  716.              gfx->setTextColor(YELLOW);
  717.              gfx->setCursor(10, 150);
  718.              gfx->print("IP: ");
  719.              gfx->print(ip);
  720.  
  721.              int32_t rssi = WiFi.RSSI();
  722.              Serial.print("Signal strength (RSSI): ");
  723.              Serial.println(rssi);
  724.              gfx->setTextColor(YELLOW);
  725.              gfx->setCursor(10, 170);
  726.              gfx->print("Wifi Signal: ");
  727.              gfx->print(rssi);
  728.              gfx->print(" dBm");
  729.           } else
  730.                 {
  731.                    Serial.println("\nFailed to connect to WiFi. Check your credentials or network.");
  732.                    gfx->setTextColor(RED);
  733.                    gfx->print("Failed!");
  734.                 }
  735.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement