/*------------------------------------------------------------------------------ RPal BT x2T LCD - two thermocouples (BT + ET) with a display, for Roasters Pal What it does: reads two K-type thermocouples through two MAX6675 boards, shows them on the built-in screen and sends them to the Roasters Pal app over Bluetooth Low Energy. Hardware: LilyGO TTGO T-Display (ESP32 with 1.14" ST7789 screen, 135 x 240) 2 x MAX6675 thermocouple boards + 2 x K-type thermocouples Wiring: MAX6675 (BT) MAX6675 (ET) -> T-Display VCC VCC -> 3V GND GND -> G SCK SCK -> GPIO 12 (shared) SO SO -> GPIO 15 (shared) CS -> GPIO 13 CS -> GPIO 17 Buttons: GPIO 0 button -> switch C / F on the screen GPIO 35 button -> screen brightness (the app always gets both C and F and uses the unit set in the app) Arduino IDE: board package "esp32 by Espressif Systems" (2.0.x or 3.x), board "ESP32 Dev Module" (or "TTGO LoRa32-OLED"), upload speed 115200. Library: TFT_eSPI by Bodmer (Library Manager). After installing it, open Documents/Arduino/libraries/TFT_eSPI/User_Setup_Select.h and - put // in front of #include - remove // in front of #include Nothing else needs editing. The app recognises the device by its name and the UUIDs below, so do not change them. Version: 2.0, Nov 2026 (first version Dec 2023) Author: (c) RoastersPal.com Licence: MIT License Copyright (c) 2023-2026 RoastersPal.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------*/ #include #include #include #include #include // ---- Do not change: the app finds the device by these -------------------- #define DEVICE_NAME "RPal BT x2T LCD" #define SERVICE_UUID "1fc7e3ac-e544-4c01-adc4-94d13757e086" #define CHAR_BT_C "00001001-0000-1000-8000-00805F9B34FB" // BT in Celsius #define CHAR_BT_F "00001002-0000-1000-8000-00805F9B34FB" // BT in Fahrenheit #define CHAR_ET_C "00001003-0000-1000-8000-00805F9B34FB" // ET in Celsius #define CHAR_ET_F "00001004-0000-1000-8000-00805F9B34FB" // ET in Fahrenheit // ---- Pins (change only if you wired it differently) ---------------------- #define TC_SCK 12 // shared by both MAX6675 boards #define TC_SO 15 // shared by both MAX6675 boards #define BT_CS 13 #define ET_CS 17 #define BUTTON_UNITS 0 // C / F on the screen #define BUTTON_BRIGHTNESS 35 // ---- Settings ------------------------------------------------------------ #define READ_INTERVAL_MS 1000 // one reading a second (the MAX6675 needs >= 220 ms) #define SMOOTHING 5 // 1 = no smoothing, higher = calmer but slower #define INVERT_DISPLAY true // set to false if the colours look inverted const int brightness[] = {10, 40, 80, 120, 200, 255}; const int brightnessSteps = sizeof(brightness) / sizeof(brightness[0]); TFT_eSPI tft = TFT_eSPI(); BLECharacteristic* charBtC; BLECharacteristic* charBtF; BLECharacteristic* charEtC; BLECharacteristic* charEtF; volatile bool deviceConnected = false; bool wasConnected = false; float btSmoothed = NAN; float etSmoothed = NAN; float lastBt = NAN, lastEt = NAN; bool showFahrenheit = false; int brightnessStep = 2; bool unitsWasDown = false, brightnessWasDown = false; unsigned long lastRead = 0; class ServerCallbacks : public BLEServerCallbacks { void onConnect(BLEServer* server) { deviceConnected = true; } void onDisconnect(BLEServer* server) { deviceConnected = false; } }; // Reads one MAX6675. Returns NAN when the thermocouple is not plugged in or // the board does not answer (check the wiring). float readMax6675(int sck, int cs, int so) { uint16_t v = 0; digitalWrite(cs, LOW); delayMicroseconds(10); for (int i = 15; i >= 0; i--) { digitalWrite(sck, LOW); delayMicroseconds(10); if (digitalRead(so)) v |= (1 << i); digitalWrite(sck, HIGH); delayMicroseconds(10); } digitalWrite(sck, LOW); digitalWrite(cs, HIGH); if (v == 0 || v == 0xFFFF) return NAN; // no answer: wiring or power if (v & 0x04) return NAN; // thermocouple open / unplugged return (v >> 3) * 0.25f; } float smooth(float& state, float raw) { if (isnan(raw)) { state = NAN; // start again when the probe comes back return NAN; } if (isnan(state)) state = raw; else state += (raw - state) / SMOOTHING; return state; } float toF(float c) { return isnan(c) ? NAN : c * 9.0f / 5.0f + 32.0f; } // The app expects plain text like "215.25". "nan" means "no reading". void sendValue(BLECharacteristic* c, float value) { char text[12]; if (isnan(value)) strcpy(text, "nan"); else dtostrf(value, 1, 2, text); c->setValue((uint8_t*)text, strlen(text)); c->notify(); } BLECharacteristic* addNotify(BLEService* service, const char* uuid) { BLECharacteristic* c = service->createCharacteristic(uuid, BLECharacteristic::PROPERTY_NOTIFY); c->addDescriptor(new BLE2902()); return c; } // ---- Screen -------------------------------------------------------------- void setBacklight(int value) { #if ESP_ARDUINO_VERSION_MAJOR >= 3 ledcWrite(TFT_BL, value); #else ledcWrite(0, value); #endif } void startBacklight() { #if ESP_ARDUINO_VERSION_MAJOR >= 3 ledcAttach(TFT_BL, 5000, 8); #else ledcSetup(0, 5000, 8); ledcAttachPin(TFT_BL, 0); #endif setBacklight(brightness[brightnessStep]); } void drawStatus() { tft.setTextDatum(TL_DATUM); tft.setTextPadding(135); tft.setTextColor(deviceConnected ? TFT_GREEN : TFT_DARKGREY, TFT_BLACK); tft.drawString(deviceConnected ? "App connected" : "Waiting for app", 0, 4, 2); tft.setTextPadding(0); tft.setTextDatum(TR_DATUM); tft.setTextColor(TFT_ORANGE, TFT_BLACK); tft.drawString(showFahrenheit ? "F" : "C", 135, 4, 2); tft.drawFastHLine(0, 24, 135, TFT_ORANGE); } void drawLabels() { tft.setTextDatum(TL_DATUM); tft.setTextPadding(0); tft.setTextColor(TFT_YELLOW, TFT_BLACK); tft.drawString("BT", 4, 34, 4); tft.setTextColor(TFT_CYAN, TFT_BLACK); tft.drawString("ET", 4, 134, 4); } void drawValue(float celsius, int y, uint16_t colour) { char text[8]; float v = showFahrenheit ? toF(celsius) : celsius; if (isnan(v)) strcpy(text, "---"); else snprintf(text, sizeof(text), "%d", (int)lroundf(v)); tft.setTextDatum(TC_DATUM); tft.setTextPadding(135); // clears the old number without flicker tft.setTextColor(isnan(v) ? TFT_RED : colour, TFT_BLACK); tft.drawString(text, 67, y, 7); tft.setTextPadding(0); } void drawScreen() { drawStatus(); drawValue(lastBt, 66, TFT_YELLOW); drawValue(lastEt, 166, TFT_CYAN); } // ---- Buttons (pressed = LOW) --------------------------------------------- void checkButtons() { bool unitsDown = digitalRead(BUTTON_UNITS) == LOW; if (unitsDown && !unitsWasDown) { showFahrenheit = !showFahrenheit; drawScreen(); } unitsWasDown = unitsDown; bool brightnessDown = digitalRead(BUTTON_BRIGHTNESS) == LOW; if (brightnessDown && !brightnessWasDown) { brightnessStep = (brightnessStep + 1) % brightnessSteps; setBacklight(brightness[brightnessStep]); } brightnessWasDown = brightnessDown; } void setup() { Serial.begin(115200); pinMode(BUTTON_UNITS, INPUT_PULLUP); pinMode(BUTTON_BRIGHTNESS, INPUT); // GPIO 35 has a pull-up on the board pinMode(TC_SCK, OUTPUT); digitalWrite(TC_SCK, LOW); pinMode(BT_CS, OUTPUT); digitalWrite(BT_CS, HIGH); pinMode(ET_CS, OUTPUT); digitalWrite(ET_CS, HIGH); pinMode(TC_SO, INPUT); tft.init(); tft.setRotation(0); // portrait, USB socket at the bottom tft.invertDisplay(INVERT_DISPLAY); tft.fillScreen(TFT_BLACK); startBacklight(); drawLabels(); drawScreen(); BLEDevice::init(DEVICE_NAME); BLEServer* server = BLEDevice::createServer(); server->setCallbacks(new ServerCallbacks()); BLEService* service = server->createService(SERVICE_UUID); charBtC = addNotify(service, CHAR_BT_C); charBtF = addNotify(service, CHAR_BT_F); charEtC = addNotify(service, CHAR_ET_C); charEtF = addNotify(service, CHAR_ET_F); service->start(); BLEDevice::startAdvertising(); Serial.println(DEVICE_NAME " ready, waiting for the app..."); } void loop() { checkButtons(); if (deviceConnected != wasConnected) { if (!deviceConnected) { // after the app disconnects, be visible again so it can reconnect delay(500); BLEDevice::startAdvertising(); } wasConnected = deviceConnected; drawStatus(); } unsigned long now = millis(); if (now - lastRead < READ_INTERVAL_MS) { delay(10); return; } lastRead = now; lastBt = smooth(btSmoothed, readMax6675(TC_SCK, BT_CS, TC_SO)); lastEt = smooth(etSmoothed, readMax6675(TC_SCK, ET_CS, TC_SO)); Serial.printf("BT: %.2f C ET: %.2f C\n", lastBt, lastEt); if (deviceConnected) { sendValue(charBtC, lastBt); sendValue(charBtF, toF(lastBt)); sendValue(charEtC, lastEt); sendValue(charEtF, toF(lastEt)); } drawScreen(); }