Showing posts with label UNIX. Show all posts
Showing posts with label UNIX. Show all posts

Sunday, 29 November 2020

Command Line Mail setup OpenPlotter

Command Line Mail setup for OpenPlotter2. We use this to send an email triggered by the OpenCPN plugin, Watchdog.  

OpenCPN Watchdog plugin (command function) can execute a command to send out an email, we use this for remote monitoring by Gmail. 

Using SSMTP on openplotter2 to send email, well SSMTP does not seem to work any longer, try as I did, it didnt connect with gmail. 

Eventually I found msmtp as an alternative to SSMPT and I was able to get it up an running in no time and had OpenCPN Watchdog emailing me with alarms.  

First thing I did was open a Gmail account, I use a Gmail account specially for the boat because I needed to change one of the the security settings to be less secure during login. DO NOT use existing accounts or usual passwords because they can be exposed.

Open a new Gmail account and enable:

https://www.google.com/settings/security/lesssecureapps

1. Enabling less secure apps to access Gmail

2. Open your Google Admin console (admin.google.com).

3. Click Security > Basic settings .

4. Under Less secure apps, select Go to settings for less secure apps .

5. In the subwindow, select the Enforce access to less secure apps for all users radio button. 

6. Click the Save button.



You can install the msmtp package with:

$sudo apt-get install msmtp msmtp-mta

The configuration file is located here : /etc/msmtprc

Strangely enough, it is empty with no clues, which is different from the old ssmtp.conf file.

However, there is a description with details if you use the man $man msmtp, or just copy and paste the contents I have supplied changing the necessary acount details.

For gmail accounts, these are the specific elements you should put into /etc/msmtprc:  

$sudo nano /etc/msmtprc


# Generics, important tls needs to be on when using port 587
defaults
auth           on
tls            on
# following is different from ssmtp:
tls_trust_file /etc/ssl/certs/ca-certificates.crt
# user specific log location, otherwise use /var/log/msmtp.log, however, 
# this will create an access violation if you are user pi, and have not changes the access rights
logfile        ~/.msmtp.log
# Gmail specifics, you shouldnt need to change these
account        gmail
host           smtp.gmail.com
port           587
# You need to change these
from          your-vessel-gmail-account_name@gmail.com
user          your-vessel-gmail-account_name@gmail.com
password      your-vessel-gmail-account-password
# Default
account default : gmail


Save the file close nano and exit.


You can test if this now works with:

$echo 'your test message' | msmtp destination-email-address@xxxxxxx.com

I have not yet found a way to set the subject, there is no -s flag as with ssmpt.

However, if you also install mailutils:

$sudo apt-get install mailutils

Then these should work:

$echo 'message' | mail -s "Hello" destination-email-address@xxxxxxx.com

$echo 'message' | sendmail destination-email-address@xxxxxx.com

You can use both mailutils and mpack with msmtp just as you did with ssmtp.

So any scripts that you have used before with ssmtp, mailutils or mpack should work just the same using msmtp.

Setting up the command script file to use in OpenCPN plugin, Watchdog.

 You can have Watchdog execute a computer command. We wanted Watchdog to send us an email if our boat moves or the NMEA data stops. We stored the command to email a boat moving alarm in a shell file called: AnchorAlarmEmail.sh and for the loss of NMEA data: NMEA_Data_Missing.sh

First up create a new directory called bin in /home/pi  

$mkdir bin  

Reboot the computer, the bin file will be seen on reboot.

After creating the files with nano $sudo nano AnchorAlarmEmail.sh and $sudo nano NMEA_Data_Missing.sh  

Save them to the created /home/pi/bin directory I changed the permissions of the file to make them executable. 

$sudo chmod 755 AnchorAlarmEmail.sh 

$sudo chmod 755 NMEA_Data_Missing.sh


As an example of the file, here is the format of the NMEA data loss script file NMEA_Data_Missing.sh: 

#!/bin/bash
echo "GPS NMEA Data Loss" | mail -s "Danger Will Robertson" destination-email-address@xxxxxxx.com 

Or for the Anchor watch script file AnchorAlarmEmail.sh

#!/bin/bash
echo "Boat appears to be on the move. Best regards, the Watchdog " | mail -s "ON THE MOVE" destination-email-address@xxxxxxx.com

You may want to send the email to your most used account that will alarm on your phone when an email is received, we don't send mail to the same account as we receive it from, but this is a personal choice.

Before running the test to check if the shell script works, shutdown and reboot OpenPlotter

 Then test the shell file is going to work. After reboot open a terminal window and type in AnchorAlarmEmail.sh or NMEA_Data_Missing.sh 

If these files are working, you receive an email or two, then you can go ahead and configure WatchDog to select these files as a command to execute.

Tuesday, 5 February 2019

Getting the tide function to work OpenCPN - OpenPlotter


