Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding mqtt logging on error. #74

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ enabled = false
server = 192.168.0.16
port = 1883
topic = solar/state
topic_error = solar/error
user =
password =

Expand Down
3 changes: 3 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def on_data_received(client, data):
def on_error(client, error):
logging.error(f"on_error: {error}")

errorTopic = config['mqtt']['topic_error']
data_logger.log_mqtt_bytopic(errorTopic, error)

# start client
if config['device']['type'] == 'RNG_CTRL':
RoverClient(config, on_data_received, on_error).connect()
Expand Down
19 changes: 13 additions & 6 deletions renogybt/DataLogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,23 @@ def log_remote(self, json_data):
logging.info("Log remote 200") if req.status_code == 200 else logging.error(f"Log remote error {req.status_code}")

def log_mqtt(self, json_data):
logging.info(f"mqtt logging")
logging.info(f"mqtt logging to default topic")
self.log_mqtt_bytopic(self.config['mqtt']['topic'], payload=json.dumps(json_data))

def log_mqtt_bytopic(self, topic, payload):
logging.info("mqtt logging to topic {}".format(topic))
user = self.config['mqtt']['user']
password = self.config['mqtt']['password']
auth = None if not user or not password else {"username": user, "password": password}

publish.single(
self.config['mqtt']['topic'], payload=json.dumps(json_data),
hostname=self.config['mqtt']['server'], port=self.config['mqtt'].getint('port'),
auth=auth, client_id="renogy-bt"
)
if topic == None:
logging.error("No topic was specified.")
else:
publish.single(
topic=topic, payload=payload,
hostname=self.config['mqtt']['server'], port=self.config['mqtt'].getint('port'),
auth=auth, client_id="renogy-bt"
)

def log_pvoutput(self, json_data):
date_time = datetime.now().strftime("d=%Y%m%d&t=%H:%M")
Expand Down