<?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 Oscar Djupfeldt on Medium]]></title>
        <description><![CDATA[Stories by Oscar Djupfeldt on Medium]]></description>
        <link>https://medium.com/@oscdj113?source=rss-37937c385d61------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*x9ybUS-DCuRABIRXhzm7iA.jpeg</url>
            <title>Stories by Oscar Djupfeldt on Medium</title>
            <link>https://medium.com/@oscdj113?source=rss-37937c385d61------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 04:03:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@oscdj113/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[NeoniousOne]]></title>
            <link>https://medium.com/@oscdj113/neoniousone-698ec4ac80a9?source=rss-37937c385d61------2</link>
            <guid isPermaLink="false">https://medium.com/p/698ec4ac80a9</guid>
            <category><![CDATA[iot]]></category>
            <category><![CDATA[neoniousone]]></category>
            <category><![CDATA[arduino]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Oscar Djupfeldt]]></dc:creator>
            <pubDate>Tue, 19 Jun 2018 19:55:21 GMT</pubDate>
            <atom:updated>2018-06-19T19:55:21.054Z</atom:updated>
            <content:encoded><![CDATA[<p>A couple of weeks ago I was at JSConf EU in Berlin, while JavaScript might not be my language of choice at all times I find that it has it’s uses. While there I found a new one.</p><p>Every visitor got a free NeoniousOne Technology Preview board, it’s an ESP32 with some extra goodies included, like a step down power regulator so you can power it with voltages up to 50 V, a LPC822 switch matrix that allows you to configure the pin outs rather freely, two user programmable LEDs and more.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vBlYNLBn9fEccQWzSLQocA.jpeg" /><figcaption>NeoniousOne TP</figcaption></figure><p>The unique selling point of the NeoniousOne is not really the hardware though, it’s the on board software. It comes with a full, web based IDE with all the bells and whistles you expect; break points, code completion, package manager and more.</p><p>And it runs JavaScript.</p><p>Which is great if you want to experiment with hardware but find Arduino or similar micro controllers overwhelming. It also means that if you already are familiar with JS and Node, you will get started quickly and you have access to a very large number of software packages via NPM.</p><p>As I see it, there is one big issue with this. There are not really any NPM packages available for embedded software, at least not yet. However knowing the JS community this problem will go away rater quickly.</p><p>I got my hands on two copies of the board, with the first had some issues connecting to its access point after a factory reset. However after trying to connect a couple of times with my laptop I switched to my phone, which connected without problems. After that the laptop also connected. The second copy did not have this problem.</p><p>It’s only a technology preview, but I can see it’s potential, especially if the availability of software packages increases. While I personally think I would go for a bare ESP-8266 for its smaller size this is a very good alternative to an Arduino or RPi, though the RPi really solves another problem entirely.</p><p>For more info on the Neonious, check out their website and their crowdfunding.</p><p><a href="https://www.neonious.com/">neonious</a></p><p><a href="https://www.indiegogo.com/projects/neonious-one-javascript-es6-ts-board-with-low-js-computers-animation#/">Crowdfunding at Indiegogo.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=698ec4ac80a9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spring Boot and Hazelcast]]></title>
            <link>https://medium.com/@oscdj113/spring-boot-and-hazelcast-f68d42b2a83d?source=rss-37937c385d61------2</link>
            <guid isPermaLink="false">https://medium.com/p/f68d42b2a83d</guid>
            <category><![CDATA[caching]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[cache]]></category>
            <category><![CDATA[hazelcast]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Oscar Djupfeldt]]></dc:creator>
            <pubDate>Sun, 06 May 2018 07:06:21 GMT</pubDate>
            <atom:updated>2018-05-06T07:07:17.386Z</atom:updated>
            <content:encoded><![CDATA[<p>Hazelcast is an in-memory datagrid — a distributed cache. What this means is that each running instance connects in a cluster, sharing the stored information between all instances. This is useful when running multiple instances of a service or when different services need to share information.</p><p>Professionally I use Hazelcast in the first way. Consider a service running in a Kubernetes cluster with at least two replicas, the user won’t know which replica a request ends up in so they all need to keep track of the users session.</p><p>When combining Hazelcast with Spring Boot we can take advantage of Springs caching framework, which Hazelcast has excellent support for. This makes for a really neat and unintrusive integration.</p><p>Let’s start by creating a new Spring Boot project, once again I recommend Spring Initializr since it will set up the project with almost everything you need.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qoGWUcMvvHIgxD9fLcAWVg.png" /></figure><p>In this case, Hazelcast is not available, so we will need to add the dependencies manually.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50c24299cf798345f13ba2e35a1ceec3/href">https://medium.com/media/50c24299cf798345f13ba2e35a1ceec3/href</a></iframe><p>These are all the dependencies needed for the most basic case, however you may want to add additional dependencies to integrate Hazelcast with Kubernetes, AWS or some other external system.</p><p>I prefer working in Kotlin and the generated project sets the version to <em>1.2.20 </em>and at the time of writing the latest version is <em>1.2.41</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2085313e95d2e9b6345d953960ccfc88/href">https://medium.com/media/2085313e95d2e9b6345d953960ccfc88/href</a></iframe><p>Above is the code generated by Spring Initializr, it does nothing really so we will need to add some code. The first thing to do is to add a Hazelcast configuration bean.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/655991a244328a707c7250b5b18dda6a/href">https://medium.com/media/655991a244328a707c7250b5b18dda6a/href</a></iframe><p>I put the configuration bean inside the <em>HazelcastApplication</em> class, however you can put it in its own class annotated with <em>@Configuration</em>.</p><p>This configuration is very basic, we create a Config object and pass the Hazelcast instance name as parameter. On the Config object we set the property <em>hazelcast.phone.home.enabled </em>to <em>false</em>, this is not necessary but depending on your situation you may not want any outgoing data from your system.</p><p>We also add a MapConfig with the map name as parameter. A map is basically a cache for one type, it behaves in the same way as any Java or Kotlin Map mapping a key to an object. You can put objects of different types in to the same map, but you might end up with deserialisation issues. On the map I set the eviction policy to LRU (least recently used) and time to live to 240 seconds. Finally we need to add the annotation <em>@EnableCaching</em> to <em>HazelcastApplication</em>.</p><p>This is all the configuration needed and if you don’t care about cache eviction or Hazelcast phoning home you could cut four more lines of code. If you start the application now you should see that a Hazelcast instance is started.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ITqu7514J-qImjixYeucqA.png" /></figure><p>We now have a Spring Boot application that can use Hazelcast for caching. The next step is to actually cache something, for this we need two new classes. We need two classes because of how Spring caching works, the cacheable function needs to be called by proxy.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ca45cd8cac04111a51526fe1eff559b/href">https://medium.com/media/3ca45cd8cac04111a51526fe1eff559b/href</a></iframe><p><em>Controller</em> adds a simple REST controller that returns a UUID, via the function <em>getUUID</em> in the class <em>Cache</em>. The controller does nothing special, the magic happens in <em>Cache</em> which is annotated with <em>@CacheConfig</em>. The parameter <em>cacheNames</em> of <em>@CacheConfig</em> specifies which Hazelcast IMap to use, in our case “map”. This is strictly not necessary as Spring will find it anyway, however if you have many different maps, it could potentially increase performance.</p><p>The function <em>getUUID</em> is annotated with <em>@Cacheable</em>, which is what actually tells Spring to cache something, the return value of the annotated function. The parameters of the annotation, <em>value</em> and <em>key</em>, tells Spring where to cache and what key to use. Of these, only <em>value</em> is necessary and specifies which IMap to use. If no key is provided, the first parameter of the cacheable function is used.</p><p>If you run this, you should end up with an application that returns a new random UUID if you change the path variable but returns the same if you don’t, until the value is evicted after two minutes.</p><p>The next thing to have a look at is the clustering, which works with UDP multicasting by default. To test this, we can run several instances of our application locally, to avoid port conflicts change your application.properties to the following</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e44f5abe4ec7d9de3bc66ba8220d5bc8/href">https://medium.com/media/e44f5abe4ec7d9de3bc66ba8220d5bc8/href</a></iframe><p>This will start the application with a random port. This should allow you to run multiple instances of the application, and when you do you should see something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PjY5BvXSXy-gk0BifHnjnQ.png" /></figure><p>There are now two members in the cluster. If you try it out now, you should get the same value for the same input from both instances of the application. This is because they share the cache.</p><p>This is a very basic setup, and you probably want to replace the multicast discovery with something more suited to your environment, for example with <a href="https://github.com/hazelcast/hazelcast-kubernetes">Hazelcast Kubernetes</a> or <a href="https://github.com/hazelcast/hazelcast-aws">Hazelcast AWS</a></p><p>Some version of the code in this article is available at <a href="https://github.com/ozsie/hazelcast">Github</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f68d42b2a83d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spring Boot 2 with WebFlux & Kotlin]]></title>
            <link>https://medium.com/@oscdj113/spring-boot-2-with-webflux-kotlin-bb1a71167591?source=rss-37937c385d61------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb1a71167591</guid>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[reactive-programming]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[webflux]]></category>
            <dc:creator><![CDATA[Oscar Djupfeldt]]></dc:creator>
            <pubDate>Sun, 15 Apr 2018 12:54:03 GMT</pubDate>
            <atom:updated>2018-04-15T12:54:03.433Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UsCCA0mBRgf8OiMqAIJHUw.png" /><figcaption>Routing made fun!</figcaption></figure><p>With the release of Spring 5 and Spring Boot 2 Kotlin has moved up in the Spring world. It is now (almost) fully supported and Spring makes use of many of the features Kotlin bring, among those Kotlin DSL.</p><p>To get you started with WebFlux, I would recommend using <a href="https://start.spring.io/">Spring Initializr</a>, as it can generate a basic project with the dependencies you want. To get a simple project up and running, with some way of storing data and exposing it as a REST api, the following setup can be used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FOw31QHL4-vQ2SFRz39gQA.png" /></figure><p>The only change you will have to make is the scope of the Embedded MongoDB dependency, which by default is in the test scope. I would also recommend that you update the Kotlin version to the latest. You can have a look at the pom after the changes <a href="https://github.com/Ozsie/kotlin-webflux/blob/master/pom.xml">here</a>.</p><p>When the changes are made, you should end up with a project that compiles and runs, but without much going on. Lets add something to store.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/edcf706f41a62ae59b0bce1e8fd81b8b/href">https://medium.com/media/edcf706f41a62ae59b0bce1e8fd81b8b/href</a></iframe><p>This is a very simple Kotlin data class. The id is annotated with <em>@Id</em> from Spring Data, in the case of MongoDB this must be a String. We also want it to be nullable, so that MongoDB can generate it for us.</p><p>Next, we create a repository and add some Items to it. Adding items isn’t necessary, however in our case it simplifies testing our application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3e985f6307188b51a7c8b2524ba9bdd/href">https://medium.com/media/f3e985f6307188b51a7c8b2524ba9bdd/href</a></iframe><p>Line 8 is the repository, it’s just an interface that extends <em>ReactiveMongoRepository</em> with the types of what we store, <em>Item</em>, and the id, <em>String</em>.</p><p>Lines 10 to 18 is the code that adds <em>Items</em> to the repository. We use a <em>Flux</em> for this, as we add more than one <em>Item</em>. On this we call <em>flatMap</em> to store the data.</p><p>The last two rows is just to print out the added items, to get some direct confirmation that they have been added.</p><p>Now we have an Item repository, with some Items stored in it. To make things happen we will need some functions that operate on the repository.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e2b5863d52331c27c2e4087ff853716/href">https://medium.com/media/0e2b5863d52331c27c2e4087ff853716/href</a></iframe><p>This might be a lot to look at, but let’s focus on the <em>getAllItems</em> function. It takes <em>ServerRequest</em> as parameter, this is an object that represents a reactive web request. We have to have that argument, even if we don’t use it. This function must return a <em>ServerResponse</em> object.</p><p>We set the response content type to <em>application/json</em> and status to <em>OK</em> and as body we call the <em>findAll</em> function on the <em>ItemRepository</em>. As you remember, the <em>ItemRepository</em> is one line of code, the functions we use to interact with it have been provided for us.</p><p>Let’s look at <em>getItem</em> next, it’s almost the same, but this time we make use of the request to find an <em>Item</em> by id. We also add a call to <em>switchIfEmpty</em> to return a <em>404 Not Found</em> if no <em>Item</em> with the provided id was found.</p><p>The other functions follow the same basic pattern, we get a <em>ServerRequest</em>, create a <em>ServerResponse</em> and operate on it, using the data from the <em>ServerRequest</em> if needed. In the case of <em>updateItem</em> we use the <em>zipWith</em> function to merge the incoming update with the existing object by creating a new <em>Item</em> with the old <em>id</em> and the new <em>value</em>, <em>flatMap</em> it with the <em>saveAndRespond</em> function and finally, call <em>switchIfEmpty</em> and return 404 Not Found if the supplied <em>id</em> did not match any <em>Items</em>.</p><p>To map these functions to REST endpoints we will use the new Spring routing DSL (domain specific language) which is available to us in Kotlin. Using the DSL, in my opinion, makes it clearer what we are doing, especially in this case.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b35375d907c3b791892cfaf5f4b03d77/href">https://medium.com/media/b35375d907c3b791892cfaf5f4b03d77/href</a></iframe><p>The function <em>router</em> is assigned <em>router</em> from the Spring reactive web framework, which takes a function as parameter. In this we can hierarchically create our routes. The <em>ItemHandler</em> is injected by Spring.</p><p>First, all routes accept <em>application/json</em>, on this we nest <strong>/api</strong> as root but with no endpoints. On <strong>/api</strong> we nest two more routes, <strong>/item</strong> for single objects and <strong>/items</strong> for multiple objects. On both of these we nest again, on <strong>/items</strong> with just one route, a GET endpoint on <strong>/</strong> to get all Items. The <em>GET</em> function takes two arguments, the first is the path of the route (nested with paths above it in the hierarchy) and the second is the handler function, in our case <em>itemHandler::getAllItems</em>. Under <strong>/item</strong> we nest a PUT endpoint, a GET endpoint and a POST endpoint and supply the corresponding handler function. In the case of PUT and GET the paths have a path argument, <em>{id}</em>, which we can access from the <em>ServerRequest</em> object.</p><p>You may have noticed that the only relevant annotation used so far is <em>@Id</em> on the id of <em>Item</em>. No <em>@Service</em> or <em>@Component</em> or <em>@Resource</em>. Instead we will make use of another part of the Spring DSL to define our Beans.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/927748b2acb02805e9744de95872c388/href">https://medium.com/media/927748b2acb02805e9744de95872c388/href</a></iframe><p>We have three beans, <em>ItemHandler</em>, <em>ItemRouter</em> and the <em>CommandLineRunner</em> used to put some <em>Items</em> into the database. The <em>ItemRepository</em> does not need any further definition since it inherits the <em>ReactiveMongoRepository</em>.</p><p>Line 12 defines the <em>ItemHandler</em> bean by calling bean with a function that calls the constructor of <em>ItemHandler</em>, with the return value of <em>ref()</em> as parameter. Calling ref() find the correct Bean for the class we instantiate.</p><p>Lines 13 and 14 are a bit different, we call a function and supply ref() as parameter, the object returned from the function is the Bean.</p><p>To get this class to actually do something, we inherit the interface <em>ApplicationContextInitializer&lt;GenericApplicationContext&gt;</em> which gives ut the function <em>initialize</em>. We can now use a property to tell Spring to use the class <em>Beans</em> to define our Beans.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3caeb2e6f8ee7f498e1025b860fc2954/href">https://medium.com/media/3caeb2e6f8ee7f498e1025b860fc2954/href</a></iframe><p>With this, everything we need is in place. The full source code is available at <a href="https://github.com/Ozsie/kotlin-webflux">Github</a>. This should get you started with Spring Boot 2 and WebFlux using Kotlin.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb1a71167591" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Detekt + Maven]]></title>
            <link>https://medium.com/@oscdj113/detekt-maven-bf204940e430?source=rss-37937c385d61------2</link>
            <guid isPermaLink="false">https://medium.com/p/bf204940e430</guid>
            <category><![CDATA[detekt]]></category>
            <category><![CDATA[maven]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Oscar Djupfeldt]]></dc:creator>
            <pubDate>Thu, 12 Apr 2018 19:45:57 GMT</pubDate>
            <atom:updated>2018-04-12T19:45:57.091Z</atom:updated>
            <content:encoded><![CDATA[<p>Detekt is a static code analysis tool for Kotlin, it comes with a cli and a Gradle plugin. However, if Maven is your goto build automation tool adding detekt to your build is a bit more cumbersome. The example in the detekt readme uses the maven-antrun-plugin to execute the detekt CLI.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8225d2394ee34178615d4d61b696bea7/href">https://medium.com/media/8225d2394ee34178615d4d61b696bea7/href</a></iframe><p>A lot of lines to run detekt when building.</p><p>Enter me, attempting to introduce Kotlin at my workplace. One issue in the way is the matter of static code analysis and detekt seems, at the moment, to be the tool to use. We also use Maven for building. This prompted me to have a look at building Maven plugins, which by the way is not that hard.</p><p>Taking the simplest approach, the detekt maven plugin is based on the detekt CLI, basically creating the parameters from Maven properties instead of CLI parameters. This means that the detekt maven plugin does almost exactly the same thing as the above antrun plugin configuration does, but with less XML.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc75491e506e5902c2e4f8c2e9e8fa7c/href">https://medium.com/media/fc75491e506e5902c2e4f8c2e9e8fa7c/href</a></iframe><p>Apart from this, which runs detekt in the verify phase using the check goal, the detekt maven plugin has one goal for creating a baseline and one for generating a default configuration file.</p><ul><li>mvn detekt:create-baseline</li><li>mvn detekt:generate-config</li></ul><p>More detailed information on the usage of the plugin is available at the <a href="https://github.com/Ozsie/detekt-maven-plugin">Github repository</a>.</p><p>To conclude, writing the detekt maven plugin has given me two things, knowledge of how to write Maven plugins and a more concise pom. It should also make it easier to avoid configuration errors since the plugin dictates what configuration parameters are available unlike the more generic antrun plugin.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bf204940e430" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kotlin & Coverity]]></title>
            <link>https://medium.com/@oscdj113/kotlin-coverity-949b900b9ee2?source=rss-37937c385d61------2</link>
            <guid isPermaLink="false">https://medium.com/p/949b900b9ee2</guid>
            <category><![CDATA[static-code-analysis]]></category>
            <category><![CDATA[moshi]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[coverity]]></category>
            <category><![CDATA[detekt]]></category>
            <dc:creator><![CDATA[Oscar Djupfeldt]]></dc:creator>
            <pubDate>Fri, 09 Mar 2018 15:02:22 GMT</pubDate>
            <atom:updated>2018-03-12T07:11:18.740Z</atom:updated>
            <content:encoded><![CDATA[<p>Where I work at the moment, Coverity has recently been introduced to perform static code analysis. Since we mostly work in Java, this works out fine, except for the increased build times.</p><p>There is also an interest in introducing Kotlin. This does not work out fine with regards to Coverity. At the moment there is no plan to add Kotlin to the set of languages that Coverity can handle.</p><p>There are however other tools that can perform at least parts of the analysis that Coverity provides. One of them is Detekt which, as it happens, is very extensible. This in combination with the fact that Coverity has tools to import data from third party tools means that it is possible to cover some issues with minimal effort.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4384c255a6912de4d7731265db0badcb/href">https://medium.com/media/4384c255a6912de4d7731265db0badcb/href</a></iframe><p>Above is a minimal extension to <em>OutputReport</em> from the Detekt API. The in parameter to the <em>render</em> function is all the detected problems, our job is to map this to a format that Coverity can parse. The format is available in the Coverity documentation, which seems to be behind pay walls.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d67063ab9290b84adf3c8af8a2922270/href">https://medium.com/media/d67063ab9290b84adf3c8af8a2922270/href</a></iframe><p>This is a mapping of the format to Kotlin classes. It’s pretty compact and a many of the fields cannot easily be mapped from Detekt, but this is fine since not all fields are required and many can be filled with duplicate data.</p><p>Let’s flesh out the generator a bit.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dbccc00971b8b6d79a16c1a7bfbf1a37/href">https://medium.com/media/dbccc00971b8b6d79a16c1a7bfbf1a37/href</a></iframe><p>Doing a quick google for Kotlin and JSON, I found Moshi which seems a reasonable way to convert my Report object to JSON. We now have a model for the report and a way to convert it to JSON. The next step is to map the input to the output.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8063b15a1ec65812c17fa68a21bbb2ea/href">https://medium.com/media/8063b15a1ec65812c17fa68a21bbb2ea/href</a></iframe><p>The above snippet goes into the render function of <em>CoverityReport</em>. What it does is create an <em>Issue</em> and an <em>Event</em> for each Detekt <em>Finding</em>. By doing some guesswork as to what values map to what we can generate something that at least superficially passes for something that Coverity can import.</p><p>Finally we need to add a file to META-INF/services</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/afc4c2c4beab4b07589af146183acde0/href">https://medium.com/media/afc4c2c4beab4b07589af146183acde0/href</a></iframe><p>This file tells Detekt that our class CoverityReport exists and is an OutputReport extension.</p><p>To actually use this plugin you can configure your pom, or gradle file for that matter, to run Detekt.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12adc07e77d95635278fa6ffd1bc33e4/href">https://medium.com/media/12adc07e77d95635278fa6ffd1bc33e4/href</a></iframe><p>By adding this to your pom Detekt will be run at the verify phase and outputting detekt/coverity.json in addition to the default formats.</p><p>At the time of writing I have not been able to verify that the resulting file actually works, however this does not change the principle. Using Detekt together with Detekt Coverity Report you will (at least in the future) be able to use Coverity to handle the results of static code analysis for Kotlin.</p><p>There is more work to be done on the plugin, apart from the verification of the output. Documentation and testing are next on the agenda, as well as some CI.</p><p>The full source code embedded in the article is available in the <a href="https://github.com/Ozsie/detekt-coverity-report">Detekt Coverity Report Github repository</a></p><h4>References</h4><ul><li>Detekt — <a href="https://github.com/arturbosch/detekt">https://github.com/arturbosch/detekt</a></li><li>Detekt Coverity Report — <a href="https://github.com/Ozsie/detekt-coverity-report">https://github.com/Ozsie/detekt-coverity-report</a></li><li>Moshi — <a href="https://github.com/square/moshi">https://github.com/square/moshi</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=949b900b9ee2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a wifi-enabled thermometer]]></title>
            <link>https://medium.com/@oscdj113/building-a-wifi-enabled-thermometer-342f40a1bf94?source=rss-37937c385d61------2</link>
            <guid isPermaLink="false">https://medium.com/p/342f40a1bf94</guid>
            <category><![CDATA[esp8266]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[ds18b20]]></category>
            <category><![CDATA[esp-12f]]></category>
            <category><![CDATA[arduino]]></category>
            <dc:creator><![CDATA[Oscar Djupfeldt]]></dc:creator>
            <pubDate>Wed, 10 Jan 2018 20:51:26 GMT</pubDate>
            <atom:updated>2018-01-10T20:51:26.988Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<p>What do you do when you have lot of free time and access to cheap electronics? You build something simple while trying to learn things, a WiFi-enabled thermometer.</p><p>If you have some prior programming knowledge, you should be able to finish this project in a day or less. If you are new to this, consider booking a weekend.</p><p>The goal of this project was to build a battery powered and wifi-enabled thermometer with a small footprint, preferably something that could be placed in all rooms of my home without being noticed. This required a small microcontroller with wifi that was also power efficient. Since I wanted more of them in the long term, something cheap was also important.</p><p>For this project I opted for an ESP-7 or ESP-12F, programmable wifi-modules based on the ESP8266 wifi and microcontroller chip. Both ESP-7 and ESP-12F are rather small as they are designed primarily to be attached to a PCB. They both expose almost the same pins from the ESP8266. The ESP-7 is a bit smaller, exposes fewer pins and has a smaller built-in antenna, but it comes with connector for an external antenna. The ESP-12F is marginally bigger, it exposes more pins and has a better built in antenna, but no connector for an external one.</p><p>For this project, either one is fine.</p><h3>Hardware</h3><p>First, a list of the required hardware:</p><ul><li>1x ESP-12F</li><li>4x 10 kΩ resistors</li><li>1x 4.7 kΩ resistor</li><li>1x DS18B20 digital thermometer</li><li>1x BAT42 DO-35 30V 200mA Schottky diode</li><li>1x 3.7 V battery, or some other 3 V power source</li></ul><p>All this can be bought for less than €10, the most expensive part is usually the ESP-12F or the battery. You will also need some form of USB to serial adapter. You may also want to invest in a breadboard, some pinstrips and jumper cables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/1*gSLQ7B7WunvtGmBVuLjp2w.png" /><figcaption>Development breadboard layout</figcaption></figure><p>This is a rather compact development setup, mostly to fit in a smaller image.</p><ul><li>EN -&gt; Vcc via 10 kΩ resistor</li><li>GPIO0 -&gt; Vcc via 10 kΩ resistor</li><li>RST -&gt; Vcc via 10 kΩ resistor</li><li>GPIO15 -&gt; GND via 10 kΩ resistor</li></ul><p>GPIO0 also connects to GND via a jumper (PROG), if the circuit is closed the ESP-12F is put into programming mode. Make sure this circuit is closed when programming the board.</p><p>RST also connects to GND via a jumper, if the circuit is closed the ESP-12F is restarted.</p><p>GPIO14 connects to GND via a jumper. If the circuit is closed, the program configuration is reset on boot. I will explain this more later on.</p><p>GPIO2 connects to the datapin (middle) of the DS18B20, with a 4.7 kΩ resistor connected to the Vcc pin (right) which in turn connects to Vcc. The GND pin (left) connects to GND.</p><p>RX connects to TX on the USB/serial adapter and TX connects to RX on the adapter. You might also have to connect GND on the USB/serial adapter to GND on the breadboard.</p><p>Connect your power source to the Vcc and GND rails respectively.</p><p>If you prefer to follow a circuit diagram, the one below represents the above breadboard. If you are interested in tweaking these by yourself, they are available in the <a href="https://github.com/Ozsie/wifiTemp">Github repository</a> as a <a href="http://fritzing.org/home/">Fritzing</a> file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w8GDb4FNiNVKSSUT-S8LMA.png" /><figcaption>Circuit diagram</figcaption></figure><p>Using this setup, you will be able to program the ESP-12F using the method of your choice. I used Arduino, since I was already familiar with it, but this setup should also work with NodeMCU.</p><h3>Software</h3><p>The full source code for the project is available at <a href="https://github.com/Ozsie/wifiTemp">https://github.com/Ozsie/wifiTemp</a></p><p>First of all you need to install Arduino, it can be <a href="https://www.arduino.cc/en/Main/Software">downloaded from their website</a>. You also need to install the ESP8266 boards, I personally prefer using the boards manager. Instructions for how to do this are available on the <a href="https://github.com/esp8266/Arduino#installing-with-boards-manager">ESP8266 Arduino Github repository</a>.</p><p>Once the boards are installed, select the board “Generic ESP8266 Module” from the Tools menu. Set the upload speed to “11520”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*Y7--LvdthqBGQhDv1o0DhA.png" /><figcaption>Board settings</figcaption></figure><p>The settings in the tools menu should look something like this. You should now be able to build and program your ESP-12F. Once again, make sure the “Prog” jumper is closed.</p><p>The resulting program is not very interesting and to access the DS18B20 you need to add a library to the Arduino libraries folder. On windows it’s usually located in “c:\Users\&lt;user&gt;\Documents\Arduino\libraries” and on OSX in “~/Documents/Arduino/libraries”.</p><p>I used <a href="https://github.com/milesburton/Arduino-Temperature-Control-Library">Arduino Temperature Control Library from Miles Burton</a>, download either the latest release or the master branch and unzip into the Arduino libraries folder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*_oGhgBBP99yebU_Uv-HS8w.png" /></figure><p>Your empty sketch should look something like this</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/652981f1096902f656e6d9f8ba5ff0ff/href">https://medium.com/media/652981f1096902f656e6d9f8ba5ff0ff/href</a></iframe><p>If you are new to Arduino, the first function (setup) is run once on start and the second function (loop) is run continuously.</p><p>To make something that actually does something interesting, let’s add some code to the setup function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7dca1061cb540d83a56aca469ff69bc/href">https://medium.com/media/e7dca1061cb540d83a56aca469ff69bc/href</a></iframe><p>At this point, it’s a good idea to open the Serial monitor from the Tools menu in Arduino. If you build and program this, you should see “Hello World” printed in the serial monitor. Still not much fun, but at least you can verify that everything except the DS18B20 works.</p><p>On that note, let’s try the sensor out. Change your code to look as below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e158fb7f334f27f8114ef0dc3c519d5/href">https://medium.com/media/9e158fb7f334f27f8114ef0dc3c519d5/href</a></iframe><p>What we are doing here is using the library we downloaded earlier.</p><p>Lines 4 to 6 defines the GPIO pin we expect the sensor to be connected to, in our case GPIO2.</p><p>We have also added some code to the loop function. Every 100 milliseconds we try to read the temperature from from the sensor until it is not 85 or -127.</p><p>The sensor defaults to 85 degrees celcius, so the first few reads will most likely return this value. If the result is -127 degrees celcius something is wrong with either the circuit or the sensor itself.</p><p>When a temperature that is not 85 or -127 is read the loop function is run again from the top. This is a bit more interesting than doing nothing, but not that useful.</p><p>Let’s leave the sensor for a while and have a look at some of the other things that the ESP-12F can do. When putting my thermometer together I needed some way to store configuration, for example SSID, password and somewhere to send the measured temperature. The simplest route here is to hard code them into the program, this however comes with other issues.</p><p>There are two different ways to store data on the ESP, either in EEPROM or in SPIFFS. SPIFFS is a filesystem, it is more useful for storing html and javascript, tough you could store a configuration file there. If you use this approach, you could download the file when programming the board but this comes with some of the same issues as hard coding. I opted for EEPROM to store my configuration.</p><p>The EEPROM is kept when programming the board, this can be frustrating when trying things out, but when you have your configuration set you can just try any changes you make without configuration.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93e4fda133753d84df5e3351b56ed38f/href">https://medium.com/media/93e4fda133753d84df5e3351b56ed38f/href</a></iframe><p>First, let’s move the includes and global variables to a header file. In this file we also include EEPROM.h to get access to the EEPROM.</p><p>Line 12 to 15 defines a struct that holds our configuration and line 19 calculates the space required to store the configuration. The last part of the line, + sizeof(“OK”), is there because we add a check at the end to verify that we have read all that we need.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7932fe3453dba7240ce781f08826988/href">https://medium.com/media/d7932fe3453dba7240ce781f08826988/href</a></iframe><p>The next thing to do is to add another file to the sketch, you can call it whatever you want but I called it “storageEeprom”. In this file we define two functions, one to read from EEPROM (loadEeprom) and one to write to EEPROM (storeEeprom).</p><p>Let’s look at storeEeprom first. The first line in the function, line 4, tells the EEPROM how much of it we want access to. In our case it’s the size of the Configuration struct plus the size of “OK”.</p><p>The second line writes the contents of conf to the EEPROM at byte 0. Next we define our “OK” check and write it at byte 0+sizeof(conf), that is right after conf.</p><p>EEPROM.write() does not actually write to EEPROM, instead it writes to RAM.</p><p>Next we commit the result, this is when our data is written to EEPROM. The return value will be true if everything went ok and false otherwise. EEPROM.end() actually also commits, but does not return anything, however it does clear the RAM.</p><p>Reading from EEPROM is very similar to writing. First, just as with writing, we need to specify how much of the EEPROM we want to use. After that we can use EEPROM.get() to read at any byte within the specified space. Lines 20 to 30 is a check to see if we got everything we wanted and if not reset what we read.</p><p>Now we have a way of storing and retrieving configuration, let’s put it to use.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cae086ee2fbe9b21e5c87c1a057a2b89/href">https://medium.com/media/cae086ee2fbe9b21e5c87c1a057a2b89/href</a></iframe><p>Create a file in the sketch called “setup”</p><p>The function on line 3 is used to check if the configuration is loaded, it might seem unnecessary to put it in its own function, but later on we will add more to it.</p><p>Lines 11 to 13 is there mainly to get an idea of how much space we are using.</p><p>Line 15 opens GPIO14 for reading, if you remember from earlier we connected <a href="#36bd">GPIO14 to GND via a jumper</a>. This is where it comes into play, we read the state of the pin, which is HIGH when the circuit is open. However if the circuit is closed the state is LOW and we call the function storeEeprom(). At this instance it will result in a clear, since our instance of Configuration is empty.</p><p>Finally we call loadEeprom() and check if the configuration was loaded.</p><p>Now we need a way to get the configuration into the ESP-12F. To do this we can use one of the main selling points of the ESP, WIFI. The ESP can act both as an access point and a client and taking inspiration from the Chromecast we can run it as an access point first and when configuration is received we switch to client.</p><p>We need to add some things to the header file, lines 4 and 5 includes the libraries needed for wifi and http server.</p><p>Line 10 creates an instance of the web server on port 80.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01fffde3228dcc2652de4d4d6fb4aa7d/href">https://medium.com/media/01fffde3228dcc2652de4d4d6fb4aa7d/href</a></iframe><p>Now that we have an instance of a web server, we can use it to receive configuration. Since we at this point have no knowledge of your SSID, we setup the ESP as an access point. For this we create a new file in the sketch, “setup”.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac709112551640df86231bd644b34d14/href">https://medium.com/media/ac709112551640df86231bd644b34d14/href</a></iframe><p>Let’s look at setupServer() first, most of the function is information sent over serial, so that we get the needed information.</p><p>Lines 9 and 10 define the SSID and password of our AP and line 12 starts it. We send the IP address over serial so that we know where to send the configuration.</p><p>Finally on line 23 we tell our webserver to use the function handleConfig() when root is called.</p><p>The function handleConfig() looks at the incoming parameters and stores them in their corresponding variable in conf. Parameters other than “ssid” and “password” are ignored and if the expected parameters are missing we return a short html snippet explaining this.</p><p>When all expected parameters are there, “OK!” is returned to the client the configuration is stored in EEPROM.</p><p>Now we can both store and retrieve configuration from EEPROM, the next step is to use the configuration to connect to your network.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/87d52c561c107376423919f277fffac1/href">https://medium.com/media/87d52c561c107376423919f277fffac1/href</a></iframe><p>On line 24 we call setupServer() instead of just printing an error on serial. As you may have figured out by now, or already knew, all files in the sketch are appended to each other.</p><p>There are some changes in the loop function, I added some limits to the attempts to read from the sensor. Lines 51 and 54 put the ESP in deep sleep mode, this is to prolong battery life. There is no need to measure constantly.</p><p>At the beginning of the loop function we once again check if the configuration is loaded, if it is not we tell the webserver to handle any client.</p><p>When the configuration has been received we try to use it, as long as the ESP is not connected to the WIFI SSID provided in the configuration we try to connect by calling wifiConnect().</p><p>This function is defined on line 60. First we make sure that we are not connected to any other network and after that we try to connect by calling WiFi.begin().</p><p>You may have noticed a call to sendTemperature() on line 53. This function is were our measurement is published to the world.</p><p>To get the temperature to somewhere other than the ESP you could send it over serial to some other computer, however that would render all the hassle with configuration moot and I want my thermometer to be completely wireless.</p><p>This means that we need somewhere to send the measurement. I opted for Firebase since I have used it before, but you just as well use AWS, Azure or your own server or any other solution really.</p><p>Add another file to the sketch, I called it “jsonTools”.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7406d20510c73cea1b73dee93aa8ab47/href">https://medium.com/media/7406d20510c73cea1b73dee93aa8ab47/href</a></iframe><p>It contains one function, it’s rather stupid but does it’s job finding the value of a key in a JSON string, but only at root level. This is fine for what we will use it for.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f03497ab1737fd1e447873594058ec4/href">https://medium.com/media/1f03497ab1737fd1e447873594058ec4/href</a></iframe><p>Create one more file in the sketch, let’s call it “firebase”. There are two functions here, the first is used to authenticate a Firebase user by connecting to <a href="http://www.googleapis.com/">www.googleapis.com</a> and posting the credentials.</p><p>Line 7 through 11 attempts to connect to the server at port 443. If this succeeds a POST request is sent with the credentials. You may notice some new values in the Configuration struct on lines 19 and 20, you should be able to figure out how to add them correctly by now.</p><p>Lines 23 to 29 are the POST request, with signInUrl and payload defined above.</p><p>To minimise the run time we wait at most one second for a response, if this time is exceeded the sign in failed from our perspective and “error” is returned.</p><p>If we get a response within our time limit we call the function we defined earlier, findJsonKey() and return it’s result.</p><p>The function sendTemperature() works on the same principle as signIn(), we connect to a server, this time from our configuration. This server, conf.hubIp, would in the case of Firebase be &lt;project-id&gt;.firebaseio.com and the port is 443.</p><p>I will not go into how my Firebase rules are configured here, but the definition is available in my <a href="https://github.com/Ozsie/wifiTemp">Github repository</a>. Suffice to say we now get use for the sign in token returned earlier.</p><p>The rest of this function posts the measured temperature to Firebase and disregards any response. I don’t care if it worked or not, if it did I do not care about the contents and if it did not I check for missing reports in the other end, this is beyond the scope of this article, but once again you can find the code for that in the <a href="https://github.com/Ozsie/wifiTemp">Github repository</a>.</p><p>That completes this guide, the full source code is available in my <a href="https://github.com/Ozsie/wifiTemp">Github respository</a>. If you have any questions, feel free to reach out to me.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*meRdCM_s13yjMUl7BHT1XQ.jpeg" /><figcaption>My development setup, in all its messy glory</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=342f40a1bf94" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>