-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHappy_Bubble_ESP32_Node.ino
170 lines (141 loc) · 5.27 KB
/
Happy_Bubble_ESP32_Node.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
#include <WiFi.h>
#include <PubSubClient.h>
#include "Settings.h"
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <ArduinoJson.h>
int scanDuration = 10; //In seconds
#define base_topic "happy-bubbles/ble/" room "/raw/"
WiFiClient espClient;
PubSubClient client(espClient);
void printDeviceInfo(BLEAdvertisedDevice advertisedDevice) {
Serial.print("---------------- Found new device ------------------");
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
Serial.print("Address :"); Serial.println(advertisedDevice.getAddress().toString().c_str());
if (advertisedDevice.haveRSSI()) {
Serial.print("RSSI : "); Serial.println(advertisedDevice.getRSSI());
}
if (advertisedDevice.haveTXPower()) {
Serial.print("TX Power : "); Serial.println(advertisedDevice.getTXPower());
}
if (advertisedDevice.haveName()) {
Serial.print("Device name : "); Serial.println(advertisedDevice.getName().c_str());
}
if (advertisedDevice.haveManufacturerData()) {
Serial.print("ManufacturerData : "); Serial.println(advertisedDevice.getManufacturerData().c_str());
}
if (advertisedDevice.haveServiceUUID()) {
Serial.print("ServiceUUID : "); Serial.println(advertisedDevice.getServiceUUID().toString().c_str());
}
}
void sendToMqtt(BLEAdvertisedDevice advertisedDevice) {
int rssi = advertisedDevice.getRSSI();
BLEAddress address = advertisedDevice.getAddress();
String macTransformed = String(address.toString().c_str());
//cc : 40 : 80 : ba : 9f : 7a
//01 2 34 5 67 8 910 11 1213 14 1516
macTransformed.remove(14, 1);
macTransformed.remove(11, 1);
macTransformed.remove(8, 1);
macTransformed.remove(5, 1);
macTransformed.remove(2, 1);
/*
{
"hostname": "living-room",
"mac": "001060AA36F8",
"rssi": -94,
"is_scan_response": "0",
"type": "3",
"data": "0201061aff4c000215e2c56db5dffb48d2b060d0f5a71096e000680068c5"
}
*/
StaticJsonBuffer<500> JSONbuffer;
JsonObject& JSONencoder = JSONbuffer.createObject();
JSONencoder["hostname"] = "living-room";
JSONencoder["mac"] = macTransformed.c_str();
JSONencoder["rssi"] = rssi;
JSONencoder["is_scan_response"] = "0";
JSONencoder["type"] = "3";
JSONencoder["data"] = "";//"0201061aff4c000215e2c56db5dffb48d2b060d0f5a71096e000680068c5";
char JSONmessageBuffer[500];
JSONencoder.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
//happy-bubbles/ble/<the-hostname>/raw/<the-bluetooth-MAC-address
String publishTopic = String(base_topic) + macTransformed.c_str();
if (client.publish(publishTopic.c_str(), JSONmessageBuffer) == true) { //TODO base_topic + mac_address
Serial.print("Success sending message to topic: "); Serial.println(publishTopic);
Serial.print("Message: "); Serial.println(JSONmessageBuffer);
} else {
Serial.print("Error sending message: "); Serial.println(publishTopic);
Serial.print("Message: "); Serial.println(JSONmessageBuffer);
}
}
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
printDeviceInfo(advertisedDevice);
sendToMqtt(advertisedDevice);
}
};
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println(base_topic);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
client.setServer(mqttServer, mqttPort);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP32Client", mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
if (client.publish("esp/test", "Hello from ESP32") == true) { //TODO base_topic + mac_address
Serial.println("Success sending message to topic");
} else {
Serial.println("Error sending message");
}
BLEDevice::init("");
Serial.println("Finished setup");
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
// If you do not want to use a username and password, change next line to
// if (client.connect("ESP8266Client")) {
if (client.connect("ESP32Client", mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
Serial.println("New loop");
// put your main code here, to run repeatedly:
if (!client.connected()) {
reconnect();
}
client.loop();
BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
BLEScanResults foundDevices = pBLEScan->start(scanDuration);
Serial.println("");
Serial.print("Total devices found: "); Serial.println(foundDevices.getCount());
Serial.println("Scan done!");
}