Permalink
Browse files

Taken variables out into a config file

  • Loading branch information...
dpoulson committed Oct 22, 2017
1 parent 166986b commit f5c1053717726421978f14c6d51567dad19ddfb3
Showing with 55 additions and 14 deletions.
  1. +2 −1 .gitignore
  2. +1 −1 README.md
  3. +8 −0 config.ini-default
  4. +44 −12 ginlong-listen.py
View
@@ -1,3 +1,4 @@
test
rawlog
.swp
*.swp
*.ini
View
@@ -25,7 +25,7 @@ You will also need a running MQTT server.
1. Log into the monitoring device, and configure the second IP option to point to the server that this daemon is running on. (Daemon defaults to port 9999)
2. Make sure that the MQTT settings are correct in the daemon.
3. Start the daemon
4. Add the following to your OpenHAB items
4. Add the following to your OpenHAB items (Replace XXXXXXXXXX with the serial number of your inverter)
```
// Environmentals
Number Solis_Temp "Temperature [%.2f °C]" (Solis) { mqtt="<[mymosquitto:ginlong/XXXXXXXXXX/temp:state:default" }
View
@@ -0,0 +1,8 @@
[DEFAULT]
listen_address = 0.0.0.0
listen_port = 9999
[MQTT]
client_id = ginlong
mqtt_server = localhost
mqtt_port = 1883
View
@@ -1,20 +1,49 @@
#!/usr/bin/python
#!/usr/bin/env python
#===============================================================================
# Copyright (C) 2017 Darren Poulson
#
# This file is part of ginlong-mqtt.
#
# R2_Control 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.
#
# R2_Control 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.
#
# You should have received a copy of the GNU General Public License
# along with ginlong-mqtt. If not, see <http://www.gnu.org/licenses/>.
#===============================================================================
import paho.mqtt.publish as publish
import socket
import binascii
import time
import sys
import string
import ConfigParser
import io
with open("config.ini") as f:
sample_config = f.read()
config = ConfigParser.RawConfigParser(allow_no_value=True)
config.readfp(io.BytesIO(sample_config))
###########################
# Variables
listen_address = "0.0.0.0" # What address to listen to (0.0.0.0 means it will listen on all addresses)
listen_port = 9999 # Port to listen on
client_id = "ginlong" # MQTT Client ID
mqtt_server = "localhost" # MQTT Address
mqtt_port = 1833 # MQTT Port
listen_address = config.get('DEFAULT', 'listen_address') # What address to listen to (0.0.0.0 means it will listen on all addresses)
listen_port = int(config.get('DEFAULT', 'listen_port')) # Port to listen on
client_id = config.get('MQTT', 'client_id') # MQTT Client ID
mqtt_server = config.get('MQTT', 'mqtt_server') # MQTT Address
mqtt_port = int(config.get('MQTT', 'mqtt_port')) # MQTT Port
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
@@ -23,7 +52,8 @@
while True:
# Wait for a connection
print >>sys.stderr, 'waiting for a connection'
if __debug__:
print 'waiting for a connection'
conn,addr = sock.accept()
try:
#print >>sys.stderr, 'connection from', addr
@@ -33,11 +63,12 @@
if(len(hexdata) == 270):
timestamp = (time.strftime("%F %H:%M")) # get date time
print >>sys.stderr, 'Hex data: ', hexdata
msgs = []
serial = binascii.unhexlify(str(hexdata[70:102])) # Serial number is used for MQTT path, allowing multiple inverters to connect to a single instance
print "Serial %s" % serial
print "Length %s" % len(hexdata)
if __debug__:
print 'Hex data: %s' % hexdata
print "Serial %s" % serial
print "Length %s" % len(hexdata)
mqtt_topic = ''.join([client_id, "/", serial, "/"]) # Create the topic base using the client_id and serial number
if __debug__:
print >>sys.stderr, 'MQTT Topic: ', mqtt_topic
@@ -132,10 +163,11 @@
print >>sys.stderr, 'kwhtotal: ', kwhtotal
msgs.append((mqtt_topic + "kwhtotal", kwhtotal, 0, False))
#publish.multiple(msgs, hostname="localhost")
publish.multiple(msgs, hostname=mqtt_server)
file = open("rawlog",'a')
file.write(timestamp + ' ' + hexdata + '\n')
file.close()
finally:
print "Finally"
if __debug__:
print "Finally"

0 comments on commit f5c1053

Please sign in to comment.