Author Topic: MightyHat power outage alert and auto restart?  (Read 14492 times)

Tony Weil

  • Jr. Member
  • **
  • Posts: 60
  • Country: us
MightyHat power outage alert and auto restart?
« on: April 20, 2018, 01:20:43 AM »
My Pi Gateway/MightyHat has had several power outages lately that last beyond the duration of the backup battery. One was a several hour power outage when I was out of town and one was unknowingly self inflicted when an extension cord powering the Pi was unplugged. In both cases, I did not realize the Pi had shut down. Therefore it was not monitoring what I thought it was monitoring. When the Pi did have power again because the power company fixed their outage or I noticed an extension cord had been unplugged, it required pushing the power button on the MightyHat to restart the Pi once I realized it was not running. This was difficult to do when I was out of town.

I would like to figure out how to add two features. I want to be notified somehow if the line power to the Pi is off and when the Pi is about to shut down after the backup battery is almost exhausted.

The second feature is for the Pi to restart by itself when it has power after an outage. For example, if I am monitoring the house for low temperature in the winter when I am away, it doesn't do much good if the Pi is off, especially when I think the house is still being monitored.

In addition to the Pi Gateway, I have an inexpensive commercial home monitoring alarm that is simple, cheap and very effective called PumpAlarm from you guessed it, pumpalarm.com. For $220 and $50/yr it monitors and notifies you by text from a battery powered cellular modem for high and low temperature, water leaks (wet floor or sump pump), power loss and restablishment, and low battery 


Thanks,
Tony

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: MightyHat power outage alert and auto restart?
« Reply #1 on: April 20, 2018, 04:16:06 PM »
Are you using the stock MightyHat sketch?
There's some code around this line for low battery shutoff.
It would need modification to shutdown after an outage and power back up when power comes back.
I don't know when I would have the time to do it myself. I have not had outages in months and usually they are very short. With a 2000mAh battery my gateway Pi lasts for a few hours, are your outages longer than that?
Im in canton, is ann arbor worse with outages?

Tony Weil

  • Jr. Member
  • **
  • Posts: 60
  • Country: us
Re: MightyHat power outage alert and auto restart?
« Reply #2 on: April 21, 2018, 03:37:16 PM »
Thanks for taking a look at it.

Are you using the stock MightyHat sketch?

Yes, I'm using the stock MightyHat sketch unmodifed

I have not had outages in months and usually they are very short. With a 2000mAh battery my gateway Pi lasts for a few hours, are your outages longer than that?

Our last outage in March was only 1 hour but my Pi shut down. I have a 2000mAh battery. I just tested and it lasted 2 hours until the voltage was down to 3.5v or so before the Pi shutdown. (I'm almost positive that the backlight was turned off.)

I can't explain why the Pi shut down in 1 hour during the March outage. Maybe the backlight was on?? I'll test it again when the battery is recharged.

There's some code around this line for low battery shutoff.
It would need modification to shutdown after an outage and power back up when power comes back.

Are you saying that the code needs to shut down the 328 after an outage and power the 328 back up when power comes back on?

During my brief testing, I noticed that if the Pi is shutdown due to a low battery, I need to reboot the 328 and then push the Square Power Button to turn the Pi back on. If I don't reboot the 328, the Square Power Button does not turn on the Pi.

Assuming we could get the 328 to shut down and restart during a a low battery shutdown and power restoration, how would the Pi get restarted since the button won't be pushed? Or, did I miss the point. I looked through the code and schematic and this is a wonderfully complex beast you created.

I don't know when I would have the time to do it myself.

Thanks for pointing me in the right direction

I still can't think of any way to be notified of a power outage without a cellular modem. Once the power is out, there is no Internet connection. It is really nice to know when the power goes out in the winter and you are out of town. You get some warning that you might have a freeze problem to deal with since the furnace is out.

 

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: MightyHat power outage alert and auto restart?
« Reply #3 on: April 21, 2018, 09:08:51 PM »
The existing sketch only does the shutdown on low batt.
So to make it fully automatic and allow the restart by itself, this needs to be coded in specifically for this function.
You can get a small IPS to keep your router and critical stuff online while the outage is going on. Should not be a large investment.
The sketch is not that complex really, it only does a few basic things, in general i try to keep the code as simple as possible knowing the user base is very spread out.
I will try to do this sometime, just have been ultra busy doing other things lately. Maybe I can do it this coming week, cant give a promise though :)

Tony Weil

  • Jr. Member
  • **
  • Posts: 60
  • Country: us
Re: MightyHat power outage alert and auto restart?
« Reply #4 on: April 22, 2018, 01:30:38 PM »
I like your UPS idea for the Pi Gateway and home network. Do you have any idea why I'm only getting 2 hours run time on my Pi Gateway with a 2000mAh battery?

