MQTT Network Controller

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi in this video I'm gonna show you how to create an MQTT server and how you can use that to interface different devices together to allow them to send and receive messages to each other so on the board here I've got three different devices I've got a Raspberry Pi I've got a we loss deewan which is running an esp8266 chip and I've got an Arduino Uno now these are all running puzzles which I've shown how to create in previous videos so there are three pi has got a little reed switch here so if I take a magnet and pressed it up against the reed switch you'll see the relay comes on and if I do it again it'll go off it okay the way Moss this is running an RFID puzzle so if I take an RFID tag and hold it up against the sensor you'll see that this relay becomes activated and the Arduino this has got a light dependent resistor here so if I hold my hand up over the resistor block the light you'll see that the relay gets activated here now all these puzzles are standalone they don't need to be on a network they don't need a server to control them or anything like that which is great but there are times when it's quite useful to actually have some kind of overarching server that can monitor and remotely track either the state that the puzzle is currently in or maybe to force it to a different state to reset it or to make it solve for example and that's what I'm going to show you how to do by the end of this video so I've deliberately chosen three different types of controllers here to show that what I'm gonna explain to you is kind of totally agnostic of what platform you might be using it on but they're all what we call mqtt clients and on my pc i've got an MQTT host running a broker and what that means is if I just turn this window once you can see what's happening here say this is the output from my host device when any of these puzzels changes state what you'll be able to see is that they're publishing a message to that server I turn this one off and you'll see that the server is able to get a notification every time the state of one of these puzzels changes which is very useful and not only that but the host is able to publish messages itself which is received by these devices so if I put an input window on like this and let's say I want to send a message to reset or maybe I want to force one of these devices to be solved so let's say I want to send a message that forces the Raspberry Pi puzzle to be solved what I can do is I can enter into this command window here so our du mosquito pops and mosquito is the name of the mqtt broker I'm using part because I'm gonna publish a message and then I'm gonna send a topic which a topic is a way of describing which device is going to listen to this message so I'm going to send it to a particular device and I'm gonna send it to the Raspberry Pi and then I can send whatever message I want but the message I want to send in this case is going to be no I was gonna solve it wasn't I so let's solve the Raspberry Pi puzzle so an operator could do this remotely when I send that message you'll see the Raspberry Pi gets solved maybe instead of solving I want to reset it instead so instead of that I'll keep the same topic so any of this device is listening but instead the message will be reset and that will turn it off now the great thing about mqtt is that you can set as many topics as you want and when the server publish is a message to a topic it doesn't know or care which clients are listening if they're any clients listening it will just send that information out and similarly the clients can listen to topics without caring whether anything is publishing there at the time so it's very kind of ad hoc way of setting up an information network so all of these devices here I've got listening to their own unique topic at the moment so let's say instead of the raspberry pie I want to tell the Arduino to reset instead I would simply send the topic of that message now the Arduino is already reset actually so let's set it to solve instead so I change that message there too so you see the Arduino get solved now let's tell the way most to be solved Mountain comes on let's set the raspberry pi we solved you'll get the hang of this by now hopefully how this is working there we go so I've got all three of my puzzles solved now a client can listen to as many different topics as it wants as well so in addition to each of these listening to their own unique topic I've also got them listening to a topic which is simply to device all like that so now I'm going to reset all the devices by sending a reset message to the to device or topic and they all go off and then finally let's just demonstrate that again by setting them all to solve and it will come back on so in that example I was publishing messages from the command line application on my PC which is the same machine as I'm actually running the mqtt broker service itself but that doesn't need to be the case you can publish messages from any device that's on your network just as you can subscribe to messages in different topics as well so as an example of a different controller application here I've got a thing running on my Android our phone and you can see I've got six buttons set up there and they correspond to the solve and reset messages that are just demonstrated for these three devices so if I press this red button at the bottom here that sends the reset command to the topic that this Arduino is listening to and this green button will send the solve command so what I've got there is a toggle which will reset and solve this device here which you can see by the relay turning on and off likewise the button to the middle line here correspond to the we loss which I can turn off or I can force to be solved and I've got the one at the top here that will reset or solve the raspberry pi and you can create as many different sort of commands as you want here all they're doing is behind the scenes that corresponds to an N qtt message that is delivered to a particular topic so I could have another line for reset all or solve all or maybe reset the devices in a particular room or solve the devices that were all linked in some way to part of a meta puzzle or something like that and I've got myself a handheld controller that I can carry around and activate the puzzles as I want so let me show you how I've got these devices wired up so like I said I've got three different devices running the three different puzzles and I come and did that deliberately just to show you a bit of variety say the simplest one in a sense is the Raspberry Pi so the Raspberry Pi has an onboard Ethernet socket anyway so I'm using that to connect to my server and then on this side I'm using the clip io pins I've got a 5 volt relay module which is plugged in to 5 volt to ground and then to this socket here which is the QIO 14 on my Raspberry Pi and then I've got a single read switch and that's going between ground and another one of the coop i/o pins I'm using to Goa for for that so as in an Arduino I've got that set that input pin set as a with a pull-up resistor activated say when a magnet comes close to it it forces the metal strips to make a contact that connects it to ground at which point that you pipe in there then has a low signal reading to it and when that happens it activates the relay so nice and simple and like I said it's just got a standard rj45 cable connecting it far Ethernet to my server then I'll show the Arduino next so the Arduino does not have a built-in Ethernet port by default but you can get lots of different shield available for it so I'm using an Ethernet shield which kind of just stacks on top of the pins of the original Arduino Uno and you can still access all of the underlying input and output pins through the shield as well say I stack the shield on top and then I've got an Ethernet cable plugged into the socket here and then again I've got a 5 volt relay module going to 5 volt ground and to a single signal pin which I'm using a 1 for this it doesn't need to be an analog pick and pin but that's just on the right side of the board for me to use and then for the sensor part of this I've got the light dependent resistor here and I've got that and a regular resistor so I'm creating a voltage divider here supplying 5 volts in on this side going across the wide Opena resistor and then the normal resistor to ground on this side and I'm taking a signal reading in the middle via this green wire here that's going to be a 0 pin so I'm taking an analog reading of the resistance at that point between the two resistors and that's what I'm using to decide whether the large inner resistor has been covered up or not that's exactly the same technique I used in the to cover up puzzle which I've shown in the past say I won't go in more detail there and then the third puzzle is using this thing called a we mas now I haven't used these before I've only recently discovered them to be honest and actually they're pretty cool so it's a board very similar to an Arduino in that it has a number of inputs and outputs and you can control various devices you know exactly the same as an Arduino you can program it using the same Arduino IDE as well the difference from it compared to a regular doing it is that it actually comes with onboard Wi-Fi and they're ever so cheap these units they're well under five pounds I think this was this cost me so have inbuilt Wi-Fi with that is brilliant they use a slightly different tip whereas the Arduino spam pon eighty tiny or an 80 mega chip the we mas is powered by an esp8266 chip which actually is the same tip that you'd have in a Wi-Fi field on an Arduino anyway so it's kind of like a cross between the Wi-Fi Arduino field and a nod in itself but they're really they're really great devices actually the only limiting factor I've found is that you don't have analog inputs or the Sun either very hard to access analog inputs so they're limited in that respect but in terms of other respects they're they're pretty great so once again I've got five relay plugged into five volt ground and a signal line which is going around here to pin T and then I've got my RFID reader which is plugged in using an SPI interface so that's going to the Molesey and the miso lines and the clock and the Select line so again that's exactly the same setup as I showed in my one of my earliest puzzles that I showed how to do using RFID project placement so the hardware side of things is actually very straightforward for these three devices say the MQTT broker service I'm using is something called Mesquite a from Eclipse and you can download it from mesquite org it's like mosquito with two T's I don't know if mosquito mosquito whatever but anyway it's an open-source and it's very good and it runs on lots of different operating systems so if you go to the download site here and I'm using I've got a Windows machine so I'm using the windows binary installation but you can also build for Mac Linux and various other systems here as well you see there's lots of different builds available if you do use the Windows installation as I do once you download and installed it you might find that there's a couple of dependencies required as well these are pretty easy to get so here's the setup screen once you download and try to installed it and here you'll be told just two additional things to install so it's open SSL and pthreads and you simply click these links and download them to the installation directory exactly as instructed and then build will carry on as normal ok so here's what the directory looks like when mosquitoes finished being installed and this three programs which I'm going to sort of concentrate on so the first one is this mosquito executable itself and that is the main MQTT broker service that we're going to run now you can either double click it and load it straight from here you can run it from command line which is useful if you want to pass additional parameters into it for example there is actually also service that you can run like a Windows service which will run in the background and you can start automatically every time your your server starts but just for the purposes of demonstrating here obviously simply double click that file here and I'll get this blank command window here which I'll just drag into the corner and that's just going to show the fact that the MQTT broker is running sort of in the background haven't got anything to display at the moment but so long as I keep that window open that broker service is going to be running there okay now these are the other two programs who are interested in this is a command-line application for publishing messages and this is a command-line application for subscribing to messages today I'm going to load up a new command prompt I felt our led up to new command prompts which I'm going to use to demonstrate how they can be used to test our server graph so in this window the first thing I'm going to do is to subscribe to a topic so I'm going to use the sub application and then I'm going to pass in the T parameter because the name of the topic that I want to subscribe to so I just subscribed a topic to start with hit enter and leave that window running and now in this command prompt what I'm going to do is use the partner application the hub command to publish the message I'm going to publish it into topic which is what the other one is listening to and the message I'm going to send is let's simply send a hello world message when I hit enter descend from the published application you'll see that this window here which was subscribe to that topic simply prints the message that was received now if I were to send so let's just send a another message so I said hello again and you'll see that it decades that there as well now suppose I was to publish instead to a different topic notice that this command still gets sent I don't get any errors or anything like that it's published it to a different topic but because this client here is not listening to that topic nothing happens so the way that I normally set my topics up and I sort of briefly touched on this before so I have each device listens to a topic that is just intended for mess is to that device but then you can also set up other as many different topics as you want maybe to control devices in each room or four devices that all want to listen to a particular type of message to example if I just cancel the subscription for a moment in this window and I start a new subscription instead so I'm still listening to topic but let's say I also want to listen to another topic so that's cool topic two and you can also put wildcards in it as well so you can put topic and then a forward stroke and then let's put a hash symbol and what that's going to say is that's going to listen to topic and any subtopics of topic so here back in my publisher window again I can now publish to topic stroke subtopic for example and the subscription appears in this window here because it's listening to the subtopics it's listen to every subtopic in fact if I publish to subtopic to still get hello again if I get subtopic to stroke sub subtopic stroke now those will get received but if I publish to a subtopic of topic two for example what we'll find is that let's change this message to a different message this one does not get received because although I subscribe to the highest level of topic tea I haven't subscribed to subtopics of it so it's a hierarchical structure and you can kind of set this up how you want to best kind of match the way that your devices are configured you know the way that you want to control them this hash symbol is a kind of a capital wildcard you can also use a plus symbol which is a one level wildcard only and through using a combination of them and also subscribe to too many topics you can get quite a good fine degree of control over which messages are received and acted upon by which plot so that's tested that the server is running okay and that we can send a receive messages to it so now let me show you the code that's running on the devices so I'll start off with the Arduino which is got the Ethernet shield attached to it and it's running the cover up puzzle so this is where you have to put your hand over the light dependent resistor to activate the puzzle now this is a puzzle that I've shown in the past this is a new and improved version with added support for MQTT but the vast majority of the main puzzle file has remained unchanged so I'm not really going to go through that in detail because you can look at one of my earlier videos we'll explain that but all I do is it show the bits that I've changed so in this file all this is the same or this is the same in the on solve callbacks is the function that gets called when the puzzle is solved I've added one new line here which calls this public command and just sends a message to the host to say that the puzzle has been solved and I've also got a matching message here in the on unsolved command even that says that it become unsolved and I've got one in the reset of course as well under 7 it's been reset so all these are just simple updates to inform the host of the current state of the puzzle and then in the setup function that's called when the program first runs I've got two new bits here obviously the mqtt service itself depends first on having connection to the network so I've got a function here that sets up first of all the Ethernet connection and then when that's done I just call some additional functions to set up the enqueue TT service itself and then from then on I'm pretty sure our nodes one more bit here in the loop function just at the end of the loop there's a few additional commands need to be doing to receive any messages that might be queued up or to send messages as well so just call one loop command in there so that is all the changes to the main puzzle file um in terms of where those functions are actually defined so what does this publish d what does Ethernet setup do what does mqtt setup do I've put them in a separate file within the project directory so as much as possible you should try to sort of keep your code files you know easily reusable so all this file here is is just specific to the puzzle and then here's another file that's just placed in the same directory that could be reused across any puzzles which I wanted to convert to use this Ethernet connection MQTT so what does this got in it okay well we start off with some includes as always so SPI and Ethernet are both inbuilt Arduino libraries I'm using one third-party library because this thing called pops-up client and you can access it from this github link here and that's going to provide the publishing and the subscription services that we'll use say download it from here and then you can install it into your Arduino installation by choosing this option here to add I agree that you install and we started with defining some constants required to make the Ethernet connection say we need to know the MAC address the unique hardware address of the network device and now this might be printed on a sticker that's attached to your Ethernet shield if not you can make a valid or you might well be able to use this value here it needs to be unique is the is the the main thing to make sure it doesn't interfere with other devices that might be on your network now here I'm assigning a IP address to use in case DHCP doesn't assign a valid IP address now whenever you're setting up anything that's kind of networked I highly encourage you to use static IP addressing say DHCP you know when you just turn your laptop on in a Wi-Fi cafe or something like that and you you know you get a different IP address you just get assigned one you don't normally need to worry about it and that's pretty convenient but when you're starting to kind of write code that is going to be you know hard-coded what the IP address of a device that's gonna be connected to a particular server or something like that it's really frustrating if that kind of changes over time as new IP addresses are assigned so I'd really recommend you to you know your Rooter or your your modem might allow you to assign a particular address to give out via DHCP anyway or you can assign it you know in code but but I highly recommend you having a static IP address for each of your devices you will find it makes it much easier later on when you actually come to try to debug any errors and things like that as well here I'm declaring this is the IP address of my host machine my PC that's running the the mosquito service that I just Kady and this is another bit that needs to be unique so every device on your network needs to have a unique ID for communicate with the mqtt breaker so I'll just name this Arduino because I've got three different devices so I'm just calling them what they are you might want to append a random string onto the end of that to make sure it's unique if you want or just make sure that you give each of your puzzles a unique name that will help identify them on the network later on okay we set up some Global's so I'm using the Ethernet client here that came with that Ethernet library declared at the top and I'm creating an instance of the pub/sub client based on that Ethernet client I'm just going to hold a variable of when the last time a message was published to the broker and here I've got some sort of buffers which I'm just gonna reuse every time I write a new message I'm gonna first copy it into this variable here and to the topic I'm gonna send here because Arduinos and and other sort of small embedded devices don't have a whole lot of memory available on them you don't really want to be assigning memory dynamically time you send a message so what I just do is declare these once and then I'll reuse those same variables for every message let's send this is a post count here so often it's quite useful to publish a little kind of pulse or heartbeat message regularly from your device as well just to inform the network that it's still on and still you know connected so that's just a counter which increments to say how many times it said hello I'm still here and still turned on and then if you compare that to the pulses received at the server end if any have been dropped you might be an indication that you need to kind of look up your networks if there's some issues there okay so this next second here this is the callback that is fired whenever a message is received on any of the topics that this device is listen to so the callback is given the topic in which a message was published it's given what's called the payload so that basically the content of the message itself and it's told how long the message is now your notice that payload is delivered as a pointed to a byte so to make it just a bit friendlier because we're passing text rings as command the first thing I'm going to do is copy that into a character array so we're going to use that message remember I said we were going to reuse a message buffer at the top here which is here so we'll copy payload into that and we'll put a null terminator on the end which is how you define a valid string we'll just print out some debug information to say that message has been received and then this is where we're going to define these are all the different commands that this device will react to so we're using a string comparison function having copied the payload into this message we can now do string comparisons it's not the only way to do this you could decide to pass all your commands around as just integer you know value so if you pass the commander you know 1000 that would be a reset 1001 is solved 1002 is a you know report state of something which would save you a little bit of memory but I think for the sake of making your code more readable I'd much rather actually see named values that you're comparing to here I just find that easier to do so if we compare the message if we compare the payload received to the string solve and if strip comp returns zero that means that they match that means it's basically there's no differences between the message and solved so in that case this is the string was received we'll call the on solve callback and that's exactly the same on solve callback as is going to be called if the puzzle is solved normally it's just this one here that was already in the puzzle from the first time we wrote it likewise if we compare the message received to the string reset and we find that they're exactly the same there's no differences we'll call the on reset callback instead which is also defined in this file here so what we're saying is have a look when a message is received if it is one of these will act just the same as if the the message had been if the puzzle had been solved or reset normally okay this next section here so this is specific to using the Ethernet shield or an Ethernet connection so well and it only just creates a network connect in the first place so we'll start the serial link if there hasn't already been a serial link set up which there might have been from early on in the setup procedure we'll just print a little bit of debug information and then we'll first try to connect just using the MAC address and if that fails then we'll begin we'll try to begin an Ethernet connection instead using the MAC address and the IP that we gave at the top say this is if you want I said not to use DHCP but but this is I'm assuming that your Rooter is assigning a fixed IP address based on the based on the MAC address there if it doesn't then this is kind of telling the device to assign itself a static IP address that's kind of you know depends on how your network is it's configured as to exactly hub now you want that set up we'll just wait to cover seconds I found that this is helpful even when this returns and kind of says that the network is ready sometimes I find that you just helps have a little bit of delay before you try to start using it so it's just a two-second delay and then we'll say that we're connected and we'll also just use this Ethernet or local IP we'll dump that to the serial outputs just so that we can get some kind of useful debugging information this is the MQTT setup so this is called immediately after ethernet setup so in the setup front at the beginning of the script we'll connect the network we'll set the server to mqtt server IP and we'll use that port 183 that's the default port number for the mqtt service and we'll assign the callback that we've just looked through so here was mqtt callback just up here okay and then the final or the nearly final function the mqtt loop so this was called from the main program loop itself it was just this bit that was added here so on every iteration through the program loop first of all we make sure that we're still connected to the MQTT client if we're not what we'll try and do is attempt a reconnection say we'll connect using the device ID say that remember I said that you needed a unique device ID this one's just called Arduino will try to collect the client using that and if we do a jeev a successful connection we're going to publish something to at the topic called to host and then we're going to publish to a subtopic of that which is this device ID so this script here is going to publish a message to two hosts and then the subtopic is going to be called Arduino say just from looking at the topic in which this message is published I actually know where it's come from now if you wanted to you could write the the device ID the Arduino in the message payload itself instead of adding it to a topic there you could put it as part of the message it doesn't really matter I find that the the structured hierarchy of the topics like this is quite a good way to pass information between clients but if you want to do you could just put it in the to host topic and include the device ID somewhere else but all it says is that this device has connected to the network and publish on topic there then we'll subscribe to the topics that we're interested in and there's two topics that were interested in the first one are messages meant only for this device and so they're going to go to a topic called two device and then the ID of this device particularly and we'll also subscribe to messages that are meant to every device so we'll subscribe to the all subtopic of the two device topic finally rule I'm sorry if this so this else here corresponds to the if here so if we couldn't connect the MQTT client or this time round we'll just will dump the failure message here we'll wait five seconds and then we'll try again so this is just kind of a reconnect loop just to make sure if anything goes wrong on the network will automatically try to reconnect and then finally at the bottom in fact I've scrolled slightly off the screen so let me just trying them bring that back up will see this public message here so this is what was being called in my on soul and my on reset and my on unsolved messages so this publish here cover up puzzle reset that calls this function here and what this simply says is it copies to the topic variable again we're sending it to the host and again we're copying in this device is ID into the subtopic that we're polishing too so the host knows where this message has come from and then what we're going to do is we're just going to publish the message that was sent so either that the puzzle was reset that it was solved etc and we're going to publish it to the host and that's the whole of the script so we've got very few changes to the main puzzle script this is nearly identical to the very first one I published with the exception of a few additions here a few additions to the end of just a set of Ethernet and the MQTT and then at the end of the loop function we just call the extra loop required for the mqtt functions and then this additional file contains mostly simple stuff to to set up the network itself and the actual process of acting or messages that are received that are subscribed here is mainly done in this bit here and it's just a case of comparing to the string and calling a function of what you want to happen and for subscribing oh and for publishing sorry it is just this event at the bottom here that's about it so this is the script that's running on the we most device and as you'll see I've structured it and almost exactly the same as I had before so in this code file here this is basically the same as the code I showed for my RFID object placement puzzle this is all identical the only difference is I'm only using one rfid reader in this example I mean I could put four in there or however many you want really but what I'm trying to demonstrate really is the MQTT bit rather than the RFID bit so does the one there this is all identical this is all identical again just as in the other example I'm calling extra setup functions at the end of the setup loop but rather than Ethernet setup this time I'm calling Wi-Fi setup because the we must device obviously uses wireless connecting rather than the wired connection and everything the same this is all the same as in the original RFID puzzle this is all the same once again at the end of the loop function I call mqtt loop in the sole function here I publish a message and the same in on solve and the same in reset say this is exactly the same process as I did before just added a few new lines into the main puzzle sketch but then alongside it I have a new file which is in the the same directory of the project say for this example here now this is almost identical if you're using a Windows like me or if you're using an Arduino with a Wi-Fi shield it's almost identical the only difference is this very first library here you'll see I'm using the esp8266 Wi-Fi library if you're just using an Arduino Wi-Fi I think I'm right in saying you simply replace that just with Wi-Fi instead the library functions themselves are exactly the same match together so you don't need to make any other changes so how does this work well this one is very similar to the the Ethernet library it's just there's a few minor differences because of the Wi-Fi connection so rather than defining a MAC address at the beginning here what I'm defining is the SSID and password required to access my Wi-Fi connection don't worry I've since changed you since creating these videos so I'm not worried about anyone jumping on getting free Wi-Fi from me again I define the IP address of my PC that's running the MQTT broker service and a defining unique ID remember my last was called Arduino this one's called we vas t1 because that's the name of the board that's running it and this is all very similar to before up and having an Ethernet client I have a Wi-Fi client here and then when I create my instance of the pub/sub client I'm pointing it to the Wi-Fi client I've just created rather than pointing it to the Ethernet client but other than that that's the same these are all exactly the same the mqtt callback is also exactly the same as with the ethernet example so I'm just comparing the message to either solve or reset obviously you can add as many other commands as you want here to respond to but I'm just using a solvent reset for now this bits slightly different because the nature of setting up a Wi-Fi connection is slightly different to a wired Ethernet connection so again we'll start the serial connection if there isn't one already we call Wi-Fi begin and we pass in the ID and the password required to access the network if you're trying to connect to an open network with no authentication you can just delete these and connect like that and then we'll wait a little bit to wait for the Wi-Fi connection to become active we'll just enter a little loop here which will just print some dots onto the screen while we're waiting to get that status code to let us know that the Wi-Fi has been connected again once it has been connected we'll just put two second delay I find that helps just stabilize the harbor a little bit before we try to use it and then we'll print the local IP address that's been assigned to us MQTT setup which is called immediately after that has remained exactly the same as in the ethernet example and the MQTT loop is also exactly the same as it was before because the MQTT client we're using exactly the same one as in the ethernet example the only difference is that right at the top here we were creating the instead of that client based on a Wi-Fi client rather than based on an Ethernet client but other than that it's exactly the same and then at the end of the file here we just defined that publish message say you know 90% of this file if you're using a Wi-Fi connection is the same as if you were using an Ethernet connection the only differences slightly is in the initial setup of how the network is connected so that's the the we must code and now let me show you the raspberry pi okay so I haven't actually shown any raspberry poly code before now I don't think I think it's the first time I've done it so I'm using the Nano editor on the Raspberry Pi itself so connected to my paya putty and then I'm using the Nano editor to actually edit the file on the pie itself say it's basically a similar structure to how you've seen in the Arduino examples though so we start off by importing any libraries we use I'm using something called the Paco or pay hoe client which you can install using pip if you're familiar installing packages on your Raspberry Pi and you can get it from there I'm also using the built-in GPIO library to access the pins that control the relay and I'm importing the inbuilt time library as well I'm gonna use that because I'll sleep I'll make the application pools for a little bit using sleep so just as with the Arduino we begin with the setup we'll set the mode of how we're gonna access the GPIO pins I will also just turn warnings off will configure the pins so 14 is going to be the pin that's connected to the relay and then pin 4 is the one that's going to read whether the read switch has been activated or not and then I've got so unlike in the Arduino sketch because I haven't shown this file before I've actually merged both the puzzle control and the network and MQTT control sort of into one file in this example so up to now we've been looking at setting up the puzzle now we're looking at setting up the MQTT bit so here's the call back when the MQTT clamp becomes connected we'll print a message to the screen and then we'll we use this subscribe method here well subscribe to the device all Channel and we'll also subscribe to the the topic just intended for this device so the the all topic and also the device specific topic on message this is going to be called when any message is published to a topic that we subscribe to so when it's published to either that all topic or the raspberry pie topic and exactly this with the audrina again what we're going to do is compare the payload of the message to the string solve all to the string reset and depending on which one of those it is we're just going to set the state of the puzzle into one or two states 1 or 0 here we have another callback this is just this is kind of just tidying up a little bit so if the client becomes disconnected we'll stop calling the the MQTT loop that's all that says a few global variables just to keep track of the puzzle itself so what was the input in the last frame what was the state before the current one and what is the current state as well and we'll just create as all as integers just get tracker ok so here is where we set up the client itself so this is the the paho client so we'll create a new instance of the mqtt client we'll assign those three callbacks have we actually declared above and then we use the connect method here's the IP address of my server that's running the broker and the port number and then we'll use loop start to start the update loop so this runs in a separate thread in the background and will just take charge of delivering messages and informing the application if any messages have been published you know new topics were being subscribed and then we'll just set up a main program loop here say we'll carry on running indefinitely through this loop will read the input on pin four so that's the pin that has the read switch attached to it if we were previously reading a high signal on that pin but now we're reading a low signal that means that a magnet has been held near the read switch this frame and there wasn't there before so we'll use that as our signal to toggle the state of the program between 1 and 0 we'll update our previous stalled input and if this state is different from the one that we knew about before so we know that the puzzle has changed if it's now become unsolved we'll use the publish method to tell the host and well again we'll send our ID so the idea of this puzzle will send us a subtopic to the house so it knows where it's coming from and we just know that the read switch deposit has been reset and we'll also activate the relay obviously and all the alternative is the state has now won and it wasn't before that means that the puzzles been solved so we'll send a message to say the puzzles been solved and send a low signal to the relay fine they will update the previous state sleep for you know tenth of a second and then we'll just keep on looping round around that loop and the final program I need to show you how to set up was the Android application which I demonstrated earlier so this is a program called MQTT - which you can get on the Google Play Store I'm sure there are equivalent programs that you can get for iPads and iPhones as well but I use Android so this is what I use it's really straightforward you simply download it once you've installed it and follow the instructions you first set up a connection to your broker so this screen here you say you can actually have multiple MQTT servers that you connect to but you can just define only one if you want and then having created a connection to the correct broker you can then define any number of messages and the message will be sent to a particular topic so this one here you can see door ford stroke lock and then you can get the message itself the payload here is on its one and off is zero but in the examples that I've been sort of demonstrating and on would be solve and off will be reset so you can simply type any text string or number you want in the payload of the message to send and you set the topic to be sent to the host and then you then you end with just the buttons on your screen that you can press to send that message so it's it's very simple to use and set up that one and there's instructions online as well so that was a very brief introduction to mqtt it's a great really simple lightweight system that you can use to send all sorts of sensor information and simple instructions on an output network between devices it's really really useful for kind of escape room types and reas if you've got any questions about anything I've said please feel free to ask in the comments and I'll include some more notes in the description there about more documentation and things that you might want to refer to I mean obviously the next stage if you wanted to take this further easy to put a slightly nicer GUI front end maybe on the server application so rather than writing command lines you can get like a nice front end that showed you a graphical look at what the stage beat two Possible's at the moment I might do that in a future video if people are interested but I hope you enjoyed watching this video and thanks very much for watching
Info
Channel: Playful Technology
Views: 113,846
Rating: undefined out of 5
Keywords: Technology, Electronics, Networking, Education
Id: VSwu-ZYiTxg
Channel Id: undefined
Length: 51min 12sec (3072 seconds)
Published: Mon Apr 02 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.