MQTT

Over the last few years my interest in Low Power, Tiny Computers like, PI, Pi Zero W, Uno R3 and most of the ESP2688 and ESP32 Family.

I like the Adafruit Feather ESP2688 it’s tiny, has WiFi and battery charging built-in.

I widely use these in the house to do stuff. Lately I built an
Adafruit Feather ESP2688 AWS IoT Temperature Monitor. Yes this is over kill
but was fun and I don’t care. Most of my self-built Home Automation has MQTT / AWS IoT links.

Lately I wanted to see these MTQQ Messages. Yes I can used my laptop but this is not very impressive!

In came E-PAPER! It’s a extremely low power display.
The people over at Waveshare are answered my wish” With the ESP2688 Driver Board…

The board has WiFi. No Battery Option but I can work with this.
They work with Waveshare e-Paper. So I got a broad and a 1.54″ BW e-Paper display.

PRO TIP: Ignore the Waveshare code example. It’s a full application and requiters knowledge of HTTP and Web severs. It’s Ok to test the display but after 3 days I could not for the life of me figure out how it worked. Look at the example that come with GxEPD2_Example/GxEPD2_Example.inO Took me an hour to understand

Two points. I HATE using code libraries other people wrote.

if code was written against a lib say version 1.2.1 version 1.2.2 may not work.

My Temperature Monitor used Adafruit_MQTT_Library, arduinoWebSockets, aws-mqtt-websockets, aws-sdk-arduino-esp8266 ANDpubsubclient

But It won’t work!

PiCam

I’d seen many project on the web for “Home Security” but none really had that thing.
Thi guide is more of a reminder for me, however please ask any questions.

Parts

Raspberry Pi Zero W Camera Pack – Includes Pi Zero W

Part 1 – Download The OS

Select the OS. I use RASPBIAN STRETCH
Download
You will end up with a ZIP it should take a minute.

Run

cd ~/Downloads/
> ~/Downloads$ unzip 2018-11-13-raspbian-stretch.img*

  • The file you downloaded

Part 2 – The OS on SD Card

You now have two files 2018-11-13-raspbian-stretch.img and 2018-11-13-raspbian-stretch.img.zip You can ignore the ZIP file we don’t need it.
Now you have to put the Raspbian OS on to the SD Card.
We are going to use DD

cd ~/Downloads/
> ~/Downloads$ dd bs=4M if=2018-11-13-raspbian-stretch.img of=/dev/mmcblk0 conv=fsync

  • /dev/mmcblk0 is the name of your SD Card Device.

See INSTALLING OPERATING SYSTEM IMAGES ON LINUX

Once you has the OS on the SD Card you’ve still got a little bit more to do.

  • Connect to WiFi
  • Start SSH Server
  • Setup authorized_keys (OPTIONAL)
  • RSYNC Backup (OPTIONAL)

Part 3 – Auto starting SSH Server [SSHD]

This is very simple. It should only take a few seconds.
Using a client PC/Laptop. Mount the SD Card. On The SD Card will be a boot partition.


cd /media/<USERNAME>/boot
> sudo touch ssh

All the above has done is create a file SSH on the boot partition on the SD Card. We you now start the Pi Zero SSH Server will auto start.

Part 4 – Connect to WiFi

This is a little more complex. Again we have alter a few file on the SD Card.
We are going to manually edit wpa_supplicant.conf so when it starts it has all the info it needs to connect to WiFi.

Using a client PC/Laptop. Mount the SD Card. On The SD Card will be a partition partition.

cd /media/<USERNAME>/partition
> wpa_passphrase <SSID> <PSK>
> wpa_passphrase BTHub6-3CGF xxxxxxxxxxxx >> etc/wpa_supplicant/wpa_supplicant.conf

The resulting file should be…

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
	ssid="BTHub6-3CGF"
	#psk="xxxxxxxxxxxx"
	psk=XXX-PSK-XXX
}

Now put the SD Card in the Pi Zero at attach power.

