BT Broadband – Faster Speeds

BT have said they are going to increase my speed

This I find really interesting as I hardly use what I have.  I monitor my speed on an hourly basis, As you can see, it is pretty reliable.  The dips are relatively small, usually plenty in reserve.  I have had one outage although that may have been when I was fiddling with my LAN setup.  This limiting factor – external bandwidth / site speed.

As ever, the press release is bull, aimed at the uninitiated. It will not improve the number of devices that can connect reliably.

Facebooktwitterby feather
Posted in Technical - General | Leave a comment

Cloudflared DNS over HTTPS (DoH) and Pi-Hole

(Using the new WordPress Editor and I am not sure I like it – in fact I hate it.  Gone back via the ‘Classic Editor’ plugin)

Background

I have been following Troy Hunt and Scott Hulme, 2 well known security experts and decided to try and employ DNS over HTTP (DoH).

I already had Pi-Hole installed on a DietPi VM (my favoured building block) so the logical thing was to put the cloudflared daemon on the same machine.

I used a combination of the following 3 references

Installation & Configuration

First step is to install the cloudflared package

To check it is installed correctly;

Next step is to configure the package correctly.  There are 2 means of providing a configuration to cloudflared; a config file or a yaml file.  I did not find the yaml method before I did the config.  I may change it at some point.

To setup the config file, create the following file

With this content

Setup a Service

Once configured, the next step is to run cloudflared as a service.  Create the required file;

and insert the following text;

Note I have commented out the reference to the user as this is DietPi and I simply run everything as root.

Once the file has been created, enable the service and start it;

To check that the service is running;

should return something like this

To test if this is working use this command;

The final piece of the puzzle is to now get Pi-Hole to use this for the DNS queries.  This is quite easy to do, simply add in  127.0.0.1#5053 to the custom DNS entry in the settings.

Change the Listener IP Address

There is one final thing.  Because of the way I wanted to set my network up, some devices bypass Pi-Hole but I still wanted the DNS queries to go via DoH.  In order to do that I wanted to point those machines DNS directly to the IP address hosting the Cloudflared daemon.  However it didn’t work.  After some discussion on the forum and looking at the output of  netstat -lnp I established that cloudflared was only listening on the loopback interface (127.0.0.1).

The solution was to add an  --address 0.0.0.0 parameter to the config line.

While the output of the netstat command still does not reflect the setup of a service like lighttpd, it does work.

Facebooktwitterby feather
Posted in DietPi, Linux, Pi-Hole, Technical - General | Tagged , | Leave a comment

UniFi Controller on a Pi or DietPi

Having just invested in a UniFi AP UAP-AC-Lite (it is absolutely fantastic), and refusing to spend £70 on the USB UniFi controller or wanting to use the cloud system, I needed to get the UniFi controller installed.  I didn’t expect any issues.  Well there were.

I first ran up a new DietPi VM on my micro HP server running an old 5.5 version of Esxi – Ain’t broke don’t fix. I now have a policy of not combining systems but running individual VMs.  I was burned recently when an installation of HomeAssistant went Pete Tong and I had to rebuild from scratch.  That also took out Node-Red and Mosquitto so they all (broardly) have their own VMs.

I tried a number of different ways of installing the controller via apt.  The problem I kept hitting was Java.

[Edit 9th March 19] I have discovered a much better way via these scripts.

