{"id":1533,"date":"2016-02-15T12:58:34","date_gmt":"2016-02-15T12:58:34","guid":{"rendered":"http:\/\/grodansparadis.com\/wordpress\/?p=1533"},"modified":"2017-11-30T20:10:21","modified_gmt":"2017-11-30T20:10:21","slug":"using-the-vscp-mqtt-driver-part-1-iot-m2m","status":"publish","type":"post","link":"https:\/\/grodansparadis.com\/wordpress\/?p=1533","title":{"rendered":"Using the #VSCP #MQTT driver Part 1 #IoT #m2m"},"content":{"rendered":"<h1>Using<\/h1>\n<p><a href=\"https:\/\/i0.wp.com\/grodansparadis.com\/wordpress\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-14-224416.png\" rel=\"attachment wp-att-1534\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1534\" data-permalink=\"https:\/\/grodansparadis.com\/wordpress\/?attachment_id=1534\" data-orig-file=\"https:\/\/i0.wp.com\/grodansparadis.com\/wordpress\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-14-224416.png?fit=1641%2C764&amp;ssl=1\" data-orig-size=\"1641,764\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Screenshot from 2016-02-14 22:44:16\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/grodansparadis.com\/wordpress\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-14-224416.png?fit=580%2C270&amp;ssl=1\" class=\"alignnone size-full wp-image-1534\" src=\"https:\/\/i0.wp.com\/grodansparadis.com\/wordpress\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-14-224416.png?resize=580%2C270\" alt=\"Screenshot from 2016-02-14 22:44:16\" width=\"580\" height=\"270\" srcset=\"https:\/\/i0.wp.com\/grodansparadis.com\/wordpress\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-14-224416.png?w=1641&amp;ssl=1 1641w, https:\/\/i0.wp.com\/grodansparadis.com\/wordpress\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-14-224416.png?resize=300%2C140&amp;ssl=1 300w, https:\/\/i0.wp.com\/grodansparadis.com\/wordpress\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-14-224416.png?resize=768%2C358&amp;ssl=1 768w, https:\/\/i0.wp.com\/grodansparadis.com\/wordpress\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-14-224416.png?resize=1024%2C477&amp;ssl=1 1024w, https:\/\/i0.wp.com\/grodansparadis.com\/wordpress\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-14-224416.png?w=1160&amp;ssl=1 1160w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p>The <a href=\"http:\/\/www.vscp.org\/docs\/vscpd\/doku.php?id=level2_driver_mqtt\">MQTT driver for the VSCP Daemon<\/a> is a way to connect VSCP\u00a0 to things communicating over MQTT.\u00a0 Well it&#8217;s just as easy as 1-2-3 to do so. Just follow this instruction.<\/p>\n<p>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.<\/p>\n<p>So for driver additions we add the following driver enteries to the vscpd.conf file (<em>\/etc\/vscp\/vscpd.conf<\/em> on Linux and <em>\/programdata\/vscp\/vscpd.conf<\/em> on windows).<\/p>\n<p><em><strong>&lt;!&#8211; Level II MQTT driver &#8211;&gt;<\/strong><\/em><br \/>\n<em><strong>\u00a0\u00a0\u00a0 &lt;driver enable=&#8221;true&#8221; &gt;<\/strong><\/em><br \/>\n<em><strong>\u00a0\u00a0\u00a0 &lt;name&gt;MQTT1&lt;\/name&gt;<\/strong><\/em><br \/>\n<em><strong>\u00a0\u00a0\u00a0 &lt;path&gt;\/usr\/local\/lib\/vscpl2drv_mqtt.so&lt;\/path&gt;<\/strong><\/em><br \/>\n<em><strong>\u00a0\u00a0\u00a0 &lt;config&gt;session1;subscribe;vscp-sub;192.168.1.9:1883<\/strong><\/em><br \/>\n<em><strong>\u00a0\u00a0 &lt;guid&gt;00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00&lt;\/guid&gt;<\/strong><\/em><br \/>\n<em><strong> &lt;\/driver&gt;<\/strong><\/em><\/p>\n<p><em><strong>&lt;!&#8211; Level II MQTT driver &#8211;&gt;<\/strong><\/em><br \/>\n<em><strong> &lt;driver enable=&#8221;true&#8221; &gt;<\/strong><\/em><br \/>\n<em><strong>\u00a0\u00a0\u00a0 &lt;name&gt;MQTT2&lt;\/name&gt;<\/strong><\/em><br \/>\n<em><strong>\u00a0\u00a0\u00a0 &lt;path&gt;\/usr\/local\/lib\/vscpl2drv_mqtt.so&lt;\/path&gt;<\/strong><\/em><br \/>\n<em><strong>\u00a0\u00a0\u00a0 &lt;config&gt;session2;publish;vscp-pub;192.168.1.9:1883<\/strong><\/em><br \/>\n<em><strong>\u00a0\u00a0\u00a0 &lt;guid&gt;00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00&lt;\/guid&gt;<\/strong><\/em><br \/>\n<em><strong> &lt;\/driver&gt;<\/strong><\/em><\/p>\n<p>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.<\/p>\n<p>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,<\/p>\n<h2>MQTT1<\/h2>\n<p>The configuration string looks like this<\/p>\n<p><em><strong>session1;subscribe;vscp-sub;192.168.1.9:1883<\/strong><\/em><\/p>\n<p>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 <a href=\"http:\/\/send head,class,type,obid,time-stamp,GUID,data1,data2,data3....\">standard text form<\/a> coming in on the channel. The broker is at IP-address 192.168.1.9 and using the\u00a0 standard port 1883. The topic we are interested in is<strong> vscp_sub<\/strong>.\u00a0 That&#8217;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 <em><strong>session1<\/strong><\/em>.\u00a0 Remember that this session name must be unique in each driver entry.<\/p>\n<p>Restating the daemon and we are ready to go<\/p>\n<p>Using\u00a0 <a href=\"http:\/\/mosquitto.org\/man\/mosquitto_pub-1.html\">mosquitto_pub<\/a> we can now send MQTT events to the VSCP system<\/p>\n<p><em><strong>mosquitto_pub -d -t vscp-sub -m &#8220;vscp-pub vscp-pub 0,10,6,0,0,-,0x88,0x82,0x0A,0x09&#8221;<\/strong><\/em><\/p>\n<p>which comes in as 25.68 degrees Celsius from sensor 1.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/grodansparadis.com\/wordpress\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-14-230600.png\" rel=\"attachment wp-att-1535\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1535\" data-permalink=\"https:\/\/grodansparadis.com\/wordpress\/?attachment_id=1535\" data-orig-file=\"https:\/\/i0.wp.com\/grodansparadis.com\/wordpress\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-14-230600.png?fit=1516%2C990&amp;ssl=1\" data-orig-size=\"1516,990\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Screenshot from 2016-02-14 23:06:00\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/grodansparadis.com\/wordpress\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-14-230600.png?fit=580%2C379&amp;ssl=1\" class=\"alignnone size-full wp-image-1535\" src=\"https:\/\/i0.wp.com\/grodansparadis.com\/wordpress\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-14-230600.png?resize=580%2C379\" alt=\"Screenshot from 2016-02-14 23:06:00\" width=\"580\" height=\"379\" srcset=\"https:\/\/i0.wp.com\/grodansparadis.com\/wordpress\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-14-230600.png?w=1516&amp;ssl=1 1516w, https:\/\/i0.wp.com\/grodansparadis.com\/wordpress\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-14-230600.png?resize=300%2C196&amp;ssl=1 300w, https:\/\/i0.wp.com\/grodansparadis.com\/wordpress\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-14-230600.png?resize=768%2C502&amp;ssl=1 768w, https:\/\/i0.wp.com\/grodansparadis.com\/wordpress\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-14-230600.png?resize=1024%2C669&amp;ssl=1 1024w, https:\/\/i0.wp.com\/grodansparadis.com\/wordpress\/wp-content\/uploads\/2016\/02\/Screenshot-from-2016-02-14-230600.png?w=1160&amp;ssl=1 1160w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/p>\n<p><strong>Data<\/strong><\/p>\n<p><em>This section can be skipped if your not interested in how the data is coed.<\/em><\/p>\n<p>This format is fully described in the <a href=\"http:\/\/www.vscp.org\/docs\/vscpspec\/doku.php?id=data_coding\">data coding section of the specification<\/a> but we give a brief explanation of the event data here also (but you should read the spec. for a comprehensive understanding).\u00a0 The event is <a href=\"http:\/\/www.vscp.org\/docs\/vscpspec\/doku.php?id=class1.measurement#type_6_0x06_temperature\">CLASS1. MEASUREMENT, Type=6, Temperature<\/a>. So we have a temperature measurement. Good to know.<\/p>\n<p>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 <a href=\"https:\/\/www.google.se\/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=2&amp;cad=rja&amp;uact=8&amp;ved=0ahUKEwiGnrGFz_nKAhXMPZoKHTh5CjMQFggkMAE&amp;url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FInternational_System_of_Units&amp;usg=AFQjCNEK4jmvq8h5WyrNlUxENEpZrmpKyg&amp;sig2=zbWcKYopPUpsYoNSsYruAA\">SI system<\/a> defined types.\u00a0 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.<\/p>\n<p>The data can be coded as a string or a floating point value or in something called a <strong>normalized integer<\/strong> 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.<\/p>\n<p>As you see the data for the measurement is<\/p>\n<p><em><strong>0x88, 0x82, 0x0A, 0x09<\/strong><\/em><\/p>\n<p>The first byte of this data\u00a0 is the coding byte, here 0x88. In this byte fits information about the index for the sensor\u00a0 (bits 0,1,2), the the unit\u00a0for the measurement (bits 3,4) and the type of data that follows (bits 5,6,7).<\/p>\n<p>So in binary 0x88 is<\/p>\n<p><strong>10001000<\/strong><\/p>\n<p>which if we break apart the different fields become<\/p>\n<p><strong>100 01 000<\/strong><\/p>\n<ul>\n<li>Bits 0,1,2 is set to 000 so this is a measurement from sensor 0 of the board.<\/li>\n<li>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\u00a0 are Kelvin (unit=0), Celsius (unit=1), Fahrenheit (unit=2). So our measurement is in degrees Celsius.<\/li>\n<li>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.<\/li>\n<\/ul>\n<p>OK We have a measurement value from sensor 0 that is expressed as degrees Celsius. Just need to know the value as well.<\/p>\n<p>A normalized integer have a decimal point shift byte as its second byte and then the\u00a0 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\u00a0 is 2569.<\/p>\n<p>The second data byte (0x82) says, if it has bit seven set,\u00a0 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.<\/p>\n<p>So the measurement value is a temperature measurement of 25.69 degrees Celsius.<\/p>\n<p><strong>Sending on\/off<\/strong><\/p>\n<p>Everything is well specified in VSCP.\u00a0 So to turn something on or off is well specified of course.<\/p>\n<p>To tun something ON one use the <a href=\"http:\/\/www.vscp.org\/docs\/vscpspec\/doku.php?id=class1.control#type_5_0x05_turnon\">CLASS1.CONTROL, Type=5 <\/a>in VSCP.\u00a0 To turn something off one use <a href=\"http:\/\/www.vscp.org\/docs\/vscpspec\/doku.php?id=class1.control#type_6_0x06_turnoff\">CLASS1.CONTROL, Type=6<\/a>.<\/p>\n<p>The event CLASS1.CONTROL is coded as 30.<\/p>\n<p>Now to something that might look strange to VSCP newcomers at first. VSCP normally does not address things.\u00a0 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.<\/p>\n<p>So for both TurnOn and TurnOff of the data format is<\/p>\n<p><strong>0x00, zone, subzone<\/strong><\/p>\n<p>So to turn something on in zone=1\/subzone=2 we send<\/p>\n<p><em><strong>mosquitto_pub -d -t vscp-sub -m &#8220;vscp-pub vscp-pub 0,30,5,0,0,-,0,1,2&#8221;<\/strong><\/em><\/p>\n<p>and to turn something off we send<\/p>\n<p><em><strong>mosquitto_pub -d -t vscp-sub -m &#8220;vscp-pub vscp-pub 0,30,6,0,0,-,0,1,2&#8221;<\/strong><\/em><\/p>\n<p>Not to hard is it?<\/p>\n<h2>MQTT1<\/h2>\n<p>The configuration string looks like this<\/p>\n<p><em><strong>session2;publish;vscp-pub;192.168.1.9:1883<\/strong><\/em><\/p>\n<p>Well we recognize the confoguration string from above. The session name is &#8220;session2&#8221;. Remember it should be unique. and we publish to a broker instead of subscribing. The MQTT topic is set to vscp_pub.<\/p>\n<p>So to if you subscribe to vscp_pub on the broker you get events from the VSCP system sent to you.\u00a0 You set filters to tell which events you want and do many other things.<\/p>\n<p>Using the mosquitto_sub subscribing is<\/p>\n<p><strong>mosquitto_sub -h 192.168.1.9 -v -t &#8216;vscp-pub&#8217;<\/strong><\/p>\n<p>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\u00a0 giving you access to the VCSP framework with web interfaces, diagrams, message routing and a lot more.<\/p>\n<p>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.<\/p>\n<p>Another part will follow describing a way to simplify message handing over MQTT.<\/p>\n<p><a href=\"http:\/\/grodansparadis.com\/wordpress\/?p=1528\">Part 2<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using The MQTT driver for the VSCP Daemon is a way to connect VSCP\u00a0 to things communicating over MQTT.\u00a0 Well it&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[53,13],"tags":[],"class_list":["post-1533","post","type-post","status-publish","format-standard","hentry","category-howtos","category-vscp"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4raCZ-oJ","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1533","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1533"}],"version-history":[{"count":5,"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1533\/revisions"}],"predecessor-version":[{"id":1543,"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1533\/revisions\/1543"}],"wp:attachment":[{"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}