Mastodon

Sunday, 28 March 2021

Some more Pi

 Been a while since I've done anything on here, let's face it 2020 was a washout and the first bit of 2021 has gone that way too. It does mean that things at home have come higher up the list of things to do and one of those things was to add a camera to a Raspberry Pi, point it at the sky and take pictures. The main idea was to get some shots of the stars moving but the clouds looked interesting too.

The first attempt was a quick lash up stuck in a plastic bag to give it some water proofing and put on the floor in the garden out of site of the street light at the back. This was a Raspberry Pi 2 and the version 1 camera which can go down to about a 6 second exposure, nothing lower than that so the stars didn't really stand out that well and there is a lot light pollution too.

Here's my first test, not great but it seems to show a bit of promise if we can get a clear night and even more so if we could try somewhere with a darker sky.


The other plan or project was a camera at the from of the house which apart from doing some timelapse would also take a picture every day at dawn, noon and sunset for a year from the 21st of March. For no other reason than it can be done. The noon bit was fairly straight forward, the sunrise and sunset were going to be harder. Until I came across something called "Heliocron". You can use this app to run a task at various times of the day and it calculates sunrise and sunset for you. Many thanks to the author for this.

Let's get started with my SkyCam as I call it, to run properly the Raspberry Pi needs a connection to a network so it can get the correct time. I have a plan B for a travel version but I'll come back to that.

I'm going to build this with a Raspberry Pi 3, a version 2 camera which gives 8Mp and shutter speeds down to 10 seconds. I need a waterproof case so I can stick it outside and a power and ethernet lead out of it. There will be some code which I've borrowed and modified from various searches, I can't remember where I got them but if it's yours then it's much appreciated.

This may not be the best way of doing it but it seems to work.

The finished article looks like this, I've managed to find an old Tripod bush so I can mount it outside and adjust the position.

Using the above mentioned Heliocron, it changes the exposure settings as it gets darker and lighter to keep some detail. I'm not clever enough to get it to take a measurement and do it automatically.



The script that will be taking the pictures is called:

mtshutter

There's going to be a couple of versions of it called mtday, mtdusk, mtearly and mtnight.

I stick the mt in front as it makes them easier to find.

I'm storing them in the /bin folder, I can't see any reason as to why not but if I'm wrong I'm sure somebody will tell me.

They all look fairly similar except for the picture taking settings, starting with mtshutter:


The pictures once taken are stored on a small file server mounted at /mnt/webcam in their own folder called skycam1, there may be another one in the future. You can of course change the location to wherever you want.




#!/bin/bash

DATE=$(date +"%Y-%m-%d_%H%M%S")

raspistill -awb auto -bm -q 95 -w 3280 -h 2464  -o /mnt/webcam/skycam1/SkyCam1-$DATE.jpg





mtearly

#!/bin/bash


DATE=$(date +"%Y-%m-%d_%H%M%S")

raspistill -awb auto -bm -q 95 -w 3280 -h 2464 -ISO 300 -ss 8000000 -o /mnt/webcam/skycam1/SkyCam1-$DATE.jpg


mtday


#!/bin/bash


DATE=$(date +"%Y-%m-%d_%H%M%S")

raspistill -awb auto -bm -q 95 -w 3280 -h 2464  -o /mnt/webcam/skycam1/SkyCam1-$DATE.jpg


mtdusk

#!/bin/bash


DATE=$(date +"%Y-%m-%d_%H%M%S")

raspistill -awb auto -bm -q 95 -w 3280 -h 2464 -ISO 100 -ss 10000000 -o /mnt/webcam/skycam1/SkyCam1-$DATE.jpg


And finally mtnight

#!/bin/bash


DATE=$(date +"%Y-%m-%d_%H%M%S")

raspistill -awb auto -bm -q 95 -w 3280 -h 2464 -ISO 800 -ss 10000000 -o /mnt/webcam/skycam1/SkyCam1-$DATE.jpg



Make these executable with chmod +x /bin/mtshutter and so on. All these do is take one picture, which isn't much use for a timelapse camera.

Next we make a script to continually take pictures


This is mttakepic


#!/bin/bash


for (( ; ; ))
do

sh /bin/mtshutter

done


All it does is continually call the mtshutter script

We can get this running as a service on startup by creating another file:

 nano /etc/systemd/system/takepics.service

Paste this into it

[Unit]
Description=Take Pictures
After=network.target

[Service]
ExecStart=/bin/mttakepic
WorkingDirectory=/mnt/webcam
StandardOutput=inherit
StandardError=inherit
Restart=always
User=root

[Install]
WantedBy=multi-user.target

And activate it with systemctl enable takepics.service


All that will happen  now is the service will start, take some pictures, it will get dark and you won't see anything till it gets light again, this is where the other files come in to play.

At the moment we are running the daytime script, as it gets darker we need to shift to the dusk option.

nano /bin/duskmode

#!/bin/bash
echo "Dusk Mode" > ~/mode.log
service takepics stop
sleep 1
cp /bin/mtdusk /bin/mtshutter
service takepics restart
sleep 1


Then 

nano /bin/nightmode

#!/bin/bash
echo "Night Mode " > ~/mode.log
service takepics stop
sleep 1
cp /bin/mtnight /bin/mtshutter
service takepics restart
sleep 1


nano /bin/earlymode

#!/bin/bash
echo "Early Mode" > ~/mode.log
service takepics stop
sleep 1
cp /bin/mtearly /bin/mtshutter
service takepics restart
sleep 1


nano /bin/daymode

#!/bin/bash
echo "Day Mode" > ~/mode.log
service takepics stop
sleep 1
cp /bin/mtday /bin/mtshutter
service takepics restart
sleep 1

You will need to get Heliocron now which can be found here

I've made it executable and again put it in the /bin folder.

Now run crontab -e

# Change to early light
1 2 * * * /bin/heliocron --latitude 51.6214N --longitude 3.9436W wait --event nautical_dawn && /bin/earlymode

# Normal Daylight
15 2 * * * /bin/heliocron --latitude 51.6214N --longitude 3.9436W wait --event civil_dawn && /bin/daymode

# Getting Dark
05 10 * * /bin/heliocron --latitude 51.6214N --longitude 3.9436W wait --event civil_dusk && /bin/duskmode

# Dark
02 10 * * * /bin/heliocron --latitude 51.6214N --longitude 3.9436W wait --event nautical_dusk && /bin/nightmode

These are coordinates for Swansea, you will need to get yours for it to work properly.

At certain time of the day, the mtshutter will be overwritten and hopefully, we'll get a proper series of pictures which can then be compiled into a timelapse movie.


I've used mencoder to do it, you can install it with sudo apt install mencoder, I've got another script called mttimelapse which consists of the following.

#!/bin/bash
echo "Creating list of pictures"
ls *.jpg > stills.txt

echo "What device are these imaged from ?"
read camname


DATE=$(date +"%Y-%m-%d_%H%M")

mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=1920:1080 -o /mnt/webcam/TimeLapse/$camname-$DATE.avi -mf type=jpeg:fps=24 mf://@stills.txt

It creates a list of pictures, asks you for a camera name and then compiles them into a movie that looks like this:


As soon as I have some decent shots and a clear night I'll upload a completed sequence.











Wednesday, 13 June 2018

SSH Stuff

I've just managed to install a load of stuff onto my desktop as I didn't realise I wasn't connected to a remote machine via ssh.

G+ gave me a suggestion about changing the login colours on the remote machine so a bit of searching came up with this.

Connect via ssh and run

nano .bashrc

Add this lot in just above the last fi

#Make prompt colored when logged in remote
export NON_LOCAL_LOGIN=`env | grep SSH`
if [ "$NON_LOCAL_LOGIN" != "" ]; then
       PS1="\033[33m[\u@\h:\w]#\033[0m "
fi;

Save and close and you're sorted

Original answer came from here

Saturday, 23 December 2017

Spotweb on Bionic Beaver

Wanted to get Spotweb running on a local machine to do some newsgroup indexing, lots of instructions out there for Raspberry Pis and older versions of Ubuntu with php 5. I've decided to go and install it on a virtual server running Bionic Beaver, it was a lot easier than I expected.

Start off with installing the OS and then updating it.


sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade -y
sudo apt-get autoremove -y
sudo shutdown -r now

Then install the required apps:

sudo apt-get install mysql-server php-mysql php-curl php-gd php-cli nginx openssl php-fpm git zip mc php-mbstring php-xml -y


Change a couple of files:

sudo nano /etc/php/7.1/fpm/php.ini

Change these lines

date.timezone = Europe/Copenhagen
memory_limit = 512M


And do the same here:

sudo nano /etc/php5/cli/php.ini




Make the webserver config file:

sudo nano /etc/nginx/sites-available/spotweb

Copy and paste this into the file:

server {
listen 80;
server_name htpcguides.crabdance.com, 192.168.40.120;
root /var/www;
index index.html index.htm index.php;

location /spotweb {


        satisfy any;


        if ($uri !~ "api/"){

                rewrite /api/?$ /spotweb/index.php?page=newznabapi last;
        }
location ~ \.php$ {
                try_files $uri =404;
                include fastcgi_params;
                fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
                fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
}
}
}

