From bab562dc3a511b2e357883bfee1b411bc3dac252 Mon Sep 17 00:00:00 2001 From: Adriaan Peeters Date: Wed, 10 Jun 2020 04:23:25 +0200 Subject: [PATCH] Turn off PN532 RF field when not expecting a tag (#1046) * Turn off PN532 RF field when not expecting a tag Avoids interference with Wifi connectivity of nearby devices. * Rename turn_off_rf_ method * documented off command bytes Co-authored-by: Guillermo Ruffino --- esphome/components/pn532/pn532.cpp | 18 +++++++++++++++++- esphome/components/pn532/pn532.h | 2 ++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/esphome/components/pn532/pn532.cpp b/esphome/components/pn532/pn532.cpp index 93000a7421..792d92a6ac 100644 --- a/esphome/components/pn532/pn532.cpp +++ b/esphome/components/pn532/pn532.cpp @@ -86,6 +86,8 @@ void PN532::setup() { this->mark_failed(); return; } + + this->turn_off_rf_(); } void PN532::update() { @@ -114,13 +116,16 @@ void PN532::loop() { if (read.size() <= 2 || read[0] != 0x4B) { // Something failed + this->turn_off_rf_(); return; } uint8_t num_targets = read[1]; - if (num_targets != 1) + if (num_targets != 1) { // no tags found or too many + this->turn_off_rf_(); return; + } // const uint8_t target_number = read[2]; // const uint16_t sens_res = uint16_t(read[3] << 8) | read[4]; @@ -150,6 +155,17 @@ void PN532::loop() { format_uid(buf, nfcid, nfcid_length); ESP_LOGD(TAG, "Found new tag '%s'", buf); } + + this->turn_off_rf_(); +} + +void PN532::turn_off_rf_() { + ESP_LOGVV(TAG, "Turning RF field OFF"); + this->pn532_write_command_check_ack_({ + 0x32, // RFConfiguration + 0x1, // RF Field + 0x0 // Off + }); } float PN532::get_setup_priority() const { return setup_priority::DATA; } diff --git a/esphome/components/pn532/pn532.h b/esphome/components/pn532/pn532.h index 49d5878265..3a734b7ba2 100644 --- a/esphome/components/pn532/pn532.h +++ b/esphome/components/pn532/pn532.h @@ -55,6 +55,8 @@ class PN532 : public PollingComponent, bool read_ack_(); + void turn_off_rf_(); + bool requested_read_{false}; std::vector binary_sensors_; std::vector triggers_;