ESPHome configuration for Arlec Grid Connect Smart Plug-in Socket

Ben asked if I could share my ESPHome configuration for the Arlec device. Here it is:

esphome:
  name: master_bedroom_heater
  platform: ESP8266
  board: esp01_1m
#  esp8266_restore_from_flash: True

wifi:
  networks: 
    - ssid: !secret work_ssid
      password: !secret work_psk 
    - ssid: !secret home_ssid
      password: !secret home_psk

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: !secret ha_api_pw

ota:
  password: !secret ota_pw


binary_sensor:
  - platform: gpio
    pin:
      number: GPIO14
      mode: INPUT_PULLUP
      inverted: True
    name: "master br heater Button"
    on_press:
      - switch.toggle: mr_br_heater
  - platform: status
    name: "master bedroom heater status"

switch:
  - platform: gpio
    name: "Master Bedroom Heater"
    pin: 
      number: GPIO12
    id: mr_br_heater
    restore_mode: RESTORE_DEFAULT_OFF
    on_turn_on:
      light.turn_on:
        id: mr_br_led1
    on_turn_off:
      light.turn_off:
        id: mr_br_led1
    
light:
  - platform: binary
    name: "led1"
    output: led1
    id: mr_br_led1
  - platform: binary
    name: "led2"
    output: led2
    
output:
  - platform: gpio
    pin:
      number: GPIO04
    id: led2
    inverted: True
  - platform: gpio
    pin:
      number: GPIO13
    id: led1
    inverted: True

Putting ESPHome on Arlec Smart Plug In Socket

I got some of these Arlec Smart Plug In Sockets from Bunnings. Apparently they are Tuya devices but I decided to take the brute force approach to putting ESPHome on the.

First challenge was getting the tri point screw out. I had to buy a special screwdriver for this.

tri point screw in arlect smart plug
They don’t really want you opening these

Once I got the screws out, it was easy to unclip the case and open it.

After tracing the circuit, I decided to solder wires to the board and connect them to my USB->Serial adapter. The only tricky part was that you have to hold GPIO0 at ground when you power up the esp8266 to get it into flash mode. I just held a wire on the tiny GPIO0 pad on the TYWE2S.

wires soldered to circuitboard to enable flashing
wires soldered to circuit board to enable flashing

Can’t log in to Netflix on Samsung TV

I had this problem on our Samsung TV where no matter what, I couldn’t log into the Netflix app. I noticed that it happened just after I had changed the password to Netflix. Now lets not think about how fucking difficult it is to type in an email address and secure password using just a remote, but then the frustration of doing it over and over again and being told the login is incorrect… I was close to smashing the TV with the remote and then throwing the whole lot out the window.

Anyway, after I finally accepted that it wasn’t me but it was either the TV or the Netflix app, I started googling around. Turns out there is a secret way to reset the Netflix app. Once I did that I was able to log in first time. WTF Samsung and Netflix? how can this bug exist?

Anyway the magic is:

Start SmartHub and select the Netflix App, then on the TV remote, press the following sequence:

Up, Up, Down, Down, Left, Right, Left, Right, Up, Up, Up, Up.

Once you have done that, the app will ask you if you want to Start Over. Hit Yes, and you should be good to go.

Let’s Encrypt when your server is behind a firewall and you can’t use DNS Challenge

Sometimes you want to give a server that is  behind a firewall a valid SSL certificate. Let’s Encrypt provides a nice solution for this called DNS Challenge. The problem with DNS Challenge is it may require some manual configuring to on your server to set it up. That is all fine and good but sometimes the server won’t allow you to do that configure. A classic example is the hass.io (Home Assistant) appliance or FreePBX

I couldn’t find a coherent set of instructions for setting up Let’s Encrypt DNS Challenges with hassio, especially as my DNS provider requires custom scripts in order to achieve this.

However, I came up with a work around. First let’s think about how Let’s Encrypt (LE) usually works. Suppose you want to get a certificate for my.example.com (MEC), but my.example.com is NATed behind example.com’s (EC) IP address. When you run the LE client on MEC, it requests the LE servers to do a challenge request to MEC. MEC will have the same public IP as EC. the client on MEC will some information on the MEC server at the <webroot>/.well-known/acme-challenge. The LE servers will try and query it. If they find what they are looking for, they issue you the certificate.

So I got to thinking, as this request takes place on port 80, it must contain the domain name header, and so, if you run an web server on port 80 of EC and A entry for  MEC the same public IP address as EC, EC could simply proxy that request to the MEC server.

I looked up how to do this and set it up on my server and low and behold, I was able to get a valid certificate.

