Sunday 29 March 2020

Pimoroni 1.12" OLED breakout

A long time ago I bought a Pimoroni Breakout Garden and a selection of breakouts.

I had intended to write some very simple test code  and put it on the blog, unfortunately I then managed to break the SD card with the code on. Must remember to back up regularly...

The Breakout Garden equipped Pi Zero then languished at the back of the cupboard while other, shinier things caught my attention.

And then I wanted to know what the temperature was. I do have a number of pieces of technology that would do that (Arduino, Micro:Bit, Raspberry Pi), but decided that the Breakout Garden would be a good starting point.

But first I wanted to check out the operation of the OLED display.

1.12" OLED breakout

Pimoroni provide a good set of example code, including a complete weather station, but I wanted to build my own using the supplied modules.

First off a simple piece of Python to display a message on the screen.

# Simple test of a Pimoroni SH1106 Breakout board attached to a Breakout Garden HAT

# Required modules
from luma.core.interface.serial import i2c, spi
from luma.core.render import canvas
from luma.oled.device import ssd1306, ssd1309, ssd1325, ssd1331, sh1106

# Instantiate a luma SH1106 object
oled = sh1106(i2c(port=1, address=0x3C), rotate=2, height=128, width=128)

# Create a suitable canvas to draw on
with canvas(oled) as draw:
    draw.rectangle(oled.bounding_box, outline="white", fill="black")
    draw.text((30, 40), "Hello SH1106!", fill="white")

print("There should be something on the display now")

References



Securing your Raspberry Pi (Raspbian)

Linux,by design, is generally more secure than say, Windows.


There are many reasons, some historical, some design (including historical).

Of course this is no excuse for not taking precautions. Inside your own network, you should take care, but the Raspberry Pi is very portable, so it might be outside, facing the rampaging hordes on the Internet.

I have a Raspberry Pi 3 that I take on holiday with me, it is used as an entertainment centre (most hotels have HDMI compatible television) so I can listen to podcasts or watch streamed television (see also get_iplayer).

However, this does require a connection to the outside, and you are never sure what is on the other end of the WIFI connection.

For this you need to have and control a Firewall.

Linux has a number of built in firewall capabilities, but they are complicated to control out of the box.

This is where Uncomplicated Firewall comes in handy.

Uncomplicated Firewall

Install Uncomplicated Firewall (UFW).

Uncomplicated Firewall (UFW) is included in most Linux  repositories, so indtallation is pretty easy.

$ sudo apt-get install ufw

Allow SSH through (if required - be careful if you are working on a 'headless' raspberry Pi)
$ sudo ufw allow ssh

Check the rules

A command has been added that allows the rules to be checked even when the UFW is not enabled.

$ sudo ufw show added

Added user rules (see 'ufw status' for running firewall):

ufw allow 22/tcp

Enabling UFW

Before enabling UFW, if you are accessing the machine remotely, ensure that you a) have set a rule allowing an SSH connection, and b) just in case, that you can directly access the machine (have access to the machine and have a keyboard and display to hand).

$ sudo ufw enable

Show status

$ sudo ufw status
Status: active
To                         Action      From
22/tcp                     ALLOW       Anywhere                  
22/tcp (v6)                ALLOW       Anywhere (v6)

Disable UFW

$ sudo ufw disable

Allowing other ports

There are a large number of pre-defined port combinations available on UFW. These can be listed using:
sudo ufw app list

Conclusion

This does not make your Raspberry Pi secure, there are a number of additional actions it is worth taking, and other products are available, but you can at least start to protect your machine and the data on it.

References

https://www.raspberrypi.org/documentation/configuration/security.md
https://wiki.debian.org/Uncomplicated%20Firewall%20%28ufw%29
https://wiki.ubuntu.com/UncomplicatedFirewall
http://manpages.ubuntu.com/manpages/cosmic/en/man8/ufw.8.html


Saturday 21 March 2020

Windows Script Host and JScript Revisited

Background

This is a small example Windows Script File (WSF) that can be used in certain work flows.

This work flow involves dealing with files that are created in the In folder.

Due to some issue with a process upstream in this work flow, some of the files that appear in the In folder are missing their file suffix. For reasons outside the scope of this article, this cannot be rectified upstream, and, because it is known that these files all should have a .txt suffix, this script appends the required suffix where there is no suffix.

The script is supplied with an In and an Out folder path.

The Code

<job>
    <script language="JScript">
        // Technology Is Not Dull 2020
        // Get in and out folders as named arguments
        var InFolder = "";
        var OutFolder = "";
        if(WScript.Arguments.Named.Exists("in")){
            InFolder = WScript.Arguments.Named("in");
        }
        if(WScript.Arguments.Named.Exists("Out")){
            OutFolder = WScript.Arguments.Named("Out");
        }
        // If there is an in and an out parameter
        if(OutFolder!="" && InFolder!=""){
            // Create objects to get the files and copy them
            var objFSO=WScript.CreateObject("Scripting.FileSystemObject");
            var objFolder = objFSO.GetFolder(InFolder);
            var colFiles =objFolder.Files;
            // Remove any trailing '\' in supplied Out path
            OutFolder = objFSO.GetFolder(OutFolder);
            // For each file in the In folder
            for(var objEnum = new Enumerator(colFiles); !objEnum.atEnd(); objEnum.moveNext()) {
                sFileName = objEnum.item();
                sOutName = OutFolder +"\\";
                // This assumes that any file with no file suffix
                // is actually a .txt file and adds the suffix for the copied filename
                if(objFSO.GetExtensionName(sFileName) == "")
                {
                    sOutName = sOutName + objFSO.GetFileName(sFileName) + ".txt";
                }
                else
                {
                    sOutName = sOutName + objFSO.GetFileName(sFileName);
                }
                // Copy the file (overwriting any existing file)
                objFSO.CopyFile (sFileName,sOutName,true);
            }
            // Quit with value 0
            WSH.Echo("Done");
            WSH.Quit(0);
        }
        // Else there are one or more missing parameters
        //so quit with error
        else
        {
            WSH.Echo("Missing Named Parameter(s)");
            WSH.Quit(1);
        }
    </script>
</job>

Operation

The script is tested with a folder structure based on "C:\Some Folder\Documents\Javascript\test folders" with two sub folders 1 and 2.

Folder 1
Folder 2

The parameters are named using /<name>:<value>.

C:\Some Folder\Documents>cscript JScriptExample.wsf /In:"C:\Some Folder\Documents\Javascript\test folders\1" /Out:"C:\Some Folder\Documents\Javascript\test folders\2\"
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

Done

C:\Some Folder\Documents>

As you can see the files have been copied with the suffix added to the file without the suffix.

References

A brief look at JavaScript on various systems including Windows Script Host can be found here.