Advertisement
LeventeDaradici

Reading data from Home Assistant and displaying them on a ST7735 display

Feb 6th, 2024 (edited)
1,935
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | Source Code | 0 0
  1. //
  2. // Can ESP32 read data from Home Assistant? Reading "Inverter Active Power" from Home Assistant connected to Huawei SUN2000-5KTL-L1 inverter
  3. //
  4. //  https://youtu.be/p7ZOHrjYuNU
  5. //
  6.  
  7. #include <Adafruit_GFX.h>
  8. #include <Adafruit_ST7735.h>
  9. #include <SPI.h>
  10. #include <ArduinoJson.h>
  11. #include <WiFi.h>
  12. #include <HTTPClient.h>
  13.  
  14. const char* ssid = "YOUR WIFI SSID";
  15. const char* password = "YOUR WIFI PASSWORD";
  16. const char* haApiUrl = "http://YOUR HOME ASSISTANT SERVER IP:8123/api/states/XXXXXXXXX_SENSOR_NAME_XXXXXXX";
  17. // EXAMPLE:
  18. // const char* haApiUrl = "http://192.168.0.103:8123/api/states/sensor.power_meter_active_power";
  19.  
  20. const char* haApiToken = "YOUR HOME ASSISTANT TOKEN";
  21.  
  22. #define TFT_CS    5
  23. #define TFT_RST   4
  24. #define TFT_DC    2
  25.  
  26. Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
  27.  
  28. void setup() {
  29.   Serial.begin(115200);
  30.   WiFi.begin(ssid, password);
  31.  
  32.   while (WiFi.status() != WL_CONNECTED) {
  33.     delay(1000);
  34.     Serial.println("Connecting to WiFi...");
  35.   }
  36.  
  37.   Serial.println("Connected to WiFi");
  38.  
  39.   //tft.initR(INITR_GREENTAB);
  40.   //tft.initR(INITR_REDTAB);
  41.   tft.initR(INITR_BLACKTAB);
  42.   tft.setRotation(1);
  43.   tft.fillScreen(ST7735_BLACK);  
  44. }
  45.  
  46. void loop() {
  47.   HTTPClient http;
  48.   http.begin(haApiUrl);
  49.   http.addHeader("Authorization", "Bearer " + String(haApiToken));
  50.  
  51.   int httpCode = http.GET();
  52.  
  53.   if (httpCode > 0) {
  54.     String payload = http.getString();
  55.  
  56.     DynamicJsonDocument doc(1024);
  57.     deserializeJson(doc, payload);
  58.  
  59.     float currentProduction = doc["state"].as<float>();
  60.  
  61.     int intProduction = int(currentProduction);
  62.     Serial.print("Current Production: ");  
  63.  
  64.     int lungimeText;
  65.     int16_t x, y;
  66.     uint16_t w, h;
  67.  
  68.     //tft.setTextColor(ST7735_WHITE);
  69.     if (intProduction >= 0)
  70.        {
  71.           //tft.setTextColor(ST7735_GREEN);
  72.           tft.setTextColor(0x07E0);
  73.        } else {
  74.                  //tft.setTextColor(ST7735_RED);
  75.                  tft.setTextColor(0xF800);
  76.               }
  77.     tft.setTextSize(5);
  78.     tft.getTextBounds(String(intProduction), 0, 0, &x, &y, &w, &h);
  79.     lungimeText = w + (intProduction < 0 ? 6 : 0);
  80.     int inaltimeText = h;
  81.     tft.setCursor(tft.width() - lungimeText, tft.height() - inaltimeText);
  82.     tft.fillScreen(ST7735_BLACK);
  83.     tft.print(intProduction);
  84.  
  85.     Serial.println(intProduction);
  86.     delay(5000);  
  87.   } else {
  88.     Serial.println("Error in HTTP request");
  89.     delay(1000);
  90.   }
  91.  
  92.   http.end();
  93.  
  94. }
  95.  
  96. int numarDigits(int numar)
  97.     {
  98.       int cifre = 1;
  99.       while (numar /= 10)
  100.            {
  101.              cifre++;
  102.            }
  103.   return cifre;
  104.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement