Skip to content
Open
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
14 changes: 12 additions & 2 deletions i2c2mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,29 @@ def getI2cSensors(devices):
help='The MQTT topic on which to publish the message (if it was a success).')
parser.add_argument('-T', '--topic-error', dest='topicError', action="store", default="error/sensor/i2c", metavar="TOPIC",
help='The MQTT topic on which to publish the message (if it wasn\'t a success).')
parser.add_argument('-u', '--username', metavar="USERNAME",
help='Username to log into the mqtt broker')
parser.add_argument('-p', '--password', metavar="PASSWORD",
help='Password to log into the mqtt broker')
parser.add_argument('-P', '--port', default=1883, metavar="PORT",
help='Port for the mqtt broker')
parser.add_argument('-v', '--verbose', dest='verbose', action="store_true", default=False,
help='Enable debug messages.')

args = parser.parse_args()
verbose = args.verbose;

mqtt_auth = None
if args.username:
mqtt_auth = {'username': args.username, 'password': args.password}

status, data = getI2cSensors(args.devices)
jsonString = json.dumps(data)
if status:
debug("Success with message (for current readings) <{0}>".format(jsonString))
if not args.dryRun:
publish.single(args.topic, jsonString, hostname=args.host)
publish.single(args.topic, jsonString, hostname=args.host, auth=mqtt_auth, port=args.port)
else:
debug("Failure with message <{0}>".format(jsonString))
if not args.dryRun:
publish.single(args.topicError, jsonString, hostname=args.host)
publish.single(args.topicError, jsonString, hostname=args.host, auth=mqtt_auth, port=args.port)