To cut a long and tedious story short, I stumbled upon a community post that linked to a docker version of the Controller (project links https://github.com/ryansch/docker-unifi-rpi and https://hub.docker.com/r/ryansch/unifi-rpi/).

To install this on DietPi (after my usual setup) I simply installed docker and then from apt docker-compose, before building the container.

And that was it.

The controller appears on port 8443, you need to use an https address and there is the usual stupidity around browsers complaining about no SSL cert for non-routable addresses but it all works fine.

Epilogue: I subsequently realised that there is a second method to install the docker that might actually be better as it has instructions on how to upgrade. I do not understand the difference in the methods and I will leave it until I need to upgrade.

Facebooktwitterby feather
Posted in DietPi, Hardware, Network, Technical - General | Tagged , , | Leave a comment

Pi-Hole and Disabling dnsmaq

Pi-Hole no longer uses the dnsmasq DNS proxy directly. However, existing installations will still have it installed and the service will still attempt to run and fail (as it should).

I was keen to get rid of it as I’m not a fan of unused services especially ones that should always fail. It was not as straightforward as I’d hoped as it is an init.d service. After a discussion on the forum, and some trial and error I think this is what I did. Unfortunately, I could not go back to where I was before I started so I cannot be 100% confident it is right (or the order).

So the issue was that, as this is a service that is started via update-rc.d a normal ‘disable’ does not work as it leaves the various run level files hanging around as seen by

To remove those you need to execute

Secondly, disabling a service simply stops it from running at boot, but does leave open the possibility of it being initiated by another service. To stop the possibility of that happening, a

is required.

This then removes it from the output of a systemctl status and avoids confusion.

There is a suggestion that dnsmasq could be uninstalled. I decided not to as it felt a little risky (I may take a VM snapshot and try).

To ‘uninstall’ a package, you need to do one of 4 things with apt.

remove– just removes the package from the os (i.e. uninstalls it) and not from the disk (the cached package).

purge – removes the installed package and associated files like personal settings, configuration files etc

auto-remove – removes any packages that are no longer required as a result of removing packages that had previously caused other dependencies to be installed. If used with purge will remove associated files as well.

clean – actually removes all of the above from disk, think emptying a recycle bin in windows.

Facebooktwitterby feather
Posted in DietPi, Linux, Pi-Hole, Technical - General | Leave a comment

Systemd Timer and monit

Why is it that, so often, a key bit of information is assumed by documentation writers?

I’ve had an issue for a while where monit will fail to start properly on boot.  The issue is that with DietPi, it seems that the system boots too quickly and monit trys to create a network interface before the network is ready.  Calling a systemctl restart monit solves the problem but that is a pain to remember to do after every boot.

I started looking at systemd solutions.  There is an obvious one; use a timer file.

In theory you create a file with the extension .timer and that then triggers the related service to start.  Good in theory but I could not get it to work; the monit service just started straight off.

The clue is a small line here – “The .service does not require an [Install] section as it is the timer units that are enabled. ”

Off goes the light.

So the monit.service file looks like this (note no install section)

and the monit.timer file looks like this

both reside in /lib/systemd/system/

To enable them you simply enable the timer systemctl enable monit.timer

One other thing I did and I’m not sure if it makes a difference, I removed the file /etc/init.d/monit

As a recap, a vanilla install of monit is simply apt-get install monit.

To access the status, you need to setup the internal http server. Add a file /etc/monit/conf-enabled/monitconfig with the contents;

There are other examples in the main config file  /etc/monit/monitrc.  This setup only allows access from my local subnet and as I used a fixed IP, I specify that IP.

I then access this from Node-Red (fill in your IP address) and output onto an MQTT broker as I explained in the past https://tech.borpin.co.uk/2017/03/21/node-red-and-xml-to-json/

Facebooktwitterby feather
Posted in Linux, Technical - General | Leave a comment

Contributing to Open Source Projects on GitHub

If it has crossed your mind to contribute to an Open Source project, but GitHub just seems too daunting, then I’d suggest reading this tutorial.

The bit that got me was how to actually contribute and this section explains it perfectly (I think). Before, there were too many origins the odd upstream and far too much pushing and pulling!

The key steps (which I have got wrong on numerous occasions before reading this tutorial) are:

  • Create a Fork of the repository you wish to contribute to (the upstream repository), to your GitHub account (the origin repository)
  • Clone the origin repository to your working machine (the local repository)
  • Add the upstream as a remote
  • Create a local branch off the master branch
  • add in your features and test them
  • commit to your local repository
  • push those changes to the origin (your GitHub repository)
  • Create a Pull Request to include it in the main project (i.e. ask the maintainer to pull in your changes).

This tutorial does refer to BitBucket but the principles are exactly the same and is a reference I keep on coming back to.

Facebooktwitterby feather
Posted in Coding and Programming, Technical - General | Leave a comment

Using nmcli to set a static IPV4 address in Hass.io

This method of setting a static IP for Hassio has been tested with a Hass.io vmdk image on an Esxi server. Log into the HASSOS base system via a console – note this is not the same as an SSH login via the add-on.

Welcome to HassOS
Hassio login:

Login as root (no password needed)

At the hassio > prompt, type login (as instructed).

From here you will use the nmcli configuration tool.

# nmcli connection show will list the “HassOS default” connection in use.

# nmcli con edit “HassOS default” will put you in a position to edit the connection.

nmcli> print ipv4 will show you the ipv4 properties of this connection.

To add your static IP address (select ‘yes’ for manual method);

nmcli> set ipv4.addresses 192.168.100.10/24
Do you also want to set 'ipv4.method' to 'manual'? [yes]:
nmcli> save
nmcli> exit

In addition I have found it is wise to set the dns server and the local gateway. For most home routers these will be the same address. If you are using Pi-Hole you can set the dns to that.

nmcli> set ipv4.dns 192.168.100.1
nmcli> set ipv4.gateway 192.168.100.1
nmcli> save
nmcli> exit

If you now view the default connection cat /etc/NetworkManager/system_connections/default you should see the method is manual and the address is set.

Doing a nmcli con reload does not always work so restart the VM.

Facebooktwitterby feather
Posted in Hass.io, HASSOS | 2 Comments

Emoncms on DietPi

Introduction

WARNING

The Install of EmonCMS has moved on since I wrote this and this guide is now depreciated

I am a huge fan of DietPi – lightweight and fast, runs an a large number of SBCs. The aim here was to build a local EmonCMS server as simply as possible. Doing it even from a Rasbian Stretch minimal image can be a bind – DietPi makes some of the tricky bits as easy as selecting from a menu. Not everything can be done that way and some command line effort is required. I started with a fresh V6.16 DietPi install. Well actually it is a base VM image I have that I simply updated. Yes, DietPi runs very happily on a VM, is much quicker than on a Pi and no issues with SD Cards corrupting. In addition, you can take snapshots so less risk of ruining an installation!

The other aspect is that these instructions will install a LLAP stack of Lightppd, PHP7 and MariaDB. Personally I think Apache is rather bloated.

Setting up

DietPi-Config

The initial setting up is best done via the DietPi interface. Simply type dietpi-config to enter the menu system.

  1. DietPi-config – Advance options – Time sync mode – time sync daemon + drift (more reliable time)
  2. DietPi-config – Security options – change root password and hostname (optional)
  3. DietPi-config – Network Options: Adapters  – fixed IP and turn off IPV6 (optional)

DietPi-Software

DietPi has a menu system for installing a number of different software packages. You can either do these installs via the menu system or shortcut it by using the command line syntax dietpi-software install [ID] (except for the SSH change).

  1. Avahi daemon [152] – gives you a hostname on your network.
  2. Install GIT [17]
  3. Install LLAP [82] – It will suggest you do not need this but go ahead anyway.
  4. DietPi-Software – SSH Server – OpenSSH (Optional – allows WinSCP to transfer files – replaces defualt dropbear)
  5. Install redis-server [91] – Only required if you are going to use Redis, but it is suggested you do!
  6. Install Mosquitto [123] – Only required if you intend to run an MQTT broker on this machine (I run the broker on my HASSIO instance). Not needed to publish and subscribe to topics.

Command line installs

Next you need to install a few bits directly. NOTE in DietPi you do most things as root (you can setup other users, I never bother) so sudo is not required.

  1. apt-get -y install php7.0-dev – This gives you the phpenmod and phpdismod commands
  2. apt-get -y install build-essential – Is used by PECL to build compile the redis and mqtt php modules.
  3. apt-get -y install php-pear
  4. apt-get -y install mosquitto-clients – Optional, required if you want to debug MQTT.
  5. pecl channel-update pecl.php.net
  6. pecl install redis-3.1.6 – This is the PHP extension for REDIS – the version is important.
  7. printf "extension=redis.so" | sudo tee /etc/php/7.0/mods-available/redis.ini 1>&2
  8. phpenmod redis – Enable the PHP Redis extension.

MQTT [edit]

I suddenly realised that I had not included how to install MQTT. The simplest way is to follow this guide (remembering you do not need sudo).

Configure Lighttpd

nano /etc/lighttpd/lighttpd.conf – Edit the Lighttpd configuration:

Add in mod_rewrite

And add these lines at the end…

Either restart Lighttpd or reboot the server (I like to do the latter) systemctl restart lighttpd

EmonCMS

I was going to refer to the main documentation for this, but it is somewhat out of date and inconsistent so…

The Base System

  1. cd /var/www
  2. git clone -b master https://github.com/emoncms/emoncms.git You may want to use the stable branch instead.

Setup The Database

  1. mysql -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1'); DELETE FROM mysql.user WHERE User=''; DROP DATABASE IF EXISTS test; DELETE FROM mysql.db WHERE Db='test' OR Db='test_%'; FLUSH PRIVILEGES;"
  2. sudo mysql -e "CREATE DATABASE emoncms DEFAULT CHARACTER SET utf8;"
  3. mysql -e "CREATE USER 'emoncms'@'localhost' IDENTIFIED BY 'emonadmin'; GRANT ALL ON emoncms.* TO 'emoncms'@'localhost'; FLUSH PRIVILEGES;"

Create Data Directories

  1. mkdir /var/lib/{phpfiwa,phpfina,phptimeseries}
  2. chown www-data:root /var/lib/{phpfiwa,phpfina,phptimeseries}
  3. chmod 755 /var/lib/{phpfiwa,phpfina,phptimeseries}

Setup Log file

  1. touch /var/log/emoncms.log
  2. chmod 666 /var/log/emoncms.log
  3. chown root:www-data /var/log/emoncms.log

Edit EmonCMS Settings

  1. cd /var/www/emoncms && cp default.settings.php settings.php
  2. nano /var/www/emoncms/settings.php

First run

Open a browser window to [YourIpAddress]/emoncms. At the user prompt click on Register. Register a user then log in. First stop is the administration page and check database. Everything should now be ready to go!

You may wish to add in some additional Modules.

You may also wish to enable MQTT for subscribing and publishing.

Facebooktwitterby feather
Posted in DietPi, OpenEnergyMonitor, Technical - General | 2 Comments

Else is never necessary – Really?

I had an interesting experience with Codacy today.  I got this error.

When doing my IT Degree (many moons ago) I was taught 2 things (amongst others you understand):

  1. An If statement always has an else. You should always handle a possible situation even if it should never happen.
  2. There should only ever be one exit point from a function, loop etc.

Perhaps these mantras stemmed from a safety critical standpoint (I was using Ada).

However, it seems the ‘modern way’ doesn’t agree. Well tough, I still think I am right and any other way is just building in problems.

Facebooktwitterby feather
Posted in Coding and Programming | Leave a comment

Keeping time on a Pi

Again, this blog fall into the category of a ‘I need to remember how I did this’ post.  I actually started writing this months ago but never finished it.  Good job as some of what I originally said was wrong and we have moved on from Jessie to Stretch.

This started when I accidentally discovered that the date/time on one of my Pi’s was significantly out.  I won’t bore you with the things that I tried and failed, rather just tell you how to fix it.

You can check the date&time your own system by simply typing date and seeing if it is right or not.

Finding out how to get the right time set is remarkably difficult.  In the end I discovered that you can simply use this command (reference);

sudo sntp -s time.google.com

NTP is the protocol that synchronises time across the internet.  There are a number of free to use servers that you can query to get the current date & time.

One important fact is that if your date & time are significantly out, you cannot use ntp to set the time correctly.  Set it manually so about the right time then sync.

On more recent versions/distributions of Linux,  timedatectl  may be used to query and change the system clock and its settings.  Note this is not a daemon in it’s own right.  On Linux there are 2 daemons you can use, ntpd  or systemd-timesyncd . However you must not have both enabled at the same time. ntpd is the older daemon. systemd-timesyncd is the newer one and is a builtin service.

Now have a look at the daemon that controls synchronising your system with an ntp server.

The best explanation of this daemon I found was here, with another guide here.

As you can see from the output above, NTP is enabled and synchronised and my timezone is set correctly, it is likely that in the response you see, they are not.  Firstly, if your timezone is not right I’d suggest using sudo raspi-config to set it correctly.

In order to use ntp to keep your clock right, you first need to point the daemon to a time server.  The recommendation is to use a suitable one out of the ntp pool project; http://www.pool.ntp.org/en/use.html . To do so you need to edit the file  sudo nano /etc/systemd/timesyncd.conf  so that your Time section looks something like this;

Interestingly, the guide above suggested the line should start ‘NTP’ but as all I did to the standard line was uncomment it and change debian to uk.

To now switch on the synchronising use the command sudo timedatectl set-ntp true

One final thing, if you are using UFW you may find you need to open the port with  sudo ufw allow 123/udp

You may also need to restart the services  sudo systemctl restart systemd-timesyncd.service

Your system should now keep time 🙂

HTH

Facebooktwitterby feather
Posted in Technical - General | 1 Comment