MailBoxGuard icon indicating copy to clipboard operation
MailBoxGuard copied to clipboard

mailbox sensor stopped working without noticeable reason

Open Rico81 opened this issue 1 year ago • 12 comments

I've been using the mailbox sensor together with the capi bridge for almost a year. However a couple of months ago I realized that the sensor (or something related) had stopped working. Trying to pinpoint to problem I fiddled around a bit and this is what I found out: -the mailbox sensor can be seen as mqtt device and is transmitting battery status -according to the LED the mailbox sensor is reacting to movement -the capi bridge can be seen as a mqtt device -I can set the device's status via the capi bridge

So obviously the only thing not working is the transmiting of movement as a status change to the capi bridge.

Has anyone an idea?

Rico81 avatar Sep 29 '25 19:09 Rico81

Hi Rico81,

It seems the mailbox sensor is detecting movement (PIR) but not transmitting that movement status to the CapiBridge gateway properly. Based on the README, here are some key points and suggestions that might help:

  1. Matching LoRa Settings:
    Make sure that the LoRa configuration in your mailbox sensor firmware and the CapiBridge gateway firmware exactly match, especially these parameters:

    • SIGNAL_BANDWITH
    • SPREADING_FACTOR
    • CODING_RATE
    • SYNC_WORD
    • PREAMBLE_LENGTH
    • BAND (frequency)

    Mismatched settings will prevent the sensor from successfully transmitting to the gateway.

  2. Firmware Versions:
    There was a breaking change on 22.05.2025 related to XOR obfuscation (encryption) for LoRa transmissions. Both your MailBox Guard sensor firmware and the CapiBridge gateway firmware need to be updated to the latest versions compatible with this change. Otherwise, communication might fail intermittently or completely.

  3. CapiBridge Configuration:
    Verify your CapiBridge settings as instructed on the CapiBridge GitHub page and ensure the Gateway key matches the sensor’s key.

  4. Testing Sensor Transmission:
    Since you can see battery status via MQTT and the sensor reacts to movements (LED), your sensor’s LoRa Tx might be working partially. However, the movement status packets might not be sent or received correctly due to configuration or firmware mismatch.

  5. Reprogramming the Sensor:
    You might want to re-upload the Mailbox_Guard_Sensor_MQTT_JSON.ino with your unique sensor name and the exact LoRa and Gateway key values matching your CapiBridge gateway configuration. The README includes full instructions on programming via UPDI and the Arduino IDE.

  6. Check MQTT Topics and Payloads:
    Ensure that your MQTT broker is receiving the correct topics and payloads from the sensor and that the CapiBridge publishes these to the expected topics for Home Assistant or your MQTT client.

If you want to explore alternatives or upgrades, the MailBox Guard production has been discontinued, and the new versions like PirBOX-LITE or PirBOX-MAX are recommended.

For detailed setup, configuration, and troubleshooting, please see the README sections on Firmware & Gateway configuration and Sensor Programming.


Summary:

  • Update both sensor and gateway firmware to the latest compatible versions.
  • Verify matching LoRa and Gateway keys/settings for sensor and CapiBridge.
  • Reprogram the sensor if needed, ensuring correct settings.
  • Check MQTT connections and topics.

This should resolve issues with movement status not transmitting to the CapiBridge.

More info:

I hope this helps you get your mailbox sensor working again!

✅ If this answer helped, please close the issue. 🤖 I’m an AI and If my response is incorrect or not sufficient, don’t worry @PricelessToolkit will review this topic soon.

AI-RedCode avatar Sep 29 '25 19:09 AI-RedCode

Hi, if it sends battery status, it sends state "mail" too. You can check it by connecting the gateway to the PC and opening the COM port to see the received messages.

try this

  1. Delete from the MQTT devices and re-trigger the sensor.
  2. Reset status from MQTT "homeassistant/sensor/YourSensorName/state" set to "empty", not from HA UI If in the past you set the status from HA, you need to delete the sensor and re-trigger it.

PricelessToolkit avatar Sep 29 '25 19:09 PricelessToolkit

Thanks for your reply. In the meantime I learned that there was a major update of the firmware, so I started to flash both devices with the current firmware. Unfortunately it did not work, which made me dig a little deeper on your github pages and then I saw that there are more than one set of files for each device. Plus, there are files for the CapiBridge in the respective github section and there are files in the MailBoxSensor section that seem to be for the Capi Bridge as well. So I am confused which files to take. So could you point me to the correct files please? My hardware is the MailBox Sensor old design and the CapiBridge prior Aug 2025. What I want to do (and already had running prior the update) is only local usage with mqtt and my home assistant.

Rico81 avatar Oct 14 '25 18:10 Rico81

Hi Rico81, To help you properly, “it did not work” is too general I need to know at which step it fails:

  1. Does flashing fail in Arduino IDE? → If yes, please post a screenshot or the error message.
  2. Or does the issue happen after flashing? (device not responding, etc.)

Once I know that, I can guide you exactly.

Correct files for your hardware

CapiBridge

Download the complete CapiBridge project ZIP from GitHub and unzip it.

  • On the CapiBridge hardware, set switch to ESP1

  • Open ESP1/ESP1.ino in Arduino IDE

  • config.h and radio.h are included automatically

  • Flash it.

  • Then switch the hardware selector to ESP2

  • Open ESP2/ESP2.ino

  • Flash it.

  • Double-check the switch before flashing — flashing the wrong ESP is a common mistake.

MailBox Sensor

Use the file: Mailbox_Guard_Sensor_CapiBridge.ino (This file is in the MailboxGuard GitHub repo, not in the CapiBridge repo)

Flash it normally with Arduino IDE.

Tell me where exactly you get stuck (flash error, compile error, device not starting, etc.), and I’ll continue from there.

PricelessToolkit avatar Oct 15 '25 15:10 PricelessToolkit

Thanks for your help and sorry for not being specific enough. "Did not work" meant and still means: both devices run through the flashing without any negative message. But afterwards the sensor is not to be seen in ha. The capi bridge is, but the sensor isn't. I repeated with the files you confirmed to be the right ones and deleted the old devices in ha. After powering them, only the capi bridge is being added. I crosschecked the sensor ino against the config.h of the bridge and think I didn't miss any code line to be matched.

Rico81 avatar Oct 20 '25 18:10 Rico81

  1. "deleted the old devices in ha" use MQTT explorer and delete from the topic. If the sensor transmits battery status, it means the configs are ok, "CamBridge can hear the sensor".
  2. Connect CapiBridge to the PC, open the serial terminal, and trigger the sensor. CapiBridge will print out all received messages. So we can see if the sensor sends only battery information or motion status too.

PricelessToolkit avatar Oct 20 '25 21:10 PricelessToolkit

To clarify the fact of submitting battery status: At a certain point I realized that the mailbox sensor wasn't reporting mail anymore. At that point it still continued to submit the battery status. Then I learned that there had been a software change and that both devices (bridge and sensor) have to be updated. Once I tried to flash the new firmware it did not submit any battery status. So no sign of the sensor at the moment. Appart from the LED showing detetctions.

So I now deleted the sensor by means of the mqtt explorer. The readout of the bridge shows a normal startup behaviour and when the sensor detects motion it gives two lines of: readData() error -24

Rico81 avatar Oct 22 '25 07:10 Rico81

readData() error -24 is a RadioLib status code meaning “LoRa packet header has been damaged.” In other words, the receiver detected a LoRa preamble, but the header couldn’t be decoded correctly.

in CapiBridge config.h

Which line is uncommented? And you are using the default config? What are you changing in it? So I can reproduce.

#define LORA_MODULE LORA_MODULE_SX1276  // SX1276 Module (orders shipped before Aug 2025)
//#define LORA_MODULE LORA_MODULE_SX1262  // SX1262 Module CapiBridge v2 (orders shipped after Aug 2025)

PricelessToolkit avatar Oct 23 '25 13:10 PricelessToolkit

I think it would be best to provide the full code:

config.h


/////////////////////////// LoRa/ESPNow Gateway Key ///////////////////////////

#define GATEWAY_KEY "1606"                           // Separation Key "Keep it small" Must match exactly sensors key
#define LoRa_Encryption true                       // Global LoRa Encryption, true or false
#define ESPNOW_Encryption false                     // Global ESP-NOW Encryption, true or false
#define encryption_key_length 4                    // must match number of bytes in the XOR key array
#define encryption_key { 0x4B, 0xA3, 0x3F, 0x9C }  // Multi-byte XOR key (2–16 bytes)

/////////////////////////////////// WIFI //////////////////////////////////////

#define WIFI_SSID ""
#define WIFI_PASSWORD ""

/////////////////////////////////// MQTT //////////////////////////////////////

#define MQTT_USERNAME "CAPI"
#define MQTT_PASSWORD "CAPI"
#define MQTT_SERVER "192.168.178.22"
#define MQTT_PORT 1883
#define DISCOVERY_EVERY_PACKET true      // true  = publish discovery every time data is received from that sensor
                                         // false = publish discovery once per CapiBridge boot (remembers if that sensor is already published)
                                         // Using false reduces MQTT traffic, but if you delete a sensor in Home Assistant,
                                         // you must reboot CapiBridge to clear the cache so it can rediscover that sensor
                                     
