dalehumby / ESPHome-Apple-Watch-detection

ESPHome BLE Apple Watch presence detection
MIT License
330 stars 16 forks source link

Getting compile error: best_rssi may be used uninitialized in this function #24

Open lweberru opened 1 year ago

lweberru commented 1 year ago

Hi,

I am getting a compile errorfor my atom lite for your code. As I cant develop C, I have no clue how to solve it. Please assist.

NFO Reading configuration atom-lite.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing atom-lite-1 (board: m5stack-atom; framework: espidf; platform: platformio/espressif32 @ 5.2.0)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
 - framework-espidf @ 3.40402.0 (4.4.2) 
 - tool-cmake @ 3.16.4 
 - tool-ninja @ 1.7.1 
 - toolchain-esp32ulp @ 2.35.0-20220830 
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch3
Reading CMake configuration...
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
Dependency Graph
|-- Improv @ 1.2.3
Compiling .pioenvs/atom-lite-1/src/main.o
atom-lite.yaml: In static member function 'static void std::_Function_handler<void(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes&& ...) [with _Functor = setup()::<lambda(const esphome::esp32_ble_tracker::ESPBTDevice&)>; _ArgTypes = {const esphome::esp32_ble_tracker::ESPBTDevice&}]':
atom-lite.yaml:86:40: error: '*((void*)& best_rssi +2)' may be used uninitialized in this function [-Werror=maybe-uninitialized]
atom-lite.yaml:61:25: note: '*((void*)& best_rssi +2)' was declared here
cc1plus: some warnings being treated as errors
*** [.pioenvs/atom-lite-1/src/main.o] Error 1

line 86 is:

id(apple_watch_rssi).publish_state(*best_rssi);

here is the full yaml

substitutions:
  name: atom-lite-1
  roomname: Wohnzimmer
  static_ip: 192.168.1.145
  yourname: Lukas
  rssi_present: id(harssi_present).state
  rssi_not_present: id(harssi_not_present).state

esphome:
  name: ${name}

packages:
  esphome.bluetooth-proxy: github://esphome/bluetooth-proxies/m5stack-atom-lite.yaml@main

esp32:
  board: m5stack-atom
  framework:
#    type: arduino
    type: esp-idf    

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: ""

ota:
  password: ""

wifi:
  ssid: "CLUELANSPEED"
  password: "xxxxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Atom-Lite-1 Fallback Hotspot"
    password: "xxxx"

#captive_portal:

bluetooth_proxy:
  active: true

#web_server:
#  port: 80
#  auth:
#    username: admin
#    password: xxxxx

esp32_ble_tracker:
  scan_parameters:
    interval: 1200ms
    window: 500ms
    active: true
  on_ble_advertise:
    - then:
      # Look for manufacturer data of form: 4c00 10 05 YY 98 XXXXXX
      # Where YY can be 01..0F or 20..2F; and XXXXXX is ignored
      - lambda: |-
          optional<int16_t> best_rssi = nullopt;
          for (auto data : x.get_manufacturer_datas()) {
            const int16_t device_rssi = x.get_rssi();
            // ESP_LOGD("ble_adv", "Found Device (mac %s) rssi %i", x.address_str().c_str(), device_rssi);
            // Guard against non-Apple datagrams, or those that are too small.
            if (data.data.size() < 4 || data.uuid.to_string() != "0x004C" || data.data[0] != 0x10 || data.data[1] < 5) {
              // ESP_LOGD("ble_adv", "Non Apple datagrams found, skipping, uuid=%s, data.data.size()=%i, data.data[0]=%i, data.data[1]=%i",data.uuid.to_string().c_str(),data.data.size(),data.data[0],data.data[1]);
              continue;
            }
            const int16_t rssi = x.get_rssi();
            const uint8_t status_flags = data.data[2] >> 4;  // High nibble
            const uint8_t data_flags = data.data[3];
            // ESP_LOGD("ble_adv", "status_flags=%i, data_flags=%i",status_flags,data_flags);            

            if (data_flags == 0x98 || data_flags == 0x18) {  // Match unlocked Apple Watch. To also match locked watch use: if (data_flags == 0x98 || data_flags == 0x18) {
              if (status_flags == 0 || status_flags == 2) {
                best_rssi = max(rssi, best_rssi.value_or(rssi));
                ESP_LOGD("ble_adv", "Found Apple Watch (mac %s) rssi %i", x.address_str().c_str(), rssi);
              } else {
                ESP_LOGD("ble_adv", "Possible Apple Watch? (mac %s) rssi %i, unrecognised status/action flags %#04x", x.address_str().c_str(), rssi, data.data[2]);
              }
            }
          }
          if (best_rssi) {
            ESP_LOGD("ble_adv", "Publish State of Apple Watch (mac %s)", x.address_str().c_str());
            id(apple_watch_rssi).publish_state(*best_rssi);
          } else {
            ESP_LOGD("ble_adv", "Not publishing State of Apple Watch (mac %s)", x.address_str().c_str());
          }

sensor:
  - platform: template
    id: apple_watch_rssi
    name: "$yourname Apple Watch $roomname RSSI"
    device_class: signal_strength
    unit_of_measurement: dBm
    accuracy_decimals: 0
    filters:
      - exponential_moving_average:
          alpha: 0.3
          send_every: 1
    on_value:
      then:
        - lambda: |-
            if (id(apple_watch_rssi).state > $rssi_present) {
              id(room_presence_debounce).publish_state(1);
            } else if (id(apple_watch_rssi).state < $rssi_not_present) {
              id(room_presence_debounce).publish_state(0);
            }
        - script.execute: presence_timeout  # Publish 0 if no rssi received

  - platform: template
    id: room_presence_debounce
    filters:
      - sliding_window_moving_average:
          window_size: 3
          send_every: 1

  - platform: homeassistant
    name: HA RSSI Present Value
    entity_id: input_number.rssi_present
    id: harssi_present
    internal: true
  - platform: homeassistant
    name: HA RSSI Not Present Value
    entity_id: input_number.rssi_not_present
    id: harssi_not_present
    internal: true

binary_sensor:
  - platform: template
    id: room_presence
    name: "$yourname $roomname presence"
    device_class: occupancy
    lambda: |-
      if (id(room_presence_debounce).state > 0.99) {
        return true;
      } else if (id(room_presence_debounce).state < 0.01) {
        return false;
      } else {
        return id(room_presence).state;
      }

script:
  # Publish event every 30 seconds when no rssi received
  id: presence_timeout
  mode: restart
  then:
    - delay: 30s
    - lambda: |-
        id(room_presence_debounce).publish_state(0);
    - script.execute: presence_timeout

Any tip is welcome. Thanks.

Lukas

easez88 commented 1 year ago

Did you figure this out? I'm getting the same error.

lweberru commented 1 year ago

Yes

here the code to ignore the error:

esphome:
  name: ${name}
  name_add_mac_suffix: True
  platformio_options:
    build_flags: 
      - -Wno-maybe-uninitialized