/*------------------------------------------------------------------------------ RPal BT IRx1T - infrared bean sensor + one thermocouple, with a display What it does: reads an MLX90614 infrared sensor (bean temperature, BT, and the room temperature inside the sensor) and one K-type thermocouple through a MAX6675 board (ET), 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) MLX90614 infrared temperature sensor board (3.3 V version) MAX6675 thermocouple board + K-type thermocouple Wiring: MLX90614 -> T-Display MAX6675 -> T-Display VIN -> 3V VCC -> 3V GND -> G GND -> G SDA -> GPIO 21 SCK -> GPIO 12 SCL -> GPIO 22 CS -> GPIO 13 SO -> GPIO 15 What the app gets: BT = infrared (object), ROOM = the sensor's own temperature, ET = thermocouple. In the app's Settings you can use the thermocouple as BT instead ("Use ET for BT"). Units: the app gets the unit shown on the screen. Set the screen to the same unit as the app before a roast (button on GPIO 0); the choice is remembered when the device is switched off. Buttons: GPIO 0 button -> switch C / F GPIO 35 button -> screen brightness 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. Libraries (Library Manager): TFT_eSPI by Bodmer, and Adafruit MLX90614 Library (say yes when it asks to install Adafruit BusIO too). After installing TFT_eSPI, 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 Oct 2021, LabAsti) 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 #include #include #include // ---- Do not change: the app finds the device by these -------------------- #define DEVICE_NAME "RPal BT IRx1T" #define SERVICE_UUID "1fc7e3ac-e544-4c01-adc4-94d13757e086" #define CHAR_BT "00001001-0000-1000-8000-00805F9B34FB" // BT: infrared #define CHAR_ROOM "00001002-0000-1000-8000-00805F9B34FB" // room (sensor's own) #define CHAR_ET "00001003-0000-1000-8000-00805F9B34FB" // ET: thermocouple // ---- Pins (change only if you wired it differently) ---------------------- #define TC_SCK 12 #define TC_SO 15 #define ET_CS 13 #define IR_SDA 21 #define IR_SCL 22 #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 // thermocouple: 1 = no smoothing, higher = calmer #define IR_SMOOTHING 3 // infrared: 1 = no smoothing, higher = calmer #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(); Adafruit_MLX90614 ir = Adafruit_MLX90614(); Preferences prefs; bool irFound = false; BLECharacteristic* charBt; BLECharacteristic* charRoom; BLECharacteristic* charEt; volatile bool deviceConnected = false; bool wasConnected = false; float btSmoothed = NAN; float etSmoothed = NAN; float lastBt = NAN, lastEt = NAN, lastRoom = 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, float factor) { if (isnan(raw)) { state = NAN; // start again when the probe comes back return NAN; } if (isnan(state)) state = raw; else state += (raw - state) / factor; return state; } // MLX90614 readings in C; NAN when the sensor doesn't answer or the value is // outside what it can measure (-70..380 C object, -40..125 C room). float readIr(bool room) { if (!irFound) return NAN; float c = room ? ir.readAmbientTempC() : ir.readObjectTempC(); if (isnan(c)) return NAN; if (room ? (c < -40 || c > 125) : (c < -70 || c > 380)) return NAN; return c; } 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_SKYBLUE, TFT_BLACK); tft.drawString("ROOM", 4, 32, 2); tft.setTextColor(TFT_YELLOW, TFT_BLACK); tft.drawString("BT infrared", 4, 54, 2); tft.setTextColor(TFT_CYAN, TFT_BLACK); tft.drawString("ET thermocouple", 4, 140, 2); } void drawRoom() { char text[12]; float v = showFahrenheit ? toF(lastRoom) : lastRoom; if (isnan(v)) strcpy(text, "---"); else snprintf(text, sizeof(text), "%.1f", v); tft.setTextDatum(TR_DATUM); tft.setTextPadding(70); tft.setTextColor(isnan(v) ? TFT_RED : TFT_SKYBLUE, TFT_BLACK); tft.drawString(text, 135, 32, 2); tft.setTextPadding(0); } 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(); drawRoom(); drawValue(lastBt, 76, TFT_YELLOW); drawValue(lastEt, 162, TFT_CYAN); } // The app gets the unit on the screen (that's how the app reads this device). float inScreenUnit(float c) { return showFahrenheit ? toF(c) : c; } // ---- Buttons (pressed = LOW) --------------------------------------------- void checkButtons() { bool unitsDown = digitalRead(BUTTON_UNITS) == LOW; if (unitsDown && !unitsWasDown) { showFahrenheit = !showFahrenheit; prefs.putBool("fahrenheit", 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(ET_CS, OUTPUT); digitalWrite(ET_CS, HIGH); pinMode(TC_SO, INPUT); prefs.begin("rpal", false); showFahrenheit = prefs.getBool("fahrenheit", false); Wire.begin(IR_SDA, IR_SCL); irFound = ir.begin(); if (!irFound) Serial.println("MLX90614 not found - check SDA, SCL and power"); 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); charBt = addNotify(service, CHAR_BT); charRoom = addNotify(service, CHAR_ROOM); charEt = addNotify(service, CHAR_ET); 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; if (!irFound) irFound = ir.begin(); // sensor plugged in later lastBt = smooth(btSmoothed, readIr(false), IR_SMOOTHING); lastRoom = readIr(true); lastEt = smooth(etSmoothed, readMax6675(TC_SCK, ET_CS, TC_SO), SMOOTHING); Serial.printf("BT (IR): %.2f C ROOM: %.2f C ET: %.2f C\n", lastBt, lastRoom, lastEt); if (deviceConnected) { sendValue(charBt, inScreenUnit(lastBt)); sendValue(charRoom, inScreenUnit(lastRoom)); sendValue(charEt, inScreenUnit(lastEt)); } drawScreen(); }