Having trouble using the mqtt broker to publish data to genmon. #1129
Replies: 1 comment
-
I am assuming you are using the MQTT Sensor Input add on to get the data into genmon. They way you describe it is the way I except it to work. MQTT brokers do not keep up with state of a sensor being read or not as it can be read by multiple endpoints. Most MQTT broker implementations are optimized to only transmit to end points that subscribe to a sensor topic (in this case genmon) if the sensor has changed. Your code publishes every second, but genmon will only see the updates that change. This minimizes network traffic. The actual sensor should be responsible for the data, otherwise any endpoint. displaying it would need to know about default values (e.g. zero for oil pressure and zero for a temperature sensor indicate wildly different conditions). Genmon could add a feature that resorts to a default value, but that would be unique for each sensor potentially. Genmon could also remove the sensor if it does not report after a period of time, but that would require a reload of the gauge array and I am not liking the downstream ramifications of that to the maintenance of the code. I think a better solution would be to add two things to your code.
For the exception handler:
For the signal handler add these lines to your code:
After the initialization but before the while loop call this:
Then add a function named SignalClose
I suspect that this will solve most of your issues. Let me know if you have any questions. |
Beta Was this translation helpful? Give feedback.
-
Checklist
Version
Version V1.19.03
Have you submitted your log files from the About page? Note that outbound email must be working for logs to be submitted.
I have not submitted logs
What is/are your question(s)?
Tom and I are developing an add on to genmon to read the oil pressure from a bellows sensor using an ADC and uploading it to a mqtt broker to be displayed in genmon as a gauge. I have included a copy of the program and all is working fine except an issue with persistent data being displayed.
What is happening is if our program is running data is being pushed to the mqtt server and it is being displayed. Now if for some reason the program crashes or is killed, the last data pushed to the mqtt server continue to be displayed. This is misleading, say the pressure was 35 psi and all is well, if the program stops the last value continue to be displayed until the program is restarted.
I am new to using a mqtt broker and I am assuming that after a value is read it is marked and should not be read again. Am I missing something when pushing my message to the mqtt broker so that it is not persistent, or is there an internal problem with genmon? Could genmon be setting a variable and not clearing it until a new value is read.
Any help would be appreciated.
Joe
Additional information
version_2.4 Removed a lot of extra code and made it more readable.
SPDX-FileCopyrightText: 2023 Joseph Sundermier, Thomas Crist
SPDX-License-Identifier: MIT
This program is a supplement to the genmon program. Once started it will:
Write out the the date and time it was started to a userdefined.json file to be displayed on the monitor page
Regullary read voltage drop across a transducer using a MCP3008 ADC over the SPI bus.
Convert the voltage to an oil pressure using an exponential fit.
Publish it to a mqtt server to be used by the genmon program to be display as a gauge on the status page.
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
import paho.mqtt.client as mqtt
import json
import os
import time
from datetime import datetime
#mqttBroker = "localhost"
mqttBroker = "127.0.0.1"
mqttTopic = "Oil pressure"
client = mqtt.Client(mqttTopic)
client.connect(mqttBroker)
Software SPI configuration,these are my pins convential is (clk=11, cs=1, miso=9, mosi=10), these can be changed to meet your requirements.
mcp = Adafruit_MCP3008.MCP3008(clk=11, cs=8, miso=9, mosi=10)
Function to read the ADC
def readADC():
Read ADC in difference mode using pins 0 and 1, then convert to a voltage.
value = mcp.read_adc_difference(0)
voltage = value * 3.3 / 1027
Convert voltage to pressure using a power function a=261.27 and b=1.7026.
pressure = 261.27 * pow(voltage, 1.7026)
pressure = int(pressure)
return pressure
Function to get the current date and time.
def dateTime():
now = datetime.now()
now = now.strftime("%A %B %d, %Y %H:%M:%S")
return now
Modifies or creates the userdefined.json file with the last started time, run only once..
data = {'Oil Pressure program last started': dateTime()}
with open('userdefined.json', 'w') as f:
json.dump(data, f)
f.close()
Main loop, keep publishing current oil pressure reading to mqtt broker
while (True):
client.publish("Oil pressure", readADC())
time.sleep(1)
Beta Was this translation helpful? Give feedback.
All reactions