I'm very amateur in programming. I only have basic knowledge in C/C++.
So very sorry if my question or programming is very in low level.
I'm developing a gateway for my sensor module (as ble server). I would like to
publish the sensor data to mqtt broker. So I'm using ESP32 that can use BT + WiFi.
Below is my programming. Based is from example of BLE_Client. And I modify
by myself (some took from this forum discussion).
My ESP32 able to connect and get data from the sensor module.
But I have a problem :
1) The result didn't change. Even my program I make it will go get new data
each second, the result is always same.
My program not going to get new value?
2) The result is in HEX (0xXXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX).
Full result as above, will have 40 character (I don't know how to call). But the problem is,
the result will stop until "0". Example, if result is 0xF31462E15C18400E00000200000000001A0711F0,
it only appear until F314 62E15 C184.
Any way to get full result?
Please help....
Code: Select all
#include "BLEDevice.h"
//#include "BLEScan.h"
#include "WiFi.h"
#include "PubSubClient.h"
const char* ssid = "xxxxxxxxxx";
const char* password = "xxxxxxxxxxxx";
const char* mqtt_server = "broker.mqtt-dashboard.com";
// The remote service we wish to connect to.
static BLEUUID serviceUUID("47FE55D8-447F-43EF-9AD9-FE6325E17C47");
// The characteristic of the remote service we are interested in.
static BLEUUID notifyUUID("686A9A3B-4C2C-4231-B871-9CFE92CC6B1E");
static BLEUUID writeUUID("0000fff2-0000-1000-8000-00805f9b34fb");
static BLEAddress macAddress = BLEAddress("28:A1:83:E1:58:8E");
static BLEAddress *pServerAddress;
static boolean doConnect = false;
static boolean connected = false;
static BLERemoteCharacteristic* notifyChar;
static BLERemoteCharacteristic* writeChar;
WiFiClient espClient;
PubSubClient client(espClient);
String result;
void mqttconnect() {
/* Loop until reconnected */
while (!client.connected()) {
Serial.print("MQTT connecting ...");
/* client ID */
String clientId = "ESP32Client";
/* connect now */
if (client.connect(clientId.c_str())) {
Serial.println("connected");
} else {
Serial.print("failed, status code =");
Serial.print(client.state());
Serial.println("try again in 5 seconds");
/* Wait 5 seconds before retrying */
delay(5000);
}
}
}
static void notifyCallback(
BLERemoteCharacteristic* pBLERemoteCharacteristic,
uint8_t* pData,
size_t length,
bool isNotify) {
Serial.print("Notify callback for characteristic ");
Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
Serial.print(" of data length ");
Serial.println(length);
}
void connectToServer(BLEAddress pAddress) {
Serial.print("Forming a connection to ");
Serial.println(pAddress.toString().c_str());
BLEClient* pClient = BLEDevice::createClient();
Serial.println(" - Created client");
// Connect to the remove BLE Server.
pClient->connect(pAddress);
Serial.println(" - Connected to server");
// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
if (pRemoteService == nullptr) {
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID.toString().c_str());
return;
}
// Obtain a reference to the characteristic in the service of the remote BLE server.
notifyChar = pRemoteService->getCharacteristic(notifyUUID);
writeChar = pRemoteService->getCharacteristic(writeUUID);
if (notifyChar == nullptr) {
Serial.print("Failed to find our characteristic UUID: ");
Serial.println(notifyUUID.toString().c_str());
return;
}
// Read the value of the characteristic.
std::string value = notifyChar->readValue();
Serial.print("The characteristic value was: ");
Serial.println(value.c_str());
result = value.c_str();
notifyChar->registerForNotify(notifyCallback);
Serial.println("Done connecting");
delay(400);
}
/**
Scan for BLE servers and find the first one that advertises the address we are looking for.
*/
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
/**
Called for each advertising BLE server.
*/
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.print("BLE Advertised Device found: ");
Serial.println(advertisedDevice.toString().c_str());
if (advertisedDevice.getAddress().equals(macAddress)) {
// if (advertisedDevice.haveServiceUUID() && advertisedDevice.getServiceUUID().equals(serviceUUID)) {
advertisedDevice.getScan()->stop();
Serial.print("Found our device! address: ");
Serial.println(advertisedDevice.getAddress().toString().c_str());
pServerAddress = new BLEAddress(advertisedDevice.getAddress());
doConnect = true;
/*
MyClient* pMyClient = new MyClient();
pMyClient->setStackSize(18000);
pMyClient->start(new BLEAddress(*advertisedDevice.getAddress().getNative()));
*/
} // Found our server
} // onResult
}; // MyAdvertisedDeviceCallbacks
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println("Starting ...");
BLEDevice::init("");
BLEScan *pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
pBLEScan->start(30);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address : ");
Serial.println(WiFi.localIP());
client.setServer(mqtt_server, 1883);
}
void loop()
{
// put your main code here, to run repeatedly:
if (doConnect == true) {
connectToServer(*pServerAddress);
doConnect = false;
connected = true;
}
if (connected) {
Serial.println(result);
client.publish("ESPert/ilzam", result.c_str());
}
if (!client.connected()) {
mqttconnect();
}
delay(1000);
}