Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ATOM LiteをBLEキーボードにして,特定の文字列を送付する。
/*
* ATOM LiteをBLEキーボードにして,特定の文字列を送付する。
*
* 例えば,決まったWi-FiのSSIDに対するパスワードを送るといった用途に使えます。
*
*/
#include <BleKeyboard.h> // https://github.com/T-vK/ESP32-BLE-Keyboard
#include <M5Atom.h>
const std::string SEND_STRING("Hello World!"); // 送りたい文字列
const std::string DEVICE_NAME("ATOM Lite Send String");
const std::string DEVICE_MANUFACTURER("M5Stack");
const CRGB CRGB_BLE_CONNECTED(0x00, 0x00, 0xf0);
// 数値は緑だが Lite では赤く光る https://github.com/m5stack/M5Atom/issues/5
const CRGB CRGB_BLE_DISCONNECTED(0x00, 0xf0, 0x00);
const bool ENABLE_SERIAL = true;
const bool ENABLE_I2C = false;
const bool ENABLE_DISPLAY = true;
BleKeyboard bleKeyboard(DEVICE_NAME, DEVICE_MANUFACTURER);
bool isBleConnected = false;
void setup() {
bleKeyboard.begin();
M5.begin(ENABLE_SERIAL, ENABLE_I2C, ENABLE_DISPLAY);
M5.dis.drawpix(0, CRGB_BLE_DISCONNECTED);
}
void loop() {
M5.update();
if (bleKeyboard.isConnected()) {
if (!isBleConnected) {
M5.dis.drawpix(0, CRGB_BLE_CONNECTED);
isBleConnected = true;
Serial.println("Connected");
}
if (M5.Btn.wasPressed()) {
bleKeyboard.write((uint8_t *)SEND_STRING.c_str(), SEND_STRING.length());
Serial.print("Send String: ");
Serial.println(SEND_STRING.c_str());
}
} else {
if (isBleConnected) {
M5.dis.drawpix(0, CRGB_BLE_DISCONNECTED);
isBleConnected = false;
Serial.println("Disconnected");
}
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment