Saturday, May 20, 2017

Google Home and the ESP8266

A few years ago there was a (now cult) tv series called Eureka. The series is about a small village in the middle of nowhere were America's most clever minds live and work. They build the most fantastic devices and do deep research on all kinds of subjects for the US government. Then a new sherrif arrives and they have to give him a house to live in. The doors of his house open automatically and in his house is an all-present computer to which he can talk to and give orders like 'do the lights on' or 'what is the temperature' etc. etc. The series ran from 2006 to 2012 (just 5 year ago) and was science fiction at its best.

And guess what, it is no longer science fiction. A computer in your house that you can talk to: that's the present !!!!

And we owe it to Google. I hesitate to admit it but I love Google. Everybody is nagging at them and yet they give us so much free to use tools and gadgets. Just look at the search engine, google drive, Blogger (on which this weblog is written if you haven't noticed), the office suite, google cardboard (check into that really !!!), Android and just now Google Home Assistant.



For those of you living under a rock I will explain brief what Home Assistant is. It is the house computer from Eureka. It is Google's answer to Alexa. It is a stand alone speaker system with a build-in microphone. Indeed no screen, no keyboard, no mouse. The device is attached to the internet (be it wired or wireless) and the microphone listens to a command that activates it. The command is "OK Google" and that is something we know from Android Phones. However this is much more advanced.

There are lists with questions (easily found on the internet) you can ask Home Assistant. Examples are calculations like 535 x 345 or how many kilometres in 6 miles, or who was the first president of the United Sates, or how long is the Great Chinese Wall, or count from 10 to 25 (and backwards) or how long will it take from Rotterdam to The Hague (by car, by train or by bycicle take your pick) or will I need an umbrella tomorrow. You can also ask to roll the dice, give a random number between any two figures or flip a coin. Let it play animal sounds. You can even set alarms and timers. And a really great feature is to tell it "Remember I put my keys on tha table" and ask a few hours later where your keys are.

Basically you can ask it anything you can ask Google on your computer, and then some. And all is done by speaking out the questions and you will be answered by a pleasant womens voice. Really Amazing.

Now this wonder device will set you back at the time of this writing about 129 Dollar. But it gets better.




To get things really rolling Google supplied a Do it Yourself kit based on a Raspberry free with number 57 of the MagPi magazine. Naturally the magazine is totally sold-out.

And still better. Google gave the complete SDK free, for everyone to use.

Actually it works better as the Raspberry version. There is no button needed to activate it. Hands free commanding !!! Next to that it reacts better to questions being split up in multiple frases, and it has a timer and alarm function.


So get your Raspberry Pi out. Buy a USB microphone or use a USB audio adapter, a speaker and an SD card and build yourself a Google Home Assistant for just a few Dollar. Just follow the instructions here:
https://developers.google.com/assistant/sdk/prototype/getting-started-pi-python/

I have to admit I have wasted hours of my time just asking it silly questions. And there are many pages on the internet dedicated to publishing all the questions you can ask. Just look at this sample:
http://notsealed.com/hey-google-home-voice-commands-easter-eggs.html

At this moment there are just a few tens of millions of the original devices sold only in the US and UK as it's only language at this moment is English.

Imagine the impact this will have on society. Just think of the possibillities for disabled persons. Just command the device to set the lights on, the temperature at a certain degree, de radio at your favorite broadcaster.


Now think what we as tinkerers/hackers can do with such a device in our home. We will make our complete home automation voice commanded.



IFTTT compatibility.

You can have your Google Home (including the Raspberry DIY unit) paired with IFTTT. And then suddenly a wealth of possibillities rises.

Command your Philips Hue lights, send a tweet by voice (works flawless believe me), control your Nest thermostat, control your TV with Chromecast, keep a list of notes etc. etc. etc.

And best of all: we can control our ESP-8266 with the Google Home Assistant through IFTTT.
And that is what this entry is going to show you.

Google Home ==> IFTTT ==> ESP8266

Requirements

To get this working we need to do several steps which I discuss into detail step by step.

- Install ESP-Basic on a NodeMcu or Wemos-D1
- Set up your ESP-8266 with some leds
- Program The ESP with a Basic Program.
- Open a communication port on your router
- Get a IFTTT account (if you do not have it already)
- Set up a few IFTTT recipes
- Command your ESP by voice.

Install ESP-Basic on a NodeMcu or Wemos-D1

Well I am not going to discuss that in detail here as I have already done that in a previous story on this weblog which you can re-read here.
http://lucstechblog.blogspot.nl/2017/03/back-to-basic-basic-language-on-esp8266.html

Set up your ESP-8266 with some leds

I am using leds for demonstration purposes. But as you have seen before on this weblog you can exchange the leds for relays like in this story http://lucstechblog.blogspot.nl/2016/09/esp-relayserver.html or by transistors for controlling led-strips, pumps, motors etc.  like in this story
http://lucstechblog.blogspot.nl/2016/05/esp8266-controlled-ledstrip.html.

In this setup I am attaching 6 leds which all will be individually controlled. But as you will see in the next parts of this story you can expand this easily or use a less amount easily.

The setup is very simple.






At 6 of the NodeMcu's I/O ports (D1, D2, D3, D5, D6 and D7) is a led attached through a delimiting resistor of 1K. The breadboard layout shows it all.

Program The ESP with a Basic Program.

The program is as said before written in ESP-Basic which is a fast and easy devellopping platform for all your ESP projects. It will even work on a ESP-01 which could give you 4 output ports (I'll be demonstrating that in an upcoming story). For convenience I am using a Wemos D1 mini (clone) here. You could also use a NodeMCU or an Adafruit Huzzah.




'-----------------------------
'init
'-----------------------------
io(po,d1,0)
io(po,d2,0)
io(po,d3,0)
io(po,d5,0)
io(po,d6,0)
io(po,d7,0)

msgbranch [mybranch]

textbox command

wait

[mybranch]

command = msgget("saywhat") 
command = replace(command 

, "." , " ")
print command
if command = "living room on" then
io(po,d1,1)
endif
if command = "living room off" then
io(po,d1,0)
endif

if command = "garage door open" then
io(po,d2,1)
endif
if command = "garage door close" then
io(po,d2,0)
endif

if command = "garage light on" then
io(po,d3,1)
endif
if command = "garage light off" then
io(po,d3,0)
endif

if command = "bathroom light on" then
io(po,d5,1)
endif
if command = "bathroom light off" then
io(po,d5,0)
endif

if command = "living room ceiling light on" then
io(po,d6,1)
endif
if command = "living room ceiling light off" then
io(po,d6,0)
endif

if command = "water pump on" then
io(po,d7,1)
endif
if command = "water pump off" then
io(po,d7,0)
endif

wait


Let us look at the most important parts of the source code.



msgbranch [mybranch]

textbox command

wait


After the init part where all IO ports are set as output and put off the program starts a MESSAGE BRANCH.
It waits till it gets a command from the internet and displays that command in a textbox on your webpage. This makes it easy to determine if your spoen commands are in the right way interpreted by Google Gome Assist.
If certain commands are always faulty interpreted you can check that here and alter the command in the IFTTT part (further on in this story).




[mybranch]

command = msgget("saywhat") 
command = replace(command 

, "." , " ")
print command
if command = "living room on" then
io(po,d1,1)
endif
if command = "living room off" then
io(po,d1,0)
endif


When a command has been received from Google Home Assist the program jumps to [mybranch]

The first thing that happens is to see if  -- saywhat -- has been received. You can alter that in the program to any other word you like which would give you a bit more security. If -- saywhat -- is not received the program will not do anything.

The next line is a bit tricky:
command = replace(command , "." , " ")

Web communication has a problem with spaces. So this line replaces spaces with dots. We are going to use this program also with another way to command the same ESP chip in a future story. That is
by accessing the chip by an Android App. This way you can command the chip from within your home with Google Home Assistant and from anywhere else in the world by a voice recognition APP on your Android Phone.

To see if everything went well the received command is printed on your webpage by the print command.

The last few lines decide which command has been received and accordingly put the accompaning I/O port on or off.

That is all folks !!

Just for testing purposes I wrote a small program that only tests which command is received and prints that on your screen.


msgbranch [mybranch]

textbox command

wait

[mybranch]
command = msgget("saywhat") 
command = replace(command 

, "." , " ")
print command
wait


Use this for testing purposes just to see if your commands come through well.

Open a communication port on your router

First thing to do is to give your ESP a name and a fixed IP adress on your local network.
If for whatever reason your router crashes and gets online again, or your ESP crashes or gets powered down, it will get the same IP adress when it reboots.



So delve into your router (mostly accessible by local IP adress 192.168.1.1) and look at your connected devices. Mine looks like above.

The lower icons are the wired apparatus and as you can see it is my computer (Nescio), printerserver (actually a Raspberry) called Epson as it is connected to my Epson printer, my Buffalo Nas, my Domoticz domotica system and a raspberry. The last one is my DIY Google Home.

The top line icons are my wireless apparatus. There you can see a Thermometer (an ESP with a DS-18b20) and a device called 165. That last one is the ESP this all is about. I named it 165 as this is its IP adress and e4asily remembered.



By clicking on the name label I get information, can alter its name and can edit the check-box which gives it a fixed IP adress (Static DHCP).

Now all your local network settings are done.

As stated before normally you access your ESP from within your local network. However Google Home will need to access it through IFTTT fromthe outside world. This means that you should open a door (just like in your home) to get commands from the big wide world into your local ESP.
This is called port-forwarding.

This is the tricky part. I can only give you general directions on how to do this as all routers have different instructions on how to activate Port

Forwarding.

ESP-Basic generally uses port 80 for communication with the your network.
So that means that any command you send to the ESP (or from the ESP) is coming from the following local IP adress

XXX.XXX.XXX.XXX:80

As it is the general communication adress for all HTTP communication you normally ommit the port number (80) and just use the IP adress when accessing the ESP on your computer or phone or tablet.

However your local IP adress is not available from the outside world.

So we need to find your global IP adress. That is the adress your router uses to communicate with the outside world.
You can find that by pointing your web-browser at: http://whatismyipaddress.com/

Now you have to dig again into the setting pages from your router.
Look at something called port-forwarding.

There you can open a new port for the outside world.

In my router I have to give it a name. Next I give it the IP adress of the ESP and the ESP's port number which is 80.
Next I instruct it to send all communication from router port no 8085 to the ESP's port.

The router will most likely ask for a starting port number and an ending port number for your local and global ports. In both cases use the same. So start local 80, end local 80, start global 8085 and end global 8085. You can use many numbers as a port number but there are some restrictions. So delve into that by checkin information on the internet about this. This method allows you to open many specific ports for all kinds of projects.


 As you can see from my setup I have my Domoticz system at port 8081 and my ESP at port 8085

That is it.

Now the only thing left is to instruct your app or whatever you are using to crontrol the ESP to send or get the info from:
http://www.YYY.YYY.YYY.YYY:8085

The YYY part is off course the global internet adress

Be aware however that opening a port in your router might bring some security issuses as you are opening your router to the world.

The last steps

Now we have an ESP that can communicate with the outside world and a Google Home Assistant that can activate all kind of things by voice commands. The only thing to do is to have them talk to eachother.

There are two ways to do that.

First is the complicated way.
You could alter the Python code that is the motor of yout DIY Raspberry Home Assistant.  The advantage would be that you actually could use just your local IP adress and not have to connect your ESP to the outside world. This would certainly be a more secure way to achive communication.

However that would mean delving deep into the code and making a system that only works for your setup.

The second method is the easy way.
Have the Google Home Assistant talk to IFTTT and let IFTTT send through the Maker Channel commands to your ESP.

This has many advantages.
It is easy to implement. And it can not only be done by the DIY Home Assistant but actrually also by the Google Home Assistant. So wether you build a Home Assistant or buy one this will work.

This story uses the second method.

Get a IFTTT account (if you do not have it already)

I am not going to tell you how to do that as I have already wrote a detailed story on this which you can read here http://lucstechblog.blogspot.nl/2017/04/ifttt-if-this-then-that.html

Next connect to the Maker Channel. You can read how to do that here http://lucstechblog.blogspot.nl/2017/05/ifttt-part-2-maker-channel.html

Just be aware that IFTTT has altered the logo for the Maker Cahnnel and altered the total setup. But let that not frighten you as it is basically the same actions.

Set up a few IFTTT recipes

Almost done. These are the last steps.



First step is to choose New Applet on the right top of your screen. Then click on the blue IF.



Search for Google and the Google Assistant label will emerge and choose that.



Now you will be presented several options. As we want to turn switches ON and OFF choose the third option being -- Say a phrase with a text ingredient --



Fill the form in like the above example. You can use your own texts and commands as you like.
There is one special character in this App and that is the $.
The $ is required in any phrases you are going to fill in. It is later on substituted in the command that is send to the ESP by the word -- on -- or --off -- and will actually set the light on or off.

For demonstration excersises I advise you to use the setup as above.
Now choose Create Trigger at the Bottom.



Next step is to choose the then component so click the blue that.



Search for maker and Choose the Maker Webhooks.



Only one option here so choose that one.


Now complete the action field and fill in your global IP-adress which you found at http://whatismyipaddress.com/ put a column behind it immediately followed by the portnumer (being 8085) that you set in the router. Now click on the white button that says Add Ingredient and choose Textfield. Please make sure that no spaces are added in this line.
Also make sure that you really fill in the dots in living.room. and do not forget that last dot.

Some clarification.
IFTTT and your Google Home will not accept spaces being communicated. That is why I put dots in the place where the spaces would be.
And now you understand what the line in ESP-BASIC program is for.


command = replace(command , "." , " ")

This line filters out the dots and replaces them back into spaces.



And lastly alter the content type in text/plain. The method GET is the one we need. There is no body. All information is in the header.

The TextField will be substituted by the command we gave using the $ in the previous step.
Again carefully inspect the APP and make sure there is no space included between living.room. and the {{TextField}}
If there is a space your ESP-Basic program will not be able to recognise the command.

So if we say "Switch the living room light on" IFTTT will send the next command to our ESP:

http://xxx.xxx.xxx.xxx:8085/msg?saywhat=living.room.on

Now click on the button Create Action



IFTTT gives you a quick summerisation. Now click on the big blue Finish button.

And all is done.

You can check the complete IFFF command structure by clicking on the gear on the top right side and make alterations.

In just a few seconds your APP will be activated and you are set to go.

Test this thoroughtly and try to understand what is happening.
If you do understand how it works you can make the rest of the APP's in IFTTT to get the other led's running.

Command your ESP by voice.



Every thing is set now. Above you can see my setup. I am looking for some kind of casing however I want it to be special. Like a womens head used in clothes-shops or a crystal ball or something like that.

The only thing you have to do is just say the Magic Words: OK Google
And wait a second.

Then say: Switch the living room light on

And the led should come on.



Just for fun look at the video demonstrating how it all comes together.
Ehhh and please do not comment on the mess.........



And just look at that. More fun coming up.
I connected Raspberry Home Assistant through IFTTT with my Domoticz system. So now I am not only able to connect to ESP's but also to commercial available lightning systems like Philips Hue or Klik-Aan-Klik-Uit and their cheap counterparts from the local dollar stores. These systems are commanded by a 433 Mhz frequency which is send by my domotica system that gets its commands from Raspberry Home Assistant. But that is a whole other story which I will save for a next time.

Till then.
Have fun

Luc Volders

Friday, May 5, 2017

IFTTT Part 3 - Basic alert over IFTTT

In a previous post (read it by clicking here) I gave general information about a PIR. To summerise it up: a PIR is a movement detector. So I wrote that you can make a burglar alarm with it. Well here we go.

However there is something special with this alarm. Most alarms based on movement detection will make some serious noise when activated. That will scare some people undoubtedly. However what if you are not at home. The noise will not be heard by you and so you will not know that ytour house is broken in.

So we are going to use the IFTT Maker channel like described in this article for sending you a notice on your phone when movement is detected.

Besides that we are going to do it in BASIC. Just to demonstrate how easy it is.

First let us look at the setup.








As the breadboard layout shows you I used a NodeMCU for this. The PIR is attached to D3 and gets it's power from the 5 volt line from the NodeMCU which is situated on the first pin on the left side of the board named VIN. As described in the general story about the PIR: it will not work at 3.3 Volts. So luckily the NodeMCU has a 5 volt power line.

A led is attached with a 220 ohm resistor to D2.

Overkill ???

Ok I could have done this with an ESP-01. So why use a NodeMCU for it. At the time when I write this story the price difference between the two is just 60 cents. So its economics. The ESP-01 does not like to have things attached to its GPIO's at startup. Therefore I should put a switch between the PIR and the GPIO and wait till the ESP-01 has started before flipping the switch to attach the PIR to the GPIO. Next the PIR works on 5 volts, the ESP-01 on 3.3 volts. So I would need to have a 5 volts power supply attached to the PIR and a 3.3volts voltage regulator attached to the ESP-01. Add it all up and a NodeMCU with its on board 5 Volts and 3.3 Volts and loads of I/O's which are free to use all the time is an economic better choice.

The BASIC program works as follows.




First nothing happens. That is important as the PIR has to settle for about a minute after power-up. The program will start after you press the Start button. Now the program waits for movement to be detected and when that is the case it will activate the LED and send notoice to your IFTTT maker Channel. and that will send a message to your Phone.




The led stays on even if there is further no movement detected anymore. The led will only go off when you press the Start button again. At that time the whole process will be repeated.

Now let's look at the program.


 ' this program checks movement  
 ' with a pir controller and if detected  
 ' send alert to your phone  
 io(po,D2,0)  
 wprint "<h1>Movement detector</h1>"  
 wprint "</br>"  
 wprint "</br>"  
 button "start", [click]  
 wprint "><><><><><><"  
 button "Exit", [progend]   
 wprint "</br>"  
 wait  
 [click]  
 io(po,D2,0)  
 do  
 movement = io(pi,D3)  
 loop until movement = 1  
 io(po,D2,1)  
 print wget("maker.ifttt.com/trigger/Motion detected/with/key/PUTYOURIFTTTKEYHERE")  
 print "alert"  
 wait  
 [progend]   
 end  


Pretty straightforward.

First I define two buttons. One for starting the movement detection and another one to end the program.

Now if the START button is pressed the programs waits until it detects a HIGH signal on pin D3 meaning that the PIR was activated. The program then puts the LED on. And this single line:

print wget("maker.ifttt.com/trigger/Motion detected 2/with/key/PUTYOURIFTTKEYHERE")

is all that is needed to activate your IFTTT Maker Channel

Some HTML code was used for pimping up the page like the H1 code but that is just for looks and not for functionality. Just like the line that prints the word alert.

So I urge you to have a look at ESPbasic it is definitely worth it.

The endresult.



After thorough testing I put the breadboard setup on a striboard.


Next I made a large casing for it.



And this is how the finished project looks like.

You can find all the necesarry files being the Basic source code and STL files for the casing on my github repositry.

https://github.com/Lucvolders/PIR-Movement-detection

That's it for now.

Have fun

Luc Volders

Monday, May 1, 2017

IFTTT Part 2 maker channel

In a previous story, which you can read by clicking here, I showed you how to automate all kinds of things on your phone and computer with IFTTT. And I told you that IFTTT has a special aid for makers/tinkerers/hackers called the Maker channel.

The Maker channel is intended to trigger all kinds of events with an IOT device. My favorite IOT device is the ESP8266. So let me show you how you can trigger all kinds of events with this wonderchip. In this case we are going to send a message to Twitter when the ESP has detected motion with a PIR. To say differently, if someone enters your room in your absence you will get a notification.





First step is to login and choose channels. Then scroll down to develloppers and choose the Maker Channel.




 

In the Maker Channel choose connect and fill in the form. I have blacked out my personal details but you get the drift.

The important part is the Makerkey. You will need this later when setting up the softawre in the ESP-8266.



Now choose for making a new channel




The first question is to choose a trigger channel. Fill the name Maker in.



Now choose for maker.



Choose the trigger you want to activate. There is only one choice for the maker channel "Receive a web request"



Give this event a name. Choose any name you like but make it significant and something easy to recognise. If you have many triggers activated it will be easy to locate if you need to make any changes. Click on "Create Trigger"



Now move on to the next section. The IF part is ready so we have to move to the "That" section.




IFTTT asks you for the Action Channel. Type twitter and the famous logo appears.


 
The twitter channel has all kinds of actions that it can perform. We just want it to post a message so choose the first item "Post tweet"



In this screen you can edit the text that will be tweeted to your account. You can change that text in anything you like. As you can see in the next screen I edited it for a more meaningfull text. On the right side you see a small blue flask.



Clicking the flask will give you an option to add a timestamp. This is something you should consider as the tweet will not only tell you that there was motion detected but also at what time that was.


 
In this last screen IFTTT shows you the complete recipe with the text that will be tweeted. Choose create and it is ready to being used.

So the next part is building the hardware being a PIR attached to the ESP-8266 and have it send a command to IFTTT when there is motion detected. And that's the story that Is following next time.

Till then
Have fun

Luc Volders

Saturday, April 15, 2017

IFTTT - If This Then That

If you, by now, haven't heard about this service it is time that you did. IFTTT is a free service that can make all kind of things in your life much easier.

First if you think it looks like some kind of programming language. You are wrong. You do not have to have any programming knowledge to use the service. The term IFTTT has however indeed originated in the programming world. It looks like the IF.......THEN...... statements in many programming languages.

So what does it do. Well exactly as the name suggest.

IF THIS (an action you perform) THEN THAT (Initiate another action).

So what can we do with it.

Let me give you a simple example.

I make a lot of photo's with my Android Phone. In fact I use my phone more for photographing as I use my regular cam. Well that is just because it is so convenient. The phone has a good camera quality and I always carry it around. But there is one flaw. I make a lot of photo's but I always forget to upload them to my computer. So they are stuck on my camera and there is always the risk of loosing them. In a previous story I told you that I use cloud services like Copy, Dropbox and Google Drive to upload my photo's, but you need the discipline to actually do that and that is where it lacks me.
So wouldn't it be great if you could automate that.

And that is where IFTTT comes in.


Direct your browser to ifttt.com and open an account. As it is a free service there is nothing that should prevent you from doing that.

In IFTTT you can make automated actions called Recipes. So I made a recipe that works as follows:

IF (I make a photograph on my phone) THEN (upload it automatically to Google Drive).

Ok let's do that in a step by step order.




First click on the tab 'My Recipes'
And choose Create a Recipe. A new screen opens that looks like this:




 Now click on the blue 'this'


The screen changes and there is a line in which you can type what you are looking for.


As you can see I filled in that I was looking for android.
Choose Android Photo and IFTTT gives you some options.


 Choose 'any new photo'




 Now press the blue button that says 'Create trigger'

Next step is choosing what happens after the photo is taken. The IF THIS from IFTTT is IF a photo is taken. We are now going to tell IFTTT what to do: the THEN side. So click the blue 'that' and search for the Google Drive logo and click on it.








Connect to your Google Drive account by filling in the form.
Just finish the required steps (for privacy reasons I do not show them here) and you're done.

Next thing is to download the IFTTT app on your phone from the play store and activate the account on your phone and you're set to go. Each time you make a new photograph with your phone the picture will automatically upload to your Google drive account.


I made the same recipe for my dropbox account. So a picture taken is automatically written to both my Dropbox as well as Google Drive. Just for safety.

However you can make things easy by making the following recipe:

This means that any new posting in your Drop-Box account is automatically copied to your Google Drive.

There are hundreds no maybe thousands recipes to discover.


This recipe couples Blogger to twitter. So the IF is Blogger and the THAT is Twitter. This tells IFTTT to post any new web-log entry automatically to twitter.


The recipe above is just for your phone. The IF is your home location and the THAT is to turn wifi in your phone off to save battery power.

Just browse all the channels in IFTTT and use your imagination. There are hundreds of possibillities which can make life a lot easier.

And then there is one channel on IFTTT that deserves our special attention and that is the Maker Channel. Using the Maker Channel we can trigger events with an ESP-8266 for example. This gives us tremendous possibilities with the Internet of Things. I'll show you how to use that in another story.

For now. Just try it and have fun.

Luc Volders