////////////////////////////// LORA CONFIG ////////////////////////////////////

// Select which LoRa module this CapiBridge is uses:

#define LORA_MODULE LORA_MODULE_SX1276  // SX1276 Module (orders shipped before Aug 2025)
//#define LORA_MODULE LORA_MODULE_SX1262  // SX1262 Module CapiBridge v2 (orders shipped after Aug 2025)

#define BAND 433.0                       // 433.0 / 868.0 / 915.0
#define LORA_TX_POWER 20                 // dBm; check regulatory limits in your region (SX1262 supports up to 22) (SX1276 supports up to 20)

/// Bandwidth vs SF support (SX126x):
// 125 kHz: SF5–SF9
// 250 kHz: SF5–SF10
// 500 kHz: SF5–SF11

#define LORA_SIGNAL_BANDWIDTH 250.0     // kHz: 125.0, 250.0, 500.0
#define LORA_SPREADING_FACTOR 10        // "5–11 on SX1262" "6-12 on SX1276"
#define LORA_CODING_RATE 5              // 5–8 → coding rate 4/5 .. 4/8
#define LORA_SYNC_WORD 0x12             // Default 0x12 Dont Change
#define LORA_PREAMBLE_LENGTH 12         // 6..65535

///////////////////////////////////////////////////////////////////////////////

#define ROW_Debug false  // true or false , Prints ROW hex values insteed of JSON`

radio.h


#pragma once
#include <Arduino.h>   // for RISING (SX1276)
#include <RadioLib.h>
#include "config.h"

//#define LORA_MODULE_SX1262 1
#define LORA_MODULE_SX1276 2

// ===== Per-module pin maps + glue macros =====
#if (LORA_MODULE == LORA_MODULE_SX1276)
// ---------- SX1276 (RA-01H): IRQ on DIO0, no BUSY ----------
  #define RADIO_CLASS SX1276

  // SPI + control pins
  #define CONFIG_MOSI 1
  #define CONFIG_MISO 0
  #define CONFIG_CLK  4
  #define CONFIG_NSS  3
  #define CONFIG_RST  6
  #define CONFIG_DIO0 5

  // Glue for constructor/IRQs
  #define CONFIG_IRQ   CONFIG_DIO0
  #define CONFIG_BUSY  RADIOLIB_NC

  // new Module(nss, dio0, rst, nc)
  #define MODULE_ARGS  CONFIG_NSS, CONFIG_IRQ, CONFIG_RST, CONFIG_BUSY

  // Hook IRQ to DIO0 (RadioLib 7.2.1 needs edge param on SX127x)
  #define SET_DIO_ACTION(r, cb) (r).setDio0Action(cb, RISING)

  // Detach the right IRQ line around TX
  #define DETACH_IRQ() detachInterrupt(digitalPinToInterrupt(CONFIG_IRQ))

  // SX1276 begin signature: last param = gain (0.0 = AGC)
  #define RADIO_BEGIN(r) \
    (r).begin(BAND, LORA_SIGNAL_BANDWIDTH, LORA_SPREADING_FACTOR, \
              LORA_CODING_RATE, LORA_SYNC_WORD, LORA_TX_POWER, \
              LORA_PREAMBLE_LENGTH, 0.0)

#elif (LORA_MODULE == LORA_MODULE_SX1262)
// ---------- SX1262: IRQ on DIO1, uses BUSY ----------
  #define RADIO_CLASS SX1262

  // SPI + control pins
  #define CONFIG_MOSI 1
  #define CONFIG_MISO 0
  #define CONFIG_CLK  4
  #define CONFIG_NSS  3
  #define CONFIG_RST  6
  #define CONFIG_DIO1 5
  #define CONFIG_BUSY 10

  // Glue for constructor/IRQs
  #define CONFIG_IRQ   CONFIG_DIO1

  // new Module(nss, dio1, rst, busy)
  #define MODULE_ARGS  CONFIG_NSS, CONFIG_IRQ, CONFIG_RST, CONFIG_BUSY

  // Hook IRQ to DIO1 (SX126x API takes only the callback)
  #define SET_DIO_ACTION(r, cb) (r).setDio1Action(cb)

  // Detach the right IRQ line around TX
  #define DETACH_IRQ() detachInterrupt(digitalPinToInterrupt(CONFIG_IRQ))

  // SX126x begin signature: (... preambleLen, tcxoVoltage, useRegulatorLDO)
  #define RADIO_BEGIN(r) \
    (r).begin(BAND, LORA_SIGNAL_BANDWIDTH, LORA_SPREADING_FACTOR, \
              LORA_CODING_RATE, LORA_SYNC_WORD, LORA_TX_POWER, \
              LORA_PREAMBLE_LENGTH, 0.0, false)

