Skip to content

Instantly share code, notes, and snippets.

@iomonad
Last active May 31, 2024 09:22
Show Gist options
  • Save iomonad/f6e4cab2581eb7f2d2126eaf792f33e6 to your computer and use it in GitHub Desktop.
Save iomonad/f6e4cab2581eb7f2d2126eaf792f33e6 to your computer and use it in GitHub Desktop.
ESP32_RELAY_X1 over BLE
/* -*- mode: c++ -*-
*
* Copyright 2024 iomonad <iomonad@riseup.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#pragma once
#define SERVICE_UUID "c0ffeeee-1337-1337-1337-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
// See: https://www.bluetooth.com/wp-content/uploads/Files/Specification/HTML/Assigned_Numbers/out/en/Assigned_Numbers.pdf?v=1716982826526
// Stealth car shit
#define RELAY_CHARACTERISTIC_UUID 0x0A88
#define RELAY_GPIO_PORT 0x10
#define BLE_MAGIC_SWITCH_ON "K7v3JnhfL5Ar9n6"
#define BLE_MAGIC_SWITCH_OFF "QNrGcirdP374uY5"
/* -*- mode: c++ -*-
*
* Copyright 2024 iomonad <iomonad@riseup.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "esp_err.h"
#include "esp_check.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <BLERemoteCharacteristic.h>
#include <EEPROM.h>
#include "esp32_x1_relay.hpp"
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled on the ESP32
#endif
static BLEServer *ble_server = NULL;
static BLECharacteristic* pCharacteristic = NULL;
static BLECharacteristic* relayCharacteristic = NULL;
static bool relay_enabled = false;
static bool isConnected = false;
static SemaphoreHandle_t ble_relay_sem;
/* _____ __ .__ .__ __ */
/* / _ \ _____/ |_|__|__ _|__|/ |_ ___.__. */
/* / /_\ \_/ ___\ __\ \ \/ / \ __< | | */
/* / | \ \___| | | |\ /| || | \___ | */
/* \____|__ /\___ >__| |__| \_/ |__||__| / ____| */
/* \/ \/ \/ */
class BLECallbacks : public BLEServerCallbacks {
void onConnect(BLEServer *pServer) {
printf("ble: new connection open\n");
isConnected = true;
};
void onDisconnect(BLEServer *pServer) {
printf("ble: device disconnected\n");
isConnected = false;
}
};
class RelayCallback : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic* ledCharacteristic) {
const std::string rxValue = ledCharacteristic->getValue();
if (xSemaphoreTake(ble_relay_sem, portMAX_DELAY) && isConnected) {
if (rxValue == BLE_MAGIC_SWITCH_ON) {
EEPROM.write(0x00, 0xFF);
digitalWrite(RELAY_GPIO_PORT, HIGH);
}
if (rxValue == BLE_MAGIC_SWITCH_OFF) {
EEPROM.write(0x00, 0x00);
digitalWrite(RELAY_GPIO_PORT, LOW);
}
} else {
printf("err: write on disconnect or semaphore timeout\n");
}
/* Release sem */
xSemaphoreGive(ble_relay_sem);
}
};
static void ble_server_lifecycle(void *_params) {
for (;;) {
if (!isConnected) {
vTaskDelay(pdMS_TO_TICKS(500));
ble_server->startAdvertising();
printf("ble: advertising\n");
}
vTaskDelay(pdMS_TO_TICKS(10000));
}
};
/* ___________ __ .__ __ */
/* \_ _____/ _____/ |________ ___.__.______ ____ |__| _____/ |_ */
/* | __)_ / \ __\_ __ < | |\____ \ / _ \| |/ \ __\ */
/* | \ | \ | | | \/\___ || |_> > <_> ) | | \ | */
/* /_______ /___| /__| |__| / ____|| __/ \____/|__|___| /__| */
/* \/ \/ \/ |__| \/ */
void setup () {
Serial.begin(115200);
int relay_status = EEPROM.read(0x00);
pinMode(RELAY_GPIO_PORT, OUTPUT);
digitalWrite(RELAY_GPIO_PORT, (relay_status == 0x00) ? LOW : HIGH);
BLEDevice::init("st_pierre");
ble_server = BLEDevice::createServer();
ble_server->setCallbacks(new BLECallbacks());
ble_relay_sem = xSemaphoreCreateBinary();
xSemaphoreGive(ble_relay_sem);
BLEService *ble_svc = ble_server->createService(SERVICE_UUID);
pCharacteristic = ble_svc->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE);
relayCharacteristic = ble_svc->createCharacteristic(
(uint16_t) RELAY_CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_INDICATE);
relayCharacteristic->setCallbacks(new RelayCallback());
/* Start SVC server */
ble_svc->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0);
BLEDevice::startAdvertising();
xTaskCreate(ble_server_lifecycle, "ble_mgmt_task", 10000, NULL, 1, NULL);
}
void loop () {}
build:
arduino-cli compile -v -p /dev/ttyUSB0 --fqbn esp32:esp32:esp32
flash:
arduino-cli compile -v -p /dev/ttyUSB0 --fqbn esp32:esp32:esp32 -u
setup:
arduino-cli core install esp32:esp32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment