Automated Environments: Making Smarter Devices
Using IoT to turn unintelligent devices into smart ones.
While sitting at home every day for the past 2 months doing boring school work, eating, playing and going to sleep, the thought of “how can I improve my life” has come up more often. I’m usually the kind of guy who makes a lot of realizations in my life, but this one (surprisingly) hadn’t ever come up.
Now usually, a lot of people (including me initially) want to go down the path of reading motivational/self help books, working out more, eating healthier, all that good stuff.
I, on the other hand, went the more impatient route. I just wanted to build something that would make my life easier and better. The idea that came out of my head was definitely not going improve my life by 1000% (or maybe it would…), but I thought it could have the potential to optimize the way I work.
Factors influencing my efficiency
The way I thought of it, there are around 2–5 factors that could affect how much work I would do in a given period of time, and they included:
- A preset constant from when I was born deciding how much potential I had and my overall intelligence.
- My overall willpower
- The past environment I was in — how much sleep I got last night, what I ate, etc.
- My current environment — lighting, temperature, mood, smell, whatever.
Two of these 4 factors I thought of happened to be related to the environment, which I have changed in the past, and it has done wonders for me and my work.
So, looking at this list, I thought to myself. What if I changed my environment to optimize for my work? I would change the small things, like the lighting around me, temperature and all those things. What if I could determine what the optimal temperature and light combination for work was. What about playing? Does this mean I could control my mood to some extent?
To solve this problem, I set out to create a device that could pretty much control my environment, including lighting. I’d recently started getting into a field called the Internet Of Things, so I wanted to try my best to use its systems to be able to remotely control my environment with a simple API call containing all the details.
How it works
Controlling heat
To start off, I’ve made this system only using one device rather than a bunch of them in order to show an MVP. For now, my system can control the temperature using an infrared-controlled heater. This meant that I could send it an IR code and it would turn on/off. I also made it so I could set the temperature on the heater, but I’d much rather prefer an external system because it would be able to measure the ambient temperature of the entire room instead of near the area of the heater (my heater)
Controlling the heater from my API
looking back at it, I probably could have made the process of controlling a heater much simpler than the steps I took to do it, but it was a really good learning experience. Essentially what happens in my system is that a computer sends an api request to the AWS API gateway, which sends the data to a lambda function, which then forwards the message to an IoT core topic, which is then received by a Raspberry Pi, which sends data to an Arduino, which then turns on the IR controller, which then turns on the heater.
“But Ankur!” you might be asking, “Why the hell is this so complicated!?!?!”, and to that I’d say, good question. To some extent, I was just trying to be extra, but I also think that onboarding new devices would become much easier because the only thing I would need to program is a control system for the device (which the arduino would handle) rather than also having to figure out I could communicate with it from anywhere and anytime. Amazon Web Services is a cloud platform that would allow me to store my data on the cloud and easily transfer it to an edge device like a raspberry pi, which is why I used it.
Okay, so far, the things I’m talking about might just sound like random words to you, so let’s break it down.
Using APIs to transfer state data
At its highest level, an Application Programming Interface (API) is kind of like the middleman for two applications to communicate. The API is the messenger bird that sends messages back and forth from two kingdoms (bad example but you get the point). One application could send a message to an API and it would return data from the other side. In my project, I used an API called REST (REpresentational State Transfer) to send data on the kind of state I want the heater to be in (either a 1 or a 0 to represent ON and OFF). The main purpose of this was to get data to the cloud, where I could do data processing and send it to a raspberry pi.
AWS Lambda
Using a cloud platform like Amazon Web Services is unfortunately not free, which means that I would need to pay-per-use of their platforms. Because I’m doing data processing in my project, I would need to host something on the cloud that could do computations, which is an EC2 instance in the case of AWS. However, I would end up only using the instance when I receive a message from an API, which would be cost inefficient. Because of that, AWS also has a service called Lambda, which is a function that only uses compute power when it’s called, allowing for a more cost-effective solution. In addition, it was really easy to route the data coming from the API into the lambda function, which is another reason why I decided to use it.
Cloud IoT Core
The AWS IoT Core is the most important aspect of this project since it interfaces with the cloud. The IoT Core is responsible for communicating with the edge device (in this case a raspberry pi) and it works using a communication protocol called MQTT, which uses a publish and subscribe model. In this, a topic is generated, and anyone with the right credentials is able to subscribe or publish to it. If a message is published to a topic, all of the subscribers will receive that message. Using this, I was able to send the IoT topic a message through the AWS lambda function and the raspberry pi (which was subscribed to the topic) received it.
Edge processing — Raspberry Pi
Now we’re at the raspberry pi, which is now able to receive messages from an IoT topic when an API call is made. Now we need to worry about processing the data we received and sending it off to the arduino in some way. The incoming data is in the form of a JSON, which is kind of like a python dictionary, so we can extract the incoming desired state messages pretty easily.
Serial communication — Raspberrry Pi to Arduino
In my setup, I used a form of data transfer called serial communication (basically transferring data through a USB or Universal Serial Bus). For this, I just needed to connect the two via USB and then write some code to allow data to be transferred between them.
The end result
Okay cool, so we’ve gone a little bit in-depth into how all of the processes communicate and yes, there are probably more efficient ways of doing this, but I wanted to learn about all these new things like APIs and different services, which I was able to do through this project.
Next Steps
So this process was just for one device — my heater. Over the next few days, I’m going to be programming it to have a lot more functionality, including these few things:
- Time based activation. I may want to convert the lambda into an EC2 instance so it is able to have a time-based command where I can activate devices. Imagine going to sleep without a heater on and waking up with a nice, toasty room ready to work in, lights setup and all that.
- IFTTT. I recently got smart lights for my room and I’m going to either be using their API or an application called IFTTT (If This Then That) to integrate my ceiling lights.
- Onboarding of new devices. I also happen to have a number of other things I want to control, such as humidity and localized lighting (using strip lights), so I’ll be working on adding those.
Conclusion
So yeah, that’s been my progress with my project so far. I’m sure there are a lot more things I can do, but I’ve pretty much set up a basic infrastructure for onboarding new devices and collecting data from certain locations. I’ll post my Github Repo to this project so you see my code for it :).
Also, if you’re interested and want to see more from me, check out my personal website!