-
Notifications
You must be signed in to change notification settings - Fork 0
/
BLE_MQTT_new.ino
120 lines (94 loc) · 3.63 KB
/
BLE_MQTT_new.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
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/
//#include <ArduinoJson.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
const char* ssid = "Nokia 8";
const char* password = "12345678a";
const char* mqtt_server = "mqtt.eclipse.org";
String info="{\"id\" : 8 ";// json message chr sequence
WiFiClient espClient;
PubSubClient client(espClient);
int scanTime = 5; //In seconds
BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
//Serial.printf("Advertised Device: %s", advertisedDevice.getName());
//Serial.printf("strength of device %s is: %d \n", advertisedDevice.getAddress().toString().c_str(), advertisedDevice.getRSSI());
//String addesss=(String)advertisedDevice.getRSSI();
//String info = "strength of device " + (String)advertisedDevice.getAddress().toString().c_str()+ " is" + (String)advertisedDevice.getRSSI();
info+=",\""+(String)advertisedDevice.getAddress().toString().c_str()+ "\":" + String(advertisedDevice.getRSSI()).c_str();
//String x = (String)*advertisedDevice.getAddress().toString().c_str();
}
};
void reconnect() { //if MQTT connection disconnected reconnect using this func
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
client.subscribe("EN3250/ESP32");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
Serial.println("Scanning...");
Serial.println();
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.println("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(mqtt_server, 1883);
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
info+="}";
//info="{\"id\" :1,\"UoM_Wireless1\"1:-56,\"UoM_Wireless2\":-97}";
Serial.println(info);
char msgCharArray[5000];
info.toCharArray(msgCharArray,5000);
client.publish("EN3250/ESP32",info.c_str());
info="{\"id\" : 8 ";
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
// Serial.println(info);
Serial.println("Scan Done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(2000);
// client.publish("group1ENTC","published");
}