#else
  #error "LORA_MODULE not set to LORA_MODULE_SX1276 or LORA_MODULE_SX1262"
#endif`

Rico81 avatar Oct 26 '25 18:10 Rico81

Thank you. Can you share the mailbox Guard code, too? So I can test it with my gear, and if you have time, can you test the default frimware with the default code "without changing any key"?

PricelessToolkit avatar Oct 27 '25 17:10 PricelessToolkit

Sure:

// Youtube Channel https://www.youtube.com/@PricelessToolkit/videos
// GitHub https://github.com/PricelessToolkit/MailBoxGuard

#include <SPI.h>
#include <LoRa.h>
#include <avr/sleep.h>

///////////////////////////////// LoRa RADIO /////////////////////////////////

#define SIGNAL_BANDWITH 250E3
#define SPREADING_FACTOR 10
#define CODING_RATE 5
#define SYNC_WORD 0x12
#define PREAMBLE_LENGTH 12
#define TX_POWER 20
#define BAND 433E6 // 433E6 / 868E6 / 915E6

//////////////////////////// Name and Keys/Encryption ////////////////////////

#define NODE_NAME "mbox"
#define GATEWAY_KEY "1606" // must match CapiBridge's key
#define Encryption true                            // Global Payload obfuscation (Encryption)
#define encryption_key_length 4                    // must match number of bytes in the XOR key array
#define encryption_key { 0x4B, 0xA3, 0x3F, 0x9C }  // Multi-byte XOR key (between 2–16 values).

///////////////////////////////////////////////////////////////////////////////

int loopcounter = 0;

void setup() {
  pinMode(3, OUTPUT);
  digitalWrite(3, HIGH);
  Serial.begin(9600);
  analogReference(VDD);
  delay(5);

  if (!LoRa.begin(BAND)) {
    Serial.println("LoRaError");
    while (1) {}
  }

  LoRa.setSignalBandwidth(250E3);   // defaults to 125E3
  LoRa.setSpreadingFactor(10);  // 6 - 12, defaults to 7
  LoRa.setCodingRate4(5);           // 5 - 8, defaults to 5
  LoRa.setSyncWord(0x12);                // defaults to 0x12
  LoRa.setPreambleLength(12);    // 6 - 65535, defaults to 8
  LoRa.disableCrc();                          // defaults to disabled
  LoRa.setTxPower(20);                  // 2 - 20, defaults to 17
}

// -------------------- Binary-safe XOR helper -------------------- //
static inline void xorBuffer(uint8_t* buf, size_t len) {
#if Encryption
  static const uint8_t key[] = encryption_key;
  const int K = encryption_key_length;
  for (size_t i = 0; i < len; ++i) {
    buf[i] ^= key[i % K];
  }
#else
  (void)buf; (void)len;
#endif
}

void loop() {
  if (loopcounter < 2) {
    delay(10);

    // Build JSON payload
    float volts = analogReadEnh(PIN_PB4, 12) * (1.1 / 4096) * (30 + 10) / 10;
    float percentage = ((volts - 3.2) / (4.15 - 3.2)) * 100;
    percentage = constrain(percentage, 0, 100);
    int intPercentage = (int)percentage;

    String payload = "{\"k\":\"" + String(GATEWAY_KEY) + "\",\"id\":\"" + String(NODE_NAME) + "\",\"s\":\"mail\",\"b\":" + String(intPercentage) + "}";

    // Copy to buffer and XOR (binary-safe)
    uint8_t buf[255];
    size_t len = payload.length();
    if (len > sizeof(buf)) len = sizeof(buf);
    memcpy(buf, payload.c_str(), len);
    xorBuffer(buf, len);

    // Send raw bytes (not print) to preserve binary data
    LoRa.beginPacket();
    LoRa.write(buf, len);
    LoRa.endPacket();

    delay(10);
  }

  if (loopcounter > 2) {
    LoRa.sleep();             // Put the radio in sleep mode
    digitalWrite(3, LOW);     // Latch pin 3 LOW for power cut off
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    sleep_enable();
    sleep_mode();             // Enter sleep mode
  }

  loopcounter++;
  delay(10);
}

I will try the default firmware as soon as I find time, but I don't think it'll be before Thursday.

Rico81 avatar Oct 28 '25 18:10 Rico81

Fixed! fix in ligne 47 "LoRa.disableCrc(); to LoRa.enableCrc();"

PricelessToolkit avatar Nov 09 '25 15:11 PricelessToolkit