Introduction: IoT - 'Intranet' of Things : Home Automation. Part 1 : IoT, Introduction
If like me you value your privacy and want to explore the topic that is IoT, then this tutorial may well be for you.
I have deliberately named the instructable 'Intranet' and not 'Internet' of Things to underline the point that all data, information, control, traffic, communications will remain local to you and not wend its merry way to some clandestine end point out there in the internet. Muah Ha Ha Ha Haaa!
The only caveat to this would be the usual health warnings using Home WiFi. Should I become aware there are any potential leaks then I will specifically mention this risk in the body of the Instructable.
So paraphrase our American cousins, 'What happens in your IoT, stays in your IoT!'
This Instructable, the first in a series of independent but related parts, documents how to set up your own IoT infrastructure and connect the pieces together and get them interworking.
Once in place you will be able to simply add whatever IoT device your heart desires. So long as you can stick a Wifi or Ethernet connection on it that is.
To get you up and running as quickly as possible the main devices used will be Raspberry Pi, Arduino with Ethernet Shield and ESP8266-01. I chose these as there is literally a 'Tonne' of information, source code, projects, videos, blogs etc. In fact cutting through rubbish to get to the good stuff was where all the hard work went (well, that and some coding). At the end of each part, where applicable I cite the sources used so you can do your own 'digging'.
As mentioned above, the guide seeks to get you up and running as quickly as possible but this comes at a price. I removed as much of the 'Techno Babble' as I could (no, seriously I did), so as wide an audience as possible could have a go.
Later on, if you have more than just a passing interest in this Instructable topic, for the more serious home automation advocate I will recapitulate the various parts typically covering;
- MQTT Topic naming conventions
- DHCP versus Fixed IP
- Practical Pi.
- Remote access via VNC
From empirical experience I have found the true test of a successful project is how much it improves your world, how well it integrates into your lifestyle, how much you come to rely upon it, how easy it is to use and how much you miss it when it's gone. The topics above will hopefully prevent the latter.
Note : When configuring the Raspberry Pi I found it exhibited non-deterministic behaviour from time to time. You should therefore follow the examples given to the letter as I most probably won't be able to help you debug your install.
As always you follow this series at your own risk.
Step 1: System Overview
Before I describe the various subsystems in our Intranet of things I want to explain what a typical Internet of things actually is. Hopefully dispelling some myths.
Outlined in the first picture is a generalised representation of an IoT set up. It typically (but not always) comprises;
- Leaf nodes or end points which are devices with sensors to measure or interact in someway with things/parameters in the real world. Such as; Fuel consumption, speed, temperature, light levels, noise, motion detection etc.
- Those sensor devices also have a means of connecting to a network. Via; Ethernet, WiFi, Bluetooth, 4G etc.
- Finally, somewhere in the network (internet) or cloud there is a data centre collecting all this information and making use of it in someway.
And that's pretty much all it is. Well that and some application specific customisation for your needs of course.
For instance if you are a trucking company wanting to streamline your operation and drive out costs you may create your own transport system IoT to monitor fuel usage, engine ware and route taken, then regularly satellite uplink this to your cloud server and run some fancy pants software to work out driver Joe from Iowa has a heavy foot and you need to tell him to 'back off the gas a little' and not stop off at 'good ol' aunt Lucy's for a quick slice of pecan pie' on his way to a 'drop off'.
Alternatively it may be a power company wanting to plan better use of their energy grid by efficiently balancing production where it is needed most. This may result in the use of smart metering fitted with GPRS data modules uploading in real time household power consumption, then out through a GGSN gateway node into a cloud server where said power company can mine the aggregated data and cross-reference this with weather maps, school districts, spend patterns, star charts, favourite colours etc. etc. etc.
Hopefully you will have got the general idea by now.
In our case the 'making use of it in someway' is home automation. If applied well it can improve your life, automate those tasks you regularly forget, reduce your fuel bills or even protect your house. This list is truly endless.
So what does our intranet of things look like?
Well, pretty much similar as you would expect, only it's not in the 'cloud' and we will be using a human friendly, common, open interface (second picture above) via an application named OpenHAB and will only develop our own IoT devices (not buy them in, after all this is all about making). The underlying backbone of our systems (which does the transferring of IoT information packets) is via a free MQTT Broker named Mosquitto.
- Why human friendly and common? So you only have to learn one method of doing things.
- What does open interface mean? It means who ever wrote it has kindly published the details and possibly the source code such that if you wish you can modify it or even contribute to the finished article. Check out GIT HUB to see more of this. https://github.com
Step 2: What Parts Do I Need?
There will be a comprehensive parts list at the beginning of each Instructable, however in general for a basic set up with two IoT nodes you will need the following;
- A home network with WiFi connectivity (b/g/n)
- Home router with DHCP server and capability to assign fixed IP addresses within your network.
- At least two Raspberry Pis
- One for MQTT Broker
- One for OpenHAB Server
- One USB Hub, Keyboard, Mouse and LCD Monitor to set up the Pi
To complete all the examples I had initially planned you will require the following extra items;
- One Raspberry Pi with camera for Facial Recognition example
- Three ESP8266-01 (one for general I/O, one for TV Simulator, One for remote mains switch)
Note : It is feasible items 3.1 and 3.2 could be combined on one Raspberry Pi but I chose to locate them on separate devices to minimise the risk of system failure and give better performance.
Step 3: What Skills Do I Need?
If you only want to follow the instructions and not extend the examples any further, you only really require;
- Minimal electronics proficiency
- Limited knowledge of Linux on the Pi (though this will improve if you follow the examples)
However, if you want to be creative then it would be useful to have some grasp of the following;
- Some electronics proficiency
- Good knowledge of Linux
I started with minimal knowledge of Linux and Python and muddled my way through. I now have a better knowledge of Linux and Python but still muddle. :-)
Step 4: What Does This Guide Cover?
To get us all 'singing from the same hymn sheet ' this first guide covers the initial ground work of setting up a Raspberry Pi to a standard configuration. By completing this activity for each and every Raspberry Pi in the examples you will end up with a facsimile of the system I assembled.
It also covers;
- how to use PuTTY, a free open source terminal emulator : This allows you to remotely issue commands to the Raspberry Pi so you can run it 'Headless'. ie without a keyboard and monitor connected.
- and map a network drive with Samba : This allows you to drag and drop things from your PC directly onto the Raspberry Pi uSD card.
Step 5: Setting Up Your Raspberry Pi
I used a Raspberry Pi B+ purely because I had a few lying around at the time.
To create an image you will need the following;
- A SD/uSD card. I suggest 16GB as they are pretty cheap and it's what I used.
- Win 32 Disk Imager. Software used to write an image to the SD/uSD card.
- A PC, to run the software above.
Flashing your first image
Get a copy of Win 32 Disk Imager from here;
Download an image file from here (you will need 'Rasbian Jessie' I used 2016-03-18-rasbian-jessie.img);
Follow the instructions on the Raspberry Pi Org web site here;
Once you have completed this you are ready to power up and custom configure the device.
Note : My drive letter consistently comes up as E:\
Step 6: Raspberry Pi Custom Configuation
Take your Raspberrry Pi out of the box
Insert your SD/uSD into the Raspberry Pi, plug in keyboard, mouse and monitor, plug in Ethernet
Plug in power adaptor and power up for first time
The system will boot directly into LXDE the Raspberry Pi windows environment.
We will now make some custom configuration changes from within LXDE. Using a terminal window enter;
- sudo raspi-config
and select the following options, given in parenthesis (hint : press tab or up and down keys on keyboard to navigate the menu);
- (1) Expand file system to make use of all your SD/uSD card.
- (2) Change user p/w if you feel it necessary
- set to auto login. (3) Boot Options, (B2) Console Autologin
- (5) Internationalisation Options (I2) Change Timezone - Europe - London [If you live in the UK]
- (8) Overclock. - Overclock Modest 800MHz ARM, 0 overvolt
- (9) Advanced Options.
- (A2) Hostname. Change from 'raspberrypi' to 'MYHOSTNAME'
- (A4) SSH. Enable SSH
Once you complete this configuration the system will ask you if you want to reboot now so the changes take effect. Choose .
System will reboot to the command line this time.
From the command prompt enter the following commands, choosing 'yes' if presented with a selection. These commands just ensure your Pi install is up to date..
- sudo apt-get update
- sudo apt-get upgrade
This may take some time.
Once complete enter the following from the command prompt.
- sudo shutdown now
Wait for all the lights to stop flashing, then remove all connectors apart from your ethernet connector.
Now re-insert your power lead. Your Pi will reboot
That's it you're now done. Your Pi is running headless!
Shutting the Pi Down
If you ever want to switch off your Pi make sure you type the following.
- sudo shutdown now
And leave the Pi until the activity lights stop flashing before you remove the supply.
- Only plug/unplug peripherals with the Raspberry Pi powered down.
- This install assumes you are connecting to your network via Ethernet.
- To start the windows environment at anytime ensure you have all your peripherals plugged in (keyboard, mouse etc.) and type 'startx'
Step 7: Remote Access to the Pi
There many ways to access your Pi remotely to keep things simple I am going to stick to one method using an SSH Telnet Client named PuTTY. Don't worry if that sounded like 'gibberish' it really doesn't matter. Think of it as a way to type in command line commands on a computer connected to a network somewhere from the comfort of your very own computer. ie. Remotely.
PuTTY can be download for free from the following URL;
And click on the link 'You can download PuTTY here'
I have the 0.65 beta, but noticed 0.67 beta is available. Choose the PuTTY: putty.exe For Windows on Intel x86 (I am assuming you will be using a Win 7 PC to do you set ups).
Download and install.
Once installed start PuTTY. It will look something like the picture above.
Ensure the following is selected;
- Saved Sessions : MYHOSTNAME
- Connection Type : SSH
- Host Name : MYHOSTNAME.local
- Port : 22
Then Click Open
You will get a window which states 'PuTTY Security Alert' just click 'yes'. A command prompt window will open, this is directly to your Pi. Now you need to log into the Pi.
Assuming you didn't change the default user name and password.
Enter 'pi' for the username and 'raspberry' for the password.
Congratulations you've now just made a remote connection to your Pi. What is more significant is you made the connection without knowing the IP address of the server. You accessed it via the hostname you gave it.
To quit, just close the window.
Oh, and the next time you want to remotely connect to MYHOSTNAME all you do is run up PuTTY Click on MYHOSTNAME in Saved session, click load, then click open. You just have to enter a user name and password.
So what is good about this you ask? Well it means you can now create a remote server, log into it, do whatever you need to do and log out. All from the comfort of your very own PC. In fact you can open multiple simultaneous windows on multiple servers. As for the remote server, you only need provide power and a network connection.
Better still, you can copy and paste between windows and PuTTY. So if I want to enter the Linux command 'ls -l' which is the equivalent of entering 'dir' in a windows command prompt. I just select the text 'ls -l' (without quotes), left click on my PuTTY window then right click. Your text will now appear. All you have to do is press . This will save a lot of time going forward.
Step 8: Using Samba to Map Drives : Optional, for Now
So what is this all about then?
Samba is a Linux Open Source/Free seamless file and print service. Yes, more 'gibberish', just think of it as a way to make your Raspberry Pi look like an extra hard drive or USB stick to your PC.
This means you can drag and drop files between Pi and PC. Just that.
It's an optional step for now, but will be worth it later on.
Ok, to get this going you will need to carry out the following steps in your remote PuTTY terminal window. Remember to use the copy and paste method with PuTTY. Answer yes to any prompts.
- sudo apt-get install samba samba-common-bin
The next line invokes a command line editor named 'nano'
- sudo nano /etc/samba/smb.conf
Once nano has opened the file smb.conf it will look like the picture above. Scroll up and down with the arrow keys until you reach the [global] section and find the line marked;
# wins support = no
and change it to yes and remove the '#'
wins support = yes
Now go to the [homes] section under Share Definitions and ensure;
read only = no
Next, go to the bottom of the file an add the following. Once again you can select all the text below, copy it and paste it into the PuTTY window by right clicking.
######## TEXT TO ADD : START #########
comment= Home Directory
######## TEXT TO ADD : END ##########
Now save and quit. Press ^X (the control, key and the 'x' key), then type 'y' followed by return. Thats it.
Finish off the setup with the following commands (this assumes you used the defaults u/n 'pi', p/w 'raspberry');
- sudo smbpasswd -a pi
followed by password 'raspberry' twice
- sudo update-rc.d smbd enable
- sudo update-rc.d nmbd enable
- sudo service smbd restart
We're done. If you got it right you will be able to see your Raspberry Pi in windows explorer named 'MYHOSTNAME' as above.
To connect click on the 'pi' share and you will get a 'windows security' dialogue box. Enter your username ('pi') and password ('raspberry') it will look something like the in pictures.
To check if you have write access from your PC, create a text file as above then select your PuTTY terminal window and enter.
- ls -l
As explained in the previous step.
You should see this new file, the green one in the picture above.
That's all you need to do.
Step 9: So What's Next?
In the next Instructable Part 2 we will set up our IoT infrastructure by creating up an MQTT Broker on a Raspberry Pi.
Step 10: References Used
I used the following references to put this instructable together;
PuTTY Terminal Software
Win32 Disk Imager for the Raspberry Pi
Raspian Jessie Image downloads
We have a be nice policy.
Please be positive and constructive.