# ATMEGA328P_connectedMailBox

## What is it ?
This system is an ATMEGA328P and NRF24L01+ based low power device to detect when something is posted in my mail box. The electronic circuit shares the same designed as the low power weather station I made enabling it to consume only 60µA when sleeping.  
**You will also need to build a receiver station to receive the messages. For that you can build your own or use [this design](/git/Th3maz1ng/ESP8266_dual_NRF24l01_gateway) I made.**

**Check the LICENSE.md file at the root of this project for more information.**  

![](http://82.64.196.164/images/git/connected_mailbox_system.png)

## What is this device capable of ?
The system is able to wait for up to three distinct events (interrupt sources).  This means that it can detect up to three actions like :  
* A letter was delivered in the mail box using the small letter tray.
* A package was delivered using the door.
* The mail was picked up using the rear door by me or a family member.  

**Changing the design of the event detection circuit can easily increase the number of various events that could be detected**  
The schematic is available [here](/git/Th3maz1ng/ATMEGA328P_connectedMailBox/src/branch/master/schematic/ATMEGA328P_connectedMailBox.pdf).  
The device is fixed to the mailbox using two strong neodymium magnets. No need to make holes in the box.

## Power consumption :
* The device consumes around 60µA when sleeping (ie : waiting for an event to happen).
* The device consumes around 13mA for a very short period of time when transmitting a RF packet after an event was detected.
* The current design is using a 2000mAh LiPo battery which should last for at least four years of every day use. **/!\ I may update the design to add a solar pannel like I did for the weather station.**

## Project folder architecture :
``` json
/
|_src               This folder contains all the C/C++ file sources.
| \_app             This folder contains the main app and it's dependencies.
|  |_libs           This folder contains all the required 3rd party libraries that should be put in "Arduino\libraries" folder in order for the app to compile.
|
|_schematic         This folder contains all the files associated with the schematic.
| \_KiCad           This folder contains the KiCad files associated with the project.
| |_ATMEGA328P_connectedMailBox.pdf             The most recent schematic exported in pdf format for quick viewing.
|
|_documentation     This folder may one day contain the associated documentation if needed.
|
|_.gitignore
|_LICENSE.md
|_README.md             The content of this very page
```

## The hardware needed to build the system :
Here is a list of the parts I used to build my connected mailbox system.  
You will find a link next to the parts name. **I do not have any partnerships with the sites linked bellow.**
* [1 * Shielded NRF24L01+](https://fr.aliexpress.com/item/32976054389.html?spm=a2g0o.cart.0.0.657d3c00t37c7e&mp=1)
* [1 * TC4056 (1 cell lipo charge/discharge protection)](https://fr.aliexpress.com/item/4000522397541.html?spm=a2g0o.productlist.0.0.175b30dfajsyO5&algo_pvid=203adaed-55ba-4678-a3e4-3aafcad89952&algo_exp_id=203adaed-55ba-4678-a3e4-3aafcad89952-1)
* [ATMEGA328PU board (3.3V 8Mhz version)](https://fr.aliexpress.com/item/4000190780977.html?spm=a2g0o.store_pc_allProduct.8148356.4.66547f0dNb2qkV)
* [1 * 3.3V LDO for the power rail : RT9013-33PB](https://fr.aliexpress.com/item/4001097171645.html?spm=a2g0o.productlist.0.0.48aa7800HoFD4h&algo_pvid=454c2fa4-a1a9-4ec2-9398-00d598a20b67&algo_exp_id=454c2fa4-a1a9-4ec2-9398-00d598a20b67-0)
* [Small junction box 8cm * 8cm * 5cm](https://www.amazon.com/LeMotech-Dustproof-Waterproof-Electrical-85mmx85mmx50mm/dp/B075X14RVH/ref=sr_1_2?dchild=1&keywords=junction+box+3.4+3.4+2&qid=1626903891&sr=8-2)
* [2 or 3 x reed switches **with 3 poles** ](https://fr.aliexpress.com/item/1005001688562983.html?spm=a2g0o.productlist.0.0.6af12fe2pOdrTe&algo_pvid=d8aa7000-49e2-490f-9772-b5938b8f802b&algo_exp_id=d8aa7000-49e2-490f-9772-b5938b8f802b-6&pdp_ext_f=%7B%22sku_id%22%3A%2212000017139802685%22%7D&pdp_npi=2%40dis%21EUR%213.11%212.31%21%21%211.06%21%21%402100bdd816646211591336834e93df%2112000017139802685%21sea&curPageLogUid=aKoLUKTtQsPD)
* [1 * sliding ON/OFF switch](https://fr.aliexpress.com/item/4001207529493.html?spm=a2g0o.productlist.0.0.3a202c513vfjq1&algo_pvid=aa695a79-38ef-4ea6-8f05-faf59ce2ef72&algo_exp_id=aa695a79-38ef-4ea6-8f05-faf59ce2ef72-6&pdp_ext_f=%7B%22sku_id%22%3A%2210000015300780421%22%7D&pdp_npi=2%40dis%21EUR%210.74%210.74%21%21%211.29%21%21%402100bdd816646213639033134e93df%2110000015300780421%21sea&curPageLogUid=xfjhNRKUeMWX)
* [4 * Plugs (male and female) : 1 for the battery + 3 for the reed switchies](https://fr.aliexpress.com/item/1005002144317363.html?spm=a2g0o.productlist.0.0.a83a45a3iDP0xc&algo_pvid=4b8bede6-01b6-40b3-a760-020cee19e8a8&algo_exp_id=4b8bede6-01b6-40b3-a760-020cee19e8a8-22&pdp_ext_f=%7B%22sku_id%22%3A%2212000018882135494%22%7D&pdp_npi=2%40dis%21EUR%211.75%211.75%21%21%21%21%21%402100bdd816646215869637519e93df%2112000018882135494%21sea&curPageLogUid=FI71z2caB3CG)
* [1 * 18650 LiPo cell](https://fr.aliexpress.com/item/32982963185.html?spm=a2g0o.productlist.0.0.239b75c73qTXUy&algo_pvid=cb89721b-6859-4b79-82d1-f9d160d9cc9b&algo_exp_id=cb89721b-6859-4b79-82d1-f9d160d9cc9b-7&pdp_ext_f=%7B%22sku_id%22%3A%2266720423635%22%7D&pdp_npi=2%40dis%21EUR%217.37%215.9%21%21%21%21%21%402100bdde16646338349172264e0dd5%2166720423635%21sea&curPageLogUid=OUCa2kvr6oVj)
* [1 * 18650 LiPo holder](https://fr.aliexpress.com/item/1005002481983856.html?spm=a2g0o.productlist.0.0.f5f763cbNLBu0K&algo_pvid=89526e49-cad2-4410-a877-e1667f90a59b&algo_exp_id=89526e49-cad2-4410-a877-e1667f90a59b-0&pdp_ext_f=%7B%22sku_id%22%3A%2212000020810965165%22%7D&pdp_npi=2%40dis%21EUR%212.17%211.47%21%21%210.71%21%21%402100bdde16646339350095156e0dd5%2112000020810965165%21sea&curPageLogUid=whHjK2QVuxQk)
* [2 * Neodymium magnets](https://fr.aliexpress.com/item/1005003456990972.html?spm=a2g0o.productlist.0.0.26814f76NZKzoT&ad_pvid=2022100107212914868010394701920022856461_5&s=p)
* 1 * IRLML6402 P-channel MOSFET
* 1 * IRLML2502 N-channel MOSFET
* 3 * 10K resistor
* 1 * 1K resistor
* 1 * 3.9K resistor
* 1 * 3.2K resistor
* 1 * 5K resistor
* 1 * 15K resistor
* 1 * 20K resistor
* 1 * 8.2K resistor 


## Getting started :
1. Clone this repository : git clone http://web-directories.cf/git/Th3maz1ng/ATMEGA328P_connectedMailBox.git
2. Go to **"src\libs"** folder of this project and copy and paste its content in your "Arduino\libraries" folder.
3. Open the app.ino file with the Arduino IDE and select the "Arduino Pro or Pro Mini" board with processor : (Atmega 3.3V 8Mhz).
4. Compile and Upload !
5. To receive the data sent by the device, you can check this other [project](/git/Th3maz1ng/ESP8266_dual_NRF24l01_gateway) which is the receiver end.

## Configuration options :
You may change some settings present in the ***definition.h*** file located in the "src/app" folder.

Set **MAX_SEND_ROUND_RETRIES** to the number of send retries the device will perform if no ACK is received.  
Default is 5, increase this parameter if the RF link between the device and the receiving station is bad.  
This may increase power consumption, but it's better to receive the message than to save power.

Set **SERIAL_DEBUG_ENABLED** to **1** to get debug output on the serial console.

Set **SERIAL_BAUD_RATE** to the desired baudrate for debugging.

Set **ADC_QUANTUM** to adjust the quantum if your VREF is not the same as mine.<br />
To do so, measure which voltage does the VCC pin of your Arduino board output and divide it by 1024 (10 bit ADC resolution).

Set **VOLTAGE_DIV_COEFF** to something different if you changed the voltage divider resistor values.

Set **RADIO_CHANNEL** to the NRF's RF channel (between 0 and 125). **Do not forget to set the receiver station up on the same channel**

Set **RADIO_NODE_ADDRESS** to change it's 5 byte address if you feel like it. **Do not forget to set the receiver station up on the same address**

Set **RADIO_PA_LEVEL** to adjust the transmit power level if needed (higher the power, higher the current consumption)

Set **ENABLE_LNA** to true or false to enable or disable the Low Noise Amplifier on the NRF modules if it has one.

***If you have any questions, do not hesitate to contact me at : bugreport[at]laposte[dot]net***

## Finally here are some pictures of the PCB, reed switches setup and mailbox :
### Overall device:  
![](http://82.64.196.164/images/git/connected_mailbox_system_open_pcb_out.png)

### Junction box layout closeup:  
![](http://82.64.196.164/images/git/connected_mailbox_system_open_closeup.png)

### PCB back :  
![](http://82.64.196.164/images/git/connected_mailbox_pcb_back.png)

### PCB front :  
![](http://82.64.196.164/images/git/connected_mailbox_pcb_front.png)

### Reed switch installation in the mailbox :  
![](http://82.64.196.164/images/git/connected_mailbox_mailbox.png)

### Package door lock reed switch closeup:  
![](http://82.64.196.164/images/git/connected_mailbox_mailbox_door_lock.png)

### Letter tray reed switch closeup:  
![](http://82.64.196.164/images/git/connected_mailbox_mailbox_letter_tray.png)