-
Notifications
You must be signed in to change notification settings - Fork 2
/
MQTT_BLE_Controller.ino
182 lines (136 loc) · 4.43 KB
/
MQTT_BLE_Controller.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <WiFi.h>
#include <PubSubClient.h>
#include "BLEDevice.h"
#include <stdlib.h>
static BLEUUID serviceUUID("0000fe50-0000-1000-8000-00805f9b34fb");
static BLEUUID charUUID("0000fe51-0000-1000-8000-00805f9b34fb");
const char* ssid = "XXXX";
const char* password = "XXXX";
const char* mqtt_server = "XXX.XXX.XXX.XXX";
static boolean doConnect = false;
static boolean connected = false;
static boolean doScan = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLEAdvertisedDevice* myDevice;
WiFiClient espClient;
PubSubClient client(espClient);
static void notifyCallback(
BLERemoteCharacteristic* pBLERemoteCharacteristic,
uint8_t* pData,
size_t length,
bool isNotify) {
String message = "";
char cmessage[length * 2 + 1];
Serial.print("Notify callback for characteristic ");
Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
Serial.print(" of data length ");
Serial.println(length);
for (int i = 0; i < length; i++) {
pData = pData + i; //Pointer + (2*i)bytes
if (String(*pData, HEX).length() == 1) {
message += "0" + String(*pData, HEX);
}
else {
message += String(*pData, HEX);
}
pData = pData - i;
}
Serial.println(message);
message.toUpperCase();
message.toCharArray(cmessage, length * 2 + 1);
//Notification filter and publish
String id = message.substring(2, 4);
if (id == "A2" || id == "A7" || id == "11" || id == "22" || id == "17") {
client.publish("am43/notifications", cmessage);
}
}
class MyClientCallback : public BLEClientCallbacks {
void onConnect(BLEClient* pclient) {
}
void onDisconnect(BLEClient* pclient) {
connected = false;
Serial.println("onDisconnect");
}
};
bool connectToServer() {
BLEClient* pClient = BLEDevice::createClient();
pClient->setClientCallbacks(new MyClientCallback());
pClient->connect(myDevice);
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
pRemoteCharacteristic->registerForNotify(notifyCallback);
connected = true;
return true;
}
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) {
BLEDevice::getScan()->stop();
myDevice = new BLEAdvertisedDevice(advertisedDevice);
doConnect = true;
doScan = true;
}
}
};
void setup() {
Serial.begin(115200);
//WiFi connection
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println("WiFi Connected");
//MQTT connection
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
if (client.connect("espClient") && client.subscribe("am43/commands")) {
Serial.println("MQTT Connected");
}
//Bluetooth connection
BLEDevice::init("");
BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setInterval(1349);
pBLEScan->setWindow(449);
pBLEScan->setActiveScan(true);
pBLEScan->start(5, false);
}
void callback(char* topic, byte* payload, unsigned int length) {
String message;
for (int i = 0; i < length; i++) {
message += (char)payload[i];
}
String id = message.substring(2, 4);
char cmessage[3];
byte command[message.length() / 2];
for (int i = 0; i < sizeof(command); i++) {
message.substring(2 * i, 2 * i + 2).toCharArray(cmessage, 3);
command[i] = strtol(cmessage, NULL, 16);
}
pRemoteCharacteristic->writeValue(command, sizeof(command));
}
void reconnect() {
while (!client.connected()) {
client.connect("espClient");
client.subscribe("am43/commands");
delay(1000);
}
}
void loop() {
if (doConnect == true) {
if (connectToServer()) {
Serial.println("We are now connected to the BLE Server.");
} else {
Serial.println("We have failed to connect to the server; there is nothin more we will do.");
}
doConnect = false;
}
if (connected) {
if (!client.connected()) {
reconnect();
}
client.loop();
} else if (doScan) {
BLEDevice::getScan()->start(0);
}
}