Part 5 – Python3, PIP3, AWS IoT (MQTT)

Part 5a – Default Python

Their are a few ways to set Python3 as default. The best


Automation

Being a geek has its advantages. When I became disabled I knew technology would bridge the gap between things I wanted and things I could no longer do!

The general term for this is Home Automation.

Home automation is the use of one or more computers to control basic home functions and features automatically and sometimes remotely. An automated home is sometimes called a smart home

This technology gave me something back when my illness had taken everything away.

When I can home my interest in Home Automation grew. I purchased several device in the Philips Hue range and some in the Samsung Smartthings range.
The Philips Hue is mostly lighting. For example, when entering my kitchen the Light’s are automatic, after 5 minutes they go off.

Recently I thought about my central heating. Along came Tado

Philips Hue
The Philips Hue requires an Internet Bridge. A small-ish white device that has a Cat5 connection to your router & power. The Internet Bridge is the link between internal service such as Alexa and external service like itth.com to the Hue Devices.

Philips Hue Light Strip
Is a LED-based multi-coloured strip of lights. These come in 2m lengths can be extended or cut (at marked points) to fit your needs.
They are self-adhesive and require a UK Plug for power.

Philips Hue Light Bulb
Is an LED based multi-coloured standard bayonet Light Bulb.

Tado
In my search for Home Automation, I’ve had to accept a few annoying things. Hue needs a free Cat5 port on my router, the automatic window opener makes a sound when it opens.

Tado has been a nightmare from the start. I was unclear what I needed so ask Tado.

Hello, I’m disabled and need a quote for a thermostat, and 4 radiator valves. I also need info n warranty and support.

A “custom” quote comes back, with everything, as I’m disabled I add “Profissional Install” I Buy, It arrives. But no Install date, so I Call. To be told you set everything up first then get an install date.
Now I disabled, my manual dexterity is poor. I told them I was disabled, they only said to add “Profissional Install”

Ever since I installed tado every few weeks the batteries in each unit need to be replaced. I am physically unable to do this.

During the summer the heating never comes on, but recently when my Wet room comes on, the radiator in the Loung as gets warm.
Trying to contact Tado Support is impossible!
The Tado system was miss-sold it is not fit for purpose.


Samsung

The Age old fight of the best OS, has been settled (mostly as Samsung don’t have a clue)

So have new Galaxy Tab A (2016). The f-ing keyboard won’t show. On inputs the icon changes but no keyboard is displayed, so can’t input stuff!

This is not a Keyboard Input issue, nothing is shown to allow input. Not the “writing” input.

So contact Samsung, first via online chat. The “engineer” can’t understand that as I’m disabled pressing multiple buttons is quite difficult.
so try the “phone” get a man that called me “Mr Daniel” and wanted to setup an account with my name, address and phone number BEFORE he could help.

Python3

Ok so until now i thought ASP was the bad, I was wrong.

As part on my PiZero & Z-Wave Cam project I’ve been forced to use

Python3
proxy = xmlrpc.client.ServerProxy('http://pi-zero-b:8000')

Creates an XML Client to

PI-ZERO-B

port

8000

Now how the hell do I get it to ‘handle’ errors!

One such error is

PI-ZERO-B

being inaccessible. This could be due to reboot or network failure.

i’ve tried

try:
proxy = xmlrpc.client.ServerProxy('http://pi-zero-b:8000')
proxy.sequence()
except xmlrpc.client.ProtocolError as err:
pass

Smarter Coffee 2nd Gen

This is a gimmick, nothing more.

I unboxed the device and joked with my mother how long before I got burned.
Answer: 2nd Use!

The space between the coffee output and craft is too small. Result…

Opps

2017-1203

By Daniel McMorrow

Photographed in Wood Green, North London

The nozzle output on the Coffee make has a spring so you have to lift before you can put in the craft. The craft when even half full is extremely heavy.

The whole product is flash to distract from the fact it’s pointless.

