# Power meter and controller a project by Jonas Cederberg, jc222wf # Overview In this project I will build a power meter and controller. The way of measuring power is approximative since only a current sensor is used. This makes it a simple project that can be done quick, but should be performed with great care as mains voltage control is involved. The general steps taken were: - Disassemble power socket (1h) - Setup microcontroller (2h) - Connect sensor, relay to cables and microcontroller (3h) - Write code and logic for collecting and send information to dashboard (4h) - Setup dashboard (1h) DISCLAIMER: **DO NOT pursue this project unless you are sure you can safely operate mains voltage, otherwise it can kill you.** If you want to learn more about AC power, a good introduction can be found here: https://learn.openenergymonitor.org/electricity-monitoring/ac-power-theory/introduction # Objective This project is fueled by my passion to control and measure electricity. I feel that there is so much potential (hah) in gaining more knowledge and control over our electric devices. Controllers like this one are beginning to hit the market, and they are plummeting in price and increasing in usability. In this project I aimed to increase my knowledge on the topic to better understand these applications and create one I can use myself. This device can approximate the power coming out from a socket, and also via a internet dashboard control whether it's powered on or not. Completing this project will give insights into how you bridge the digital and physical divide by controlling real life applications over the internet. # Material | Component | Description| Link | Price | | ------ | ----------- | -------- | - | | WEMOS D1 mini v3.0 | Microcontroller |https://bit.ly/37dAWi2 | ~21kr | ACS758LCB-050B-PFF-T | Current sensor | https://www.aliexpress.com/i/32897164336.html | ~54kr | SRD-05VDC-SL-C | Relay | https://bit.ly/3A4Lhcs | ~9kr | Kopplingsur Mini | Mains socket | https://www.clasohlson.com/se/Kopplingsur-mini/p/36-6245 | 49,90kr | Connector cables | - | - | Cheap | | Breadboard | - | - | Cheap | Tools used were a soldering iron, solder and several screwdrivers for opening the mains socket and closing the connectors on the relay. ## Description of components The WEMOS D1 mini is a ESP8266 type board. I chose it because it is very cheap and also simple to setup for the Arduino IDE. ![](https://i.imgur.com/92pKGsC.jpg) *WEMOS D1 Mini v3.0* The current sensor and the relay was chosen because they were the ones available. ![](https://i.imgur.com/dwjH6pI.png) *Current sensor* ![](https://i.imgur.com/3NWUNXj.jpg) *Relay module* Last a mains socket with timer function was bought as to salvage the connector parts for more secure connections to the power grid. ![](https://i.imgur.com/J30R8ra.png) # Computer setup I'm using the Arduino IDE to code the microcontroller. The IDE supports compilation of the C-type code as well as transfer over a serial connection to the microcontroller. To gain support for the specific microcontroller board a ESP8266 library (link) is added according to https://help.ubidots.com/en/articles/2033398-setting-up-the-arduino-ide-for-ubidots. For the connection to Ubidots another library is installed as described in (https://help.ubidots.com/en/articles/1439097-connect-a-wemos-d1-to-ubidots-over-http). For manual control and check of files on the ESP8266, a python packet called REPL can be used. To install: $ python3 -m pip install mpy-repl-tool then you can follow `python3 -m there` with normal CLI promts, to for example list files `ls -l /flash/*` or remove them `rm /flash/file.py`. (this is a trick I learnt from https://hackmd.io/@mpetter/HyrfZ0TCu#Putting-everything-together, all credits to him) # Assembly The assembly was done by soldering pins to the microcontroller and cables to the current sensor, followed by connecting the control pins of the sensor and relay (VCC, GND, IN1/OUT1) to the relevant sockets on the microcontroller. The product is just a prototype so this was done on a breadboard. ## Schematic The microcontroller is connected to both the current sensor and the relay, taking input the voltage from the sensor and outputting a high/low to the relay control pin. ![](https://i.imgur.com/RjZvscQ.jpg) The current sensor is then coupled in series with the relay as shown in the picture below. ![](https://i.imgur.com/sFO50Fy.jpg) ## Current sensor output and microcontroller ADC limitations The ADC of the WEMOS D1 mini has a maximum input voltage of 3.3V, so sensor outputs with higher voltage would cause a problem. The sensor outputs a voltage of $QOV \approx VCC/2 = 2.5 \text{ [V]}$ when measuring $0 \text{ [A]}$ and has a sensitivity of $40 \text{ m[V/A]} = 0.04 \text{ [V/A]}$. The maximum current that we can measure is thus $$ 0.04*I_{max} + 2.5 = 3.3 \implies I_{max} = 0.8/0.04 = 20 \text{ [A]} $$ Since most breakers in normal households are set for less than $10 \text{ [A]}$ this will not be a problem. ## Voltage requirements Both the current sensor and the relay requires a 5V source which can be provided from the microcontroller. # Platform Requirements for the platform are: - Two-way communication for control of the relay - Time series data visualization for showing the current draw - Wifi based - Compatible with the chosen microcontroller ## Choice of platform: Ubidots +Easy low-code platform +Fulfils specs -Proprietary Time and convenience made me choose Ubidots as platform. Ubidots is a very convenient interface that handles all the data transmission, from code running on the microcontroller to the final dashboard. However its free version is limited to only 3 devices and doesn't offer full functionality. For this project however it turned out sufficient. The dashboard is set up to have one time-series data graph displaying the current values and a button acting as a power switch. Alternatives evaluated were among others the TIG stack which had more customizability and is open source. # Code In the code setup wifi connection is established through the ubidots library as well as pin declaration. ``` void setup() { Serial.begin(115200); ubidots.wifiConnect(WIFI_SSID, WIFI_PASS); // ubidots.setDebug(true); // Uncomment this line for printing debug messages // Pin declaration pinMode(D3,OUTPUT); // A0 is used for analog read, set as default in the LOLIN(WEMOS) D1 R2 & mini board // D3 is used for control of the switch } ``` The main program loop consists of several functions: readCurrent, calcIRMS, sendDataIRMS and toggleSwitch. ``` void loop() { int nSamples = 1024; // Number of samples to calculate the RMS over int iRMS = calcIRMS(nSamples); float approxPower = iRMS * 230; Serial.print("Approximated power:"); Serial.println(approxPower); // Timer: only send data and toggle switch every interval time unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; // Send data & check if switch is toggled sendDataIRMS(iRMS); toggleSwitch(); } } ``` The readCurrent function is based on the documentation of the current sensor and the code found at: https://robojax.com/learn/arduino/?vid=robojax-allegro_ACS758 . The sendDataIRMS is based on the previously mentioned WEMOS tutorial for Ubidots. # Data transmission ## Ubidots Data transmission is done entirely by Ubidots. It is crazy easy. The data is sent from the microcontroller to the internet and is stored in a Ubidots database. The data is sent from the device every 5 seconds by taking the most recently caculated RMS value of the current and dispatching it with a function from the Ubidots library. # Presenting the data The data is presented in a dashboard that looks like this: ![](https://i.imgur.com/rZrhZ0v.png) The data is stored for one month in accordance with the free Ubidots STEM license. # Final results The final results were not as good as expected. The current sensor had very high variance and was sensitive to electromagnetic disturbances, for example from the coils in the relay. A state-based offset to the sensor reading depending on if the relay is on or off was added to negate these effects but there was still variance in the measurements. In the end, the power was only roughly approximated. For a more precise result a voltage sensor would have been needed as well which was known from the beginning. There are several examples of other projects using both these sensors to great success (https://www.youtube.com/watch?v=PSzkaSy5lHY, https://www.youtube.com/watch?v=Vb9-pbLdsfQ&t=484s).