-2

Brief: The question is about whether I should modify my Ubuntu 16.04 system to accomplish a task, or is there an easier way.


This is a complex system of computers, involving a mix of languages and operating systems.

After setting up the Ubuntu 16.04 primary system for the solar plant to speak, as per:

16.04 LTS How to make the system announce the time at the top of the hour with eSpeak

EXCERPT: Use espeak

sudo apt-get update
sudo apt-get install espeak

the next step is then to find a way for the remote data-logging systems (8 screens, generated mostly by Ubuntu 16.04 desktops and a Rpi3B) to command the primary Ubuntu 16.04 control system to verbally speak out alerts.

They all are running Python programs to do the logging and sending data to the Prime Ubuntu control system for archiving, image manipulation and uploading to the web hosting service.

Current data plots are created on the Prime Ubuntu 16.04 system using montage in Imagemagick to format the plots which can be seen here: https://www.SDsolarBlog.com/montage


To get voice alerts, the first attempt was to have them simply use scp to update a file in a directory and have the Ubuntu system detect the change to a file and respond by running a script that uses espeak to announce, for example, that a low-voltage condition exists. All the systems use RSA passwordless logins, so doing the copy is simple.

The go-to-tool for monitoring file and directory changes is incron - which is similar to cron except that it triggers based on file system events instead of the clock.

Here is the info on using incron:

Monitor file and directory activity with incron

EXCERPT:

Start with installing incron and inotify-tools on the Ubuntu 16.04 control system.**

sudo apt-get update
sudo apt-get install incron
sudo apt-get install inotify-tools
sudo apt-get autoremove

then use incrontab -e to set up a simple entry like so:

/home/me/alarms IN_CREATE /home/me/alarms/saylow24

where the contents of /home/me/scripts/saylow24 are

#!/bin/bash
espeak "Low Voltage on 24"
sleep 10
espeak "Low Voltage on 24"

...so the indoor Ubuntu 16.04 data logger can command the Prime console to speak out a voice alert when the 6 kWh battery bank falls below a threshold voltage.


Then I began to set up a similar script for the outside data logger for the solar panel positioning system, which has its own separate PV panel and battery. Cloudy days are a killer for it.


At that point I stopped and asked myself whether I really wanted to add the extra complexity of using the incron daemon on the Ubuntu 16.04 console at all. I prefer things to be simple.

Question: Is there an easier way to do this without having to make major changes to Ubuntu?

SDsolar
  • 3,169
  • 2
    I don't see what this has to do with Ubuntu. You already know how to SSH to Ubuntu and run espeak, the question seems to be about automating the Raspbian to do so, which is for [Unix.se]. – Olorin Nov 20 '17 at 18:08
  • Yes, that was in the other question you linked to. This is about connecting from the pi to the Ubuntu system. You already know how to SSH to Ubuntu and run espeak, the question seems to be about automating the Raspbian to do so, which is for [Unix.Se]. – Olorin Nov 20 '17 at 22:22
  • Ok... That still doesn't say why this question is about the Ubuntu systems and not the Pi. – Olorin Nov 21 '17 at 01:19
  • Why exactly does this question need a new tag called incron, since you're repeatedly creating it? After all, the one thing you're avoiding here is incron. – muru Nov 22 '17 at 00:36
  • @SDsolar well, if I wanted a solution that doesn't use bash, I wouldn't tag it bash, since that indicates that you want a solution using bash. Similarly for incron here. – muru Nov 22 '17 at 03:58

1 Answers1

0

incron works fine - it detects the scp or even a touch of a file and triggers the bash script. Yet there is no need to use a dedicated daemon just to detect a rarely-used signal. Using incron is like taking out a sledgehammer to swat a fly, even though it is the "Ubuntu way." Others may need it but I don't.


Spoiler: All it takes is adding one single line of Python code:

os.system("ssh me@prime ' ( /home/me/alarms/saylow12 ) ' &")

The rest of this answer explains how it is used, and may be TL;DR unless you are a systems engineer.

The data-loggers run Python programs to pick up input from USB/serial Nano units which do temperature and voltage readings.

Most are on Ubuntu Desktops and the one outside is a low-power Rpi3 which runs from a separate smaller solar panel and a 12V battery which is shared with the solar panel positioning system.

They all use Python programs to read the serial data from Nano microcontrollers which read temperatures and voltages, add the date and time to each data point, then save it into the daily data files in the Linux systems.


The logging programs look like so (excerpt):

import os
import serial
import datetime
import time

ser = serial.Serial("/dev/ttyUSB0",9600) ser.flushInput()

while True : linein = ser.readline() if linein[:5]=="LOW V" : print os.system('echo "SUBJECT: ALERT LOW VOLTAGE OUTSIDE" | ssmtp myemail4@gmail.com') etc to add date/time stamp then save in CSV file


