Skip to content

Instantly share code, notes, and snippets.

@sivar2311
Created August 21, 2021 06:39
Show Gist options
  • Save sivar2311/9d4398fd43172c60ac00579f69096930 to your computer and use it in GitHub Desktop.
Save sivar2311/9d4398fd43172c60ac00579f69096930 to your computer and use it in GitHub Desktop.
SinricPro BLE Keyboard example
/*
* Required libraries and their dependencies:
*
* SinricPro (https://github.com/sinricpro/esp8266-esp32-sdk)
* -> WebSockets (https://github.com/Links2004/arduinoWebSockets)
* -> ArduinoJson (https://github.com/bblanchon/ArduinoJson)
*
* ESP32-NimBLE-Keyboard (https://github.com/sivar2311/ESP32-NimBLE-Keyboard)
* -> NimBLE-Arduino (https://github.com/h2zero/NimBLE-Arduino)
*/
#include <Arduino.h>
#include <WiFi.h>
#include "BleKeyboard.h"
#include "SinricPro.h"
#include "SinricProSwitch.h"
#define WIFI_SSID ""
#define WIFI_PASS ""
#define APP_KEY ""
#define APP_SECRET ""
#define DEVICEID_1 ""
#define DEVICEID_2 ""
#define DEVICEID_3 ""
BleKeyboard bleKeyboard;
SinricProSwitch& key1 = SinricPro[DEVICEID_1];
SinricProSwitch& key2 = SinricPro[DEVICEID_2];
SinricProSwitch& key3 = SinricPro[DEVICEID_3];
bool onPowerState(const String& deviceId, bool& state) {
if (!bleKeyboard.isConnected()) return false;
if (!state) return true;
// write a single character
if (deviceId == DEVICEID_1) bleKeyboard.write('a');
// send a complete string
if (deviceId == DEVICEID_2) bleKeyboard.print("Hello world");
// send keystrokes
if (deviceId == DEVICEID_3) {
bleKeyboard.press(KEY_LEFT_CTRL);
bleKeyboard.press(KEY_LEFT_ALT);
bleKeyboard.press(KEY_DELETE);
delay(100);
bleKeyboard.releaseAll();
}
return true;
}
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(false);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
Serial.printf(".");
delay(250);
}
key1.onPowerState(onPowerState);
key2.onPowerState(onPowerState);
key3.onPowerState(onPowerState);
SinricPro.begin(APP_KEY, APP_SECRET);
bleKeyboard.begin();
}
void loop() {
SinricPro.handle();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment