bacnet-mqtt-gateway icon indicating copy to clipboard operation
bacnet-mqtt-gateway copied to clipboard

mqtt packet is always {}

Open kskenyon opened this issue 4 years ago • 7 comments

I know support for mqtt other than infinimesh is limited.

I modified the mqtt_client.js to run on mosquito with username and password authentication.

However, with several different bacnet devices, all I get is empty {} messages to mqtt on a weird subscription. devices/0x132/state/reported/delta which seems hardwired into the code and doesn't make much sense to me. I'm mostly a poke and try coder and I don't fully understand bacnet-stack.

I can read the bacnet OK with other apps including node-red. I think I have the correct type and instances in the config file.

Also, the web server doesn't seem to work. I get the main screen for "who is" and "object" but nothing happens when I click the buttons. The REST API doesn't work either.

So is this so totally entwined with infinimesh that it is hopeless to try to fix it?

I think there are others who would like to use this gateway with their existing setup.

Kevin

pi@pi3:~/bacnet-mqtt-gateway $ npm start

[email protected] start /home/pi/bacnet-mqtt-gateway node src/app.js

{"level":"info","message":"Gateway server listening on port 8083","timestamp":"2020-12-20 21:57:39"} {"level":"info","message":"Device configs found: baspi.json,baspi1.json,baspi2.json","timestamp":"2020-12-20 21:57:39"} {"level":"info","message":"Client connected","timestamp":"2020-12-20 21:57:39"} {"level":"info","message":"Schedule polling for device 10.0.1.32 with expression */3 * * * * *","timestamp":"2020-12-20 21:57:39"} {"level":"info","message":"Schedule polling for device 10.0.1.150 with expression */3 * * * * *","timestamp":"2020-12-20 21:57:39"} {"level":"info","message":"Schedule polling for device 10.0.1.150 with expression */3 * * * * *","timestamp":"2020-12-20 21:57:39"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:42"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:42"} {"level":"info","message":"Sending actuals for device 10.0.1.150 to IoT Hub","timestamp":"2020-12-20 21:57:42"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:42"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:42"} {"level":"info","message":"Sending actuals for device 10.0.1.150 to IoT Hub","timestamp":"2020-12-20 21:57:42"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:42"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:42"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:42"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"Sending actuals for device 10.0.1.150 to IoT Hub","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"Sending actuals for device 10.0.1.150 to IoT Hub","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"Sending actuals for device 10.0.1.32 to IoT Hub","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"Sending actuals for device 10.0.1.32 to IoT Hub","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"Sending actuals for device 10.0.1.150 to IoT Hub","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"Sending actuals for device 10.0.1.150 to IoT Hub","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"Sending actuals for device 10.0.1.150 to IoT Hub","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"Sending actuals for device 10.0.1.32 to IoT Hub","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"Sending actuals for device 10.0.1.150 to IoT Hub","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:51"} ^C

kskenyon avatar Dec 21 '20 03:12 kskenyon

Hi Kevin,

if you are using other BACnet sniffer tools like YABE on the same PC, where you run the BACnet2MQTT gateway, make sure you close them all before running npm start.

Regarding the publishing format: You can edit the publish_msg in mqtt_client.js to your desire. E.g., you can publish each BACnet object on a separate topic. For example like this:

publishMessage(messageJson) {
    if(sendConfig == true) { // Publish config:
        console.log('CONFIGURATION:');
        console.table(messageJson);
    
    // Publish values:
    console.log('PUBLISH UPDATE:');
        Object.keys(messageJson).forEach(key => {
            // console.table('Key : ' + key + ', Value : ' + JSON.stringify(messageJson[key]))
            // const topic = 'devices/' + gatewayId + '/state/reported/'+key;
            const objectType = key.split('_')[0];
            var type = 'sensor';
            // console.log('ObjType: ' + objectType);
            if (objectType == '0') {
                type = 'sensor';
            } else if (objectType == '3') {
                type = 'binary_sensor';
            } else if (objectType == '13') {
                type = 'sensor';
            } else { type = 'unknownObject' };

            const topic = 'homeassistant/' + type + '/' + key + "/state";
            const message = JSON.stringify(messageJson[key].value);
            console.log(topic, message)
            this.client.publish(topic, message);
        });
    }
}

Matthias

magicmatt007 avatar Dec 21 '20 07:12 magicmatt007

Thanks Matthias, that was helpful. I still don't read any objects and the web interface still doesn't work but there is some progress in mqtt.

kskenyon avatar Dec 22 '20 02:12 kskenyon

I wasn't successful with the web interface either. I'm just editing the "baspi.json" with my bacnet objects:

Enter the IP of the BACnet device you want to read from.

Add the BACnet objects, which you want to read. You can use tools like YABE to figure out the objects you want to read: ObjectType needs to be translated to its number, e.g. check here: https://www.winccoa.top/help314p016/BACnet/Bacnet_Applikation-06.htm Instance number is available directly from YABE

magicmatt007 avatar Dec 22 '20 06:12 magicmatt007

@magicmatt007 thanks, can you please file an PR and we can add this into the readme. And if you find bugs, pls contribute the fix. We built this stack some time ago on customer needs. We are happy to look into improvements :)