Thanks for taking a look at the code when you have a chance. I appreciate it. No rush at all.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: MightyHat power outage alert and auto restart?
« Reply #5 on: April 22, 2018, 05:41:45 PM »
2hrs with 2Ah sounds about right.

NorPad

  • NewMember
  • *
  • Posts: 2
Re: MightyHat power outage alert and auto restart?
« Reply #6 on: April 25, 2018, 07:30:42 PM »
Having a "fully automatic restart after the power is back" feature in the sketch would be very useful. Thumbs up for that.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: MightyHat power outage alert and auto restart?
« Reply #7 on: April 26, 2018, 09:16:45 AM »
Having a "fully automatic restart after the power is back" feature in the sketch would be very useful. Thumbs up for that.
I appreciate the input, it makes a difference!
It will require a fair bit of testing under different loads and different conditions. The battery will be a crucial part in this. A weak battery (or small) might not do the job. So I will have to document all this along with the findings during testing.

Kylix

  • Jr. Member
  • **
  • Posts: 67
  • Country: ro
    • Omologazioni veicoli
Re: MightyHat power outage alert and auto restart?
« Reply #8 on: April 26, 2018, 10:47:45 AM »
Another thumbs up here for this feature !!!!!!!   :)

perky

  • Hero Member
  • *****
  • Posts: 873
  • Country: gb
Re: MightyHat power outage alert and auto restart?
« Reply #9 on: April 26, 2018, 10:51:06 AM »
Some interesting solutions out there for Pi power supplies, like PiJuice:
https://uk.pi-supply.com/products/pijuice-standard

Tony Weil

  • Jr. Member
  • **
  • Posts: 60
  • Country: us
Re: MightyHat power outage alert and auto restart?
« Reply #10 on: April 29, 2018, 03:56:24 PM »
Here are some possible inexpensive ideas for adding capacity to the backup battery of the MightyHat so it is less likeky to become exhausted duriing a power outage. Auto restart in tbe sketch woukd still be helpful in the future.

Would these work?

1) Replace the 2000mAh “internal” (wedged between Pi and MightyHat) backup battery with an “external” larger Lipo battery that can still be charged with thr MightyHat charger, such as the following Adafruit 6600 mAh battery with cells in parallel? https://www.adafruit.com/product/353

2) Keep the 2000mAh backup battery between the Pi amd MightyHat. Hookup an external PowerBank type battery to the USB input of the MightyHat and plug it into the wall to keep it charged. Something like a 20000mAh battery from Walmart for $11.
https://www.walmart.com/ip/20000mah-Power-Bank-USB-Ultra-Thin-Portable-External-Battery-Charger/188792234?wmlspartner=wlpa&selectedSellerId=1291&adid=22222222228044326950&wl0=&wl1=g&wl2=m&wl3=108203185397&wl4=pla-302655261437&wl5=9011879&wl6=&wl7=&wl8=&wl9=pla&wl10=112549905&wl11=online&wl12=188792234&wl13=&veh=sem

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: MightyHat power outage alert and auto restart?
« Reply #11 on: April 30, 2018, 10:21:03 AM »
Tony,
I don't think such a large battery makes sense to JUST power the Pi.
I think a local small battery (2AH) or so which is compact and goes between the MHat and Pi is appropriate, should be able to power the Pi for at least 2 hours.
I thikn if you go the big battery route, it's a wiser investment into a proper 120v IPS which will be more expensive but can backup a lot more than just the Pi - all your critical computer/surveillance/router/internet stuff. I would suggest that.
Otherwise the 6.6AH battery is OK and the MHat JST battery connector being on the edge can allow an external battery to be used instead.
I still plan to add the auto-restart code, please bear with me :)

Tony Weil

  • Jr. Member
  • **
  • Posts: 60
  • Country: us
Re: MightyHat power outage alert and auto restart?
« Reply #12 on: April 30, 2018, 11:01:29 AM »
The UPS is a good idea all the way around. My reluctance is that my Pi, Modem/Router, and an all important Cable Amplifier are currently scattered in 3 locations. I could probably reduce that to two locations with a little work. Thanks again for all the ideas

Tony Weil

  • Jr. Member
  • **
  • Posts: 60
  • Country: us
Re: MightyHat power outage alert and auto restart?
« Reply #13 on: September 29, 2018, 01:10:44 PM »
I'm finally getting back to this. I did some calculations with a relatively inexpensive $45 350VA UPS . My Pi draws about 500mA or 2.5watts. A 350VA UPS with a 12V, 4Amp-hr battery can supply about 50 watt-hours. That should keep the Pi running for 20 hours or so, more than enough! Somehow, I thought I would need a larger UPS to get 4-8 hours of runtime. A $75 750VA UPS would provide double the runtime or could power other devices like cable modem/wifi.

