Ing. Cosimo Mercuro
Published © GPL3+

Home Assistant Deep BLE Relay-Low energy

BLE relay device for Home Assistant that, for the most of time is in a very low energy state and only a few seconds per minute is awake.

IntermediateFull instructions provided2 hours3,038
Home Assistant Deep BLE Relay-Low energy

Things used in this project

Hardware components

Espressif ESP-WROOM-32
×1
AZDelivery Mini Modulo Alimentatore 220V a 5V
×1
5V Relay
×1
Texas Instruments CD40106BE
×1
Texas Instruments SN74HC74N
×1
Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
×1
LilyGO TTGO T-Internet PoE ESP32 - LAN8720A
×1
LilyGO TTGO TTL USB Serial Port Adapter for T-Internet PoE
×1

Software apps and online services

EasyEDA
JLCPCB EasyEDA

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Home Assistant Deep BLE Relay

This is the EasyEDA circuit diagram used

Gerber file

Use this file i.e. on JLCPCB to order PCB

PCB

Code

Home Assistant ESPHome lilygo-ttgo-poe-bt-proxy-5f7de4.yaml

YAML
Here the yaml file used to configure the local ESP32 client board
substitutions:
  name: lilygo-ttgo-poe-bt-proxy-5f7de4
packages:
  esphome.bluetooth-proxy: github://esphome/bluetooth-proxies/lilygo-t-eth-poe.yaml@main
esphome:
  name: ${name}
  name_add_mac_suffix: false

esp32_ble_tracker:  
  
ble_client:
  - mac_address: 34:94:54:25:26:2e
    id: ESP32_BLE_Remote
    on_connect:
         then:
           - lambda: |-
               id(ESP32_text_sensor_name2).publish_state("AWAKE");
    on_disconnect:
         then:
           - lambda: |-
               id(ESP32_text_sensor_name2).publish_state("Deep Sleep");

output:
  - platform: ble_client
    ble_client_id: ESP32_BLE_Remote
    service_uuid: '4fafc201-1fb5-459e-8fcc-c5c9c331914b'
    characteristic_uuid: 'beb5483e-36e1-4688-b7f5-ea07361b26a8'
    id: out_ble01   

  - platform: ble_client
    ble_client_id: ESP32_BLE_Remote
    service_uuid: '4fafc201-1fb5-459e-8fcc-c5c9c331914b'
    characteristic_uuid: 'cba1d466-344c-4be3-ab3f-189f80dd7518'
    id: out_ble02 
switch:

# Momentary Switch
  - platform: output
    name: "Momentary Switch LED BLU - Remote ESP32"
    id: relay
    icon: "mdi:power"
    on_turn_on:
    - delay: 3s
    - switch.turn_off: relay   
    output: out_ble01   

# Indicazione stato pin34
  - platform: gpio
    pin: 32
    name: "Stato pin 34 (ON-Off)"
    icon: "mdi:alarm-light"

text_sensor:
  - platform: template
    name: "Stato ESP32 remote"
    id: ESP32_text_sensor_name2
    icon: "mdi:arrow-up-circle"

sensor:
   
# RSSI based on MAC address
  - platform: ble_rssi
    mac_address: 34:94:54:25:26:2e
    name: "BLE ESP32 remote RSSI value"  
   
# Valore sul pin 34
  - platform: ble_client
    type: characteristic
    ble_client_id: ESP32_BLE_Remote
    name: "pin34 value"
    service_uuid: '4fafc201-1fb5-459e-8fcc-c5c9c331914b'
    characteristic_uuid: 'cba1d466-344c-4be3-ab3f-189f80dd7518'
    update_interval: 1s
   

Arduino IDE configuration for the remote ESP32 server board

Arduino
Here the configuration file that must be uploaded on the remote ESP32 server board
#include <dummy.h>

/*
    Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleWrite.cpp
    Ported to Arduino ESP32 by Evandro Copercini
*/
//This code is modified by Sachin Soni(techiesms) for ESP32 tutorial Series
// ESP32 Series :- https://www.youtube.com/playlist?list=PLruzZCuhcsGNFXfOPUfNqdhP3-7_Hh7F0

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>


// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
#define uS_TO_S_FACTOR 1000000  //Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP  56        //Time ESP32 will go to sleep (in seconds)
#define mS_TO_S_FACTOR 1000  //Conversion factor for milli seconds to seconds
#define WAKE_TIME  4        //Time ESP32 it's awake (in seconds)

RTC_DATA_ATTR int bootCount = 0;


class MyCallbacks: public BLECharacteristicCallbacks 
{
    void onWrite(BLECharacteristic *pCharacteristic) 
    {
     std::string value = pCharacteristic->getValue();
     
      if (value.length() > 0) {
        if (value[0] > 0) {
          Serial.println("Valore inviato dal BLE Client: 1");
          digitalWrite(25,HIGH);
          Serial.println(millis());
        }
      } else Serial.println("empty value");   
    }
    };

// LED Blu Characteristic and Descriptor pin34
  BLECharacteristic LedBluCharacteristics("cba1d466-344c-4be3-ab3f-189f80dd7518", BLECharacteristic::PROPERTY_NOTIFY);
  BLEDescriptor LedBluDescriptor(BLEUUID((uint16_t)0x2902));


void setup() 
{
  Serial.begin(115200);

  pinMode(25,OUTPUT);
  Serial.println("Visualizza quanto trasmesso dal BLE Client a questo BLE Server: ");
  
  //Set timer to TIME_TO_SLEEP seconds
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  // Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
  // " Seconds");
  Serial.println("ESP32 is awake for " +  String(WAKE_TIME) + " Seconds");
  Serial.println(millis());

  // Create the BLE Device 
  BLEDevice::init("MyESP32-Cosimo-New");

  // Create the BLE Server
  BLEServer *pServer = BLEDevice::createServer();

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

    
  // Set Characteristic valore da trasmettere
  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  // Create BLE Characteristics and Create a BLE Descriptor
  // valore pin34
  pService->addCharacteristic(&LedBluCharacteristics);
  LedBluDescriptor.setValue("pin34");
  LedBluCharacteristics.addDescriptor(&LedBluDescriptor);
  
  pCharacteristic->setCallbacks(new MyCallbacks());

  // Start the service 
  pService->start();

   // Start advertising
  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->start();

  // Legge il valore sul pin 34 
  float pin34 = analogRead(34);  
  static char pin34_C[7];
  dtostrf(pin34, 6, 2, pin34_C);

  //Set pin34 Characteristic value and notify connected client
  LedBluCharacteristics.setValue(pin34_C);
  LedBluCharacteristics.notify();
  Serial.print("Valore pin 34: ");
  Serial.print(pin34);
  Serial.println(""); // Carriage Return
  
  delay(WAKE_TIME * mS_TO_S_FACTOR);

  //Go to sleep now
  Serial.println(millis());
  Serial.println("ESP32 go to sleep for " + String(TIME_TO_SLEEP) + " Seconds");
  esp_deep_sleep_start();
  
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(2000);
}

Credits

Ing. Cosimo Mercuro

Ing. Cosimo Mercuro

1 project • 0 followers
Mechanical engineer. Mechatronics teacher in italian high school.

Comments