Both sides previous revisionPrevious revisionNext revision | Previous revision |
spark_counter [2016/02/21 10:40] – add warning kingkevin | spark_counter [2024/01/07 17:49] (current) – external edit 127.0.0.1 |
---|
{{:spark_counter:dsc02371.jpg?0x300|complete spark counter}} | {{:spark_counter:dsc02371.jpg?0x300|complete spark counter}} |
| |
**warning: the electricity meter I am presenting will only work for 1 phase 2 wires power distribution systems. I have a 3 phases 4 wires system and I am doing it wrong.** | **warning: the electricity meter I am presenting will only work for 1-phase 2-wire power distribution systems. I have a 3-phase 4-wire system and I was doing it wrong. This has been fixed with the [[spark abacus]].** |
| |
====== peacefair PZEM-004 ====== | ====== peacefair PZEM-004 ====== |
| |
The [[http://www.aliexpress.com/wholesale?catId=0&SearchText=peacefair|peacefair]] [[http://www.aliexpress.com/wholesale?catId=0&SearchText=peacefair+pzem-004|PZEM-004]] is a simple power meter ({{:spark_counter:peacefair_pzem-004_porduct.pdf|datasheet}}). | The [[https://www.aliexpress.com/store/1773456|peacefair]] [[https://www.aliexpress.com/store/product/PEACEFAIR-AC-100A-Digital-LCD-power-meter-power-energy-Volt-Ammeter-with-shell-4-led-panel/1773456_32320321777.html|PZEM-004]] is a simple power meter ({{:spark_counter:peacefair_pzem-004_porduct.pdf|datasheet}}). |
It: | It: |
* measures voltages 80-260 V AC | * measures voltages 80-260 V AC |
{{:spark_counter:dsc02328.jpg?0x200|PZEM-004 opto-couplers}} | {{:spark_counter:dsc02328.jpg?0x200|PZEM-004 opto-couplers}} |
| |
The meter also provides a UART port to query the measured values. | The meter also provides a UART port (using an XH-4P connector) to query the measured values. |
Two opto-couplers isolate the main power meter circuits from the TX and RX UART ports. | Two opto-couplers isolate the main power meter circuits from the TX and RX UART ports. |
| |
The {{:spark_counter:pzem-004.rtf|manual}} provides the corresponding commands, but lacks some information. | The {{:spark_counter:pzem-004.rtf|manual}} provides the corresponding commands, but lacks some information. |
The UART configurations is 9600 bps 8N1. | The UART configurations is 9600 bps 8N1. |
Before querying the measurements, you have to set an address to the device. | You have to set the device's address (i.e. C0 A8 01 01 = 192.168.1.1 in the examples) at least once using the 0xB4 command before querying measurements. |
Else it won't reply to the other commands. | This address will be saved so the next time (even after power down) you can directly query measurements using the address(es) set in the device(s). |
The manual uses an IP as address (192.168.1.1 = C0 A8 01 01). | All commands include the target device address and only the device with the specified address will respond, except for the command setting the address in the first place. |
No idea why you would set an IP for a UART address. | This allows to query multiple devices connected to the same UART port, one after another, when they have different addresses. |
| Note: the optocouplers on the Tx pin of the device have a pull-up resistor. Be sure to remove all but one when using multiple devices, else the low value of the signal will be too high for the receiver to decode the serial communication. |
| |
The commands use the following 7 bytes message format: | The commands use the following 7 bytes message format: |
I removed the pizeo-element to prevent this annoying sound. | I removed the pizeo-element to prevent this annoying sound. |
| |
{{:spark_counter:dsc02344.jpg?0x200|PZEM-004 board front}} | {{:spark_counter:pzem-004-01.jpg?0x200|PZEM-004 board front}} |
{{:spark_counter:dsc02325.jpg?0x200|PZEM-004 board back}} | {{:spark_counter:pzem-004-02.jpg?0x200|PZEM-004 board back (without buzzer)}} |
{{:spark_counter:dsc02327.jpg?0x200|PZEM-004 board IC}} | |
| |
The power meter uses a single chip solution, the [[http://www.sdicmicro.com/products.html?ic=SD3004|SDIC microelectronics SD3004]] ({{:spark_counter:sd3004_datasheet_v0.2c.pdf|datasheet}}). | The power meter uses a single chip solution, the [[http://www.sdicmicro.com/products.html?ic=SD3004|SDIC microelectronics SD3004]] ({{:spark_counter:sd3004_datasheet_v0.2c.pdf|datasheet}}). |
| |
USART to query the power meter, SPI to communicate with the nRF24L01+, and the nRF24L01+ protocol are implemented in C (using a lot of interrupts). | USART to query the power meter, SPI to communicate with the nRF24L01+, and the nRF24L01+ protocol are implemented in C (using a lot of interrupts). |
The source code is available [[https://git.cuvoodoo.info/kingkevin/spark_counter/tree/master|in this git]]. | The source code is available [[https://git.cuvoodoo.info/kingkevin/spark_counter/|in this git]]. |
I prefer directly writing in the registers as described in the [[http://www.atmel.com/images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet_Complete.pdf|datasheet]] rather than using an [[https://www.arduino.cc/en/Main/Software|abstract IDE]] and [[https://github.com/TMRh20/RF24|libraries]]. | I prefer directly writing in the registers as described in the [[http://www.atmel.com/images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet_Complete.pdf|datasheet]] rather than using an [[https://www.arduino.cc/en/Main/Software|abstract IDE]] and [[https://github.com/TMRh20/RF24|libraries]]. |
This way I learn how it works in details. | This way I learn how it works in details. |
| |
On the [[#ATmega328P|micro-controller]] side I implemented the complete SPI-based protocol using the [[https://www.nordicsemi.com/eng/content/download/2726/34069/file/nRF24L01P_Product_Specification_1_0.pdf|datasheet]]. | On the [[#ATmega328P|micro-controller]] side I implemented the complete SPI-based protocol using the [[https://www.nordicsemi.com/eng/content/download/2726/34069/file/nRF24L01P_Product_Specification_1_0.pdf|datasheet]]. |
The source code is available [[https://git.cuvoodoo.info/kingkevin/spark_counter/tree/master|in this git]]. | The source code is available [[https://git.cuvoodoo.info/kingkevin/spark_counter/|in this git]]. |
| |
On the [[#Raspberry Pi|computer]] side I use the [[https://github.com/TMRh20/RF24|RF24 library]]. | On the [[#Raspberry Pi|computer]] side I use the [[https://github.com/TMRh20/RF24|RF24 library]]. |
| |
To receive the measurements I used a [[#nRF24L01+|nRF24L01+ transceiver]] with the [[https://github.com/TMRh20/RF24|RF24 library]]. | To receive the measurements I used a [[#nRF24L01+|nRF24L01+ transceiver]] with the [[https://github.com/TMRh20/RF24|RF24 library]]. |
The source code is available [[https://git.cuvoodoo.info/kingkevin/spark_counter/tree/master|in this git]]. | The source code is available [[https://git.cuvoodoo.info/kingkevin/spark_counter/|in this git]]. |
| |
[[http://elinux.org/RPi_Low-level_peripherals#P1_Header|RPi]] - nRF24L01+ connection: | [[http://elinux.org/RPi_Low-level_peripherals#P1_Header|RPi]] - nRF24L01+ connection: |
{{:spark_counter:grafana.png?0x200|grafana visualization}} | {{:spark_counter:grafana.png?0x200|grafana visualization}} |
| |
====== Orange Pi ====== | |
| |
The [[http://www.orangepi.org/orangepipc/|Orange Pi PC]] is a cheap [[http://www.aliexpress.com/item/Orange-Pi-PC-ubuntu-linux-and-android-mini-PC-Beyond-and-Compatible-with-Raspberry-Pi-2/32448079125.html|$15 alternative]] to the Raspberry Pi 2. | |
This single board computer is based on an [[https://linux-sunxi.org/H3|Allwinner H3 (sun8iw7p1)]] SoC with ARM Cortex-A7 @ 1.2GHz (often over-clocked), and 1 GB of RAM. | |
| |
For this price the hardware is unbeatable, but the software support is really poor. | |
Also only Android supports the video hardware acceleration currently. | |
The official [[http://www.orangepi.org/downloadresources/|images]] are quite old (e.g. Android 4.2.2), but there are a bit newer images made available by the community: | |
* [[http://www.orangepi.org/orangepibbsen/forum.php?mod=viewthread&tid=755|Android]] (often ROMs from other Allwinner based products), with a weird [[http://www.orangepi.org/Docs/SDcardinstallation.html#Install_Android_OS_image|installation process]] | |
* various [[http://www.orangepi.org/orangepibbsen/forum.php?mod=viewthread&tid=342|linux]] distributions | |
Since I'll only use it as server I decided to go with debian jessie minimal. | |
| |
The pin header is the same as the [[http://www.raspberry-projects.com/pi/pi-hardware/raspberry-pi-2-model-b/rpi2-model-b-io-pins|40-pin Raspberry Pi]]. | |
The SPI port to connect the nRF24L01 is also available over ''/dev/spi''. | |
But else this board is not really worth spending time on. | |
| |
Also don't forget to put a heat sink on the CPU, else they will be slowed down quite often because of the high temperatures. | |