MQTTany - GPIO and more over MQTT

I recommend using MQTTfx to monitor MQTT traffic, it will help you understand topics in MQTTany at first and to make sure things are working.

Assuming you are using all default settings, the topic would be HOSTNAME/gpio/pintest. You will need to replace HOSTNAME with the actual hostname of the Raspberry Pi (raspberrypi on Raspbian, openhabian on openhabian).

Ok MQTTany is working. I use mosquitto_sub. But it appears that I have a different problem. I use a manual pushbutton with a pullup resistor. So Pin7 → Pullupresistor → VCC and Pin7 → Pushbutton → GND. In mqttany I have configured resistor: ‘pullup’.

Pressing the button occasionally (very very rarely, 1 out of 15 cases) provides me an “ON” message. Never I receive an OFF message. Mostly, I receive nothing at all. Why that? Is this a problem with the RaspberryPi4, broken?

raspi-gpio get 7
GPIO 7: level=1 fsel=0 func=INPUT pull=UP

How to proceed?

Thank you!!

Interesting, someone else reported similar behaviour but never got back to me with details. Let’s move this to Discussions on GitHub. Please make sure you are running the latest release of MQTTany, delete your log file, and start it with the -vv option. Press the button a few times then stop MQTTany (Ctrl+c) and attach your log file to the discussion.

OK, Problem solved. There was a parameter missing (respect the spaces!!!)

digital:
  interrupt: both

and the Pin number was wrong. The SoC numbering is default in MQTTany, so use the SoC numbers.

1 Like

Hi, I followed the instructions to install the program but it fails to start as a service.
Can you pls guide me how to debug?

sudo systemctl status mqttany
● mqttany.service - MQTTany service
   Loaded: loaded (/etc/systemd/system/mqttany.service; enabled; vendor preset: enabled)
   Active: activating (auto-restart) (Result: exit-code) since Sun 2021-08-01 02:03:12 CEST; 7s ago
  Process: 3308 ExecStart=/usr/bin/python3 mqttany.py (code=exited, status=1/FAILURE)
 Main PID: 3308 (code=exited, status=1/FAILURE)

running the python script I get:

openhabian@openHABianDevice:/opt/mqttany/mqttany $ python3 mqttany.py
2021-08-04 00:45:51,716 [INFO ] [core                    ] MQTTany 0.14.3 starting
2021-08-04 00:45:51,790 [INFO ] [core.gpio               ] Detected board: RASPBERRY_PI_4B
2021-08-04 00:45:51,928 [INFO ] [core                    ] Module 'mqtt' started successfully
2021-08-04 00:45:51,948 [ERROR] [mqtt                    ] Connection Refused: not authorised. (5)
2021-08-04 00:45:52,048 [INFO ] [core                    ] Module 'mqtt' unloaded
2021-08-04 00:45:52,936 [WARN ] [core                    ] MQTTany exiting with errors

The reason it won’t run as a service is the same as why it exits when you run it manually. Your MQTT server is refusing the connection.

Does your server require a username and password? Does your server require SSL authentication, if so that is not yet supported.

It requires username and password but not SSL. It is not clear for me if only SSL is not supported or also username and password are not supported?

SSL is not supported but user/pass is. The MQTT page in the docs shows you all available options, including username and password.

Ivan,

Got it all installed on my raspberry pi and when i try to run it i get this.
Something simple I am sure I did on the install?

Thanks

Looks like the hostname for you MQTT server is invalid. What is it set to in the config?

Hey Ivan

I’ll get back to you when I have my stuff setup again. Had to evacuate due to a wildfire yesterday. Hopefully be home soon!

Thanks

1 Like

Hi,

I hadn’t got much time recently so I just tried installing the program again.
When I try to run it manually the program exits:

openhabian@openHABianDevice:/opt/mqttany/mqttany $ python3 mqttany.py -v /etc/mqttany/mqttany.yml
2021-09-26 02:18:14,231 [INFO ] [core    ] [core                    ] MQTTany 0.14.3 starting
2021-09-26 02:18:14,304 [INFO ] [core    ] [core.gpio               ] Detected board: RASPBERRY_PI_4B
2021-09-26 02:18:14,305 [DEBUG] [core    ] [core.gpio               ] Detected GPIO character device
2021-09-26 02:18:14,305 [DEBUG] [core    ] [core.gpio               ] Detected sysfs GPIO interface
2021-09-26 02:18:14,306 [DEBUG] [core    ] [config                  ] Loading config file
2021-09-26 02:18:14,311 [ERROR] [core    ] [config                  ] Config file contains errors
2021-09-26 02:18:14,311 [ERROR] [core    ] [config                  ]

Traceback (most recent call last):
yaml.scanner.ScannerError: while scanning a simple key
  in "/etc/mqttany/mqttany.yml", line 28, column 3
could not find expected ':'
  in "/etc/mqttany/mqttany.yml", line 29, column 3

2021-09-26 02:18:14,312 [ERROR] [core    ] [config                  ] Config file does not specify a version
2021-09-26 02:18:14,312 [ERROR] [core    ] [config                  ] This version of MQTTany requires a minimum config file version of '1.0'
2021-09-26 02:18:14,415 [DEBUG] [core    ] [core                    ] Received exit request from core
2021-09-26 02:18:14,416 [WARN ] [core    ] [core                    ] MQTTany exiting with errors

My config file in etc/mqttany/ is as follows (I didnt configure the sensor yet, trying to establish the connection first):

######## Configuration for MQTTany ########

# All optional values are commented out and default values are shown



### Config file version
version:  '1.0'


######## MQTT Module ########
mqtt:

  ### Broker Host
  # can be either IPv4 address or hostname
  host: '192.168.4.227'

  ### Broker Port
  # port to connect to on the host
  port: 1883

  ### Client ID
  # Substitutions:
  # - {hostname} = the computer's hostname
  client id: 'MQTTAny'

  ### Login
  username:'xxxxxx'
  password:'xxxx'

  ### Session Options
  #qos: 0
  #retain: false

  ### Topics
  # Substitutions:
  # - {hostname} = the computer's hostname
  # - {client_id} = the mqtt 'client id'
  # topics are composed as `{root topic}/{node id}/{property id}`
  #root topic: '{client_id}' # Root topic
  # last will topic becomes `{root topic}/{lwt topic}`
  #lwt topic: 'lwt' # Last Will topic

  ### Heartbeat
  # Interval in seconds to publish status messages
  #heartbeat interval: 300


######## GPIO Module ########
#gpio:

  ### Pin Numbering Mode
  #mode: 'SOC'

  ### Polling Interval
  #polling interval: 60 # interval in seconds to poll all pins

  ### Interrupt Debounce
  #debounce: 50 # in milliseconds

  ### Single Pin Configuration
  #pin-id:

    ### Pin Number
    #pin:

    ### Pin Name
    # Subsitutions:
    # - {pin} = pin number
    # - {pin:02d} = pin number padded with 0's to always be 2 numbers
    # - {pin_id} = name of this section
    #name: '{pin_id}'

    ### Pin Mode
    #pin mode: 'input' # can be 'input' or 'output'

    ### Pin Resistor
    #resistor: 'off' # can be 'pullup', 'pulldown', or 'off'

    ### Pin Type Specifc Options
    # Each pin type will have some options specific to it,
    # consult the documentation for details.

  ### Multiple Pin Configuration
  # alternatively a list of pins can be given.
  # actual id's will be '{batch-id}-{index + first index}'
  # all options from a single pin configuration apply, except those shown below
  #batch-id:

    ### Pin Name
    # a single name may be given, or a list of names the same length as the
    # list of pins can be provided. if a single name is given then actual names will
    # be '{name} {index + first index}'
    # Subsitutions:
    # - {pin} = pin number
    # - {pin:02d} = pin number padded with 0's to always be 2 numbers
    # - {pin_id} = name of this section
    # - {index} = the pin's index in the list of pins plus 'first index'
    #name: '{pin_id}'

    ### Pin Numbers
    #pin: []

    ### First Index
    # this number will be added to 'index' when substituting '{index}' in id's and names
    #first index: 0



######## I2C Module ########
#i2c:

  ### Polling Interval
  #polling interval: 60 # interval in seconds to poll all pins

  ### Device Configuration
  # !! this id is used as the node so it must not match any other node or module name !!
  #device-id:

    ### Device Friendly Name
    # Friendly name used in logging etc
    # Substitutions:
    # - {device_id} = name of device's section in config file
    # - {address:02x} = device address in hex (ex. '20')
    # - {device} = device identifier
    #name: '{device_id}'

    ### Device Type
    # Device identifier, see the wiki for a list of supported devices
    #device: ''

    ### I2C Bus ID
    # ID of the I2C bus to use on your SBC
    # ex. Raspberry Pi 3 this will probably be 1
    # Can also specify full path ex. '/dev/i2c-1'
    #bus:

    ### Device Address
    # 7-bit hex address of the device (ex. 0x20)
    # this can also be a base-10 integer
    #address: 0x

    ### Device Specific Options
    # Each device type will have some options specific to it
    # consult the wiki page for the device for details.



######## OneWire Module ########
#onewire:

  ### Bus Interface
  #bus: 'w1'

  ### Polling Interval
  #polling interval: 60 # interval in seconds to poll all devices

  ### Bus Scan
  # Scan bus for any unconfigured devices and add them
  #bus scan: false

  ### Device Configuration
  #device-id:

    ### Device Name
    # Substitutions:
    # - {device_id} = will be replaced with the device definition section name
    # - {device_type} = will be replaced with the device type (ex. `DS18B20`)
    # - {address} = will be replaced with the device's **FULL 8 BYTE ADDRESS**
    #name: '{device_id}'

    ### Device Address
    # The device's address, can be 7 or 8 bytes, can use w1 style `xx-xxxxxxxxxxxx`
    #address: '' # **MUST BE IN SINGLE QUOTES `''`**

    ### Device Specific Options
    # Each device type will have some options specific to it
    # consult the wiki page for the device for details.

  ### Multiple Device Configuration
  #batch-id:

    ### Device Name
    # a single name may be given, or a list of names the same length as the
    # list of pins can be provided. if a single name is given then actual names will
    # be '{name} {index + first index}'
    # Substitutions:
    # - {device_id} = will be replaced with the device definition section name
    # - {device_type} = will be replaced with the device type (ex. `DS18B20`)
    # - {address} = will be replaced with the device's **FULL 8 BYTE ADDRESS**
    # - {index} = will be replaced with the device's index in the list of addresses
    #name: '{device_id}'

    ### Device Address
    # List of device addresses, can be 7 or 8 bytes, can use w1 style `xx-xxxxxxxxxxxx`
    #address: ['', ''] # **MUST BE IN SINGLE QUOTES `''`**

    ### First Index
    # this number will be added to 'index' when substituting '{index}' in id's and names
    #first index: 0



######## LED Module ########
#led:

  ### Animation Directory
  # A directory or list of directories to look for additional animations in
  #anim dir: []

  ### Startup Animation
  # Animation to play when MQTTany loads
  #anim startup: 'test.array'

  ### Array Configuration
  #array-id:

    ### Array Name
    # Substitutions:
    # - {array_id} = will be replaced with the array definition section name
    #name: '{array_id}'

    ### Output
    # The interface to use for outputting data
    # Available options are 'rpi' or 'sacn'
    #output: ''

    ### Pixel Count
    # The number of pixels in your array (not necessarily LEDs, see next option)
    #count:

    ### LEDs per Pixel
    # Allows you to have more than one LED handled as a single pixel.
    # Ex. A bulb with 3 LEDs in it can be treated as a single pixel in the array.
    # Total number of LEDs in the array should be 'count' times 'leds per pixel'
    #leds per pixel: 1

    ### Initial Brightness
    # The initial brightness of the array, can be 0-255
    #brightness: 255

    ### Color Order
    # The byte order for the color data sent to the LEDs.
    # See the 'test.order' animation for how to determine this
    #color order: '{default}'

    ### Animation Frame Rate
    # Frame rate for animations, you may need to decrease this for
    # longer arrays or certain interface types.
    #anim fps: 60

    ### Interface Specific Options
    # Each interface may have some options specific to it
    # consult the wiki page for the interface for details.



######## XSET Module ########
#xset:

  ### Default Display
  # Display option to use when commands do not specify a display.
  # Omitting this option will omit the '-display' option from the call to xset
  #default display: None

  ### Startup Commands
  # A list of commands to run when the module loads.
  # See the documentation on sending commands for formatting.
  # If none are desired, use an empty list as shown below.
  #startup commands: []

Any ideas?

@DarkoG I do not have any ideas… Config file looks ok but the yaml reader says it’s no good.

Maybe try renaming your current config and copying the current default config file to etc/mqttany in case there are errors in your current one. Add only your broker information to the mqtt section and test to see if it will run.

Hi, I finally got time to work on this again and I found out there was no space between the : and the username and password, so I can now run the script.

However I am failing to configure counters. The config file is as follows:

gpio:
  WaterMeter19:
    pin: 19
    counter:

  IrrigationWaterMeter26:
    pin: 26
    counter:

I can see new MQTT connection, but the log on the Raspberry says my counters are wrong configuration:

openhabian@openHABianDevice:/opt/mqttany $ python3 mqttany/mqttany.py
2022-05-08 01:50:55,403 [INFO ] [core                    ] MQTTany 0.14.3 starting
2022-05-08 01:50:55,479 [INFO ] [core.gpio               ] Detected board: RASPBERRY_PI_4B
2022-05-08 01:50:55,609 [WARN ] [gpio                    ] 'WaterMeter19' is not a valid ID and will be ignored
2022-05-08 01:50:55,609 [WARN ] [gpio                    ] 'IrrigationWaterMeter26' is not a valid ID and will be ignored
2022-05-08 01:50:55,619 [INFO ] [core                    ] Module 'mqtt' started successfully
2022-05-08 01:50:55,626 [INFO ] [core                    ] Module 'gpio' started successfully
2022-05-08 01:50:55,644 [INFO ] [mqtt                    ] Connected to broker '192.168.4.227:1883'

OK , got it. I just saw the ID cant have capital letters