After four uses, it’s stopped working!
I ask Alexa or use the iOS app to make coffee, It grinds the beans and that’s it. Nothing no message, error or lights.

You open the box, find the quick start (The only documentation included)

Wrong

2017-12-09 11.04.59

By Daniel McMorrow

Photographed in Wood Green, North London

This is wrong the iOS App is called Smarter 3.0

Smarter 3.0

2017-12-09 11.04.59

By Daniel McMorrow

Photographed in Wood Green, North London

So In my 16 odd years as an IT Geek, I’ve networked VoIP Phones, CT & X-Ray machines, Door security and an endless sea of computed.
This coffee machine is by far the most bizarre! It uses ‘BlinkUp’ basically it flashes code via your phone screen to the coffee maker. Most small items use Bluetooth or NFC at a push.

So after an hour faffing about with the app and BlinkUp. I got it connected.
Then…

Helpful


By Daniel McMorrow

Photographed in Wood Green, North London

If you follow this helpful suggestion you end up with a coffee machine name “Home”, this is not an issue until you link with Alexa and find out the name is what you ask Alexa.
Saying “Alexa Home on” is stupid! and not intuitive.

Energenie

As a disabled (young) man Home Automation open up a world. I can ‘ask’ lights to come on, have wireless monitoring to alert family if there is a problem.

I’m a geek so find Home Automation a simple task. I’ve used Smartthings,Philips Hue, Z-Wave, Amazon (Echo/Dot/Show) have even built my own based on ESP2886 WiFi.

I have had the misfortune of using Energenie/MI Home. And it’s really, really bad.

I brought Two “Smart Plugs” as part of a Pi kit. One of the plugs went bang! This is usually enough to make me return it.
I had no planned use for them so mark it as a learning experience.

Energenie do a light switch that’s ‘smart’ as I can’t Automate the bathroom, the type of bulb. Automating the switch is the next logical step.
I purchased a “kit” from Amazon.

It came, setup was relatively easy. Put the code from the back of the hub into MiHome website.

I decided to use the plug I have to Automate a Radiator Fan
I setup two timers for the fan. 8am and 5pm. When the heating comes on. Yet the plug only responds a few time. Their is no “log” or status in the iOS app.
I turned the fan off a few minutes ago via the iOS App, now won’t come back on! I can’t log in to the MiHome website as it can’t find my account!

I don’t have time to waste fixing system the don’t work.

Update

Just spent 5 minute trying to turn off the plug via the iOS app. Gave up turned off the socket.

Google

I use Google Webmaster Tools mostly Structured Data to analyze micro data on my site.

Every few f**king days L&Q is removed!

If I used the “Fetch as Google” & “Index” option it put back. Live Structured Data shows no errors.

Alexa

So alexa is quite handy as A disabled man being able to do stuff with my voice is quite cool.
One part I use a lot is the Timer’s,Alarm and Reminders function.

However it sucks!
I use a 30 minute timer to remind me to Drink my coffee.

Alexa takes several tries to get this right! She often sets a 15 minute timer

It would seem Alexa lacks the ability to “learn” – comparing my new timer to the last 5 timers and
as asking for confirmation, would seem easy.

Lambda

In true Amazon fashion, everything has to be overly complex.

All I want is to build an Alexa ‘skill’ using Lambda to send an SMS via AWS SNS Topic. Triggered with ‘Alarm’ or similar.

/* eslint-disable  func-names */
/* eslint quote-props: ["error", "consistent"]*/
/**
 * This sample demonstrates a sample skill built with Amazon Alexa Skills nodejs
 * skill development kit.
 * This sample supports multiple languages (en-US, en-GB, de-GB).
 * The Intent Schema, Custom Slot and Sample Utterances for this skill, as well
 * as testing instructions are located at https://github.com/alexa/skill-sample-nodejs-howto
 **/

'use strict';

const Alexa = require('alexa-sdk');
const AWS = require('aws-sdk');
const recipes = require('./recipes');
const APP_ID = undefined; // TODO replace with your app ID (OPTIONAL).

