picofssd with mail functionality

Various Python Tools that helps handling of the UPS PIco

picofssd with mail functionality

Postby mrTheWheel » Wed Nov 04, 2015 3:53 pm

Hi,

I'm new to this UPS PIco stuff .. but I like it!

I couldn't find a script that would inform me of shutdown and powerup of the Raspberry PI so I made one myself. I'm not a python programmer but copy-paste from different files I found on the net. All you need to do is change the lines with the email addresses and make sure you can send emails from your RPi (see http://www.sbprojects.com/projects/raspberrypi/exim4.php or http://willem.aandewiel.nl/index.php/raspberry-pi-inrichten-met-postfix/).

In your /etc/rc.local script replace the line calling "picofssd.py" with this file (I called it "picofssdMail.py").

This is the code
Code: Select all
#!/usr/bin/env python
# Import the libraries to use time delays, send os commands and access GPIO pins
import RPi.GPIO as GPIO
import time
import os
import smtplib

sender = 'UPS_PIco@your.domain'
receivers = ['yourEmal@your.domain']

powerFail = """From: UPS PIco <UPS_PIco@your.domain>
To: Admin
Subject: Power Failure

Server is experiencing a Power Failure
"""

powerUp = """From: UPS PIco <UPS_PIco@your.domain>
To: Admin
Subject: Server PowerUp

Server is Powered Up
"""

#--------------------------------------------------------
def mailMessage(message):
   try:
      smtpObj = smtplib.SMTP('localhost')
      smtpObj.sendmail(sender, receivers, message)
      print "Successfully sent email"
   except SMTPException:
      print "Error: unable to send email"
# mailMessage()
#--------------------------------------------------------

mailMessage(powerUp)

GPIO.setmode(GPIO.BCM)                         # Set pin numbering to board numbering
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)   # Setup pin 27 as an input
GPIO.setup(22, GPIO.OUT)                      # Setup pin 22 as an output

while True:             # Setup a while loop to wait for a button press
    GPIO.output(22,True)
    time.sleep(0.25)       # Allow a sleep time of 0.25 second to reduce CPU usage
    GPIO.output(22,False)
    if(GPIO.input(27)==0):   # Setup an if loop to run a shutdown command when button press sensed
        mailMessage(powerFail)
        os.system("sudo shutdown -h now") # Send shutdown command to os
        break

    time.sleep(0.25)       # Allow a sleep time of 0.25 second to reduce CPU usage
mrTheWheel
 
Posts: 4
Joined: Wed Nov 04, 2015 3:34 pm

Re: picofssd with mail functionality

Postby Pimaster » Thu Jul 07, 2016 9:13 am

Hi,
Please also look for our new firmware and demands when we also implemented email functionality. It is open source.
BR PiM
Warmest Regards
PiM
---
Designing with Mentor Graphics PADS - www.pads.com
Please read and follow the PiForum rules
http://www.forum.pimodules.com/viewtopic.php?f=13&t=196
---
Pimaster
Site Admin
 
Posts: 1617
Joined: Fri Sep 14, 2012 7:50 am


Return to PIco Python and other Tools

Who is online

Users browsing this forum: No registered users and 1 guest

cron