My first LoraWAN Sensor - struggling with the code for AB01

I’ve bought HTCC-AB01 board to make a sensor for my letterbox. I’ve also bought a battery and two reed sensors. This is my rig:

I’m not a programmer at all, but somehow I’ve managed to develop the code in Arduino IDE through several “stages”:

  1. First, I’ve successfully connected it to TTN
  2. Than I’ve managed to read state of two reed sensors and pass it to TTN
  3. Then I’ve added battery and read battery voltage and calculated battery state
  4. I’ve added also a payload formater at TTN app, so I see all data I wanted to see:
  • contact sensor #1 state (boolean)
  • contact sensor #2 state (boolean)
  • battery voltage (V)
  • battery level (%)
  1. I also see that the board is put to sleep mode between transmit cycles.

I’m struggling with the last state - waking it up on contact sensor state.
I’ve used the interrupt example but I’m lost here:

a) First I wanted to transmit both edges - rising and falling. But that does not work reliably. Sensor open state (1) is usually transmited, but sensor close state (0) is not. Is that becuse of LoraWAN transmission limitations?

b) So later I’ve decided to transmit only rising edge (sensor opening) but I’m event failing on this. The board is woken up on both state changes and I don’t know why.

Anyway it doesn’t work reliably.
Here’s my code:

Any tips / ideas / optimalisations - very welcome!

Ok, I managed to fix the code to wake the board only on sensor open:

BUT I still don’t get message published when sensor is closed. I have even postponed them by 41 seconds. They just don’t get published. Please see this screenshot:

In fact, you don’t need to add LoRaWAN.send() repeatedly;

So how do I notify my app that the sensor (reed switch) has been closed?

The status will be uploaded here.

But the device is in sleep mode after reed switch open and transmit. So its in DEVICE_STATE_SLEEP state and reed switch close event would be reported in next transmission cycle (that could be next 22 hours). That’s how I understand the code. Am I wrong?

You can add external interrupt to wake up.

How do I do it? I’m a beginner in programming, unfortunately.
I’ve already tried using attchInterrupt on FALLING but that was way to early. Seems that TTN doesn’t allow to transmit so quickly one message after the second.