Re: HowTo - Anwesenheitserkennung mit Bluetooth
Verfasst: 10. Mär 2020 16:14

Code: Alles auswählen
#include <WiFi.h>
#include <WiFiClient.h>
#include <BLEDevice.h>
#include <BLEScan.h>
#include <BLEAddress.h>
const char* ssid = "SSID";
const char* password = "PASSWORT";
const char* openhabIP = "10.10.80.3";
unsigned int openhabPORT = 8484;
char* BTag_01_ID = "7f:5f:88:33:d9:6e";
char* BTag_01_OH_ITEM = "BTag_01_Buero";
char* BTag_02_ID = "7f:5f:84:95:cr:19";
char* BTag_02_OH_ITEM = "BTag_02_Buero";
char* BTag_03_ID = "7f:7f:85:44:cd:3e";
char* BTag_03_OH_ITEM = "BTag_03_Buero";
int scanTime = 30;
BLEScan* pBLEScan ;
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
Serial.println("Start BLE Scanner");
BLEDevice::init("");
pBLEScan = BLEDevice::getScan();
pBLEScan->setActiveScan(true);
pBLEScan->setInterval(100);
pBLEScan->setWindow(99);
}
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void openhabRestApiPost(char* OH_VALUE, char* OH_ITEM){
if (client.connect(openhabIP, openhabPORT)) {
char buf[100];
String value = OH_VALUE;
String item = OH_ITEM;
Serial.println("Connected to REST-API");
client.println("POST /rest/items/" + item + " HTTP/1.1");
sprintf(buf, "Host: http://%s:%d", openhabIP, openhabPORT);
client.println(buf);
client.println("Accept: application/json");
client.println("Content-Length: " + String(value.length()));
client.println("Content-Type: text/plain");
client.println();
client.println(value);
Serial.print("POST -> Value: ");
Serial.print(value);
Serial.print(" to ");
Serial.print(item);
Serial.println();
}
}
void loop()
{
if (WiFi.status() != WL_CONNECTED) {
setup_wifi();
}
int j;
String resultJSON;
BLEScanResults foundDevices = pBLEScan->start(scanTime);
resultJSON = "{";
for (j = 0; j < foundDevices.getCount(); j++)
{
resultJSON = resultJSON + "\"" + foundDevices.getDevice(j).getAddress().toString().c_str() + "\":";
if (j != foundDevices.getCount() - 1) resultJSON = resultJSON + String(foundDevices.getDevice(j).getRSSI()) + ",\r\n";
else resultJSON = resultJSON + String(foundDevices.getDevice(j).getRSSI()) + "\r\n}";
}
pBLEScan->clearResults();
Serial.println(resultJSON);
if(resultJSON.indexOf(BTag_01_ID) > 0){
openhabRestApiPost("ON", BTag_01_OH_ITEM);
} else {
openhabRestApiPost("OFF", BTag_01_OH_ITEM);
}
if(resultJSON.indexOf(BTag_02_ID) > 0){
openhabRestApiPost("ON", BTag_02_OH_ITEM);
} else {
openhabRestApiPost("OFF", BTag_02_OH_ITEM);
}
if(resultJSON.indexOf(BTag_03_ID) > 0){
openhabRestApiPost("ON", BTag_03_OH_ITEM);
} else {
openhabRestApiPost("OFF", BTag_03_OH_ITEM);
}
}
Dann läuft das Script gerade und scannt.....
Code: Alles auswählen
hcitool lescan > $filename & sleep 15