Remote Mail Notifier (and GPS Tracker)
At the moment I’m living in a block of flats and my mailbox is in a central lobby area. I’m often receiving small surprise packets from China and never know when the postman has been, usually between 8 and 12, sometimes even twice a day! I also don’t have any road facing windows that I can look out of for the bright red Royal Mail van.
So, I wanted to make something that can notify me of when the postman comes by and pops something into my mailbox. The previous place I lived at had the mailboxes at the far end of a gated car park, not great when it’s cold and wet!
I can’t have any cables going from the mailbox to my flat of course, so a wireless solution is needed. The first choice would have been to use WiFi, but the mailboxes are too far to get a signal to my back to my flat. I could probably use some kind of 433MHz radio module with a receiver in my flat, but I had recently come across A9G GPRS/GPS modules from A.I. Thinker that can have custom firmware loaded onto them. They only cost around £4 each, and it would also mean I don’t have to deal with annoying AT command parsing like with the popular SIM800.
In the end I decided to go with the A9G and add secondary functions of being a listening bug and GPS tracker since it also has a built-in GPS receiver!
Having only an A9G is no good in this project as its shutdown mode still draws a massive 240uA, and the battery would only last a few months between charges. An ATtiny402 was added to deal with power management of the A9G by controlling a P-MOSFET to completely remove power from the module when sleeping. The ATtiny402 also deals with detecting when the mailbox flap has been opened, when the GPS tracking mode button is pressed, when battery charging has completed (for sending a charge complete notification) and making sure the battery voltage is high enough before turning on the A9G. It communicates with the A9G via 1-wire UART, telling it the reason that it was woken up for and a few other things.
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.
An enclosure is needed to protect the device from being damaged by falling mail and to stop it from shorting out on the metal mailbox. The enclosure I had chosen had PCB mounting screw holes in an awkward place, being blocked by the battery holder and A9G module, which need to be placed on the same side as the antenna connectors for best RF performance, so the mounting points were removed and some of my own screw mounts were added. Some holes and slots were drilled out for an environmental sensor, microphone, USB charging port and reed switch input.
For no particular reason I also added a BME280 environmental sensor that can sense temperature, humidity and pressure. Unfortunately, only the first reading at start-up is reliable since the A9G module quickly warms up, causing the temperature reading to rise and humidity to fall. The sensor should probably be sticking outside of the enclosure!
General Operation
When the A9G powers up and finishes booting it uses 1-wire UART to ask the ATtiny402 for the reason that it was woken up for, the ATtiny402 then replies with those reasons (new mail, tracking mode, battery charging finished, switch stuck) along with a few stats like number of successful and failed notification attempts and if a balance check is needed. The module then connects to the GSM network, and if a balance check is needed an SMS is sent to 2732 and waits for the reply containing the remaining balance on the PAYG SIM. It then connects to GPRS, sends the notification to my web server via HTTP, disconnects from the network and sends a power off request to the ATtiny402. However, if tracking mode was requested then it instead stays connected to the GSM/GPRS network, turns on the GPS module and sends updates to my web server every 60 seconds.
Originally I had the A9G send emails via SMTP, but then I thought it would be better to send a HTTP request with JSON formatted data to a web server, where a PHP script can send the email and maybe in future save things to a database, like the GPS coordinates when in tracking mode and then show them on a map. Using HTTP also speeds things up and helps with battery life as the entire request can be sent in a single TCP data packet (as long as it stays under 1360 bytes otherwise it will be broken up to fit the MTU of the GPRS network), unlike SMTP which needs to have a few messages going back-and-forth with the server.
But! Emails are rather old fashioned, and I remembered that Telegram has an API for bots, so the final iteration now sends notifications and GPS tracking coordinates from the PHP script to a Telegram group chat!
The total amount of data sent and received by the A9G during a notification is around 1,582 bytes, which includes all the TCP SYN, ACK, FIN, etc packets and ignoring any re-transmissions. The JSON data is usually around 560 bytes and contains things like temperature, humidity, IMEI and various other stats. The HTTP header is around 190 bytes and the DNS lookup is around 240 bytes, depending on what kind of extra fields the DNS server responds with. The HTTP response from the server is a very minimal “ok” or “error” message. The PAYG SIM used in the notifier charges £0.05 per MB of data, so that means 1,000 mail notifications will only cost a tiny £0.09!
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 was much better than I anticipated. The A9G can repeatedly power on, connect to the GSM/GPRS network, send a HTTP request and power off up to 800 times on a single battery charge! Each cycle usually takes 18 – 23 seconds to complete, add an extra 8 seconds if doing a balance check which happens every 10 cycles. The current draw in sleep mode is almost nothing at less than 100nA. If mail is received 3 times per week, ignoring battery self-discharge, the battery should last for around 5 years and I’ll only be charged £0.06 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
By using the A9G module I thought I had got around the whole pain of unreliable AT command stuff, but instead I found out that the A9G SDK library is rather buggy and that development of the closed-source SDK stopped at the end of 2018. Apart from the buggy SDK, A9Gs are very easy to program, only a USB-serial converter is needed.
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 the A9G module to be extremely picky about antenna type, location and orientation. Placing the antenna in the wrong area will cause the microphone to pick up tons of GSM noise. A piece of wire cut to 3/4 wave length (125mm for 1800MHz), with one end attached directly to the antenna pad and the other end to the metal shield of the module near the antenna output seemed to induce the least amount of noise, however the signal strength wasn’t great. The miniature beginners erotic toy antenna had the best signal, but also created the most noise, interestingly the noise could be stopped by touching the gold part of the antenna. The PCB antenna had the middle ground; good signal and not too much noise if placed in the correct area.
The module sometimes has a hard time connecting to the GSM network, even with an excellent signal. Usually connection takes around 5 seconds, but if it hasn’t managed to connect within 60 seconds then the best thing to do is restart the module and it will then connect without a problem.
When performing a balance check the mobile operator would sometimes reply with a slightly incorrect balance. When attached to GPRS the operator will report a balance £0.25 less than when not attached, however I think this might be a safety feature so if a large file is accidentally downloaded you don’t end up with £0 balance and then unable to make calls or send texts.
Another very usual thing that happened one time during development was that the balance rapidly dropped over a few minutes from £1.18 to £0.18, disabling the data connection. This continued for around 30 minutes before it gradually began to climb back up to the original amount and data started working again. ¯\(°_o)/¯
Conclusion
At time of writing this post the mail notifier has been in use for 4 months and works extremely well! I’ve also been flipping the mail flap every time I walk past the box to accelerate testing and so far, it has been triggered 483 times without issue and still has 61% battery life remaining. Even though the device is inside a metal box there doesn’t seem to be any impact on the signal strength.
Schematics, firmware and everything can be found on my GitHub.
Comments
Skip to comment form