<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mohit Mayank on Medium]]></title>
        <description><![CDATA[Stories by Mohit Mayank on Medium]]></description>
        <link>https://medium.com/@cueball45?source=rss-633a4ff0b953------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*RnWcEQjQrMru8PpmfQPZPg.jpeg</url>
            <title>Stories by Mohit Mayank on Medium</title>
            <link>https://medium.com/@cueball45?source=rss-633a4ff0b953------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 17:31:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cueball45/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Connecting to Oracle RDS using Python]]></title>
            <link>https://medium.com/@cueball45/connecting-to-oracle-rds-using-python-d019436025d4?source=rss-633a4ff0b953------2</link>
            <guid isPermaLink="false">https://medium.com/p/d019436025d4</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[oracle]]></category>
            <category><![CDATA[ssh]]></category>
            <category><![CDATA[mysql]]></category>
            <dc:creator><![CDATA[Mohit Mayank]]></dc:creator>
            <pubDate>Thu, 10 Sep 2020 05:44:57 GMT</pubDate>
            <atom:updated>2020-09-11T17:13:28.495Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*wQejrrS1goEK7FHC" /><figcaption>Photo by <a href="https://unsplash.com/@ikukevk?utm_source=medium&amp;utm_medium=referral">Kevin Ku</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Recently while trying to automate some data fixes at work, I had a requirement to connect to Oracle RDS which my team is using currently. I have done this with MySQL a lot of times in the past, so I believed doing the same with Oracle should be a similar experience. However, I was mistaken and the lack of documentation and blogs made the situation even worse.</p><p>In this short article, I will be sharing a step-by-step guide on how to connect to Oracle RDS via SSH (using Python).</p><h3>Prerequisite</h3><h4>Dependencies</h4><p>You’d need the following dependencies:</p><pre>OracleCx<br>sshtunnel</pre><h4>Setting up Oracle</h4><p>We need to install Oracle instant client from <a href="https://www.oracle.com/in/database/technologies/instant-client/downloads.html">here</a>. This is needed to allow out Python module to connect to Oracle DB. Now execute the following:</p><pre>cd ~<br>unzip ~/Downloads/instantclient-basic-macos.x64-19.3.0.0.0dbru.zip -d .<br>mkdir ~/lib<br>ln -s ~/instantclient_19_3/libclntsh.dylib ~/lib/</pre><blockquote>The above commands are for MacOS. Replace them according to the OS you are using.</blockquote><h3>Connecting to RDS</h3><h4>SSH Tunneling</h4><pre>from sshtunnel import SSHTunnelForwarder</pre><pre>ssh_host = &#39;ec2-xxx.compute.amazonaws.com&#39;<br>ssh_user = &#39;ec2-user&#39;<br>private_key = &#39;/path/to/pem&#39;<br>ssh_port = 22<br>local_port = 3000  # any random value</pre><pre>db_host = &#39;xxx.rds.aws.com&#39;<br>db_port = 1521</pre><pre>def tunnel():<br>  return SSHTunnelForwarder(<br>    ssh_host,<br>    ssh_username=ssh_user,<br>    ssh_pkey=private_key,<br>    remote_bind_address=(db_host, db_port),<br>    local_bind_address=(&#39;0.0.0.0&#39;, local_port)<br>  )</pre><pre><br>with tunnel() as _:<br>  print(&#39;SSH successful!&#39;)</pre><p>Let’s understand the nuances here:</p><ul><li>SSHTunnelForwarder: we are trying to do SSH tunneling or port forwarding from our local machine to the RDS AWS server</li><li>ssh_host: IP / web address of the host server (EC2) which can connect to RDS</li><li>ssh_username: user on the host machine whose role we would assume</li><li>ssh_pkey: private or security key file path, generally a PEM file, required to connect to the host</li><li>remote_bind_address: a tuple of RDS host address and port (generally 1521)</li><li>local_bind_address: a tuple of local host IP and port (can be any unused port number)</li></ul><h4>Connecting to DB</h4><pre>import cx_Oracle as cx</pre><pre>sid = &#39;xxx&#39;<br>dsn = cx.makedsn(&#39;127.0.0.1&#39;, local_port, sid=sid)  # replace sid with service_name if using that<br>query = &quot;select * from table where id = &#39;123&#39;&quot;</pre><pre>def run_query():<br>  with tunnel() as _:<br>    conn = cx_Oracle.connect(user, password, dsn)<br>    print(&#39;Connection successful!&#39;)<br>    cursor = conn.cursor()<br>    cursor.execute(query)<br>    print(&#39;Query executed succesfully!&#39;)<br>    data = cursor.fetchall()<br>    conn.close()<br>    return data</pre><p>Well, that’s it. You are done, you should be able to connect and run any query in your Oracle RDS DB.</p><h3>Stitching it all together…</h3><p>All in all, your script should look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f864778a5af8183bf47be01f5d70a09d/href">https://medium.com/media/f864778a5af8183bf47be01f5d70a09d/href</a></iframe><p>Cheers! 🍺</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d019436025d4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Motion controlled lighting]]></title>
            <link>https://medium.com/@cueball45/motion-controlled-lighting-6f3bbac16cb4?source=rss-633a4ff0b953------2</link>
            <guid isPermaLink="false">https://medium.com/p/6f3bbac16cb4</guid>
            <category><![CDATA[motion-sensing-light]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[philips-hue]]></category>
            <category><![CDATA[motion-sensor]]></category>
            <dc:creator><![CDATA[Mohit Mayank]]></dc:creator>
            <pubDate>Sun, 05 Jul 2020 04:57:40 GMT</pubDate>
            <atom:updated>2020-07-05T04:57:40.142Z</atom:updated>
            <content:encoded><![CDATA[<h4>Using Philips Wiz LED and Raspberry Pi</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_BM_zUpMH44AoVHI" /><figcaption>Photo by <a href="https://unsplash.com/@_louisreed?utm_source=medium&amp;utm_medium=referral">Louis Reed</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Introduction</h3><p>Last week I had this ingenious and <em>unique</em> idea:</p><blockquote>What if I don’t have to turn off the lights every time I leave my room and turn it on when I enter.</blockquote><p>With this idea in mind I set on the journey to create a motion controlled lighting system for my room.</p><h3>Hardware requirements</h3><p>You would need to procure the following before we start:</p><ul><li>Philips Smart LED (Wiz or Hue)</li><li>Raspberry Pi</li><li>PIR motion sensor</li><li>Female-to-female wires (at least 3)</li></ul><h3>Controlling Philips Smart LED</h3><p>If you have Philips LED with Hue bridge, life is simple. Hue exposes certain sets of API which needs to be called from your code. However, Wiz do not expose any API. But you can trace the network calls using the Wiz app to understand the calls made by your phone to Wiz bulb. However, since I have already done that, you don’t need to go through that pain.</p><p>Before executing the following code, find out the IP of your Wiz lightbulb and make sure your laptop is connected on the same network.</p><ul><li>Create a class to send message to bulb</li></ul><pre>import socket</pre><pre>class UDP:<br>    def __init__(self, ip):<br>        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)<br>        self.addr = (ip, 38899)</pre><pre>def call(self, message):<br>        self.sock.sendto(bytes(message), self.addr)<br>        data, _ = self.sock.recvfrom(1024)<br>        return data</pre><ul><li>Now let’s use this class to switch on / off the bulb.</li></ul><pre>IP = &#39;192.168.xx.xxx&#39;<br>udp = UDP(IP)</pre><pre>## turn on<br># message = b&#39;{&quot;method&quot;: &quot;setPilot&quot;, &quot;params&quot;:{&quot;state&quot;: true}}&#39;<br>## turn off<br>message = b&#39;{&quot;method&quot;: &quot;setPilot&quot;, &quot;params&quot;:{&quot;state&quot;: true}}&#39;<br>udp.call(message)</pre><ul><li>Now let’s use this class to change the colour of the lightbulb to 🔴.</li></ul><pre>message = b&#39;{&quot;method&quot;: &quot;setPilot&quot;, &quot;params&quot;: {&quot;r&quot;: 255, &quot;g&quot;: 0, &quot;b&quot;: 0}}&#39;<br>udp.call(message)</pre><p>You get the general idea. The entire code can be found on my <a href="https://github.com/cueo/wiz">Github</a>.</p><h3>Circuit</h3><p>The raspberry pi and the motion sensors are connected as shown:</p><p>The circuit needs to be connected as shown in the images:</p><ul><li>VCC to pin 2 (5V)</li><li>OUT to pin 16 (GPIO 23)</li><li>GND an pin 6 (ground)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/993/1*HnmusbsguCbkFzjP3NEH7w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9RJauAkEBDbkMFrJD_lPLg.jpeg" /></figure><h3>Detecting motion</h3><p>Now we can start writing code to tell Raspberry Pi to do something once the sensor detects motion. The following code just prints “<em>There was a movement!</em>” every time it detects a motion.</p><pre>import RPi.GPIO as GPIO<br>import time<br> <br>SENSOR_PIN = 23<br> <br>GPIO.setmode(GPIO.BCM)<br>GPIO.setup(SENSOR_PIN, GPIO.IN)<br> <br>def my_callback(channel):<br>    # Here, alternatively, an application / command etc. can be started.<br>    print(&#39;There was a movement!&#39;)<br> <br>try:<br>    print(&#39;Started...&#39;)<br>    GPIO.add_event_detect(SENSOR_PIN , GPIO.RISING, callback=my_callback)<br>    while True:<br>        time.sleep(100)<br>except KeyboardInterrupt:<br>    print(&#39;Finish...&#39;)<br>GPIO.cleanup()</pre><h3>Troubleshooting PIR</h3><p>It is likely that the sensor does not start detecting motion right away (however much you try). The following YouTube video helped me quite a bit. Issue with my PIR was I had to adjust its potentiometer a bit.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fv7Mi49Mv4Sw%3Fstart%3D90%26feature%3Doembed%26start%3D90&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dv7Mi49Mv4Sw&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fv7Mi49Mv4Sw%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8413039df0719dafc079952c0cecfb8/href">https://medium.com/media/d8413039df0719dafc079952c0cecfb8/href</a></iframe><h3>Stitching it together</h3><p>Now let’s modify the code to switch the light on / off alternatively every time the sensor detects any motion.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13a10d3891471640074b6861bd7432a2/href">https://medium.com/media/13a10d3891471640074b6861bd7432a2/href</a></iframe><p>That’s all, now put the sensor in a place where you want to detect movement and every time it detects a movement the sensor will switch the light to on or off!</p><p>Cheers! 🍻</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6f3bbac16cb4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Kotlin library with Gradle]]></title>
            <link>https://medium.com/@cueball45/building-a-kotlin-library-with-gradle-8b41b6038b54?source=rss-633a4ff0b953------2</link>
            <guid isPermaLink="false">https://medium.com/p/8b41b6038b54</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[maven]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[nexus]]></category>
            <category><![CDATA[gradle]]></category>
            <dc:creator><![CDATA[Mohit Mayank]]></dc:creator>
            <pubDate>Sat, 04 Jul 2020 08:03:50 GMT</pubDate>
            <atom:updated>2020-07-05T04:39:23.721Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*MYeS7A-tdhGILpaXJbF-Ww.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@louis993546?utm_source=medium&amp;utm_medium=referral">Louis Tsai</a></figcaption></figure><h3>Introduction</h3><p>The most boring part of a library is the setting up part. Choosing the build systems, publishing builds, setting up code coverage, linting, etc etc. This blog is aimed at abstracting those concerns in the form of a simple file at the end of all this.</p><h3>Choosing a build system</h3><ul><li>Gradle</li><li>Maven</li></ul><p>Both Gradle and Maven have their own pros and cons. It is not in the scope of this article to delve into those details. However, I’d mention that Gradle tends to be a little bit faster while Maven has better support (in terms of IDE or plugins).</p><p>In this blog, we will only deal with Gradle.</p><h3>Initial set-up</h3><p>For a Kotlin project, our initial Gradle file looks something like this:</p><pre>plugins {<br>  id &#39;org.jetbrains.kotlin.jvm&#39; version &#39;1.3.72&#39;<br>}</pre><pre>group &#39;com.cueo&#39;<br>version &#39;1.0-SNAPSHOT&#39;</pre><pre>repositories {<br>  mavenLocal()<br>  mavenCentral()<br>}</pre><pre>dependencies {<br>  implementation &quot;org.jetbrains.kotlin:kotlin-stdlib-jdk8&quot;<br>}</pre><pre>compileKotlin {<br>  kotlinOptions.jvmTarget = &quot;1.8&quot;<br>}<br>compileTestKotlin {<br>  kotlinOptions.jvmTarget = &quot;1.8&quot;<br>}</pre><p>Few pointers about the above configuration:</p><ol><li>plugins In order to build your Kotlin project with Gradle you need to set-up the kotlin-gradle plugin. This is done by the line: id &#39;org.jetbrains.kotlin.jvm&#39; version &#39;1.3.72&#39;. (1.3.72 is the latest version at the time of writing).</li><li>repositories This is where you configure if you want to download the artifacts from Maven <a href="https://repo.maven.apache.org/maven2/">central</a>, a Nexus proxy or your organisation’s specific Nexus. Do NOT forget to add mavenLocal(). This speeds up loading of the dependencies even if you do not have them cached in the build folder. If some other project has a same dependency, it loads the dependency from Maven’s .m2 folder.</li><li>kotlinOptions.jvmTarget inside compileKotlin and compileTestKotlinensures the target JVM.</li></ol><h3>Linting</h3><p>A linter forces (and sometimes formats) the code to follow a coding style. If you work on a project with several team members, linting ensures the project follows a specific coding style, eg: use spaces instead of tabs, space before brackets and braces, space after commas, etc.</p><p>Let’s enable <a href="https://ktlint.github.io/">ktlint</a> in our project.</p><blockquote><em>An anti-bikeshedding Kotlin linter with built-in formatter.</em></blockquote><pre>plugins {<br>  ...<br>  id &#39;org.jlleitschuh.gradle.ktlint&#39; version &#39;9.2.1&#39;<br>}</pre><p>That’s it! Now you can run:</p><ul><li>gradle ktlint to check for errors.</li><li>gradle ktlintFormat to auto fix some of the <em>fixable</em> errors.</li></ul><p>Adding the plugin also automatically attaches itself to the build lifecycle.</p><h3>Code coverage</h3><p>We will use Gradle’s <a href="https://docs.gradle.org/current/userguide/jacoco_plugin.html">Jacoco</a> plugin for checking code coverage. Start by adding Jacoco to the list of plugins.</p><pre>plugins {<br>  ...<br>  id &#39;jacoco&#39;<br>}</pre><p>You can also make sure each every time gradle test runs, a coverage report is generated by Jacoco.</p><pre>test {<br>  finalizedBy jacocoTestReport<br>}</pre><p>Enable or disable Jacoco XML and HTML reports as follows:</p><pre>jacocoTestReport {<br>  reports {<br>    xml.enabled true<br>    html.enabled true<br>  }<br>}</pre><h3>Publish to Nexus</h3><p>Eventually you’d want to publish your library to Nexus so that other projects can add your library as a dependency. We will use Gradle’s maven-publish plugin to do that. You can find detailed description of the plugin <a href="https://docs.gradle.org/current/userguide/publishing_maven.html">here</a>.</p><p>Add the plugin to build.gradle:</p><pre>plugins {<br>  ...<br>  id &#39;maven-publish&#39;<br>}</pre><p>Configure the plugin:</p><pre>publishing {<br>  publications {<br>    mavenJava(MavenPublication) {<br>      artifactId = &#39;awesome-kotlin-lib&#39;<br>      from components.java<br>      pom {<br>        name = &#39;Awesome Kotlin Library&#39;<br>        description = &#39;An awesome library written in Kotlin built using Gradle.&#39;<br>      }<br>    }<br>  }<br>  repositories {<br>    maven {<br>      def snapshotsRepoUrl = &#39;https://your-nexus-url.com/snapshots&#39;<br>      def releasesRepoUrl = &#39;https://your-nexus-url.com/releases&#39;<br>      url = version.endsWith(&#39;SNAPSHOT&#39;) ? snapshotsRepoUrl : releasesRepoUrl</pre><pre>      credentials {<br>        username = System.getenv(&#39;NEXUS_USERNAME&#39;)<br>        password = System.getenv(&#39;NEXUS_PASSWORD&#39;)<br>      }<br>    }<br>  }<br>}</pre><p>The above configuration makes some assumptions about the maven repository:</p><ul><li>The URLs for SNAPSHOT and RELEASE builds are different. You might as well use the same endpoint for both SNAPSHOT and RELEASE builds.</li><li>Your system or CI is configured with the Nexus username and password. Locally you can just do the following before running gradle publish:</li></ul><pre>export NEXUS_USERNAME=&#39;username&#39;<br>export NEXUS_PASSWORD=&#39;unhackable-password&#39;</pre><p>Now gradle publish will publish your jar to the Nexus given you have sufficient permissions.</p><h3>Releases</h3><p>maven-publish is incomplete without a release plugin. While the former publishes the jar to the Nexus, the latter is responsible for bumping up the versions in Git and creating relevant tags. We will use <a href="https://github.com/researchgate/gradle-release">gradle-release</a> plugin by ResearchGate. As usual, start by adding the plugin.</p><pre>plugins {<br>  ...<br>  id &#39;net.researchgate.release&#39; version &#39;2.6.0&#39;<br>}</pre><p>Configure the plugin with your Github username and token.</p><pre>release {<br>  svn {<br>    username = System.getenv(&#39;GITHUB_USERNAME&#39;)<br>    password = System.getenv(&#39;GITHUB_TOKEN&#39;)<br>  }<br>}</pre><p>Make sure each gradle release is followed by publishing the new build to the Nexus.</p><pre>tasks {<br>  afterReleaseBuild {<br>    dependsOn publish<br>  }<br>}</pre><h3>Conclusion</h3><p>In the end our build.gradle file should look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7612b23e57325e6e0890c7b557ed393a/href">https://medium.com/media/7612b23e57325e6e0890c7b557ed393a/href</a></iframe><p>That’s all, folks! You can now go ahead and write tons of libraries helping out all the developers out there.</p><p>Cheers! 🍻</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8b41b6038b54" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>