Mailbox notification

Guys, I am totally new to the imp and saw Jon Lee’s mailbox notifier. I really liked the project and have since sent him a message asking for some aid in putting this together. Unfortunately, I have not heard anything and was wondering if you guys could help me out on the code side of this. I put together the imp with a photocell, running off of a 9v battery, which I may change to AA’s, but that is something else. Anyway, I need help with the code. I want the little guy to wake up inside the mailbox when it sees light, connect and send me a pushover message that mail has arrived, and then fall back asleep. I want the imp to make sure it notifies me even if the mailbox has closed before it completes its task. In addition to this, I would like to know the battery life, if possible. That’s it. I have the code for the pushover functionality which I have gotten off of this forum but I need help with the rest.

In advance, I am very grateful for any assistance!



Quick initial question… is your mailbox within range of whatever WiFi signal you will be using?

Yes, it is. First thing I made sure of.

Do you have the datasheet or model# for the photocell?

As a sort of similar, and completely ridiculous project, I have considered doing the following: Whenever you see someone draw a sword in a movie, there is always a “sssccchhhhiiiiingggg” sound. Of course, that would mean that metal was scraping against metal, and no one would design a scabbard or sheath that wasn’t at least lined with something non-metallic to protect the sharp blade. (so no “sching” sound). I have considered installing a photocell or similar to the wooden knife block in my kitchen, so that when I pull out a knife, you hear: “Scchhhiinnnggg!”.
Ok, back to your project. I haven’t connected a photocell to an Imp yet, but I’ll pick some up at Radio Shack on the way home and see about some code… unless some else jumps in first.

I do.

One end is connected to gnd, the other to pin2 with a 10k ohm resistor between it and 3.3v.

The photocell is going to give you an analog output, but I believe if you set things up correctly, you could use a pin configured as DIGITAL_IN_WAKEUP. That way you could put the Imp into deep sleep, and have the photocell wake it up and trigger your push function. The cell would have to get enough light, even on an overcast day to push the output to near 3.3V or whatever is considered a HIGH signal. I don’t think there are any analog callbacks yet…

Pin 1 is the only pin that you can wake the imp from deep sleep on, and I’m pretty sure that is the best bet for saving battery power. I’ll test when I get home…

I wonder if attaching a small solar panel to the mailbox, it could charge a battery as well.

definitely could, but then you have to throw a solar charger and LiPo or something in the mix. I started messing with that on my hexbug project, but the solar panel was too weak.

Of course, I just remembered that a battery level indicator was a requirement, so maybe that could be swapped out for solar.

The Nora reference design basically does this project, and a number of other things.

BTW… do you really want to use a photocell for the project or would any sensor that did the trick be ok? A simple NO switch of some kind, tied to Pin 1 and connected to the mailbox door would work really nicely. Might freak out your mailman if it looked too sketchy though.

I do want to use the photocell and I did think about a solar cell but this is more than I originally desired. I was merely looking for some code samples. In other words, if you were to write the code for something like this, what would it look like. I am not asking for someone to do this for me, as I enjoy figuring things out myself. I just want to see some code that would be a best practice with conserving battery.

@JerryW Release 27.9 allows you to use the Imp’s internal phototransistor as a light sensor. Not sure if you can wake on it, but might be worth looking into.

This would be awesome! Though, I still need some code examples to help me get started!?

Thanks again for helping me out!

It seems like you should be able to do something like this:

function notify() { server.log("Imp awake."); } hardware.pin1.configure(DIGITAL_IN_WAKEUP, notify); imp.configure("Phototransistor", [], []); imp.sleep(3600); //Go to sleep for an hour

But that doesn’t work, if I connect pin 1 between a PT and resistor, even though flipping a light on and off over top of it drives it between states that the imp reads as High and Low if I configure the pin as a Digital Input and read the state.

I’m a bit confused about what happens when you try to put the imp to sleep right away, like my code does. It should go to sleep, but the connection indicator LED keeps blinking, and the wakeup function doesn’t fire. Someone else will need to fill in what I am missing.

wouldn’t you need an imp.wakeup in your notify function, then to get state?

I don’t think so, but there is something I don’t understand. You can configure pin 1, and only Pin 1 to wake the Imp from sleep on a rising edge, meaning that something else needs to supply it with a HIGH signal. I think that the phototransistor should be able to do that. My understanding of how it should work is, configure Pin 1 as DIGITAL_IN_WAKEUP, with a function to execute when it wakes. Then put the imp to sleep. When something pulls Pin 1 high, the Imp should wake and execute the specified function. Maybe I’m not putting it to sleep properly. It is sending me a power state log that it goes offline after 20 seconds, but then nothing.

what about using server.sleepfor(3600). The processor is effectively shut down and will only reawaken once the specified time is reached – or if the Wake-up Pin is triggered.

This works:
function sleep() { server.log("Imp idle. Sleeping."); imp.deepsleepfor(3600); //Go to sleep for an hour } function notify() { server.log("Imp awake."); } hardware.pin1.configure(DIGITAL_IN_WAKEUP, notify); imp.configure("Phototransistor", [], []); imp.onidle(sleep);

There’s a couple things you need to change in your code @jwehr.

When imp wakes up because of a hardware.pin1 interrupt, a handler isn’t executed. Instead, code is executed from the top of the file as if it had been a cold boot. Typically, when you’re working with hardware.pin1 wakeups, you should have a check at the top of your code to see if the imp wokeup from a hardware.pin1 interrupt.

Since the imp ONLY wakes on pin1 going high, we don’t need to check the value (although the imp won’t go back into a deep sleep until hardware.pin1 is low, so we need to check for that before we sleep).

Finally, imp.sleep() puts the imp into a shallow sleep, this won’t get you tons of battery saving. You actually want to use imp.deepsleepfor().

Your code will likely look something like this:

`//device code:
function hardwareWakeup() {
agent.send(“pin1wakeup”, null);
// if we woke up because of PIN1, notify agent
if(hardware.wakereason() == WAKEREASON_PIN1) {

// we can only go to deepsleep if hardware.pin1 is low
function TrySleep() {
if ( == 0) {
// if pin1 is low, we can sleep
imp.onidle(function() { imp.deepsleepfor(3600.0); });
} else {
// if we can’t go to sleep yet, wait a while, them try again

// configure pin1 to be a digital wakeup
// go to sleep