I want to create a system that can notify me when the mailman delivers a package to my mailbox. Considering that my mailbox is located in the central hallway area, the mailman’s visit time is unpredictable, and I don’t have a window facing the road, so I can’t see the mailman’s arrival.
Here are some suggestions:
Install a smart mailbox: I can install a smart mailbox equipped with sensors and Wi-Fi connectivity, which will send a notification to my phone or email when the mailman delivers a package.
Use a motion detector: I can install a motion detector near the mailbox, which will detect the mailman’s movement and send a notification to my phone.
Mailbox camera: I can install a small camera near the mailbox, which will take a photo when the mailman delivers a package and send it to my phone.
Mailman signing system: I can work with my neighbors or building administrators to establish a mailman signing system, which requires the mailman to sign for confirmation when delivering a package, and then I will receive a corresponding notification.
These methods can help me receive notifications when the mailman delivers a package, solving my problem.
The mailboxes
Of course, I can’t connect my mailbox to my apartment using any cables, so I need a wireless solution. My first choice is to use WiFi, but the mailbox is too far away, and the signal can’t be transmitted back to my apartment. I might be able to use a 433MHz wireless module with a receiver in my apartment, but I recently came across the AI Thinker A9G GPRS/GPS module, which can be loaded with custom firmware. Each one only costs around 4 pounds, which means I don’t have to deal with the hassle of parsing AT commands like I would with the popular SIM800.
In the end, I decided to use the A9G and add auxiliary functions as a listener and GPS tracker, since it has a built-in GPS receiver! This way, I can create a more comprehensive solution.
Under the metal shield of the A9G module; RDA8955L main processor, RDA6625e GSM front-end IC and a GK9501 GPS receiver
In this project, using only the A9G is not feasible, as it still consumes a significant 240uA current in sleep mode, and the battery can only last for a few months between charges. To address this, I added an ATtiny402 to handle power management for the A9G, by controlling a P-MOSFET to completely cut off the module’s power during sleep. The ATtiny402 is also responsible for detecting when the mailbox lid is opened, when the GPS tracking mode button is pressed, when the battery is fully charged (to send a charging completion notification), and ensuring that the battery voltage is high enough before waking up the A9G. It communicates with the A9G through a 1-wire UART, informing it of the reason for wake-up and other details.
The power source is a rechargeable 10440 lithium battery (equivalent size to AAA) with a rated capacity of 380mAh, though the actual measured capacity is 317mAh. The supply voltage range for the A9G is 3.5V – 4.2V and 1.8V – 5V for the ATtiny402, so the lithium-ion voltage range of 3V – 4.2V is perfect. Don’t trust what they write on these batteries, there are no built-in protection circuits!
New mail is detected by a reed switch and a magnet attached to the mailbox flap. The reed switch is a normally-closed type, so that when the flap is open and the magnet is away from the switch the contacts will close, triggering the ATtiny402. When the flap is closed the magnet will be near the reed switch, opening the contacts and breaking the circuit and in this state no power is consumed at all.
I need an enclosure to protect the device from damage caused by falling mail and to prevent it from short-circuiting on the metal mailbox. The enclosure I chose has awkwardly placed PCB mounting screw holes, which are blocked by the battery holder and A9G module. To achieve optimal radio frequency performance, they need to be on the same side as the antenna connector. Therefore, I removed the mounting points and added some of my own screw holes. I drilled some holes and slots for installing environmental sensors, a microphone, a USB charging port, and a spring-loaded switch input.
I also added a BME280 environmental sensor, which can detect temperature, humidity, and pressure, but without any particular reason. Unfortunately, only the first reading at startup is reliable, because the A9G module heats up quickly, causing the temperature reading to rise and the humidity to decrease. It’s possible that the sensor should be stuck on the outside of the enclosure!
When the A9G is powered on and completes its startup, it uses a single-line UART to ask the ATtiny402 why it was woken up, and the ATtiny402 responds with the reasons (new mail, tracking mode, battery charging complete, or switch stuck) and some statistical data, such as the number of successful and failed notification attempts and whether a balance check is needed. Then, the module connects to the GSM network, and if a balance check is needed, it sends an SMS to 2732 and waits for a response containing the remaining balance on the PAYG SIM. Next, it connects to GPRS and sends a notification to my web server via HTTP, disconnects from the network, and sends a shutdown request to the ATtiny402. However, if tracking mode is requested, it keeps the connection to the GSM/GPRS network, turns on the GPS module, and sends an update to my web server every 60 seconds.
Initially, I had the A9G send emails via SMTP, but later I thought it would be better to send HTTP requests with JSON-formatted data to the web server, where a PHP script can send emails, and maybe even save the content to a database, such as saving GPS coordinates in tracking mode and displaying them on a map. Using HTTP also speeds up the process and extends battery life, as the entire request can be sent in a single TCP packet (as long as it stays below 1360 bytes, otherwise it will be fragmented to fit the GPRS network’s MTU), unlike SMTP which requires sending multiple messages back and forth with the server. But, email is already outdated, and I remembered that Telegram has an API for robots, so the final iteration now sends notifications and GPS tracking coordinates from the PHP script to a Telegram group chat!
During the notification process, the A9G sends and receives a total of approximately 1,582 bytes of data, including all TCP SYN, ACK, FIN packets, ignoring any retransmissions. The JSON data typically amounts to around 560 bytes, containing temperature, humidity, IMEI, and other various statistical data. The HTTP header is around 190 bytes, and the DNS lookup is around 240 bytes, depending on the extra fields returned by the DNS server. The HTTP response from the server is a very simple “ok” or “error” message. The PAYG SIM used in the notification program charges 0.05 pounds per MB of data, which means that 1,000 email notifications would only cost 0.09 pounds!
Example JSON data sent in a notification (the “track” object is only included when in GPS tracking mode):{
"key": "aabbccddeeff11223344556677889900abcdef12",
"millis": 13753,
"firmware": {
"version": "1.0.0 200103",
"built": "Jan 3 2020 00:49:41"
},
"network": {
"signal": 13,
"biterror": 99,
"ip": "10.224.58.10",
"number": "",
"imei": "860000000000009",
"iccid": "8944300000000000008"
},
"battery": {
"voltage": 3978,
"percent": 72,
"vlm": 0
},
"balance": {
"state": 1,
"message": "Your balance is ?1.41",
"datetime": "2020/01/03,22:27:28+00"
},
"reasons": {
"newmail": 0,
"endcharge": 0,
"trackmode": 1,
"switchstuck": 0
},
"counts": {
"success": 23,
"failure": 0,
"timeout": 0
},
"environment": {
"temperature": 33.85,
"pressure": 1020.02703125,
"humidity": 23.396484375
},
"track": {
"gps": {
"fix": 3,
"sattotal": 8,
"sattrack": 4
},
"bds": {
"fix": 3,
"sattotal": 3,
"sattrack": 2
},
"quality": 2,
"sattrack": 6,
"latitude": -33.8567844,
"longitude": 151.213108,
"altitude": -23.200000762939453,
"speed": 167.99800002574920654,
"course": 218.99000549316406,
"date": {
"y": 19,
"m": 12,
"d": 18
},
"time": {
"h": 15,
"m": 36,
"s": 45,
"ms": 0
}
}
}
Does it Work?
After some testing, the expected battery life is much better than I expected. The A9G can repeat the process of booting, connecting to the GSM/GPRS network, sending HTTP requests, and shutting down up to 800 times on a single charge! Each cycle typically takes 18-23 seconds to complete, and if a balance check is performed every 10 cycles, it adds an extra 8 seconds. The current consumption in sleep mode is almost zero, less than 100nA. If I receive 3 emails per week, ignoring battery self-discharge, the battery should last around 5 years, and I only need to pay 0.06 pounds for the data!
In GPS tracking mode the battery life is only 3.5 hours since there’s been no optimization for long running time in this mode. It stays connected to the GSM network with GPS enabled the entire time and sends a HTTP request every 60 seconds, with an average current draw of 85mA. Since it stays connected to the GSM network it can become a listening bug when its phone number is called, however when a call is active the current draw goes way up to 200-400mA and the data connection is disabled.
Problems
I thought I had escaped all the pain of unreliable AT commands by using the A9G module, but I discovered that the A9G SDK library has many defects, and the development of the closed-source SDK stopped at the end of 2018. Apart from the defective SDK, the A9G is very easy to program, requiring only a USB serial converter.
Some SDK problems:
- Re-attaching to GPRS after detaching will mess up the module and require a restart (if it hasn’t already crashed while re-attaching) [Workaround]
- Listing unicode SMSs (usually containing emojis) will create memory leaks [LINK]
- Can only send 1 DTMF tone per phone call
- Unable to unmute the microphone until module restart [LINK]
- Deleting an SMS from SIM doesn’t always work, need to keep retrying until successful
- Gets stuck when deleting an SMS from flash, the SMS is deleted, but it then needs a restart
- And many more…
Anyway, I managed to work around most of these problems for this project.
I also found that the A9G module is very picky about the type, location, and direction of the antenna. Placing the antenna in the wrong position can cause the microphone to pick up a lot of GSM noise. Cutting a wire to 3/4 wavelength (125mm for 1800MHz) and connecting one end directly to the antenna pad and the other end to the metal shield near the antenna output on the module seems to produce the least noise, but the signal strength is not great. The miniature beginner’s antenna has the best signal, but also produces the most noise; interestingly, touching the golden part of the antenna can stop the noise. The PCB antenna has a middle ground; if placed in the correct area, the signal is good, and the noise is not too great.
Even with a good signal, the module sometimes has difficulty connecting to the GSM network. Typically, connection takes around 5 seconds, but if it fails to connect within 60 seconds, the best approach is to restart the module, and it will connect without issue.
When checking the balance, the mobile operator sometimes responds with a slightly incorrect balance. When connected to GPRS, the operator-reported balance is 0.25 pounds less than when not connected, but I think this might be a security feature to prevent accidental downloads of large files that could drain your balance to zero, making it impossible to make calls or send texts.
During development, I also encountered a very common issue where the balance rapidly dropped from 1.18 pounds to 0.18 pounds within a few minutes, causing data connections to be interrupted. This issue lasted for around 30 minutes before gradually recovering to the original amount, and data connections returned to normal.
Conclusion
As I write this, the email notifier has been in use for 4 months and has been working extremely well! Every time I pass by the box, I flip the mail lid to accelerate testing, and so far, it has triggered 483 times without any issues, and the battery life still has 61% remaining. It seems that even with the device located inside a metal box, it doesn’t affect the signal strength at all.
Schematics, firmware and everything can be found on my GitHub.