<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.2">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2022-10-21T05:38:17+00:00</updated><id>/feed.xml</id><title type="html">Dominik Kapusta</title><subtitle></subtitle><entry><title type="html">Symbolicate macOS (and iOS) apps crash reports on Linux</title><link href="/2022/06/13/symbolicate-macos-crash-reports-on-linux" rel="alternate" type="text/html" title="Symbolicate macOS (and iOS) apps crash reports on Linux" /><published>2022-06-13T00:00:00+00:00</published><updated>2022-06-13T00:00:00+00:00</updated><id>/2022/06/13/symbolicate-macos-crashes-on-linux</id><content type="html" xml:base="/2022/06/13/symbolicate-macos-crash-reports-on-linux">&lt;p&gt;If you need to symbolicate crash reports for your macOS or iOS app yourself,
you’d probably use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;atos&lt;/code&gt; command line utility to get the job done. Apple has
in-depth &lt;a href=&quot;https://developer.apple.com/documentation/xcode/adding-identifiable-symbol-names-to-a-crash-report&quot;&gt;documentation&lt;/a&gt;
on crash reports symbolication, and they recommend &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;atos&lt;/code&gt; in the section called
&lt;a href=&quot;https://developer.apple.com/documentation/xcode/adding-identifiable-symbol-names-to-a-crash-report#Symbolicate-the-Crash-Report-with-the-Command-Line&quot;&gt;Symbolicate the Crash Report with the Command Line&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Basically what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;atos&lt;/code&gt; does is:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;take the dSYM file, the architecture name and the load address of the app’s binary image,&lt;/li&gt;
  &lt;li&gt;take the list of memory addresses to symbolicate,&lt;/li&gt;
  &lt;li&gt;return the list of symbols identifying the memory addresses in question.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you ever wanted to automate symbolication (not relying on a 3rd party
service) you might have noticed that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;atos&lt;/code&gt; is only available on macOS.
This limits your options for performing symbolication server-side, requiring
you to provision a Mac server – and these machines tend to be expensive and/or cumbersome to maintain.&lt;/p&gt;

&lt;p&gt;However, it appears that you still can symbolicate an Apple-platform crash report
on Linux. It only takes a bit more &lt;a href=&quot;https://xkcd.com/196/&quot;&gt;Command Line Fu&lt;/a&gt;.&lt;/p&gt;

&lt;h1 id=&quot;sample-app&quot;&gt;Sample app&lt;/h1&gt;

&lt;p&gt;In this article we’ll work with a sample application that crashes. It contains
just a single window and a button. Tapping the button would attempt to update
a non-existent text field:&lt;/p&gt;

&lt;div class=&quot;language-swift highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ViewController&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;NSViewController&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;@inline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;never&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;@IBAction&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;buttonAction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sender&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;updateLabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;@inline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;never&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;updateLabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stringValue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Boom&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;weak&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;NSTextField&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@inline(never)&lt;/code&gt; attribute is there to instruct the compiler to not try and
inline the functions (collapsing the stack trace), which would make this example harder
to follow. It should not be used here in a real-life scenario.&lt;/p&gt;

&lt;p&gt;A sample crash stack trace (edited for brevity) looks like this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   SampleCrashingApp        0x1004aeb3c 0x1004ac000 + 11068
1   SampleCrashingApp        0x1004aeaf4 0x1004ac000 + 10996
2   SampleCrashingApp        0x1004aeab8 0x1004ac000 + 10936
3   AppKit                   0x185c9bbc4 -[NSApplication(NSResponder) sendAction:to:from:] + 460
    [... more AppKit frames]
16  AppKit                   0x185ec0848 -[NSApplication _handleEvent:] + 76
17  AppKit                   0x185a88618 -[NSApplication run] + 636
18  AppKit                   0x185a59d08 NSApplicationMain + 1132
19  SampleCrashingApp        0x1004aecf4 0x1004ac000 + 11508
20  dyld                     0x1006c1088 start + 516
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;environment-setup&quot;&gt;Environment setup&lt;/h1&gt;

