Raspberry Pi - Getting started with MQTT

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
get a microphone core electronics here once again welcome back to the lab today we are going to be setting up a simple MQTT program on the Raspberry Pi so let's just jump into it first we have to download some tools for MCTV so we can use pit for that if you're running pixel pip should already be installed it is a package manager for python so we're going to run through the pip install part - mqtt and we'll see that doing a thing ok that successfully installed so we can close that tunnel and in your Documents folder that's what we're going to keep our scripts today I've already copied them from our tutorial and included them in our Documents folder so let's just open that up and we're just going to have a quick look at the code just just to get a better understanding for it this is the client D MQTT client that's going to run on our raspberry pi just ignoring all the the center text here the first command is to import the mqtt client structure from the package that we just downloaded so that's we're importing that and we're calling it mqtt ignoring the definitions here our code is just this bottom part here and all its really doing is setting up the client so we with we're creating a variable or an object called client and we're invoking our mqtt package so client is being is being defined as an mqtt client object and then the next two lines of code we're just attaching some routines to the client object so an MQTT client object which was called client this . represents subroutines that it can be called and they're their standardized so anytime that client does something like connect to a server or receive a message whatever we attach to that object is what's going to be called and in this case we're attaching a retain that we've written called on connect that's this top routine here the same is true for client on message we're attaching our own routine called on message so just just to reiterate every time the client makes a connection the on connect routine is called and every time a message is received the on message routinely called and then after just attaching those objects we're going to have our client connect to the test dot mesquite org and as a viewport and I think timeout characteristics there as well once it's connected it just lives forever this is also another built-in function of the part of MQTT package so the actual script that runs is quite short the power of this script comes from these functions that we write that we attach to the client object so the unconnect retained that we write all we're saying is that we we're printing the result code so that's essentially just a static code that we can get at the terminal to tell us whether we successfully connected on a and then on the connection we're subscribing to two topics and here I've just called them core electronics tests and correlate racks topic this means that every time we reconnect it will research scribe to these topics this handled things if we lose connection for instance the on message callback that we have here all we're doing in this is printing the topic name for any incoming data and then when printing whatever data that was better known as payload after that that's that's just for a bit of fun so that we can see the raw data that comes through and what and what topic it's coming through on after that we do a little bit of string comparing we're checking the payload to see if it's equal to some predefined strengths and if that's the case we can choose to execute some other code in this case all I'm doing is printing to the shell but this could be anything this is the this is where the home automation part comes in this could be turning on some lights turning on a pool pump turning on the air conditioner whatever you like that's that's the kind of thing that would sit in these parts of the code so that's the client let's have a quick look at the publish code this is the publish demo and just as before we're importing something from the part MQTT package we're importing a publish library and all that's happening here is that we're publishing two messages so we're publishing the text hello to one of the topics and we're publishing the message world to another topic so I've got core electronics test and core electronics topic the hostname is just the server that we'd like to use so this of course has to be the same host that we connected to in the client script so let's give this a quick run just to see how it works I'm just going to minimize these and open up the terminal so this left terminal is going to be representative of our own Raspberry Pi this is going to be the local machine for instance let's just navigate to document and I want to run the client demo so we execute - mqtt client demo py and we've connected with result code 0 so that means that we've successfully connected if you get anything else here you might want to check that you're connected to a network correctly and that's that the network is key to the Internet so that is that is setting up the client it's currently running and it's losing forever now I'm going to open up another terminal and this this terminal could be representative of any internet-connected so even though at the moment it's running on the same Raspberry Pi this could be another Raspberry Pi across across the planet it could be another computer a spark or for instance or a particle so this this is just for this demonstration to keep it simple but this could be absolutely anything anything they can they can publish to a MPPT topic so again let's change directories into documents and let's run the publish code that's - mqtt publish gonna publish I'm still down oty and we can see that the scripts finished successfully and over on the first terminal we can see something happening so we got our connection that was that was when we ran the client code and then it was looting forever and then when something was published we got through the on message received call back so at the puppet is printing the topic that it received data on and it's printing the data and because Hello was one of our predefined strings that we're comparing to remember hello was one of the strings that we're comparing if the payload is equal to hello we want to print receive message number one do something and that's exactly what came up and the same thing happened again for the data world coming in on the other topic we received message number two so we did something else that's that's basically yet this is now a functioning and UTC client that we can have execute code conditionally they stop the data that it receives just for it just kind of a little bit of interest we can use control-c to to end that script I'm just going to clear the console let's edit our quiet demo let's let's change this world - hello core and save that and now the string has changed the string that we're comparing to has changed if we using the up arrow key if we rerun the client demo and we can see that it's connected successfully and if we up arrow key again we can publish to that and this time the the output has changed a little bit we still get hello and will still receive message number one so we can execute some code but then we receive we receive the data world exclamation point on the other topic but we but we haven't actually received message number two we we don't do actually get the other code and that's because the publish demo is always publishing world and we just changed our client to compare the core so because the incoming data is world and not core we're not actually executing the print receive message to do something else with only was only receive message number one so we're only doing one of the tasks that are possible that's essentially it so by now you've got the the basic building blocks to create an mqtt client and also to publish to it from another device because we're running Python this code should be able to run on many many different platforms so that should be that should be a useful tool to get you started with doing some home automation projects as always if you have any questions you can hit us up in our forums or indeed comment on in the tutorial post [Music] you you
Info
Channel: Core Electronics
Views: 172,525
Rating: undefined out of 5
Keywords: raspberry pi mqtt, how to use mqtt on raspberry pi, mqtt tutorial, raspberry pi, raspberry pi how to use mqtt, raspberry pi mqtt home automation
Id: Pb3FLznsdwI
Channel Id: undefined
Length: 10min 44sec (644 seconds)
Published: Sun Feb 19 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.