Categories
HowTo's VSCP

Using the #VSCP #MQTT driver Part 1 #IoT #m2m

Using

Screenshot from 2016-02-14 22:44:16

The MQTT driver for the VSCP Daemon is a way to connect VSCP  to things communicating over MQTT.  Well it’s just as easy as 1-2-3 to do so. Just follow this instruction.

Suppose that we want two MQTT channels. One that publish content to a broker and one that subscribe content from a broker channel. Of course we can have as many as we like. Just add more driver entries if you need more. There is no practical limit.

So for driver additions we add the following driver enteries to the vscpd.conf file (/etc/vscp/vscpd.conf on Linux and /programdata/vscp/vscpd.conf on windows).

<!– Level II MQTT driver –>
    <driver enable=”true” >
    <name>MQTT1</name>
    <path>/usr/local/lib/vscpl2drv_mqtt.so</path>
    <config>session1;subscribe;vscp-sub;192.168.1.9:1883
   <guid>00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00</guid>
</driver>

<!– Level II MQTT driver –>
<driver enable=”true” >
    <name>MQTT2</name>
    <path>/usr/local/lib/vscpl2drv_mqtt.so</path>
    <config>session2;publish;vscp-pub;192.168.1.9:1883
    <guid>00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00</guid>
</driver>

Both have there own unique names, MQTT1 and MQTT2 as they should. Paths are set to the driver location. Windows user need to point this path to the installation folder. The GUID is set to all zeros meaning the driver will use the GUID of the interface. We are fine with that in this sample but for most cases it is better to set a unique GUID.

Then we have the configuration string and it is different for the two driver entries. We look at each driver on its own from here,

MQTT1

The configuration string looks like this

session1;subscribe;vscp-sub;192.168.1.9:1883

That is we create a subscription channel, that is a channel which subscribes on content from a MQTT broker, and in this case expects VSCP events on the standard text form coming in on the channel. The broker is at IP-address 192.168.1.9 and using the  standard port 1883. The topic we are interested in is vscp_sub.  That’s almost it. We need one more thing. We need to name the session for our communication with the MQTT broker. In this case we set the session name to session1.  Remember that this session name must be unique in each driver entry.

Restating the daemon and we are ready to go

Using  mosquitto_pub we can now send MQTT events to the VSCP system

mosquitto_pub -d -t vscp-sub -m “vscp-pub vscp-pub 0,10,6,0,0,-,0x88,0x82,0x0A,0x09”

which comes in as 25.68 degrees Celsius from sensor 1.

Screenshot from 2016-02-14 23:06:00

Data

This section can be skipped if your not interested in how the data is coed.

This format is fully described in the data coding section of the specification but we give a brief explanation of the event data here also (but you should read the spec. for a comprehensive understanding).  The event is CLASS1. MEASUREMENT, Type=6, Temperature. So we have a temperature measurement. Good to know.

It is a VSCP level I events. We see that from CLASS1. in the class name. This is a low end measurement event used in VSCP that can handle all SI system defined types.  VSCP level I event have a maximum of eight data bytes as it is defined to be possoble to be handled by the smallest devices out there. Eight byte is not much but you can tell what type of measurement it is, what sensor of eight possible on a board it comes from, and what unit the measurement is give in. Not bad. Compare that to just sensing 44.20 over the topic.

The data can be coded as a string or a floating point value or in something called a normalized integer as here. If your device can handle floating point you should use that. But many devices does not have resources to handle floating point data as it costs memory. They ned the memory for other tasks. And that is where the normalized integer comes in handy.

As you see the data for the measurement is

0x88, 0x82, 0x0A, 0x09

The first byte of this data  is the coding byte, here 0x88. In this byte fits information about the index for the sensor  (bits 0,1,2), the the unit for the measurement (bits 3,4) and the type of data that follows (bits 5,6,7).

So in binary 0x88 is

10001000

which if we break apart the different fields become

100 01 000

  • Bits 0,1,2 is set to 000 so this is a measurement from sensor 0 of the board.
  • Bits 3,4 is set to 01 so this is the second defined unit of the measurement. Unit zero is always the SI unit and as this is a temperature measurement (we got that information from the class and the type) the available units for temperatures  are Kelvin (unit=0), Celsius (unit=1), Fahrenheit (unit=2). So our measurement is in degrees Celsius.
  • Bit 5,6,7 Tells if the value is a string, a floating point value or any of the other defined types. In this case 100 tells us that it is a normalized integer.

OK We have a measurement value from sensor 0 that is expressed as degrees Celsius. Just need to know the value as well.

A normalized integer have a decimal point shift byte as its second byte and then the  1-6 bytes that follow are the actual integer value. So here the value is 0x0A09 (VSCP always store everything MSB first). Translated to decimal 0x0A09  is 2569.

The second data byte (0x82) says, if it has bit seven set,  that the decimal point should be moved left in the number that follows with the amount told by the remaining bits. So in this case the decimal point should move two steps to the left which gives 25.62.

So the measurement value is a temperature measurement of 25.69 degrees Celsius.

Sending on/off

Everything is well specified in VSCP.  So to turn something on or off is well specified of course.

To tun something ON one use the CLASS1.CONTROL, Type=5 in VSCP.  To turn something off one use CLASS1.CONTROL, Type=6.

The event CLASS1.CONTROL is coded as 30.

Now to something that might look strange to VSCP newcomers at first. VSCP normally does not address things.  WHAT!? you might scream out. Yes it does not. Instead it send a TurnOn event to devices in a zone/subzone. The sender does not know if the receiver is one or as man as hundred devices. It does not even care. It just send the event. It trust the transport mechanism to deliver the event to interested parties.

So for both TurnOn and TurnOff of the data format is

0x00, zone, subzone

So to turn something on in zone=1/subzone=2 we send

mosquitto_pub -d -t vscp-sub -m “vscp-pub vscp-pub 0,30,5,0,0,-,0,1,2”

and to turn something off we send

mosquitto_pub -d -t vscp-sub -m “vscp-pub vscp-pub 0,30,6,0,0,-,0,1,2”

Not to hard is it?

MQTT1

The configuration string looks like this

session2;publish;vscp-pub;192.168.1.9:1883

Well we recognize the confoguration string from above. The session name is “session2”. Remember it should be unique. and we publish to a broker instead of subscribing. The MQTT topic is set to vscp_pub.

So to if you subscribe to vscp_pub on the broker you get events from the VSCP system sent to you.  You set filters to tell which events you want and do many other things.

Using the mosquitto_sub subscribing is

mosquitto_sub -h 192.168.1.9 -v -t ‘vscp-pub’

which you can use for testing. But Arduino or Raspberry Pi also have MQTT libraries so you can take in VSCP events to them to, well or sening  giving you access to the VCSP framework with web interfaces, diagrams, message routing and a lot more.

It may be simpler to use just VSCP and get the same advantages but this write-up is for those of you that use MQTT today.

Another part will follow describing a way to simplify message handing over MQTT.

Part 2

One reply on “Using the #VSCP #MQTT driver Part 1 #IoT #m2m”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.