Skip to content

Instantly share code, notes, and snippets.

@AgustinPelaez
Created June 5, 2015 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save AgustinPelaez/09ca0b61f0826fa2d3c4 to your computer and use it in GitHub Desktop.
Save AgustinPelaez/09ca0b61f0826fa2d3c4 to your computer and use it in GitHub Desktop.
A Python Script to read a Z Wave Home Energy Meter using the library: http://github.com/OpenZWave/python-openzwave
#!/usr/bin/env python
import logging
import ubidots
import sys, os
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('openzwave')
import openzwave
from openzwave.node import ZWaveNode
from openzwave.value import ZWaveValue
from openzwave.scene import ZWaveScene
from openzwave.controller import ZWaveController
from openzwave.network import ZWaveNetwork
from openzwave.option import ZWaveOption
import time
from louie import dispatcher, All
device="/dev/ttyUSB0"
sniff=300.0
#Define some manager options
options = ZWaveOption(device, \
config_path="../openzwave/config", \
user_path=".", cmd_line="")
options.set_log_file("OZW_Log.log")
options.set_append_log_file(False)
options.set_console_output(False)
options.set_save_log_level('Debug')
options.set_logging(True)
options.lock()
# DEFINING SOME FUNCTIONS:
def louie_network_started(network):
print("Louie network started: homeid %0.8x - %d nodes were found." % \
(network.home_id, network.nodes_count))
node_present = 1
def louie_network_failed(network):
print("Louie network failed.")
def louie_network_ready(network):
print("Louie network ready : %d nodes were found." % network.nodes_count)
print("My controller is : %s" % network.controller)
for node in network.nodes:
if "Energy Meter" in network.nodes[node].product_name:
meter_name = network.nodes[node].product_name + " (Node " + str(node) + ")"
if network.nodes[node].isReady:
ds = None
for cur_ds in api.get_datasources():
if cur_ds.name == meter_name:
ds = cur_ds
break
if ds is None:
ds = api.create_datasource({"name": meter_name})
sensors = network.nodes[node].get_sensors()
prepare_node(meter_name, sensors)
# If node is ready then connect to dispatchers of updates
dispatcher.connect(louie_node_update, ZWaveNetwork.SIGNAL_NODE)
dispatcher.connect(louie_value_update, ZWaveNetwork.SIGNAL_VALUE)
else:
print "Energy Meter found but not active."
reset_timeout =+ 1
else:
print "Energy Meter not found"
reset_timeout =+ 1
def louie_node_update(network, node):
print('Louie node update from node : %s.' % node)
def louie_value_update(network, node, value):
#print('Louie value update') #......................................., value : %s.' % value)
print "Value update received: " + value
if "Energy" in value.label or "Power" in value.label:
print "Sending data to Ubidots..."
vars[value.label].save_value({"value": value.data})
else:
print "Ignoring sensor update"
#Ubidots Functions and Declarations
def get_var_by_name(var_name, ds):
"""Search for a variable in a datasource. If found, returns the variable.
If not found, returns None."""
for var in ds.get_variables():
if var.name == var_name:
return var
return None
def prepare_node(meter_name, sensors):
print('Setting up HEM device in Ubidots...')
# Search for variables in the data source with names matching
# the ones we will update. If they don't exist, create them.
count = 0
for val in sensors:
print "Reading variable " + str(count)
if count == 2:
prepare_variable("Active ", sensors[val], ds)
elif count == 3:
prepare_variable("Reactive ", sensors[val], ds)
elif count == 4:
prepare_variable("Apparent ", sensors[val], ds)
elif count == 13:
prepare_variable("Active ", sensors[val], ds)
elif count == 15:
prepare_variable("Apparent ", sensors[val], ds)
elif count == 16:
prepare_variable("Reactive ", sensors[val], ds)
else:
print "Ignoring variable " + sensors[val].label
sensors[val].enable_poll(0)
count = count +1
def prepare_variable(preffix, sensor, ds):
sensor.enable_poll(1)
sensor.label = preffix + sensor.label
ubi_var = get_var_by_name(sensor.label, ds)
if ubi_var is None:
ubi_var = ds.create_variable({"name": sensor.label, "unit": sensor.units})
vars[sensor.label] = ubi_var
print "Done preparing " + sensor.label
# MAIN LOOP:
#Create a Ubidots connection object
api = ubidots.ApiClient("UBIDOTS-API-KEY-HERE");
#Create a network object
network = ZWaveNetwork(options, autostart=False)
network.set_poll_interval(5000, False)
#We connect to the louie dispatcher
dispatcher.connect(louie_network_started, ZWaveNetwork.SIGNAL_NETWORK_STARTED)
dispatcher.connect(louie_network_failed, ZWaveNetwork.SIGNAL_NETWORK_FAILED)
dispatcher.connect(louie_network_ready, ZWaveNetwork.SIGNAL_NETWORK_READY)
node_present = 0
for i in range(0,10):
network.start()
#We wait for the network.
print "***** Waiting for network to become ready : "
for j in range(0,50):
if network.state >= network.STATE_READY:
print "***** Network is ready"
break
else:
sys.stdout.write(".")
sys.stdout.flush()
time.sleep(1.0)
#time.sleep(5.0)
# Infinite loop waiting for updates...
while(1):
print "Waiting for data from the network..."
time.sleep(1)
print "Stopping network and exiting..."
network.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment