# pinHP Alarm Mail

Alarm Mail

About

Did you ever leave your game room for "just a minute" and forgot to come back before the next weekend? Receive an email that the system is still up and running after a specified amount of time. A custom script can be run when an alarm is triggered, to e.g. switch off a Shelly plug (an example script is provided). Alarm Mail is a pinHP Arcade Image built-in feature.

This is the stand-alone Raspberry Pi solution, to be connected to your LAN or Wi-Fi and to be switched on and off together with your gameroom toys. The file system is read-only, it is safe to simply power off the Raspberry Pi.

To be configured inside "rpi2jamma/.mail" on the SD card's DOS RPI_DATA partition. Write the image to an SD card (min. 4 GB) using the imager of your choice (Win32DiskImager is included with the distribution). This image is not as plug and play as the Arcade image, there is no fancy interface. It requires some configuration to set up your email account, as explained in the readme.

Download

Download pinHP alarm mail image for Pi3 / Pi4:
  pinhp_alarm_mail_pi3_pi4_28FEB24.zip
(File size 773 MB)

readme.txt


################################
###  pinHP alarm mail script ###
###  http://pinhp-image.com  ###
################################



########## SYSTEM SETUP ##########

SSH login:
USERNAME / PASSWORD
pinhp / root
root / root

To configure email, two files
MUST be edited:
ssmtp.conf and mail_alarm.txt, located in the .mail directory on the DOS RPI_DATA partition (mounted at /mnt/data)

OPTIONAL:
mail_start.txt, if you want to be able to receive start notification emails.
config.ini, which is preconfigured, see the comments inside.

If a script file 'customscript-alarm.sh' is found in the .mail directory, it will be run when an alarm is triggered.
A script file 'customscript-shutdown.sh' will be run when a system shutdown is initiated (available in pinHP Arcade Image only).

FILE SYSTEM:
The Linux file system is read only, it may be simply powered off.
Make it writeable: /usr/local/bin/writeenable.sh
Make it read only: /usr/local/bin/readonly.sh

Send a test mail: /usr/local/bin/test.sh


########## MAIL SETUP ##########

I have successfully tested a setup with a live gmx.com account.
As an example, let's assume the following fake acoount. Replace email and password with yours:

Email/Login: myrealmail@gmx.com
Password: Supers3cretpa$$word

### BEGIN example ssmtp.conf ###

mailhub=mail.gmx.com:587
RewriteDomain=gmx.com
AuthUser=myrealmail@gmx.com
AuthPass=Supers3cretpa$$word
AuthMethod=LOGIN
FromLineOverride=YES
UseTLS=YES
UseSTARTTLS=YES
TLS_CA_File=/etc/ssl/certs/ca-certificates.crt

### END example ssmtp.conf ###


In mail_alarm.txt / mail_start.txt you need to edit the "To:" and "From:" fields.
GMX requires the "From:" field to match your authenticated email address.
The "To:" field obviously, takes the address where you want the alarm mail to be sent to.

### BEGIN example mail_alarm.txt ###

To:myrealmail@gmx.com
From:myrealmail@gmx.com
Subject: ARCADE

Up and running!

### END example mail_alarm.txt ###

config.ini


### Alarm mail script configuration file ###
#
# Session log file: /tmp/mail_status.txt (log level 3)
# Permanent log file: rpi2jamma/.mail/log.txt (USB stick or DATA partition)
#


#
# Send email at start
# Possible values: Y|N
# Default: N
#
SEND_START_MAIL=N


#
# Send email at start in regular intervals, to confirm the setup still works (independent from $SEND_START_MAIL)
# 0 - Disabled
# Default: 7 days
#
START_MAIL_INTERVAL=7


#
# Shutdown system after alarm (available in pinHP Arcade Image only)
# Possible values: Y|N
# Default: N
#
SHUTDOWN=N


#
# Alarm and possible shutdown also during MAME game (available in pinHP Arcade Image only)
# Possible values: Y|N
# Default: N
#
ALWAYS=N


#
# Log level (permanent log file)
# 0 - No logging
# 1 - Script start and alarm mail notification only
# 2 - Detailed sendmail info (useful for email debugging)
# 3 - Log everything (overkill)
# N.B.: Log level 3 is *always* logged in temporary session log file
# Default: 1
#
LOGLEVEL=1


#
# Max. log file size (KB) until auto delete
# 0 - No auto delete (keep log forever)
# Default: 1024 (= 1 MB)
#
MAXLOGSIZE=1024


#
# Uptime minutes until alarm
# Default: 480 minutes (= 8 hours)
#
INTERVAL=480


#
# Check and log to session logfile every x idle minutes
# Default: 20 minutes
#
LOOP_TIME=20


#
# Max. wait for NTP time sync (seconds) after script start
# If Internet time is not available, logfile entries will show incorrect times
# Default: 60 seconds (= 1 minute)
#
MAX_NTP_WAIT=60

'customscript-alarm.sh' example script file


#!/bin/bash

# This file is run when an alarm is triggered, if found with its exact file name 'customscript-alarm.sh'
# Remove underscore from this example file to activate the script.

### Power off Shelly with a delay of $SHELLY_DELAY seconds, after $COUNT alarms ###
### 28 FEB 2024 ###

# Example command line for Shelly Gen1 password protected devices
# Custom username
# curl -u YourUsername:YourPassword "http://$SHELLY_IP/relay/0?turn=on&timer=$SHELLY_DELAY"

# Example command line for Shelly Gen2 password protected devices
# Username is always "admin"
# curl --digest -u admin:YourPassword "http://$SHELLY_IP/rpc/Switch.set?id=0&on=true&toggle_after=$SHELLY_DELAY"

# Example command line, no password protected device
# curl "$SHELLY_IP/relay/0?turn=on&timer=$SHELLY_DELAY"

SHELLY_IP="192.168.1.123"
SHELLY_DELAY=10
COUNT=2

#####

PERMLOG=$( cat /tmp/pinhp_variables | grep "PERMLOG=" | awk -F'"' '{print $2}' )
COUNTERFILE="/tmp/counterfile"

if ! [ -e "$COUNTERFILE" ]; then
  counter=1
  echo $counter > $COUNTERFILE
else
  counter=`cat $COUNTERFILE`
  ((counter++))
  echo $counter > $COUNTERFILE
fi

if [ "$counter" -ge "$COUNT" ]; then
  curl "$SHELLY_IP/relay/0?turn=on&timer=$SHELLY_DELAY"
  currentdate=$(date)
  echo "$currentdate - Shelly poweroff in $SHELLY_DELAY seconds ..." >> $PERMLOG
  sync
fi

Additional information:
https://wiki.archlinux.org/index.php/SSMTP