Sonoff Pow R2 Custom Firmware and 3D Printer Standby Killer
by Wim3d in Circuits > Microcontrollers
1013 Views, 0 Favorites, 0 Comments
Sonoff Pow R2 Custom Firmware and 3D Printer Standby Killer
In this Instructable I will explain how I wrote my own custom firmware for a Sonoff Pow R2 smart switch and how I implemented this switch as a standby killer for my 3D printer to cut off the power when the 3D print is finished.
Warning: this Sonoff normally is connected to mains voltage. This is DANGEROUS!
Note: It is not only dangerous for you, but also dangerous for your computer if your computer is connected to the Sonoff when you connect the Sonoff to mains. The reason for this is that the ground pin of the Sonoff is not 0V DC when the Sonoff is powered by mains voltage.
For an explanation of the basics of my smart switch firmware and the standby killing function I recommend reading my other Instrucable. My 3D printer uses 10W when idle, so the power in cut when the power remains a few minutes below 12W.
The firmware is published on my Github page as Wifi-smartplug_v3.
This Instructable focusses on the power metering function of the Sonoff Pow R2.
Supplies
Supplies needed:
- Sonoff Pow R2
- FDTI programmer with wires etc.
Understanding the Basics of the Sonoff Pow R2
The first step in creating the custom firmware is understanding the ESP8266 based Sonoff Pow R2 functions.
The Sonoff module has the following functions.
- Connect to WiFi (processed by the ESP8266)
- Read the tactile button (GPIO0)
- Switch the relay and the LED connected to the relay controll pin (GPIO12)
- Light the connectivity LED (GPIO13)
- Read the power of the connected circuit (GPIO1 and GPIO3)
On this website I found out which GPIO pins of the ESP8266 are used.
The power is read via a serial connection of the hardware serial port of the ESP8266 (GPIO1 TX and GPIO3 RX). This means that you can not use the normal serial port for debugging when you are using the sonoff for power measurement. This is good, since you should not connect your computer to the Sonoff when it is connected to mains voltage! See the warning in the Introduction.
Power Monitoring
In the other power metering smart switches I used and explained in my Instructable, the BL0937 chip is used, this works somewhat similar to the HLW8012 and sends pulses to the microcontroller. The amount of pulses or the length of the pulses corresponds to the amount of power/current measured by the chip.
The Sonoff Pow R2 uses a Chipsea CSE7759B. Note: this is different than the CSE7759.
To understand this chip, we have to look at the datasheet of the chip. Searching the internet for the datasheet of this Chinese chip gives a lot of Chinese datasheets. I fed them to Google Translate. However, you can also find an English datasheet.
This chip measures the power and sends the data via a serial UART connection between chip and ESP8266 as a datastream.
In my first tries of the firmware, I checked the serial port for data and read the datastream like I did in other programs. Note that the chip uses a very low baudrate of 4.800 Bps. The sending of the datastream of 24 bytes takes over 50 ms (see datasheet screenshot). Blocking the program for this long period to read the serial data did not work out well, since the ESP8266 should do other things in this time, like keeping up the WiFi connection and listening to MQTT messages.
The MQTT are needed for communicating the measurements, but also to receive commands like 'Switch on'. In the programming fase of my project, I used a lot of MQTT messages for debugging purposes, since the serial port of the ESP8266 could not be used for that (see previous step).
Understanding the Datastream
The CSE7759B sends long datastreams (24 bytes) via a slow connection (4.800 Bps). To read the datastream, we have to find the beginning of this datastream. The chip sends two bytes identifying the start of the datastream.
These start bytes are 0x55 and 0x5a. In my code, I look for the first byte (0x55) and then check whether the next byte is the second identifier byte (0x5A). If this is the case I set the measurement to 'succesfull' and continue to read the chain of bytes with a total length of 24 bytes.
Calculating the Power and Voltage
From the datasheet, you can read that after the two start bytes, the datastream consists of:
- Voltage coefficient (3 bytes)
- Voltage cycle (3 bytes)
- Current coefficient (3 bytes
- Current cycle (3 bytes)
- Power coefficient (3 bytes
- Power cycle (3 bytes)
- calibration times (1 byte)
- number of CF pulses (2 bytes)
- Packet tail (1byte)
In my setup, I only use the voltage, current and power values.
How to combine (concatenate) the 3 bytes to a single value, I found out with help of the picture from this Github page and Arduino forum.
See the code below how to combine the bytes from the data[] array of 24 bytes. Byte 0 and 1 are the 2 start bytes which identify the start of the datastream and are not further used.
voltage_coef = (data[2] << 16) | (data[3] << 8) | (data[4]); voltage_cycle = (data[5] << 16) | (data[6] << 8) | (data[7]); current_coef = (data[8] << 16) | (data[9] << 8) | (data[10]); current_cycle = (data[11] << 16) | (data[12] << 8) | (data[13]); power_coef = (data[14] << 16) | (data[15] << 8) | (data[16]); power_cycle = (data[17] << 16) | (data[18] << 8) | (data[19]);
The voltage, current and power are calculated as a float via:
float current = (float)current_coef / (float)current_cycle; float voltage = (float)voltage_coef / (float)voltage_cycle; powerW = (float)power_coef / (float)power_cycle;
I hope I have helped you further or inspired you in your projects.