-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpower_monitor_rf24.py
70 lines (51 loc) · 1.87 KB
/
power_monitor_rf24.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
# receive values from CS5460A power monitor via NRF24L01
# may need to run as sudo
# see https://github.com/zerog2k/power_meter_cs5460a for arduino transmitter code
import time as time
from RF24 import *
import RPi.GPIO as GPIO
import binascii
import struct
from datetime import datetime, date
MSGTYPES = [ "MSG_POWER_METER" ]
irq_gpio_pin = None
########### USER CONFIGURATION ###########
# See https://github.com/TMRh20/RF24/blob/master/RPi/pyRF24/readme.md
# CE Pin, CSN Pin, SPI Speed
#RPi B+
# Setup for GPIO 22 CE and CE0 CSN for RPi B+ with SPI Speed @ 8Mhz
radio = RF24(RPI_BPLUS_GPIO_J8_15, RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_1MHZ)
# Setup for connected IRQ pin, GPIO 24 on RPi B+; uncomment to activate
#irq_gpio_pin = RPI_BPLUS_GPIO_J8_18
#irq_gpio_pin = 24
pipes = [ 0x4A454E5300 ]
radio.begin()
radio.setChannel( 1 )
# set datarate
radio.setDataRate( RF24_250KBPS )
#radio.setPALevel(RF24_PA_MAX)
radio.enableDynamicPayloads()
radio.printDetails()
radio.openReadingPipe(1, pipes[0])
radio.startListening()
dt = datetime
pipenum = -1
# forever loop
while True:
try:
have_data, pipenum = radio.available_pipe()
if have_data:
len = radio.getDynamicPayloadSize()
if len > 0:
msgtype = radio.read(1);
receive_payload = radio.read(len)
if msgtype[0] == MSGTYPES.index("MSG_POWER_METER"):
(voltage, current, true_power, power_factor) = struct.unpack_from("ffff", receive_payload, 1)
print "%s pipe: %d, msgtype: %s, voltage: %0.1f, current: %0.2f, true_power: %0.1f, PF: %0.2f" \
% (dt.now(), pipenum, MSGTYPES[msgtype[0]], voltage, current, true_power, power_factor)
else:
print "%s got: pipe=%d size=%s raw=%s" % (dt.now(), pipenum, len, binascii.hexlify(receive_payload))
time.sleep(1)
except Exception as e:
print e.strerror