Skip to content

Instantly share code, notes, and snippets.

@alteist
Created May 1, 2020 09:29
Show Gist options
  • Save alteist/c7f98f08223a6cc4c0b042ca34884c90 to your computer and use it in GitHub Desktop.
Save alteist/c7f98f08223a6cc4c0b042ca34884c90 to your computer and use it in GitHub Desktop.
/**
iTAG service - press button detection by notify
original author:
https://github.com/krzyspx/ESP32_BLE_iTAG_press_detection/blob/master/BLE_client_iTAG_noti_ver3.ino/BLE_client_iTAG_noti_ver3.ino.ino
http://100-x-arduino.blogspot.com/
use "nRF Connect" app for Android to discover tagAddress, serviceUUID and charUUIDs
http://100-x-arduino.blogspot.com/2018/05/itag-ble-jak-je-odczytac.html
http://100-x-arduino.blogspot.com/2018/05/itag-i-arduino-ide-czy-esp32-otworzy.html
http://100-x-arduino.blogspot.com/2018/05/esp32-ble-i-poprawione-arduino-ide.html
http://100-x-arduino.blogspot.com/2018/05/esp32-ble-itag-nowy-rozdzia-iot.html
Arduino IDE 1.8.5
*/
//#include <WiFi.h>
#include "BLEDevice.h"
const int ledPin = 2;
//WiFiClient wclient;
//const char* ssid = "";
//const char* pass = "";
//const uint16_t port = 5000;
const char * host = "192.168.1.2"; ip or dns
const char* tagAddress = "18:7a:93:02:97:aa";
static BLEUUID serviceUUID("0000FFF0-0000-1000-8000-00805F9B34FB"); // The remote service we wish to connect to.
static BLEUUID charUUID("0000FFF1-0000-1000-8000-00805F9B34FB"); // The characteristic of the remote service for listening to button presses
static BLEUUID charUUID2("0000FFF2-0000-1000-8000-00805F9B34FB"); // The characteristic of the remote service for sending sounds
static BLEAddress *pServerAddress;
static boolean doConnect = false;
static boolean connected = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLERemoteCharacteristic* pRemoteCharacteristic2;
bool deviceBleConnected = false;
bool serverfound = false;
bool emitSound = true;
int buttonHoldCount = 0;
int buttonTimer = 0;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.println(); Serial.print("BLE Advertised Device found: "); Serial.println(advertisedDevice.toString().c_str());
advertisedDevice.getScan()->stop();
pServerAddress = new BLEAddress(advertisedDevice.getAddress());
serverfound = true;
Serial.print("Found our device! ");
} // onResult
}; // MyAdvertisedDeviceCallbacks
// not used in this sketch:
void scanBLEservers() {
serverfound = false;
BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
pBLEScan->start(1);
}
class MyClientCallbacks: public BLEClientCallbacks {
void onConnect(BLEClient *pClient) {
deviceBleConnected = true; // set ble connected flag
Serial.println("connected to BLE server");
};
void onDisconnect(BLEClient *pClient) {
// serverfound = false;
// pClient->disconnect();
deviceBleConnected = false; clear ble connected flag
// connected = false;
Serial.println("disconnected from BLE server, restarting");
ESP.restart();
}
};
static void notifyCallback(
BLERemoteCharacteristic* pBLERemoteCharacteristic,
uint8_t* pData,
size_t length,
bool isNotify) {
// Serial.println(); Serial.println("button pressed"); Serial.print(" length "); Serial.println(length);
buttonHoldCount = buttonHoldCount + 1;
buttonTimer = 3;
}
bool connectToServer(BLEAddress pAddress) {
BLEClient* pClient = BLEDevice::createClient(); //Serial.println(" Created client");
pClient->setClientCallbacks(new MyClientCallbacks()); Serial.println(" Set Callbacks");
pClient->connect(pAddress); Serial.println(" Connected to server");
int ifconn = pClient->isConnected();
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
if (pRemoteService == nullptr) {
Serial.print("Failed to find our service UUID: "); return false;
}
Serial.println("Found service " + String(pRemoteService->toString().c_str())); Serial.println(pRemoteService->toString().c_str());
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
pRemoteCharacteristic2 = pRemoteService->getCharacteristic(charUUID2);
if (pRemoteCharacteristic == nullptr) {
Serial.print("Failed to find our characteristic UUID: "); return false;
}
Serial.println("Found characteristic" + String(pRemoteCharacteristic->toString().c_str()));
std::string value = pRemoteCharacteristic->readValue(); Read the value of the characteristic.
// Serial.print("Read characteristic value: "); Serial.println(value.c_str());
const uint8_t bothOff[] = {0x0, 0x0};
const uint8_t notificationOn[] = {0x1, 0x0};
const uint8_t indicationOn[] = {0x2, 0x0};
const uint8_t bothOn[] = {0x3, 0x0};
pRemoteCharacteristic->getDescriptor(BLEUUID((uint16_t)0x2902))->writeValue((uint8_t*)notificationOn, 2, true); //notification ON
//Serial.println("Notification ON ");
pRemoteCharacteristic->registerForNotify(notifyCallback); // Callback function when notification
Serial.println("Notification Callback Activated");
}
// sends requests and gets current status over Wi-Fi
//void makeRequest(int type) {
// Serial.print("Connecting to ");
// Serial.println(host);
//
Use WiFiClient class to create TCP connections
// WiFiClient client;
//
// if (!client.connect(host, port)) {
// Serial.println("Connection failed.");
// Serial.println("Waiting 5 seconds before retrying...");
// delay(5000);
// return;
// }
//
This will send a request to the server
uncomment this line to send an arbitrary string to the server
client.print("Send this data to the server");
uncomment this line to send a basic document request to the server
// if (type == 0) {
// client.print("GET / HTTP/1.1\n\n");
// }
// if (type == 1) {
// client.print("GET /toggle HTTP/1.1\n\n");
// }
//
// int maxloops = 0;
//
wait for the server's reply to become available
// while (!client.available() && maxloops < 1000)
// {
// maxloops++;
delay(1); delay 1 msec
// }
// String line;
// if (client.available() > 0)
// {
// line = client.readStringUntil('[');
// line = client.readStringUntil(']');
// if (line == "active") {
// Serial.println("active!");
// } else if (line == "inactive") {
// Serial.println("inactive!");
// }
// }
// else
// {
// Serial.println("client.available() timed out ");
// }
// Serial.println("Closing connection.");
// client.stop();
//}
void setup() {
Serial.begin(115200);
// if (WiFi.status() != WL_CONNECTED) {
// Serial.print("Connecting to ");
// Serial.print(ssid);
// Serial.println("...");
// WiFi.begin(ssid, pass);
// if (WiFi.waitForConnectResult() != WL_CONNECTED)
// ESP.restart();
// Serial.println("WiFi connected");
// }
pServerAddress = new BLEAddress(tagAddress);
BLEDevice::init(""); Serial.println("Starting Arduino BLE Client application...");
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
} // End of setup.
void loop() {
if (connected == false) {
if (connectToServer(*pServerAddress)) {
connected = true; Serial.println("Server UP");
digitalWrite(ledPin, HIGH);
} else {
Serial.println("Server DOWN");
deviceBleConnected = false;
emitSound = true;
digitalWrite(ledPin, LOW);
}
} else {
if (deviceBleConnected) {
Serial.print("BLE device connected");
if (emitSound) {
pRemoteCharacteristic2->writeValue({0xAA,0x03,0x03,0x11,0x11}, 5);
emitSound = false;
}
if (buttonHoldCount > 2) {
Serial.println("long press");
pRemoteCharacteristic2->writeValue({0xAA,0x03,0x03,0x11,0x11}, 5);
pRemoteCharacteristic2->writeValue({0xAA,0x03,0x03,0x33,0x33}, 5);
buttonHoldCount = 0;
buttonTimer = 0;
//makeRequest(1);
}
if (buttonHoldCount > 0 and buttonTimer <= 0) {
Serial.println("short press");
pRemoteCharacteristic2->writeValue({0xAA,0x03,0x02,0x11,0x99}, 5);
buttonHoldCount = 0;
buttonTimer = 0;
//makeRequest(0);
}
}
}
delay(333);
if (buttonTimer < 1) {
buttonTimer = 0;
} else {
buttonTimer = buttonTimer - 1;
}
Serial.print("buttonHoldCount: "); Serial.print(buttonHoldCount);
Serial.print(", buttonTimer: "); Serial.println(buttonTimer);
} // End of loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment