Code Monkey home page Code Monkey logo

bthomev2-esp32-example's Introduction

BTHomeV2 ESP32 Example

V2 of the example, simplified for least input

An BTHome v2 example. Make your DIY sensor, BTHome compatible Thanks @countrysideboy for his contributions on Encryption and cleanup code, the most of the code is from his side!

This is an example for a DIY BTHome v2 sensor.

The original code is from: https://github.com/TheDigital1/ESP32_BTHome

The header file contains human readable variables for the hex Object ids.

Sending entities to Home Assistant is simplified.

  1. Download the files from Arduino Code folder.

  2. Edit the BTHome.ino in your favourite Arduino IDE

  3. Change the DEVICE_NAME to the one that should be recognised in BTHome integration

  4. Change the BIND_KEY with your preference or remove it along with the #define ENABLE_ENCRYPT for no encryption

  5. Add the object id of the individual entities and the state of it as in the examples

  6. Compile and upload to ESP32

DONE!

Read the comments in the code for more info. For the object ids consider using the variables from BTHome.h instead.

bthomev2-esp32-example's People

Contributors

bvweerd avatar chreece avatar countrysideboy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

bthomev2-esp32-example's Issues

BTHome device is not sending object ids in numerical order (from low to high object id)

Logger: bthome_ble.parser
Source: components/bthome/__init__.py:44
First occurred: 13:37:51 (6 occurrences)
Last logged: 13:38:30

BTHome device is not sending object ids in numerical order (from low to high object id). This can cause issues with your BTHome receiver, payload: 05d913004a010904f28f0102ac0d
BTHome device is not sending object ids in numerical order (from low to high object id). This can cause issues with your BTHome receiver, payload: 03a00f135e0112b804100121002501

Measurements:

void loop() {
  //MEASUREMENT_MAX_LEN = 23, ENABLE_ENCRYPT will use extra 8 bytes, so each Measurement should smaller than 15

  // 1st method: just addMeasurement as much as you can, the code will split and send the adv packet automatically
  // each adv packet sending lasts for 1500ms
  bthome.resetMeasurement();
  // bthome.addMeasurement(sensorid, value) you can use the sensorids from the BTHome.h file
  bthome.addMeasurement(ID_ILLUMINANCE, 50.81f);
  bthome.addMeasurement(ID_VOLTAGE1, 230.5f);
  bthome.addMeasurement(ID_PRESSURE, 1023.86f);
  bthome.addMeasurement(ID_TEMPERATURE_PRECISE, 35.00f);
  bthome.addMeasurement(ID_HUMIDITY_PRECISE, 40.00f);
  bthome.addMeasurement(ID_TVOC, (uint64_t)350);
  bthome.addMeasurement(ID_CO2, (uint64_t)1208);
  bthome.addMeasurement_state(STATE_POWER_ON, STATE_ON);
  bthome.addMeasurement_state(STATE_MOTION, STATE_OFF);
  bthome.addMeasurement_state(STATE_PRESENCE, STATE_ON);
  bthome.sendPacket();
  bthome.stop();

  delay(10000);
}

BTHome.cpp:7:6: error: no declaration matches 'void BTHome::begin(String, bool, const uint8_t*, bool)'

Hi !
First, thank you for the code.
I'm trying to use this, but getting the following error:

/home/feiticeir0/Documents/Arduino/BTHome/BTHome.ino: In function 'void setup()':
/home/feiticeir0/Documents/Arduino/BTHome/BTHome.ino:24:27: error: call of overloaded 'begin(const char [8])' is ambiguous
   bthome.begin(DEVICE_NAME);
                           ^
In file included from /home/feiticeir0/Documents/Arduino/BTHome/BTHome.ino:11:
/home/feiticeir0/Documents/Arduino/BTHome/BTHome.h:117:10: note: candidate: 'void BTHome::begin(String, bool, const uint8_t*, bool)'
     void begin(String dname = "DIY-sensor", bool encryption = false, uint8_t const* const key = NULL, bool trigger_based_device = false);
          ^~~~~
/home/feiticeir0/Documents/Arduino/BTHome/BTHome.h:118:10: note: candidate: 'void BTHome::begin(String, bool, String, bool)'
     void begin(String dname = "DIY-sensor", bool encryption = false, String key = "", bool trigger_based_device = false);
          ^~~~~

exit status 1

Compilation error: call of overloaded 'begin(const char [8])' is ambiguous

Per instructions, without encryption, I've commented out DEFINE_ENCRYPTION and BIND_KEY...

The line that gets red in the editor is this one:

bthome.begin(DEVICE_NAME);

Thank you

High Power Consumption in Deep Sleep

Hi all!

I am building a simple water sensor that reads a water level from a touch PIN, packs the result into a BLEAdvertisement and goes into deep sleep.
However, during deep sleep the consumption is at 90mA which is way to high.
Without the bthome.begin(DEVICE_NAME, false, "", false); call, I am at 10.5 mA consumption in deep sleep, so initializing the BLE device seems to be the issue.
I already tried a few things to disable BLE before going to deep sleep (see below), but nothing seems to work. Any ideas?

Code:

/*
   Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
   Ported to Arduino ESP32 by pcbreflux
   Modified to work with NimBLE
   Modified for V2 by Chreece
   Modified by countrysideboy: Code cleanups, Chop Data, Encryption
   BLE advertisement format from https://bthome.io/

*/

#include "BTHome.h"
#include "NimBLEDevice.h"

#define DEVICE_NAME "Wassersensor"  // The name of the sensor
#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 28800

#define NO_WATER 0
#define FEW_WATER 1
#define HALF_FULL_WATER 2
#define FULL_WATER 3


// Pin 32 is close to the bottom of the eimer
// Pin 15 at 50%
// Pin 4 at the top
const int touchPins[] = {4, 15, 32};
const int touchThreshold = 10;

int touchValue = 0;
int waterLevel = 0;
int batteryAnalogValue = 0;
float batteryVoltage = 0;

BTHome bthome;

int getWaterStatus(int pin){
  touchValue = touchRead(pin);
  //voltageMeasured = analogRead(buttonPin);

  if (touchValue <= touchThreshold){
     // Water detected
      return 1;
  } else {
      // Not enough water
     return 0;
  }
}

void sendBLEAdvertisement(int water_level){
  bthome.resetMeasurement();
  bthome.addMeasurement_state(EVENT_BUTTON, water_level);
  bthome.buildPaket();
  bthome.start();
  delay(2000);
  bthome.stop();
}


void setup() {
  bthome.begin(DEVICE_NAME, false, "", false);

  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);

  for (int pin : touchPins) {
    //Can have up to 4 touch pins. Each value adds +1
     waterLevel += getWaterStatus(pin);
  }

  sendBLEAdvertisement(waterLevel);

  // Trying to disable BT
  BLEDevice::deinit(true);
  btStop();
  esp_bt_controller_disable();
  esp_bt_controller_deinit();
  esp_bt_mem_release(ESP_BT_MODE_BTDM);

  esp_deep_sleep_start();  
}

void loop() {

}

//Object ids by order
#if 0
#define ID_PACKET				0x00
#define ID_BATTERY				0x01
#define ID_TEMPERATURE_PRECISE 	0x02
#define ID_HUMIDITY_PRECISE 	0x03
#define ID_PRESSURE 			0x04
#define ID_ILLUMINANCE			0x05
#define ID_MASS					0x06
#define ID_MASSLB				0x07
#define ID_DEWPOINT				0x08
#define ID_COUNT				0x09
#define ID_ENERGY				0x0A
#define ID_POWER				0x0B
#define ID_VOLTAGE				0x0C
#define ID_PM25					0x0D
#define ID_PM10					0x0E
#define STATE_GENERIC_BOOLEAN	0x0F
#define STATE_POWER_ON			0x10
#define STATE_OPENING			0x11
#define ID_CO2					0x12
#define ID_TVOC					0x13
#define ID_MOISTURE_PRECISE		0x14
#define STATE_BATTERY_LOW		0x15
#define STATE_BATTERY_CHARHING	0x16
#define STATE_CO				0x17
#define STATE_COLD				0x18
#define STATE_CONNECTIVITY		0x19
#define STATE_DOOR				0x1A
#define STATE_GARAGE_DOOR		0x1B
#define STATE_GAS_DETECTED		0x1C
#define STATE_HEAT				0x1D
#define STATE_LIGHT				0x1E
#define STATE_LOCK				0x1F
#define STATE_MOISTURE			0x20
#define STATE_MOTION			0x21
#define STATE_MOVING			0x22
#define STATE_OCCUPANCY			0x23
#define STATE_PLUG				0x24
#define STATE_PRESENCE			0x25
#define STATE_PROBLEM			0x26
#define STATE_RUNNING			0x27
#define STATE_SAFETY			0x28
#define STATE_SMOKE				0x29
#define STATE_SOUND				0x2A
#define STATE_TAMPER			0x2B
#define STATE_VIBRATION			0x2C
#define STATE_WINDOW			0x2D
#define ID_HUMIDITY				0x2E
#define ID_MOISTURE				0x2F
#define EVENT_BUTTON			0x3A
#define EVENT_DIMMER			0x3C
#define ID_COUNT2				0x3D
#define ID_COUNT4				0x3E
#define ID_ROTATION				0x3F
#define ID_DISTANCE				0x40
#define ID_DISTANCEM			0x41
#define ID_DURATION				0x42
#define ID_CURRENT				0x43
#define ID_SPD					0x44
#define ID_TEMPERATURE			0x45
#define ID_UV					0x46
#define ID_VOLUME1				0x47
#define ID_VOLUME2				0x48
#define ID_VOLUMEFR				0x49
#define ID_VOLTAGE1				0x4A
#define ID_GAS					0x4B
#define ID_GAS4					0x4C
#define ID_ENERGY4				0x4D
#define ID_VOLUME				0x4E
#define ID_WATER				0x4F
#endif

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.