Sunday 19 February 2017

Google Will Take Over The World - Terminator - Armageddon - End Of Days


I once said to my students back in 2010 that Google will take over the world, I don't think we're too far from all of that.

What I mean by that is an apocalyptic way, you've probably seen Terminator, where Kyle Reese is sent back in time from the year 2029 by John Connor to 1984 to rescue the teenage Sarah Connor from the claws of the T-800 played by Arnold Schwarzenegger who was sent by Skynet to kill her to prevent John Conner from ever being born, he of course is the leader of the Tech-Com resistance force who are trying to bring down Skynet.

So where does Google fit into all of this? Good question. We all know that google has been around since 1998, we also know that they quickly became the biggest search engine in the world. They're famous for developing the Android operating system, as of 2016 it is now in almost 90% of all smartphones on the market. So we can see how google has grown year after year.

In 2013 Boston Dynamics was bought out to the tune of $500 million. Boston Dynamics is owned by Google X, Google X is a semi secret research and development company owned by Google. Google is now a subsidiary of Alphabet Inc, a company created by the two founding members of Google, Larry Page and Sergey Brin back in 2015.

You only have to take a look at some of the robots that this company is making to see that the days of the T-800 are actually not that far away.

For those of you with smartphones, have you ever noticed how when you're having a conversation with someone that when you pick up your phone and start typing something into Google that it seems to already know what it is you want to search for? Maybe google is secretly listening into our conversations. They might use the excuse that it's all about providing us with a better service, but is this really the case.

How long will it be before we see computers and robots being developed into machines that look just like us. Yes it may just start off where robots are helping us around the house doing odd jobs here and there, there may even be sex dolls that can see to our every need, but where will it stop?

Google have already developed, tested and are now producing driverless cars, how long will it be before those cars turn to robots or can talk to you like another human can? If you're a fan of the 80s then you'll already of heard of Michael Knight and his car KITT.

How long will it be before google becomes the next skynet? And how long will it be before we see the first terminator robot serving on the frontline in Afghanistan replacing soldiers, how long before they're on our streets and our homes?

Raspberry Pi - Upgrading Node.js, npm and installing latest Node-Red v0.16.2

I've posted this more for myself really as a reference point to come back to as I keep forgetting how to do it, but if anyone else finds this useful then even better.

As of posting this, the latest version of raspbian was released in January 2017 still doesn't come with the latest versions of npm and node.js leaving you with the rather outdated version node 0.10.29 and npm 1.4.21

I'm not sure why they pack these images with the outdated versions, it's really annoying to have to do it yourself, and then having to scour the internet all the time, especially if you're still a noob at this like I am.

Upgrading to latest Nodejs

Firstly we need to check our current version by running the following command from the terminal

$ node -v
v0.10.29

Next we need to run the update command, this doesn't actually update anything, it just downloads (or is supposed to) the latest package lists from the repositories.

$ sudo apt update

To find the latest version we need to run this

$ apt list nodejs

The output of which should look like this

Listing... Done

According to the result on the terminal though there was no upgrade available, even though I knew there is a Nodejs v7.5 out there. 

Running "$ sudo apt install nodejs" just told me that the latest version was already installed, which of course it wasn't. Running node -v still produced v0.10.29 as an output.

So let's get the latest version which we know to be v7.5

$ curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -

This doesn't actually install anything, it just pulls everything down from the repository, what we need to do now is to install it.

$ sudo apt install nodejs

Now this is when the latest version gets installed, afterwards we run the command again

$ node -v
v7.5.0

