Hi Lichurbagan
Good News
I uploaded the main.ino sketch to ChatGPT and requested it to modify the code to remove MQTT and Platformio features. Then modify to show device readings on the serial monitor.
And, it worked! Although I had to change the UUIDs.
I did try a modification to also use ATC software. No joy so I abandoned ATC for the time being.
So, now I have a working script that reads LYWSD03MMC stock firmware.
It shows that not all ChatGPT output is rubbish!
For anyone researching the same problem I am parking the code here:-
Code: Select all
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEClient.h>
#include <BLEAddress.h>
#define SENSOR_MAC "a4:c1:38:f6:89:e0" // <-- CHANGE THIS
#define SERIAL_BAUD 115200
// LYWSD03MMC UUIDs
// static BLEUUID serviceUUID("0000fe95-0000-1000-8000-00805f9b34fb"); // Did not work
// static BLEUUID charUUID("00000017-0000-1000-8000-00805f9b34fb"); // Did not work
static BLEUUID serviceUUID("ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6");
static BLEUUID charUUID ("ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6");
BLEClient* pClient;
bool connected = false;
class MyClientCallback : public BLEClientCallbacks {
void onConnect(BLEClient* pclient) {
Serial.println("✅ BLE connected");
}
void onDisconnect(BLEClient* pclient) {
Serial.println("❌ BLE disconnected");
connected = false;
}
};
void notifyCallback(
BLERemoteCharacteristic* pBLERemoteCharacteristic,
uint8_t* pData,
size_t length,
bool isNotify) {
if (length != 5) {
Serial.println("❗ Unexpected data length");
return;
}
float temperature = (pData[0] | (pData[1] << 8)) * 0.01;
float humidity = pData[2];
float voltage = ((pData[4] << 8) | pData[3]) / 1000.0;
float battery = (voltage - 2.1) * 100.0;
if (battery > 100) battery = 100;
if (battery < 0) battery = 0;
Serial.println("📡 Sensor Data:");
Serial.print(" Temperature: "); Serial.print(temperature); Serial.println(" °C");
Serial.print(" Humidity: "); Serial.print(humidity); Serial.println(" %");
Serial.print(" Voltage: "); Serial.print(voltage); Serial.println(" V");
Serial.print(" Battery: "); Serial.print(battery); Serial.println(" %");
Serial.println("--------------------------------");
pClient->disconnect();
}
void connectToSensor() {
BLEAddress address(SENSOR_MAC);
pClient = BLEDevice::createClient();
pClient->setClientCallbacks(new MyClientCallback());
if (!pClient->connect(address)) {
Serial.println("❌ Failed to connect to sensor");
return;
}
connected = true;
BLERemoteService* pService = pClient->getService(serviceUUID);
if (pService == nullptr) {
Serial.println("❌ Service not found");
pClient->disconnect();
return;
}
BLERemoteCharacteristic* pChar = pService->getCharacteristic(charUUID);
if (pChar == nullptr) {
Serial.println("❌ Characteristic not found");
pClient->disconnect();
return;
}
Serial.println("🔔 Waiting for notifications...");
pChar->registerForNotify(notifyCallback);
}
void setup() {
Serial.begin(SERIAL_BAUD);
delay(1000);
Serial.println("🚀 Starting BLE Serial Monitor Client");
BLEDevice::init("ESP32_BLE_Reader");
connectToSensor();
}
void loop() {
// Nothing needed here
delay(1000);
}
Many Thanks