They then produce plots which are displayed as live plots on monitors up-to-the-minute. The gnuplot programs are very simple:

set title "OUTSIDE 12V Battery"
set xlabel "Time"
set ylabel "Volts"
set yrange [11.5:15.5]
set grid
unset mouse
unset log
set key top left
set timestamp
set xdata time
set timefmt '%H:%M:%S'
set xtics format '%H:%M'
set style fill solid 1.0
set terminal x11
plot  15.5 lw 2 lc rgb 'navy' notitle, \
      14.5 lw 1 lc rgb 'black' t "Charger off", \
      14.4 lw 2 lc rgb 'blue' t "14.4V Charge", \
      12.7 lw 2 lc rgb 'green' t "12.7 Full", \
      12.45 lw 1 lc rgb 'black' t "Charger on", \
      12.1 lw 2 lc rgb 'black' t "12.1 V Alert", \
      "today.dat" using 2:15 skip 2 with boxes lw 1 lc rgb 'gray' t "Charger", \
      "today.dat" using 2:8 skip 2 with lines lw 1 lc rgb 'red' t "Battery"
pause 15
reread

Note that the reread command in gnuplot causes the plot to update so they are displayed in real time on the screen when the logging system is accessed by Windows RDP via WiFi.
6 plots fit on a single large screen. Then two others on a different screen.

enter image description here

Every 15 minutes the Prime Ubuntu 16.04 system gathers all the plots, applies some Imagemagic Fu and sends them up to https://www.SDsolarBlog.com/montage


To the point: When the voltage is too low the logging programs are already set up to send an email alert using this code (or similar):

os.system("echo 'SUBJECT:  ALERT - Low Voltage on 24' >>temp")
os.system("cat temp | ssmtp myaddress@gmail.com")
  • gmail is set up to apply an alert label to any message with ALERT in the subject line

  • My smart phone has a special notification tone for incoming gmail messages with that label

  • But while working near the Ubuntu system the phone may not be within earshot.


All that it takes to command the Ubuntu system speak it out audibly is to add a single line after the email is sent, like so:

os.system("echo 'SUBJECT:  ALERT - Low Voltage on 24' >>temp")
os.system("cat temp | ssmtp myaddress@gmail.com")

os.system("ssh me@prime ' ( /home/me/alarms/saylow24 ) ' &")

where the file saylow24 is the same as in the question.

Note that the quote marks are normal apostrophes.

The ampersand is used so this signal command doesn't block the Python script while it executes in the background on the Ubuntu mothership.

Even better, I now have it do this:

os.system("echo 'SUBJECT:  ALERT - Low Voltage on 24' >>temp")
os.system("cat temp | ssmtp myaddress@gmail.com")
os.system("ssh me@prime ' ( espeak -s 100 E-Mail-Has-Been-Sent ) ' &")
os.system("ssh me@prime ' ( /home/me/alarms/saylow24 ) ' &")

Then for the outside voltage logger it is similar, except that it triggers the saylow12 program.

The additional line added there is:

os.system("ssh me@prime ' ( /home/me/alarms/saylow12 ) ' &")

where the file saylow12 is very similar:

#!/bin/bash
for i in {1..5}
do  
  espeak "Low Voltage on 12"
  sleep 10
done

This bash script is used so the alert repeats every 10 seconds because it demands rapid action or the outside Rpi3 CPU will lose power which causes the solar panels to cease tracking the sun.


As shown above, arbitrary informational messages can be spoken this way:

os.system("ssh me@prime ' ( espeak High-Temp-Inside-Control-Box ) ' &")

Note the use of hyphens to denote word spaces.

This only needs to be spoken once per data point since I can't change the weather.


Now all the data loggers have a way to give verbal feedback at the Ubuntu console.

Very simple to implement and it works well.


SDsolar
  • 3,169
  • This answer is a Rube Goldberg contraption. You haven't said which file you had to edit. Without that, I don't see why anybody would use Python instead of a simple shell script. Worse, you complain about qouting, but don't use the subprocess.call function to avoid a layer of qouting. – Olorin Nov 20 '17 at 22:38
  • So what's the secret? Please don't hold back on us. – SDsolar Nov 21 '17 at 00:55
  • The secret to what? You're the one talking about logging programs and editing them without saying where they are – Olorin Nov 21 '17 at 01:18
  • OK, now I see what subprocess.call is, and it isn't relevant here. It is for calling other processes on the same system from within python. What is being done here is issuing a single command to run in the background on the the Prime computer. (I also see a lot of caveats in the use of subprocess.call, as spelled out here: https://docs.python.org/2/library/subprocess.html) It would be overkill in the same way incron is. – SDsolar Nov 24 '17 at 08:35