Mailbox notification

True, I guess I am assuming that you wouldn’t ever get close to maximum voltage with the PT… as you would have to have a really bright light directly over it. My thinking was that if you started with a voltage close to the Imp Pin max, it would be easier to get to the wake voltage with lower light. Maybe a 3.7v LiPo? Those stay pretty close to the rated voltage, right?

LiPo is 4.2v fully charged. Sorry, I thought you were talking about powering the imp from this voltage.

The phototransistor will give you more swing from dark to light really, much easier than trying to add another voltage rail.

No, I assume he would be using an April (and the P+,P- pads), and could tie the PT to whatever VIN was. I’m going to put one in a box and do some testing tonight. Seems like a PT in the back of a mailbox on a cloudy day isn’t going to see much light, right?

@JerryW - I should just draw it, but I have the photocell between pin 1 and +3.3v, and the other leg of the divider from pin 1 to ground.

I did spend a bit more time with the circuit this afternoon, and all looks pretty much as expected. (My photocell measures about 10M dark, and about 7k in moderate/dim light). One thing I did note is that the internal pulldown that @bi mentioned earlier does load the circuit a bit, so could possibly contribute to not getting a solid logic ‘1’ during light conditions. I’ll play around with the divider resistor a bit … a higher value may be needed (or no external pulldown at all, relying solely on the internal pulldown on the pin).

And, as @jwehr has commented, can probably just avoid any of this futzing around with values by going with a phototransistor. I’ll order up a couple of those to play around with.

I have it all working and notifying with a phototransistor, using 3.3V(Not in a mailbox). Doesn’t look like there is a reason to risk higher voltages, you just need to get a high enough resistor value to wake the Imp in really low light. I am currently using a 1M ohm resistor, and it doesn’t take much light to wake the Imp, as long as the light is directed at the PT. If you decide to use Twilio, here is fully working code, along with the code that @beardedinventor posted earlier. Twilio has a free service, you just get some Twilio advertisement with your text, or you can pay a tiny bit and fully customize it. The nice this is that you can SMS so you aren’t limited to any type of phone.

Make sure to fix the " in the URL… I’m not sure how to fix that in the forum
Also, you probably want to change the deep sleep timer for more than 24 hours. No need to wake more often than necessary.

Agent code:
`
//TWILIO**********************
const TWILIO_ACCOUNT_SID = “”;
const TWILIO_AUTH_TOKEN = “”;
const TWILIO_FROM_NUMBER = “”; // your phone no goes here
const TWILIO_TO_NUMBER = “”; // destination phone no

function send_sms(number, message) {
local twilio_url = format(“https://api.twilio.com/2010-04-01/Accounts/%s/SMS/Messages.json” , TWILIO_ACCOUNT_SID);
local auth = “Basic " + http.base64encode(TWILIO_ACCOUNT_SID+”:"+TWILIO_AUTH_TOKEN);
local body = http.urlencode({From=TWILIO_FROM_NUMBER, To=number, Body=message});
local req = http.post(twilio_url, {Authorization=auth}, body);
local res = req.sendsync();
if(res.statuscode != 201) {
server.log("error sending message: "+res.body);
}
}
//END TWILIO**********
device.on(“pin1wakeup”, function(v) {
server.log(“Imp was woken.”)
send_sms(TWILIO_TO_NUMBER, “The mail has been delivered.”);
});
`

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

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

