2018-11-26 11:58:15 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the "bluetoothheater" distribution
|
|
|
|
* (https://gitlab.com/mrjones.id.au/bluetoothheater)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Ray Jones <ray@mrjones.id.au>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-10-31 18:57:21 +00:00
|
|
|
#include <Arduino.h>
|
2018-10-18 09:49:14 +00:00
|
|
|
#include "pins.h"
|
2018-10-20 07:11:23 +00:00
|
|
|
#include "Protocol.h"
|
|
|
|
#include "debugport.h"
|
2018-10-30 11:05:29 +00:00
|
|
|
#include "BluetoothESP32.h"
|
2018-11-06 09:43:54 +00:00
|
|
|
#include "BTCConfig.h"
|
2018-10-18 09:49:14 +00:00
|
|
|
|
2018-10-28 03:03:44 +00:00
|
|
|
|
|
|
|
|
2018-10-20 07:11:23 +00:00
|
|
|
#ifdef ESP32
|
2018-10-18 09:49:14 +00:00
|
|
|
|
|
|
|
|
2018-10-31 18:57:21 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// HC-05 BLUETOOTH with ESP32
|
|
|
|
// |
|
|
|
|
// V
|
2018-10-27 09:03:10 +00:00
|
|
|
//
|
2018-10-31 18:57:21 +00:00
|
|
|
CBluetoothESP32HC05::CBluetoothESP32HC05(int keyPin, int sensePin, int rxPin, int txPin) : CBluetoothHC05(keyPin, sensePin)
|
2018-10-27 06:35:17 +00:00
|
|
|
{
|
2018-10-31 18:57:21 +00:00
|
|
|
_rxPin = rxPin;
|
|
|
|
_txPin = txPin;
|
2018-11-06 09:43:54 +00:00
|
|
|
|
|
|
|
pinMode(_txPin, OUTPUT);
|
|
|
|
digitalWrite(_txPin, HIGH);
|
|
|
|
pinMode(_rxPin, INPUT_PULLUP);
|
2018-10-27 06:35:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 18:57:21 +00:00
|
|
|
void
|
|
|
|
CBluetoothESP32HC05::openSerial(int baudrate)
|
2018-10-27 06:35:17 +00:00
|
|
|
{
|
2018-10-31 18:57:21 +00:00
|
|
|
// Open Serial port on the ESP32
|
|
|
|
// best to explicitly specify pins for the pin multiplexer!
|
|
|
|
HC05_SerialPort.begin(baudrate, SERIAL_8N1, _rxPin, _txPin);
|
2018-10-27 06:35:17 +00:00
|
|
|
}
|
2018-10-31 18:57:21 +00:00
|
|
|
// ^
|
|
|
|
// |
|
|
|
|
// HC-05 BLUETOOTH with ESP32
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-10-27 06:35:17 +00:00
|
|
|
|
|
|
|
|
2018-10-18 09:49:14 +00:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
2018-10-31 18:57:21 +00:00
|
|
|
// CLASSIC BLUETOOTH on ESP32
|
2018-10-18 09:49:14 +00:00
|
|
|
// |
|
|
|
|
// V
|
|
|
|
|
2018-10-31 18:57:21 +00:00
|
|
|
void
|
2018-12-12 10:37:02 +00:00
|
|
|
CBluetoothESP32Classic::begin()
|
2018-10-18 09:49:14 +00:00
|
|
|
{
|
2018-10-31 18:57:21 +00:00
|
|
|
_rxLine.clear();
|
2018-11-07 10:31:00 +00:00
|
|
|
DebugPort.println("Initialising ESP32 Classic Bluetooth");
|
2018-10-20 07:11:23 +00:00
|
|
|
|
2018-10-18 09:49:14 +00:00
|
|
|
if(!SerialBT.begin("ESPHEATER")) {
|
2018-10-30 07:23:22 +00:00
|
|
|
DebugPort.println("An error occurred initialising Bluetooth");
|
2018-10-18 09:49:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:57:21 +00:00
|
|
|
void
|
|
|
|
CBluetoothESP32Classic::check()
|
2018-10-18 09:49:14 +00:00
|
|
|
{
|
|
|
|
if(SerialBT.available()) {
|
|
|
|
char rxVal = SerialBT.read();
|
2018-10-31 18:57:21 +00:00
|
|
|
collectRxData(rxVal);
|
2018-10-18 09:49:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:57:21 +00:00
|
|
|
void
|
|
|
|
CBluetoothESP32Classic::sendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm)
|
2018-10-18 09:49:14 +00:00
|
|
|
{
|
2018-10-27 06:35:17 +00:00
|
|
|
char fullMsg[32];
|
|
|
|
|
2018-10-31 18:57:21 +00:00
|
|
|
// report to debug port
|
|
|
|
CBluetoothAbstract::sendFrame(pHdr, Frame, lineterm);
|
|
|
|
|
2018-10-27 06:35:17 +00:00
|
|
|
delay(40);
|
2018-12-08 09:31:15 +00:00
|
|
|
if(isConnected()) {
|
2018-10-20 07:11:23 +00:00
|
|
|
|
|
|
|
if(Frame.verifyCRC()) {
|
2018-11-06 09:43:54 +00:00
|
|
|
#if BT_LED == 1
|
|
|
|
digitalWrite(LED_Pin, !digitalRead(LED_Pin)); // toggle LED
|
2018-10-31 18:57:21 +00:00
|
|
|
#endif
|
2018-10-27 06:35:17 +00:00
|
|
|
int len = strlen(pHdr);
|
|
|
|
if(len < 8) {
|
|
|
|
strcpy(fullMsg, pHdr);
|
|
|
|
memcpy(&fullMsg[len], Frame.Data, 24);
|
2018-10-31 18:57:21 +00:00
|
|
|
|
2018-10-27 06:35:17 +00:00
|
|
|
SerialBT.write((uint8_t*)fullMsg, 24+len);
|
|
|
|
}
|
|
|
|
delay(10);
|
2018-10-20 07:11:23 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-10-30 07:23:22 +00:00
|
|
|
DebugPort.println("Data not sent to Bluetooth, CRC error!");
|
2018-10-20 07:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2018-10-30 07:23:22 +00:00
|
|
|
DebugPort.println("No Bluetooth client");
|
2018-11-06 09:43:54 +00:00
|
|
|
#if BT_LED == 1
|
|
|
|
digitalWrite(LED_Pin, 0);
|
2018-10-31 18:57:21 +00:00
|
|
|
#endif
|
2018-10-18 09:49:14 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-08 09:31:15 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
CBluetoothESP32Classic::isConnected()
|
|
|
|
{
|
|
|
|
return SerialBT.hasClient();
|
|
|
|
}
|
|
|
|
|
2018-10-18 09:49:14 +00:00
|
|
|
// ^
|
|
|
|
// |
|
2018-10-31 18:57:21 +00:00
|
|
|
// CLASSIC BLUETOOTH on ESP32
|
2018-10-18 09:49:14 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
2018-10-31 18:57:21 +00:00
|
|
|
// BLE on ESP32
|
2018-10-18 09:49:14 +00:00
|
|
|
// |
|
|
|
|
// V
|
|
|
|
|
2018-10-31 18:57:21 +00:00
|
|
|
/*#include <BLEDevice.h>
|
2018-10-18 09:49:14 +00:00
|
|
|
#include <BLEServer.h>
|
|
|
|
#include <BLEUtils.h>
|
|
|
|
#include <BLE2902.h>
|
|
|
|
|
|
|
|
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
|
|
|
|
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
|
|
|
|
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
|
|
|
|
|
2018-10-31 18:57:21 +00:00
|
|
|
void BLE_Send(std::string Data);*/
|
2018-10-18 09:49:14 +00:00
|
|
|
|
2018-10-31 18:57:21 +00:00
|
|
|
/*
|
2018-10-18 09:49:14 +00:00
|
|
|
BLEServer *pServer = NULL;
|
|
|
|
BLECharacteristic* pTxCharacteristic = NULL;
|
|
|
|
volatile bool deviceConnected = false;
|
|
|
|
bool oldDeviceConnected = false;
|
2018-10-31 18:57:21 +00:00
|
|
|
*/
|
2018-10-18 09:49:14 +00:00
|
|
|
|
|
|
|
class MyServerCallbacks : public BLEServerCallbacks {
|
2018-10-31 18:57:21 +00:00
|
|
|
volatile bool& _deviceConnected;
|
|
|
|
public:
|
|
|
|
MyServerCallbacks(volatile bool& devConnected) : _deviceConnected(devConnected), BLEServerCallbacks() {};
|
|
|
|
|
|
|
|
private:
|
2018-10-18 09:49:14 +00:00
|
|
|
void onConnect(BLEServer* pServer) {
|
2018-10-31 18:57:21 +00:00
|
|
|
_deviceConnected = true;
|
2018-10-18 09:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onDisconnect(BLEServer* pServer) {
|
2018-10-31 18:57:21 +00:00
|
|
|
_deviceConnected = false;
|
2018-10-18 09:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class MyCallbacks : public BLECharacteristicCallbacks {
|
2018-10-31 18:57:21 +00:00
|
|
|
CBluetoothESP32BLE* _pHost;
|
|
|
|
public:
|
|
|
|
MyCallbacks(CBluetoothESP32BLE* pHost) : BLECharacteristicCallbacks() {
|
|
|
|
_pHost = pHost;
|
|
|
|
};
|
|
|
|
private:
|
2018-10-18 09:49:14 +00:00
|
|
|
// this callback is called when the ESP WRITE characteristic has been written to by a client
|
|
|
|
// We need to *read* the new information!
|
|
|
|
void onWrite(BLECharacteristic* pCharacteristic) {
|
|
|
|
|
|
|
|
std::string rxValue = pCharacteristic->getValue();
|
|
|
|
|
|
|
|
while(rxValue.length() > 0) {
|
|
|
|
char rxVal = rxValue[0];
|
2018-10-31 18:57:21 +00:00
|
|
|
if(_pHost) _pHost->collectRxData(rxVal);
|
2018-10-18 09:49:14 +00:00
|
|
|
rxValue.erase(0, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-10-31 18:57:21 +00:00
|
|
|
CBluetoothESP32BLE::CBluetoothESP32BLE()
|
|
|
|
{
|
|
|
|
_pServer = NULL;
|
|
|
|
_pTxCharacteristic = NULL;
|
|
|
|
_deviceConnected = false;
|
|
|
|
_oldDeviceConnected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CBluetoothESP32BLE::~CBluetoothESP32BLE()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-12-12 10:37:02 +00:00
|
|
|
CBluetoothESP32BLE::begin()
|
2018-10-18 09:49:14 +00:00
|
|
|
{
|
2018-11-07 18:23:40 +00:00
|
|
|
DebugPort.println("Initialising ESP32 BLE");
|
2018-10-18 09:49:14 +00:00
|
|
|
// create the BLE device
|
|
|
|
BLEDevice::init("DieselHeater");
|
|
|
|
|
|
|
|
// create the BLE server
|
2018-10-31 18:57:21 +00:00
|
|
|
_pServer = BLEDevice::createServer();
|
|
|
|
_pServer->setCallbacks(new MyServerCallbacks(_deviceConnected));
|
2018-10-18 09:49:14 +00:00
|
|
|
|
|
|
|
// create the BLE service
|
2018-10-31 18:57:21 +00:00
|
|
|
BLEService *pService = _pServer->createService(SERVICE_UUID);
|
2018-10-18 09:49:14 +00:00
|
|
|
|
|
|
|
// create a BLE characteristic
|
2018-10-31 18:57:21 +00:00
|
|
|
_pTxCharacteristic = pService->createCharacteristic(
|
2018-10-18 09:49:14 +00:00
|
|
|
CHARACTERISTIC_UUID_TX,
|
|
|
|
BLECharacteristic::PROPERTY_NOTIFY
|
|
|
|
);
|
2018-10-31 18:57:21 +00:00
|
|
|
_pTxCharacteristic->addDescriptor(new BLE2902());
|
2018-10-18 09:49:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
BLECharacteristic* pRxCharacteristic = pService->createCharacteristic(
|
|
|
|
CHARACTERISTIC_UUID_RX,
|
|
|
|
BLECharacteristic::PROPERTY_WRITE
|
|
|
|
);
|
2018-10-31 18:57:21 +00:00
|
|
|
pRxCharacteristic->setCallbacks(new MyCallbacks(this));
|
2018-10-18 09:49:14 +00:00
|
|
|
|
|
|
|
// start the service
|
|
|
|
pService->start();
|
|
|
|
// start advertising
|
2018-10-31 18:57:21 +00:00
|
|
|
_pServer->getAdvertising()->start();
|
2018-10-30 07:23:22 +00:00
|
|
|
DebugPort.println("Awaiting a client to notify...");
|
2018-10-18 09:49:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 18:57:21 +00:00
|
|
|
void
|
|
|
|
CBluetoothESP32BLE::sendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm)
|
2018-10-18 09:49:14 +00:00
|
|
|
{
|
2018-10-31 18:57:21 +00:00
|
|
|
char fullMsg[32];
|
|
|
|
|
|
|
|
// report to debug port
|
|
|
|
CBluetoothAbstract::sendFrame(pHdr, Frame, lineterm);
|
|
|
|
|
|
|
|
delay(40);
|
2018-12-08 09:31:15 +00:00
|
|
|
if(isConnected()) {
|
2018-10-31 18:57:21 +00:00
|
|
|
|
2018-10-20 07:11:23 +00:00
|
|
|
if(Frame.verifyCRC()) {
|
2018-11-06 09:43:54 +00:00
|
|
|
#if BT_LED == 1
|
|
|
|
digitalWrite(LED_Pin, !digitalRead(LED_Pin)); // toggle LED
|
2018-10-31 18:57:21 +00:00
|
|
|
#endif
|
2018-10-20 07:11:23 +00:00
|
|
|
std::string txData = pHdr;
|
|
|
|
txData.append((char*)Frame.Data, 24);
|
2018-10-31 18:57:21 +00:00
|
|
|
|
2018-10-20 07:11:23 +00:00
|
|
|
BLE_Send(txData);
|
2018-10-31 18:57:21 +00:00
|
|
|
delay(10);
|
2018-10-20 07:11:23 +00:00
|
|
|
}
|
2018-10-31 18:57:21 +00:00
|
|
|
else {
|
|
|
|
DebugPort.println("Data not sent to Bluetooth, CRC error!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DebugPort.println("No Bluetooth client");
|
2018-11-06 09:43:54 +00:00
|
|
|
#if BT_LED == 1
|
|
|
|
digitalWrite(LED_Pin, 0);
|
2018-10-31 18:57:21 +00:00
|
|
|
#endif
|
2018-10-18 09:49:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-08 09:31:15 +00:00
|
|
|
bool
|
|
|
|
CBluetoothESP32BLE::isConnected()
|
|
|
|
{
|
|
|
|
return _deviceConnected;
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:57:21 +00:00
|
|
|
void
|
|
|
|
CBluetoothESP32BLE::check()
|
2018-10-18 09:49:14 +00:00
|
|
|
{
|
|
|
|
// disconnecting
|
2018-10-31 18:57:21 +00:00
|
|
|
if (!_deviceConnected && _oldDeviceConnected) {
|
2018-10-18 09:49:14 +00:00
|
|
|
delay(500); // give the bluetooth stack the chance to get things ready
|
2018-10-31 18:57:21 +00:00
|
|
|
_pServer->startAdvertising(); // restart advertising
|
2018-10-30 07:23:22 +00:00
|
|
|
DebugPort.println("start advertising");
|
2018-10-31 18:57:21 +00:00
|
|
|
DebugPort.println("CLIENT DISCONNECTED");
|
|
|
|
_oldDeviceConnected = _deviceConnected;
|
2018-10-18 09:49:14 +00:00
|
|
|
}
|
|
|
|
// connecting
|
2018-10-31 18:57:21 +00:00
|
|
|
if (_deviceConnected && !_oldDeviceConnected) {
|
2018-10-18 09:49:14 +00:00
|
|
|
// do stuff here on connecting
|
2018-10-31 18:57:21 +00:00
|
|
|
DebugPort.println("CLIENT CONNECTED");
|
|
|
|
_oldDeviceConnected = _deviceConnected;
|
2018-10-18 09:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// break down supplied string into 20 byte chunks (or less)
|
|
|
|
// BLE can only handle 20 bytes per packet!
|
2018-10-31 18:57:21 +00:00
|
|
|
void
|
|
|
|
CBluetoothESP32BLE::BLE_Send(std::string Data)
|
2018-10-18 09:49:14 +00:00
|
|
|
{
|
|
|
|
while(!Data.empty()) {
|
|
|
|
std::string substr = Data.substr(0, 20);
|
|
|
|
int len = substr.length();
|
2018-10-31 18:57:21 +00:00
|
|
|
_pTxCharacteristic->setValue((uint8_t*)Data.data(), len);
|
|
|
|
_pTxCharacteristic->notify();
|
2018-10-18 09:49:14 +00:00
|
|
|
Data.erase(0, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ^
|
|
|
|
// |
|
2018-10-31 18:57:21 +00:00
|
|
|
// BLE on ESP32
|
2018-10-18 09:49:14 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2018-10-27 06:35:17 +00:00
|
|
|
|
|
|
|
#endif // __ESP32__
|
2018-10-30 11:05:29 +00:00
|
|
|
|