const languageStrings = {
    'en': {
        translation: {
            RECIPES: recipes.RECIPE_EN_US,
            SKILL_NAME: 'Minecraft Helper',
            WELCOME_MESSAGE: "Welcome to %s. You can ask a question like, what\'s the recipe for a chest? ... Now, what can I help you with?",
            WELCOME_REPROMT: 'For instructions on what you can say, please say help me.',
            DISPLAY_CARD_TITLE: '%s  - Recipe for %s.',
            HELP_MESSAGE: "You can ask questions such as, what\'s the recipe, or, you can say exit...Now, what can I help you with?",
            HELP_REPROMT: "You can say things like, what\'s the recipe, or you can say exit...Now, what can I help you with?",
            STOP_MESSAGE: 'Goodbye!',
            RECIPE_REPEAT_MESSAGE: 'Try saying repeat.',
            RECIPE_NOT_FOUND_MESSAGE: "I\'m sorry, I currently do not know ",
            RECIPE_NOT_FOUND_WITH_ITEM_NAME: 'the recipe for %s. ',
            RECIPE_NOT_FOUND_WITHOUT_ITEM_NAME: 'that recipe. ',
            RECIPE_NOT_FOUND_REPROMPT: 'What else can I help with?',
        },
    },
    'en-GB': {
        translation: {
            RECIPES: recipes.RECIPE_EN_GB,
            SKILL_NAME: 'British Minecraft Helper',
        },
    },
};

const handlers = {
    'LaunchRequest': function () {
	self = this

	var sns = new AWS.SNS();

	sns.publish({
		Message: 'Test publish to SNS from Lambda',
		TopicArn: 'arn:aws:sns:eu-west-1:6303xxxxxxxxx:Alarm'
	}, function(err, data) {});

	this.attributes.speechOutput = this.t('WELCOME_MESSAGE', this.t('SKILL_NAME'));
	this.attributes.repromptSpeech = this.t('WELCOME_REPROMT');
	this.emit(':ask', this.attributes.speechOutput, this.attributes.repromptSpeech);
    },
    'AMAZON.HelpIntent': function () {
        this.attributes.speechOutput = this.t('HELP_MESSAGE');
        this.attributes.repromptSpeech = this.t('HELP_REPROMT');
        this.emit(':ask', this.attributes.speechOutput, this.attributes.repromptSpeech);
    },
    'AMAZON.RepeatIntent': function () {
        this.emit(':ask', this.attributes.speechOutput, this.attributes.repromptSpeech);
    },
    'AMAZON.StopIntent': function () {
        this.emit('SessionEndedRequest');
    },
    'AMAZON.CancelIntent': function () {
        this.emit('SessionEndedRequest');
    },
    'SessionEndedRequest': function () {
        this.emit(':tell', this.t('STOP_MESSAGE'));
    },
    'Unhandled': function () {
        this.attributes.speechOutput = this.t('HELP_MESSAGE');
        this.attributes.repromptSpeech = this.t('HELP_REPROMPT');
        this.emit(':ask', this.attributes.speechOutput, this.attributes.repromptSpeech);
    },
};

exports.handler = function (event, context) {
    const alexa = Alexa.handler(event, context);
    alexa.APP_ID = APP_ID;
    alexa.resources = languageStrings;
    alexa.registerHandlers(handlers);
    alexa.execute();
};


When tested with the Lambda console, I get…

{
  "errorMessage": "RequestId: 55088201-6261-11e7-90d2-6941b4326177 Process exited before completing request"
}


When tested with the Alexa Service Simulator…
Single Utterance of “Alarm” gives…The remote endpoint could not be called, or the response it returned was invalid.
Single Utterance of “Alarm Alarm” works, but no SNS.

Update

So Alexa was never going to work. The combination of no working examples and poor debugging made it impossible!

Next I moved to AWS IoT and an IoT Button. The button power the Amazon Dash Button. They are quite cool.