&lt;p&gt;In the absence of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;atos&lt;/code&gt; on Linux, you’ll need the following tools to get the job done:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;llvm-nm&lt;/em&gt;: to find the base image address as stored in the dSYM file,&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;llvm-addr2line&lt;/em&gt;: to map symbol addresses to specific lines of code,&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;swift-demangle&lt;/em&gt;: to convert mangled Swift symbol names to a human-readable form.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first two are included in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;llvm&lt;/code&gt; package available in main package tree on most
Linux distributions, including Debian-based (e.g. Ubuntu) and RHEL-based (e.g. Amazon Linux).
The third one requires Swift to be installed in the system. Swift, in turn, is likely
unavailable from your preferred Linux distro’s package manager, so you’d have to install
it manually or use a Docker image (as listed in the &lt;a href=&quot;https://www.swift.org/download/&quot;&gt;Downloads&lt;/a&gt; page at swift.org).&lt;/p&gt;

&lt;p&gt;Apart from the tools, you’ll need your crash report and your app’s debug symbols (as a dSYM bundle).&lt;/p&gt;

&lt;h1 id=&quot;symbolication&quot;&gt;Symbolication&lt;/h1&gt;

&lt;h2 id=&quot;note-always-specify-architecture&quot;&gt;Note: always specify architecture&lt;/h2&gt;

&lt;p&gt;Please note that you must observe the architecture specified in the crash report (x86_64 vs arm64)
and pass it over to symbolication-related commands. This is because app (and dSYMs) are fat
binaries containing sets of binary code supporting specific architectures. The debug symbol addresses
will differ between architectures.&lt;/p&gt;

&lt;h2 id=&quot;get-the-binary-image-load-address&quot;&gt;Get the binary image load address&lt;/h2&gt;

&lt;p&gt;Unsymbolicated stack frames are described using binary image load address and offset, for instance:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;1   SampleCrashingApp        0x1004aeaf4 0x1004ac000 + 10996
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Let’s have a look at the numbers on the right hand side:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x1004aeaf4&lt;/code&gt; is a symbol address,&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x1004ac000&lt;/code&gt; is a binary image load address,&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10996&lt;/code&gt; is symbol’s offset within a binary image.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The symbol address is a sum of the binary image load address and the symbol offset (hence the plus sign).
Note that the left operand is a hexadecimal number while the right operand is decimal… 🤯
The binary image load address varies between application sessions, and symbol offsets
are constant per binary image (i.e. an offset for a given symbol will be the same
across application sessions).&lt;/p&gt;

&lt;p&gt;When using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;atos&lt;/code&gt; you need to pass the load address and the symbol address as parameters.
With &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;llvm-addr2line&lt;/code&gt; it’s a bit different:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;You only pass the symbol address, without the load address.&lt;/li&gt;
  &lt;li&gt;The symbol address is different than the one specified in the crash report 💥.
  To be more specific, it’s the load address part that differs. Instead of an
  arbitrary, session-dependent value, it seems to be fixed to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x100000000&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Arguably it makes more sense this way – after all the tool operates on the dSYM file so
it looks up the same addresses for the same symbols all the time. Likely it’s just a convenience
of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;atos&lt;/code&gt; that it works with addresses present in the crash report, and under the hood it
just subtracts load address from the symbol address and works on the resulting offset.&lt;/p&gt;

&lt;p&gt;Anyways, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x100000000&lt;/code&gt; magic number is actually present in the dSYM (and the app binary)
symbol table and can be extracted with the following command:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;llvm-nm &lt;span class=&quot;nt&quot;&gt;-P&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--arch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;arm64 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    SampleCrashingApp.app.dSYM/Contents/Resources/DWARF/SampleCrashingApp &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;__mh_execute_header
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The above would output:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;__mh_execute_header T 100000000 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;resolve-addresses-to-symbols&quot;&gt;Resolve addresses to symbols&lt;/h2&gt;

&lt;p&gt;Knowing the normalized load address, we can proceed to computing symbol addresses
and resolving them. A little calculation is required here. Let’s go back to our stack trace:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0   SampleCrashingApp        0x1004aeb3c 0x1004ac000 + 11068
1   SampleCrashingApp        0x1004aeaf4 0x1004ac000 + 10996
2   SampleCrashingApp        0x1004aeab8 0x1004ac000 + 10936
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We don’t care about addresses anymore, and are only interested in symbol offsets,
i.e. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;11068&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10996&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10936&lt;/code&gt; (decimals!). Those need to be added to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x100000000&lt;/code&gt;, making:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x100000000&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;11068&lt;/code&gt; = &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x100002b3c&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x100000000&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10996&lt;/code&gt; = &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x100002af4&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x100000000&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10936&lt;/code&gt; = &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x100002ab8&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These addresses can now be passed to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;llvm-addr2line&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;llvm-addr2line &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--default-arch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;arm64 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;--obj&lt;/span&gt; SampleCrashingApp.app.dSYM/Contents/Resources/DWARF/SampleCrashingApp &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    0x100002af4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This tool would output the source code location (file and line number) of the function
represented by the stack frame. Adding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-f&lt;/code&gt; will make it also print the function name.
Here is a sample output (always 2 lines):&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$s17SampleCrashingApp14ViewControllerC11updateLabel33_CEBA0EBCB7099BA3FFA1062F19F801EELLyyF
/Users/user/code/SampleCrashingApp/SampleCrashingApp/ViewController.swift:18
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The top line is a mangled Swift symbol, so it needs to be demangled in a separate command:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;swift-demangle &lt;span class=&quot;nt&quot;&gt;--simplified&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--compact&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    &lt;span class=&quot;s1&quot;&gt;'$s17SampleCrashingApp14ViewControllerC11updateLabel33_CEBA0EBCB7099BA3FFA1062F19F801EELLyyF'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;rendering the actual function name:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ViewController.updateLabel()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;output&quot;&gt;Output&lt;/h2&gt;

&lt;p&gt;After symbolicating all stack frames and gluing the data together, the stack trace
symbolicated using LLVM on Linux looks like this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   SampleCrashingApp        Swift runtime failure: Unexpectedly found nil while implicitly unwrapping an Optional value (ViewController.swift:0)
1   SampleCrashingApp        ViewController.updateLabel() (ViewController.swift:18)
2   SampleCrashingApp        @objc ViewController.buttonAction(_:) (&amp;lt;compiler-generated&amp;gt;:0)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And here is the same stack trace symbolicated on macOS using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;atos&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   SampleCrashingApp        ViewController.updateLabel() (in SampleCrashingApp) (ViewController.swift:18)
1   SampleCrashingApp        ViewController.updateLabel() (in SampleCrashingApp) (ViewController.swift:18)
2   SampleCrashingApp        @objc ViewController.buttonAction(_:) (in SampleCrashingApp) (&amp;lt;compiler-generated&amp;gt;:0)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That’s not bad already!&lt;/p&gt;

&lt;h1 id=&quot;additional-remarks&quot;&gt;Additional remarks&lt;/h1&gt;

&lt;h2 id=&quot;llvm-symbolizer&quot;&gt;llvm-symbolizer&lt;/h2&gt;

&lt;p&gt;This tool is shipped with LLVM alongside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;llvm-addr2line&lt;/code&gt; and it does roughly
the same – takes similar arguments and produces similar output. I’ve only found
it recently and still need to learn the difference between these.
By quick checking it looks like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;llvm-symbolizer&lt;/code&gt; defaults to displaying inlined
symbols, and it also provides the source code column, in addition to the row.
And historically, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;llvm-addr2line&lt;/code&gt; was designed as a drop-in replacement to
GNU &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;addr2line&lt;/code&gt;. Perhaps it’s just a matter of preference to pick either of the two.&lt;/p&gt;

&lt;h2 id=&quot;batch-operations&quot;&gt;Batch operations&lt;/h2&gt;

&lt;p&gt;Both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;llvm-addr2line&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;llvm-symbolizer&lt;/code&gt; can take multiple symbols for processing.&lt;/p&gt;

&lt;h2 id=&quot;sample-dockerfile&quot;&gt;Sample Dockerfile&lt;/h2&gt;

&lt;p&gt;This Dockerfile contains all the software required to symbolicate Apple
crash reports on Linux:&lt;/p&gt;

&lt;div class=&quot;language-Dockerfile highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; swiftlang/swift:nightly-5.6-amazonlinux2&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;RUN &lt;/span&gt;yum &lt;span class=&quot;nt&quot;&gt;-y&lt;/span&gt; update &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; yum &lt;span class=&quot;nt&quot;&gt;-y&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;llvm
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It uses one of the &lt;a href=&quot;https://hub.docker.com/r/swiftlang/swift&quot;&gt;official Swift Docker images&lt;/a&gt;.&lt;/p&gt;

&lt;div id=&quot;disqus_thread&quot;&gt;&lt;/div&gt;
&lt;script&gt;

/**
*  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
*  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/

var disqus_config = function () {
this.page.url = 'https://kapusta.cc/2022/06/13/symbolicate-macos-crashes-on-linux';  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = '2022-06-13-symbolicate-macos-crashes-on-linux'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};

(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://kapusta-cc.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
&lt;/script&gt;

&lt;noscript&gt;Please enable JavaScript to view the &lt;a href=&quot;https://disqus.com/?ref_noscript&quot;&gt;comments powered by Disqus.&lt;/a&gt;&lt;/noscript&gt;</content><author><name>Dominik Kapusta</name></author><category term="ios" /><category term="ios" /><category term="macos" /><category term="crash" /><category term="crashlog" /><category term="ips" /><category term="lldb" /><category term="atos" /><summary type="html">If you use a custom (in-house?) crashlog collection service and want to symbolicate crashlogs server-side, you don't need a macOS machine.</summary></entry><entry><title type="html">FontEdit 1.1.0 - partial font exporting</title><link href="/2020/05/31/fontedit-1-1-0" rel="alternate" type="text/html" title="FontEdit 1.1.0 - partial font exporting" /><published>2020-05-31T00:00:00+00:00</published><updated>2020-05-31T00:00:00+00:00</updated><id>/2020/05/31/fontedit-subsets</id><content type="html" xml:base="/2020/05/31/fontedit-1-1-0">&lt;p&gt;&lt;img class=&quot;cover&quot; src=&quot;/assets/fontedit-1.1.0/cover.jpg&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;but-whats-fontedit&quot;&gt;…but what’s FontEdit?&lt;/h3&gt;

&lt;p&gt;It’s an app which allows you to generate and use custom fonts in embedded systems’ displays. See &lt;a href=&quot;/2020/03/20/fontedit&quot;&gt;here&lt;/a&gt; for an introduction.&lt;/p&gt;

&lt;h2 id=&quot;release-notes&quot;&gt;Release notes&lt;/h2&gt;

&lt;p&gt;The version released today brings the following improvements:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;exported source code is wrapped at around 80 columns,&lt;/li&gt;
  &lt;li&gt;the source code contains a pseudocode explanation of how to retrieve individual font glyphs,&lt;/li&gt;
  &lt;li&gt;more user-friendly Undo/Redo functionality.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;New features:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;exporting ASCII table subset (skipping the characters that you don’t need) - helps to reduce 
font size when you only use a bunch of characters and not the whole ASCII table,&lt;/li&gt;
  &lt;li&gt;checking for app updates at start-up.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The binaries for Windows, MacOS and Ubuntu/Debian/Raspbian are available from &lt;a href=&quot;https://github.com/ayoy/fontedit/releases/v1.1.0&quot;&gt;FontEdit’s GitHub Releases page&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;the-rationale-behind-exporting-font-subsets&quot;&gt;The rationale behind exporting font subsets&lt;/h2&gt;

&lt;p&gt;A &lt;em&gt;font source code&lt;/em&gt;, as understood by FontEdit (and embedded systems displays) represents
a collection of bitmaps of the font’s characters (where, by definition of bitmap, each pixel
is stored as 1 bit). Therefore to load all printable characters from the ASCII table, which is
between code 32 and 126 inclusive, it takes 95 small bitmaps, each representing a single
character.&lt;/p&gt;

&lt;p&gt;Take a very small font as an example – 8x8px:&lt;/p&gt;

&lt;figure&gt;
  &lt;a href=&quot;/assets/fontedit-1.1.0/font8x8.png&quot;&gt;
    &lt;img src=&quot;/assets/fontedit-1.1.0/font8x8.png&quot; alt=&quot;A generic 8x8px font.&quot; /&gt;
  &lt;/a&gt;
&lt;/figure&gt;

&lt;p&gt;It takes 8 bytes to store a single 8x8px font character (glyph): 1 byte (8 bits) to store 
each row, times 8 rows. Times 95 glyphs, it’s &lt;strong&gt;760 bytes&lt;/strong&gt; to store the whole font.&lt;/p&gt;

&lt;p&gt;Now let’s take a larger font, something what FontEdit was designed for. Ubuntu Mono 44pt:&lt;/p&gt;

&lt;figure&gt;
  &lt;a href=&quot;/assets/fontedit-1.1.0/ubuntu44.png&quot;&gt;
    &lt;img src=&quot;/assets/fontedit-1.1.0/ubuntu44.png&quot; alt=&quot;Ubuntu Mono 44pt font.&quot; /&gt;
  &lt;/a&gt;
&lt;/figure&gt;

&lt;p&gt;This 44pt font is actually 22x39px. Each row now needs 3 bytes (22 bits + 2 unused bits), 
times 39 rows. 3 x 39 x 95 glyphs –&amp;gt; &lt;strong&gt;11115 bytes or almost 11KB&lt;/strong&gt;. And it will eventually
need to be loaded into RAM 😱. You can’t always afford this kind of memory expense. But many 
times you don’t need the whole ASCII table worth of characters.&lt;/p&gt;

&lt;p&gt;Say you work on an OLED alarm clock, and you want to display the current time with a huge font.
You’d possibly only need these 14 characters: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0123456789:APM&lt;/code&gt; (or 11 characters for a 24-hour
clock). It only takes approx. 7 to 10 times less memory than the whole printable ASCII table.&lt;/p&gt;

&lt;h3 id=&quot;preserving-character-ascii-codes-in-a-subset-export&quot;&gt;Preserving character ASCII codes in a subset export&lt;/h3&gt;

&lt;p&gt;The regular (full) font source code export comprises an array of bytes representing a font,
character by character, starting from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;' '&lt;/code&gt; (ASCII 32) and ending with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'~'&lt;/code&gt; (ASCII 126).
It’s designed to easily match individual font characters with their respective ASCII codes
– retrieving specific character data can be expressed with the following pseudocode:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// font - the font data array&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// bytes_per_character - the number of bytes that holds a single character&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;offset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ascii_code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;character&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ascii_code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;' '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bytes_per_character&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;char_data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// char_data points at character's first byte&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For a subset export, where only specific characters are in the output, the 
ASCII code &amp;lt;–&amp;gt; array index relationship for the font array is lost. Back to our example,
a font array containing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'0123456789:APM'&lt;/code&gt; would yield the bytes for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; when you ask for 
a space (ASCII 32), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; when you want to print an exclamation mark (ASCII 33), and so on.
When you try to print &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; (ASCII 48), the code would request an index that’s out of
bounds – because there are only 14 characters in the font array.&lt;/p&gt;

&lt;p&gt;To preserve the ASCII code to font array index relationship, a helper look-up table
is defined alongside the font array. The look-up table values are font array offsets for 
respective characters, relative to these characters’ ASCII codes. Sounds complicated, but here’s an example:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// Using 8x8px font to make the example simpler&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Dummy blank character&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x90&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xA8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xD0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x30 (48: '0')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x31 (49: '1')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x70&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x32 (50: '2')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x33 (51: '3')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xF0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x34 (52: '4')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x70&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x35 (53: '5')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xD0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x48&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x36 (54: '6')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x70&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x37 (55: '7')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x70&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x70&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x38 (56: '8')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x90&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x70&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x39 (57: '9')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x3a (58: ':')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x70&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x70&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x90&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x41 (65: 'A')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x98&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xD8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xF8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0xA8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x88&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x4d (77: 'M')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x70&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x50 (80: 'P')&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Pseudocode for retrieving data for a specific character:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// offset = ascii_code(character) - ascii_code(' ')&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// char_data = font[lut[offset]]&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Characters that are not exported use 0 as font array index, &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// falling back to a dummy blank character&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//32   33   34   35   36   37   38   39&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//40   41   42   43   44   45   46   47&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x08&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x30 (48: '0')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x31 (49: '1')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x32 (50: '2')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x33 (51: '3')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x28&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x34 (52: '4')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x35 (53: '5')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x38&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x36 (54: '6')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x37 (55: '7')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x48&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x38 (56: '8')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x39 (57: '9')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x58&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x3a (58: ':')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x41 (65: 'A')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x68&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x4d (77: 'M')&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;mh&quot;&gt;0x70&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Character 0x50 (80: 'P')&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As you can see, using a LUT makes it even simpler to retrieve a specific character from the
font array. And it obviously reduces the memory overhead heavily. Be aware that the above code
will result in an out-of-bounds array access for characters above &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'P'&lt;/code&gt; in the ASCII table.
At the end of the day, if you export a subset, it’s assumed that you know what you’re doing :).&lt;/p&gt;

&lt;p&gt;Your text drawing code would probably need to be changed a little bit to support font subsets,
but the changes are straightforward, and likely worth the effort.&lt;/p&gt;

&lt;p&gt;Thanks for reading through!&lt;/p&gt;

&lt;div id=&quot;disqus_thread&quot;&gt;&lt;/div&gt;
&lt;script&gt;

/**
*  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
*  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/

var disqus_config = function () {
this.page.url = 'https://kapusta.cc/2020/05/31/fontedit-1-1-0';  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = '2020-05-31-fontedit-1-1-0'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};

(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://kapusta-cc.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
&lt;/script&gt;

&lt;noscript&gt;Please enable JavaScript to view the &lt;a href=&quot;https://disqus.com/?ref_noscript&quot;&gt;comments powered by Disqus.&lt;/a&gt;&lt;/noscript&gt;</content><author><name>Dominik Kapusta</name></author><category term="embedded" /><category term="c++" /><category term="e-ink" /><category term="e-paper" /><category term="epd" /><category term="font" /><category term="python" /><category term="qt" /><category term="waveshare" /><category term="subset" /><summary type="html">In the new release of the FontEdit app you get the ability to export only the characters of the font that your system requires. A life-saver in heavily memory-constrained environments.</summary></entry><entry><title type="html">FontEdit - a custom font editor for LCD, LED and E-Paper displays</title><link href="/2020/03/20/fontedit/" rel="alternate" type="text/html" title="FontEdit - a custom font editor for LCD, LED and E-Paper displays" /><published>2020-03-20T00:00:00+00:00</published><updated>2020-03-20T00:00:00+00:00</updated><id>/2020/03/20/fontedit</id><content type="html" xml:base="/2020/03/20/fontedit/">&lt;h2 id=&quot;why&quot;&gt;Why?&lt;/h2&gt;

&lt;p&gt;Over a year ago I spent some time working with e-paper displays and I did some
research on using custom fonts for displaying text. I ended up writing 
&lt;a href=&quot;/2019/02/10/font2bytes&quot;&gt;a small app&lt;/a&gt; for preparing such fonts (it actually was an
iteration over someone else’s idea). The app would take a predefined image containing
font glyphs, and given font width and height in pixels, it would transform it to
a source code file.&lt;/p&gt;

&lt;p&gt;While this was a working solution, proven many times and even used by some people
other than me, using it was a bit cumbersome in the long run. Some of the major pain points 
were the following:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;the font image had to be edited in an image editing app (such as GIMP, Photoshop, etc.)
prior to converting it to source code,&lt;/li&gt;
  &lt;li&gt;to effectively work with a font image, a proper template image was needed (so
that you knew the bounds of a glyph you wanted to edit); people were sometimes 
contacting me and asking for help in generating a template image with a given font size.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That app was just too complicated for casual use. I then decided to try and come up
with an app that’s easier to use and doesn’t depend on external editing software.&lt;/p&gt;

&lt;h2 id=&quot;what-if-you-could-just-select-a-font-and-export-it-to-source-code&quot;&gt;What if you could just select a font and export it to source code?&lt;/h2&gt;

&lt;p&gt;This is &lt;strong&gt;FontEdit&lt;/strong&gt; – the app that does just that: import, preview, edit
and export a font for use in embedded systems’ displays.&lt;/p&gt;

&lt;figure&gt;
  &lt;a href=&quot;/assets/fontedit/import_dialog.png&quot;&gt;
    &lt;img src=&quot;/assets/fontedit/import_dialog.png&quot; alt=&quot;Here's how you import a font into FontEdit.&quot; /&gt;
  &lt;/a&gt;
  &lt;figcaption&gt;Here's how you import a font into FontEdit.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;FontEdit can open any general-purpose fixed-width desktop font. You can use custom
fonts, but (at least for now) they have to be registered in your operating system. 
The app automatically imports font glyphs for characters in the ASCII printable range
(32…126 or 0x20…0x7e). Once the font is imported, you will see the list of all
available glyphs and an edit view where individual glyphs can be fine tuned.&lt;/p&gt;

&lt;figure&gt;
  &lt;a href=&quot;/assets/fontedit/imported_font.png&quot;&gt;
    &lt;img src=&quot;/assets/fontedit/imported_font.png&quot; alt=&quot;Viewing an imported font.&quot; /&gt;
  &lt;/a&gt;
  &lt;figcaption&gt;Viewing an imported font.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h3 id=&quot;imported-font-size&quot;&gt;Imported font size&lt;/h3&gt;

&lt;p&gt;Notice the gray rows on the top and bottom of the edit view. The glyph size is decided
by the app at the time of import and it’s based on font metrics. Most of the time it’s
too tall and none of the imported glyphs use some of the rows at both ends. The app 
calculates these margins at import time and by default doesn’t include them in the exported
source code (to limit the memory footprint of the exported font).&lt;/p&gt;

&lt;h3 id=&quot;font-editor&quot;&gt;Font Editor&lt;/h3&gt;

&lt;p&gt;You can edit font glyphs with a minimal editor that’s controlled with a mouse
and keyboard. Click and drag the mouse to set pixels (making them black), hold
Alt or Ctrl (⌘) to erase. Use touchpad scroll (mouse wheel) with Ctrl (⌘) to zoom
the editor canvas.&lt;/p&gt;

&lt;figure&gt;
  &lt;a href=&quot;/assets/fontedit/custom_drawing.png&quot;&gt;
    &lt;img src=&quot;/assets/fontedit/custom_drawing.png&quot; alt=&quot;Editing a Glyph.&quot; /&gt;
  &lt;/a&gt;
  &lt;figcaption&gt;Editing a Glyph.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;You can also reset the current glyph or the whole font to their initial state
(from latest save). The editor supports Undo/Redo for most operations.&lt;/p&gt;

&lt;h3 id=&quot;adding-custom-glyphs&quot;&gt;Adding custom Glyphs&lt;/h3&gt;

&lt;p&gt;Besides editing imported glyphs, you can also add new glyphs to a font document
– either by copying an existing glyph, starting from scratch with a blank canvas
or adding a glyph from a character you put in (useful for adding non-ASCII characters
to your font).&lt;/p&gt;

&lt;figure&gt;
  &lt;a href=&quot;/assets/fontedit/add_new_glyph.png&quot;&gt;
    &lt;img src=&quot;/assets/fontedit/add_new_glyph.png&quot; alt=&quot;Adding a new Glyph.&quot; /&gt;
  &lt;/a&gt;
  &lt;figcaption&gt;Adding a new Glyph.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The additional glyphs are appended to the printable ASCII set starting fom ASCII code 127 (0x7f). As non-printable characters, they have to be referenced in your app’s source code by their hex code, e.g.:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Total price: 7.50&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x84&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// where 0x84 corresponds to the € symbol&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\x80&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; 2020 Dominik Kapusta&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 0x80 is a © symbol&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;figure&gt;
  &lt;a href=&quot;/assets/fontedit/extra_glyphs.png&quot;&gt;
    &lt;img src=&quot;/assets/fontedit/extra_glyphs.png&quot; alt=&quot;Custom Glyphs added in the lower half of ASCII table.&quot; /&gt;
  &lt;/a&gt;
  &lt;figcaption&gt;Custom Glyphs added in the lower half of ASCII table.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h3 id=&quot;source-code-export&quot;&gt;Source code export&lt;/h3&gt;

&lt;p&gt;The source code representation can be previewed at any time. The font data can be exported to:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;a C file (also suitable for use with C++),&lt;/li&gt;
  &lt;li&gt;an Arduino-specific C file (using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PROGMEM&lt;/code&gt;),&lt;/li&gt;
  &lt;li&gt;a Python list or bytes object (both compatible with Python 2.x/3.x and MicroPython).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can switch between MSB and LSB mode, invert all the bits, and conditionally include
line spacings in font definition (not recommended unless you have a very good reason
to do it). You can also configure the tab size and the font array name.&lt;/p&gt;

&lt;figure&gt;
  &lt;a href=&quot;/assets/fontedit/source_code.png&quot;&gt;
    &lt;img src=&quot;/assets/fontedit/source_code.png&quot; alt=&quot;Source code preview.&quot; /&gt;
  &lt;/a&gt;
  &lt;figcaption&gt;Source code preview.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h3 id=&quot;saving-progress-and-portability&quot;&gt;Saving progress and portability&lt;/h3&gt;

&lt;p&gt;You can save your work on a font at any time and resume it later. Note that this is
not equivalent to exporting the source code – the app saves a binary font document
with additional metadata of your font.&lt;/p&gt;

&lt;p&gt;The saved document can be transferred to another machine (even across operating systems)
where it can be further adjusted. The limitation here is that you won’t be able to
add new glyphs using the “keyboard input” method unless the original font is available
on the OS you’re working on. For example, if you import Consolas font on Windows and 
open your document on a Mac (where Consolas is not present), adding a new glyph would
be limited to an empty glyph or a copy of an existing glyph.&lt;/p&gt;

&lt;figure&gt;
  &lt;a href=&quot;/assets/fontedit/epaper_rpi.jpg&quot;&gt;
    &lt;img src=&quot;/assets/fontedit/epaper_rpi.jpg&quot; alt=&quot;Ubuntu Mono 20pt on a Waveshare E-Paper display connected to Raspberry Pi.&quot; /&gt;
  &lt;/a&gt;
  &lt;figcaption&gt;Ubuntu Mono 20pt on a Waveshare E-Paper display connected to Raspberry Pi.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h2 id=&quot;get-the-app&quot;&gt;Get the app&lt;/h2&gt;

&lt;p&gt;The app is written in C++17 with UI in Qt and I tested it on MacOS, Linux (Ubuntu 18.04 and 19.10),
Windows 10 and Raspbian Buster. I put some effort into optimizing performance, and so
it works smoothly even on my Raspberry Pi 3B.&lt;/p&gt;

&lt;p&gt;The source code is available from &lt;a href=&quot;https://github.com/ayoy/fontedit&quot;&gt;GitHub&lt;/a&gt;, but there 
are packages available from &lt;a href=&quot;https://github.com/ayoy/fontedit/releases&quot;&gt;GitHub Releases page&lt;/a&gt; 
with binaries for platforms mentioned above. FontEdit is distributed under the conditions of GPL v3.&lt;/p&gt;

&lt;p&gt;Although it’s only a first release, it already comes with features of some real value,
and I plan to continue working on the app in my spare time to make it even more powerful
and versatile. If you happen to use the app, feel free to report bugs and feature 
requests via &lt;a href=&quot;https://github.com/ayoy/fontedit/issues&quot;&gt;GitHub Issues&lt;/a&gt; or as 
a &lt;a href=&quot;https://github.com/ayoy/fontedit/pulls&quot;&gt;pull request&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;

&lt;figure&gt;
  &lt;a href=&quot;/assets/fontedit/raspbian.jpg&quot;&gt;
    &lt;img src=&quot;/assets/fontedit/raspbian.jpg&quot; alt=&quot;FontEdit running on Raspberry Pi 3 with Raspbian Buster&quot; /&gt;
  &lt;/a&gt;
  &lt;figcaption&gt;FontEdit running on Raspberry Pi 3 with Raspbian Buster.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;div id=&quot;disqus_thread&quot;&gt;&lt;/div&gt;
&lt;script&gt;

/**
*  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
*  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/

var disqus_config = function () {
this.page.url = 'https://kapusta.cc/2019/03/20/fontedit/';  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = '2019-03-20-fontedit'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};

(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://kapusta-cc.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
&lt;/script&gt;

&lt;noscript&gt;Please enable JavaScript to view the &lt;a href=&quot;https://disqus.com/?ref_noscript&quot;&gt;comments powered by Disqus.&lt;/a&gt;&lt;/noscript&gt;</content><author><name>Dominik Kapusta</name></author><category term="embedded" /><category term="c++" /><category term="e-ink" /><category term="e-paper" /><category term="epd" /><category term="font" /><category term="python" /><category term="qt" /><category term="waveshare" /><summary type="html">Here's the app I've been working on past couple of weeks. It allows you to import a&amp;nbsp;system font, edit it according to your needs and export as a byte array suitable for use in embedded systems.</summary></entry><entry><title type="html">Adding custom fonts to E-paper Display</title><link href="/2019/02/10/font2bytes/" rel="alternate" type="text/html" title="Adding custom fonts to E-paper Display" /><published>2019-02-10T18:43:14+00:00</published><updated>2019-02-10T18:43:14+00:00</updated><id>/2019/02/10/font2bytes</id><content type="html" xml:base="/2019/02/10/font2bytes/">&lt;p style=&quot;text-align:justify;&quot;&gt;It's a long overdue post, but my real life kicked in heavily last summer and I couldn't get back to working on a blog until now. Read on for the writeup on a tool to convert png images into font definition files.&lt;/p&gt;
&lt;h2 style=&quot;text-align:justify;&quot;&gt;TL;DR:&lt;/h2&gt;
&lt;ul&gt;
&lt;li style=&quot;text-align:justify;&quot;&gt;&lt;a href=&quot;https://github.com/ayoy/font2bytes&quot;&gt;https://github.com/ayoy/font2bytes&lt;/a&gt; – a multiplatform C++ library with a CLI tool and Qt-based GUI app,&lt;/li&gt;
&lt;li style=&quot;text-align:justify;&quot;&gt;&lt;a href=&quot;https://github.com/ayoy/FontToBytes&quot;&gt;https://github.com/ayoy/FontToBytes&lt;/a&gt; – a MacOS app that was an inspiration (I'm linking here my fork with extended functionality).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;hr /&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Following up on my &lt;a href=&quot;/2018/03/31/epd/&quot;&gt;e-paper display library&lt;/a&gt;, I found out fairly quickly that the original fonts provided by Waveshare have some shortcomings. First of all, you might want a different size than 8, 12, 16, 20 or 24px high. I can also imagine situations where you'd need a bold or italic font typeface alongside the regular one. Then, the default font contains basic ASCII characters only, which is fine for English but falls short for virtually any other language. Any of the following:&lt;/p&gt;
&lt;ul style=&quot;text-align:justify;&quot;&gt;
&lt;li&gt;the extended ASCII set (code range 128-255),&lt;/li&gt;
&lt;li&gt;non-ASCII Latin alphabet characters (&lt;code&gt;ąčćęěłňńšśźżž&lt;/code&gt;, to name a few :) ),&lt;/li&gt;
&lt;li&gt;any non-Latin alphabet,&lt;/li&gt;
&lt;/ul&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;is just not there. So whenever you're working on an embedded system that displays messages in your local language, you might be interested in providing your own font for the display.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Last but not least, a custom font allows for defining your own characters (icons, symbols, etc.), which can be very useful on various occasions.&lt;/p&gt;
&lt;h1&gt;The font file structure&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The font definition is basically an array of bytes, where each byte represents 8 horizontally aligned pixels. A set bit means a filled pixel, an unset bit is a clear pixel. For example, the 8x8 font needs 8 bytes per character (one byte per line), but 8x9 (9 pixels wide) would need 16 bytes per character (9 horizontal bits fit into 2 bytes). Likewise, the 24x17 font requires 24x3=72 bytes per character. &lt;a href=&quot;https://github.com/ayoy/esp32-waveshare-epd/blob/master/main/font24.c&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;This is an example font definition in C&lt;/a&gt;, with comments allowing for easier understanding of what goes where.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Now by shuffling individual bits, you can adjust the font's characters to your liking, or add new ones at the end, remembering that their codes would start from 127 (as the last character in the font file is the &lt;code&gt;~&lt;/code&gt;, which corresponds to ASCII code 126). If you're fine with it then good for you :) but how cool would it be to have a tool that can do this automatically based on an input image?&lt;/p&gt;
&lt;h1&gt;The original &lt;em&gt;Font To Bytes&lt;/em&gt;&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;When searching for a tool to prepare a custom font for use in embedded systems displays, I stumbled upon &lt;a href=&quot;https://luckyresistor.me/font-to-byte/&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;Lucky Resistor's &lt;em&gt;Font To Bytes&lt;/em&gt;&lt;/a&gt; application. It takes a PNG image with the font characters as input, such as this:&lt;/p&gt;
&lt;p align='center'&gt;&lt;img class=&quot;alignnone size-full wp-image-698&quot; src=&quot;/assets/font1.png&quot; alt=&quot;font1&quot;/&gt;&lt;br&gt;Font input image. Source: &lt;a href=&quot;https://luckyresistor.me/font-to-byte/&quot;&gt;https://luckyresistor.me/font-to-byte/&lt;/a&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The image is converted into a font source code file, for either Arduino or generic C/C++. Originally it only worked with 8x8 fonts, but the code (pretty neat by the way!) was prepared for adding new types of converters. I updated the code to Swift 4 (it was originally written back in 2015 in Swift 2) and added a parametrized converter that works with arbitrary font widths and heights:&lt;/p&gt;
&lt;p&gt;&lt;img class=&quot;alignnone size-full wp-image-699&quot; src=&quot;/assets/screenshot-2019-02-10-at-16.49.24.png&quot; alt=&quot;Screenshot 2019-02-10 at 16.49.24&quot;/&gt; &lt;em&gt;Font To Bytes&lt;/em&gt; revamped by me.&lt;/p&gt;
&lt;h1&gt;The multiplatform rewrite&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;At that time this was enough for my needs – as I happen to be a Mac user, I got what I needed. But Twitter gave me a reality check as soon as I let the world know about my finding – guess what, not everyone is on a Mac. 💥&lt;/p&gt;
&lt;p&gt;https://twitter.com/ioPush_net/status/989455886203662336&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;At first I was like, &lt;em&gt;well, tough luck&lt;/em&gt;. But then I realized that it could be a great opportunity to brush up on my C++ skills and get back to programming with Qt (after doing it for the last time with Qt 4 in 2011). Especially having no experience with modern C++, I took a chance to learn some of it by porting &lt;em&gt;Font To Bytes&lt;/em&gt; to C++ with Qt (also to see what's new in Qt 5 – thankfully I found my way around it pretty quickly).&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;This is how my app looks like:&lt;/p&gt;
&lt;p&gt;&lt;img class=&quot;alignnone size-full wp-image-701&quot; src=&quot;/assets/screenshot-2019-02-10-at-18.00.28.png&quot; alt=&quot;Screenshot 2019-02-10 at 18.00.28.png&quot;/&gt; Font To Bytes running on Linux&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;There are some changes/improvements compared to the original MacOS X version:&lt;/p&gt;
&lt;ul&gt;
&lt;li style=&quot;text-align:justify;&quot;&gt;Font width and height is adjustable freely (that is, between 1 and 255px) and independently of each other.&lt;/li&gt;
&lt;li style=&quot;text-align:justify;&quot;&gt;Python List and Python Bytes output formats are added. The latter produces a Python &lt;code&gt;bytes&lt;/code&gt; object, which is the optimal way to store font data (and can additionally benefit from freezing it in the firmware, as described in &lt;a href=&quot;/2018/03/31/epd/&quot;&gt;one of my previous blog posts&lt;/a&gt;). New types of converters can still be easily added if need be.&lt;/li&gt;
&lt;li style=&quot;text-align:justify;&quot;&gt;Besides the GUI application, a command line tool is there for those that don't need the UI or don't want to use Qt for some reason:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img class=&quot;alignnone size-full wp-image-702&quot; src=&quot;/assets/screenshot-2019-02-10-at-18.16.39.png&quot; alt=&quot;Screenshot 2019-02-10 at 18.16.39.png&quot;/&gt; &lt;em&gt;f2b&lt;/em&gt; command line utility&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The code is available from &lt;a href=&quot;https://github.com/ayoy/font2bytes&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;GitHub&lt;/a&gt;, together with example font input images and templates. The command-line tool depends only on &lt;code&gt;libpng&lt;/code&gt; (and CMake for building), while the GUI app requires Qt 5. I tested it on Mac OS and Kubuntu with no (obvious) issues. The GUI app *should* work fine on Windows, but I had no way of testing it so far.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Thanks for reading through, and in case you happen to use the app, be sure to let me know of any issues you've encountered.&lt;/p&gt;

&lt;div id=&quot;disqus_thread&quot;&gt;&lt;/div&gt;
&lt;script&gt;

/**
*  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
*  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/

var disqus_config = function () {
this.page.url = 'https://ayoy.github.io';  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = 'kapusta-cc'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};

(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://kapusta-cc.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
&lt;/script&gt;
&lt;noscript&gt;Please enable JavaScript to view the &lt;a href=&quot;https://disqus.com/?ref_noscript&quot;&gt;comments powered by Disqus.&lt;/a&gt;&lt;/noscript&gt;</content><author><name>Dominik Kapusta</name></author><category term="embedded" /><category term="c++" /><category term="e-ink" /><category term="e-paper" /><category term="epd" /><category term="font" /><category term="python" /><category term="qt" /><category term="waveshare" /><summary type="html">It's a long overdue post, but my real life kicked in heavily last summer and I couldn't get back to working on a&amp;nbsp;blog until now. Read on for the writeup on a tool to convert png images into font definition files.</summary></entry><entry><title type="html">Track-ing weather on a velodrome</title><link href="/2018/08/13/tracking-weather-on-a-velodrome/" rel="alternate" type="text/html" title="Track-ing weather on a velodrome" /><published>2018-08-13T20:07:36+00:00</published><updated>2018-08-13T20:07:36+00:00</updated><id>/2018/08/13/tracking-weather-on-a-velodrome</id><content type="html" xml:base="/2018/08/13/tracking-weather-on-a-velodrome/">&lt;p&gt;A friend of mine, &lt;a href=&quot;https://twitter.com/Klinplus2pedaly&quot;&gt;Piotr Klin&lt;/a&gt;, is the fastest Masters level cyclist in Poland (as proven during last weekend's National Championships), the current silver medalist of the Masters World Cup, and one of the fastest people on the bike that I know in person. He is taking on the next challenge this Sunday - the ultimate cycling endurance test, known as the &lt;a href=&quot;https://en.wikipedia.org/wiki/Hour_record&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;hour record attempt&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In simple words, the hour record attempt is about covering as long distance as possible within one hour, on an indoor bicycle track. Riding a track bike, i.e. fixed gear, no breaks, no water, no eating, around a 250m wooden velodrome — you can already imagine how mentally challenging it can be. And when I tell you that the aim is to beat the national Elite record and world Masters record, which would require Piotr to ride over 50km (within one hour), you should get the idea of what kind of a dreaded nightmare it is.&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_624&quot; align=&quot;alignnone&quot; width=&quot;2000&quot;]&lt;img class=&quot;alignnone size-full wp-image-624&quot; src=&quot;/assets/uwct.jpg&quot; alt=&quot;uwct&quot;/&gt; Piotr Klin in full flight.[/caption]&lt;/p&gt;
&lt;p&gt;In order to succeed in such an attempt, not only the extraordinary physical and mental form is required. Aerodynamics of both the rider and the bicycle play a very significant role too. Piotr is a mechanical engineer and he works on aerodynamics in the automotive industry, so he realizes the importance of a streamlined airflow around the body and the bike very well and is able to utilize his knowledge to improve the results. To the extent of 3D-printing some bike components...&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_623&quot; align=&quot;alignnone&quot; width=&quot;1152&quot;]&lt;img class=&quot;alignnone size-full wp-image-623&quot; src=&quot;/assets/handlebar-e1534182411445.jpg&quot; alt=&quot;handlebar&quot;/&gt; This is a handlebar.[/caption]&lt;/p&gt;
&lt;p&gt;Last but not least, aerodynamics depend heavily on the weather conditions. Namely, on the air density (often marked with a Greek letter &lt;em&gt;rho&lt;/em&gt; – ρ), which is a factor of air pressure, relative humidity and temperature. The higher the air density, the more difficult is to travel through the air. Even if its impact is unnoticeable in daily life, in competition, especially at relatively high speed over a long time, it makes a significant difference. Let me just point out that for an hour attempt, just a 2-3% change in air density could alter the resulting distance by up to 300-400m, which can mean winning or losing.&lt;/p&gt;
&lt;h1&gt;Indoor weather station&lt;/h1&gt;
&lt;p&gt;Even if the date for the attempt was set and the velodrome was booked a couple of months ago, the precise time of day when Piotr would start was yet to be decided. We developed the simple weather station to figure out the air density at the velodrome and the optimal attempt time.&lt;/p&gt;
&lt;h2&gt;Requirements&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;The weather station would measure air pressure, relative humidity and temperature and calculate the air density out of these values.&lt;/li&gt;
&lt;li&gt;It needs to do measurements frequently enough so that we'll be able to nail down the optimal one-hour time window.&lt;/li&gt;
&lt;li&gt;It should send the data periodically to the internet to allow analysing it on the fly.&lt;/li&gt;
&lt;li&gt;It would be installed on a velodrome, without an easy way to connect to grid power, so it should run on the battery.&lt;/li&gt;
&lt;li&gt;There is no LoRaWAN coverage in the area, so it has to use WiFi.&lt;/li&gt;
&lt;li&gt;The velodrome WiFi is open (!) and tends to break down under heavy load (e.g. during track race days), and the device shouldn't lose data due to lack of connectivity.&lt;/li&gt;
&lt;li&gt;The velodrome is in Pruszków, Poland. It's over 100km from where I live, and I'm not visiting it often in summer. The device should then be either pretty bulletproof or easy to fix/reset by the non-technical velodrome staff that I could contact over the phone.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Plus, the hour record attempt takes place on the 19th of August, and it was the end of June when Piotr asked me to do it :) Given that we'd like to gather a couple of weeks worth of data before deciding on a start time, the device should be ready within days.&lt;/p&gt;
&lt;h2&gt;Hardware choice&lt;/h2&gt;
&lt;p&gt;Now I need to stress out that I'm not in any way affiliated with or sponsored by Pycom (ok, that's not counting the WiPy board I got a long time ago from Jose Marcelino ❤️), but simply put, to make a working prototype within a couple of days, it was the easiest for me to leverage my MicroPython experience and loads of existing code I once wrote to do this new project.&lt;/p&gt;
&lt;p&gt;I went with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;WiPy 3.0 board&lt;/li&gt;
&lt;li&gt;Pycom Expansion Board for connecting everything without preparing a custom PCB,&lt;/li&gt;
&lt;li&gt;BME280 environmental sensor that measures air pressure, humidity and temperature,&lt;/li&gt;
&lt;li&gt;DS3231 external RTC module to keep correct measurement timestamps (more on that later),&lt;/li&gt;
&lt;li&gt;a microSD card to store and backup measurements,&lt;/li&gt;
&lt;li&gt;a 2400mAh battery, hoping it would last 6 weeks,&lt;/li&gt;
&lt;li&gt;an external WiFi antenna, just in case.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The expansion board also provides a battery connector and the microSD card slot. It drains some extra current for battery level measurement, and that could be optimized on a custom circuit board, but it's a trade-off for having all the goods mentioned before.&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_625&quot; align=&quot;alignnone&quot; width=&quot;4032&quot;]&lt;img class=&quot;alignnone size-full wp-image-625&quot; src=&quot;/assets/circuit.jpg&quot; alt=&quot;circuit&quot;/&gt; Test circuit.[/caption]&lt;/p&gt;
&lt;p&gt;&lt;img class=&quot; size-medium wp-image-626 alignright&quot; src=&quot;/assets/elevated.jpg?w=600&quot; alt=&quot;elevated&quot;/&gt;Fitting the BME280 breakout board was not a simple task, because I got the version with an extra CCS811 sensor that used 7 pins in total, but I finally had the excuse to use the weirdly looking elevated goldpins that I once bought for a no good reason. Needless to say, my RTC module also required some extra wizardry to fit on the expansion board, but at the end of the day, &lt;em&gt;it is possible to connect any two interfaces with a finite number of adapters.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;Software implementation&lt;/h2&gt;
&lt;h3&gt;Air Density measurement&lt;/h3&gt;
&lt;p&gt;I was lucky to find a &lt;a href=&quot;https://bitbucket.org/oscarBravo/wipy_bme280&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;MicroPython BME280 library&lt;/a&gt; that I ported to Pycom MicroPython with just a little effort, so the core functionality was a no-brainer. Well, not. The sensor obviously reports only the raw values of temperature, absolute air pressure and relative humidity. The air density still needs to be calculated. Piotr shared a spreadsheet with me:&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_622&quot; align=&quot;aligncenter&quot; width=&quot;1024&quot;]&lt;img class=&quot;aligncenter size-large wp-image-622&quot; src=&quot;/assets/screen-shot-2018-08-13-at-17-24-20.png?w=2048&quot; alt=&quot;Screen Shot 2018-08-13 at 17.24.20&quot;/&gt; Here for example, for values in red cells, the air density is 1.131kg/m^3 (dark green cell).[/caption]&lt;/p&gt;
&lt;p&gt;All I needed to do was to write it in Python and verify that it outputs the correct values. Check.&lt;/p&gt;
&lt;h3&gt;Data storage and upload&lt;/h3&gt;
&lt;p&gt;In order to achieve the proper measurement resolution, we decided to capture the data every 10 minutes. Now should the device upload data points one by one to the server, the WiFi would drain the battery in a couple of days at most. Since we didn't need a real-time information on weather conditions, it was okay to upload the data once a day (or more precisely, once every 144 measurements, which is &lt;em&gt;24 hours&lt;/em&gt; times &lt;em&gt;6 per hour&lt;/em&gt;), and otherwise keep the WiFi radio turned off.&lt;/p&gt;
&lt;p&gt;With a native Pycom support for the microSD card, it was a piece of cake to store measurements between uploads in a text file on the card. Already uploaded data points are kept on the microSD card for offline access and for backup purposes.&lt;/p&gt;
&lt;h3&gt;Timestamping&lt;/h3&gt;
&lt;p&gt;The problem with uploading once per day is that the device doesn't know the time between measurements. The real-time clock on Espressif ESP-32 chip (which is the heart of Pycom devices) is not powered during deep sleep, so when device wakes up 10 minutes after last data upload, it's back to 1970. So when it comes to keeping time during sleep, Espressif is a &lt;strong&gt;BYOC&lt;/strong&gt; (&lt;em&gt;bring your own clock&lt;/em&gt;) platform.&lt;/p&gt;
&lt;p&gt;The time is synced once per day (during data upload), it is kept in the external RTC module (powered with a separate battery), and it's used for timestamping intermediate measurements.&lt;/p&gt;
&lt;h2&gt;Server&lt;/h2&gt;
&lt;p&gt;No fancy stuff here, I went with good old &lt;a href=&quot;https://www.influxdata.com/time-series-platform/influxdb/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;InfluxDB&lt;/a&gt; for storing data and &lt;a href=&quot;https://grafana.com&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Grafana&lt;/a&gt; for visualization. It's good enough, looks nice and clean and can be up and running within minutes.&lt;/p&gt;
&lt;p&gt;&lt;img class=&quot;alignnone size-full wp-image-646&quot; src=&quot;/assets/screen-shot-2018-08-13-at-22-48-57.png&quot; alt=&quot;Screen Shot 2018-08-13 at 22.48.57.png&quot;/&gt;&lt;/p&gt;
&lt;h2&gt;Deployment&lt;/h2&gt;
&lt;p&gt;After proving that prototype works stable at my home, I tested it on the velodrome. Thankfully, it happened during Grand Prix Poland, with lots of people on the track, and, as it turned out, a broken WiFi. I realized that the device tried to connect for too long, then it got reset by a watchdog (that was set to 60s) and it repeated the same thing again until it managed to upload the data successfully (which didn't happen in a couple of hours).&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;[gallery ids=&quot;644,647&quot; type=&quot;rectangular&quot;]&lt;/p&gt;
&lt;p&gt;Trying to connect to WiFi repeatedly over a couple of hours is, of course, deadly to the battery, so I needed another trade-off to save the battery and the data, to some extent. I reworked the code to try to upload the data only once, and on failure don't repeat, but just store the data on the SD card and move on. This way it would skip a day worth of measurements, but would still work and gather the data, possibly the next day the WiFi would be up again, and I could access the missing data offline by copying them from the SD card.&lt;/p&gt;
&lt;h1&gt;Conclusions&lt;/h1&gt;
&lt;p&gt;Piotr will go at 18:00. My work is done and it's on him now. 🤞🏻🤞🏻🤞🏻&lt;/p&gt;
&lt;p&gt;https://www.facebook.com/piotr.klin.5/videos/pcb.10155804967730847/10155804967355847&lt;/p&gt;
&lt;p&gt;PS. It's August 14th and the battery voltage is at around 3.7V after 38 days. Not bad.&lt;/p&gt;

&lt;div id=&quot;disqus_thread&quot;&gt;&lt;/div&gt;
&lt;script&gt;

/**
*  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
*  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/

var disqus_config = function () {
this.page.url = 'https://ayoy.github.io';  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = 'kapusta-cc'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};

(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://kapusta-cc.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
&lt;/script&gt;
&lt;noscript&gt;Please enable JavaScript to view the &lt;a href=&quot;https://disqus.com/?ref_noscript&quot;&gt;comments powered by Disqus.&lt;/a&gt;&lt;/noscript&gt;</content><author><name>Dominik Kapusta</name></author><category term="embedded" /><category term="air" /><category term="air density" /><category term="bme280" /><category term="pycom" /><category term="station" /><category term="velodrome" /><category term="weather" /><summary type="html">A friend of mine, Piotr Klin, will attempt breaking the cycling hour record. Here's how I helped him nail down the start time by measuring air density on the velodrome.</summary></entry><entry><title type="html">An efficient MicroPython library for E-Paper Display</title><link href="/2018/03/31/epd/" rel="alternate" type="text/html" title="An efficient MicroPython library for E-Paper Display" /><published>2018-03-31T21:19:55+00:00</published><updated>2018-03-31T21:19:55+00:00</updated><id>/2018/03/31/epd</id><content type="html" xml:base="/2018/03/31/epd/">&lt;p style=&quot;text-align:justify;&quot;&gt;I recently got a &lt;a href=&quot;https://www.waveshare.com/1.54inch-e-paper-module-b.htm&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Waveshare E-Paper Display&lt;/a&gt;, in order to learn more about displays – programming and using them with microcontrollers. I chose specifically the e-paper because of its features like very low power consumption (up to 8mA only when refreshing) and preserving the state when unpowered. The latter can be especially handy for battery-powered sensor devices. Think of an environmental sensor that measures data only once in a while and sleeps in between, but with e-paper, it can display e.g. the last measurement all the time without affecting battery life. For starters, I got the pretty small, 1.54&quot; display, with three available colours, i.e. black, red, and no colour.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The Waveshare E-Paper Display (EPD) communicates via SPI, uses 6 pins for communication (which with power supply makes 8 in total). It comes with libraries for Arduino, Raspberry Pi (in Python and C++) and STM32, so you're all set on these platforms. For other environments, well, tough luck.&lt;/p&gt;
&lt;h1&gt;MicroPython implementation&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The Python library for Raspberry Pi uses PIL (Python Imaging Library) for displaying text and images. It's awesome because it greatly simplifies the implementation. PIL is however not available in MicroPython, so I had to resort to drawing text using fonts defined in the code as arrays.&lt;/p&gt;
&lt;h2&gt;Optimizations&lt;/h2&gt;
&lt;p&gt;Here's the example definition of a letter 'A' in a 24-pixel font:&lt;/p&gt;&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span class=&quot;cp&quot;&gt;# @2376 'A' (17 pixels wide)
&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x1F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x80&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;    &lt;span class=&quot;err&quot;&gt;######&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x1F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xC0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;    &lt;span class=&quot;err&quot;&gt;#######&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x01&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xC0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;        &lt;span class=&quot;err&quot;&gt;###&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x03&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;       &lt;span class=&quot;err&quot;&gt;##&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;##&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x03&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;       &lt;span class=&quot;err&quot;&gt;##&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;##&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x06&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;      &lt;span class=&quot;err&quot;&gt;##&lt;/span&gt;   &lt;span class=&quot;err&quot;&gt;##&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x06&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;      &lt;span class=&quot;err&quot;&gt;##&lt;/span&gt;   &lt;span class=&quot;err&quot;&gt;##&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x0C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;     &lt;span class=&quot;err&quot;&gt;##&lt;/span&gt;    &lt;span class=&quot;err&quot;&gt;##&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x0F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xF8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;     &lt;span class=&quot;err&quot;&gt;#########&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x1F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xF8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;    &lt;span class=&quot;err&quot;&gt;##########&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x0C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;    &lt;span class=&quot;err&quot;&gt;##&lt;/span&gt;       &lt;span class=&quot;err&quot;&gt;##&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x0C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;   &lt;span class=&quot;err&quot;&gt;##&lt;/span&gt;        &lt;span class=&quot;err&quot;&gt;##&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0xFC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x7F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;######&lt;/span&gt;   &lt;span class=&quot;err&quot;&gt;#######&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0xFC&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x7F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;######&lt;/span&gt;   &lt;span class=&quot;err&quot;&gt;#######&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x00&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;#&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;p style=&quot;text-align:justify;&quot;&gt;Full font definition is available &lt;a href=&quot;https://github.com/ayoy/micropython-waveshare-epd/blob/11c31c601ef144228fbfd969bb65a300a3aa3030/fonts/font24.py&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;here&lt;/a&gt;. 3x24 = 72 bytes are required for a single character. Now multiply it by the number of printable ASCII characters (95, in the 32-127 range) and you'll get 6840 items in the list. Even for the small 8-pixel font the list has 760 elements.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Similarly for images, to display an image you'll need a full-screen bitmap, which in case of my small 200x200px display is a list with 200x200/8 = 5000 bytes.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;This brings up performance problems on all possible fronts: loading a font module is terribly slow and it generates huge memory overhead. Have a look:&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code class=&quot;Python&quot;&gt;&amp;gt;&amp;gt;&amp;gt; from machine import Timer
&amp;gt;&amp;gt;&amp;gt; import time
&amp;gt;&amp;gt;&amp;gt; import gc
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; chrono = Timer.Chrono()
&amp;gt;&amp;gt;&amp;gt; mem_free = gc.mem_free()
&amp;gt;&amp;gt;&amp;gt; chrono.start() # start a timer
&amp;gt;&amp;gt;&amp;gt; import font24
&amp;gt;&amp;gt;&amp;gt; chrono.stop() # stop a timer after a module has loaded
&amp;gt;&amp;gt;&amp;gt; print(mem_free - gc.mem_free())
36656
&amp;gt;&amp;gt;&amp;gt; print(chrono.read()*1000)
1666.031
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Over 30kB of RAM and over 1.5s to load a module 😱. But we're far from the optimal solution here. For starters, let's replace the list with bytes - &lt;code&gt;struct&lt;/code&gt; can be used for that:&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code class=&quot;Python&quot;&gt;&amp;gt;&amp;gt;&amp;gt; import ustruct as struct
&amp;gt;&amp;gt;&amp;gt; import font24
&amp;gt;&amp;gt;&amp;gt; data = struct.pack(&quot;%sB&quot; % len(font24.data), *(font24.data))
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Now &lt;code&gt;data&lt;/code&gt; is a &lt;code&gt;bytes&lt;/code&gt; object with contents of &lt;code&gt;font24.data&lt;/code&gt;. Furthermore, &lt;code&gt;bytes&lt;/code&gt; is an immutable data structure, which is exactly what we need for font files, and it can allow MicroPython interpreter to optimize it better.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The optimized font file looks like &lt;a href=&quot;https://github.com/ayoy/micropython-waveshare-epd/blob/master/fonts/font24.py&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;this&lt;/a&gt;. Well, you won't easily consult the individual letters anymore, but that's the price for it being FAST. Here's how fast:&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code class=&quot;Python&quot;&gt;&amp;gt;&amp;gt;&amp;gt; chrono = Timer.Chrono()
&amp;gt;&amp;gt;&amp;gt; mem_free = gc.mem_free()
&amp;gt;&amp;gt;&amp;gt; chrono.start() # start a timer
&amp;gt;&amp;gt;&amp;gt; import font24
&amp;gt;&amp;gt;&amp;gt; chrono.stop() # stop a timer after a module has loaded
&amp;gt;&amp;gt;&amp;gt; print(mem_free - gc.mem_free())
14240
&amp;gt;&amp;gt;&amp;gt; print(chrono.read()*1000)
377.0546
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;More than 4 times faster to load, with roughly a third of an original memory footprint. 💥🚀😎&lt;/p&gt;
&lt;p&gt;If this wasn't enough, we can go further.&lt;/p&gt;
&lt;h3&gt;Frozen modules&lt;/h3&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;A lengthy Python module, and especially a huge chunk of immutable binary data is a perfect candidate to be included in the firmware. It's not a trivial task as it includes building a firmware image yourself, but it's also not overly complicated. If it's something you haven't tried yet, I encourage you to give it a spin.&lt;/p&gt;
&lt;ol&gt;
&lt;li style=&quot;text-align:justify;&quot;&gt;Get the Pycom MicroPython source code from GitHub:&lt;br /&gt;
&lt;a href=&quot;https://github.com/pycom/pycom-micropython-sigfox&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://github.com/pycom/pycom-micropython-sigfox&lt;/a&gt;&lt;/li&gt;
&lt;li style=&quot;text-align:justify;&quot;&gt;&lt;strong&gt;Recommended: &lt;/strong&gt;check out the most recent tag, so that you build the release image, and not the current live image from git:&lt;br /&gt;
&lt;pre&gt;&lt;code class=&quot;Bash&quot;&gt;$ git tag # list available tags
1.11.0.b1
1.12.0.b1
1.13.0.b1
1.6.13.b1
v1.14.0.b1
v1.15.0.b1
v1.16.0.b1
v1.17.0.b1
v1.17.2.b1
$ git checkout v1.17.2.b1
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li style=&quot;text-align:justify;&quot;&gt;Follow the instructions in README closely on how to set up the build environment (the &lt;em&gt;&lt;a href=&quot;https://github.com/pycom/pycom-micropython-sigfox#the-esp32-version&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;The ESP32 version&lt;/a&gt;&lt;/em&gt; section) up to the point of building &lt;code&gt;mpy-cross&lt;/code&gt;&lt;/li&gt;
&lt;li style=&quot;text-align:justify;&quot;&gt;Actually, build &lt;code&gt;mpy-cross&lt;/code&gt; according to instructions&lt;/li&gt;
&lt;li style=&quot;text-align:justify;&quot;&gt;Enter &lt;code&gt;esp32/frozen&lt;/code&gt;&lt;/li&gt;
&lt;li style=&quot;text-align:justify;&quot;&gt;Copy there all the &lt;code&gt;.py&lt;/code&gt; files you need to be frozen in firmware&lt;/li&gt;
&lt;li style=&quot;text-align:justify;&quot;&gt;Go one level up, to &lt;code&gt;esp32&lt;/code&gt; directory&lt;/li&gt;
&lt;li style=&quot;text-align:justify;&quot;&gt;Make a build for your board. In case of my LoPy4, the commands were:&lt;br /&gt;
&lt;pre&gt;&lt;code class=&quot;Bash&quot;&gt;$ make BOARD=LOPY4 clean
$ make BOARD=LOPY4 TARGET=boot -j9
$ make BOARD=LOPY4 TARGET=app -j9
$ make BOARD=LOPY4 flash
&lt;/code&gt;&lt;/pre&gt;
Note on &lt;code&gt;-j9&lt;/code&gt;: it is used to speed up building by parallelizing the compilation of independent objects. The number specifies the maximum number of jobs that can be run in parallel. As a rule of thumb, you can safely use &lt;code&gt;-j5&lt;/code&gt; on dual-core machines and &lt;code&gt;-j9&lt;/code&gt; on quad-core machines.&lt;/li&gt;
&lt;li style=&quot;text-align:justify;&quot;&gt;Flash the image according to the README.&lt;/li&gt;
&lt;/ol&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Now you don't need your frozen modules' files on the flash because they're included in the firmware. Other than that you work with frozen modules the same way as with regular ones, i.e. you still import them using &lt;code&gt;import&lt;/code&gt; statement, etc.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;And here's how a module performs after freezing:&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code class=&quot;Python&quot;&gt;&amp;gt;&amp;gt;&amp;gt; chrono = Timer.Chrono()
&amp;gt;&amp;gt;&amp;gt; mem_free = gc.mem_free()
&amp;gt;&amp;gt;&amp;gt; chrono.start() # start a timer
&amp;gt;&amp;gt;&amp;gt; import font24
&amp;gt;&amp;gt;&amp;gt; chrono.stop() # stop a timer after a module has loaded
&amp;gt;&amp;gt;&amp;gt; print(mem_free - gc.mem_free())
560
&amp;gt;&amp;gt;&amp;gt; print(chrono.read()*1000)
16.80003
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Here, the garbage collector is irrelevant because frozen modules are apparently already loaded into RAM together with the whole firmware, but as you can see, the improvement in import time is huge.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;To recap the optimizations, we've tried:&lt;/p&gt;
&lt;ul style=&quot;text-align:justify;&quot;&gt;
&lt;li&gt;list data: 36kB RAM, 1666ms to import&lt;/li&gt;
&lt;li&gt;bytes data: 14kB RAM, 377ms to import&lt;/li&gt;
&lt;li&gt;frozen bytes data: N/A (loaded with firmware), &lt;strong&gt;16ms to import &lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Also, with a simple test program that draws a couple of shapes, prints a few strings and displays an image, the total available RAM remaining after the program has finished is around 2.4MB (with 2.55MB available at startup after loading firmware), when before optimizations it was at around 1.8MB.&lt;/p&gt;
&lt;p&gt;&lt;img class=&quot; size-full wp-image-575 aligncenter&quot; src=&quot;/assets/img_8450.jpg&quot; alt=&quot;IMG_8450.JPG&quot;/&gt;&lt;/p&gt;
&lt;h1&gt;Library features&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;In the current state, the library supports only the 1.54 inch two-colour black and red Waveshare display, but adding support for other EPDs should be a simple task. I'll maybe get some other unit to give it a try.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Apart from printing text, you can also draw lines (between any two points, i.e. not limited to horizontal and vertical) or rectangles and circles (both regular and filled).&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_582&quot; align=&quot;alignright&quot; width=&quot;269&quot;]&lt;img class=&quot;alignnone size-full wp-image-582&quot; src=&quot;/assets/epd-goinvent.gif&quot; alt=&quot;epd-goinvent&quot;/&gt; #GOINVENT![/caption]&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Drawing images is available via filling the frame buffer with raw image bytes (which is the fastest possible way, especially if you freeze the image data in firmware). It's not a very straightforward or flexible method though, as you have to convert your image to bytes beforehand. So I added support for loading a Windows-style 1-color BMP file directly. You can even display the bitmap at given coordinates, if it's smaller than the screen size – this should be helpful when combining images with text.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The library is available from &lt;a href=&quot;https://github.com/ayoy/micropython-waveshare-epd&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;GitHub&lt;/a&gt;, and should you need the C/C++ version for plain ESP32, I also did it and it's in a &lt;a href=&quot;https://github.com/ayoy/esp32-waveshare-epd&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;separate repository&lt;/a&gt; (although without the BMP file support). Let me know if you find any of it helpful and of course, contributions are welcome!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;div id=&quot;disqus_thread&quot;&gt;&lt;/div&gt;
&lt;script&gt;

/**
*  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
*  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/

var disqus_config = function () {
this.page.url = 'https://ayoy.github.io';  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = 'kapusta-cc'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};

(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://kapusta-cc.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
&lt;/script&gt;
&lt;noscript&gt;Please enable JavaScript to view the &lt;a href=&quot;https://disqus.com/?ref_noscript&quot;&gt;comments powered by Disqus.&lt;/a&gt;&lt;/noscript&gt;</content><author><name>Dominik Kapusta</name></author><category term="embedded" /><category term="e-ink" /><category term="e-paper" /><category term="epd" /><category term="frozen modules" /><category term="library" /><category term="micropython" /><category term="pycom" /><category term="python" /><category term="waveshare" /><summary type="html">This time porting Python code to MicroPython wasn't only about fixing syntax and API. It turns out that for the e-paper display library that uses relatively big amounts of data at once, you have to sweat a bit more to get satisfactory results.</summary></entry><entry><title type="html">My first LoRaWAN app​</title><link href="/2018/02/14/my-first-lorawan-app/" rel="alternate" type="text/html" title="My first LoRaWAN app​" /><published>2018-02-14T17:46:27+00:00</published><updated>2018-02-14T17:46:27+00:00</updated><id>/2018/02/14/my-first-lorawan-app</id><content type="html" xml:base="/2018/02/14/my-first-lorawan-app/">&lt;p style=&quot;text-align:justify;&quot;&gt;After updating the &lt;a href=&quot;/2018/02/02/air-quality-monitor-revisited/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Air Quality Monitor with LoPy4 board&lt;/a&gt;, when I finally got it up and running with Wi-Fi, I moved on to working on LoRa connectivity. I previously had little to none experience with LoRa, so it took me some time, but it was totally worth it.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;This is a story of how I got the air quality sensor to transmit its data via LoRa, from the balcony of my flat, over LoRaWAN nano-gateway to TTN router in the Netherlands, then to my relay server elsewhere in Europe, and to the database on the Raspberry Pi in the living room of the same flat. Just because it can™. Buckle up!&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_547&quot; align=&quot;alignnone&quot; width=&quot;1024&quot;]&lt;img class=&quot;alignnone size-large wp-image-547&quot; src=&quot;/assets/architecture.png?w=2048&quot; alt=&quot;architecture&quot;/&gt; The overview of LoRaWAN architecture. Source: &lt;a href=&quot;https://www.thethingsnetwork.org/docs/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://www.thethingsnetwork.org/docs/&lt;/a&gt;[/caption]&lt;/p&gt;
&lt;h1 style=&quot;text-align:left;&quot;&gt;But what's the big deal with LoRa?&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;LoRa, as in &quot;Low-Power Long-Range&quot; is a wireless communication technology of relatively small bandwidth, with an effective range of up to several kilometres, or even more in the open area. It uses an unrestricted frequency band of 868MHz for Europe, 915MHz for Americas, Australia and New Zealand, and 470-510MHz for China.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Then, the &lt;strong&gt;LoRaWAN&lt;/strong&gt; is a type of &lt;em&gt;Low Power Wide Area Network&lt;/em&gt; (LPWAN), which uses LoRa connectivity. The LoRa-enabled end-devices (nodes) connect to the network (the internet) via &lt;em&gt;LoRaWAN gateways&lt;/em&gt; in a similar fashion to mobile phones connecting to GSM network via GSM base stations. I mean, a node isn't assigned to a specific gateway (like in the case of WiFi, when you connect to the given router), but rather it can use any gateway that's in its range.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;A LoRaWAN gateway has the internet connection and passes the data packets from the device (&lt;em&gt;uplink&lt;/em&gt;) to the &lt;em&gt;LoRaWAN router&lt;/em&gt;. The router then forwards them to the associated web service. It can also handle &lt;em&gt;downlink&lt;/em&gt; messages, i.e. data packets sent from the web service back to the device, in response to received uplink messages.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The LoRa's bandwidth of only 0.3-50kbps is not suitable for every use case – you wouldn't certainly be streaming a video camera footage or even transferring files with it. But it's a great fit for connected devices such as various types of sensors (environmental, agricultural or industrial just to name a few) to exchange tiny bits of data. And, most importantly, as the name suggests, data transfer using LoRa requires significantly less power than other wireless technologies like Wi-Fi or Bluetooth LE. You can get more info about LoRa and LoRaWAN at the &lt;a href=&quot;https://www.lora-alliance.org/technology&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;LoRa Alliance website&lt;/a&gt;.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;While LoRa exists without LoRaWAN, as you can just set up a 1-1 connection between LoRa-enabled devices, I'll focus on LoRaWAN in this article.&lt;/p&gt;
&lt;h1 style=&quot;text-align:left;&quot;&gt;My setup&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Obviously, in order to make use of LoRaWAN, your node must be in range of a LoRaWAN gateway. This is not the case for me yet, but I'm working on getting one – BTW, it's not extremely expensive as you can build a decent GW for under €200 - &lt;a href=&quot;https://github.com/ttn-zh/ic880a-gateway/wiki#ordering-the-parts&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;see this document for a nice how-to&lt;/a&gt;. Hope is not lost though, as you can actually turn a LoRa device into a so-called &lt;em&gt;nano-gateway&lt;/em&gt;. A nano GW has some significant limitations – e.g. it requires a specific configuration for devices connecting to it. It's therefore not suitable for public use but works just fine for development, or for personal use.&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_533&quot; align=&quot;aligncenter&quot; width=&quot;580&quot;]&lt;img class=&quot;aligncenter  wp-image-533&quot; src=&quot;/assets/img_0651.jpg?w=2048&quot; alt=&quot;IMG_0651&quot;/&gt; A LoPy4 serving as a LoRaWAN nano-gateway.[/caption]&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Quite a while ago, in November last year, I preordered two &lt;a href=&quot;http://pycom.io/product/lopy4/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;LoPy4&lt;/a&gt; boards to evaluate LoRa connectivity myself. I received them mid-January, and for a first test, I wanted to connect my &lt;a href=&quot;/2017/12/02/home-made-air-quality-monitoring-using-wipy/&quot;&gt;air quality monitor&lt;/a&gt; to LoRaWAN. One of the boards would replace the WiPy in the sensor, and the other one would serve as a nano-gateway.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Setting up a nano-gateway is dead simple – it's &lt;a href=&quot;https://docs.pycom.io/chapter/tutorials/lopy/lorawan-nano-gateway.html&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;well documented&lt;/a&gt; and you can get the most up-to-date sample code from &lt;a href=&quot;https://github.com/pycom/pycom-libraries/tree/master/examples/lorawan-nano-gateway&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Pycom's GitHub&lt;/a&gt;. Replacing WiPy with LoPy4 in the air quality monitor was a bit trickier and deserved &lt;a href=&quot;/2018/02/02/air-quality-monitor-revisited/&quot;&gt;a separate blog post&lt;/a&gt;, but here it is:&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_536&quot; align=&quot;aligncenter&quot; width=&quot;580&quot;]&lt;img class=&quot;alignnone  wp-image-536&quot; src=&quot;/assets/smyr.jpg&quot; alt=&quot;smyr&quot;/&gt; The LoRa-enabled version of the Air Quality Monitor.[/caption]&lt;/p&gt;
&lt;h1 style=&quot;text-align:left;&quot;&gt;Connecting to LoRaWAN network&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Once you get the gateway up and running you need to connect it to the LoRaWAN router. There are a couple of public LoRaWAN network operators and &lt;a href=&quot;https://www.thethingsnetwork.org&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;The Things Network&lt;/a&gt; is arguably the most popular one.&lt;/p&gt;
&lt;h2 style=&quot;text-align:left;&quot;&gt;Setting up the nano-gateway&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;If you're within LoRaWAN network range and you don't need to make your own gateway you can skip this step.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Otherwise, after booting up the nano-gateway you need to register it at the TTN website. This is a simple process, already documented in many places, including &lt;a href=&quot;https://docs.pycom.io/chapter/tutorials/lopy/lorawan-nano-gateway.html&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Pycom docs&lt;/a&gt;, so I'll skip it here. It's also pretty straightforward and the only thing to remember when doing it for the nano-gateway is to be sure to check the &quot;I'm using the legacy packet forwarder&quot; checkbox.&lt;/p&gt;
&lt;h2 style=&quot;text-align:left;&quot;&gt;Creating a TTN Application&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;To do something actually useful with your data you'll need to pass it on from TTN to the service that would handle and process it appropriately. This is done by an application, that you create and configure in &lt;a href=&quot;https://console.thethingsnetwork.org&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;TTN console&lt;/a&gt;:&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;img class=&quot;  wp-image-514 aligncenter&quot; src=&quot;/assets/screen-shot-2018-02-13-at-17-06-10.png&quot; alt=&quot;Screen Shot 2018-02-13 at 17.06.10&quot;/&gt;&lt;/p&gt;
&lt;h2 style=&quot;text-align:left;&quot;&gt;Setting up the node&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The node needs to be registered in the TTN too, and it's configured at the application scope:&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;img class=&quot;  wp-image-515 aligncenter&quot; src=&quot;/assets/screen-shot-2018-02-13-at-17-11-42.png&quot; alt=&quot;Screen Shot 2018-02-13 at 17.11.42&quot;/&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The important thing that you need to choose when configuring a LoRaWAN node is the authentication mode, i.e. how nodes are authorized with the application. Two modes are available: Over-the-Air Activation (&lt;strong&gt;OTAA&lt;/strong&gt;), and Activation by Personalization (&lt;strong&gt;ABP&lt;/strong&gt;). Although there are significant differences between these two, at the basic &quot;my-first-app&quot; level we don't need to go that deep into details.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;For a proof-of-concept connecting to LoRaWAN, there's an example code for both OTAA and ABP nodes available from &lt;a href=&quot;https://github.com/pycom/pycom-libraries/tree/master/examples/lorawan-nano-gateway&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;GitHub&lt;/a&gt; (&lt;code&gt;otaa_node.py&lt;/code&gt; and &lt;code&gt;abp_node.py&lt;/code&gt;). In my case, I had some troubles connecting the node using OTAA. It might be me, the faulty firmware at that time, or some other reason, but I saw people on Pycom's forum complaining that they couldn't make OTAA work with nano-gateway too. Anyways, I ended up going with ABP. It's as simple as copying the &lt;em&gt;Device Address&lt;/em&gt;, &lt;em&gt;Application Session Key&lt;/em&gt; and &lt;em&gt;Network Session Key&lt;/em&gt; from device settings to the node script, and boom, it's online!&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;img class=&quot;  wp-image-517 aligncenter&quot; src=&quot;/assets/screen-shot-2018-02-13-at-17-23-33.png&quot; alt=&quot;Screen Shot 2018-02-13 at 17.23.33&quot;/&gt;&lt;/p&gt;
&lt;h1 style=&quot;text-align:left;&quot;&gt;Communication over LoRa&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Once you connect the node to LoRaWAN, the communication is as simple as creating a socket and writing bytes to it. The following MicroPython code does that for LoPy4:&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;# create a LoRa socket
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
# set the LoRaWAN data rate
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
# make the socket blocking
s.setblocking(True)
# send the data
payload = b'\x00\x01\x02\x03'
s.send(payload)
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;If everything works, you should see the packet appear on the TTN application console.&lt;/p&gt;
&lt;h2 style=&quot;text-align:left;&quot;&gt;Sending actual data&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;In case of my air quality sensor, I had several values to serialize and send. I went with the simple solution of converting numeric values to integers and writing them as bytes:&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;def to_bytes(self):
    # pm10 [ug/m^3] - int - 2b
    # pm25 [ug/m^3] - int - 2b
    # temp [K] - int - 2b
    # humidity [%] - int - 2b
    # voltage (mV) - int - 2b
    # duration (ms) - int - 2b
    # version - str

    payload = b''
    payload += int(self.pm10).to_bytes(2, 'little')
    payload += int(self.pm25).to_bytes(2, 'little')
    temp_k = self.temperature + 273.15
    payload += int(temp_k*100).to_bytes(2, 'little')
    payload += int(self.humidity*100).to_bytes(2, 'little')
    payload += int(self.voltage).to_bytes(2, 'little')
    payload += int(self.duration).to_bytes(2, 'little')
    payload += self.version
    return payload
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;To simplify serializing temperature, I converted it to Kelvins (but adding e.g. 50 would work fine too) to avoid negative values for sub-zero °C data points. Multiplying by 100 allowed me to store temperature and humidity as integers while keeping the resolution of 2 fraction digits.&lt;/p&gt;
&lt;h1 style=&quot;text-align:left;&quot;&gt;Handling data at TTN&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;TTN application allows you to define JavaScript handlers for decoding the payload, converting its fields and validating them before forwarding to your server. These handlers are not required, but they give you an opportunity to convert the binary payload to a pretty formatted JSON file, e.g. like this:&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code class=&quot;Javascript&quot;&gt;function bin2String(array) {
    var result = &quot;&quot;;
    for (var i = 0; i &amp;lt; array.length; i++) {
        result += String.fromCharCode(parseInt(array[i], 10));
    }
    return result;
}

function Decoder(bytes, port) {
    var decoded = {};
    decoded.pm10 = bytes[0] + (bytes[1]&amp;lt;&amp;lt;8);
    decoded.pm25 = bytes[2] + (bytes[3]&amp;lt;&amp;lt;8);
    decoded.temperature = ((bytes[5]&amp;lt;&amp;lt;8) + bytes[4])/100 - 273.15;
    decoded.humidity = ((bytes[7]&amp;lt;&amp;lt;8) + bytes[6])/100;
    decoded.vbatt = (bytes[9]&amp;lt;&amp;lt;8) + bytes[8];
    decoded.duration = (bytes[11]&amp;lt;&amp;lt;8) + bytes[10];
    decoded.version = bin2String(bytes.slice(12, bytes.length));
    return decoded;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The above code running in my TTN application can turn this payload:&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code&gt;52 00 45 00 30 6B 29 22 0E 02 98 2A 30 2E 38 2E 30
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;into the following JSON:&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code class=&quot;json&quot;&gt;{
    &quot;duration&quot;: 10904,
    &quot;humidity&quot;: 87.45,
    &quot;pm10&quot;: 82,
    &quot;pm25&quot;: 69,
    &quot;temperature&quot;: 1.25,
    &quot;vbatt&quot;: 526,
    &quot;version&quot;: &quot;0.8.0&quot;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;h1 style=&quot;text-align:left;&quot;&gt;Consuming data&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Having preprocessed the raw payload we can pass it on to the destination. This is where TTN app integrations come into play. If you're fortunate enough to be using one of the listed services, you can take advantage of its dedicated built-in handler:&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;img class=&quot;aligncenter  wp-image-518&quot; src=&quot;/assets/screen-shot-2018-02-13-at-17-30-02.png?w=414&quot; alt=&quot;Screen Shot 2018-02-13 at 17.30.02&quot;/&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;In all other cases, there's a generic HTTP Integration that allows you to forward the (optionally prettified) payload to the server of your choice. That was exactly my case, as I needed the data back at home, in the InfluxDB server running on my Raspberry Pi.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;As InfluxDB comes with an HTTP API, I technically could set up the integration so that it points directly to the InfluxDB server (it would, however, require a different payload format than JSON). But for a more generic case, when you want to add another integration in future, it's better to keep the JSON format and forward it to the relay server that would process the data accordingly for all services you support.&lt;/p&gt;
&lt;h2 style=&quot;text-align:left;&quot;&gt;TTN-to-InfluxDB forwarding microservice&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;My recipe for a minimal forwarding web service is:&lt;/p&gt;
&lt;ol style=&quot;text-align:justify;&quot;&gt;
&lt;li&gt;Python and &lt;a href=&quot;http://flask.pocoo.org&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Flask&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;A hosting service of your choice (e.g. &lt;a href=&quot;http://heroku.com&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Heroku&lt;/a&gt; is free and very well established)&lt;/li&gt;
&lt;/ol&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;This will be really quick:&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code language=&quot;Python&quot;&gt;from flask import Flask, request, jsonify
import requests

app = Flask(__name__)

@app.route('/aqi', methods=['POST'])
def forward_aqi():
    influx_response = influxdb(request.json['payload_fields'])
    success = len(influx_response) == 0
    return jsonify(success=success,
                   response=influx_response.decode('utf-8'))

def influxdb(payload):
    influx_data = 'aqi,version={} pm25={},pm10={},temperature={},'\
                  'humidity={},voltage={},duration={}' \
        .format(payload['version'], payload['pm25'],
                payload['pm10'], payload['temperature'],
                payload['humidity'], payload['vbatt'],
                payload['duration'])
    return requests.post(&quot;http://ax616034.ngrok.io/write?db=mydb&quot;,
                         data=influx_data).content
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;This is all the code required to set up a single-endpoint Python web service with Flask, that would read JSON data from POST payload, convert it to InfluxDB format and post it to the database. Welcome to Python!&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Putting it on Heroku is another story, but it took me just a couple of minutes with the documentation found online. And it was my first experience with Heroku (yes, apparently I've been living in a cave).&lt;/p&gt;
&lt;h2 style=&quot;text-align:left;&quot;&gt;HTTP Integration at TTN&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;This, again, is a no-brainer. Once the web service is up, all you have to do is pass the right URL to the HTTP Integration:&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;img class=&quot;  wp-image-524 aligncenter&quot; src=&quot;/assets/screen-shot-2018-02-13-at-18-12-56-e1518544241816.png&quot; alt=&quot;screen-shot-2018-02-13-at-18-12-56-e1518544223356.png&quot;/&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;There are additional fields to fill in on that form, such as header fields, including authorization (highly recommended for a production server), but this is the minimal required configuration.&lt;/p&gt;
&lt;h1 style=&quot;text-align:left;&quot;&gt;Watch bytes flowing&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The HTTP integration with &lt;code&gt;lora-forwarder&lt;/code&gt; was the last step. Now, given that the gateway is online, the node is in range, and TTN, Heroku and my Raspberry Pi are up, we should see the data appearing on a graph. Let's take a closer look at the single payload, sent at 22:18:39 CET.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Here you can see the node data reaching the gateway:&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;img class=&quot;  wp-image-526 aligncenter&quot; src=&quot;/assets/screen-shot-2018-02-13-at-22-40-33.png&quot; alt=&quot;Screen Shot 2018-02-13 at 22.40.33&quot;/&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The gateway recognizes the app and forwards the payload accordingly. The app decodes the payload to JSON:&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;img class=&quot;  wp-image-527 aligncenter&quot; src=&quot;/assets/screen-shot-2018-02-13-at-22-40-44.png&quot; alt=&quot;Screen Shot 2018-02-13 at 22.40.44&quot;/&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The JSON data, via HTTP Integration, is passed on to lora-forwarder, which posts it to InfluxDB:&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_528&quot; align=&quot;aligncenter&quot; width=&quot;580&quot;]&lt;img class=&quot;alignnone  wp-image-528&quot; src=&quot;/assets/screen-shot-2018-02-13-at-22-47-07.png&quot; alt=&quot;Screen Shot 2018-02-13 at 22.47.07&quot;/&gt; Timestamps are in UTC here.[/caption]&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;InfluxDB is read by Grafana that displays the data:&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;img class=&quot;  wp-image-529 aligncenter&quot; src=&quot;/assets/screen-shot-2018-02-13-at-22-49-27.png&quot; alt=&quot;Screen Shot 2018-02-13 at 22.49.27&quot;/&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;You might say &quot;Cool, but why bother?&quot;. Well, this time there's far more to it than just &quot;because you can&quot;.&lt;/p&gt;
&lt;h1 style=&quot;text-align:left;&quot;&gt;Why bother&lt;/h1&gt;
&lt;h2 id=&quot;power&quot; style=&quot;text-align:left;&quot;&gt;Power consumption&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The LoPy4 board draws approximately 140mA current with wifi enabled. When LoRa is active, with wifi disabled, the current is not bigger than &lt;strong&gt;38mA&lt;/strong&gt;. This is a huge saving, and that's what you could expect having learnt that LoRa is a low power network. But this is just the beginning.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;If you think of sending 30 bytes of data via wifi, you'd normally assume that it's gonna take a couple of seconds. After the device turns on the radio, it scans for the known network, joins it, acquires IP address from DHCP, and then it can transmit the payload. It can take as long as 10 seconds, or even more if the router signal is poor.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Now the killer feature is that the node joins LoRaWAN much faster, plus you can actually store LoRaWAN connection state in the non-volatile memory and retrieve it next time you need to send data. In a long run, the node is able to send data immediately after switching on the radio, and the total time when the LoRa radio is on is measured in milliseconds.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Take the air quality sensor as an example. With wifi connectivity, it sent data in 6-measurement batches (every hour) and it ran for 2 weeks on a 2400mAh battery. With LoRa, I'm sending data one by one (every 10 minutes), and after 2 weeks the battery voltage dropped from 4.15V to 3.77V (still well above the 3.7V limit). And the air quality monitor is not the perfect example because with every measurement the sensor alone draws 200mA for around 10 seconds. If it only measured temperature and humidity, I'd expect it to work &lt;strong&gt;for years&lt;/strong&gt;.&lt;/p&gt;
&lt;hr /&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;strong&gt;Update 20/02/2018:&lt;br /&gt;
&lt;/strong&gt;I disconnected the sensor when the battery voltage dropped to 3.71V after &lt;strong&gt;22 days&lt;/strong&gt; of uninterrupted uptime, which is over 50% longer than when working on WiFi.&lt;/p&gt;
&lt;p&gt;&lt;img class=&quot;  wp-image-566 aligncenter&quot; src=&quot;/assets/screen-shot-2018-02-19-at-23-29-30.png&quot; alt=&quot;Screen Shot 2018-02-19 at 23.29.30&quot;/&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;&lt;/h2&gt;
&lt;h2 style=&quot;text-align:left;&quot;&gt;Range&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;If constrained by wifi connectivity, you can't get more than a couple dozen metres of range for your device, maybe a couple hundred metres in open terrain with very well-matched high-gain antennas on both ends. This is a piece of cake for LoRa, where a range of several kilometres is nothing unusual.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Also, let me remind you that LoRaWAN infrastructure is not required if you'd like to take advantage of LoRa transmission. It's possible to set up a two-way 1-1 LoRa communication between two LoRa-enabled devices, where you have full control over the amount and format of data being transferred.&lt;/p&gt;
&lt;h2 style=&quot;text-align:left;&quot;&gt;Costs&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;One would argue that for a long-range communication you could use GSM, especially with its IoT-friendly technologies like NB-IoT and LTE-M. In many situations, this is the only option and it's fine, but it comes at a cost. Firstly, you need the right hardware, and GSM-enabled devices are usually pretty expensive. And then there comes the service fee you need to pay to your GSM operator.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Now compare it to LoRa-enabled chips, that you can get for &lt;a href=&quot;http://shop.imst.de/wireless-modules/lora-products/12/im880b-l-lorawan-long-range-radio-module?c=24&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;as little as €15&lt;/a&gt; with no additional recurring costs. For me, the choice is obvious, as long as I can ensure that the node will remain within the range of the gateway/receiver.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The other aspect is that in some cases by switching to LoRa you can decrease the complexity of your device. In my case for example, when sending data one measurement at a time, I don't need to track exact timestamps of data points anymore – this means I could get rid of the external RTC module. Not only it reduces the overall cost of the device, but it also simplifies the code (which is always welcome), and even improves battery life a tiny bit.&lt;/p&gt;
&lt;hr /&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;To sum up, after first trials, I am just amazed by LoRa and LoRaWAN technology. The possibilities are enormous if you consider the really long range, fantastic battery life and low overall deployment cost. As LoRaWAN is pretty much a social network at the moment, everyone can contribute to its expansion by setting up a gateway (the real one though, not the nano GW). I'm definitely getting one in the next couple of weeks and I'll try to make it useful not only for me but possibly for my neighbourhood, by deploying some devices around the building ;)&lt;/p&gt;

&lt;div id=&quot;disqus_thread&quot;&gt;&lt;/div&gt;
&lt;script&gt;

/**
*  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
*  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/

var disqus_config = function () {
this.page.url = 'https://ayoy.github.io';  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = 'kapusta-cc'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};

(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://kapusta-cc.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
&lt;/script&gt;
&lt;noscript&gt;Please enable JavaScript to view the &lt;a href=&quot;https://disqus.com/?ref_noscript&quot;&gt;comments powered by Disqus.&lt;/a&gt;&lt;/noscript&gt;</content><author><name>Dominik Kapusta</name></author><category term="embedded" /><category term="air quality" /><category term="lopy" /><category term="lora" /><category term="lorawan" /><category term="nano-gateway" /><category term="thethingsnetwork" /><category term="ttn" /><summary type="html">This is a story of how I got the air quality sensor to transmit its data via LoRa, from the balcony of my flat, over LoRaWAN nano-gateway to TTN router in the Netherlands, then to my relay server elsewhere in Europe, and to the database on the Raspberry Pi in the living room of the same flat. (Not) just because it can™.</summary></entry><entry><title type="html">Air Quality Monitor revisited: LoPy4 and the custom PCB</title><link href="/2018/02/02/air-quality-monitor-revisited/" rel="alternate" type="text/html" title="Air Quality Monitor revisited: LoPy4 and the custom PCB" /><published>2018-02-02T22:39:15+00:00</published><updated>2018-02-02T22:39:15+00:00</updated><id>/2018/02/02/air-quality-monitor-revisited</id><content type="html" xml:base="/2018/02/02/air-quality-monitor-revisited/">&lt;p style=&quot;text-align:justify;&quot;&gt;It's been a while since I deployed the air quality monitor on my balcony. If you're reading this, chances are you have seen &lt;a href=&quot;/2017/12/02/home-made-air-quality-monitoring-using-wipy/&quot;&gt;the first article&lt;/a&gt; where I described the lengthy process of getting it to work with the ESP32-based &lt;a href=&quot;https://pycom.io/product/wipy-3/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;WiPy&lt;/a&gt; board, optimizing battery usage, visualizing the data and putting everything in a box. Over 2 months later I'm after several iterations of improving it, fixing issues and trying out new solutions.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;To recap, the first release version had the following features:&lt;/p&gt;
&lt;ol style=&quot;text-align:justify;&quot;&gt;
&lt;li&gt;Measured values:
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;PM2.5 and PM10&lt;/strong&gt; concentration via a PMS5003 air quality sensor,&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;temperature and relative humidity&lt;/strong&gt; via a SHT10 sensor,&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;battery voltage&lt;/strong&gt; using the Pycom expansion board's built-in voltage divider and ESP32 analogue-to-digital converter,&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;total measurement duration.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Measurements were taken roughly every 10 minutes (controlled by a not-so-accurate built-in ESP32 real-time clock), they were sent over Wi-Fi to InfluxDB running on RaspberryPi in 6-datapoint batches (i.e. approx. every hour).&lt;/li&gt;
&lt;li&gt;Data was visualized in Grafana (also running on the RPi).&lt;/li&gt;
&lt;/ol&gt;
&lt;h1 style=&quot;text-align:justify;&quot;&gt;Battery wars continued&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The 700mAh battery that I tested while publishing the previous article eventually lasted just &lt;strong&gt;over 5 days and nights, around 130 hours&lt;/strong&gt;. I'd call it quite an impressive result, given that for every measurement the particulate matter sensor had to run for ~11 seconds, draining up to 200mA current alone. So the device stopped working because the battery couldn't supply it with a decent voltage anymore, and unfortunately, I found that the battery was completely dead and showing zero voltage.&lt;/p&gt;
&lt;h2 style=&quot;text-align:justify;&quot;&gt;PCM my ass&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;This is unexpected and somewhat disappointing, because the battery had the Protection Circuit Module (PCM) and yes, it was mounted there as I could see it, yet it apparently didn't do the job of protecting the cell from over-discharging. Also, judging from the battery voltage graph, definitely something went wrong in the last couple of hours, but I wouldn't say such weird behaviour can occur around 3.7V:&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_486&quot; align=&quot;alignnone&quot; width=&quot;2836&quot;]&lt;a href=&quot;https://kapustacc.files.wordpress.com/2018/01/700mah-dead1.png&quot;&gt;&lt;img class=&quot;alignnone size-full wp-image-486&quot; src=&quot;/assets/700mah-dead1.png&quot; alt=&quot;700mAh-dead&quot;/&gt;&lt;/a&gt; Other graphs attached as a proof that the system worked fine all the time during the heavy battery drain...[/caption]&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The dead battery shows 0V on a voltmeter, and that's because the PCM is now apparently active and prevents it from powering the load. The voltage measured on the PCM module input showed 2.85V, which is far below a healthy 3.7-3.6V and even below the (deadly for Li-Po cells) 3.0V level.&lt;/p&gt;
&lt;h2 style=&quot;text-align:justify;&quot;&gt;The Big Boy&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Not thinking too much about it I went on and replace the battery, this time adding a &lt;strong&gt;huge 2400mAh battery&lt;/strong&gt; in hope that it would survive around 2 weeks on a single charge. In fact, it worked great for over 2 weeks, when on the day 16, at slightly over 3.8V, the device stopped working:&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_media-40&quot; align=&quot;alignnone&quot; width=&quot;2856&quot;]&lt;a href=&quot;https://kapustacc.files.wordpress.com/2018/01/2400mah-dead.png&quot;&gt;&lt;img class=&quot;alignnone size-full wp-image-487&quot; src=&quot;/assets/2400mah-dead.png&quot; alt=&quot;2400mAh-dead.png&quot;/&gt;&lt;/a&gt; On the bright side, this means &amp;gt;2 weeks of uninterrupted uptime, yay![/caption]&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;At first, I was like &quot;oh ok, the Raspberry Pi Grafana server went offline&quot;. But no, it didn't. The battery was dead again. With the same symptoms (0V and &amp;lt;3V at the PCM input). This was enough of a motivation to revisit the battery voltage measurement code.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;It turned out that with some of the recent firmware updates that I got in the meantime, the attenuation level that I used for the ADC &lt;strong&gt;changed from 3dB to 2.5dB&lt;/strong&gt;. This basically means that all the measurements were higher than the actual battery voltage, so when the graph said it was at 3.8V, it was in fact somewhere lower.&lt;/p&gt;
&lt;h2 style=&quot;text-align:justify;&quot;&gt;Confession&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Ok, fair enough, but why the script didn't fail at battery voltage measurement function since &lt;code&gt;ADC.ATTN_3DB&lt;/code&gt; changed to &lt;code&gt;ADC.ATTN_2_5DB&lt;/code&gt;?&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Because I used the raw value of &lt;code&gt;1&lt;/code&gt;, instead of specifying the constant:&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code class=&quot;Python&quot;&gt;# good
apin = adc.channel(attn=ADC.ATTN_2_5DB, pin='P16')

# baaaad!
apin = adc.channel(attn=1, pin='P16')
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;In previous firmware the &lt;code&gt;1&lt;/code&gt; used to mean 3dB, and now it became 2.5dB. It wasn't my code after all, as I copied the code from elsewhere, but still, I didn't bother to carefully read it through and try to understand what's going on there and this is purely my fault. This is the great example of how &lt;strong&gt;not paying enough attention to your code can sometimes cost you real money&lt;/strong&gt;.&lt;/p&gt;
&lt;hr /&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;All things considered, if the 2400mAh battery died after 16 days, given the discharging curve similar to that of 700mAh battery, we can safely assume that it would still be in a good condition after 14 days, which is the number I was hoping to achieve when planning the battery consumption for the system. Recharging the device twice a month is still not optimal, but quite acceptable for a home sensor.&lt;/p&gt;
&lt;h1 style=&quot;text-align:justify;&quot;&gt;MQTT&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Mostly to just learn what it is and how it works, I wanted to test out the &lt;a href=&quot;http://mqtt.org&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;MQTT&lt;/a&gt; (Message Queuing Telemetry Transport) protocol. I went with &lt;a href=&quot;https://thingspeak.com&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;ThingSpeak&lt;/a&gt; IoT platform by MathWorks. Setting up an MQTT channel is a no-brainer task, as well as sending the actual data when you have &lt;a href=&quot;https://github.com/pycom/pycom-libraries/blob/master/examples/mqtt/mqtt.py&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;a dedicated library&lt;/a&gt; for that.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The air quality monitor, however, since it mostly just sleeps and becomes active only for a few seconds from time to time, is not the best application for MQTT. As a publish-subscribe-based protocol, MQTT can set up a connection once and keep it alive as the device transmits or receives data. Conversely, the HTTP sends one request per connection and then requires reconnecting for another chunk of data – this way it can drain the battery more than MQTT.&lt;/p&gt;
&lt;h1 style=&quot;text-align:justify;&quot;&gt;LoPy4&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Back in November 2017, I preordered two LoPy4 boards, with LoRa and Sigfox connectivity (in addition to Wi-Fi and Bluetooth), 4MB RAM and 8MB flash memory 😮. The main purpose was to play with LoRa, and the natural first step – to connect the air quality monitor to LoRaWAN. When I received the boards a couple of weeks ago I started off by replacing the WiPy with LoPy4 in the device. Which turned out tougher than I would expect.&lt;/p&gt;
&lt;h2 style=&quot;text-align:justify;&quot;&gt;Running short of GPIOs&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Both LoPy4 and WiPy (and all the other Pycom development boards)  have the same amount of pins, but some of them are reserved, especially on more sophisticated boards. In case of LoPy4 (and the first version of LoPy), there are three pins reserved for communication with the LoRa chip. This way they can't be used by the developer. Add to that 6 input-only pins, 2 programming pins, a Wi-Fi antenna-switch pin and overall 9 pins taken by my device's sensors, it's getting pretty tight. If I were to add another peripheral, I might have a hard time finding a place for it.&lt;/p&gt;
&lt;h2 style=&quot;text-align:justify;&quot;&gt;Firmware and RTC issues&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Like Pycom staff said &lt;a href=&quot;https://forum.pycom.io/topic/2077/new-order-of-lopy-deep-sleep-issues/21&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;on their forum&lt;/a&gt;, they got the boards a bit earlier than expected so they started shipping them while still ironing out the software. I could precisely feel it while watching my Guru Meditation errors in the console :) according to other comments on the forum, these errors could have been caused by importing some modules or calling specific functions/classes (e.g. &lt;code&gt;machine.PWM&lt;/code&gt; in my case) too early in the &lt;code&gt;main.py&lt;/code&gt; script. It was clearly a firmware bug and it seems to have been fixed in 1.14.0.b1 release.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The other thing I noticed in the LoPy4 was that the real-time clock didn't persist its state in deep sleep &lt;strong&gt;while on &lt;/strong&gt;&lt;strong&gt;battery power&lt;/strong&gt;. It worked perfectly fine (for an ESP32 RTC ;)) when powered via USB. This was something I heavily relied on, as the script's workflow looked like this:&lt;/p&gt;
&lt;ul style=&quot;text-align:justify;&quot;&gt;
&lt;li&gt;sync RTC with NTP server on first boot,&lt;/li&gt;
&lt;li&gt;take 6 measurements every 10 minutes,&lt;/li&gt;
&lt;li&gt;after 1 hour send the data via Wi-Fi and resync the RTC,&lt;/li&gt;
&lt;li&gt;repeat from step 2.&lt;/li&gt;
&lt;/ul&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;In the current situation, the RTC time was gone after first 10-minute deep sleep. A quick workaround was sacrificing the 10-minute granularity and sending only one data point with mean values of the 6 measurements. But as I wanted to keep the 10-minute resolution, I went with an external RTC module.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;I found the DS3231 module relatively cheap and somewhat nicely fitting the expansion board. I'd prefer male pins, but eventually, I sorted it out in some way:&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_490&quot; align=&quot;alignnone&quot; width=&quot;1024&quot;]&lt;a href=&quot;https://kapustacc.files.wordpress.com/2018/02/img_3373.jpg&quot;&gt;&lt;img class=&quot;aligncenter size-large wp-image-490&quot; src=&quot;/assets/img_3373.jpg?w=2048&quot; alt=&quot;IMG_3373&quot;/&gt;&lt;/a&gt; &lt;em&gt;&quot;Any two interfaces can be connected using a finite number of adapters&quot;.&lt;/em&gt;[/caption]&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;My DS3231 module comes with a battery and has 5 pins: two for power, two for I2C connectivity and one is not connected, yet it still occupies the precious place on the expansion board. I found the &lt;a href=&quot;https://github.com/peterhinch/micropython-samples/tree/master/DS3231&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;DS3231 MicroPython library on GitHub&lt;/a&gt; and was able to adapt it to Pycom board &lt;a href=&quot;https://github.com/ayoy/upython-aq-monitor/blob/master/lib/ds3231.py&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;fairly easily&lt;/a&gt;.&lt;/p&gt;
&lt;h2 style=&quot;text-align:justify;&quot;&gt;RTC lagging&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Once I got the external RTC up and running, I noticed that although it remembered the synced time, it stopped for around 8-10 seconds with every board reset. For the time of reset and boot-up the time wouldn't have progressed, and after that, it ran fine again, but with this several-second offset. After some investigation, I found out that LoPy4 GPIO pins connected to the power pins for DS3231 were pulled down during reset. In this case, the DS3231 activated some kind of power cut-off circuit that also switched off the onboard battery and was effectively stopping the RTC.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;I solved the issue by pulling up the Vin pin of the RTC to LoPy's 3V3 output. It's not straightforward to do so on the expansion board, but this solution is good enough for a proof of concept:&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_492&quot; align=&quot;alignnone&quot; width=&quot;2560&quot;]&lt;a href=&quot;https://kapustacc.files.wordpress.com/2018/02/resistor1.jpg&quot;&gt;&lt;img class=&quot;alignnone size-full wp-image-492&quot; src=&quot;/assets/resistor1.jpg&quot; alt=&quot;resistor&quot;/&gt;&lt;/a&gt; Surprise resistor![/caption]&lt;/p&gt;
&lt;h2 style=&quot;text-align:justify;&quot;&gt;LoRaWAN&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;...And then I managed to connect the board via LoRaWAN to The Things Network. It was an interesting experience, took me almost a whole weekend but now it seems to work reliably for a week already. I'll better write about it in a separate blog post though because otherwise, no-one would ever finish reading this one.&lt;/p&gt;
&lt;h1 style=&quot;text-align:justify;&quot;&gt;Custom PCB&lt;/h1&gt;
&lt;p&gt;[caption id=&quot;attachment_493&quot; align=&quot;alignnone&quot; width=&quot;1024&quot;]&lt;a href=&quot;https://kapustacc.files.wordpress.com/2018/02/boxed.jpg&quot;&gt;&lt;img class=&quot;aligncenter size-large wp-image-493&quot; src=&quot;/assets/boxed.jpg?w=2048&quot; alt=&quot;boxed&quot;/&gt;&lt;/a&gt; Getting tight...[/caption]&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;So I initially built the whole device around the expansion board and it was fine for a first prototype, with only two peripherals (the air quality sensor and the T/RH sensor). After adding the RTC, especially with an extra resistor, I thought it was too much and decided to prepare a custom circuit for the sensor.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;a href=&quot;https://kapustacc.files.wordpress.com/2018/02/schematic.png&quot;&gt;&lt;img class=&quot;alignnone size-full wp-image-494&quot; src=&quot;/assets/schematic.png&quot; alt=&quot;schematic&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;It's not overly complicated, basically just connecting peripherals to the LoPy + a 5V power supply for the air quality monitor.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The first, home-made version of the PCB was just a proof of concept, mainly to confirm that I can get rid of the expansion board. I also decided to take things to the next level and manufacture the final boards in a PCB factory. But for now, here it is:&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_495&quot; align=&quot;alignnone&quot; width=&quot;4032&quot;]&lt;a href=&quot;https://kapustacc.files.wordpress.com/2018/02/img_3385-2.jpg&quot;&gt;&lt;img class=&quot;alignnone size-full wp-image-495&quot; src=&quot;/assets/img_3385-2.jpg&quot; alt=&quot;IMG_3385 2&quot;/&gt;&lt;/a&gt; Sorry for the ugly silicone sealing![/caption]&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;It works great, with the exception of battery voltage measurement. Yes, I completely forgot about the voltage divider – with this board I'd need to monitor it manually with a voltmeter every couple of days...&lt;/p&gt;
&lt;h1 style=&quot;text-align:justify;&quot;&gt;Final PCB project&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Apart from the voltage measurement circuit, the other important component missing from the board is the battery charger. I mean, for the device to be shippable and easy to use, it should come equipped with a USB port to charge the battery.&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_497&quot; align=&quot;alignnone&quot; width=&quot;1356&quot;]&lt;a href=&quot;https://kapustacc.files.wordpress.com/2018/02/charger.png&quot;&gt;&lt;img class=&quot;alignnone size-full wp-image-497&quot; src=&quot;/assets/charger.png&quot; alt=&quot;charger&quot;/&gt;&lt;/a&gt; Battery charger circuit[/caption]&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The battery charger circuit includes:&lt;/p&gt;
&lt;ul style=&quot;text-align:justify;&quot;&gt;
&lt;li&gt;the MAX1811 charger module itself,&lt;/li&gt;
&lt;li&gt;the micro USB port,&lt;/li&gt;
&lt;li&gt;the LED diode that indicates charging,&lt;/li&gt;
&lt;li&gt;the circuit that cuts off the battery from the load during charging, and connects the USB voltage instead. It's very cool – check out how it works &lt;a href=&quot;http://tinyurl.com/y8ybudgo&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;over here&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;This adds some significant complexity to the device and especially to the PCB layout, but I'm fine with it as I admire working with a PCB designer. As an Integrated Circuits Design graduate, I can recall a little bit the good old times of working long hours with Cadence Virtuoso, which still, compared to KiCad Pcbnew, is like building with Lego Technics vs Duplo.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;a href=&quot;https://kapustacc.files.wordpress.com/2018/02/screen-shot-2018-01-26-at-00-04-57.png&quot;&gt;&lt;img class=&quot;alignnone size-large wp-image-498&quot; src=&quot;/assets/screen-shot-2018-01-26-at-00-04-57.png?w=2048&quot; alt=&quot;Screen Shot 2018-01-26 at 00.04.57&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The above project got sent to the factory, and while writing this, I'm still waiting for the delivery. I'll keep you posted as the PCBs arrive, hope I'll manage to solder the micro USB port correctly :)&lt;/p&gt;
&lt;p&gt;Of course, the most up-to-date code for the device is available &lt;a href=&quot;https://github.com/ayoy/upython-aq-monitor/tree/lora&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;from Github, from the &lt;code&gt;lora&lt;/code&gt; branch&lt;/a&gt;.&lt;/p&gt;

&lt;div id=&quot;disqus_thread&quot;&gt;&lt;/div&gt;
&lt;script&gt;

/**
*  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
*  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/

var disqus_config = function () {
this.page.url = 'https://ayoy.github.io';  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = 'kapusta-cc'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};

(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://kapusta-cc.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
&lt;/script&gt;
&lt;noscript&gt;Please enable JavaScript to view the &lt;a href=&quot;https://disqus.com/?ref_noscript&quot;&gt;comments powered by Disqus.&lt;/a&gt;&lt;/noscript&gt;</content><author><name>Dominik Kapusta</name></author><category term="embedded" /><category term="air quality" /><category term="battery" /><category term="lopy" /><category term="pycom" /><category term="python" /><category term="wipy" /><summary type="html">It's been a while since I deployed the WiPy-powered air quality monitor on my balcony. I have since replaced the microcontroller with a LoPy4 to enable LoRa connectivity, and I switched to a custom-made PCB.</summary></entry><entry><title type="html">Project: Build Button</title><link href="/2018/01/16/build-button/" rel="alternate" type="text/html" title="Project: Build Button" /><published>2018-01-16T18:06:14+00:00</published><updated>2018-01-16T18:06:14+00:00</updated><id>/2018/01/16/build-button</id><content type="html" xml:base="/2018/01/16/build-button/">&lt;p style=&quot;text-align:justify;&quot;&gt;A couple of months ago I did a strong effort towards automating build-related tasks of our iOS app at &lt;a href=&quot;http://lab.getbase.com&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Base&lt;/a&gt; (you can read more on that in &lt;a href=&quot;/2018/01/09/reducing-tech-debt-in-base-ios-app-part-3-automation/&quot;&gt;my previous blog post&lt;/a&gt;). The result was amazing, especially when it comes to preparing AppStore submissions, that normally require &lt;a href=&quot;/assets/screen-shot-2018-01-05-at-13-01-33.png&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;much more effort&lt;/a&gt; than just doing a release build. Our team really enjoyed the new tools:&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_452&quot; align=&quot;alignnone&quot; width=&quot;1024&quot;]&lt;img class=&quot;alignnone size-large wp-image-452&quot; src=&quot;/assets/hipchat-triggered-e1515998330458.png?w=2048&quot; alt=&quot;hipchat-triggered&quot;/&gt; https://www.youtube.com/watch?v=NMS2VnDveP8[/caption]&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Siri support aside, the button instantly caught my attention. It sounded like a simple project with (somewhat) real-life use case. My friend was in need and I could help him out, possibly sparking his and our other colleagues' interest in learning more about embedded programming. And, after all, you always wanted a big button on your desk for whatever purpose, admit it.&lt;/p&gt;
&lt;h1 style=&quot;text-align:justify;&quot;&gt;Scope&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;I defined the following requirements straight off:&lt;/p&gt;
&lt;ul style=&quot;text-align:justify;&quot;&gt;
&lt;li&gt;The button would connect to the computer and run a predefined command. Fastlane runs in terminal so I assumed it would first open a system shell and then execute the command.&lt;/li&gt;
&lt;li&gt;The wired connection is so 2000, the Wi-Fi connection is too much and not really handy ⇒ Bluetooth Low Energy sounds just fine. I know close to nothing about it (barring mobile apps working with BLE beacons), but that's fine since I'm going to learn it.&lt;/li&gt;
&lt;li&gt;I could write a client app on the OS X that would connect to the button, and allow for configuring its action. I'm normally doing roughly one OS X app every 4 years and it was about time.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 style=&quot;text-align:justify;&quot;&gt;Hardware&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Remember I said I knew nothing about Bluetooth? That's true to the extent that I wasn't sure whether I'd achieve what I wanted with Bluetooth Low Energy, or I needed to go with Bluetooth Classic. I also wanted to cut costs a little bit, so I decided to give it a try with &lt;a class=&quot;&quot; href=&quot;http://espressif.com/en/products/hardware/esp-wroom-32/overview&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;ESP-WROOM-32 microcontroller&lt;/a&gt; from Espressif Systems. It's a very powerful dual-core chip with WiFi, BLE, and Bluetooth Classic connectivity, and it measures a mere 18x25.5mm with integrated PCB antenna included.&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_454&quot; align=&quot;aligncenter&quot; width=&quot;1532&quot;]&lt;img class=&quot;aligncenter size-full wp-image-454&quot; src=&quot;/assets/esp-ali.png&quot; alt=&quot;esp-ali.png&quot;/&gt; Cutting costs pretty aggressively actually.[/caption]&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;It has a 1.27mm raster which meant some serious SMD effort just to connect wires to it, so I ordered two chips in case I fail, and planned to solder it to the ESP-32-specific 2.54mm raster adapter.&lt;/p&gt;
&lt;h2 style=&quot;text-align:justify;&quot;&gt;Fun&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;For the most crucial part of the device, the &lt;a href=&quot;https://www.adafruit.com/product/1189&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Adafruit Massive Arcade Button&lt;/a&gt; was the obvious ideal candidate. It features a 10cm diameter dome with an LED backlight, and since I saw it for the first time I was looking for a good enough excuse to finally get it.&lt;/p&gt;
&lt;h1 style=&quot;text-align:justify;&quot;&gt;Ready, set, go!&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;One month later when the package from Singapore arrived, I was ready to get my hands dirty. It was a very wise decision to order two chips, as it didn't take long till I failed at putting the first one on the adapter. I didn't notice the slight misalignment in the left upper area and made several short circuits, and then unsoldering the chip using decent temperature was too much for my skills.&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_455&quot; align=&quot;aligncenter&quot; width=&quot;1024&quot;]&lt;img class=&quot; size-large wp-image-455 aligncenter&quot; src=&quot;/assets/adapter-fail.jpg?w=2048&quot; alt=&quot;adapter-fail&quot;/&gt; How to: waste your shiny new ESP-WROOM-32 by soldering it misaligned.[/caption]&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;If that was not enough of a bad luck, I wasted the other chip in a similar way just a few moments later. (I thought) I soldered it properly this time (without obviously visible misalignments), but still, something went wrong, as I lost the connection to the chip soon after I powered it up.&lt;/p&gt;
&lt;h2 style=&quot;text-align:justify;&quot;&gt;Step back. Regroup.&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Having failed so miserably I resorted to getting a dedicated development board. I thought I'd do it the right way – start off with the dev board, and having figured out the working prototype, I'd replace it with a bare microcontroller and only the required circuitry. Luckily enough I got an &lt;a href=&quot;http://esp-idf.readthedocs.io/en/latest/get-started/get-started-devkitc.html&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;ESP-DevKitC&lt;/a&gt; just before the Christmas break.&lt;/p&gt;
&lt;h1 style=&quot;text-align:justify;&quot;&gt;Prototype&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The circuit is really simple – here's what's needed:&lt;/p&gt;
&lt;ul style=&quot;text-align:justify;&quot;&gt;
&lt;li&gt;1x ESP-32 dev board,&lt;/li&gt;
&lt;li&gt;1x tact switch,&lt;/li&gt;
&lt;li&gt;1x LED,&lt;/li&gt;
&lt;li&gt;1x resistor (to limit the LED current).&lt;/li&gt;
&lt;/ul&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The LED (and the resistor) could be omitted but since it's already provided with the Adafruit dome button, let's make some use of it.&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_456&quot; align=&quot;aligncenter&quot; width=&quot;2560&quot;]&lt;img class=&quot; size-full wp-image-456 aligncenter&quot; src=&quot;/assets/esp-devkitc.jpg&quot; alt=&quot;esp-devkitc&quot;/&gt; Simple as that.[/caption]&lt;/p&gt;
&lt;h2 style=&quot;text-align:justify;&quot;&gt;Working principle&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;I imagined the big picture the following way:&lt;/p&gt;
&lt;ul style=&quot;text-align:justify;&quot;&gt;
&lt;li&gt;the button device is a BLE accessory, just like a heart rate monitor,&lt;/li&gt;
&lt;li&gt;the computer is a client that connects to the button; it can be configured to run a predefined command when triggered,&lt;/li&gt;
&lt;li&gt;when the button is pressed, it notifies the client; it also switches on the LED to indicate a busy state,&lt;/li&gt;
&lt;li&gt;the client acknowledges the notification and runs a command,&lt;/li&gt;
&lt;li&gt;when the command finishes, the client updates the button so that it turns off the LED and waits to be pressed again.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 style=&quot;text-align:justify;&quot;&gt;First run&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Like I mentioned earlier, I was new to Bluetooth Low Energy, especially server-side, and then I was also new to ESP-32 and &lt;a href=&quot;https://esp-idf.readthedocs.io&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;esp-idf development environment&lt;/a&gt;. Having spent some quality time coding in MicroPython for &lt;a href=&quot;http://pycom.io&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Pycom&lt;/a&gt; boards, I was pretty upset to realize that at the moment ESP-32 MicroPython still lacks some of the chip's features, including Bluetooth APIs. This meant that I had to resort to plain C programming in FreeRTOS environment.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Fortunately, I was able to base my work on the GATT Server example of the esp-idf, and adjust it to my needs, removing unnecessary code and adding handlers for the button and the LED.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;After some trial-and-error fiddling with both the development board and the OS X app I was extremely happy to see them interact with each other:&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;[vimeo 251331227 w=640 h=360]&lt;/p&gt;
&lt;h1 style=&quot;text-align:justify;&quot;&gt;Implementation&lt;/h1&gt;
&lt;h3 style=&quot;text-align:justify;&quot;&gt;Server (the button)&lt;/h3&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Going into details, the button implements the GATT Server, and the computer acts as a GATT client. The Server defines two services:&lt;/p&gt;
&lt;ul style=&quot;text-align:justify;&quot;&gt;
&lt;li&gt;&lt;em&gt;trigger&lt;/em&gt; – with the read-only characteristic with notification capability, sent out when pressing the button,&lt;/li&gt;
&lt;li&gt;&lt;em&gt;idle&lt;/em&gt; – with write-only characteristic, accepting a boolean value (effectively only the &lt;code&gt;true&lt;/code&gt; value) to indicate that the task finished.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 style=&quot;text-align:justify;&quot;&gt;Client (the computer)&lt;/h3&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The Client searches for peripherals with mentioned services. Once found and connected, it sets the &lt;em&gt;idle&lt;/em&gt; characteristic to &lt;code&gt;true&lt;/code&gt; and requests notifications for the &lt;em&gt;trigger &lt;/em&gt;characteristic. In the UI it allows for defining a command (or set of commands) to be passed to the Terminal.app for execution.&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_460&quot; align=&quot;aligncenter&quot; width=&quot;398&quot;]&lt;img class=&quot;aligncenter  wp-image-460&quot; src=&quot;/assets/buildbutton-osx1.png?w=600&quot; alt=&quot;buildbutton-osx&quot;/&gt; So much for the UI.[/caption]&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Now for the tricky part: &lt;strong&gt;figuring out when the command finished&lt;/strong&gt;. Since the app passes control to the external application (Terminal.app), to know when Terminal.app is done running a command, it needs to handle some kind of inter-process communication. Which is not a big deal, at least compared to &lt;em&gt;how to tell the other app to notify my app back&lt;/em&gt;.&lt;/p&gt;
&lt;h3 style=&quot;text-align:justify;&quot;&gt;Inter-process communication in MacOS X&lt;/h3&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Well, in case of Terminal.app, it runs a system shell so we can easily inject some command into the shell to do a callback for the BuildButton.app. As it comes to the IPC itself, there are several solutions for OS X ranging from a shared file on disk to using Mach ports. I ended up somewhere in between by using the oldie but goodie &lt;strong&gt;Apple Script&lt;/strong&gt;. With its funny, naïve syntax and the ability to expose virtually any behaviour to the app's scripting interface I believed it was the right tool for the job here.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;For opening up the Terminal and running a command, I came up with the following helper script:&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code class=&quot;AppleScript&quot;&gt;on runInTerminal(command)
    tell application &amp;quot;Terminal&amp;quot;
      activate
      set newTab to do script(command)
    end tell
    return newTab
end runInTerminal
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;And the command itself had to contain the actual user-defined list of commands, plus a callback to our app:&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code class=&quot;Swift&quot;&gt;let argument = { () -&amp;gt; String in
    let finishCommand = &amp;quot;osascript -e 'tell application \\\&amp;quot;BuildButton\\\&amp;quot; to finish'&amp;quot;
    if commandToRun.isEmpty {
        return finishCommand
    }
    // commandToRun is the user-defined command
    return &amp;quot;\(commandToRun); \(finishCommand)&amp;quot;
}()
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;What does it mean to &lt;em&gt;tell application &quot;BuildButton&quot; to finish&lt;/em&gt;? Have a look at the definition:&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code class=&quot;xml&quot;&gt;&amp;lt;command name=&amp;quot;finish&amp;quot; code=&amp;quot;bbfinish&amp;quot; description=&amp;quot;Finish current task.&amp;quot;&amp;gt;
    &amp;lt;cocoa class=&amp;quot;BuildButton.FinishCommand&amp;quot;&amp;gt;
&amp;lt;/command&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;It tells the app to use &lt;code&gt;FinishCommand&lt;/code&gt;, which is a subclass of &lt;code&gt;NSScriptCommand&lt;/code&gt; and does the actual job of updating the &lt;em&gt;idle&lt;/em&gt; characteristic of the button.&lt;/p&gt;
&lt;h1 style=&quot;text-align:justify;&quot;&gt;The dedicated button circuit&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;img class=&quot;alignright size-medium wp-image-471&quot; src=&quot;/assets/dome.gif&quot; alt=&quot;dome&quot;/&gt;Since the dev board prototype turned out working fine, I could finally order THE BUTTON, and design a PCB for the circuit. I also decided to give it a last shot with soldering the bare ESP-32 chip to the adapter and I finally got it right :D&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The first version of a circuit contains the following components:&lt;/p&gt;
&lt;ul style=&quot;text-align:justify;&quot;&gt;
&lt;li&gt;3xAAA battery power supply (4.5V),&lt;/li&gt;
&lt;li&gt;3.3V step-up/step-down voltage regulator for the ESP-32,&lt;/li&gt;
&lt;li&gt;9V step-up voltage regulator for the LED (I was told it's for 5-12V but I was short of time and I didn't want to risk it not working or flashing too dim),&lt;/li&gt;
&lt;li&gt;an additional circuitry for the chip to work fine: power supply capacitors, and EN and IO0 pin pull-up resistors,&lt;/li&gt;
&lt;li&gt;button interface,&lt;/li&gt;
&lt;li&gt;LED interface,&lt;/li&gt;
&lt;li&gt;programming interface (UART + VCC/GND).&lt;/li&gt;
&lt;/ul&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;This is the schematic:&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;img class=&quot;alignnone size-full wp-image-465&quot; src=&quot;/assets/sch1.png&quot; alt=&quot;sch.png&quot;/&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;I copied over some part of it (like EN/IO0 and power supply capacitors) directly from the schematic of ESP-DevKitC which is available &lt;a href=&quot;https://dl.espressif.com/dl/schematics/ESP32-Core-Board-V2_sch.pdf&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;online&lt;/a&gt;. If you take a closer look at my schematic, you'll notice that it's highly unoptimized, e.g. it includes two DC voltage regulator circuits: a step-up 9V for the LED and a step-up/step-down 3.3V for the chip. Firstly, it's a pretty expensive solution, and also the 9V regulator drains about 1mA of constant current when idle, with significant impact on battery life.&lt;/p&gt;
&lt;h2 style=&quot;text-align:justify;&quot;&gt;Power supply issues&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;I did some initial bare board testing with the circuit similar to this:&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;img class=&quot; size-large wp-image-467 aligncenter&quot; src=&quot;/assets/protoboard.jpg?w=2048&quot; alt=&quot;protoboard&quot;/&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;And I couldn't get it to work because the chip was constantly restarting right after booting up. The console output showed the following message:&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code class=&quot;plaintext&quot;&gt;Brownout detector was triggered
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;It means that the power supply can't deliver high enough voltage to drive the microcontroller. In my case, I was powering it from my computer's USB port via the USB-UART adapter, which should deliver a solid 3.3V for up to at least around 500mA. I also tried the same with a 3.7V Li-ion battery and the standalone regulated DC adapter with no luck. Then I decided to have a look at the program I was running on the ESP.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;When I tried it with a simple hello world example, everything worked fine. But every time I initialized Bluetooth, the brownout detector fired and restarted the device. This must have been caused by a high current peak occurring when powering up Bluetooth. And that's what the bulk capacitor sitting between power supply lines is supposed to handle – cover the current peaks with its charge. In my case, however, it didn't work with a capacitor placed on the breadboard. When I later dug into the topic I learned that this capacitor:&lt;/p&gt;
&lt;ul style=&quot;text-align:justify;&quot;&gt;
&lt;li&gt;should be added as close to the chip power input as possible – apparently, a 20cm wire between the breadboard and the microcontroller is not close enough,&lt;/li&gt;
&lt;li&gt;should be of high quality, i.e. low impedance – the regular electrolytic capacitor is actually quite the opposite.&lt;/li&gt;
&lt;/ul&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Even trying to place the huge (680uF) electrolytic capacitor so close that it directly touched the power input pins didn't work. I ended up disabling the brownout detector in my chip, which is obviously not recommended*, but it didn't cause any unexpected crashes in my case. I still ordered a bunch of low ESR SMD capacitors and hoped it would work on the final PCB.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;[*] - you usually risk the undefined behaviour in places where the logic level cannot be determined due to the voltage being too low for a logic one, yet too high for a logic zero&lt;/p&gt;
&lt;h2 style=&quot;text-align:justify;&quot;&gt;Printed board design&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;For the PCB, I wanted it to be some kind of a &quot;hat&quot; on top of the ESP adapter board:&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_466&quot; align=&quot;alignnone&quot; width=&quot;1024&quot;]&lt;img class=&quot;alignnone size-large wp-image-466&quot; src=&quot;/assets/screen-shot-2018-01-15-at-22-17-48.png?w=2048&quot; alt=&quot;Screen Shot 2018-01-15 at 22.17.48&quot;/&gt; Quite a tight fit for a hand-made PCB, if you ask me.[/caption]&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Even though the dome button itself is huge and it needs an even bigger case, I went with a reasonably small form factor, and the final board looked like this:&lt;/p&gt;
&lt;p&gt;[gallery ids=&quot;470,468,469&quot; type=&quot;rectangular&quot; link=&quot;file&quot;]&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;With help of some velcro and a glue gun I managed to put everything in a box:&lt;/p&gt;
&lt;p&gt;[gallery ids=&quot;472,473&quot; type=&quot;rectangular&quot;]&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;And give it a proper test:&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;[vimeo 251343761 w=640 h=360]&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Also, with the bulk capacitor soldered just millimeters away from the VCC/GND pins I managed to get rid of the brownout issues!&lt;/p&gt;
&lt;h1 style=&quot;text-align:justify;&quot;&gt;Recap&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;I must say that when I thought of doing the button for the first time, I had no idea just how much work it would require. After all, it sounds extremely simple, you press a button and it does one simple thing on the Mac. Obviously the requirement of using plain C language for the ESP32 app had huge impact on my perceived effort of this project. And while the device does its job already and I can showcase it to people and get very positive feedback (again, who doesn't like big arcade-style push buttons), it's not yet ready to be left alone in the office for the guys to actually trigger builds with it.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Have a look at the short list of features:&lt;/p&gt;
&lt;ul style=&quot;text-align:justify;&quot;&gt;
&lt;li&gt;it can connect to the client app and remember it&lt;/li&gt;
&lt;li&gt;it, most importantly, can trigger a client app to run a command&lt;/li&gt;
&lt;li&gt;it does the pulse/heartbeat LED flashing when the command is run; the heartbeat stops when done, or it can be turned off by pressing the button again while running the command (it effectively resets the button state to idle)&lt;/li&gt;
&lt;li&gt;it goes to deep sleep after 20s of inactivity (very short time, to conserve battery); when pressed during deep sleep it starts advertising and if the remembered client connects to it, it sends trigger notification - but this adds a 2-3s delay.&lt;/li&gt;
&lt;/ul&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;And for bugs, or stuff that's missing:&lt;/p&gt;
&lt;ul style=&quot;text-align:justify;&quot;&gt;
&lt;li&gt;The device drains around 1500uA when sleeping, and I bet it's due to the sophisticated voltage regulators I used for both the chip and the LED circuit. Skipping these regulators should have reduced the deep sleep battery drain, and I'm going to give it a try.&lt;/li&gt;
&lt;li&gt;The ESP-32 drains up to 150mA when Bluetooth is active and LED is flashing and it's a ridiculously high current. I tried optimizing it by e.g. clocking down the CPU, disabling one of the cores, limiting Bluetooth radio power but no luck so far. I don't use WiFi when the chip is running, and the LED current is less than 10mA. It may be that ESP-32 is simply not the right tool for this job. The Nordic Semiconductor's nRF51822 would probably work much longer, but would possibly require even more work (...?)&lt;/li&gt;
&lt;li&gt;Switching between clients is not sorted out yet – for now it needs the currently paired client app to disconnect (e.g. by quitting the app) in order to connect to the other one. I wanted as minimal UI as possible in the device, so I ended up with just one button. I recently implemented the long press recognizer and I'll try to add pairing mode there.&lt;/li&gt;
&lt;/ul&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;And so on and so on... Even if the project is unfinished, it gave me yet another solid lesson of embedded systems. I was able to learn about esp-idf, basics of Bluetooth Low Energy, soldering 1.27mm raster integrated circuits just to name a few. Oh and AppleScript, obviously :)&lt;/p&gt;
&lt;p&gt;Also, you know it was worth the effort, when people want to have such a button right after you showcase it to them. And when you give an awesome talk on improving iOS apps build performance and automating tasks, but the main topic on Q&amp;amp;A is &lt;em&gt;how does the button work?&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;https://www.facebook.com/lodzwiosluje/videos/1221621174637929/&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;I'll still be tinkering with the button as time permits, trying to make something shippable. The up-to-date code is available on GitHub, for both &lt;a href=&quot;https://github.com/ayoy/the-build-button-esp&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;the button itself&lt;/a&gt; and &lt;a href=&quot;https://github.com/ayoy/the-build-button-osx&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;the client app&lt;/a&gt;. Stay tuned for more weird stuff!&lt;/p&gt;

&lt;div id=&quot;disqus_thread&quot;&gt;&lt;/div&gt;
&lt;script&gt;

/**
*  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
*  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/

var disqus_config = function () {
this.page.url = 'https://ayoy.github.io';  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = 'kapusta-cc'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};

(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://kapusta-cc.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
&lt;/script&gt;
&lt;noscript&gt;Please enable JavaScript to view the &lt;a href=&quot;https://disqus.com/?ref_noscript&quot;&gt;comments powered by Disqus.&lt;/a&gt;&lt;/noscript&gt;</content><author><name>Dominik Kapusta</name></author><category term="embedded" /><category term="adafruit" /><category term="applescript" /><category term="ble" /><category term="bluetooth" /><category term="build button" /><category term="button" /><category term="esp" /><category term="esp-idf" /><category term="esp-wroom-32" /><category term="macos" /><category term="osx" /><summary type="html">Accidentally inspired by my colleagues during a casual chat, I developed the the ESP32-powered Bluetooth Low Energy button that triggers shell commands on a Mac.</summary></entry><entry><title type="html">Reducing tech debt in Base iOS app: Part 3 – automation</title><link href="/2018/01/09/reducing-tech-debt-in-base-ios-app-part-3-automation/" rel="alternate" type="text/html" title="Reducing tech debt in Base iOS app: Part 3 – automation" /><published>2018-01-09T10:24:52+00:00</published><updated>2018-01-09T10:24:52+00:00</updated><id>/2018/01/09/reducing-tech-debt-in-base-ios-app-part-3-automation</id><content type="html" xml:base="/2018/01/09/reducing-tech-debt-in-base-ios-app-part-3-automation/">&lt;p style=&quot;text-align:justify;&quot;&gt;This is the third and final part of a series of blog posts related to limiting tech debt in Base iOS app. Take a look at &lt;a href=&quot;/2018/01/02/tech-debt-at-base-warnings/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;the first part&lt;/a&gt; and &lt;a href=&quot;/2018/01/05/reducing-ios-tech-debt-at-base-part-2-build-time/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;the second part&lt;/a&gt; if you like.&lt;/p&gt;
&lt;h1 style=&quot;text-align:justify;&quot;&gt;Why bother?&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Base iOS client version 3.5.2 is a build number 2259. Base 2.3 from January 2013 was a build number 270. Roughly 2000 builds in 5 years means roughly 8 builds every week, and if you won't optimize it to be a streamlined task, it can become a painful experience.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Other than just automating release builds, you might want to perform regular health checks of your project. Continuous integration helps in that by running unit tests and/or automated UI tests suites. But it can be additionally upgraded to collect some interesting data while already making builds.&lt;/p&gt;
&lt;h2 style=&quot;text-align:justify;&quot;&gt;Common tools&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;We recently adopted &lt;a href=&quot;http://fastlane.tools&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;fastlane&lt;/a&gt; to automate various tasks around our codebase. I'm not sure if it requires an introduction, but if you were living in a cave &lt;strong&gt;just like me&lt;/strong&gt;, fastlane is a modern, good-looking automation tool, written in Ruby and developed very actively all the time. Not only it contains a great number of built-in actions but it also allows you to define your own actions or plugins that nicely integrate into it.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;We run continuous integration using Jenkins, but that's not sexy at all so I'll just stop here by saying that we configured Jenkins jobs such way that they simply call fastlane to do all the heavy-lifting.&lt;/p&gt;
&lt;h2 style=&quot;text-align:justify;&quot;&gt;Uncommon tools&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;What's perhaps more interesting, is that while we're running CI tasks we also gather additional information about the build, like the duration, warning count and code coverage stats. We store that information in &lt;a href=&quot;https://www.influxdata.com/time-series-platform/influxdb/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;InfluxDB&lt;/a&gt; – a lightweight time series database with HTTP API. We then visualize the data with &lt;a href=&quot;http://grafana.com&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Grafana&lt;/a&gt;. What is very important – &lt;strong&gt;both of these tools are available for free&lt;/strong&gt; when you self-host them so you can give them a spin any time, and I recommend it. I personally run them at home on my RaspberryPi and use them for &lt;a href=&quot;/2017/12/02/home-made-air-quality-monitoring-using-wipy/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;some cool stuff&lt;/a&gt;.&lt;/p&gt;
&lt;h1 style=&quot;text-align:justify;&quot;&gt;AppStore release builds&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Let's have a look at the list of AppStore release chores for Base App:&lt;/p&gt;
&lt;ol style=&quot;text-align:justify;&quot;&gt;
&lt;li&gt;sync submodules&lt;/li&gt;
&lt;li&gt;install pods&lt;/li&gt;
&lt;li&gt;set desired build version&lt;/li&gt;
&lt;li&gt;update build number (check HockeyApp for the latest beta/RC build number and increment it)&lt;/li&gt;
&lt;li&gt;go to iTunesConnect; create a new app version&lt;/li&gt;
&lt;li&gt;go to Xcode; select AppStore scheme&lt;/li&gt;
&lt;li&gt;build and archive&lt;/li&gt;
&lt;li&gt;upload to iTunesConnect&lt;/li&gt;
&lt;li&gt;locate the Xcode archive&lt;/li&gt;
&lt;li&gt;compress debug symbols&lt;/li&gt;
&lt;li&gt;upload debug symbols to HockeyApp&lt;/li&gt;
&lt;li&gt;compress the whole Xcode archive&lt;/li&gt;
&lt;li&gt;upload the archive to Dropbox&lt;/li&gt;
&lt;/ol&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;It could take around 60-90 minutes to do all these things, possibly a little less if you focus solely on the release job. I mean, while waiting for a release build to compile, and then for various uploads to finish, you oftentimes decide to start a side task in the meantime (like a small bugfix, testing, documentation, &lt;del&gt;facebook&lt;/del&gt;, etc.) that you'd still have to interrupt shortly in order to follow up with the release. In case you forget you were releasing an app, the process could extend up to a whole day...&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Let's try to eliminate the human factor. The list of AppStore release chores for Base when using fastlane:&lt;/p&gt;
&lt;ol style=&quot;text-align:justify;&quot;&gt;
&lt;li&gt;call &lt;code&gt;bundle exec fastlane submit version:X.Y.Z&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;There's no point 2.&lt;/li&gt;
&lt;/ol&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Oh, and it also fetches the most up-to-date certificates from Apple Developer Portal, I forgot about it because it just automagically happens while I'm moving on to other tasks of the day.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;img class=&quot;  wp-image-377 aligncenter&quot; src=&quot;/assets/screen-shot-2018-01-05-at-13-01-33.png?w=1262&quot; alt=&quot;Screen Shot 2018-01-05 at 13.01.33&quot;/&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;34 minutes? Not bad for a release build and uploading over 300MB of data (ipa file, dSYMs + Xcode archive) on my home internet connection. And all that happened while I've already been ~33 minutes into my next tasks*. 🚀🚀🚀&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Yes, such a release &lt;em&gt;lane&lt;/em&gt; (as fastlane calls its user-made scripts) doesn't come bundled with your copy of fastlane and you'd have to implement it yourself, but most of the time it's just a matter of using built-in fastlane actions with an occasional Ruby scripting**. Surely a doable thing and definitely worth the effort as it pays off quickly.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;For what it's worth, I published a &lt;a href=&quot;https://github.com/ayoy/fastlane-plugin-dropbox&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;fastlane Dropbox plugin&lt;/a&gt; that you might find useful if Dropbox is part of your release flow (or any other flow you'd want to automate for that matter).&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;[*] – &lt;strong&gt;Pro tip:&lt;/strong&gt; it's a good idea to have another copy of your repository for creating AppStore builds (and for other automated tasks in general). This way you're not blocking your working copy of the repo with fastlane run, so you can freely switch branches, etc.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;[**] – If you're allergic to Ruby, you can also write your Fastfiles in Swift! &lt;a href=&quot;http://shashikantjagtap.net/first-impressions-fastlane-swift-ios/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Read here&lt;/a&gt; for more info.&lt;/p&gt;
&lt;h2 style=&quot;text-align:justify;&quot;&gt;Preparing beta builds&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;There are significantly fewer steps to complete in order to create a beta build for HockeyApp – and these are a subset of the AppStore &lt;em&gt;lane&lt;/em&gt;, so with minimal modifications, we got that case covered too. And even though we used to have a custom-made script for preparing beta builds, being able to replace it with fastlane was very relieving, as it's one thing less to care about.&lt;/p&gt;
&lt;h1 style=&quot;text-align:justify;&quot;&gt;Automated unit testing on CI&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The elementary task for continuous integration is building the app and running unit tests. With fastlane, it's as easy as calling its &lt;code&gt;scan&lt;/code&gt; action, e.g. like this:&lt;/p&gt;
&lt;p&gt;&lt;pre&gt;&lt;code class=&quot;Ruby&quot;&gt;lane :unit_tests do
    scan(scheme: &amp;quot;Example&amp;quot;)
end
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;It cleans the project, builds the app for testing and runs tests suite. It's widely configurable, so if you're new to fastlane, take a look at &lt;a href=&quot;https://docs.fastlane.tools/actions/scan/#automating-the-whole-process&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;the documentation&lt;/a&gt; for detailed info on &lt;code&gt;scan&lt;/code&gt;. For our case, provided that we prepend it with submodule update and pods install, we'd be good to go, only having to direct Jenkins to fastlane's output files.&lt;/p&gt;
&lt;h2 style=&quot;text-align:justify;&quot;&gt;Tracking build duration and warning count&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;We were setting up fastlane for Jenkins just at the time of our &lt;a href=&quot;/2018/01/02/tech-debt-at-base-warnings/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;warnings crusade&lt;/a&gt;, so we wanted to keep clear visibility into what's the current warning count and whether pull requests introduce any new warnings. We also gave it a shot with measuring CI build time – although completely unrelated to the local build time (our iOS CI runs on Mac minis that are OH SO SLOW), and prone to significant variations (dependent on the current load of the given Jenkins slave), it could still give some value to us.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Since &lt;code&gt;scan&lt;/code&gt; can't build without running tests, and that's what we needed for measuring build time, we had to use a lower level  &lt;code&gt;xcodebuild&lt;/code&gt; action. The flow is as follows:&lt;/p&gt;
&lt;ol style=&quot;text-align:justify;&quot;&gt;
&lt;li&gt;update submodules&lt;/li&gt;
&lt;li&gt;install pods&lt;/li&gt;
&lt;li&gt;clean the project using &lt;code&gt;xcodebuild&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;record current timestamp:&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;pre&gt;&lt;code class=&quot;Ruby&quot;&gt;build_duration = Time.now
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;ol style=&quot;text-align:justify;&quot; start=&quot;5&quot;&gt;
&lt;li&gt;build the project for testing using &lt;code&gt;xcodebuild&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;store build duration using the recorded timestamp:&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;pre&gt;&lt;code class=&quot;Ruby&quot;&gt;build_duration = Time.now - build_duration
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;ol style=&quot;text-align:justify;&quot; start=&quot;7&quot;&gt;
&lt;li&gt;extract warning count from the log file:&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;pre&gt;&lt;code class=&quot;Ruby&quot;&gt;warning_count = File.foreach(build_output_path)
                  .grep(/: warning:/).uniq.count
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;ol style=&quot;text-align:justify;&quot; start=&quot;8&quot;&gt;
&lt;li&gt;extract build number and version from &lt;code&gt;Info.plist&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;submit data to InfluxDB:&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;pre&gt;&lt;code class=&quot;Ruby&quot;&gt;influx(
    tags: {
        branch: git_branch,
        build_number: current_build_number,
        version: current_version_name
    },
    values: {
        build_duration: build_duration,
        warning_count: warning_count
    }
)
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;em&gt;Influx tags can be used later on to filter presented values.&lt;/em&gt;&lt;/p&gt;
&lt;ol style=&quot;text-align:justify;&quot; start=&quot;10&quot;&gt;
&lt;li&gt;eventually&lt;strong&gt;, run tests :)&lt;/strong&gt; using &lt;code&gt;scan&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;generate code coverage report using &lt;code&gt;slather&lt;/code&gt;, but only on the master branch, because it takes a significant amount of time and we don't want to do it for every update to every pull request.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 style=&quot;text-align:justify;&quot;&gt;Visualizing data&lt;/h2&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The data stored in InfluxDB is retrieved by Grafana to be visualized as graphs and/or tables:&lt;/p&gt;
&lt;p&gt;[caption id=&quot;attachment_379&quot; align=&quot;aligncenter&quot; width=&quot;1392&quot;]&lt;img class=&quot;alignnone size-full wp-image-379&quot; src=&quot;/assets/screen-shot-2018-01-08-at-09-10-23.png&quot; alt=&quot;Screen Shot 2018-01-08 at 09.10.23&quot;/&gt; This must be called a &lt;em&gt;locally consistent&lt;/em&gt; data.[/caption]&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;As you can see, the build duration may differ wildly on the CI server, even for two builds done next to each other. But one could argue that after neglecting the spikes you can observe some kind of a trend line, and it doesn't even have to go down to indicate our build time optimization effort, because:&lt;/p&gt;
&lt;ul style=&quot;text-align:justify;&quot;&gt;
&lt;li&gt;we set up Grafana only close to the end of the build time optimization effort,&lt;/li&gt;
&lt;li&gt;we have been adding more code to master branch all the time, so it must have required more time to compile.&lt;/li&gt;
&lt;/ul&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;On the other hand, the warning count graph does the job perfectly:&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;img class=&quot; size-full wp-image-380 aligncenter&quot; src=&quot;/assets/screen-shot-2018-01-08-at-09-02-52.png&quot; alt=&quot;Screen Shot 2018-01-08 at 09.02.52&quot;/&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;You can see the build warnings count on master constantly getting lower, and the rapid slope around 20th of November is the merge of &lt;code&gt;UILocalNotification&lt;/code&gt; rewrite – our last huge build health offender.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Also, notice the heart to the left of the graph title. It signifies that the alerting is enabled for this graph. It's configured so that whenever a data point with warning count greater than the current master occurs, it posts a notification to our HipChat room:&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;img class=&quot;aligncenter size-full wp-image-381&quot; src=&quot;/assets/screen-shot-2017-11-22-at-21-03-38-e1515446324678.png&quot; alt=&quot;Screen Shot 2017-11-22 at 21.03.38&quot;/&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;This way the right person could be blamed instantly ;)&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Our whole Grafana dashboard is still a work in progress. Although it's pretty messy without additional filtering and with 2-month time span, you can easily filter by time range, branch, user or version to get the insights you're interested in:&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;&lt;a href=&quot;https://kapustacc.files.wordpress.com/2018/01/screen-shot-2018-01-08-at-23-36-51.png&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img class=&quot;alignnone size-full wp-image-384&quot; src=&quot;/assets/screen-shot-2018-01-08-at-23-36-51.png&quot; alt=&quot;Screen Shot 2018-01-08 at 23.36.51&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;As a bonus, at the very bottom, there's a graph of the duration of local Xcode development build done by developers while working (when in the office, because our InfluxDB server is inaccessible from the outside). It uses custom Build Phases of Xcode project to mark the beginning of a build and compute the duration, and then reports the time alongside some metadata to InfluxDB.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;The goal here was to get an idea on how long the average build (no matter clean or incremental) takes, and well, the number is &lt;em&gt;close to 30 seconds&lt;/em&gt;, but I'm not quite sure what to do with it now. It's not actionable because the value is acceptable, given the big amount of code. I'd probably try and rework the script to calculate only clean build times.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;We also kind of hoped to get the proof that new MacBook Pro compiles significantly faster and so the whole team needs an upgrade, but it doesn't seem to be the case this time ;)&lt;/p&gt;
&lt;h1 style=&quot;text-align:justify;&quot;&gt;Recap&lt;/h1&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Automating common tasks around our codebase was the last remaining task of dealing with increased tech debt. During last two months, we resolved almost 400 warnings, reduced build time by half, started saving some significant amount of work time by offloading common tasks to the appropriate tools and gained much better visibility and control of our codebase health. The work is not by any means finished, but it has actually only started. However, after such a solid initial effort, following up will be much easier.&lt;/p&gt;
&lt;p style=&quot;text-align:justify;&quot;&gt;Thank you for reading through, and I hope it wasn't a total waste of time!&lt;/p&gt;

&lt;div id=&quot;disqus_thread&quot;&gt;&lt;/div&gt;
&lt;script&gt;

/**
*  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
*  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/

var disqus_config = function () {
this.page.url = 'https://ayoy.github.io';  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = 'kapusta-cc'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};

(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://kapusta-cc.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
&lt;/script&gt;
&lt;noscript&gt;Please enable JavaScript to view the &lt;a href=&quot;https://disqus.com/?ref_noscript&quot;&gt;comments powered by Disqus.&lt;/a&gt;&lt;/noscript&gt;</content><author><name>Dominik Kapusta</name></author><category term="iOS" /><category term="automation" /><category term="base" /><category term="build time" /><category term="fastlane" /><category term="grafana" /><category term="influxdb" /><category term="jenkins" /><category term="monitoring" /><category term="tech debt" /><category term="xcode" /><summary type="html">Base iOS client version 3.5.2 is a build number 2259. Base 2.3 from January 2013 was a build number 270. Roughly 2000 builds in 5 years means roughly 8 builds every week, and if you won't optimize it to be a streamlined task, it can become a painful experience.</summary></entry></feed>