The Tides feature of OpenCPN does not include world-wide tidal stations.  So this means depending on where you are in the world the tide function for the area you are displaying on OpenCPN will either work or it won't. If you have found you cannot see tide information when you turn on the tide function in OpenCPN here is how we did it. I will do a PC installation first then move on to our OpenPlotter.

After replacing the supplied tide and current data with new files we have tide data at our finger tips.

On a fresh install of OpenCPN you will need to replace the files in the tcdata folder.

Firstly make sure OpenCPN is not running.

PC 
For OpenCPN installed on a PC go to the folder C:\Program Files (x86)\OpenCPN\tcdata to replace the existing files. The files are names HARMONIC.IDX and HARMONIC.

I downloaded the replacement harmonic files here tcdata-2023.zip 

If you would like a link to Terry's topics with up to date waypoints and mbtiles or KAP  and general info great for any cruisers in the east click here. On the page is a handy reference to set up OpenCPN for new users

I created a new folder in the tcdata folder called old harmonic and copied the old files into it. I then copied the tcdata.zip file into the tcdata folder and extracted the contents.

tcdata.zip extracted into C:\Program Files (x86)\OpenCPN\tcdata

Now launch the OpenCPN program and go to Options/Chart Files and click on the ‘Tides & Currents’ tab, then ‘Add Dataset..’, and navigate to where you have saved the ‘tcdata’ folder, open it and select the ‘HARMONIC.IDX’ file, click on ‘Open’, then click on ‘Apply’ and ‘OK’.


Now launch the OpenCPN program and go to Options/Chart Files and click on the ‘Tides & Currents’ tab, then ‘Add Dataset..’, and navigate to where you have saved the ‘tcdata’ folder, open it and select the ‘HARMONIC.IDX’ file, click on ‘Open’, then click on ‘Apply’ and ‘OK’.



Open the chart panel options and select show tide stations. 


What to look for tide station local to your location.
Tide station are easier to see at lower zoom levels.
In the above screen capture when at this zoom level, at a glance  you can see the state of the tide e.g flowing or ebbing. Above we see the arrows denote the tide direction and it is just about full tide as can be seen from the next screen capture below.

Right click on the tide symbol you require information of and this window will pop up.


OpenPlotter
For OpenCPN as part of the OpenPlotter package the install is similar but will require you to type in some commands in a terminal window. more on this later.

I start by downloading the zip file from the web straight into the Downloads folder of OpenPlotter. The file tcdata.zip.



The file is down loaded to the Downloads folder as seen here.

The file is extracted into the Downloads folder.
Right click on the tcdata.zip file and extract here. 
tcdata.zip file has been extracted to the Downloads folder

The files inside the extracted zip file need to be moved to the folder  /usr/share/opencpn/tcdata/ however there is a complication and you will not be able to copy the files directly to the folder using File Manager. This is so the files can be protected and to move the files into this folder requires the use of the sudo command in a terminal window. This allows the operator to assume the identity of the administrator (root) to move files around the system.

First up you need to open a terminal window.

Click on the raspberry symbol on the top left of the screen and select Terminal from the Accessories menu
A terminal window will open up. .

After the tcdata has been extracted to the folder in Downloads I  use the following commands, for more information look at the screen capture of the terminal window to see all the commands necessary to do the job.



The Commands and session used in the terminal window are listed below. 
The commands used to move over the newly downloaded files.


cd /usr/share/opencpn/tcdata will move you to the folder where the tcdata files are stored 

ls command lists the contents of a folder, in this example of the  /usr/share/opencpn/tcdata and as can be seen the answer is the two files HARMONIC and HARMONIC.IDX

sudo mkdir oldtcdata this command makes a folder called oldtcdata

sudo mv HARMONIC HARMONIC.IDX oldtcdata will move the two files HARMONIC HARMONIC.IDX to the folder called oldtcdata.

cd command changes directory (folder) in this instance cd /home/pi/Downloads/tcdata moves to the /home/pi/Downloads/tcdata folder.

ls command lists the contents of the folder in this example the reply is the two files HARMONIC and HARMONIC.IDX

sudo mv HARMONIC HARMONIC.IDX  /usr/share/opencpn/tcdata moves the two new files to the folder /usr/share/opencpn/tcdata  to be used by OpenCPN.

ls  /usr/share/opencpn/tcdata command lists the contents of the folder  /usr/share/opencpn/tcdata  and as can be seen the two files HARMONIC, HARMONIC.IDX and the folder oldtcdata.

So if these files are there then you should be ready to go, start up OpenCPN and see if you can see an tidal stations in your area.


Now launch the OpenCPN program and go to Options/Chart Files and click on the ‘Tides & Currents’ tab, then ‘Add Dataset..’, and navigate to where you have saved the ‘tcdata’ folder, open it and select the ‘HARMONIC.IDX’ file, click on ‘Open’, then click on ‘Apply’ and ‘OK’.


Open the chart panel options and select show tide stations.