Well I saw the smart air cooler project by Moiz where he switched the cooler on/off from his web app and I thought of extending it a little. While controlling an appliance from phone is cool, it doesn't save you any money, right! So I thought what if we could see how our appliance is consuming power and tune our usage around that and eventually reduce our electricity bills by a few percent. So in this tutorial, I'll show you how I built my IoT power meter that monitors current and power of appliances similar to the cooler. The power meter shows the current and power consumption graphs in the web app on my phone.
Let's Talk Business!I'm designing my power meter from the same commercial and production point of view as Moiz did: a company producing power meters and selling it to real users. So when the power meters hit the market, there will be real users buying them and using them.
Making them light and cheap gave me the idea of showing the current flow and power consumption in a web app (mobile app) instead of attaching an LCD on the meter (LCDs that can plot graphs are not an option for a $4 power meter).
Since I have real users, there are some issues here. Will my app contain all the power meters I ever sold till date? So when a customer downloads my web app, will he/she have to find his/her power meter from a huge list of all those power meters to see its power consumption? This not only makes it more complex than it needs to be but also compromises other users' privacy. No way. There needs to be a mechanism that shows a user only his/her devices when he/she opens the web app. This hooked me up to the idea of setting up user accounts. Each customer would have to register his/her account on the web app when he/she buys a device from me for the very first time. To see his devices and their data, he/she would have to log in just like you do on facebook (you see your friends, your own posts, and your own groups, not everyone else's). Now let's move to the second issue. When a user logs in, how will the app know which devices (from the devices that I sold) does this particular user own? Pairing scheme solves our troubles here. When a user logs into the app, he/she sees empty list, and an "Add a device" button, on clicking which he could enter the device ID (which I would print on the device itself), and that device would be added to his/her devices list. What it'd do, under the hood, is it'll pair that power meter with that user account making that account the owner of that device, and only he/she can see the device data.
Now to implement this, I went through the most common tools out there including Blynk. While those tools are good for prototyping a hardware project, they do not solve issues of commercializing an IoT product. Blynk's app is non-customizable which means saying goodbye to my brand, it's energy based model gets pretty expensive when you scale it to a few hundred power meters, and it solves none of the accounts and pairing issues for me. Not just for this power meter, if I'm selling anything in the market to real users, Blynk app looks very immature.
That's when Grandeur came to the rescue. It's pretty much designed for the IoT businesses. It is offered as a backend that solved pretty much all business, app, and hardware hassles involved in developing an IoT product (their words, not mine!). It's open-ended. What that means is I can develop my own power meter (on device-end) and my own app (on user-end) and just connect them together with Grandeur as their integrator. So I no longer have to rely on the same Blynk app for every product I make — I can make separate standalone apps for each. They made available these SDKs to program the hardware (Arduino, ESP, Raspberry Pi) and apps (Javascript, Android, and iOS). In the App SDK, there's this Auth API which lets you perform user authentication in the app. This means I can build register/login functionalities. And there's the Device API, which lets you send/fetch data to/from the hardware device. It also has the pairing feature that I need. Feels neat!
ObjectiveNow that we know the base platform I'd develop my power meters on, let's officially define our project:
An IoT power meter using ESP8266 and ACS712 current sensor that would send current and power readings to the app where I'd push those readings to a graph in real-time.
Along with this, a user would be able to pair a device and monitor the current and power readings in realtime after he/she logs into the app. Okay then
The idea behind this project is to use the current sensor ACS712 to calculate the current. ACS712 is, in fact, a hall effect sensor and it works by measuring the induced voltage of in current carrying conductor — sounds confusing 😅? Let me make it easy for you.
So we know from basic physics that when current flows from a conductor, magnetic fields gets produced and if you place another conductor in its close vicinity then the magnetic field will induce current in it. That's how the transformers work and that's how we can measure current safely (considering we want to measure current of real appliances — which is up to several amps of current).
What I did was doing ADC on the output of current sensor (which is the voltage induced due to the current) and measured the actual current by multiplying it with the sensitivity of the current sensor (1A change in current changes the induced voltage by 185mV for a 30A current sensor). That's how simple the circuit looks like:
I powered the current sensor with 3.3V (via the ESP) and connected its output to the analog input of the ESP. Here's code to measure the current from the current sensor:
Making a PCB out of it, here is a glimpse of the design:
The fritzing file of the design and schematic is attached in the end. Then I given it a test run and here is how the output looks like
So, the hardware worked. Let's see how I integrated Grandeur and built the app.
Step 2: Get Started with GrandeurGrandeur is designed to be straight forward and have a seamless yet powerful integration in your IoT products. You can find the documentation here Here's the "hello world" tutorial to give you the most beginner context.
Here is every step that I did to make use Grandeur to build my app:
- I logged into the platform and created a new project for my IoT product:
- Then I registered my first ever power monitor device and named it PM-1. This is the very first prototype of my power meter. Here's how I did its registration:
- Since we want to build our power meter for selling to customers, we'll add signup and signin functionality in our app using Grandeur Auth, so that the customers can register themselves to use the app. We'll add a demo user for testing our device for now. I created the first user of my product from the Users page:
- To test my device, I paired it with my user directly from the Devices tab (I'm loving it), so that my user account has access to the device (PM-1) and its data (it's current and power). Here's how I paired them together:
To build up program for ESP8266 on Arduino IDE, I used Grandeur's Arduino SDK for pushing current and power updates to my project on Grandeur. But the Arduino SDK requires my project's API Key and my device's ID to know which device in which project are we referring to when setting summary. Device auth token helps in validating the device's authenticity. My ESP cannot connect to Grandeur without this token.
Grandeur handles device communications with Cloud over the web, which means we first need to connect our ESP to internet which is what ESP8266WiFi library is for. It handles my ESP's connection with my WiFi which gives it the internet connection it needs.
Here's the Arduino program for ESP8266:
In every loop, it runs the sendUpdate
function which reads current sensor's OUT pin (connected to ESP's A0) for one second, translates from ADC levels to voltage, gets upper and lower voltage peaks from that one second data, calculates peak-to-peak voltage, then RMS voltage, translates it to RMS current, and then finally calculates power. It then prepares a summary packet containing the RMS current as current, power, and the time of the update, and updates the old summary on the Cloud with the new one.
If a summary update occurs on the Cloud, it prints it to the Serial (just for logging purpose).
Timestamping each summary packet with the time of the update helps me plot the current and power on a timeseries graph in my web app.
Step 4: Web AppTo build my app, I used Grandeur's JS SDK for fetching summary updates from the Cloud, and then put up current and power graphs from that data using chart.js. With JS SDK's Auth service, I implemented the login functionality in my app which let me build up a user's profile and show him the data of only those devices that he paired with.
But to use the JS SDK in my app, I needed my project's API key and access credentials (access key and token) from Grandeur Console's Access page, which the JS SDK uses to make connection with my project on the Cloud. API key acts as the identifier for my project while Secret is part of the request's source validating scheme. This is how I got them from the Grandeur Dashboard:
Here's the code for my web app:
DemoTo test my power meter, I used python3 -m http.server 3000
in my app's directory to locally serve my web app on localhost:3000. Grandeur naturally rejects all requests coming from unknown origins for security, therefore we need to add localhost:3000 to project's allowed origins list. Here's how I did this from Grandeur console's access page:
Opening localhost:3000 in browser to run my power meter web app. Here's how it looks while running:
We log into the app with the email and password we gave our Test User. On logging in, I see my PM-1 power meter that I registered earlier on Grandeur console. When I click on it, I see it's current and power graphs getting plotted on data coming from my power meter in real-time.
Warning: It is important note here that experimenting with 220V⚡ AC can be lethal if not handled carefully. Another important consideration is the fact that ACS712 should only be used for subsystem monitoring and it is highly not recommended to use it for power monitoring of entire home. This circuit design is not fit for production use and is only meant for delivering a proof of concept since the focus of this project is more around the software segment.Conclusion
So it's settled. After putting it in a glamorous casing, my power meter is all done and ready to be shipped to the stores. Users will buy them, install them with their appliance, download my app from the app store, register their accounts, log in, pair the newly bought meters, monitor their appliance power consumption, and start saving.
Grandeur is a rather new but very mature tool to build IoT products if you are aiming to commercialize. You can check what they are offering at their website. Their SDKs and examples are present on Github and each SDK has its own details documentation. But I found their hackster channel to be the most helpful for they feature real projects there.
Till next time.
Comments