MQTT with a Raspberry Pi and an Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name's Kerry Sims and this is garrix Peter has my unexpected snow here and I'm on my one trip today to go and buy some food from the shops we can feed ourselves but today's video ignoring all that I want to talk about mqtt and how you can use it on an Arduino and a Raspberry Pi so if you want to find out more please let me explain [Music] okay stay we can we talk about MQTT which is one of the kind of backbones of the IRT in terms of things how small sensors and microcontrollers can talk to each other it's all this data that's been collected can be analyzed and shared or a smartphone in the cloud whatever now MQTT stands for mq telemetry transport now a common mistake is people think the MQ part stands for something like you know maybe a message queue actually there are no message queues in MQTT and the MQ was actually just part of a product suite that actually was part of IBM at the time that this thing was invented so it was queued the MQ telemetry transport and the mg doesn't actually really stand for in the oven that was a brand name that was being used at the time in IBM now there are some interesting features about MQTT it's a publish/subscribe model which we'll talk about in detail in a few moments it's very lightweight I'm going to show you a version running on an Arduino so he doesn't need very much terms of computing power or RAM to actually get it working as it's designed for constrained device like devices like microcontroller Arduino as all the Raspberry Pi o pi and that makes it therefore ideal for IOT the Internet of Things so what is transport in the terms of telemetry transport if we think about people then let's say you've got some people down here they want to get to this place up here and we go via a transport system where there's buses or taxis or cars or trains or trams bicycles walking we have a transport system which transports people from one place to another so if we look in terms of IOT basically there are messages that we want to go from one place to another place to a destination and we need a means of transport a pipe that you can send the messages down and this runs over tcp/ip so we're talking Ethernet Wi-Fi over the Internet so these small devices can send this information across the internet cause a local area network from one device to another see here I've got a simple example I've got a smart phone here and I've got a light bulb both of them can connect to my local Wi-Fi and I want them to talk to each other well how do we do that well if we want to send a message for example that says you know switch the light bulb on or off from the smart phone then we're gonna need a way to do it and mqtt provides that way you can send an on message or an off message over MQTT and it gets from the smart phone to the light bulb problem we've got is that most addressing nowadays is dynamic so what is the IP address of the Lipo imagine if you had to set up all the light bulbs in your house to have fixed IP addresses you need to keep a you know a document of address all the addresses that you've allocated and kind of start doing your own Network administration and does it change if you change the light bulb you know what's the new light bulb gonna be and with if you know when if you're using the DHCP or is it coming from a Rooter we change tomorrow does your smart phone everything's dynamic it's very hard to know how you get this connection between these two because today they said up might be one thing and tomorrow it might be different and this is where an MQTT server sometimes they use the word broker comes into it basically I can send a message from the smartphone over to this server this gateway that's running on your network and then that sends it over to the light bulb and this thing here has a fixed IP address so you've got this running on a piece of hardware could be a desktop could be a Raspberry Pi which is what I'm running on and this has got a static IP address on your network and everything else has got dynamic IP addresses but as long as you all know the address of the Gateway so you know the address of this broker then the messages can go apart because this connects to this so the smart phone connects to the riser pipe the sum the light bulb connects to the Raspberry Pi doesn't matter what addresses they've got as long as they can connect then the Raspberry Pi knows it because they've already said hey we're here and then it can start routing the messages between them and so there are two modes inside of em QT team is very important there's the publisher a device that sends as publishes things and there's a subscriber a device that wants to receive that information and that's what it's like you know where you have a magazine publisher or a newspaper publisher or website publisher and we are subscribed as viewers readers of those things we like to see what information is being put out in this case it might be a light bulb might be a temperature sensor whatever and it's publishing data and someone else wants to receive that data and is interested in it so let's take the idea of a temperature sensor a temperature sensor connects to the MQTT server as a publisher says hey I'm here and it starts publishing every few minutes whatever the setup is the current temperature in in in the living room for example and then the smartphone might connect to the same MQTT server this gateway as a subscriber say I'm interested it registers interest I'm interesting data from that temperature sensor and then whenever the temperature sensor publishes something the smartphone will receive it because the MQTT server broker make sure that anyone that registered interest in that thing will receive that data so the messages only go to the devices that want to hear about them if I don't want to hear about the temperature but I do want to hear about the status of a light bulb or the size of an alarm system then you can configure which topics you're interested in again the idea of publishing i'm interests in a particular topic and so the temperature sensor connect to the MQTT server as a publisher it publishes every n minutes and it publish it on the topic called living room temperature so every time it sends that piece of information back the temperature it's a tagged attached to it this topic saying this is a living room temperature topic message related to that and then the smartphone likewise connected to the same subscribe to the same server and it says I'm interesting in all information that is connected to the living room temperature so it knows then that it needs to receive that information so whenever the temperature sensor publishes something to a living room temperature the smartphone will receive it because they're both connected the same topic this is the idea of a light bulb this one's a bit more complicated the Bob connects to the MQTT server as a publisher saying it's going to publish under the topic of bulb status and it also connects as a subscriber saying I'm interested in any bulb commands so one of them it's publishing the status and one of them is receiving the commands and the smartphone does the opposite the smartphone connects to the mqtt server as a publisher saying I'm gonna publish commands I'm sending out commands and I'm going to be a subscriber to find out about the bold status or what to hear about the bulb status so to switch on the light the smartphone sends the on command it could send one because in on it could send turn on whatever you decide with inside the topic of bulb command then the light receives the on message add is a subscriber to bulb command it switches itself on and then it sends a lit message with the topic bulb status then the smartphone receives the lit message as it is a subscriber to bold bulb status let's look at that as a diagram here's a smartphone it says I'm a publisher of bulb command and a subscriber of bulb status the light bulb I'm a publisher of bulb status and I'm subscriber of bulb command and then the smartphone sends an on command inside the bulb command topic because it's inside that topic and the light bulb is a subscriber of that topic it arrives at the light bulb light bulb switches itself on it then sends back a lit under bulb status and then the bulb status finally gets back over to the smart phone because the smartphone is a subscriber of bulb status of the smartphone can know it's issued a command and effectively gets a response back saying yes I've switched on because because this is a subscribe published model you can actually have multiple devices subscribing or publishing so for example here our smartphone is as before it's a publisher of bolt command and a subscriber of bold state bulb status and the light bulb the opposite we've got another phone down here which is just a subscriber of bulb status so the same thing happens in the on command the on command goes to the light bulb light bulb sends back the lid on bulb status but because now there are two subscribers to bulb status is another message sent to the other smartphone so on its app on its interface it can show the bike the light bulb has been lit up even though it wasn't the actual device that issued that command in the first place at this point you want to just recommend a course to the Linux power user bundle we're going to be using the Raspberry Pi in a minute so maybe you need to brush up on your Linux skills this is only nineteen dollars at the moment if you want to get this course it looks like you're gonna get lots of interesting staff Linux alternative to Windows applications Linux for beginners then it's command line essentials learn Linux in five days and level up your career only $19 not only will you level up your career not only we will learn something you'll also help out this channel as this is an affiliate link link in the description below ok so what we do is we go over to a Raspberry Pi and we're gonna first of all install an MQTT server called mosquito ok and to do that you just do sudo apt updates who do app install mosquito and mosquito clients and then we're gonna run the mosquito server so I'm assuming you can know how to do that you're familiar with installing apps on the Raspberry Pi and then what we're going to do is we're gonna have a subscriber and a publisher and so this one here is gonna say and have three windows open one for the server one for the subscriber one for the publisher this one says I am interesting anything under the topic Gary - t4 topic and then we're going to publish on the topic Gary the message hello and we'll see that the goes through the server and arrives at the subscriber so let's switch over to the Raspberry Pi and do that now ok so here we are on the Raspberry Pi in the middle window I'm going to run the mosquito server so there it is running it's just basically I've started up and I'm listening and then on the left hand right hand side here I'm going to run this command these are some of the clients that come mosquito sub mosquito public clients and I'm basically say I'm interested in anything to do with the topic Gary and it just sits there now waiting to hear on the topic Gary and the server just showed us that their client connected there then over here what I'm going to say is I want to publish something I'm a publisher on the topic Gary and I'm going to say hello soon as you hit that goes through the server and we can see here hello has come up because I'm interested in messages on the Gary topic and I can eat that as many times as I like and it will just keep sending and we can send other things as well hi you know whatever and they all come up here on the subscriber so this mqtt server is basically making sure that the anything that's published on the topic Gary gets through now if I change the topic here to you know barb we're doing this for barb then it gets sent to the server but nothing over here because this subscriber is not interesting messages with the topic of Bob nothing to do with me I'm don't care I don't want to hear about it only when we say this is for Gary then it actually can turn up over here okay so that's a very very simple inside the command line look at how you can publish data you can subscribe to data ins and using mosquito as a broker as a gateway to make sure those things get routed around to the right places now that you can also use mqtt apps for example clients on android so there's one I have here that I've been using I quite like it I'm not affiliated with this in any way it's just one of the ones I tested out and actually is not that bad at all so we're gonna open up this now and show how we can connect to the same broker and send messages on the topic of of Gary okay so here I've already entered into the app and I have set it up to say that I'm connecting to that raspberry pi and now you can actually build your little user interface it we can say add a panel we're going to add a text log and what we're gonna say is I'm interested the panel's name we will call it log and the topic of cause I'm interesting is Gary and one thing to note is that it is case sensitive all through MQTT the topic names are case sensitive and so we've got this little log here and I've cuz I have know this quite well I know that you can go into advanced option you can make this a bit bigger so we'll make it six lines let's say rather than just two so there is a log of Gary now if we go back to the raspberry pie here and publish something now on Gary we can see that it appears here and it's appeared inside of the other subscriber on the raspberry pie let's make this a bit more or unique let's just say two question mark is it coming to two places yes look at that it's arrived in both subscribers so if you have multiple subscribers they can both they will both receive because there is subscribing to this topic which in this case was Gary now another thing I can do inside of this Android app is I can create a button for example let's just call it um okay I'm a topic again is Gary that's very important and we're gonna have a payload of let's say button rest okay exclamation mark that's what we're gonna say so every time this button is pressed on here now the interesting thing is is that we are now gonna send this on Gary and of course we're a subscriber here on the Android app - Gary's when we hit button we're gonna have button press comes up in our little log here here and it comes up on the subscriber in the Raspberry Pi so now we've got a way of sending things publishing things down here and we're actually seeing that's coming up on the climb a subscriber to the channel Gary so you can actually build up quite an interesting interface like all the different things you can do here and you can get them to do things with mqtt now I also want to show you how you can use on an Arduino what I've got here I've got an Arduino with a basic button on it I won't show you how to build that there's lots of tutorials on the internet for building that it's basically about a resistor and you basically just need to connect it up to the right pins a pin for detecting the GPIO and for the power okay and so I'm gonna have that button and it connects up I've got our dueƱo code on here which got an mqtt client on it all of this will be inside of my github repository and it connects to the same raspberry pi case to the same raspberry pi a broker mqtt server so that we can see our an arduino talking to it now in the arduino what we're gonna do is we're gonna have to channel our topics arduino simple an arduino command two things notice again case sensitive because i'm using arduino now all in lowercase own where I use Gary in uppercase and you can actually have kind of like subtopics so here it's Arduino / simple do we know / command and that actually can be you can actually develop this scheme quite well and there are ways of subscribing to wildcards and think I won't talk about that now but it basically allows us to say well okay this is the Arduino you know send channel this is the Arduino receive channel effectively the subscriber the publisher and you can kind of divide up your topics inside of the the MQTT broker so it's much more organized much more ear so delineate it and what we do is inside command we're gonna send on and off and if that happens the AED on the Arduino here will also switch on or off and if I press the button it will send something on simple so that the message appears inside of subscribers too simple so let's go ahead and do that okay so what we're going to do over here and our subscriber is now we're going to subscribe to our do we know / simple okay so that we can find out the things that are going on there and what happens is when I press the button now on the Arduino then that's actually going to send a message on Arduino simple and we can see as a subscriber to it it sends you the button and the button counter tells you how many times it's been pressed since the program has been running so there we go and also over on this side we can actually now send a message on Arduino command okay and we can send the message on and that will allow us to turn the LED on on the Arduino it's now on and then if we send off we can turn it off again on off and that is a great like we're controlling a light bulb we're effectively controlling our own little light bulb there and of course we can do all this from the android client so let's go back into our android client there and have a look so we'll get rid of our existing panels here and what we'll do now is we'll create a panel which will be a log again and this one we'll just call law but what we're interested in now is Arduino / simple that's what we want to receive remember it's case sensitive so now if we press our button we should see the button count also coming up here inside of the app and it's also of course coming up on the client on the Raspberry Pi which is also subscribe to the same topic and now we can actually create a switch here turning on our light bulb on and off it will just call it switch and the topic is arduino / case sensitive there command okay and we want when we turn it on we want to send the word on when we turn it off on us in the word off okay and then we create that now and then now here we can flick the switch and we can turn the light on and off on the arduino okay that's it my name is gary sims as gary explained i really hope you enjoyed this video if you did please do because these are giving it a thumbs up and why not subscribe to the channel if you like this kind of content and I don't forget I have the gary explains newsletter and if you want to receive that which will also not only cover what's going on here on the go explained channel it will also cover things I found interesting on the Internet in the most recent while and so maybe you'll find some things that are useful and educating okay to do that go over to Gary expense comm and sign up there on the forum ok that's it I'll see you in [Music]
Info
Channel: Gary Explains
Views: 41,221
Rating: undefined out of 5
Keywords: Gary Explains, Tech, Explanation, Tutorial, MQTT, MQ Telemetry Transpor, IoT, Internet of Things, Arduino, Raspberry Pi, Cloud, Mosquitto MQTT, Mosquitto Broker, Mosquitto, Broker
Id: p3vJxGKWDIg
Channel Id: undefined
Length: 19min 34sec (1174 seconds)
Published: Tue Mar 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.