Again the setup of these buttons is ridiculous. The examples or guides don’t work and the lack of a Decent Error message makes it impossible.

One thing that works is the Lambda Test, so as expected I test and get a SNS Email a few seconds later.

But when I push the ASW IoT Button nothing.

Its fortunate my Reserved Ec2 Instance expires soon, time to evaluate Google.

If I ever talk about AWS being good please laugh at me

policy-overview

So not only do AWS expect you to type this in, when you do it don’t work.

aws iot list-things --region eu-west-1
{
    "things": [
        {
            "thingName": "iotbutton_G030PT027106VPAS",
            "version": 1,
            "attributes": {}
        }
    ]
}

aws iot list-topic-rules --region eu-west-1
{
    "rules": [
        {
            "ruleArn": "arn:aws:iot:eu-west-1:{Account-Id}:rule/iotbutton_G030PT027106VPAS",
            "ruleDisabled": false,
            "ruleName": "iotbutton_G030PT027106VPAS",
            "createdAt": 1499677251.0,
            "topicPattern": "iotbutton_G030PT027106VPAS"
        }
    ]
}

aws iot get-topic-rule --rule-name iotbutton_G030PT027106VPAS --region eu-west-1
{
    "rule": {
        "sql": "SELECT * FROM 'iotbutton_G030PT027106VPAS'",
        "ruleName": "iotbutton_G030PT027106VPAS",
        "actions": [
            {
                "lambda": {
                    "functionArn": "arn:aws:lambda:eu-west-1:{Account-Id}:function:AlarmFunction"
                }
            }
        ],
        "description": "Event source for your IoT Button to Lambda",
        "awsIotSqlVersion": "2016-03-23",
        "ruleDisabled": false
    },
    "ruleArn": "arn:aws:iot:eu-west-1:{Account-Id}:rule/iotbutton_G030PT027106VPAS"
}

aws iot describe-endpoint --region eu-west-1
{
    "endpointAddress": "a3t1otmrg7j71e.iot.eu-west-1.amazonaws.com"
}

aws iot list-certificates --region eu-west-1
{
    "certificates": [
        {
            "certificateId": "981c38379c24672c4843647cb92cdc12c8b78d94a870376d7fa61aaea3fa2ac7",
            "creationDate": 1499692507.149,
            "status": "ACTIVE",
            "certificateArn": "arn:aws:iot:eu-west-1:{Account-Id}:cert/981c38379c24672c4843647cb92cdc12c8b78d94a870376d7fa61aaea3fa2ac7"
        },
        {
            "certificateId": "7c5dc7fd1b45e1c85c82352d2bd82d7ea04fd59aabff22d40e0a40680ab397ac",
            "creationDate": 1499605333.323,
            "status": "ACTIVE",
            "certificateArn": "arn:aws:iot:eu-west-1:{Account-Id}:cert/7c5dc7fd1b45e1c85c82352d2bd82d7ea04fd59aabff22d40e0a40680ab397ac"
        }
    ]
}

aws iot list-policies --region eu-west-1
{
    "policies": [
        {
            "policyName": "iotbutton_G030PT027106VPAS",
            "policyArn": "arn:aws:iot:eu-west-1:832404184041:policy/iotbutton_G030PT027106VPAS"
        }
    ]
}

aws iot get-policy --policy-name iotbutton_G030PT027106VPAS --region eu-west-1
{
    "policyArn": "arn:aws:iot:eu-west-1:832404184041:policy/iotbutton_G030PT027106VPAS",
    "policyName": "iotbutton_G030PT027106VPAS",
    "policyDocument": "{\n  \"Version\": \"2012-10-17\",\n  \"Statement\": [\n    {\n      \"Effect\": \"Allow\",\n      \"Action\": \"iot:*\",\n      \"Resource\": \"*\"\n    }\n  ]\n}",
    "defaultVersionId": "4"
}