2pk03 avatar Dec 22 '20 12:12 2pk03

could I

I know support for mqtt other than infinimesh is limited.

I modified the mqtt_client.js to run on mosquito with username and password authentication.

However, with several different bacnet devices, all I get is empty {} messages to mqtt on a weird subscription. devices/0x132/state/reported/delta which seems hardwired into the code and doesn't make much sense to me. I'm mostly a poke and try coder and I don't fully understand bacnet-stack.

I can read the bacnet OK with other apps including node-red. I think I have the correct type and instances in the config file.

Also, the web server doesn't seem to work. I get the main screen for "who is" and "object" but nothing happens when I click the buttons. The REST API doesn't work either.

So is this so totally entwined with infinimesh that it is hopeless to try to fix it?

I think there are others who would like to use this gateway with their existing setup.

Kevin

pi@pi3:~/bacnet-mqtt-gateway $ npm start

[email protected] start /home/pi/bacnet-mqtt-gateway node src/app.js

{"level":"info","message":"Gateway server listening on port 8083","timestamp":"2020-12-20 21:57:39"} {"level":"info","message":"Device configs found: baspi.json,baspi1.json,baspi2.json","timestamp":"2020-12-20 21:57:39"} {"level":"info","message":"Client connected","timestamp":"2020-12-20 21:57:39"} {"level":"info","message":"Schedule polling for device 10.0.1.32 with expression */3 * * * * *","timestamp":"2020-12-20 21:57:39"} {"level":"info","message":"Schedule polling for device 10.0.1.150 with expression */3 * * * * *","timestamp":"2020-12-20 21:57:39"} {"level":"info","message":"Schedule polling for device 10.0.1.150 with expression */3 * * * * *","timestamp":"2020-12-20 21:57:39"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:42"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:42"} {"level":"info","message":"Sending actuals for device 10.0.1.150 to IoT Hub","timestamp":"2020-12-20 21:57:42"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:42"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:42"} {"level":"info","message":"Sending actuals for device 10.0.1.150 to IoT Hub","timestamp":"2020-12-20 21:57:42"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:42"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:42"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:42"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"Sending actuals for device 10.0.1.150 to IoT Hub","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"Sending actuals for device 10.0.1.150 to IoT Hub","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"Sending actuals for device 10.0.1.32 to IoT Hub","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:45"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"Sending actuals for device 10.0.1.32 to IoT Hub","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"Sending actuals for device 10.0.1.150 to IoT Hub","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"Sending actuals for device 10.0.1.150 to IoT Hub","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:48"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"Fetching device object values","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"Sending actuals for device 10.0.1.150 to IoT Hub","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"Sending actuals for device 10.0.1.32 to IoT Hub","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"Sending actuals for device 10.0.1.150 to IoT Hub","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"{}","timestamp":"2020-12-20 21:57:51"} {"level":"info","message":"Publish message to MQTT Broker","timestamp":"2020-12-20 21:57:51"} ^C

could I be cheeky and ask for your mosquitto config for this? I am battling to get the formatting right... many many thanks

jjanderson avatar Nov 03 '22 09:11 jjanderson

@kskenyon

Any chance that you still keep the mosquito config file?

mikihacia avatar Jan 04 '23 15:01 mikihacia

I modified the mqtt_client.js to run on mosquito with username and password authentication.

Can you post the code for the modified mqtt_client.js?

Bagunda avatar Dec 21 '23 10:12 Bagunda