Initial commit: BLE relay controller for Atom VoiceS3R
This commit is contained in:
commit
7c9dde172a
15 changed files with 764 additions and 0 deletions
122
src/main.cpp
Normal file
122
src/main.cpp
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
#include <Arduino.h>
|
||||
#include <BLEDevice.h>
|
||||
#include <BLEServer.h>
|
||||
#include <BLEUtils.h>
|
||||
#include <BLE2902.h>
|
||||
|
||||
#define SENSOR_PIN 38
|
||||
#define RELAY_PIN 1
|
||||
#define LED_PIN 46
|
||||
|
||||
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
|
||||
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361f26d8"
|
||||
|
||||
BLECharacteristic *pCharacteristic;
|
||||
bool deviceConnected = false;
|
||||
bool bleCommand = false;
|
||||
bool relayState = false;
|
||||
|
||||
class MyServerCallbacks : public BLEServerCallbacks {
|
||||
void onConnect(BLEServer *pServer) override {
|
||||
deviceConnected = true;
|
||||
Serial.println("BLE: device connected");
|
||||
}
|
||||
void onDisconnect(BLEServer *pServer) override {
|
||||
deviceConnected = false;
|
||||
bleCommand = false;
|
||||
Serial.println("BLE: device disconnected");
|
||||
BLEDevice::startAdvertising();
|
||||
}
|
||||
};
|
||||
|
||||
class MyCharacteristicCallbacks : public BLECharacteristicCallbacks {
|
||||
void onWrite(BLECharacteristic *pChar) override {
|
||||
std::string value = pChar->getValue();
|
||||
if (value.length() > 0) {
|
||||
uint8_t cmd = (uint8_t)value[0];
|
||||
if (cmd == 0x01) {
|
||||
bleCommand = true;
|
||||
Serial.println("BLE: relay ON");
|
||||
} else {
|
||||
bleCommand = false;
|
||||
Serial.println("BLE: relay OFF");
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void setup() {
|
||||
delay(2000);
|
||||
Serial.begin(115200);
|
||||
delay(500);
|
||||
Serial.println("=== Atom VoiceS3R BLE Relay ===");
|
||||
|
||||
pinMode(SENSOR_PIN, INPUT_PULLUP);
|
||||
pinMode(RELAY_PIN, OUTPUT);
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
|
||||
// Visual confirmation: blink LED 3x before BLE init
|
||||
for (int i = 0; i < 3; i++) {
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
delay(200);
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
delay(200);
|
||||
}
|
||||
Serial.println("LED blink done, starting BLE...");
|
||||
|
||||
BLEDevice::init("AtomS3R-Relay");
|
||||
BLEDevice::setMTU(23);
|
||||
BLEServer *pServer = BLEDevice::createServer();
|
||||
pServer->setCallbacks(new MyServerCallbacks());
|
||||
|
||||
BLEService *pService = pServer->createService(SERVICE_UUID);
|
||||
pCharacteristic = pService->createCharacteristic(
|
||||
CHARACTERISTIC_UUID,
|
||||
BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY
|
||||
);
|
||||
pCharacteristic->addDescriptor(new BLE2902());
|
||||
pCharacteristic->setCallbacks(new MyCharacteristicCallbacks());
|
||||
pCharacteristic->setValue("0");
|
||||
pService->start();
|
||||
|
||||
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
|
||||
pAdvertising->addServiceUUID(SERVICE_UUID);
|
||||
pAdvertising->setScanResponse(true);
|
||||
pAdvertising->setMinPreferred(0x06);
|
||||
pAdvertising->setMinPreferred(0x12);
|
||||
BLEDevice::startAdvertising();
|
||||
|
||||
Serial.println("BLE: advertising started as 'AtomS3R-Relay'");
|
||||
Serial.println("Setup done.");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
int sensorState = digitalRead(SENSOR_PIN);
|
||||
|
||||
bool touchActive = (sensorState == LOW);
|
||||
bool bleActive = (deviceConnected && bleCommand);
|
||||
|
||||
relayState = touchActive || bleActive;
|
||||
|
||||
digitalWrite(RELAY_PIN, relayState ? HIGH : LOW);
|
||||
|
||||
static bool lastRelay = false;
|
||||
if (relayState != lastRelay) {
|
||||
Serial.printf("Relay %s (touch=%s, ble=%s)\n",
|
||||
relayState ? "ON" : "OFF",
|
||||
sensorState == LOW ? "YES" : "no",
|
||||
deviceConnected ? "connected" : "disconnected");
|
||||
lastRelay = relayState;
|
||||
}
|
||||
|
||||
static uint32_t heartbeat = 0;
|
||||
if (millis() - heartbeat > 5000) {
|
||||
Serial.printf("[heartbeat] relay=%s touch=%s ble=%s\n",
|
||||
relayState ? "ON" : "OFF",
|
||||
sensorState == LOW ? "YES" : "no",
|
||||
deviceConnected ? "connected" : "disconnected");
|
||||
heartbeat = millis();
|
||||
}
|
||||
|
||||
delay(50);
|
||||
}
|
||||
Loading…
Reference in a new issue