In my case I did it with nginx , so the config looked like this:

On my server on port 80 (example.com) of the firewall in /etc/nginx/sites-available/my.example.com:

server {
       server_name my.example.com;
       location / {
       		proxy_pass http://192.168.1.9:80/;
		proxy_set_header Host $http_host;
	}
}

Then symlink it to /etc/nginx/sites-enabled/my.example.com and test the nginx config:

$ sudo ln -s /etc/nginx/sites-available/my.example.com  /etc/nginx/sites-enabled/my.example.com
$ sudo nginx -t
[sudo] password for jason: 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart nginx:

sudo systemctl restart nginx.service

Now start your Let’s Encrypt certificate request on your server and you should be good to go.

MSSQL ODBC Client on Debian 9 Stretch

Many years ago fREW Schmidt wrote a very handy article on installing MSSQL ODBC drivers in debian. Since then Microsoft have improved their support of debian somewhat and things have changed making it easier to install, however they still (as at 2017-11-06) have not released proper drivers for Debian Stretch.

Add the microsoft repo

echo  'deb [arch=amd64] https://packages.microsoft.com/debian/8/prod jessie main' > /etc/apt/sources.list.d/mssql-release.list

Add the key

curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -

then update and install

sudo apt update
sudo apt install msodbcsql mssql-tools

If you try and connect to sql now you get an obscure error like this:

/opt/mssql-tools/bin/sqlcmd -S 10.0.2.13 -U xxx -P yyy
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Can't open lib '/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.1.so.9.1' : file not found.

But the file exists:

$ file /opt/mssql-tools/bin/sqlcmd
/opt/mssql-tools/bin/sqlcmd: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=21a353af470e7849544daab892ec9b1bfc36dc87, not stripped

This somewhat misleading error is actually due to the libmsodbcsql lib being linked against a very specific version of  libssl. Check the output of ldd:

$ ldd /opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.1.so.9.1 | grep 'not found'
libcrypto.so.1.0.0 => not found
libssl.so.1.0.0 => not found

But it turns out this can easily be solved by manually installing the libssl package from Debian Jessie:

wget "http://security.debian.org/debian-security/pool/updates/main/o/openssl/libssl1.0.0_1.0.1t-1+deb8u7_amd64.deb"
sudo apt install ./libssl1.0.0_1.0.1t-1+deb8u7_amd64.deb

then you should be back in business.

I made a ukulele tuner web app

close up of a ukulele
Ukulele by texasfeel  (CC BY-ND 2.0)

I got sick of all the ukulele tuner apps so I decided to make my own. The best ukulele tuner app I’ve found on the iPhone works by repeatedly playing the note you are tuning to rather than trying to analyse the audio and display the frequency of the note it detects. The human ear is still better than software at doing that in my opinion.

It’s pure html and javascript. I used the howlerjs javascript framework for playing the audio as I found the basic javascript audio functions not precise enough for the looping.

The audio samples came from freesound.org courtesy stomachache.

Ukulele Tuner

convert mp3 to m4b on the linux command line

I quite often want to convert mp3 files to m4b, Apple’s proprietary nastiness. There is pacpl of course but that has issues these days, particularly with copying the id3 tags to the new file.

FFmpeg can do it when you build from source (I just found a nice script that will build it from source for you). So I came up with this way to do it from the command line. It uses an awesome feature of xargs which will run it in parallel, so one for each core.

find -type f -name \*.mp3 \
| xargs -n 1 -P $(getconf _NPROCESSORS_ONLN || echo 1) -i \
  bash -c \
 'i="{}"; ffmpeg -y -i "$i" -map_metadata 0 \
        -c copy -c:a libfdk_aac -b:a 128k\
        -map_metadata:s:a 0:s:a -f ipod "${i%.*}.m4b"'

What that does is:

  1. find all the mp3 files and pipe their names into xargs
  2. then get the number of cores available and pass that to the -P argument
  3. and run ffmpeg and map all the metadata to the new file
  4. and strip off the mp3 file extension and add m4b to it

There! Nothing to it!

Update 2017-03-23: Added double quotes around the bash variable $i to cope with spaces. You should always do this (and so should I)

Update 2018-12-20: Added “-c copy” so ffmpeg can cope with album art in the file

Build and install emacssnapshot packages on Debian Stable

the emacs logoDespite emacssnapshot only providing prebuilt packages for Debian unstable, building and installing the packages on stable is quite straight forward.

I wrote a script to automate it. I’m sure there are better ways to do this, but it works for me. Thanks to twb for feedback on the script. Sorry I was too lazy to implement all of your suggestions twb.