#48 Connect ESP8266 with the world (and IFTT) through MQTT and Adafruit.io (Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
create see youtubers here is the guy with a Swiss accent again in episode number 29 we were able to connect our device to an existing service in the cloud and get useful information for us today we will get active and either switch something somewhere in the world or start something on our esp8266 from somewhere in the world how can this be done if you connect your ESP to your home Wi-Fi it is hopefully not easy to see it from the internet your router or firewall protects your wifey from others surfing on the Internet if we want to get information from the Internet this is not a problem because we call a service and the service answers back to our ESP because we called the service knows how to reach us and provides us with the answer the firewall is no problem for this case the other way around it will not work because of our firewall we can solve this problem but it's not easy one solution is to use a dynamic DNS service to make sure our address is known to the outside and in addition open particular ports on our router this is very inconvenient and inflexible we need a better solution and it has to have a very small footprint to fit on our small devices and it has to be standard that we get freedom to choose and of course it has to be compatible with our beloved esp8266 fortunately in 1999 IBM developed the MQTT protocol for machine to machine communication this is a simple protocol which enables small devices to communicate in the meanwhile it became an ISO standard and everybody can use it freely MQTT is based on a publish/subscribe model what does this mean your device can either publish something to the world or subscribe something from anywhere as long as it is able to connect it think of YouTube you either publish content on your channel as I do right now or you subscribe to my channel as you hopefully already did this sounds great but does it really solve our problem unfortunately no we still have the issue that we cannot establish a connection to our device from the outside of our home network fortunately this publish/subscribe model has an advantage which will help us solve this issue let's assume we place another device which understands n QT t somewhere in the internet and publish its address then it is easy for our ESP to send a message to this device because its address is known to us and this device could respond easily because it knows the path back to our ESP so let's assume we measure the luminosity in our office and we publish it to the device in the Internet this will work without problems and the device in the Internet knows all with if our light is on or off this is the publish part of the protocol now let's assume that you want to know if I'm working or not then you could subscribe to this information this is the SUBSCRIBE part of the protocol simple of course this works also in the opposite direction and others can subscribe to to the same information these information channels are called topics in MQTT the device in the internet which can deal with the MQTT messages is called MQTT broker there are some free MQTT brokers available in the internet today I will use the broker which is hosted by Adafruit I it is still in beta and I hope you will be able to get an account as I did if not you can use cloud MQTT or another service be aware that these services are free but might also not be 100% reliable but to do our tests they are ok let's try now to publish the luminosity to Adafruit IO for ease of use we use an mqtt library on our ESP board I use the Witte board because it can measure luminosity and switch a light on or off I tried three different MQTT libraries all work but their implementation differs I enclose the link to them and also link to the sketches are used for my tests for this tutorial I use the pop shop client library because it's readily available in the arduino ide and can be installed as all libraries just search for MQTT in your libraries manager the sketch is quite straightforward we connect to the Wi-Fi as usual next we have to define the MQTT broker with an address and the port number you find this information at the home pages of these services next we have to connect to the broker to do this we have to enter at least username and password of our account on the broker page the password sometimes is also called key you get all this information if you create your account on the broker I put this program part into the loop if the connection to the broker is lost it automatically reconnects here you find also the subscribe command for a particular topic as soon as we establish the connection we can measure and publish our luminosity value because Adafruit taught of 2 uploads per second we delay our measurements and only publish if the new value is different to the value before here we have to do some C++ string handling to get the number into the format expected from the library our topic name is luminosity on Adafruit il you have to add your username and the word feeds in front of your topic name so we end up with the topic sensors IOT / feeds / luminosity you can test if you can subscribe to this topic this should be possible from your account please leave a note in the comment if you were successful now we upload our sketch start the serial and go to the website of Adafruit I oh here we go - your feeds nothing is there but if we refresh we see that our topic was automatically created when the first messages came from our small esp8266 we did not have to tinker around on the broker beauty as Steve Jones would say on Adafruit dot we get more than just broker services we also can design a small dashboard to show our feeds we create a stream block and see the values coming from our ESP we can also create a gaucho and see the values visually everything straight forward now let's assume we want to switch on our LED on the Witte board remotely to do that we create a topic command on 8 of route IO for the moment we want to create messages for this topic manually therefore we create a new block with an on/off switch and call it command now we can create messages with the topic command and the content on or off let's go back to our ESP and subscribe to the topic command if we subscribe to a topic we get all May with this topic and can react on them a function called callback is responsible for this handling please keep in mind that mqtt transfers all values as characters so we check for the second character of our message if it is F in off then it means that the command received was off so we can read the luminosity in my lab and switch on the light if we want both from wherever you are in the world again you can try to switch my LED on or off the topic for this is sensors IOT / feeds / command but how can you know if my LED really switched on this is our next topic reliability of transmission in real world nothing is completely reliable for example our ESP could be offline or crashed or the internet connection is not working for a moment etc etc mqtt offers so-called quality of service levels I was not successful in using them so I will not cover this topic now but as engineers we can implement a workaround we create a new topic called client status publish the status of my LED and include the feed in the dashboard now I can switch the LED and see nearly real-time the effect keep in mind that the message has to cross three times with Atlantic Ocean first when Adafruit I owe sends the message to my ESP second my ESP sends the message back to Adafruit and third Adafruit updates my internet browser pretty fast and of course if you subscribe to the topic sensors IOT / feeds / client status you see whether you are successful switched my LED on or off or at least you see if I work or not I hope this works at least lady ada said that it should be possible to subscribe to topics of other users since I have only one account I was not able to test it so anyway we created a neat remote control for my ESP device but this is only the beginning of many other stories other devices can now subscribe also to the luminosity topic or publish a command as an example I connect my iPad I use an mqtt client app to publish and subscribe to my topics on Adafruit I oh now I see the luminosity on my iPad and can switch the lamp on or off from there or I can use a note read installation to switch the LED or to check the luminosity or you can connect Adafruit io2 IFTTT for all who do not know this great site it's name is derived from if this then that you can create recipes for example if your lamp is on send a mail to your spouse that you are home of course only if you really want the Gino's or you could create an emergency button for your old mother or father which when it is pressed sends you an SMS that you know there is a problem there are other topics of mqtt which I will cover in a later video if you subscribe you get an automatic update you need not to use MQTT for that I hope this video was useful or at least interesting for you bye you
Info
Channel: Andreas Spiess
Views: 99,679
Rating: undefined out of 5
Keywords: MQTT, Arduino, electronics, hobby, DIY, microcontroller, mcu, esp8266, Wi-Fi, adafruit.io, IFTT, witty, esp8266 project
Id: 9G-nMGcELG8
Channel Id: undefined
Length: 13min 17sec (797 seconds)
Published: Fri Mar 04 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.