<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hakase </title>
    <description>The latest articles on DEV Community by Hakase  (@hakase).</description>
    <link>https://dev.to/hakase</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1122453%2F46ca0760-6ab1-4e09-8e14-d27d7a542493.png</url>
      <title>DEV Community: Hakase </title>
      <link>https://dev.to/hakase</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hakase"/>
    <language>en</language>
    <item>
      <title>Contributing to mqtt-recorder project</title>
      <dc:creator>Hakase </dc:creator>
      <pubDate>Wed, 19 Jul 2023 16:53:50 +0000</pubDate>
      <link>https://dev.to/hakase/contributing-to-mqtt-recorder-project-4e83</link>
      <guid>https://dev.to/hakase/contributing-to-mqtt-recorder-project-4e83</guid>
      <description>&lt;p&gt;Recently I just contributed to an open source project. The link for the project is as following: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/rpdswtk/mqtt_recorder"&gt;MQTT Recorder&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a python library that I use to record messages that are published by MQTT. The reason why I want to record the messages is because I want to play it back another time, so I don’t have to, let say, make the sensors/hardware push the data again.&lt;/p&gt;

&lt;p&gt;This library is already working great but I see the opportunity for me to contribute to an open source project, by adding a few new features to this library.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it is
&lt;/h2&gt;

&lt;p&gt;MQTT Recorder - That’s the name of this library. It is a simple command line tool for recording and replaying MQTT messages. &lt;/p&gt;

&lt;h3&gt;
  
  
  Record
&lt;/h3&gt;

&lt;p&gt;To record MQTT messages, you can simply run as following:&lt;br&gt;
&lt;code&gt;mqtt-recorder --host localhost --mode record --file recording.csv&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;What it does is it will connect to the MQTT broker that is hosted in &lt;strong&gt;localhost&lt;/strong&gt;, record the MQTT messages, and store it into &lt;strong&gt;recording.csv&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Once you finish recording the messages, you can stop it from recording by &lt;strong&gt;CTRL+C&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Replay
&lt;/h3&gt;

&lt;p&gt;To replay the recording, simply run as following:&lt;br&gt;
&lt;code&gt;mqtt-recorder --host localhost --mode replay --file recording.csv&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Contribution
&lt;/h2&gt;

&lt;p&gt;Previously, I only showed the basic use of recording and replaying the messages. But when using it, I need to add my own configuration, such as using username and password for authentication, defining client_id for identity, and using SSL for secure data transmission. However, in &lt;strong&gt;version 1.2.0&lt;/strong&gt;, it does not support &lt;strong&gt;client_id&lt;/strong&gt; and to use &lt;strong&gt;insecure TLS certs&lt;/strong&gt;. &lt;br&gt;
Hence, I added two new features which are, passing a value to parameter client_id and enable insecure TLS.&lt;/p&gt;

&lt;p&gt;First, I modified the &lt;em&gt;&lt;strong&gt;__main__.py&lt;/strong&gt;&lt;/em&gt; file in &lt;strong&gt;mqtt_recorder&lt;/strong&gt;. I added 2 arguments, where to define &lt;strong&gt;&lt;em&gt;--client_id&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;--tls_insecure&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8ymofipS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fnnsdjaf10dw5h4l9151.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8ymofipS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fnnsdjaf10dw5h4l9151.png" alt="--client_id argument" width="212" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6rG3Reuu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u6pxxpk953vyi3vqiyrg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6rG3Reuu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u6pxxpk953vyi3vqiyrg.png" alt="--tls-insecure argument" width="382" height="147"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So &lt;strong&gt;&lt;em&gt;--client_id&lt;/em&gt;&lt;/strong&gt; will receive the input as a &lt;strong&gt;string&lt;/strong&gt;, while &lt;strong&gt;&lt;em&gt;--tls_insecure&lt;/em&gt;&lt;/strong&gt; will receive the input as a &lt;strong&gt;boolean&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Then go to the &lt;strong&gt;&lt;em&gt;main()&lt;/em&gt;&lt;/strong&gt; function, to pass the arguments in the function.&lt;/p&gt;

&lt;p&gt;I defined the argument &lt;strong&gt;&lt;em&gt;client_id&lt;/em&gt;&lt;/strong&gt; inside the &lt;strong&gt;MqttRecorder&lt;/strong&gt; class. The code is:&lt;br&gt;
&lt;code&gt;args.client_id&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;At the same time, I defined the argument for &lt;strong&gt;&lt;em&gt;--tls_insecure&lt;/em&gt;&lt;/strong&gt;, which is located in the &lt;strong&gt;SslContext&lt;/strong&gt; class. The code is:&lt;br&gt;
&lt;code&gt;args.tls_insecure&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pFYtDlm_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjdzodhdlvefp96qpa2l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pFYtDlm_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjdzodhdlvefp96qpa2l.png" alt="__main__.py" width="800" height="508"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, I have added the code for defining parameters at &lt;em&gt;&lt;strong&gt;__main__.py&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Proceed on, the file that is needed to modify is &lt;strong&gt;&lt;em&gt;recorder.py&lt;/em&gt;&lt;/strong&gt;, located at &lt;strong&gt;mqtt_recorder&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Go to &lt;strong&gt;&lt;em&gt;SslContext&lt;/em&gt;&lt;/strong&gt; class, to add the argument &lt;strong&gt;&lt;em&gt;tls_insecure&lt;/em&gt;&lt;/strong&gt;, and define the variable. An additional line of code to receive the value based on the parameters was added.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s----skpRHr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/76u7w0hv4wysbwpi5i63.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s----skpRHr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/76u7w0hv4wysbwpi5i63.png" alt="SslContext()" width="581" height="156"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, go to &lt;strong&gt;MqttRecorder&lt;/strong&gt; class. In &lt;em&gt;&lt;strong&gt;__init__&lt;/strong&gt;&lt;/em&gt; function, to define parameter &lt;strong&gt;&lt;em&gt;client_id&lt;/em&gt;&lt;/strong&gt; with a string type.&lt;br&gt;
&lt;code&gt;client_id: str&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After the parameter is defined, it can now be used in the MQTT library. The &lt;strong&gt;&lt;em&gt;client_id&lt;/em&gt;&lt;/strong&gt; can now be passed to the MQTT Client class.&lt;br&gt;
&lt;code&gt;self.__client = mqtt.Client(client_id=client_id)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Lastly, an if statement for tls_insecure was added. This is only needed if the &lt;strong&gt;&lt;em&gt;--enable_ssl&lt;/em&gt;&lt;/strong&gt; parameter is True. The if statement is used only when the parameter is passed, then the &lt;strong&gt;&lt;em&gt;tls_insecure_set&lt;/em&gt;&lt;/strong&gt; function is executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--crRN77yA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zee5e3qzu58m7tmjudag.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--crRN77yA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zee5e3qzu58m7tmjudag.png" alt="MqttRecorder" width="741" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's how you add client_id and enable insecure TLS features. I hope this helps!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Notes:&lt;/strong&gt;&lt;br&gt;
|&amp;gt; 20 July 2023 - My pull request still not merge: &lt;a href="https://github.com/rpdswtk/mqtt_recorder/pull/5"&gt;https://github.com/rpdswtk/mqtt_recorder/pull/5&lt;/a&gt;, however you can use the updated version here: &lt;a href="https://github.com/Tester2009/mqtt_recorder"&gt;https://github.com/Tester2009/mqtt_recorder&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;|&amp;gt; 7 August 2023 - Finally, my pull request has been merged on 5th August, 2023.&lt;/p&gt;

</description>
      <category>python</category>
      <category>mqtt</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
