/*------------------------------------------------------------------------------ RPal BT x2T - two thermocouple (BT + ET) Bluetooth thermometer for Roasters Pal What it does: reads two K-type thermocouples through two MAX6675 boards (BT = bean temperature, ET = environment / exhaust temperature) and sends them to the Roasters Pal app over Bluetooth Low Energy. Hardware: ESP32 board (ESP32 Dev Module / DevKit V1 / WROOM / WROVER) 2 x MAX6675 thermocouple boards + 2 x K-type thermocouples Wiring: MAX6675 (BT) MAX6675 (ET) -> ESP32 VCC VCC -> 3V3 GND GND -> GND SCK SCK -> GPIO 14 (shared) SO SO -> GPIO 26 (shared) CS -> GPIO 27 CS -> GPIO 25 Arduino IDE: board package "esp32 by Espressif Systems" (2.0.x or 3.x), board "ESP32 Dev Module", upload speed 115200. No extra libraries needed: Bluetooth is part of the board package and the MAX6675 is read directly by this sketch. The app recognises the device by its name and the UUIDs below, so do not change them. Version: 2.0, Nov 2026 (first version Apr 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 // ---- Do not change: the app finds the device by these -------------------- #define DEVICE_NAME "RPal BT x2T" #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 14 // shared by both MAX6675 boards #define TC_SO 26 // shared by both MAX6675 boards #define BT_CS 27 #define ET_CS 25 #define LED_PIN 2 // blue LED on most ESP32 boards; -1 if you have none // ---- 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 BLECharacteristic* charBtC; BLECharacteristic* charBtF; BLECharacteristic* charEtC; BLECharacteristic* charEtF; volatile bool deviceConnected = false; bool wasConnected = false; float btSmoothed = NAN; float etSmoothed = NAN; 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; } // 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(); } float toF(float c) { return isnan(c) ? NAN : c * 9.0f / 5.0f + 32.0f; } void printReading(const char* name, float c) { Serial.print(name); Serial.print(": "); if (isnan(c)) Serial.println("no reading - is the thermocouple plugged in?"); else { Serial.print(c); Serial.println(" C"); } } void blink(int times, int onMs, int offMs) { if (LED_PIN < 0) return; for (int i = 0; i < times; i++) { digitalWrite(LED_PIN, HIGH); delay(onMs); digitalWrite(LED_PIN, LOW); delay(offMs); } } BLECharacteristic* addNotify(BLEService* service, const char* uuid) { BLECharacteristic* c = service->createCharacteristic(uuid, BLECharacteristic::PROPERTY_NOTIFY); c->addDescriptor(new BLE2902()); return c; } void setup() { Serial.begin(115200); 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); if (LED_PIN >= 0) pinMode(LED_PIN, OUTPUT); 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() { // after the app disconnects, be visible again so it can reconnect if (!deviceConnected && wasConnected) { delay(500); BLEDevice::startAdvertising(); Serial.println("Disconnected, advertising again"); } wasConnected = deviceConnected; unsigned long now = millis(); if (now - lastRead < READ_INTERVAL_MS) { delay(10); return; } lastRead = now; float btC = smooth(btSmoothed, readMax6675(TC_SCK, BT_CS, TC_SO)); float etC = smooth(etSmoothed, readMax6675(TC_SCK, ET_CS, TC_SO)); printReading("BT", btC); printReading("ET", etC); if (deviceConnected) { sendValue(charBtC, btC); sendValue(charBtF, toF(btC)); sendValue(charEtC, etC); sendValue(charEtF, toF(etC)); } // LED: 4 quick flashes = probe problem, 1 flash = sending, short tick = waiting if (isnan(btC) || isnan(etC)) blink(4, 40, 60); else if (deviceConnected) blink(1, 150, 0); else blink(1, 30, 0); }