 So hello everyone. Hope you are having a good day and everyone is safe at the home. So my name is Harsh Mital and I think he's already given me a brief introduction about myself. So I'm currently working as an Embedded Software Engineer at Asheera Aerospace. So today I'm here to present in front of you how you can build your very own home automation system using very minimum setup and with lot of ease. And also do not worry in case if you do not have the hardware with you right now or so you can still continue along with the talk and you'll be able to implement each and everything. I hope you enjoy this talk because this is something you know you all should definitely try and it will make your life a lot more easier. I'll be providing all the source code. Please refer to this link for complete source code that I'll be discussing today. So let's start with the introduction. You must be already be aware of what exactly is home automation. It is nothing but you know automating all your different household activities using different sensors to collect and analyze them to make smart actions and helps you monitor everything. So what are the key benefits of having one? First is improved security. So you can have you know different intrusion detection sensors like cameras, LiDARs etc and integrate them together over your local network. So that makes it really difficult for someone to break into your house. Second, better care to your family. So you can have features like you know running your AC lights and other appliances remotely using a single GUI over multiple devices. Third is energy saving. You can keep track of all the appliances that are running and you know you can automatically turn off the ones that you are not using right now. Also you can keep track of energy consumption using different current and voltage sensors and apply machine learning for better analysis. Also it will improve your productivity. So why make your own automation system? Why not buy something that is already existing? Well you can do that but there are several drawbacks to it and there's no fun in doing that. So first the solutions available in the market are way too expensive. Second comes the privacy issue. So most of these sensors they generally have a cloud based implementation. So they might be sending your private data 24-7 to the cloud and you never know how they are actually processing it and how they are actually using your own data. So that can be a major issue. Another major issue is integrating with each other. So each company wants you to use their own complete stack for automation. Therefore it might be a challenging task for you to integrate them on your own. And fourth so each individual has you know different requirements. So what is available for the mass may not suit you. So you always need some kind of customizations to it. Now comes the perks of creating your own automation system. It will be less expensive. You will have full control of the data. And yes you will be able to integrate with existing systems like you know different home systems and several smart appliances etc. And fourth and most important of all it will be really cool to have your own automation system. Well I hope by now I have motivated each one of you to create your own system. Let's start by creating one. So we will be using MQTT, Arduino and PyCute as it is already mentioned in the title. But the main question is why. So there are lot of different tools that are available but there are some specific reason why I have chosen them. So I will be covering them and along with it I will be giving you a brief introduction about these topics as well. First comes our MQTT. MQTT is a very lightweight public subscriber based protocol which basically runs over TCP IP. It is very popular and widely used protocol for transferring data between different nodes. It can be configured for cloud based application or your local application. This particular protocol was developed by IBM. So let's see how it actually works. Let's say you have bunch of different sensors and you want to disperse the data over multiple devices. Now instead of having different connection with each other we will send each data to a common server. This server is our MQTT broker. So purpose of this MQTT broker is to keep track of all the different nodes that are connected and send them very specific data that they have asked for. This can be done using publishing and subscribing of topics by different nodes. So we will discuss MQTT topics in detail in some time. Each node which wants to send the data will publish that particular data on a specific topic only. And each node which wants to receive that data will subscribe to that topic. So let's understand this using a very simple example. Let's say you have a temperature sensor and it is connected to your Arduino and we want to display its data over our laptop and our mobile devices. So this Arduino will publish the temperature data over the topic temp. Now temp is the topic that we have decided through which we will be communicating. Now all the devices that wants to use this data that is your laptop and your mobile devices will be subscribing to this particular topic temp. And whenever MQTT broker receives the data it will automatically forward it to all these devices. So a node will only receive the data for the topics that it has subscribed for. Now you can set up your MQTT broker either on your PC or some SOC devices or any other platform which supports either your Windows or Linux. Now comes MQTT topics. Topics are very important in MQTT. You will need to have complete knowledge of them before even publishing and subscribing to them. So topics are used as a routing information for MQTT broker. They have an hierarchical level which is separated by a single slash. As you can see in the table we have a house where we have two rooms and in each of them we have different sensors. Now if you want the temperature data then you will be subscribing to house slash room 1 slash temperature topic. But what if you want to subscribe to all the sensors from all the rooms? For that particular thing we will be using a multi-level card which is represented by a hash. If we subscribe to house slash hash then all the topics which are under house will be subscribed automatically. Now the question arises what is the difference between subscribing to house slash and house slash hash topic. So in case of house slash you will be only hear the data when a note publishes on the topic house. While in the other case you will be able to hear the house topic as well as all the child topics as well. So that includes all the temperature and your room data. So you need to keep all these things in mind. Now comes the quality of service in short course. So this is a very important feature in MQTT. Cost basically increases the reliability of sending data. In some scenarios like a fire alarms we need to make sure that our message is sent and it is also received by the note. This can be achieved by setting some flags. So we have three different cost level. First is the cost 0. It is the default cost level in MQTT. It is meant for applications where even if you lose a particular packet it will not affect your system that much. For example your humidity data or your temperature data. Second level is cost 1. It ensures that the message is received by the subscriber at least once. It is used when you want guarantee and you have no issue in receiving the data multiple times. And the last level is cost 2. It is suitable for critical scenarios where you want guarantee as well as you do not want any duplicate messages. So again the question comes why not always use cost 2. Well it is the most reliable method but it also requires more bandwidth and will take more time to respond. Now to exchange data between different notes there are lots and lots of tools available. Still why do I prefer using MQTT? First it is pubsum model. So generally while using HTTP we have request response model in which you need to keep track of all the clients and have direct connection between the clients and the server. This is good for mobile application or your laptop application where you just need to browse the internet. But it is not good for a home automation system. Embedded devices usually have very limited space RAM and have single processing port. So they might get overloaded because of all the requests they might be getting and it will delay the response time as well. Second is less connections. So as explained everything will be centralized using MQTT broker. So automatically total number of connections reduces and you will be having more bandwidth to work with. Third is receive what you need. You do not need to filter out the data that you require from the particular packet. Since we are subscribing to specific topics only. Fourth it is very lightweight. So all the data exchange is taken care by MQTT broker. Therefore it is ideal for implementing over embedded devices. Even the header size is just 2 bytes as compared to 8000 bytes in HTTP. Fifth it is battery friendly. So this was part of a research that was concluded by IBM while they were developing this protocol. Sixth is security. So MQTT supports TLS SSL for encryption between device and the broker. Also broker can block the access of unpermitted devices to restricted topics. Seventh it is generally perceived like the MQTT can be implemented only over TCP IP. It is not exactly true. So any protocol that provides orderless, lossless and bidirectional connection can support MQTT. It ensures message delivery. So this is something that we have discussed in great detail in the previous slide. And yes it is scalable. So since it is PubSum model only you just need to upgrade your MQTT broker. So after giving so many advantages I think it's enough to convince you why this particular protocol was chosen. So in order to set up MQTT on the broker we'll be using Mosquito which can be installed either on your Windows or your Linux system. On the client side in Arduino I'll be using PubSum client library and in Python Paho library is used. Now comes Arduino. Arduino is an open source hardware and software company. It has very large user community which has built a lot of project over a period of time. So you can go online and explore it. So why Arduino was chosen? Because I feel Arduino is best for beginners. There are a lot of different microcontrollers available in the market but it has several advantages over them. First it is open source. So you'll get a lot of documentation and also you'll get very good online support for all the bugs that you might be having in your code. There are also a lot of variants of Arduino boards itself that are present online and you can buy them very easily. You can also customize one according to your own need. And also there are lots and lots of already built library present for Arduino even for MQTT. So you can just focus on the development part. Now which Arduino board to select? You have your common boards like Arduino Uno, Mega and NodeMCU. For this specific project I have chosen NodeMCU. The main reason was that it has Wi-Fi module already integrated in it. So in other boards you'll have to connect an external ESP8266 module for Wi-Fi connectivity. Other thing while selecting board is to keep in mind the sensor compatibility also. So make sure that your board supports the input voltage, current and output pin type of the sensor. For example in NodeMCU you only get one analog pin. So that can be a bottleneck in some case where you know you want to have multiple analog sensors. Now this is only for those who are not familiar with coding in Arduino. The language is simplified version of CC++. Make sure that you have selected the correct compute and hardware in the tools menu and you have your two main functions with you. One is setup function which is responsible for initial setup while booting up and other one is loop function which runs indefinitely. Now comes our GUI part. So we'll be using PyQt for that and PyQt is a GUI widget toolkit. It is a Python interface for Qt. Now Qt is one of the most powerful and popular cross-platform GUI library. This is the GUI that we'll be creating for visualizing our data from the sensor. Also you can choose any GUI of your choice. There are a lot of different options available online. So implementing over PyQt is really very simple. You have basically two methods of doing that. First is code each and everything. That means starting from your window size, what kind of widgets, buttons you want and their location, their properties and each and everything. So that can be a really challenging task for someone who is just getting into PyQt. The second method which I personally prefer is first you develop the complete GUI using the Qt designer. This will save your GUI in .UI format. Next you can use this program PyUIC5 which will convert your GUI file directly into Python file. And once you have your Python code ready, so you can integrate it with MQtT and add some additional functions. And you are good to go. So let's start with actually implementing it. Here I will give you an overview of the code flow and architecture. In case if you want to know everything, you can find all the details on the GitHub repository. So this is the basic architecture that I have implemented. You have a microcontroller that is Arduino in our case, which is connected to DH11 sensor. That is your temperature and humidity sensor. So it will be continuously collecting data from this particular sensor and will be publishing it over your local network. So our MQtT broker from there will receive this particular data and send it to our laptops from which we have subscribed it. So in this implementation, our MQtT broker is also running on a laptop itself. First start by implementing our Arduino code. So as I mentioned earlier, we will be using simplified C in Arduino. First you need to store the correct details in order to connect to a network. That is your Wi-Fi details and as well as your local MQtT server IP address. Then comes our main function that are setup and loop functions. So in setup function, you need to connect to your MQtT broker and start your DH11 sensor. And in the loop function, you will be collecting and sending data every one second using the function get and send sensor data. Now let's look into this function in great detail. So as you can see in the function, after collecting the data, there are two ways of actually publishing it. First is to combine all the data that you have collected and parse it in JSON format and publish it under a single topic. For example, over here I have published it on house. Second method is to send the data on different topics. That is house slash temp or house slash humidity. So the choice is totally up to you. You can choose between any one of them. Okay, now our Arduino is good to go. Let's start the implementation over Python. In Python, we'll be implementing three things. First, code to listen to all the topics that are active in MQtT broker. It will be best for checking which topics are currently being published by which devices. Second comes our dummy sender. For those of you who do not have an Arduino and sensor at the home, you can use this particular script just to simulate one. Third is to GUI to control and monitor our automation system. So let's start with subscriber.py. This program will help you to subscribe to all the topics in your network. So first, you need to initialize your client. And then you can mention two call bank functions. First is onConnect function and second one is onMessage function. So onConnection function is called whenever your client is connected to the MQtT broker. And onMessage function is called when you receive a new message from your subscribed topics only. So in order to connect to your server, you need to mention your server IP, your port and also you can mention your keepalive time. Always remember the default port is 1883 for mosquito. Now, as you can see, for the path I have added the multi-level card. So that means I'll be subscribing to all the topics that are present on my network. Now you need to add this loop forever function. There are other looping function as well which will use multi-threading and I'll let you know about them in further slides. So now comes our dummy sender part. Don't worry in case if you don't have the hardware you can create your own dummy sender and test your system with all the ease. So the basic setup is same in order to connect to MQtT server that we have discussed earlier. Also as you can see here I have used loop underscore start function. So here I'll be using multi-threading. This is a multi-threading function that allows you to execute other codes as well. So you'll be sending your random data using this function send underscore data. So let's see how it is actually implemented. Send underscore data function is very similar to the one implemented over Arduino. You'll just generate a random data for temperature and humidity under certain range and in a while loop you'll be just publishing it to the topics. So again over here also you can choose between either sending it on JSON packet or sending them separately. So let's start creating our GUI in Qt Designer. Till now we have taken care of sending our data from our sensor to our MQtT broker. Now we need something to visualize it. So this is your basic Qt Designer and you will start by creating a new window. On the left-hand side you will get all the widgets, buttons, etc. which you can add by simple drag and drop. Also you can edit the object properties on the menu present on your right-hand side. So first of all you need to add push buttons, LCDs, text levels and horizontal slider bar. You can add more widgets and buttons according to your own need. Please feel free to explore it. Now select a push button and you need to change the object name and the text. So changing object name is important because same variable name will be used when you convert this UI into your Python file. Now do this for all the push buttons, LCD and your slider bar. In case of your slider you also need to set the minimum and the maximum value of the slider. Since we are implementing for controlling our AC temperature so over here I have set minimum at 15 and maximum at 30. Now we will be adding signal and slot features for closing our program via close button and displaying current AC temperature from our slider to our LCD. So signal and slot are used in Qt for communication between two objects. As you can see on the screen after going into the signal and slot menu you need to select the widget or the button for which you need to add the signal and then choose from all the options like in case of close button I have chosen close option. So basically signal is emitted when a particular event is occurred. It is an alternative to the callback functions. Now to have full capabilities of signal and slot you actually need to implement it in C++ but don't worry in case if you are not able to understand this we can add this feature directly in Python using two lines of code. Now comes the part of actually converting it. So as I mentioned we will be using pyUIC5 program to convert from .ui to .py file. So this is the command for converting it. As you can see you just need to set the input file name and output file name as input arguments and this file is available in the installation location of pyQt and also in case if you don't find it over there you can download it from my GitHub repository. Now that you have converted everything in Python we will integrate MQTT and add some additional features. Again I will not be going into the detail for better documentation please refer my GitHub repo. So your main window of GUI is implemented in form of class in Python. So we will be adding additional functions in the class itself. Here is a snippet of some of the functions that I have added in the class itself. On the top you can see the implementation because of signal and slot and down below are some of the functions that I have added for connect and disconnect buttons also for changing AC temperature using slider. As you can clearly see all the push buttons have inherited the names from GUI file that we have created in Qt Designer. For push buttons I have added the feature that once they are clicked a certain function will be called. So when connect button is pressed connect underscore MQTT function which is implemented inside the class only will be executed. You can look into Python documentation for better details. Also keep that in mind that you never use a forever underscore loop function for the client over here else your GUI will get freeze. Finally let's see a quick demo of this project. Here we have a dummy sender that will work. This script will publish temperature and humidity data to the MQTT broker every 2 seconds. This is similar to how Arduino will be sending the data. But in case of Arduino in order to see the temperature data you need to use your serial monitor and to actually see your published data you can use subscriber.py file to display all the data coming into MQTT broker. Okay. Now comes the GUI part. So initially everything is at default. As soon as you click connect button the data will be updated and that is only in the case if your Arduino is sending your data or either your dummy sender script is running and if you click on disconnect the data application will get stopped and you know you can just click on connect again and if you move the slider the temperature you can see it will start changing. So now we can see both of them working together. First you need to start your dummy sender and as soon as your dummy sender is started you can click on connect and you can see the live real data is being displayed and you can... 5 minutes more. 5 minutes more. Sure. Okay. So you can see I have also published the AC temperature very easily. So yeah that's all I can tell you in such short time. Feel free to ask any questions that you have related to the talk. I'll be more than happy to play with them. Thank you so much. Thank you so much, Harsh. I think there were a lot of interesting chats that were happening in the background as well and I'll ask some of the questions that people had. Sure. One question was when I asked are you using RabbitMQ or MQTT or is it mosquito? The answer was mosquito of course. And one question is is there any online simulation platform for NodeMCU? So when I provided the answer that there is a marketplace visual studio extension but do you have any more recommendations? Not exactly for NodeMCU but you know if in case someone wants to try on just Arduino Uno board then there's online platform called thinker board I think. Thinker board or thinker cat. So you can Google it up and you can add all your different type of sensors and you can just run your script so you can simulate it online also. It will not have the feature of connecting it to the internet but you can just see how your sensors actually send your data and how you can perform different activities over them. So that can be used. Cool, nice. Also how did you get the AC temperature? If you have mentioned it they might have missed it. Okay. So AC temperature is you cannot get them directly. So in case if you have a smart AC only then you will be having a real time data from that. I have just shown you an example that yes you can do such and so in thing. So in case any vendor that provides you some kind of information just only then you will be able to integrate it directly. Got it. I think we still have some more time for some more questions. Does anyone else have a smile chase? Thanks for the wonderful session Arvind is clapping. I think someone else Yeah, there are a lot of links. Okay, so I think you provided the way to contact you over here. People can note them down and can you please will you be available for questions later on the ZULIP channel? Yeah, definitely. Yeah. Is there any mail ID that they can Yeah, I have also mentioned over there at the bottom like health information. Got it. So mail ID as well. Okay, I gather that there are no more questions. Okay, so the next thing that's going to happen is a bunch of lightning bolts from 355. It will be happening in the Bangalore stage. Okay, we have one more question. It asks how to proceed in case we have a normal AC. Is there like a adapter or something that can be installed? No, and actually I do not you know recommend doing that because for that you need to have complete knowledge of you know electronics and it might affect your complete AC sensor so your AC might be affected. So actually I would say that you do not try this at home unless and until someone provides specifically for that. Yeah. Also because asked how can we scale the broker? Okay, so the limitation is like you know how many connections your system can handle. So in that sense only you need to scale up and you know the amount of RAM to process each and every data that will be coming and you need to send everything. So you can just increase your network adapter, your RAM and your processing unit that you have. So I think using that you will be able to scale it and also in case if you do not want to implement on your laptops so you can use some of the online servers that are present for MQTT as well. Got it. Okay. Other than temperature and humidity, what else do you think we can automate? I think the light bulbs are already a solid problem. I think we can get a little bit more interesting with that. Yeah. So the original project that I had implemented so it was way bigger than this. So in that like I have included data from all the different rooms like you know the temperature data is one thing but you can also collect camera information. So you can have like a Raspberry Pi or something located at your home and from that you can process using camera particular data and you send the server only the processed data. For example like who is at the front door and if there is someone or such kind of information you can just send the processed information using MQTT very easily. So in that sense I think you can use. Right. I think the possibilities are endless. Yeah. But it does require a lot more modern equipment than the age old analog equipment that was tripled. Yes. This is a very wide field. For this project like I worked on one of my internship at Malaysia. So I worked there for around two months in it. So yeah you can explore it and tell a lot of different things that you can do with it. And even you can apply machine learning for you know what I did was taken all the current and temperature sensor data and predicted what will be the future power consumption. So at that time I was using just for a single house you can scale it up for the whole community and so on and so forth. That's interesting because yeah normally you're thinking about your own smart home automation but when you live in a community it does import some regulation as well right? Like maybe you are under your homeowners regulatory policies and all of that. Exactly. So this guy when I asked have you slept on the NodeMCU he's had problems with it on the MicroPython. Okay MicroPython like I have not used physically I've just used the online simulator for MicroPython that is available so you know I cannot go into that much detail in MicroPython but for yeah in case of R you know I have used the silly function you know for every one second it will wake up and only then it will reconnect to the network in case the connection is broken and then it will be sending the data. Yeah and I did miss one question how about privacy of the data shared between these and MQTT is there a vulnerability could somebody hack into this wireless and transpose and change them Okay Yes. So yeah you can add additional security so like you can add your username in order to first have the connection and then you know you can encrypt your particular data and also even after that if you feel that yes my data is not so secure you can add additional you know security and then parse it in like I said in JSON format like if you parse all the data and add some additional like AES or some type of encryption over that particular data so in that way also you can increase your privacy issue I think. Excellent. Okay so there are some more questions but I think we are out of time but thank you so much this was a very well done talk you know we had enough time for Q&A and for the talk. So Harsh again yeah thank you and I gather you'll be available for Zulip on Zulip so Sundra Kanesh you have a question but can you please take it on Zulip he will be there or any of the contact information any of the ways the contact information provided. So yeah and Harsh I'm going to bring you down from the stage and people please go to the Bangalore stage because their lightning talks are just starting at 355 followed by there is a small there's no break but 430 there's a keynote by James Powell and I know for a fact that Vinay is very excited about it and so am I so I guess we all come back for sessions to the Chennai stage at 550 where Akshay Bahadur will be talking about resource utilization as a metric for machine learning 550 Chennai stage till then please look around stages expose sessions all of that again Harsh thank you and to the audience it's a lot for the participation and because so much for handling the questions and making them accessible to me again there's a community effort see you soon yeah thank you so much