ESP32 MAX31855-Thermoelement-LCD minimales Beispiel mit PlatformIO

Wichtig: In neueren Versionen von PlatformIO werden die meisten ESP32-Boards mit diesem Beispiel abstürzen, es sei denn, Sie erstellen eine custom lv_conf.h-Konfigurationsdatei, die angibt, internen Speicher statt PSRAM zu verwenden. Siehe Wie man ESP32-LVGL-Absturz/Reboot in lv_tlsf_create() / lv_mem_init() behebt für Anweisungen zur Behebung.

In unserem vorherigen Beispiel Adafruit-ST7735R-Display minimales LVGL-Beispiel für PlatformIO haben wir gezeigt, wie man LVGL verwendet, um einen einfachen Text auf einem ST7735R-basierten SPI-Display anzuzeigen.

In diesem Post erweitern wir dieses Beispiel um

Siehe Minimales ESP32 PlatformIO TFT-Display-Beispiel mit Adafruit ST7735 für die empfohlene Hardware-Konfiguration für das Display. Der Code unterstützt zwei MAX31855, aber nur einer wird für dieses Beispiel verwendet. Der Pinout für den MAX31855 ist:

max31855_pins.h
constexpr int8_t Pin_MAX31855_CLK = 21;
constexpr int8_t Pin_MAX31855_MISO = 5;
constexpr int8_t Pin_MAX31855_A_CS = 18;

Mit einem ESP32 mit einem 128x160px-ST7735R-Display erreicht dieser Code eine Aktualisierungsrate von ~5Hz. Beachten Sie, dass dieses Beispiel leicht modifiziert werden kann, um mit anderen LCD-Controllern oder Displays zu funktionieren.

ESP32 mit ST7735R-TFT-Display zeigt MAX31855-Thermoelement-Temperaturmessung in Grad Celsius mit LVGL

max31855_lcd_example.cpp
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <Adafruit_LvGL_Glue.h>
#include <Adafruit_MAX31855.h>
#include <string>
#include <sstream>

constexpr int8_t Pin_LCD_CS = 27;
constexpr int8_t Pin_LCD_DC = 23;
constexpr int8_t Pin_LCD_RST = 22;
constexpr int8_t Pin_LCD_SCLK = 14;
constexpr int8_t Pin_LCD_MISO = 12;
constexpr int8_t Pin_LCD_MOSI = 13;

constexpr int8_t Pin_MAX31855_CLK = 21;
constexpr int8_t Pin_MAX31855_MISO = 5;
// Two different Chip selects for two different MAX31855
constexpr int8_t Pin_MAX31855_A_CS = 18;
constexpr int8_t Pin_MAX31855_B_CS = 19;

SPIClass vspi(VSPI);
Adafruit_MAX31855 thermocouple1(Pin_MAX31855_A_CS, &vspi);
Adafruit_MAX31855 thermocouple2(Pin_MAX31855_B_CS, &vspi);

Adafruit_ST7735 lcd(Pin_LCD_CS, Pin_LCD_DC, Pin_LCD_MOSI, Pin_LCD_SCLK,
                                 Pin_LCD_RST);
Adafruit_LvGL_Glue glue;

// Number of consecutive NaN reads before we assume the thermocouple is actually disconnected
uint32_t consecutiveNaNReads = 0;
constexpr uint32_t maxConsecutiveNaNReads = 10;
std::string labelText = "";
lv_obj_t *label;

void lvgl_setup(void) {
  // Create simple label centered on screen
  label = lv_label_create(lv_scr_act());
  lv_label_set_text(label, "Initializing..");
  lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
}

template<typename T>
std::string celsiusToString(const T a_value, const int decimals = 1)
{
    std::ostringstream out;
    out.precision(decimals);
    out << std::fixed << a_value << " °C";
    return std::move(out).str();
}

bool IsValidReading(double reading) {
  return !isnan(reading) && reading > -40.0 && reading < 1000.0;
}

void ReadTemperature() {
  double celsius = thermocouple1.readCelsius();
  if(!IsValidReading(celsius)) {
    consecutiveNaNReads++;
    if(consecutiveNaNReads >= maxConsecutiveNaNReads) {
      // Thermocouple is disconnected
      lv_label_set_text(label, "Error");
    }
  } else {
    consecutiveNaNReads = 0;
    labelText = "T: " + celsiusToString(celsius);
    lv_label_set_text(label, labelText.c_str());
  }
}

void setup() {
  Serial.begin(115200);
  // Start MAX31855 SPI
  vspi.begin(Pin_MAX31855_CLK, Pin_MAX31855_MISO, Pin_MAX31855_A_CS);

  lcd.initR(INITR_BLACKTAB);      // Init ST7735S chip, black tab
  lcd.setRotation(1);

  LvGLStatus status = glue.begin(&lcd);
  if(status != LVGL_OK) {
    Serial.printf("Glue error %d\r\n", (int)status);
    ESP.restart();
  }

  lvgl_setup(); // Call UI-building function above
}

void loop() {
  ReadTemperature();
  lv_task_handler(); // Call LittleVGL task handler periodically
  delay(5);
}
platformio.ini
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps =
    adafruit/Adafruit GFX Library@^1.11.5
    adafruit/Adafruit ST7735 and ST7789 Library@^1.10.0
    adafruit/Adafruit LittlevGL Glue Library@^2.1.4
    adafruit/SdFat - Adafruit Fork@^2.2.1
    lvgl/lvgl@^8.3.7
    adafruit/Adafruit MAX31855 library@^1.4.0

Check out similar posts by category: Arduino, LVGL, PlatformIO