Bash controlled Raspberry Pi 3B relay hat

If you would like to control some electrical devices by switching them on and off then one possiblity is to use Rasberry Pi relay hat. It is from Waveshare/Botland and it fits on top of your Raspberry Pi. It contains 3 relays controllers with screw connectors.

Once you wire your devices you can even put it within DIN box like this one below. Be sure to be careful inserting micro SD card as this box fits tight and you can easily break card apart like I once did.

In order to control relay connectors first you export pins:

echo '20' > /sys/class/gpio/export
echo '21' > /sys/class/gpio/export
echo '26' > /sys/class/gpio/export

Then you set pin direction:

echo 'out' > /sys/class/gpio/gpio20/direction
echo 'out' > /sys/class/gpio/gpio21/direction
echo 'out' > /sys/class/gpio/gpio26/direction

And finally initialize relay state to switched off:

echo 1 > /sys/class/gpio/gpio20/value
echo 1 > /sys/class/gpio/gpio21/value
echo 1 > /sys/class/gpio/gpio26/value

You can change logic settings on board using jumpers. I have tested this setup 12V wiring but it is possible to have lower or even higher voltages. Also many devices require only to close loop without any current draw.

To expose your Raspberry Pi to network (for further control from OpenHAB for instance) you can set up simple HTTP server using Ruby and Sinatra library.

sudo apt update
sudo apt install git ruby
sudo gem install sintara
require 'sinatra'
set :bind, '0.0.0.0'
get '/open' do
	system("echo 0 > /sys/class/gpio/gpio21/value")
	sleep 28
	system("echo 1 > /sys/class/gpio/gpio21/value")
	return 'Opened & Closed' 
end

To run:

ruby server.rb -o 0.0.0.0

pg gem on Ubuntu 22

I thought that installing pg gem on my clean Ubuntu 22 will be easy, but no. I got some weird message:

The following packages have unmet dependencies:
 libpq-dev : Depends: libpq5 (= 14.5-0ubuntu0.22.04.1) but 15.1-1.pgdg22.04+1 is to be installed
E: Unable to correct problems, you have held broken packages

So I tried to force installation:

sudo apt-get install libpq5=14.5-0ubuntu0.22.04.

After this:

sudo apt install libpq-dev
sudo gem install pg

On this Ubuntu 22 release installing Ruby interpreter from packages you got 3.0.2p107. The problem might be because of pgadmin4 which I installed before, so it could break something.

SSH access using Ruby to outdated servers

If for some unknown reasons you need to connect thru SSH to some outdated server using Ruby code, then you are in the right place. But first a little piece of theory behind it. There are 4 configurable things in OpenSSH concerning security configuration:

  • KexAlgorithms: per-connection keys (key exchange)
  • HostkeyAlgorithms: public key to identify server to clients
  • Ciphers: connection encryption
  • MACs: message authentication codes

As time passes various algorithms become deprecated and are known to be vulnerable. However there are tons of servers on the internet and on private networks which still use these old things. On a regular SSH client as well as net-ssh Ruby client code you will not be able to connect to such servers, instead you will receive error messages that you are unable to establish connection.

There is solution for that.

First:

:host_key => "+ssh-dss"

Then:

append_all_supported_algorithm: true

With these two additional options which you pass to Net::SSH.start method you will be able to connect to the most of running servers out there. Still there will be some chance you may need to adjust it, like for some old Cisco hardware, but you will cover 99.99%. Hope that you find it useful no matter if you try to administer you old networking rig or just playing around with various things…

Ruby on Rails – ćwiczenia

Dzięki książce “Ruby on Rails. Ćwiczenia” opanujesz podstawy tworzenia aplikacji internetowych za pomocą tej technologii. Nauczysz się programować w języku Ruby: poznasz jego elementy i zasady projektowania obiektowego, metody przetwarzania danych tekstowych, pracy z plikami i katalogami oraz obsługi błędów i wyjątków. Przeczytasz także o środowisku Rails, instalowanym na serwerze aplikacji. Wykonując ćwiczenia z ostatnich rozdziałów, zrealizujesz projekt aplikacji służącej do zarządzania czasem w technice Ruby on Rails.” – Helion, 2006

Programowanie w języku Ruby. Mikrousługi i konteneryzacja

Ruby to nowoczesny, wieloparadygmatowy, interpretowany język programowania. Wraz z platformą Rails stanowi jedno z najpopularniejszych rozwiązań służących do szybkiego tworzenia aplikacji sieciowych; wspiera wiele znanych serwisów dostępnych w internecie. Ruby on Rails od lat utrzymuje się w ścisłej czołówce platform klasy MVC – dzięki rozbudowanym funkcjom, wysokiej wydajności oraz łatwości pisania kodu, a także możliwości stosowania dużej liczby rozszerzeń. Jeśli chcesz się dowiedzieć, jak wykorzystać tę platformę w swoich projektach, jesteś na dobrym tropie! Dzięki tej książce poznasz możliwości i konstrukcje języka Ruby oraz mechanizm działania platformy Rails, a w szczególności interfejs Rack. Dowiesz się, jak zapewniać odpowiednią jakość swoich rozwiązań, nauczysz się je uruchamiać przy użyciu technologii wirtualizacji VMware ESXi oraz konteneryzacji Docker na platformach OpenShift Origin, OKD i Nomad. Prześledziwszy praktyczne przykłady, zdobędziesz wiedzę na temat architektury mikrousług, poznasz też sposoby wykorzystania oprogramowania GitLab w funkcji repozytorium kodu, systemu zgłoszeń, bazy wiedzy i narzędzia CI/CD” – Helion