openssl s_client -connect a3t1otmrg7j71e.iot.us-east-1.amazonaws.com:8443 -CAfile VeriSign-Class\ 3-Public-Primary-Certification-Authority-G5.pem -cert 981c38379c-certificate.pem.crt -key 981c38379c-private.pem.key 
CONNECTED(00000003)
depth=2 C = US, O = "VeriSign, Inc.", OU = VeriSign Trust Network, OU = "(c) 2006 VeriSign, Inc. - For authorized use only", CN = VeriSign Class 3 Public Primary Certification Authority - G5
verify return:1
depth=1 C = US, O = Symantec Corporation, OU = Symantec Trust Network, CN = Symantec Class 3 Secure Server CA - G4
verify return:1
depth=0 C = US, ST = Washington, L = Seattle, O = "Amazon.com, Inc.", CN = *.iot.us-east-1.amazonaws.com
verify return:1
139936071315160:error:14094416:SSL routines:ssl3_read_bytes:sslv3 alert certificate unknown:s3_pkt.c:1487:SSL alert number 46
139936071315160:error:140790E5:SSL routines:ssl23_write:ssl handshake failure:s23_lib.c:177:
---
Certificate chain
 0 s:/C=US/ST=Washington/L=Seattle/O=Amazon.com, Inc./CN=*.iot.us-east-1.amazonaws.com
   i:/C=US/O=Symantec Corporation/OU=Symantec Trust Network/CN=Symantec Class 3 Secure Server CA - G4
 1 s:/C=US/O=Symantec Corporation/OU=Symantec Trust Network/CN=Symantec Class 3 Secure Server CA - G4
   i:/C=US/O=VeriSign, Inc./OU=VeriSign Trust Network/OU=(c) 2006 VeriSign, Inc. - For authorized use only/CN=VeriSign Class 3 Public Primary Certification Authority - G5
---
Server certificate
-----BEGIN CERTIFICATE-----
{CERT}
-----END CERTIFICATE-----
subject=/C=US/ST=Washington/L=Seattle/O=Amazon.com, Inc./CN=*.iot.us-east-1.amazonaws.com
issuer=/C=US/O=Symantec Corporation/OU=Symantec Trust Network/CN=Symantec Class 3 Secure Server CA - G4
---
No client certificate CA names sent
Client Certificate Types: RSA sign, DSA sign, ECDSA sign
Requested Signature Algorithms: ECDSA+SHA512:RSA+SHA512:ECDSA+SHA384:RSA+SHA384:ECDSA+SHA256:RSA+SHA256:DSA+SHA256:ECDSA+SHA224:RSA+SHA224:DSA+SHA224:ECDSA+SHA1:RSA+SHA1:DSA+SHA1
Shared Requested Signature Algorithms: ECDSA+SHA512:RSA+SHA512:ECDSA+SHA384:RSA+SHA384:ECDSA+SHA256:RSA+SHA256:DSA+SHA256:ECDSA+SHA224:RSA+SHA224:DSA+SHA224:ECDSA+SHA1:RSA+SHA1:DSA+SHA1
Peer signing digest: SHA512
Server Temp Key: ECDH, P-256, 256 bits
---
SSL handshake has read 3386 bytes and written 1271 bytes
---
New, TLSv1/SSLv3, Cipher is ECDHE-RSA-AES256-GCM-SHA384
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
    Protocol  : TLSv1.2
    Cipher    : ECDHE-RSA-AES256-GCM-SHA384
    Session-ID: 5963987114C94B9EC5D31F4625464699926CF6EBCF893D15CB46D994F7AE478D
    Session-ID-ctx: 
    Master-Key: 8CF18BAE922E621F5F4781C8584A8C13401D4E0A4F1013A24926CE846DBB419AD1932396B580D049EB05406A1EC725C8
    Key-Arg   : None
    PSK identity: None
    PSK identity hint: None
    SRP username: None
    Start Time: 1499699313
    Timeout   : 300 (sec)
    Verify return code: 0 (ok)
---

AWS IoT Console





No Rules executed