Save it and then close nano


Activate the configuration

sudo -i

unlink /etc/nginx/sites-enabled/default

ln -s /etc/nginx/sites-available/spotweb /etc/nginx/sites-enabled/spotweb


Install Spotweb:


git clone https://github.com/spotweb/spotweb /var/www/spotweb

Sort out the permissions:


chown -R www-data:www-data /var/www/spotweb

Create the database, replace the word password with your password but leave the ' ' in place:
mysql -u root -p

CREATE USER spotwebuser@localhost IDENTIFIED BY 'password';
CREATE DATABASE spotwebdb;
GRANT ALL PRIVILEGES ON spotwebdb.* TO spotwebuser@localhost IDENTIFIED BY 'password';
FLUSH PRIVILEGES;


Go for a reboot


sudo shutdown -r now

And now finish the configuration off with your browser.


Go to http://ip.address/spotweb/install.php

Do an initial download and then setup a cron job to run:

php retrieve.php from the /var/www/spotweb directory.


Friday, 22 December 2017

Nextcloud install, the easy way.

Here's an easy way to setup a NextCloud server, a bit less customisable than installing it the manual way.

In the past, I've messed round with installing, SQL, Apache and so on, I thought I'd give the Ubuntu snap package a go.

I've run up Ubuntu 20.04 server, installed SSH onto it so I can manage it from command line and next it was time to install NextCloud.

sudo snap install nextcloud

That's it, it's done and installed, just a few more things to get it working properly and securely with ssl enabled.

Adjust the memory limits with:

sudo snap set nextcloud php.memory-limit=-1

Tell it which ports to listen to with:

sudo snap set nextcloud ports.http=80 ports.https=443

Restart Apache with:

systemctl restart snap.nextcloud.apache

Configure https and install lets encrypt with:

sudo nextcloud.enable-https lets-encrypt

Finish off the configuration by pointing your web browser at the dynamic domain you have set up for it.

https://domaine.name or https://ipaddress

The final configuration is done via web browser.

It really was that easy

Friday, 12 May 2017

Shell Script stuff in Linux

I've wanted to make up a simple script that runs an update on my desktop and then either shuts down, reboots or continues depending on what parameters I add to the end.

Something like;

update reboot

update shutdown

or just update to install the updates and do nothing else.

I managed this with good old DOS many years ago and started to look up how to do this with Linux, I have to say that the vast majority of instructions are based up using foo and bar and are not the easiest things in the world to understand, here's one of the easier examples:

 $ mytest foo bar quux
   There are 3 arguments to mytest: foo bar quux
   first argument: foo
   second argument: bar
   third argument: quux
   here they are again: foo bar quux

I really can't work out how that helps me but it gave me a starting point.

So in plain language here's what I want to do, I've created a script with:

sudo nano /bin/mtupdate

The main part of the script checks for updates and installs the updates:

sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade -y
sudo apt-get autoremove -y

It then goes onto check what additional parameters have been passed and this is the bit that caused me heartache.

So if I run mtupdate shutdown it does the above and then this bit

# checking for shutdown parameter
if [ "$1" = "shutdown" ]; then
echo "Shutting Down in 10 seconds"
# gives you a chance to change your mind
echo "Press CTRl+C to abort"
sleep 10
echo "Shutting Down"
# shuts down
shutdown -h now
else
if

Then it became harder as I wanted to check for a reboot parameter, to get it all in I had to do the following;

# checks for reboot parameter
if [ "$1" = "reboot" ]; then
echo "Rebooting in 10 seconds"
echo "Press CTRl+C to abort"
sleep 10
echo "Rebooting"
shutdown -r now
else

# checks for shut down parameter
if [ "$1" = "shutdown" ]; then
echo "Shutting Down in 10 seconds"
echo "Press CTRl+C to abort"
sleep 10
echo "Shutting Down"
shutdown -h now

else
# no parameters
clear
echo .
echo.

echo "Updates Completed, please reboot as soon as you can"

fi fi

You have to have a fi at the end for every if in the list


The end product looks like this:

# mtupdate reboot or shutdown
echo "Running Update"
apt-get update
echo "Running Upgrade"
apt-get upgrade -y
echo "Running Dist Upgrade"
apt-get dist-upgrade -y
echo "Running Cleanup"
apt-get autoremove -y
apt-get install -fy

# checking for reboot parameter
if [ "$1" = "reboot" ]; then
echo "Rebooting in 10 seconds"
echo "Press CTRl+C to abort"
sleep 10
echo "Rebooting"
shutdown -r now
else

