Monitor Room Temperature Remotely with Arduino & MQTT

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there my name is Gary Sims and this is Gary explains now home automation and the smart home are of course popular topics today and there are many many commercial Solutions but you can also roll your own in this video what I want to talk about is how you can monitor the temperature in your home and have that information sent over to your smartphone even when you are away from your house so if you want to find out more please let me explain now before we dive into the detail just let me remind you you can follow me on Twitter at Gary explains and I also have a monthly newsletter go to garrettspace.com type email address no spam but you will get the newsletter okay let's crack on okay so what we're trying to achieve is that somewhere at home I have a micro controller with some kind of temperature sensor on it and I want to get that data on my phone and I want that data to be available when I'm in the house and when I'm outside the house away in another place so I can see the temperature and the humidity back at home even if I'm not physically there so that's the goal to have information on my smartphone telling me about the temperature and the humidity in my house now to achieve that we're going to have a network enabled microcontroller that means that one's got Wi-Fi basically we're going to need to add a temperature sensor if the board you're using doesn't already have a temperature sensor on it and we're going to use a Arduino for programming it because that is one of the most popular ways and it means we can use a variety of boards whichever one you want to use that's what you can use for this setup and the other end we want the data to come in on a smartphone it doesn't matter whether it is Android or iOS now the glue to this whole thing how do we get this thing here to talk to this thing here is mqtt that sits up in the cloud and basically what happens is the microcontroller pushes data into the mqtt server and then your smartphone talks to it and says is there any data for me and then please please give me that data now I've got a whole much more in-depth video about mqtt with the Raspberry Pi and Arduino talking about subscribers and topics and all the things that you need to know to get that working so if you're not familiar with mqtt then I do highly recommend that video to bring you up to speed so as I said if you don't have a temperature sensor on your board then you're going to need to add one now I've got the Ground studio carbon V3 which is an esp32 development board and I do talk about that here in a video I've got on this channel the advantage of the Ground studio board is they are manufactured here in Europe and not in China however of course the chip still comes from China because the expressive systems esp32 of course is a is a Chinese chip now a Ground studio also sell a bunch of different sensors and one of them is the ah t21 temperature sensor in fact it's a temperature and humidity sensor and the point about it is is that you can be connected very easily to the Ground studio carbon V3 using one of these quick or stemmer QT cables now quick I squared C that's where the iic comes from it is from spark fans stem acutis from age group they're basically the same things a very simple plug here that plugs into these connectors here and those are already wired up to the I squared C uh part on the board so without having to have a breadboard without having to do any soldering without having to do any fancy stuff you can just connect the temperature sensor over this ribbon cable and you've got it up and running that's what I'm going to use now for the mqtt I'm going to use the mosquito test server so it's a publicly available Eclipse mosquito mqtt server broker you are free to use it for any application but of course it's there for the community so do not abuse it and obviously don't rely on it for anything too important because it can get rebooted it can disappear it could be offline for a few hours the server is provided as a service for the community do to do testing but it's extremely useful for testing the eclipse server on their part as well which means they'll often be running unreleased or experimental code and it may not be as stable as you want so lots of caveats there but from my experience it's pretty good you can just use this server and especially for like something like this where you're sending this day small amounts of data this works really well if you wanted something more serious you'd have to make sure you got yourself an mqtt server using some kind of paid plan to make sure it's always there with support and so on uh that's up to you now on the smartphone side of course I'm using Android but there are a whole plethora of different kinds of apps that are able to create mqtt panels or dashboards now I'm using this one the iot mqtt panel I'm not affiliated with this or guy at all I don't have anything to do with this it's just I've found this one and I've used it a lot and it works really well as you can see from the little screenshots here you build up these little these panels these little dashboards and you can just kind of say well this is a temperature this is humidity this is something else it's something else and you can have graphs and everything and you can just build your own little dashboard inside of this app and then it just shows you what that data is that you're sending and it's available there are other ones available and there are other ones available for IOS as well but if you're using some kind of mqtt panel or dashboard app on your smartphone then you are golden okay so finally a quick overview before we dive into the code we've got a temperature and humidity sensor which is talking over I squared C to a control microcontroller board we're going to program this with a Arduino to push data up to test.mosquito Dot org which is a free test server and then we're going to use some kind of mqtt panel or dashboard app on our smartphone to interrogate the mosquito server and get our data out and then get it displayed in a nice temperature graph or whatever it is that we want to do according to the panel software on here okay so the next step now is to dive into the software the Arduino code for this microcontroller as I said this is the esp32 but it'll work with just about anything that's supported by Arduino and with a network connectivity okay so here we are inside of the Arduino IDE and because we were talking to an mqtt server you need to have the Arduino mqtt client which is an official Arduino Library installed and then you'll be able to get all that stuff about the protocol and how that works it will automatically be there you just need to say send this message and it will know how to send the message so make sure that's installed and then here in the actual code first of all you need to include the Arduino mqtt client.h and wi-fi.h because obviously you're sending things over the network up into the cloud the next bit of code here is just to make sure that you've got your SSID and your password set up and then here you need to actually Define the Wi-Fi client and the mqtt client you needed to find the Wi-Fi client first because it is a parameter to the creation of the mqtt client then you can set up here some constants that we're going to use so we know we're using test.mosquito Dot org 1883 is the port number for mqtt and I've got three topics defined here these are just random numbers because obviously it's a public server so you'd have to pick something you can't just pick you know temperature because I'm sure that's already being used so I've just picked some random digits here and this first one will be a text log and then these next two are look the same but in fact one ends in t for temperature and one end in H for humidity so we're going to end in sending things to three topics uh in the end and a moment we're going to be initializing the aht sensor that I showed you previously so you're going to need to install the Adafruit aht x0 library and that again knows how to speak over I squared C to get the humidity and the temperature data out of that so make sure you install that inside of the library manager there then since that's now installed we need to include the Adafruit ahtx0.h file and then we need to create an aht object which is defined here as that Adafruit thing and we can access that now like get the temperature get the humidity through that object the next little bit of code here is just to fiddle with the um the neopixel that's built into this carbon V3 board so it's not just a simple red LED we've got all the different colors there and so this is the way you just access the neopixel code here on the uh on the Arduino and the next two functions in fact color wipe and wheel are just about colors to do with to do with the the RGB pixel so for example this one you pass it in a number from zero to two five five and it gives you a red green blue so basically you can cycle through a whole range of colors uh by just giving it a value zero is two five five and it kind of gives you the red green blue to develop the whole Spectrum in those three colors so that's just color functions now we get down to the interesting stuff now we're here in setups the first thing we do is we just initialize the neopixel so that is done before the serial because if worst case scenario you can know you can turn on an LED and kind of do some debugging so we start by initializing that that's the same as a standard code for doing the neopixel then we initialize the serial as you would do often in any Arduino script and then we say okay we're going to attempt to connect to the Wi-Fi and you use Wi-Fi begin to do that this is all standard code for the Arduino for doing Wi-Fi and then here it basically Waits printing out dots on the serial Port until it is connected once it's connected it prints out some information about the connection including the IP address that has been assigned to the board then we look here to see if we can find the aht uh sensor and so you call this aht.begin remember we defined that object earlier on and if it can find it it finds it if it can't it goes into an infinite Loops I'm sorry I couldn't find that and you can't do anything else after that check your connections check whatever but assuming it finds it and you're okay the final thing so we've connected to the Wi-Fi we've connected to the temperature sensor and now we actually need to connect to the mqtt server so we just print out that's what we're attempting to do it then attempts to run the connect here and then if that succeeds then it says you are connected to the mqtt server and then finally we change the color here red green red green blue red green here to show that actually everything's going well so when the light go green and the light goes green it means that everything has connected up absolutely fine so once initialization has happened so that's connecting to Wi-Fi connecting to the sensor and connecting to mqtt now we're ready to actually start doing some processing of course that happens in the loop function so the first thing we do is we try to find out the temperature and the humidity by calling a HD get event and the new part it gives Returns the humidity and the temperature in these two variables and then we just print them out so the first thing you want to make sure you can actually get the temperature and the ability print them out on the serial port and that happens every time the loop goes around now that we've got them we actually want to send three messages to three different topics to our mqtt so the first one is going to be just to that normal topic just topic and that's going to be a text log so basically you do begin message and end message and then you put some stuff in it so it's mqtt client print print and what we're doing I'm just building up a message here that basically says temperature and humidity that's a text log and then when you call end message that is actually then sent up to the cloud and then we do exactly the same thing now for just the temperature but we're not doing it in text we're actually sending the number so you just do print here temp dot temperature and notice we're sending it to the topic with a T at the end so it's slightly different to the one above and that will just come into a topic on the mqtt server that's just sending the temperature and then we do exactly the same thing for the humidity so every time we go around the loop we do three uh send three message we read the temperature and the humidity from the sensor and then we send a text version and then a version just with the temperature and then a version just with the humidity and then here at the end we just do a little Loop that Cycles through all the different colors using that wheel and color white function this parameter here is a delay so when you do 255 by 235 roughly one minute so this little Loop here which Cycles through the colors takes about one minute and then because we loop back up to the top again and we read the new temperature and the new humidity and send it all again so this will just go around in a loop every minute sending the temperature and the humidity now maybe you think that's too frequently maybe like do it every 10 minutes you can do it however you want but the important thing is it's being sent up to the mqtt server and you can connect now to the mqtt server from your Android smartphone okay so here we are inside of the mqtt iot panel software on Android and basically just to show you how it works you can see it's already received some humidity data here's the text version that came through so you can see that it's logged there and that's just the string that I send through from the uh from the Arduino there from the esp32 development board now we're going to add the temperature gauge just to show you how these kind of programs work so you can add a new panel you click down there and we can scroll all the different types here look progress bars and graphs and everything we're gonna go with a gauge and then we're going to give it the topic now I've got the topic cut and paste already remember this is exactly the same as a topic that we found in our Arduino source code it's got the T at the end which means it's just receiving the temperature you can give it a scale so let's say hopefully it never gets down to zero inside my house outside would be different but inside my house and let's go with 40 degrees maximum that you might get in the summer and that gives you a color range here that's what beta that does and so you can hit create okay so there's the temperature we can actually edit its position so let's just move it up right to the very top there like that now we have to wait for the next um the next data to be pushed out from the Arduino which should come very quickly and then when we see it that temperature gauge will move and there you go so we can see the new log messages come in there and we can see the temperature now 21.22 degrees and the gauge is showing that between our minimum and our maximum that we set also the humidity data was sent annuals you can you can change this however you want obviously you can send them all stuff if you've got more sensors but here you've basically got a very quick way of monitoring the temperature in your house from your Android smartphone okay that's it my name is Gary Sims this is Gary Express I really hope you enjoyed this video if you did please do give it a thumbs up and if you like these kind of videos don't forget to subscribe to the channel okay that's it I'll see in the next one foreign [Music]
Info
Channel: Gary Explains
Views: 20,648
Rating: undefined out of 5
Keywords: Gary Explains, Tech, Explanation, Tutorial, Arduino, ESP32, MQTT, remote temperature monitoring, remote temperature monitor, How can I monitor my temperature remotely?, temperature sensor, AHT21, Integrated temperature and humidity sensor, humidity sensor, Wireless Temperature Sensors for Remote Monitoring, Monitor Temperature Remotely, Remote Temperature Monitoring System, Home Temperature Monitor, Android, iOS
Id: P8DnANWusrM
Channel Id: undefined
Length: 15min 22sec (922 seconds)
Published: Fri Oct 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.