I would still be interested in code that would autostart the PI after a the battery shuts down or a further pointer in the right direction. I know you are busy. Clearly, since I dropped this since April, it can't be too urgent. ;)

Thanks,
Tony


Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: MightyHat power outage alert and auto restart?
« Reply #14 on: October 01, 2018, 10:47:23 AM »
Yes I'm very busy :)
BUT here's a chance to experiment on your own. You can use the EEPROM to remember if a shutdown was done by the user or not. Then when power comes on it can check that bit and power the Hat if it wasn't a manual action. On the first deploy you'd have to write a different value in some other byte to know it's the first time running the sketch. So with 2 EEPROM bytes you can solve this puzzle. If both are 0 then write a 1 to the second to mark your first run, the first would serve as the power loss indicator. On manual shutdown you write a 1 to that bit to know it was a legit shutdown.

Tony Weil

  • Jr. Member
  • **
  • Posts: 60
  • Country: us
Re: MightyHat power outage alert and auto restart?
« Reply #15 on: October 01, 2018, 05:49:44 PM »
I'll give your suggestion a try.

Do I really care why the Pi was shut down? I was thinking all I needed to do was have the Pi startup whenever the 328p starts up. Your solution is more elegant.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: MightyHat power outage alert and auto restart?
« Reply #16 on: October 02, 2018, 10:01:59 AM »
Tony,
You mean an unconditional Pi-power-ON when USB power is available?
If I was able to run the Pi up to 2-3 hours on a small LiPo battery, I don't see why you can't do the same or more on your UPS, assuming the power outage will not last for days.
I always take the easy route. I don't have any fancy startup algorithm on my MHat, it's the stock firmware, the LiPo backup is enough to keep it going through any regular outage.

Tony Weil

  • Jr. Member
  • **
  • Posts: 60
  • Country: us
Re: MightyHat power outage alert and auto restart?
« Reply #17 on: October 02, 2018, 10:43:44 AM »
Felix,

To keep it simple, I was suggesting unconditional Pi Power On whenever the Pi Hat (Atmel 328p) boots up, regardless of how it is powered, Lipo or USB.

As you suggested, to keep it even simpler, I just ordered a UPS (900VA for Pi and other small equipment) and already have 2 hours worth of 2000mAh Lipo backup on the PI Gateway.

This will also help solve an additional silly failure mode other than power outage. I like to keep my Pi Gateway in my funky "Michigan" basement lab that is run by a number of extension cords strung across the basement ceiling. Very occasionally, the cords get accidentally disconnected without my realizing it. Now, the UPS should start beeping if that happens and I'll know to check the power. cords. I suppose I could have added a beep to the Pi Gateway if there was no USB power. Maybe it is time to move the Pi Gateway to a safer place or add some real electrical outlets?? ;)

Thanks a lot,

Tony

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: MightyHat power outage alert and auto restart?
« Reply #18 on: October 02, 2018, 01:02:09 PM »
Unconditional power ON is fine, I think most people might not prefer that though. Does your TV, PC or some other device automatically turn ON when plugged in? Maybe a router or simple device would, but not a PC for instance. I think for that reason the stock firmware will stay as is and will require user intervention to power back on.

The LiPo backup provides a means to keep it ON and it's only limited by the LiPo capacity. An external UPS will further limit the case where it reaches power off.

Inherently the design was made so that the MHat keeps going, and only shuts off if the backup power is depleted, which I've never encoutered since I use mine, and I only have the 1 LiPo backup attached to it without external UPS. I also live in Michigan, just east of you I believe ;)

But the change to power ON when 328 starts is pretty easy (not tested pseudo code):

Add this code at the end of setup:

Code: [Select]
PowerState = ON;
POWER(PowerState);

Tony Weil

  • Jr. Member
  • **
  • Posts: 60
  • Country: us
Re: MightyHat power outage alert and auto restart?
« Reply #19 on: October 02, 2018, 01:04:22 PM »
Thank for all the good info, Felix. I'll try a few things.

Tony

HeneryH

  • Full Member
  • ***
  • Posts: 229
Re: MightyHat power outage alert and auto restart?
« Reply #20 on: December 24, 2018, 04:23:34 PM »
There is code in the sketch that looks like an initial thought, but it must not be fleshed out in the various if/if/else/if paths.  It will take some study...

Code: [Select]
  //artificial power ON after a low battery shutdown
  if (PowerState == OFF && batteryLowShutdown && systemVoltage >= CHARGINGTHRESHOLD)
    reading = HIGH;
 

My system will be in a remote location so anything I can do to prevent me from making a 3 hr one-way drive just to push a button is worth my effort.