arduino mqtt library
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 publishsubscribe 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 publishsubscribe 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 onoff 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.
arduino mqtt library Video
arduino mqtt library news
Posted on Thursday March 21, 2019Library Makes ESP Over The Air Updates Easy Hackaday … Continue Reading »
Posted on Thursday February 07, 2019Arduino Enters The Cloud Hackaday … Continue Reading »
Posted on Thursday February 14, 2019Wemos Lolin = ESP32 + OLED on one board Elektor … Continue Reading »
arduino mqtt library Social
🗓️ Thursday, October 10th
🕓 4pm CET