#!/usr/bin/env python3 # encoding: utf-8 # |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| # Copyright (C) 2017 Piotr Murawski # Niniejszy program jest wolnym oprogramowaniem; mozesz go # rozprowadzac dalej i/lub modyfikowac na warunkach Powszechnej # Licencji Publicznej GNU, wydanej przez Fundacje Wolnego # Oprogramowania - wedlug wersji 3-ciej tej Licencji lub ktorejs # z pozniejszych wersji. # Niniejszy program rozpowszechniany jest z nadzieja iz bedzie on # uzyteczny - jednak BEZ JAKIEJKOLWIEK GWARANCJI, nawet domyslnej # gwarancji PRZYDATNOSCI HANDLOWEJ albo PRZYDATNOSCI DO OKRESLONYCH # ZASTOSOWAN. W celu uzyskania blizszych informacji - Powszechna # Licencja Publiczna GNU. # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # Z pewnoscia wraz z niniejszym programem otrzymales tez egzemplarz # Powszechnej Licencji Publicznej GNU (GNU General Public License); # jesli nie - napisz do Free Software Foundation, Inc., 675 Mass Ave, # Cambridge, MA 02139, USA. # Powszechna Licencja GNU dostepna jest rowniez na stronie: # http://www.gnu.org/licenses/licenses.html # nieoficjalne polskie tlumaczenie na # http://www.gnu.org.pl # |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| import sys import os import energymeter import math import struct import paho.mqtt.publish as publish #publish.single("paho/test/single", "boo", hostname="mqtt.eclipse.org") DEVICE_0="/dev/ttyUSB0" ADDR=2 def mem2float(reg1, reg2): # found on beloved "satckoverflow" raw = struct.pack('>HH', reg1, reg2) return struct.unpack('>f', raw)[0] def float2mem(flo): # found on beloved "satckoverflow" raw = struct.pack('>f', flo) regsy=struct.unpack('>HH', raw) return [regsy[0], regsy[1]] print("testing @ ", DEVICE_0) for iter in range(50): try: print("\n--- Iteration %d ---" % (iter+1)) testEmeter=energymeter.testPort(DEVICE_0, ADDR) #mbcli = createClient(portDevice, adres) testEmeter = True if testEmeter: print("meter ok") mbcli = energymeter.startClient(DEVICE_0, ADDR) #print(regs) regs=energymeter.readRegs(mbcli, 0xE,6) # ORNO print() print("voltage L1 ...", round(mem2float(regs[0], regs[1]), 1)) sU1 = round(mem2float(regs[0], regs[1]), 1) publish.single("/energy/orno/U1", round(mem2float(regs[0], regs[1]),1), hostname="10.1.1.1") print("voltage L2 ...", round(mem2float(regs[2], regs[3]), 1)) sU2 = round(mem2float(regs[2], regs[3]), 1) publish.single("/energy/orno/U2", round(mem2float(regs[2], regs[3]),1), hostname="10.1.1.1") print("voltage L3 ...", round(mem2float(regs[4], regs[5]), 1)) sU3 = round(mem2float(regs[4], regs[5]), 1) publish.single("/energy/orno/U3", round(mem2float(regs[4], regs[5]),1), hostname="10.1.1.1") sU = "{\"U1\":%f,\"U2\":%f,\"U3\":%f}" % (sU1, sU2, sU3) print(sU) publish.single("/energy/orno/U", sU, hostname="10.1.1.1") regs=energymeter.readRegs(mbcli, 0x16,6) print() print("current L1 ...", round(mem2float(regs[0], regs[1]), 2)) sI1 = round(mem2float(regs[0], regs[1]), 2) publish.single("/energy/orno/I1", round(mem2float(regs[0], regs[1]),2), hostname="10.1.1.1") print("current L2 ...", round(mem2float(regs[2], regs[3]), 2)) sI2 = round(mem2float(regs[2], regs[3]), 2) publish.single("/energy/orno/I2", round(mem2float(regs[2], regs[3]),2), hostname="10.1.1.1") print("current L3 ...", round(mem2float(regs[4], regs[5]), 2)) sI3 = round(mem2float(regs[4], regs[5]), 2) publish.single("/energy/orno/I3", round(mem2float(regs[4], regs[5]),2), hostname="10.1.1.1") sI = "{\"I1\":%f,\"I2\":%f,\"I3\":%f}" % (sI1, sI2, sI3) print(sI) publish.single("/energy/orno/I", sI, hostname="10.1.1.1") regs=energymeter.readRegs(mbcli, 0x1C,8) print() print("power total ..", round(mem2float(regs[0], regs[1]), 3)) sP_Tot = round(mem2float(regs[0], regs[1]), 3) publish.single("/energy/orno/P_Tot", round(mem2float(regs[0], regs[1]),3), hostname="10.1.1.1") print("power L1 ...", round(mem2float(regs[2], regs[3]), 3)) sP1 = round(mem2float(regs[2], regs[3]), 3) publish.single("/energy/orno/P1", round(mem2float(regs[2], regs[3]),3), hostname="10.1.1.1") print("power L2 ...", round(mem2float(regs[4], regs[5]), 3)) sP2 = round(mem2float(regs[4], regs[5]), 3) publish.single("/energy/orno/P2", round(mem2float(regs[4], regs[5]),3), hostname="10.1.1.1") print("power L3 ...", round(mem2float(regs[6], regs[7]), 3)) sP3 = round(mem2float(regs[6], regs[7]), 3) publish.single("/energy/orno/P3", round(mem2float(regs[6], regs[7]),3), hostname="10.1.1.1") sP = "{\"P_Tot\":%f,\"P1\":%f,\"P2\":%f,\"P3\":%f}" % (sP_Tot, sP1, sP2, sP3) print(sP) publish.single("/energy/orno/P", sP, hostname="10.1.1.1") regs=energymeter.readRegs(mbcli, 0x100,8) print() print("energy total..", round(mem2float(regs[0], regs[1]), 3)) sW_Tot = round(mem2float(regs[0], regs[1]), 3) publish.single("/energy/orno/W_Tot", round(mem2float(regs[0], regs[1]),3), hostname="10.1.1.1") print("energy L1 ...", round(mem2float(regs[2], regs[3]), 3)) sW1 = round(mem2float(regs[2], regs[3]), 3) publish.single("/energy/orno/W1", round(mem2float(regs[2], regs[3]),3), hostname="10.1.1.1") print("energy L2 ...", round(mem2float(regs[4], regs[5]), 3)) sW2 = round(mem2float(regs[4], regs[5]), 3) publish.single("/energy/orno/W2", round(mem2float(regs[4], regs[5]),3), hostname="10.1.1.1") print("energy L3 ...", round(mem2float(regs[6], regs[7]), 3)) sW3 = round(mem2float(regs[6], regs[7]), 3) publish.single("/energy/orno/W3", round(mem2float(regs[6], regs[7]), 3), hostname="10.1.1.1") sW = "{\"W_Tot\":%f,\"W1\":%f,\"W2\":%f,\"W3\":%f}" % (sW_Tot, sW1, sW2, sW3) print(sW) publish.single("/energy/orno/W", sW, hostname="10.1.1.1") regs=energymeter.readRegs(mbcli, 0x14,2) print() print("net frequency.", round(mem2float(regs[0], regs[1]), 4)) sFreq = round(mem2float(regs[0], regs[1]), 4) publish.single("/energy/orno/Hz", round(mem2float(regs[0], regs[1]),4), hostname="10.1.1.1") else: print("ERROR: meter not found") except Exception as e: print('Iteration %d error:' % (iter+1), e) finally: import time time.sleep(2)