# checking for shutdown parameter
if [ "$1" = "shutdown" ]; then
echo "Shutting Down in 10 seconds"
echo "Press CTRl+C to abort"
sleep 10
echo "Shutting Down"
shutdown -h now

else
# no parameters entered
clear
echo .
echo .
echo "Updates Completed, please reboot as soon as you can"
fi fi

To finish it off I ran 

sudo chmod +x /bin/mtupdate 

To make it executable

My suggestion to the people that write help stuff is to use a proper example, don't use foo bar quux as this means bugger all to people like me.

My next bit of scripting is going to revolve around taking two parameters and running something based on them, such as

mtbackup source destination

Where mtbackup runs something like rsync or cp or mv

I'll come to that next time round


Tuesday, 21 June 2016

OpenVPN Server on Debian Jessie

It was time to setup a way of getting secure access to my network while away and also to get a secure connection to the Interwebs while away from home, it also means that I can watch BBC Iplayer as if I'm at home.

I'm installing this on an old machine running Debian Jessie, it should work on a Raspberry Pi too, the only things I have installed during the initial setup process as the standard Debian utilities and ssh server so I can do everything remotely.

I've found lots on instructions out there but the one from this website was the easiest to follow, I've modified it slightly to make it easier to get at the keys.

I've modified a few things myself

First thing is to ensure we are up to date, lets switch to root for the install

su

then

apt-get update
apt-get upgrade


Time to start installing stuff

apt-get install openvpn easy-rsa

Then copy some example files over to make the job easier

cp -r /usr/share/easy-rsa/ /etc/openvpn
mkdir /etc/openvpn/easy-rsa/keys


Now we edit the certificate variables

nano /etc/openvpn/easy-rsa/vars


# These are the default values for fields
# which will be placed in the certificate.
# Don't leave any of these fields blank.
export KEY_COUNTRY="changeme"
export KEY_PROVINCE="changeme"
export KEY_CITY="changeme"
export KEY_ORG="example"
export KEY_EMAIL="changeme@example.com"
export KEY_OU="changeme"

# X509 Subject Field
export KEY_NAME="server"

Time to generate some stuff and go and have a coffee, on a Pi, this may take some time

openssl dhparam -out /etc/openvpn/dh2048.pem 2048


Now we make the server certificate keys:

cd /etc/openvpn/easy-rsa
. ./vars
./clean-all
./build-ca
./build-key-server server

Let's copy them to where they belong

cp /etc/openvpn/easy-rsa/keys/{server.crt,server.key,ca.crt} /etc/openvpn

Now time to make some changes to the network settings:

echo 1 > /proc/sys/net/ipv4/ip_forward

And let's make the changes permanent with:

nano /etc/sysctl.conf

Look for the following bit:

# Uncomment the next line to enable packet forwarding for IPv4
# net.ipv4.ip_forward=1

Then remove the # from the second line so it looks like this:

# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1

Now we make the server config file:

nano /etc/openvpn/server.conf

Paste this lot into the empty file, this will run the VPN server on port 1194.


port 1194
proto udp
dev tun
ca ca.crt
cert server.crt
key server.key 
dh dh2048.pem
server 10.90.10.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 8.8.8.8"
client-to-client
duplicate-cn
keepalive 10 120
cipher AES-128-CBC
comp-lzo
user nobody
group nogroup
persist-key
persist-tun
status logs/status.log
log-append logs/openvpn.log
verb 3


Now we make the log files:

mkdir -p /etc/openvpn/logs
touch /etc/openvpn/logs/{openvpn,status}.log


And let's do some firewall configuration:

iptables -t nat -A POSTROUTING -s 10.90.10.0/24 -o eth0 -j MASQUERADE
iptables-save

Now let's restart the server to put the changes into place:

systemctl restart openvpn@server.service

Now the original instructions came with a script file to help you create new keys for each user and device, pointless changing it.

nano /etc/openvpn/gen-client.sh

Paste this lot in:

#!/bin/bash

username=$1

# Generating key
echo "Generating key for user ${username}"
cd /etc/openvpn/easy-rsa/
source vars && ./pkitool ${username}
cp /etc/openvpn/clients/.tmp/.tmp.ovpn /etc/openvpn/clients/.tmp/${username}.ovpn
echo "Done"