Nodejs has now been updated to the latest version, and npm (although I didn't physically install it) is now npm v4.1.2 which you can find by using the command npm -v

On checking this now though I found that my already preinstalled version of Node-Red no longer worked and it no longer existed in the start menu either... So time for a reinstall.

As of writing the latest version of Node-Red is v0.16.2, use the following command in the terminal to install Node-Red.

sudo npm install -g node-red

Unlike the preinstall version of Node-Red, the new version doesn't work in the same way as before using the command node-red-start, instead you have to use the command "node-red" to get it started

The new version of Node-Red comes with a new interface which is really cool (I might cover this another time) and unlike the previous version where you had to stop and restart Node-Red you can now search for and install nodes from the GUI, and for the most part you don't have to do a restart, pretty cool. The downside I found though is that there is no option to get Node-Red to start up at boot time, so if you're like me where I use it for home automation, if there is a system crash or power failure which causes a reboot you'll find that Node-Red isn't running. Even an attempt at getting it to run via pm2 didn't work. ...but there is hope.

This next bit of wizardry comes from GitHub, just copy and paste the following lines (one at a time) into the terminal, these run as sudo so it's up to you how you want to do things.

sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/nodered.service -O /lib/systemd/system/nodered.service
sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-start -O /usr/bin/node-red-start
sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-stop -O /usr/bin/node-red-stop
sudo chmod +x /usr/bin/node-red-st*
sudo systemctl daemon-reload

These commands update the system service starter with the start and stop commands (node-red-start and node-red-stop) which are then made executable and then the final command refreshes the systemd config.

If you run the following command this will enable Node-Red to run as a service at start up as it used to when you previously had the preinstalled version, before you upgraded. 

sudo systemctl enable nodered.service

Hope this helps some of you out there, if it does then please feel free to comment below.

Node-Red Phone Pinger - Home Automation.

Node-Red Phone Pinger... Home Automation

As part of my home automation project that I have running on the raspberry pi, I wanted to use some sort of system where I could also automate my home security too, which is also built into my raspberry pi. I wanted to find a way to do away with the process of remembering to press buttons before you leave the house to set the alarm system and then the same again when you enter the house. It needed to be as fool proof as possible


I started thinking about using an RFID approach and build this into a key pad, coupled with an Arduino Nano and linked to my raspberry pi via I2c with the an RFID keyring attached to my keys, but again this involved me having to do something in order to arm and disarm my house alarm, it wasn't the approach I was looking for. I needed something that I would always have by my side pretty much 24 hours of the day but wouldn't cost me a huge amount of money. So, after searching the internet I found an easier solution by using my phone.


As I'm already using Node-Red throughout all of this project I found a way for Node-Red to ping my phone every 20, 30 or 60 seconds, it's really up to you. As I'm still in the testing stages I have it pinging my phone every 5 seconds.There are a few things you need to do in order to get all of this working first though.

Firstly, you'll need your WiFi on your phone active all the time, don't worry about running your battery flat, WiFi on smartphones usually only accounts for around 5% of battery usage. It will also need to have a static IP address too since you will need a way for your ping node to always ping the phone you want it to, this isn't as complicated as it first might sound but does involve you logging into your ISP router, for me it's Virgin Media and quite straight forward.

The thing I like using this approach is that you can lock your phones identity to your home network via the IP address you assign it and the MAC address built into your phone. It's a bit like using the RFID approach with a built in digital signature but of course using something that you'll have with you all the time.

Let's make a start. For Virgin Media users who have the black Super Hub 2, once you've logged in to your hub you need to click on the advance settings button (bottom right hand corner), when you get to the next page, scroll down towards the bottom to where it says DHCP, you'll need to click where it says "DHCP Reservation". On the top of the next screen you'll see a tab that says "Attached Devices", somewhere on this list you will see the IP address and the MAC address of your phone.

If you're not sure which one is yours then you can find this one your phone. For Android users, go to Settings > About Phone > Status and on the list you'll see the "IP Address" and "WiFi MAC Address" of your phone. In my case it was 192.168.0.8 and the MAC address ended with b3.

As I've done below, looking on the DHCP page of you router will reveal that your phone and it's MAC and IP address are listed.




Underneath all of this you'll see the section where you can add the IP address reservation for your phone, this means that it's only your phone with it's unique digital marker (MAC address) that can connect to your homes WiFi with the IP address that you specify. Give it a name to easily identify it, in my case I've called it "Reena Phone", after you've filled out the MAC address details you can then click the Add Reservation button.

Underneath all of this you'll see the IP Lease Table. This gives you a list of every device connected to your home network and the IP address that's been assigned to it by the DHCP server built into your router, or in my case, the IP address that you've reserved for specific devices that you have connected. You'll see here that I have five phones connected and all of their IP addresses have been reserved. Don't forget to click "Apply" before you finish.

Now we've got all of that out of the way we move onto the Node-Red part of the process. Below you can see the simple flow that I've got set up along with the two nodes, Ping, and Function.




For the Ping node it's pretty straight forward, all you need to do is enter the IP address of your phone, specify how often you want it to ping and then just name the node








With the function node we just need to add a few lines to make everything work. Because I have five devices that are going to be pinged I need to know which device is either on or off, to do this we have to set a topic message, in this case it's "Reena". I also wanted the output on this to be either a zero or one to represent on or off. The typical debug output for the ping node is either "false" showing the phones WiFi to be off or out of range, or a bunch of numbers which I imagine represents the ping time, but this is no good for what we need, which is a one or zero. So we need a simple if and else command and then output this as a message to the next node. 




In the case of the phone connected to the home WiFi network the output would be topic: Reena and msg.payload = 1 as shown here
So there it is, a simple way to use your phone as part of a home automation project which is secure and safe, but don't just take my word for it, try it for yourself. You could add extra nodes to the flow for more connected mobile phones/devices and then pull all of this into a function node to deal with the logic side of things.

If you fancy giving this a go just copy the code below and import it into Node-Red the usual way.

[{"id":"3e290137.a4488e","type":"debug","z":"c5eb5e6e.d7c1b","name":"","active":true,"complete":"false","x":671,"y":72,"wires":[]},{"id":"a39c7032.c8cc6","type":"function","z":"c5eb5e6e.d7c1b","name":"Analyze Result","func":"msg.topic=\"Reena\";\nif (msg.payload === false){\n        msg.payload = 0;\n} else{\n        msg.payload = 1;\n}\nreturn msg;","outputs":1,"noerr":0,"x":324,"y":72,"wires":[["3e290137.a4488e"]]},{"id":"89cba4fe.3d2058","type":"ping","z":"c5eb5e6e.d7c1b","name":"Reena Phone","host":"192.168.0.8","timer":"5","x":116,"y":72,"wires":[["a39c7032.c8cc6"]]}]

I'll add more for this as I go along, but If you have anything to say or add to this then please feel free to comment below, thank you.