// configure pin1 to be a digital wakeup
hardware.pin1.configure(DIGITAL_IN_WAKEUP);
imp.configure(“Phototransistor Wake-up”, [], []);
// go to sleep
TrySleep();
`

Thanks for the code post @jwehr. I use Twilio, so will load it up and get it going.

I guess I could stick it all in a box right next to my garage door, and then open the garage door during the day tomorrow to test it (from work). Imps testing Imps. Sweet. :slight_smile:

The 1M ohm resistor value isn’t going to be high enough. I’m getting intermittent success testing it in a box in my garage. Cloud cover is varying here today, so its a decent test.

I am thinking maybe I chose the wrong method of detection. Maybe a reed or momentary switch. This way if no one checked the mail that day I would not had to worry about any mail pieces blocking the cell. Not to mention, like you guys have said, a very cloudy/rainy day may not provide enough light. Just thinking out loud.

Yes, if I were going to set this up, I would definitely use a switch…but, that is really boring and I think that most of the people on this forum are going to value the use of, or the attempt at using non-traditional methods. IoT isn’t going to get really interesting if we just do the same old stuff with it, right?

True! Forget my comment. :wink:

:slight_smile: Your comment was very valid, but this method does work. I just toggled my garage door and there was sufficient light to wake the imp… just needs some tweaking.

Well, I have to get the phototransistor you are using. I don’t have one right now. Som will be ordering.

I wonder if there is a better PT for gathering sunlight. That one is 935nm, and obviously it was picked to use with mobile device screens, but I’m not sure if its optimal for sunlight. Does anyone know what the most prevalent wavelengths are for sunlight… either overcast or clear?

This looks interesting:
http://www.mouser.com/new/maxim-integrated/maximMAX44000/

Very low current draw. I wonder if the interrupt signal would work as the trigger on pin1?

@jwehr - I was looking at going ahead and using twilio but I saw no reference to a free version. Everything appears to be pay-as-you-go. Is this correct? I know you use it so I wanted to make sure.

I’d have to look again, but when I signed up…quite a few months ago now, there was free but limited functionality. I pay for it, since its like a fraction of a cent per SMS, If I recall.

it’s free on your developer account but there limits on no texts etc, When paying 3/4¢ per SMS and you pre purchased in blocks. Being in the UK it’s £10 not sure what is in US. You can also get a delivery report POSTED back to if required.

i decided to use sendhub. when you sign up for the free account, you get 100 sms messages free per month. Here is the code I am using, if anyone would like to do the same.

`
function sendhub() {
shcontactnum <- “YOUR CONTACT ID NUMBER”; //Get this from the url after you add a contact and click on that contact
shmsg <- “Your text message”;
local data =
{
“contacts”: [shcontactnum],
“text”: shmsg
};

local headers = {
    "Content-Type": "application/json"
};
    
local body = http.jsonencode(data);
local response = http.post("https://api.sendhub.com/v1/messages/?username=YOURSENDHUBMOBILENUMBER&api_key=YOURAPIKEY", headers, body).sendsync();     
server.log(response.statuscode + ": " + response.body);

}
`

Had some time to play around with the light detection circuit over the weekend, so thought I’d give an update with my observations.

I was interested in getting quantitative information re: ‘how light is light’ to establish a reliable trigger (i.e. light) level, so I picked up an inexpensive lux meter …

I made some measurements at the back of the mailbox under various conditions, and found out to be reliable I needed the circuit to trigger (i.e. pull pin 1 high) at much lower level light levels than I initially expected. I think a good design goal for this project is perhaps in the 5-10 lux range (due to mailbox shading and/or shadowing, late afternoon conditions, clouds, etc.) … way lower than some of the levels I had been testing at.

I initially looked at the basic voltage divider circuit which has been previously discussed. (To make it even simpler, I removed the resistor from pin 1 to ground, just relying on the internal pin 1 pulldown. I wouldn’t normally depend on something non-spec’s like this … but the circuit is fairly forgiving). With no light, the pulldown holds the input low … with light, the low photocell R allows the pin to pull high. Conceptually very simple … but there may be a problem. It turns out that at lower light levels the photocell R is still fairly significant (100k - 200k or so for my device), so, insufficient to pull pin 1 high.

Are there photocells with lower resistance at low light levels that would pull the pin high? Sure … but their dark R is also lower, perhaps only a few hundred K. Hmmm … not so good for battery life.

To work around this, I added a MOSFET stage between the voltage divider and pin 1. I’m still fine tuning the fixed R (and perhaps choice of photocell), but I’m pretty encouraged by initial test results … I’m getting reliable triggering down to the 2-3 lux range with this topology, which allows maintaining a high dark R for the photocell.

Anyway, that’s perhaps way more than enough at this point for a general forum post. I’ve got some more measurements and data, so please feel free to PM me if anyone would like to discuss in more detail.