# Adding ca certificate to ovpn client configuration file
echo "Adding ca certificate to ovpn client configuration file"
echo "<ca>" >> /etc/openvpn/clients/.tmp/${username}.ovpn
cat /etc/openvpn/easy-rsa/keys/ca.crt | grep -A 100 "BEGIN CERTIFICATE" | grep -B 100 "END CERTIFICATE" >> /etc/openvpn/clients/.tmp/${username}.ovpn
echo "</ca>" >> /etc/openvpn/clients/.tmp/${username}.ovpn
echo "Done"

# Adding user certificate to ovpn client configuration file
echo "Adding user certificate to ovpn client configuration file"
echo "<cert>" >> /etc/openvpn/clients/.tmp/${username}.ovpn
cat /etc/openvpn/easy-rsa/keys/${username}.crt | grep -A 100 "BEGIN CERTIFICATE" | grep -B 100 "END CERTIFICATE" >> /etc/openvpn/clients/.tmp/${username}.ovpn
echo "</cert>" >> /etc/openvpn/clients/.tmp/${username}.ovpn
echo "Done"

# Adding user key to ovpn client configuration file
echo "Adding user key to ovpn client configuration file"
echo "<key>" >> /etc/openvpn/clients/.tmp/${username}.ovpn
cat /etc/openvpn/easy-rsa/keys/${username}.key | grep -A 100 "BEGIN PRIVATE KEY" | grep -B 100 "END PRIVATE KEY" >> /etc/openvpn/clients/.tmp/${username}.ovpn
echo "</key>" >> /etc/openvpn/clients/.tmp/${username}.ovpn

mkdir -p /etc/openvpn/clients/${username}
mv /etc/openvpn/clients/.tmp/${username}.ovpn /etc/openvpn/clients/${username}/${username}.ovpn
cp /etc/openvpn/easy-rsa/keys/${username}.{crt,key} /etc/openvpn/clients/${username}
cp /etc/openvpn/easy-rsa/keys/ca.crt /etc/openvpn/clients/${username}

cd /etc/openvpn/clients; tar -jcf ${username}.tar.gz ${username}/


chmod 0777 -R /etc/openvpn/clients

echo "Done"

echo "
=========================================================================================

            Configurations are located in /etc/openvpn/clients/${username}

    ---------------------------------------------------------------------------------

                        Download friendly version with:

         'scp root@`hostname -f`:/etc/openvpn/clients/${username}.tar.gz .'

=========================================================================================
"

exit 0


Save it and then make it executable with:

chmod +x /etc/openvpn/gen-client.sh

Next we have to create the template file for this to use:

mkdir -p /etc/openvpn/clients/.tmp/

nano /etc/openvpn/clients/.tmp/.tmp.ovpn


Paste this in, change example.com for your external IP or server address

client
verb 1
dev tun
proto udp
port 1194
remote example.com 1194 udp
remote-cert-tls server
resolv-retry infinite
nobind
persist-key
persist-tun
comp-lzo
cipher AES-128-CBC

Now, let's make some keys:

cd /etc/openvpn/


replace username with your username, I'm going to install this onto an S5 so it will be freds5 or something.

./gen-client.sh username


To make it easy to get the files off the server and onto my device, I've decided to install Samba and setup the client keys folder as a Windows share, this is how this is done.

apt-get install samba samba-common

Once it's finished we edit the Samba config file:

nano /etc/samba/smb.conf

Change the workgroup name at the top of the file and you can also add:

netbios name = servername under it if you want.

Add the bottom add the following


[VPNKeys]
        path = /etc/openvpn/clients/
        browseable = yes
        public = yes
        writeable = no

Restart the server with:

service smbd restart

Just got to change the folder permissions to make sure we can get the files off:

chmod 0777 -R /etc/openvpn/clients

Onto my phone now, I've installed OpenVPN Connect from the play store, then I've copied the files from the Windows share into dropbox, then saved them into a folder on the phone called VPN, you could just install a file browser and do the same.

Then import the .ovpn file into OpenVPN connect and click on connect.

One last thing, make sure you give the server a static IP and forward port 1194 on the router.



Thursday, 12 November 2015

SabNZB setup on Ubuntu

To be honest, this is really very easy. we start by opening a terminal onto the machine we are going to install the package onto and type:

sudo apt-get install sabnzbdplus


Now we edit one of the config files:

sudo nano /etc/default/sabnzbdplus


USER=root
# The Host one can be 0.0.0.0 if you only have one IP address
HOST=0.0.0.0
# Change this to another port if 8080 is already in use.
PORT=8082


Then we make it executable:

sudo chmod +x /etc/init.d/sabnzbdplus


Then we restart the service

sudo service sabnzbdplus restart

Once this has finished point your browser to http://ipaddress:8082 and finish setting it up, you will need a usenet provider, I use newsdemon myself.