<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>pinetimepro on JF&#39;s dev blog</title>
    <link>https://codingfield.com/tags/pinetimepro/</link>
    <description>Recent content in pinetimepro on JF&#39;s dev blog</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <copyright>@2021 Codingfield</copyright>
    <lastBuildDate>Tue, 24 Nov 2026 19:25:00 +0100</lastBuildDate>
    
        <atom:link href="https://codingfield.com/tags/pinetimepro/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>AI on my bench: an agent-friendly embedded dev setup</title>
      <link>https://codingfield.com/blog/2026-11/building-an-ai-friendly-embedded-development-environment/</link>
      <pubDate>Tue, 24 Nov 2026 19:25:00 +0100</pubDate>
      
      <guid>https://codingfield.com/blog/2026-11/building-an-ai-friendly-embedded-development-environment/</guid>
      <description>&lt;h1 id=&#34;tldr&#34;&gt;TL;DR&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;I made my PineTimePro dev environment &amp;ldquo;AI-friendly&amp;rdquo;: agents can build, flash, debug, read logs, take screenshots and use the UI by themselves.&lt;/li&gt;
&lt;li&gt;They can now check their own work.&lt;/li&gt;
&lt;li&gt;This works surprisingly well, but it&amp;rsquo;s not magic: it took a lot of time to set up, and I still review everything.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&#34;introduction&#34;&gt;Introduction&lt;/h1&gt;
&lt;p&gt;I&amp;rsquo;ve been working on the bring-up of the &lt;a href=&#34;https://pine64.org/2026/03/28/pinetime_march_2026/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;PineTimePro&lt;/a&gt;
 for quite a while now, and I recently started working on the software that will be its foundation: the PineTimePro SDK, and &lt;a href=&#34;https://codeberg.org/InfiniTimeOrg/NeoTime&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;NeoTime&lt;/a&gt;
, the &lt;a href=&#34;https://github.com/InfiniTimeOrg/InfiniTime&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;InfiniTime&lt;/a&gt;
 for the PineTimePro.&lt;/p&gt;
&lt;p&gt;Bringing up new hardware is mostly experimenting: browse a huge SDK to find the parts that matter, write a driver, flash it, see what happens, test a new hypothesis, try again&amp;hellip; Even with docs and examples, it&amp;rsquo;s rarely possible to set all the registers correctly the first time. It takes a lot of time and energy, and I thought this is where AI could help: it can read a whole SDK in minutes, and it can run many of those experiments for me.&lt;/p&gt;
&lt;p&gt;But for that, the AI needs to be able to do what I do at my desk: build the firmware, flash it, see what happens on the watch, interact with it and check the results.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;This post is about that: how I set up my development environment and my projects so that both humans and AI can easily experiment with the hardware.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I use several coding agents, some hosted and paid, some self-hosted. I won&amp;rsquo;t name them, because this setup doesn&amp;rsquo;t depend on any of them: the goal is to show the process, not to advertise tools. To be clear, I&amp;rsquo;m not into AI for the hype. I see those agents the same way I see my logic analyzer : tools that help me work faster, and it&amp;rsquo;s up to me to decide when and how to use them.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;d like to take this opportunity to thank TL Lim, founder of &lt;a href=&#34;https://pine64.org&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Pine64&lt;/a&gt;
 and &lt;a href=&#34;https://pine64.com&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;PineStore&lt;/a&gt;
, for sponsoring a generous LLM subscription for my work on the PineTimePro!&lt;/p&gt;
&lt;h1 id=&#34;the-hardware-setup&#34;&gt;The hardware setup&lt;/h1&gt;
&lt;p&gt;Let&amp;rsquo;s start with the hardware setup. In addition to my desktop computer and the watch itself, I use:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a J-Link probe (generously donated by TL Lim as well!) to flash and debug the code running on the hardware&lt;/li&gt;
&lt;li&gt;a USB-serial adapter for the debug serial port&lt;/li&gt;
&lt;li&gt;an &lt;a href=&#34;https://en.alientek.com/Product_Details/45.html&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;ALIENTEK DP100&lt;/a&gt;
 lab power supply, which can be controlled from the computer over USB&lt;/li&gt;
&lt;li&gt;an &lt;a href=&#34;https://www.nordicsemi.com/Products/Development-hardware/Power-Profiler-Kit-2&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;nRF PPK2&lt;/a&gt;
 power profiler&lt;/li&gt;
&lt;li&gt;a &lt;a href=&#34;https://www.saleae.com/logic&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Saleae Logic 8&lt;/a&gt;
 logic analyzer&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With those tools, I can flash and debug the code, check the power usage, probe the signals (I²C, SPI, etc.) on the PCB, and read the logs from the serial port. That&amp;rsquo;s basically everything I need to build great firmware for the PineTimePro!&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;setup.jpg&#34;
        alt=&#34;My dev setup: the PineTimePro, the J-Link probe, the USB-serial adapter, the lab power supply, the power profiler and the logic analyzer&#34;/&gt;&lt;/p&gt;
&lt;p&gt;Without those tools, I would be coding blind: I couldn&amp;rsquo;t see the code running on the hardware, debug it, diagnose communication issues or read logs.&lt;/p&gt;
&lt;p&gt;And the same is true for the agents: without any additional &amp;ldquo;tools&amp;rdquo;, all they can do is write code from a prompt, the existing source code, documentation and online searches. And then, you, the developer, still need to flash, debug, diagnose, probe, measure and provide all that data to the agent so that it can fix or improve the code accordingly.&lt;/p&gt;
&lt;p&gt;But what if the agent could use the J-Link probe and GDB to flash and debug? What if it could use the serial port, get data from the power measurement tool and drive the power supply?&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s exactly what we&amp;rsquo;ll explore in the next sections.&lt;/p&gt;
&lt;h1 id=&#34;step-1-experiments-with-ai-or-how-can-an-agent-help-me&#34;&gt;Step 1: Experiments with AI, or how can an agent help me?&lt;/h1&gt;
&lt;p&gt;While experimenting with the new PineTimePro hardware and writing the basic infrastructure for NeoTime, I noticed that the &amp;ldquo;AI-assisted&amp;rdquo; workflow was:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Let&amp;rsquo;s write a plan to implement this feature / experiment with this driver / figure out how that part of the SDK works&lt;/li&gt;
&lt;li&gt;The agent writes the plan, I review it (and often remove a lot of garbage and unneeded stuff)&lt;/li&gt;
&lt;li&gt;When the plan is ready, I let the agent do its work&lt;/li&gt;
&lt;li&gt;And when the agent is done, in addition to reviewing the code extensively, I build the firmware, run it and test it. If anything goes wrong (build issue, runtime issue, crash, feature not working as expected), I either fix it myself or report back to the agent.&lt;/li&gt;
&lt;li&gt;&amp;hellip; rinse and repeat until it works the way I want.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This workflow can be quite frustrating: the agent announces with a very high degree of confidence that it&amp;rsquo;s finished, but the project does not even build or run properly.&lt;/p&gt;
&lt;p&gt;So the idea was simple: give the agent a way to check its own work so that it can loop until the project builds and runs as expected on the hardware.&lt;/p&gt;
&lt;p&gt;The first tool the agent needs is a way to &lt;strong&gt;build&lt;/strong&gt; the project. This one is quite easy since many harnesses already provide a way for the agent to run commands on the computer. This allows the agent to read the documentation of the project and run &lt;code&gt;cmake&lt;/code&gt; and &lt;code&gt;make&lt;/code&gt; to build it and check for any errors.&lt;/p&gt;
&lt;p&gt;When working on desktop applications, the agent can also use the command line to run and debug the application. But in this case, the application does not run on the local computer. Instead, it runs on external hardware that is connected to the computer with an SWD probe and a serial port.&lt;/p&gt;
&lt;p&gt;Just around that time, I learnt about a new feature in &lt;em&gt;CLion 2026.1&lt;/em&gt;: &lt;a href=&#34;https://www.jetbrains.com/help/clion/agentic-debugging.html&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;agentic debugging&lt;/a&gt;
. CLion basically exposes the IDE&amp;rsquo;s &lt;strong&gt;debugger&lt;/strong&gt; through an MCP server, which allows any agent to discover and use it.&lt;/p&gt;
&lt;p&gt;And it worked&amp;hellip; with smart enough models. Self-hosted models that are small enough to run on my computer would sometimes make silly mistakes in tool calls or hit an error and try to work around it in very unexpected ways, but bigger models would work quite well (and even better with a skill, more on that in the next section).&lt;/p&gt;
&lt;p&gt;Of course, the agent could also use GDB and OpenOCD directly from the command line, but using the CLion tools allows the agent to use exactly the same tools as me. I can see what it is doing, I can see the breakpoints in the code, I can see the debugger output. There is exactly one place where the whole interaction with the hardware happens, so I can always follow what the agent is doing and jump in whenever I want.&lt;/p&gt;
&lt;p&gt;Building and debugging already provide a lot of feedback to the agent, but we can do more! The PineTimePro is equipped with a &lt;strong&gt;serial port&lt;/strong&gt; that can be used to output debugging logs. Why not instruct the agent to use that communication channel as well? The agent can read the logs and also add new debugging logs when trying to figure out how things work at runtime!&lt;/p&gt;
&lt;p&gt;When working on NeoTime, I also use my eyes (to check what&amp;rsquo;s displayed on the screen) and hands (to interact with the UI). How can I give eyes and hands to the agent?&lt;/p&gt;
&lt;p&gt;Most models have &lt;strong&gt;vision&lt;/strong&gt; capabilities: they can analyze a picture and describe what&amp;rsquo;s in it. And it turns out that taking a &amp;ldquo;screenshot&amp;rdquo; in the current implementation of the graphics driver is quite easy: the whole frame buffer sits in RAM. This allows the agent to read that frame buffer and convert it to a PNG file using a small Python script.&lt;/p&gt;
&lt;p&gt;Last but not least: &lt;strong&gt;user inputs&lt;/strong&gt;. The agent needs to be able to interact with the UI like a human can. I don&amp;rsquo;t want to build a whole robot to allow the agent to actually tap and swipe on the touch panel. Instead, I instructed it to implement a small &amp;ldquo;debug console&amp;rdquo; that allows it to send commands via the serial port. These commands are then received by the firmware which executes the corresponding action, like swiping and tapping on the display or pressing a button.&lt;/p&gt;
&lt;p&gt;Those were all ad hoc experiments: I just chatted with the agent, asked it if it had any way to build, flash, debug, read the serial port, get a screenshot,&amp;hellip; There was some back and forth, and some trial and error, and the agent eventually figured out how to do all of those tasks. But in the next session, a new agent would start from scratch and figure everything out again. That&amp;rsquo;s where skills come in.&lt;/p&gt;
&lt;h1 id=&#34;step-2-skills&#34;&gt;Step 2: Skills&lt;/h1&gt;
&lt;p&gt;Now that I knew agents were able to run those tasks, I wanted to make sure they could run them over and over again:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;without making new mistakes (LLMs are very good at inventing new ways to mess things up),&lt;/li&gt;
&lt;li&gt;without having to rediscover everything and re-invent the wheel,&lt;/li&gt;
&lt;li&gt;and even with smaller models.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Skills are the perfect way to reach that goal. A skill is basically a text file (usually written in Markdown) that describes how to execute a specific task. It contains everything the agent needs to know to execute that task as reliably as possible.&lt;/p&gt;
&lt;p&gt;You can write those skills yourself or&amp;hellip; make the agent write them for you. Here&amp;rsquo;s how I did it:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Make the agent succeed at the task.&lt;/li&gt;
&lt;li&gt;Ask it to write the corresponding skill.&lt;/li&gt;
&lt;li&gt;Start a new session, with a fresh context, and ask the agent to follow that skill.&lt;/li&gt;
&lt;li&gt;If something goes wrong, ask the agent to fix the skill so that it doesn&amp;rsquo;t run into that issue next time, and go back to step 3.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I ended up with skills that work but&amp;hellip; I don&amp;rsquo;t like the wording. When you read them, you immediately notice that they were written by an LLM. I&amp;rsquo;ll definitely review those skills before publishing them!&lt;/p&gt;
&lt;p&gt;I also wrote a small &lt;code&gt;AGENTS.md&lt;/code&gt; file that describes the project to the agent:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;short description of the project&lt;/li&gt;
&lt;li&gt;project layout&lt;/li&gt;
&lt;li&gt;list of skills&lt;/li&gt;
&lt;li&gt;rules that are specific to my setup (for example: build the project with the CLion tools, not manually with cmake/make)&lt;/li&gt;
&lt;li&gt;rules about how I want the agent to behave (for example: if an unexpected error happens, don&amp;rsquo;t try to work around it: stop and report back to me)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This file is injected into the context of every new session, so it&amp;rsquo;s important to keep it short (so it doesn&amp;rsquo;t bloat the context), but with enough information that the agent doesn&amp;rsquo;t need to rediscover the project each time a new session is started.&lt;/p&gt;
&lt;p&gt;The skills and the &lt;code&gt;AGENTS.md&lt;/code&gt; file made those tasks much more reliable, even with smaller (self-hosted) LLMs!&lt;/p&gt;
&lt;h1 id=&#34;step-3-see-it-in-action&#34;&gt;Step 3: See it in action&lt;/h1&gt;
&lt;p&gt;It honestly felt like magic the first time I asked the agent to make a small change in the code (make this button red), build it, flash it, run it and take a screenshot as proof! I could see everything happening in real time in my IDE, in the serial console and on the watch. And the agent eventually showed me the screenshot, with the button now drawn in red.&lt;/p&gt;
&lt;p&gt;For this post, I recorded a similar demo. In a single prompt, I asked the agent to build, flash and run the firmware, stop on two breakpoints and show some local variables, use the debug console to switch to the analog watch face, and finally take a screenshot of it:&lt;/p&gt;
&lt;figure class=&#34;video&#34;&gt;
  &lt;video controls playsinline preload=&#34;metadata&#34; poster=&#34;/blog/2026-11/building-an-ai-friendly-embedded-development-environment/neotime-ai-flash-debug-screenshot.jpg&#34; style=&#34;display:block; max-width:100%; height:auto;&#34;&gt;
    &lt;source src=&#34;https://codingfield.com/blog/2026-11/building-an-ai-friendly-embedded-development-environment/neotime-ai-flash-debug-screenshot.mp4&#34; type=&#34;video/mp4&#34;&gt;
    Your browser can&#39;t play this video. &lt;a href=&#34;https://codingfield.com/blog/2026-11/building-an-ai-friendly-embedded-development-environment/neotime-ai-flash-debug-screenshot.mp4&#34;&gt;Download it&lt;/a&gt; instead.
  &lt;/video&gt;
  &lt;figcaption&gt;The agent builds and flashes the firmware, stops on breakpoints, switches watch faces through the debug console and takes a screenshot, while I watch in CLion and on the watch&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Remember the frustrating loop, where I had to build, flash and test everything myself? Here, the agent does all of it on its own, using every tool we set up in the previous steps: build, debugger, serial port, user inputs and vision.&lt;/p&gt;
&lt;h1 id=&#34;step-4-do-some-actual-work&#34;&gt;Step 4: Do some actual work&lt;/h1&gt;
&lt;p&gt;Now, does it work for actual development tasks? Oh yes, it does!&lt;/p&gt;
&lt;p&gt;One of the first things I tried with this setup was to&amp;hellip; make the agent run Doom on the watch. I did not expect much out of it, and my prompt was very short, something along the lines of &amp;ldquo;This is the firmware for a smartwatch. It implements most of the low-level drivers (display, button, touch panel, audio). I want you to port Doom to it. The first step will only implement the video, we&amp;rsquo;ll add user inputs and audio afterward&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;And to my surprise, it worked! If I remember correctly, the very first try was not functional, but after a couple more messages, I could admire Doom running pretty well on my PineTimePro. And within a few more turns, the agent implemented the audio and the user inputs.&lt;/p&gt;
&lt;p&gt;While the agent was working, I could see it editing the code, flashing and running it, reading the logs and taking screenshots. And it only stopped once it had checked that the code worked as expected and that Doom was actually running.&lt;/p&gt;
&lt;figure class=&#34;video&#34;&gt;
  &lt;video controls playsinline preload=&#34;metadata&#34; poster=&#34;/blog/2026-11/building-an-ai-friendly-embedded-development-environment/doom.jpg&#34; style=&#34;display:block; width:100%; max-width:min(100%, 360px); height:auto;&#34;&gt;
    &lt;source src=&#34;https://codingfield.com/blog/2026-11/building-an-ai-friendly-embedded-development-environment/doom.mp4&#34; type=&#34;video/mp4&#34;&gt;
    Your browser can&#39;t play this video. &lt;a href=&#34;https://codingfield.com/blog/2026-11/building-an-ai-friendly-embedded-development-environment/doom.mp4&#34;&gt;Download it&lt;/a&gt; instead.
  &lt;/video&gt;
  &lt;figcaption&gt;Doom running on the PineTimePro&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Afterward, I used those same skills to do more serious work like planning and implementing a more complete debug console, analyzing the performance of the video pipeline and testing a few optimization ideas, and adding unit tests to the code.&lt;/p&gt;
&lt;p&gt;Each time, the agent could check its own work on the real hardware. It worked surprisingly well&amp;hellip; but it was not all magic.&lt;/p&gt;
&lt;h1 id=&#34;caveats-and-lessons-learned&#34;&gt;Caveats and lessons learned&lt;/h1&gt;
&lt;p&gt;Reading this article, you might be tempted to think that building this setup was fast and easy.&lt;/p&gt;
&lt;p&gt;But you would be wrong! All of this took way more time than I expected and was often quite frustrating. The agent can be very useful at times (I would have spent hours porting Doom, it did it in a few minutes) but also very stubborn. For example, even though I told the agent to &amp;ldquo;stop and report back to me in case of unexpected errors&amp;rdquo;, it would often try to work around issues it couldn&amp;rsquo;t fix (like the J-Link probe crashing and needing a full power cycle to come back to life).&lt;/p&gt;
&lt;p&gt;Then, when a skill seemed good, I would use it in real tasks, and the agent would fail once again. And in that case, I needed to stop what I was doing and figure out what went wrong.&lt;/p&gt;
&lt;p&gt;And even when this setup worked fine, it&amp;rsquo;s still not a silver bullet that makes AI-generated code ready to be pushed to the repo. I, as the human developer, still need to review the changes, make sure the original design is respected, that the code looks exactly how I want it, and that the agent hasn&amp;rsquo;t been overzealous and added unnecessary things &amp;ldquo;just in case&amp;rdquo; or for &amp;ldquo;future-proofing&amp;rdquo;. That review often takes way longer than writing the code took the agent.&lt;/p&gt;
&lt;h1 id=&#34;conclusion-and-whats-next&#34;&gt;Conclusion and what&amp;rsquo;s next&lt;/h1&gt;
&lt;p&gt;I&amp;rsquo;m very impressed by what this environment made possible. I can now delegate boring tasks (like adding debug code, trying multiple ideas to reach a goal, rapid prototyping) that would have taken me hours to do by myself.&lt;/p&gt;
&lt;p&gt;Even if the project is now &amp;ldquo;AI-friendly&amp;rdquo;, I want it to stay &amp;ldquo;human first&amp;rdquo;. Most of the things I added are usable by a human developer as well: for example, the scripts the agent uses to take a screenshot or read the serial port can also be run from the command line. To be honest, I would have built most of these tools anyway. Every step the agent executes can also easily be done by a human. The goal of this setup is to have AI agents assist human developers, not replace them.&lt;/p&gt;
&lt;p&gt;The key takeaway of this experiment is that an agent that can check its own work stops being a code generator and becomes an experimenter. That feedback loop is what took the agent from &amp;ldquo;sounding very confident that it was finished&amp;rdquo; to actually getting things done.&lt;/p&gt;
&lt;p&gt;And there is still a lot to add. The next step is to give the agent access to the rest of my bench: drive the power supply (to power cycle the watch when the probe crashes, for example), read the measurements from the power profiler, and maybe even use the logic analyzer. I also want to try the new &lt;a href=&#34;https://blog.jetbrains.com/clion/2026/09/hard-fault-debugging/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;hard fault analyzer in CLion&lt;/a&gt;
.&lt;/p&gt;
&lt;p&gt;All in all, it was a great learning experience, and I&amp;rsquo;m sure it&amp;rsquo;s just the beginning!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>A Week with OpenClaw: My Experience as a Tech Tinkerer</title>
      <link>https://codingfield.com/blog/2026-02/a-week-with-openclaw-my-experience-as-a-tech-tinkerer/</link>
      <pubDate>Mon, 23 Feb 2026 10:00:00 +0200</pubDate>
      
      <guid>https://codingfield.com/blog/2026-02/a-week-with-openclaw-my-experience-as-a-tech-tinkerer/</guid>
      <description>&lt;h1 id=&#34;tldr&#34;&gt;TL;DR&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;https://openclaw.ai/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;OpenClaw&lt;/a&gt;
 is promising but not ready for mainstream use yet. It requires technical know-how, has security considerations, and needs constant babysitting—the agents hallucinate, break configs, and don&amp;rsquo;t always report back.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The real value was &lt;strong&gt;learning, not automating&lt;/strong&gt;. I didn&amp;rsquo;t find killer use cases, but I gained hands-on experience with LLMs, agents, and the current limitations of the tech.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;We&amp;rsquo;re in the early days. Expensive, clunky, unclear what it&amp;rsquo;s good for—but the direction is interesting. Local models might make this practical eventually. For now, it&amp;rsquo;s a fascinating experiment for tinkerers, not a must-have tool.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&#34;introduction&#34;&gt;Introduction&lt;/h1&gt;
&lt;p&gt;After returning from FOSDEM, I was bombarded with hype around &lt;a href=&#34;https://openclaw.ai/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;OpenClaw&lt;/a&gt;
—an open‑source, self‑hosted AI assistant that promises to glue large language models (LLMs) to your personal tools. As an embedded‑software engineer who lives on Linux, loves FOSS &amp;amp; maintains FOSS projects, I was both skeptical and curious. Skeptical because, honestly, I&amp;rsquo;m not a &lt;em&gt;pro AI&lt;/em&gt; guy: in my opinion, most &lt;em&gt;AI&lt;/em&gt; tools are garbage, and LLMs like ChatGPT or Claude are environmental and ethical disasters. But also curious because &lt;em&gt;AI&lt;/em&gt; is here to stay, and as a software engineer, I need to stay relevant. And since I&amp;rsquo;m all‑in on self‑hosting, I had to try this allegedly revolutionary tool. Here&amp;rsquo;s what a week of hands‑on testing taught me.&lt;/p&gt;
&lt;h1 id=&#34;what-is-openclaw-anyway-&#34;&gt;What is OpenClaw anyway ?&lt;/h1&gt;
&lt;p&gt;OpenClaw is an open-source, self-hosted AI assistant that connects to LLMs and your personal tools.&lt;/p&gt;
&lt;p&gt;What does that exactly mean? Before jumping into this, I barely knew what an AI agent is, but I understood that OpenClaw is a tool that allows to run an AI-thing on any of my computer/SBC, talk to it via Telegram and give it access to all your files, network, applications to automate anything you want.&lt;/p&gt;
&lt;p&gt;I was not so interested at first, I guess my brain learnt to ignore all those useless AI news. But the more I read and talked about it with some friends, the more curious I became about it. And since it&amp;rsquo;s open-source, easily self-hostable and &amp;ldquo;free&amp;rdquo;, I decided to install it and check it out!&lt;/p&gt;
&lt;h1 id=&#34;the-setup&#34;&gt;The setup&lt;/h1&gt;
&lt;p&gt;I first installed it in a VM on my laptop, but quickly move it to an SBC that I could leave on 24/7. I used my OrangePi5+, since it&amp;rsquo;s the only unused SBC I had laying around. The installation on Fedora for ARM64 went well, just copy/paste the &lt;em&gt;curl &amp;amp; bash&lt;/em&gt; command from the  &lt;a href=&#34;https://docs.openclaw.ai/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;documentation&lt;/a&gt;
 and follow the instructions. I won&amp;rsquo;t detail the whole installation process since it&amp;rsquo;s quite straightforward, and there are tons of tutorial available on the internet.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;./orangepi5plus.jpg&#34;
        alt=&#34;The OrangePi5&amp;#43; running my OpenClaw instance&#34;/&gt;&lt;/p&gt;
&lt;p&gt;The OrangePi5+ is over-powered for OpenClaw, and I&amp;rsquo;ll probably move it to a &lt;a href=&#34;https://pine64.org/devices/quartz64_model_a/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Quartz64 Model A 8GB from Pine64&lt;/a&gt;
 in the near future. The Quartz64 is an amazing board that sips very little power and doesn&amp;rsquo;t heat up at all, even without any heat spreader!&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s time for a small disclaimer: OpenClaw is known to have many security issues, which could be problematic if you give your agent free rein on your local machine, network, alone accounts,&amp;hellip; Please make sure you know what you&amp;rsquo;re doing before setting your instance up!&lt;/p&gt;
&lt;p&gt;In my case, OpenClaw is running on a dedicated machine, and, except for the API keys for the LLMs, I won&amp;rsquo;t provide it any of my credential nor access to any other machines on my network!&lt;/p&gt;
&lt;h1 id=&#34;first-attempt-the-free-tier-struggle&#34;&gt;First Attempt: The Free Tier Struggle&lt;/h1&gt;
&lt;p&gt;At first, I didn&amp;rsquo;t want to pay for an LLM subscription since I just wanted to test the waters, see what was being the hype and do some experiments.&lt;/p&gt;
&lt;p&gt;I first tried with the free tier of &lt;a href=&#34;https://chat.qwen.ai/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Qwen&lt;/a&gt;
 it worked&amp;hellip; at the beginning, but I quickly hit limits of the free tier.&lt;/p&gt;
&lt;p&gt;I also tried using free models at &lt;a href=&#34;https://openrouter.ai/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;OpenRouter&lt;/a&gt;
, but that didn&amp;rsquo;t work at all. I guess the free tier is way too limited for agent use.&lt;/p&gt;
&lt;h1 id=&#34;lets-spend-a-few-bucks-for-glm-47&#34;&gt;Let&amp;rsquo;s spend a few bucks for GLM-4.7&lt;/h1&gt;
&lt;p&gt;*&lt;em&gt;You get what you pay for&lt;/em&gt;, as they say, so I decided to spend a few €€€ on a subscription. I opted with the &lt;a href=&#34;https://z.ai/subscribe&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;GLM Coding Lite plan from z.ai&lt;/a&gt;
 since GLM4.7 was often mentioned as a cheaper alternative to other big LLMs (like ChatGPT or Claude, for example).&lt;/p&gt;
&lt;p&gt;All of a sudden, things went much more smoothly: I could talk with the agent, ask it to setup the Telegram channel, test a few prompts, and experiment with &lt;em&gt;AI&lt;/em&gt;!&lt;/p&gt;
&lt;p&gt;The first thing I did was asking the agent to setup the Telegram channel, which went very well.&lt;/p&gt;
&lt;p&gt;Then I asked it to do whatever necessary so that I can access the web UI of OpenClaw from my local network, since by default, it&amp;rsquo;s only available from the computer it&amp;rsquo;s running on. Aaaaan&amp;hellip; I watched the agent edit the configuration of OpenClaw, restart it, say that it was done (but it wasn&amp;rsquo;t working), try again, break the configuration so that OpenClaw would not start again,&amp;hellip; It was a bit frustrating, but also quite funny&amp;hellip; it was trying but had no idea what it was doing! I eventually stopped that and reverted the configuration since, according to the documentation, OpenClaw is not designed to work that way, and you should use a SSH or Tailscale tunnel to access your OpenClaw instance from other machines.&lt;/p&gt;
&lt;p&gt;Until now, that&amp;rsquo;s not very impressive, I just watched the agent configure itself with a very low success ratio.&lt;/p&gt;
&lt;p&gt;But then I asked it to connect to my &lt;a href=&#34;https://www.home-assistant.io/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Home Assistant&lt;/a&gt;
 instance. It told me how to create the &lt;em&gt;Long-term access token&lt;/em&gt; it needed to access the instance, and then manage answer requests like &amp;ldquo;what&amp;rsquo;s the outdoor temperature&amp;rdquo; and &amp;ldquo;switch that light on/off&amp;rdquo;:&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;./openclaw-ha1.png&#34;
        alt=&#34;Tell me what&amp;amp;rsquo;s the outdoor temperature and the temperature from the lounge. Then switch the backyard light on and switch it off after 2 minutes&#34;/&gt;&lt;/p&gt;
&lt;p&gt;And it did exactly that:&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;./openclaw-ha2.png&#34;
        alt/&gt;&lt;/p&gt;
&lt;p&gt;I didn&amp;rsquo;t have to do any coding, or provide any additional info or anything else, it figured everything out by itself, which is quite amazing to me!&lt;/p&gt;
&lt;p&gt;I played with it for 3 days, quite intensively in the evening. Then, the 3rd day, I hit a new rate limit: the weekly quota. With Z.ai, you pay for a monthly subscription (and probably a limited amount of tokens per months), but you also get rate-limited by 5 hours and by weeks! And when you reach the weekly quota&amp;hellip; you have to wait for the end of the week before being able to use your subscription again. Shame, but&amp;hellip; &lt;em&gt;you get what you pay for&lt;/em&gt;, right?&lt;/p&gt;
&lt;h1 id=&#34;i-want-more-the-openrouter-experiment&#34;&gt;I want more! The OpenRouter experiment&lt;/h1&gt;
&lt;p&gt;Since the GLM subscription was cheap, I decided to burn 10 additional € to &lt;a href=&#34;https://openrouter.ai/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;OpenRouter&lt;/a&gt;
. OpenRouter is a service which allows you to use many models from many providers from a single subscription (in exchange of a small fee).&lt;/p&gt;
&lt;p&gt;When you buy at least 10€, you get access to all their paid models, obviously, but also to free ones (that are most likely rate limited as well). And yes, it worked.&lt;/p&gt;
&lt;!--  --&gt;
&lt;p&gt;First I tried their  &lt;code&gt;openrouter/auto&lt;/code&gt; &amp;ldquo;model&amp;rdquo;. This is not an actual model, but it automatically analyzes the prompt and chooses the best model to execute it.&lt;/p&gt;
&lt;p&gt;Once again, that was not a good idea! It routed too many requests to the new Claude Opus 4.6 which costs a lot! A few requests cost more than 3€ out of my bucket of 10!&lt;/p&gt;
&lt;p&gt;So I quickly decided to try other models and mostly used &lt;code&gt;MiniMax M2.5&lt;/code&gt; which has a lot of good feedback on &lt;a href=&#34;https://www.reddit.com/r/openclaw/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Reddit&lt;/a&gt;
. I also eventually tried Kimi K2.5, Trinity Large Preview (free) and Step 3.5 (free).&lt;/p&gt;
&lt;p&gt;And I did more experiments: make them use my searxng instance for web search, set up cron jobs for small automations, read my &lt;em&gt;Logseq&lt;/em&gt; notes, install new skills, write some code, and even generate a whole blog for the agent.&lt;/p&gt;
&lt;!--  --&gt;
&lt;p&gt;While all of this worked, it was not as easy as many posts on reddit and videos on youtube would have you believe: it hallucinates fixes for errors it creates itself, it restarts the gateway and does not report back when the gateway is running again, it loops on parts of the discussion even when I asked it to stop talking about that, failed to build a small test application (but the prompt was probably too short),&amp;hellip; It basically needs a lot of hand holding and monitoring.&lt;/p&gt;
&lt;h1 id=&#34;what-about-local-models&#34;&gt;What about local models?&lt;/h1&gt;
&lt;p&gt;I do not have a huge AI rig at home. And even if my desktop can run some models on its 3060ti and its 64GB RAM, I do not want to let it run 24/7.&lt;/p&gt;
&lt;p&gt;But the OrangePi5+ it&amp;rsquo;s running on has 32GB RAM sooo&amp;hellip; why not try to run llama.cpp and a model that fits into the memory?&lt;/p&gt;
&lt;p&gt;I think that would be nice for simple automations, cron jobs running during the night, when the speed is not an issue.&lt;/p&gt;
&lt;p&gt;I tried &lt;em&gt;Qwen3-30B&lt;/em&gt; and &lt;em&gt;GPT-OSS-20B&lt;/em&gt;. They run very slowly (1-3 token/second) but they run and fit in memory!&lt;/p&gt;
&lt;p&gt;Buuut that didn&amp;rsquo;t integrate well with OpenClaw: it would automatically fall back to paid subscriptions or just silently timeout. Request would stay unanswered, with no error or status whatsoever.&lt;/p&gt;
&lt;h1 id=&#34;ok-but-what-is-it-for&#34;&gt;Ok, but what is it for?&lt;/h1&gt;
&lt;p&gt;I did all of these experiments over the course of a week, mainly during the evenings. I had a lot of fun watching the agent doing stuff by itself, sometimes failing miserably, sometimes doing things I did not expect.&lt;/p&gt;
&lt;p&gt;All in all, it&amp;rsquo;s not ready to be used by the general public: you need to know what you are doing, be aware of security risks, pay attention to subscription cost, know that the agents hallucinate,&amp;hellip;&lt;/p&gt;
&lt;p&gt;And I failed at finding actual useful use-cases that would make my life simpler.&lt;/p&gt;
&lt;p&gt;Yes, I can use it as  an AI chat, but I can use any other platform for that, and I already pay for &lt;a href=&#34;https://lumo.proton.me&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Proton Lumo&lt;/a&gt;
 for that use-case.&lt;/p&gt;
&lt;p&gt;I could also use it to help with coding, but I&amp;rsquo;m not sure that would be as good as Codex or JetBrains AI, for example. And, to be honest, I barely use them.&lt;/p&gt;
&lt;p&gt;It turns out that I cannot think of any workflow or process that I would like to automate and that I cannot automate with Home Assistant or a simple script. Maybe I&amp;rsquo;ll find some in the future, as I learn how to use the technology and as this technology evolves day by day.&lt;/p&gt;
&lt;p&gt;But does that mean that this experiment is a failure? Absolutely &lt;strong&gt;not&lt;/strong&gt;! I learnt &lt;strong&gt;a lot&lt;/strong&gt; about AI, models, providers and agents these last few days, a lot more that I learnt these last few months by using Github Copilot at work for example.&lt;/p&gt;
&lt;p&gt;And even if I do not particularly like the AI hype, I think I have to stay updated to new technology to stay relevant in my job, I think I have to learn to use those tools before they eventually replace me (if that ever happens). And for that, this experiment was a big win!&lt;/p&gt;
&lt;h1 id=&#34;whats-next&#34;&gt;What&amp;rsquo;s next?&lt;/h1&gt;
&lt;p&gt;I feel like I&amp;rsquo;ve just scratched the surface here. The more I play with it, the more I want to try new things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Try other models, maybe try ChatGPT and see if a bigger well-known model performs better/faster&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Try to better integrate my Logseq notes with &lt;a href=&#34;https://github.com/tobi/qmd&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;QMD&lt;/a&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Try multiple agents talking to each other and sub-agents&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Try more skills and plugins&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Try alternatives like &lt;a href=&#34;https://github.com/sipeed/picoclaw&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;PicoClaw&lt;/a&gt;
 that is much more lightweight&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I&amp;rsquo;ll keep my instance running, continue playing with it from time to time, especially when I&amp;rsquo;m too tired to do anything else meaningful, and I&amp;rsquo;ll stay up to date on the evolution of this new technology and see how it goes!&lt;/p&gt;
&lt;h1 id=&#34;and-beyond&#34;&gt;And beyond?&lt;/h1&gt;
&lt;p&gt;During this experiment, I felt like I (and all other people using OpenClaw right now) am a pioneer testing brand new technology. I don&amp;rsquo;t know much about it, I don&amp;rsquo;t know how to use it, but I feel like it&amp;rsquo;ll be part of our life in the future.&lt;/p&gt;
&lt;p&gt;I think of it like the early days of the internet when internet access was expensive (you paid to the minute), slow, not reliable and not secure. And most of the people didn&amp;rsquo;t know what to do on the internet. Then came the ADSL connections. Less and less expensive, but with limited quota. And now, internet is a commodity, most of the people subscribe to their internet provider as if they would subscribe to their electricity company, for example.&lt;/p&gt;
&lt;p&gt;The same might happen to this &lt;em&gt;AI&lt;/em&gt; technology. We are just discovering what it can do, but still have to find use-cases that will actually make our lives easier. It cost a lot of money and most people do not want to spend money on it.&lt;/p&gt;
&lt;p&gt;But the technology progresses rapidly. It might become cheaper in the future? Or we might be able to run the LLM locally, on a simple computer or SBC ? Who knows?&lt;/p&gt;
&lt;p&gt;I know the current state of the technology is not bright. But that doesn&amp;rsquo;t mean that it cannot improve in the future, right?&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Running OpenWRT on the Pine64 LoRa Gateway</title>
      <link>https://codingfield.com/blog/2024-10/running-openwrt-on-the-pine64-lora-gatewat/</link>
      <pubDate>Sun, 13 Oct 2024 18:00:00 +0200</pubDate>
      
      <guid>https://codingfield.com/blog/2024-10/running-openwrt-on-the-pine64-lora-gatewat/</guid>
      <description>&lt;p&gt;&lt;a href=&#34;https://pine64.org&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Pine64&lt;/a&gt;
 offers a &lt;a href=&#34;https://pine64.org/documentation/Pinedio/Gateway/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;LoRa gateway&lt;/a&gt;
 built around their &lt;a href=&#34;https://pine64.org/devices/pine_a64-lts/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Pine A64-LTS&lt;/a&gt;
 SBC.&lt;/p&gt;
&lt;p&gt;I was fortunate enough to receive an engineering sample from Pine64, allowing me to test the gateway and provide a custom OS image before it became available for purchase. RTP (&lt;a href=&#34;https://fosstodon.org/@RTP&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;@RTP&lt;/a&gt;
 on Fosstodon ) later created a custom image based on Armbian, which worked well.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ve been running one of RTP&amp;rsquo;s early image for a few years now. It&amp;rsquo;s connected on the &lt;a href=&#34;https://www.thethingsnetwork.org/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;TTN (The Things Network)&lt;/a&gt;
, and I use it to get the data from my &lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part1/&#34;&gt;LoRa water meter&lt;/a&gt;
.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m now planning on buying a better antenna and install the gateway in a sealed enclosure to mount it on the outdoor wall of my garage. I would like this setup to be a &amp;ldquo;install and forget&amp;rdquo; setup : it should just run with very minimal maintenance.&lt;/p&gt;
&lt;p&gt;The downside of my current setup based on Armbian is the SD card : it &lt;strong&gt;will&lt;/strong&gt; fail at some point.&lt;/p&gt;
&lt;p&gt;So my idea was to use &lt;a href=&#34;https://openwrt.org/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;OpenWRT&lt;/a&gt;
 instead of Armbian to run the gateway. OpenWRT is very lightweight and run from RAM, which makes it a very good fit for this use-case.&lt;/p&gt;
&lt;p&gt;This should be definitely possible to achieve since there are a few LoRa gateways from 3rd party providers that run on OpenWRT.&lt;/p&gt;
&lt;p&gt;OpenWRT also provides a few LoRa related packages on their repo(&lt;a href=&#34;https://openwrt.org/packages/pkgdata/libloragw&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;libloragw&lt;/a&gt;
, &lt;a href=&#34;https://openwrt.org/packages/pkgdata/libloragw-utils&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;libloragw-utils&lt;/a&gt;
, &lt;a href=&#34;https://openwrt.org/packages/pkgdata/libloragw-tests&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;libloragw-tests&lt;/a&gt;
).&lt;/p&gt;
&lt;p&gt;I thought it should be an easy project but&amp;hellip; I eventually didn&amp;rsquo;t manage to make it run. Since I&amp;rsquo;ll probably not have the time to continue the project in the near future, I decided to write this post with all the notes I took during my experiments. This way, someone might be able to continue the project, or I&amp;rsquo;ll at least remember what I did when I&amp;rsquo;ll find the time to continue it later on!&lt;/p&gt;
&lt;h2 id=&#34;gateway-documentation&#34;&gt;Gateway documentation&lt;/h2&gt;
&lt;p&gt;The documentation related to the LoRa gateway is available on the &lt;a href=&#34;https://pine64.org/documentation/Pinedio/_full/#gateway&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Pine64 community website&lt;/a&gt;
. The most important part is the pin mapping between the board PI-2 connector and the RAK5146 LoRa module.&lt;/p&gt;
&lt;h2 id=&#34;openwrt-image-and-dts&#34;&gt;OpenWRT image and DTS&lt;/h2&gt;
&lt;p&gt;The support for the Pine A64-LTS board is provided by the &lt;a href=&#34;https://openwrt.org/toh/hwdata/pine64/pine64_sopine_a64&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;SOPINE&lt;/a&gt;
 image. However, the DTS file in this default image does not enable the &lt;strong&gt;spidev&lt;/strong&gt; driver for the SPI0 bus, that is needed to drive the LoRa module. So we&amp;rsquo;ll need to build our own version of OpenWRT to enable it (and probably make a few other changes).&lt;/p&gt;
&lt;p&gt;To do so, I followed the &lt;a href=&#34;https://github.com/openwrt/openwrt?tab=readme-ov-file#development&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;documentation&lt;/a&gt;
 of the project to download and build OpenWRT &lt;a href=&#34;https://github.com/openwrt/openwrt/releases/tag/v23.05.5&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;v23.05.0&lt;/a&gt;
.&lt;/p&gt;
&lt;p&gt;To modify the DTS, I simply edited the file &lt;code&gt;&amp;lt;git folder&amp;gt;/openwrt/build_dir/target-aarch64_cortex-a53_musl/linux-sunxi_cortexa53/linux-5.15.134/arch/arm64/boot/dts/allwinner/sun50i-a64-sopine.dtsi&lt;/code&gt; so that the &lt;code&gt;spi0&lt;/code&gt; node looks like this :&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code class=&#34;language-dts&#34; data-lang=&#34;dts&#34;&gt;&amp;amp;spi0  {
	status = &amp;#34;okay&amp;#34;;

	spidev@0 {
		compatible = &amp;#34;semtech,sx1301&amp;#34;;
		status = &amp;#34;okay&amp;#34;;
		reg = &amp;lt;0&amp;gt;;
		spi-max-frequency = &amp;lt;8000000&amp;gt;;
	};
};
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;em&gt;Note that this is a hacky way to edit the sources of OpenWRT and those changes will be reverted as soon as OpenWRT will refresh the build directory. The correct way to do this involves creating a patch file and inserting it in the build system.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&#34;spidev-driver&#34;&gt;SPIDEV driver&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;spidev&lt;/code&gt; driver must also be added to the image. To do so, in the &lt;code&gt;make menuconfig&lt;/code&gt; interface, go to &lt;code&gt;Kernel modules --&amp;gt; SPI support&lt;/code&gt; and enable &lt;code&gt;kmod-spi-dev&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&#34;gpio&#34;&gt;GPIO&lt;/h2&gt;
&lt;p&gt;Most of the scripts from &lt;code&gt;RAK&lt;/code&gt; use the now deprecated &lt;a href=&#34;https://docs.kernel.org/next/admin-guide/gpio/sysfs.html&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;&lt;code&gt;sysfs&lt;/code&gt; api&lt;/a&gt;
 to set and clear the RESET pin of the LoRa module. This API is deprecated and does not seem to work  anymore on newer kernels (&amp;gt;6.6). Instead, you should use &lt;code&gt;gpioset&lt;/code&gt; to drive the GPIO pins. In this case, use &lt;code&gt;gpioset gpiochip2 71=0&lt;/code&gt; to reset the LoRa module and &lt;code&gt;gpioset gpiochip2 71=1&lt;/code&gt; to enable it again.&lt;/p&gt;
&lt;p&gt;The package &lt;code&gt;gpiod-tools&lt;/code&gt; must be enabled in the &lt;code&gt;make menuconfig&lt;/code&gt; interface for this tool to be available.&lt;/p&gt;
&lt;h2 id=&#34;testing-the-communication-with-the-lora-module&#34;&gt;Testing the communication with the LoRa module.&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;m stuck at this step : the &lt;code&gt;/dev/spidev0.0&lt;/code&gt; device driver is available and seems to work, but all the test programs (util_* and test_*) fail with errors.&lt;/p&gt;
&lt;p&gt;For example, here is the result of &lt;code&gt;test_loragw_reg&lt;/code&gt;, which is supposed to read the content of the registers of the LoRa module:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;root@OpenWrt:/# test_loragw_reg 
Beginning of test for loragw_reg.c
ERROR: CONCENTRATOR UNCONNECTED
IMPLICIT_PAYLOAD_LENGHT = 0 (should be 197)
FRAME_SYNCH_PEAK2_POS = 0 (should be 11)
PREAMBLE_SYMB1_NB = 0 (should be 49253)
ADJUST_MODEM_START_OFFSET_SF12_RDX4 = 0 (should be 3173)
IF_FREQ_1 = 0 (should be -1947)
End of test for loragw_reg.c
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;As you can see, it read all 0s when it was expecting other specific values.&lt;/p&gt;
&lt;p&gt;I checked with my logic analyzer : data is sent on the MOSI, CS and CLK pins, but the module never writes data on the MISO pin.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;pine64-lora-gw-logic-analyzer.jpg&#34;
        alt/&gt;&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;mosi-but-no-miso.jpg&#34;
        alt/&gt;&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s as far as I got. I don&amp;rsquo;t know how to go further and fix this issue. Note that this is not a hardware issue since this exact same board work fine on Armbian with the RAK tools running.&lt;/p&gt;
&lt;p&gt;If you have any idea about this or if you want to continue this project, feel free email me at jf[at]codingfield[dot]com or reach out to me on Matrix at @jf:matrix.codingfield.com.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Install Frigate in Proxmox on the OrangePi5&#43; (RK3588)</title>
      <link>https://codingfield.com/blog/2024-01/install-frigate-in-proxmox-on-orangepi5plus/</link>
      <pubDate>Wed, 24 Jan 2024 15:00:00 +0100</pubDate>
      
      <guid>https://codingfield.com/blog/2024-01/install-frigate-in-proxmox-on-orangepi5plus/</guid>
      <description>&lt;p&gt;I&amp;rsquo;ve already tried in the past to run some NVRs to monitor &lt;strong&gt;surveillance camera&lt;/strong&gt; on ARM SBC, but the performances were a bit underwhelming : it was too slow, it would miss events while it was recording the previous ones, and the CPU would be maxed out all of the time.&lt;/p&gt;
&lt;p&gt;But I reconsidered this with the &lt;strong&gt;RK3588&lt;/strong&gt; since it&amp;rsquo;s way more powerful than the other SBCs I had the opportunity to use so far.&lt;/p&gt;
&lt;p&gt;Since I&amp;rsquo;m using &lt;strong&gt;Proxmox&lt;/strong&gt; (see &lt;a href=&#34;https://codingfield.com/blog/2024-01/install-armbian-and-proxmox-on-orangepi5plus/&#34;&gt;this previous article where documented the installation of Armbian and Proxmox on the OrangePi5+&lt;/a&gt;
), I created a new container to run Frigate. First, I downloaded a new container template. I choose an &lt;strong&gt;Alpine&lt;/strong&gt; container because they are very lightweight.&lt;/p&gt;
&lt;p&gt;And then I created the container using this template. I assigned a virtual disk of 50GB, 8GB of RAM and 4 CPU cores.&lt;/p&gt;
&lt;p&gt;Once the container is created and started, installing Frigate &lt;a href=&#34;https://docs.frigate.video/frigate/installation#docker&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;using Docker&lt;/a&gt;
 is really easy :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Create a &lt;code&gt;docker-compose.yaml&lt;/code&gt; file. Here mine for reference:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code class=&#34;language-version:&#34; data-lang=&#34;version:&#34;&gt;services:
 frigate:
 container_name: frigate
 privileged: true # this may not be necessary for all setups
 restart: unless-stopped
 image: ghcr.io/blakeblackshear/frigate:stable
 shm_size: &amp;#34;64mb&amp;#34; # update for your cameras based on calculation above
 devices:
 - /dev/apex_0:/dev/apex_0 # passes a PCIe Coral, follow driver instructions here https://coral.ai/docs/m2/get-started/#2a-on-linux
 volumes:
 - /etc/localtime:/etc/localtime:ro
 - /root/frigate/config.yml:/config/config.yml
 - /root/frigate/storage:/media/frigate
 - /root/frigate/data:/data 
- type: tmpfs # Optional: 1GB of memory, reduces SSD/SD Card wear
 target: /tmp/cache
 tmpfs:
 size: 1000000000
 ports:
 - &amp;#34;5000:5000&amp;#34;
 - &amp;#34;8554:8554&amp;#34; # RTSP feeds
 - &amp;#34;8555:8555/tcp&amp;#34; # WebRTC over tcp
 - &amp;#34;8555:8555/udp&amp;#34; # WebRTC over udp
 environment:
 FRIGATE_RTSP_PASSWORD: &amp;#34;password&amp;#34; ```
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a configuration file for Frigate : &lt;code&gt;config.yml&lt;/code&gt;. Here&amp;rsquo;s what worked in my case:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code class=&#34;language-cameras:&#34; data-lang=&#34;cameras:&#34;&gt; My test camera: # &amp;lt;------ Name the camera
 ffmpeg:
 inputs:
 - path: rtsp://user::pass@192.168.1.201 # &amp;lt;----- The stream you want to use for detection
 roles:
 - detect
 - record
 detect:
 enabled: True # &amp;lt;---- disable detection until you have a working camera feed
 width: 1920 # &amp;lt;---- update for your camera&amp;#39;s resolution
 height: 1080 # &amp;lt;---- update for your camera&amp;#39;s resolution
 record:
 enabled: true
 snapshots:
 enabled: True
objects:
 track:
 - person
 - cat
 - dog
 - bird ```
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Start the container : &lt;code&gt;docker-compose up -d&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And that&amp;rsquo;s it : it just works! The most difficult part was to find the actual URL, user and password of my random cheap WiFi camera from Aliexpress&amp;hellip;&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;./frigate1.png&#34;
        alt/&gt;&lt;/p&gt;
&lt;p&gt;Using this configuration, Frigate is using a &lt;strong&gt;CPU implementation for objects detection&lt;/strong&gt;. This is fine on the RK3588, but we can do better : using a &lt;strong&gt;TPU&lt;/strong&gt; to run the AI object detection model and reduce the load on the CPU&lt;/p&gt;
&lt;h2 id=&#34;coral-tpu&#34;&gt;Coral TPU&lt;/h2&gt;
&lt;p&gt;Frigate actually recommands using such a TPU since they provide more performance and reduce the latency. The &lt;a href=&#34;https://docs.frigate.video/frigate/hardware#google-coral-tpu&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;most recommanded TPU&lt;/a&gt;
 is the &lt;a href=&#34;https://www.coral.ai/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Google Coral TPU&lt;/a&gt;
 which is very well integrated in Frigate. It&amp;rsquo;s however not easy to source it at a reasonnable price, unfortunately. I eventually found it at a not so reasonnable price on Mouser.com.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;coral_1.jpg&#34;
        alt/&gt;&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;coral_2.jpg&#34;
        alt/&gt;&lt;/p&gt;
&lt;p&gt;The OrangePi5+ supports 2 M.2 ports :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;1 M.2 Key-M slot that supports NVMe SSD&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;1 M.2 Key-E slot that supports the WiFi adapter.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In theory, the &lt;a href=&#34;https://www.coral.ai/products/m2-accelerator-dual-edgetpu&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Coral TPU (M.2 Accelerator with Dual Edge TPU)&lt;/a&gt;
 should be compatible with the &lt;strong&gt;KEY-E slot&lt;/strong&gt;, but at that point, I had no idea if it would actually work on ARM64 Linux and through Proxmox.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;TL;DR: it works! &amp;hellip; but not out-of-the-box. And only one of the 2 TPU is actually detected.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;So far, the Linux kernel detects that a PCIe device is connected on the M.2 slot, but it doesn&amp;rsquo;t have any &lt;strong&gt;driver&lt;/strong&gt; to support this device.&lt;/p&gt;
&lt;p&gt;Fortunately, &lt;strong&gt;Google provides the sources of their driver&lt;/strong&gt; that we can build for our kernel. A bit scary if you&amp;rsquo;re not used to build your own kernel. But, as I said earlier, &lt;strong&gt;Armbian&lt;/strong&gt; allows us to &lt;em&gt;easily&lt;/em&gt; build our own version of Armbian with our own patches. We&amp;rsquo;ll use this feature to build the Coral TPU driver into the Armbian kernel.&lt;/p&gt;
&lt;h3 id=&#34;build-armbian-with-the-driver-for-the-coral-tpu&#34;&gt;Build Armbian with the driver for the Coral TPU&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;Please note that this is the 1st time that I do this kind of thing and that I&amp;rsquo;m absolutely not an expert when it comes to buidling a kernel and using Armbian build script. Feel free to let me know if there&amp;rsquo;s a better way to do this!&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The &lt;a href=&#34;https://github.com/armbian/build&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Armbian Linux Build Framework&lt;/a&gt;
 is the tool that is used by the Armbian team to automitcally build images for so many computers and SBCs. The good thing here is that they make this tool available to all users and developers so they can customize their own build of Armbian.&lt;/p&gt;
&lt;p&gt;Using this tool, you can for example add support for a board that is currently not supported, or customize the build for an already supported board : install another kernel, edit the kernel configuration, install other packages in the image are some examples of customizations the build tool makes possible.&lt;/p&gt;
&lt;p&gt;In order to install the driver for the Coral TPU, we&amp;rsquo;ll&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;clone&lt;/strong&gt; the git repo&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;add a few &lt;em&gt;user&lt;/em&gt; &lt;strong&gt;patches&lt;/strong&gt; that contains the Linux driver&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;customize the &lt;strong&gt;kernel configuration&lt;/strong&gt; file to enable this driver&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;build the new &lt;strong&gt;kernel&lt;/strong&gt; together with a whole new image that integrates this kernel.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&amp;rsquo;s get to it and start by cloning the repo:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;git clone --depth&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;m&#34;&gt;1&lt;/span&gt; --branch&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;main https://github.com/armbian/build
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This will download the framework into a folder named &lt;code&gt;build&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;At this point, you can launch the build tool by just running &lt;code&gt;./compile.sh&lt;/code&gt;. This will open a pseudo graphical UI that will guide you through different configuration steps before actually building an image according to your settings.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;./armbian-build1.png&#34;
        alt/&gt;&lt;/p&gt;
&lt;p&gt;As an example, you can build an image similar to &lt;a href=&#34;https://armbian.hosthatch.com/dl/orangepi5-plus/archive/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;the ones that are provided by Armbian&lt;/a&gt;
 by using the following settings:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Kernel configuration&lt;/strong&gt; : Do not change the kernel configuration&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Board&lt;/strong&gt; : orangepi5-plus&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Kernel&lt;/strong&gt; : edge &lt;em&gt;(This is the mainline kernel, while the legacy one is based on the Rockchip BSP and on kernel 5.15)&lt;/em&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Release package base&lt;/strong&gt; : bookworm&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Image type&lt;/strong&gt; : Image with console interface (server)&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So now, let&amp;rsquo;s &lt;strong&gt;customize&lt;/strong&gt; the build environment to &lt;strong&gt;add the Coral TPU driver&lt;/strong&gt;! This procedure is based on the &lt;a href=&#34;https://coral.ai/docs/m2/get-started#2a-on-linux&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;installation procedure from Google&lt;/a&gt;
 For this, I created 3 patch files&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;./gasket-driver.patch&#34;&gt;gasket-driver.patch&lt;/a&gt;
. It basically contains the &lt;a href=&#34;https://github.com/google/gasket-driver&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;driver from Google&lt;/a&gt;
 converted into a patch file&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;./gasket-makefile.patch&#34;&gt;gasket-makefile.patch&lt;/a&gt;
 which makes sure the driver will be compiled&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;./gasket-kconfig&#34;&gt;gasket-kconfig&lt;/a&gt;
 which adds the driver in the list of options that you can enable and disable at build time&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Those files must be copied into to the &lt;code&gt;userpatches/kernel/rockchip-rk3588-edge&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We also need to provide a kernel configuration file that activates the driver:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;copy the original configuration file into the &lt;code&gt;userpatches&lt;/code&gt; folder:&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;cp config/kernel/linux-rockchip-rk3588-edge.config userpatches/
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;Enable the driver:&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;append &lt;span class=&#34;s2&#34;&gt;&amp;#34;CONFIG_STAGING_GASKET_FRAMEWORK=y&amp;#34;&lt;/span&gt; &amp;gt; userpatches/linux-rockchip-rk3588-edge.config
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;append &lt;span class=&#34;s2&#34;&gt;&amp;#34;CONFIG_STAGING_APEX_DRIVER=y&amp;#34;&lt;/span&gt; &amp;gt; userpatches/linux-rockchip-rk3588-edge.config
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then run &lt;code&gt;./compile.sh&lt;/code&gt; again. Now, the build framework will automatically apply the patches and the custom kernel configuration from the &lt;code&gt;userpatches&lt;/code&gt; folder. When the build is finished, you&amp;rsquo;ll find a brand new &lt;strong&gt;image&lt;/strong&gt; in &lt;code&gt;output/images&lt;/code&gt; which you can install on your board. If, like me, you don&amp;rsquo;t want to start once again from scratch, you can simply copy the kernel &lt;strong&gt;packages&lt;/strong&gt; (&lt;code&gt;output/debs/linux-headers-edge-rockchip-rk3588_24.2.0[...].deb&lt;/code&gt; and &lt;code&gt;output/debs/linux-image-edge-rockchip-rk3588_24.2.0[...].deb&lt;/code&gt;) to your OrangePi5+ board and install them.&lt;/p&gt;
&lt;p&gt;After a reboot, the Coral TPU should be &lt;strong&gt;detected&lt;/strong&gt; by the Linux kernel. You can check that with the following commands:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-shell&#34; data-lang=&#34;shell&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;$ lspci -nn &lt;span class=&#34;p&#34;&gt;|&lt;/span&gt; grep 089a
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;0002:21:00.0 System peripheral &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;0880&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;: Global Unichip Corp. Coral Edge TPU &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;1ac1:089a&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;$ ls /dev/apex_0
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;/dev/apex_0
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;using-the-coral-tpu-from-an-lxc-container&#34;&gt;Using the Coral TPU from an LXC container&lt;/h3&gt;
&lt;p&gt;So now, the &lt;strong&gt;TPU&lt;/strong&gt; is detected on the host system, but I would like to use it from the LXC container I created at the beginning of this post. Passing the device through to a container is much easier than to a VM since containers basically share the same kernel with the host. So it&amp;rsquo;s just a matter of configuring the container correctly.&lt;/p&gt;
&lt;p&gt;First, the container must be a &lt;strong&gt;privileged&lt;/strong&gt; one. I don&amp;rsquo;t exactly know what that means, but the container won&amp;rsquo;t be able to access to the device if it&amp;rsquo;s not privileded. This setting must be specified when creating the container.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;./proxmox4.png&#34;
        alt/&gt;&lt;/p&gt;
&lt;p&gt;Then you&amp;rsquo;ll need to edit the configuration file of the container manually. The configuration file is located in &lt;code&gt;/etc/pve/nodes/&amp;lt;NODENAME&amp;gt;/&amp;lt;CONTAINER&amp;gt;.conf&lt;/code&gt;. For example, the name for my container &lt;em&gt;101&lt;/em&gt; on my node named &lt;em&gt;orangepi5-plus&lt;/em&gt; is &lt;code&gt;/etc/pve/nodes/orangepi5-plus/lxc/101.conf&lt;/code&gt;. Edit this file and add this at the end&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;lxc.cgroup2.devices.allow: c 120:* rwm
lxc.cgroup2.devices.allow: a
lxc.cap.drop:
lxc.mount.auto: cgroup:rw
lxc.mount.entry: /dev/apex_0 dev/apex_0 none bind,optional,create=file 0, 0
lxc.apparmor.profile: unconfinednd:
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now, start the container and edit the configuration file of Frigate (&lt;code&gt;config.yml&lt;/code&gt;)  and add this at the end of the file : it specifies to Frigate that it should use the Coral TPU to run the detection algorithm.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;detectors:
  coral:
    type: edgetpu
    device: pci
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Save the file, and restart Frigate. The following lines in the logs will confirm that the TPU is detected and used by Frigate:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;2024-01-20 11:13:39.434363436  [2024-01-20 11:13:39] frigate.detectors.plugins.edgetpu_tfl INFO    : Attempting to load TPU as pci
2024-01-20 11:13:39.505662609  [2024-01-20 11:13:39] frigate.detectors.plugins.edgetpu_tfl INFO    : TPU found
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can also check this in the web UI, in the System panel:&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;./frigate2.png&#34;
        alt/&gt;&lt;/p&gt;
&lt;p&gt;And that&amp;rsquo;s it! I&amp;rsquo;m currently using a single camera, and CPU usage is very low. I&amp;rsquo;ll probably buy a couple of better quality cameras and install them around my house!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Install Armbian and Proxmox on the OrangePi5&#43; (RK3588)</title>
      <link>https://codingfield.com/blog/2024-01/install-armbian-and-proxmox-on-orangepi5plus/</link>
      <pubDate>Wed, 24 Jan 2024 14:00:00 +0100</pubDate>
      
      <guid>https://codingfield.com/blog/2024-01/install-armbian-and-proxmox-on-orangepi5plus/</guid>
      <description>&lt;h2 id=&#34;introduction&#34;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;ve been using ARM based SBCs (RaspberryPi, Odroid, Quartz64, NanoPi) as home servers for quite some time now. I use them to host my NAS, backups, Home-Assistant, MQTT broker, Matrix home server&amp;hellip;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ARM&lt;/strong&gt; CPUs are becoming more and more powerful, support more and more memory and provide more and more I/Os, which enable new use-cases and functionalities.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;Rockchip RK3588&lt;/strong&gt; is probably one of the most powerful of such CPU that are available to the general public. It&amp;rsquo;s a strong chip that hosts 8 AACH64 cores, supports up to 32GB of RAM and bundles quite a lot of I/O and peripherals : GPU, TPU, PCIe, GBe, USB2/3, SATA,&amp;hellip; Everything needed to build a strong desktop computer, a virtualization server, or many other projects.&lt;/p&gt;
&lt;p&gt;I have already done  &lt;a href=&#34;https://codingfield.com/blog/2023-02/quartzpro64-in-february-2023/&#34;&gt;some experiments with the RK3588 using the QuartzPro64&lt;/a&gt;
 board from &lt;a href=&#34;https://pine64.org&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Pine64&lt;/a&gt;
. The &lt;strong&gt;mainlining&lt;/strong&gt; efforts keep on progressing and most of the RK3588 is now supported on mainline Linux. PCIe, USB, network interfaces, thermal management, frequency scaling, SATA and many other parts of the CPU are now supported. Even the HDMI output is working! I think the last important missing bit is the GPU, but I understand that some work is already in progress on that topic!&lt;/p&gt;
&lt;p&gt;Using an RK3588 board without GPU as a &lt;strong&gt;desktop computer&lt;/strong&gt; works but&amp;hellip; it&amp;rsquo;s not great : animations are glitchy, playing a video uses a lot of CPU resources and it feels sluggish, even on such a powerful CPU. But for many other purposes, the GPU is not needed.&lt;/p&gt;
&lt;p&gt;In this series of post, I&amp;rsquo;ll explore using an RK3588 SBC to run multiple &lt;strong&gt;containers&lt;/strong&gt; and &lt;strong&gt;VMs&lt;/strong&gt; on &lt;strong&gt;Proxmox&lt;/strong&gt;. My end goal consists in running &lt;a href=&#34;https://frigate.video/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Frigate&lt;/a&gt;
 (open source local NVR that uses AI for object detection and that integrates nicely with Home-Asssitant) and &lt;a href=&#34;https://forgejo.org/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Forgejo&lt;/a&gt;
 and &lt;a href=&#34;https://woodpecker-ci.org/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Woodpecker&lt;/a&gt;
 to build a local CI/CD pipeline for my own software developments&lt;/p&gt;
&lt;p&gt;The QuartzPro64 is a really nice board, but it&amp;rsquo;s not really convenient when it comes to mount it in a case and keep it running 24/7. So I decided to buy another board : the &lt;a href=&#34;http://www.orangepi.org/html/hardWare/computerAndMicrocontrollers/details/Orange-Pi-5-plus-32GB.html&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;OrangePi 5+ with 32GB of RAM memory&lt;/a&gt;
. I gifted it to myself for Christmas. I chose the bundle with the aluminum case, the 256GB eMMC module and the WiFi card.&lt;/p&gt;
&lt;p&gt;/TODO Add pictures of the board and the case&lt;/p&gt;
&lt;p&gt;In this series of posts, I&amp;rsquo;ll document&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;How  I setup Armbian and Proxmox on the board (this post)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;https://codingfield.com/blog/2024-01/install-frigate-in-proxmox-on-orangepi5plus/&#34;&gt;The installation of Frigate NVR with a Coral TPU to improve the performance of the detection&lt;/a&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;hardware-setup&#34;&gt;Hardware setup&lt;/h2&gt;
&lt;p&gt;The aluminum case looks pretty good and acts as a heat spreader. It exposes a USB-C port, 2 USB3 ports, the power button, the IR receiver, the audio port and the micro SD slot.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;case_front_2.jpg&#34;
        alt/&gt;&lt;/p&gt;
&lt;p&gt;The back exposes 2 USB2 ports, 2 HDMI outs and 1 HDMI in, 2 RJ45 (2.5G LAN), a USB-C port (for power) and a slot for display/camera flat cables. The case is also equipped with 2 WiFi antennas.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;case_back_1.jpg&#34;
        alt/&gt;&lt;/p&gt;
&lt;p&gt;The case opens by removing the bottom plate (there are 4 screws). The SSD NVMe slot is easily accessible without having to remove the board from the case.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;case_bottom_open_1.jpg&#34;
        alt/&gt;&lt;/p&gt;
&lt;p&gt;When the board is removed from the case, you can easily see the aluminum piece that allows the heat from the memory chips and CPU to be dissipated through the case (with a heat pad in-between). I also used Kapton tape to protect the WiFi antenna I do not use right now. The Coral TPU is already installed on the M.2 slot on the top left of the board.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;case_board_2.jpg&#34;
        alt/&gt;&lt;/p&gt;
&lt;p&gt;Close-up view of the Coral TPU installed in the M.2 slot:&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;coral_3.jpg&#34;
        alt/&gt;&lt;/p&gt;
&lt;p&gt;An overview of the hardware : the OrangePi5+ board, a 512GB NVMe SSD, the Coral TPU and the WiFi board (included with the board).&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;board_1.jpg&#34;
        alt/&gt;&lt;/p&gt;
&lt;p&gt;I did a few tests when I received the board : I installed the Debian images provided by OrangePi, checked that the HDMI outputs and the WiFi work, play a bit with desktop environment, play &lt;a href=&#34;https://tilvids.com/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;TILVids&lt;/a&gt;
 videos,&amp;hellip; Everything worked just fine.&lt;/p&gt;
&lt;p&gt;Then I installed the SSD and replaced the WiFi module with the Coral TPU and moved on with my virtualization project!&lt;/p&gt;
&lt;h2 id=&#34;setup&#34;&gt;Setup&lt;/h2&gt;
&lt;p&gt;I decided to use &lt;a href=&#34;https://www.armbian.com/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Armbian&lt;/a&gt;
 for this project. &lt;strong&gt;Armbian&lt;/strong&gt; supports multiple boards, the OrangePi5(+) included, and offers the possibility to easily customize and build your own version of the distribution, which will prove useful a bit later for this project!&lt;/p&gt;
&lt;p&gt;But before digging into building Armbian, I&amp;rsquo;ll simply download and install an image provided by the &lt;a href=&#34;https://armbian.hosthatch.com/dl/orangepi5-plus/archive/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Armbian team&lt;/a&gt;
. At the time of writing this post, &lt;code&gt;Armbian_23.11.1_Orangepi5-plus_bookworm_edge_6.7.0-rc1.img.xz&lt;/code&gt; is the most recent build available. It&amp;rsquo;s based on the &lt;strong&gt;mainline&lt;/strong&gt; kernel version 6.7.&lt;/p&gt;
&lt;p&gt;I first installed Armbian on the SD card, and then moved to the eMMC for better performance.&lt;/p&gt;
&lt;p&gt;Installing Armbian on the &lt;strong&gt;SD card&lt;/strong&gt; is very simple : download the image and flash on your SD card using the tool of your choice (&lt;code&gt;dd&lt;/code&gt;, &lt;code&gt;win32diskimager&lt;/code&gt;, &lt;code&gt;BalenaEtcher&lt;/code&gt;), insert the SD Card in your OrangePi5+ board, power it up and follow the installer wizard (you need a display and a keyboard for this 1st boot installation process).&lt;/p&gt;
&lt;p&gt;There are multiple ways to install Linux on the &lt;strong&gt;eMMC&lt;/strong&gt;, and I decided to use the procedure which consists in first installing Armbian on the SD card, and then downloading and flashing the image on the eMMC from this installation on the SD card and then rebooting without the SD card inserted. Not the fastest one, but the simplest procedure in my opinion.&lt;/p&gt;
&lt;p&gt;Now that Armbian is up and running on the board, let&amp;rsquo;s proceed and install Proxmox!&lt;/p&gt;
&lt;h2 id=&#34;proxmox&#34;&gt;Proxmox&lt;/h2&gt;
&lt;p&gt;&lt;a href=&#34;https://www.proxmox.com/en/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Proxmox&lt;/a&gt;
 is a FOSS &lt;strong&gt;virtual environment&lt;/strong&gt; for your servers. It provides a nice web UI that allows you to easily manage 1 or multiple servers, VMs, containers, networks,&amp;hellip; It&amp;rsquo;s really powerful and yet simple enough to be used by an hobbyist like me.&lt;/p&gt;
&lt;h3 id=&#34;installation&#34;&gt;Installation&lt;/h3&gt;
&lt;p&gt;The official release of Proxmox does not support ARM/AARCH64 architectures (yet?), unfortunately. However, &lt;a href=&#34;https://github.com/jiangcuo/Proxmox-PortProxmox&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;this project&lt;/a&gt;
 provides a port of Proxmox that runs on many ARM SBCs and, even if the OrangePi5+ is not listed on their README, it&amp;rsquo;s working really fine on my setup! This port is made available to any Debian based distribution like Armbian, so the &lt;a href=&#34;https://github.com/jiangcuo/Proxmox-Port/wiki/Install-Proxmox-VE-on-Debian-bookworm&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;installation procedure&lt;/a&gt;
 is very easy:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Ensure that your host has a static IP address, and edit your &lt;code&gt;/etc/hosts&lt;/code&gt; file accordingly&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add their repo into your system&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Update your system&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Install Proxmox and its dependancies using a simple &lt;code&gt;apt&lt;/code&gt; command.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img  src=&#34;./proxmox1.png&#34;
        alt/&gt;&lt;/p&gt;
&lt;p&gt;And that&amp;rsquo;s it : &lt;strong&gt;Proxmox is now running on your OrangePi5+&lt;/strong&gt;! Note that I followed exactly the same procedure on the &lt;strong&gt;QuartzPro64&lt;/strong&gt; and it worked exactly the same! I guess it should run fine on many AARCH64 boards.&lt;/p&gt;
&lt;p&gt;At this point, I did experiment a bit with &lt;strong&gt;containers&lt;/strong&gt; and &lt;strong&gt;VMs&lt;/strong&gt;. I was able to run &lt;strong&gt;Alpine&lt;/strong&gt;, &lt;strong&gt;Debian&lt;/strong&gt; and &lt;strong&gt;Centos&lt;/strong&gt; containers and a &lt;strong&gt;Debian&lt;/strong&gt; VM. Everything works fine : text console, graphical UI, networkd, storage,&amp;hellip;&lt;/p&gt;
&lt;h3 id=&#34;storage&#34;&gt;Storage&lt;/h3&gt;
&lt;p&gt;After the installation, a first &lt;code&gt;local&lt;/code&gt; storage is created from the OS disk (the eMMC flash memory in this case). By default, this storage is used by Proxmox to store the container templates, VM ISO, backups,&amp;hellip;&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;./proxmox5.png&#34;
        alt/&gt;&lt;/p&gt;
&lt;p&gt;I added a 2nd storage called &lt;code&gt;ssd&lt;/code&gt; to store the actual disks of the VMs and containers. This storage uses the NVMe drive I installed on the back of the board, and if configured as a LVM-Thin storage.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;./proxmox6.png&#34;
        alt/&gt;&lt;/p&gt;
&lt;h2 id=&#34;wrap-up&#34;&gt;Wrap up&lt;/h2&gt;
&lt;p&gt;I found the installation of Armbian and Proxmox surpringly easy, considering that the support of the RK3588 is still relatively new in mainline kernel.&lt;/p&gt;
&lt;p&gt;Since I installed Proxmox on the board, I had the opportunity to enjoy creating containers and VMs to do some experiments, like building a RISCV-toolchain for ARM64 CPU! The RK3588 has a lot of CPU power, and the 32GB of RAM give a lot of head room for many projects!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>DIY low-power LoRa water-meter - Part 5 : Integration with TTN, Node-RED and Home-Assistant</title>
      <link>https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part5/</link>
      <pubDate>Sat, 05 Aug 2023 16:00:00 +0100</pubDate>
      
      <guid>https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part5/</guid>
      <description>&lt;p&gt;This blog post is part of a series of posts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part1&#34;&gt;Part 1 : Introduction&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part2&#34;&gt;Part 2 : From prototypes to V1.0&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part3&#34;&gt;Part 3 : Hardware design&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part4&#34;&gt;Part 4 : Software&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part5&#34;&gt;Part 5 : Integration with TTN, Node-RED and Home-Assistant&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;p&gt;Now that the device is built and the firmware is running as expected, let’s see how to actually use it and get the measurements out of the sensor!&lt;/p&gt;
&lt;p&gt;As a reminder, the device counts pulses from the watermeter and sends the data (message number, battery voltage and number of pulses detected) to TTN using LoRaWAN technology.&lt;/p&gt;
&lt;p&gt;If you own your own LoRaWAN gateway, you should start by &lt;a href=&#34;https://www.thethingsindustries.com/docs/gateways/concepts/adding-gateways/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;registering it to the TTN network&lt;/a&gt;
.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;ttn-gw.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;p&gt;My gateway is the &lt;a href=&#34;https://pine64.com/product/pinedio-lora-indoor-gateway-kit/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Pine64 indoor gateway&lt;/a&gt;
. It’s currently sitting on my desk, and I’ve already installed it in my garage for some range testing. It works very well, and I know that Pine64 plans on releasing an outdoor version of this gateway, which would be awesome!&lt;/p&gt;
&lt;p&gt;Next, you’ll need to &lt;a href=&#34;https://www.thethingsindustries.com/docs/devices/adding-devices/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;register your end device on the TTN network&lt;/a&gt;
. During this process, you’ll be asked to provide a few parameters like the Device ID, keys, etc. Those info are hard-coded in the file &lt;code&gt;src/LoRaMac-node/src/peripherals/soft-se/se-identity.h&lt;/code&gt; . Don’t forget to edit this file with different values and to ensure that you provide consistent information to the TTN console.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;soft-se-settings.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;p&gt;Then, if you power up the device, messages should appear on the &lt;em&gt;Live Data&lt;/em&gt; panel of the TTN console:&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;ttn-live-data.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;p&gt;Great, the messages are correctly sent by the device and received by TTN.&lt;/p&gt;
&lt;p&gt;Now, we want to integrate those data into our custom application : Home-Assistant. Fortunately, TTN provides multiple integration functionalities.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;ttn-integrations.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;p&gt;For this project, I decided to use the MQTT integration, which I already know and which integrates well with home-assistant. With this integration, TTN publishes all messages received from the end device into a specific topic on their MQTT server.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;ttn-mqtt.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;p&gt;Now, all I need to do is make Home-Assistant connect to this MQTT server and process the data. The simplest way I found to do this is using the &lt;a href=&#34;https://nodered.org/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Node-RED&lt;/a&gt;
 &lt;a href=&#34;https://community.home-assistant.io/t/home-assistant-community-add-on-node-red/55023&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;integration&lt;/a&gt;
 in Home-Assisant. Node-RED allows the creation of nice automations using a graphical environment.&lt;/p&gt;
&lt;p&gt;Here is what I came up with:&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;nodered-flow.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;p&gt;This &lt;em&gt;flow&lt;/em&gt; diagram first connects to the TTN MQTT server and subscribe to the “global” topic &lt;code&gt;#&lt;/code&gt;. When a new message is receives (in JSON format), the payload is parsed and the 3 values are extracted. Finally, those processed values are published on… the internal MQTT server of my Home-Assistant instance. That’s probably not the most straightforward way to do this, but I couldn’t find any other way to make HA work with 2 MQTT servers…&lt;/p&gt;
&lt;p&gt;Then I added a new MQTT sensor by editing the file &lt;code&gt;configuration.yaml&lt;/code&gt; :&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nl&#34;&gt;mqtt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;nl&#34;&gt;sensor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;   &lt;span class=&#34;o&#34;&gt;-&lt;/span&gt; &lt;span class=&#34;nl&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;watermeter_usage&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;     &lt;span class=&#34;nl&#34;&gt;state_topic&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;/watermeter/water_value&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;     &lt;span class=&#34;nl&#34;&gt;unit_of_measurement&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;L&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;     &lt;span class=&#34;nl&#34;&gt;device_class&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;water&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;     &lt;span class=&#34;nl&#34;&gt;state_class&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;total_increasing&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;     &lt;span class=&#34;nl&#34;&gt;unique_id&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;watermetervalue&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;   &lt;span class=&#34;o&#34;&gt;-&lt;/span&gt; &lt;span class=&#34;nl&#34;&gt;name&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;watermeter_battery&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;     &lt;span class=&#34;nl&#34;&gt;state_topic&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;/watermeter/battery&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;     &lt;span class=&#34;nl&#34;&gt;unit_of_measurement&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;v&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;     &lt;span class=&#34;nl&#34;&gt;device_class&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;voltage&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;     &lt;span class=&#34;nl&#34;&gt;state_class&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;measurement&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;     &lt;span class=&#34;nl&#34;&gt;unique_id&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;watermeterbattery&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This adds 2 new sensors to my HA instance : &lt;code&gt;watermeter_usage&lt;/code&gt; which represents the number of pulses detected by the sensor and &lt;code&gt;watermeter_battery&lt;/code&gt; which is the battery voltage of the device.&lt;/p&gt;
&lt;p&gt;Now, all I have to do is adding this sensor to the Energy dashboard of home assistant :&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;ha.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;h1 id=&#34;wrap-up&#34;&gt;Wrap up&lt;/h1&gt;
&lt;p&gt;The general idea of this project is very simple : I just want to count the number of pulses generated by my water meter and process the data in my home automation application.&lt;/p&gt;
&lt;p&gt;But when you look at all the details, this project becomes way more interesting, especially for a software engineer who has somewhat limited experience with hardware and electronics. This project allowed me to learn, experiment and have fun with&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;multiple MCU families and architectures&lt;/li&gt;
&lt;li&gt;low-power embedded software programming&lt;/li&gt;
&lt;li&gt;LoRa, LoRaWAN and The Things Network (TTN) public network&lt;/li&gt;
&lt;li&gt;MQTT and home automation&lt;/li&gt;
&lt;li&gt;prototyping on breadboard&lt;/li&gt;
&lt;li&gt;PCB design and manufacturing&lt;/li&gt;
&lt;li&gt;assembly of the whole device in a waterproof case&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And, once it&amp;rsquo;ll be installed on my water meter, this project will eventually allow me to monitor my water usage. I&amp;rsquo;ll probably also connect it to my gas meter in the near future!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>DIY low-power LoRa water-meter - Part 4 : Software</title>
      <link>https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part4/</link>
      <pubDate>Sat, 05 Aug 2023 15:00:00 +0100</pubDate>
      
      <guid>https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part4/</guid>
      <description>&lt;p&gt;This blog post is part of a series of posts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part1&#34;&gt;Part 1 : Introduction&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part2&#34;&gt;Part 2 : From prototypes to V1.0&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part3&#34;&gt;Part 3 : Hardware design&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part4&#34;&gt;Part 4 : Software&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part5&#34;&gt;Part 5 : Integration with TTN, Node-RED and Home-Assistant&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h1 id=&#34;software&#34;&gt;Software&lt;/h1&gt;
&lt;p&gt;The code is fully open source and available on my &lt;a href=&#34;https://codeberg.org/JF002/xiao-lora-water-meter&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Codeberg account&lt;/a&gt;
.&lt;/p&gt;
&lt;p&gt;The firmware is written in C, and mostly relies on two 3rd party libraries : LoRaMac-node and LittleFS.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://github.com/Lora-net/LoRaMac-node/tree/master&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;LoRaMac-node&lt;/a&gt;
 is a LoRaWAN stack implementation for end-device, it provides all the code necessary to build a LoRaWAN end device : join the network, send and receive data and many more.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://github.com/littlefs-project/littlefs&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;LittleFS&lt;/a&gt;
 provides a fail-safe file system designed for micro controllers and embedded systems. I use this library to store data to non-volatile memory (SPI flash memory). This data is, for example, all the internal context of the LoRaMac-node library.&lt;/p&gt;
&lt;p&gt;I tried to keep the code as simple and as straightforward as possible. Remember : my goal was to build this project in a limited amount of time. Which means that I implemented only the functionalities that were absolutely necessary.&lt;/p&gt;
&lt;p&gt;The following diagrams shows high level workflow of the firmware:&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;software.png&#34;
        alt=&#34;software.png&#34;/&gt;&lt;/p&gt;
&lt;p&gt;When the device is powered ON, the firmware begins by initializing all the hardware (GPIO pins, SPI bus,…) and the LoRaWAN stack. Then, it enters in an infinite loops that just counts the pulses from the water meter.&lt;/p&gt;
&lt;p&gt;During the initialization step, a timer is started. This timer is responsible for sending LoRaWAN message periodically, if new pulses were detected since the last execution of the timer.&lt;/p&gt;
&lt;p&gt;The whole application is mostly based on an example provided by LoRaMac-node : &lt;a href=&#34;https://github.com/Lora-net/LoRaMac-node/tree/master/src/apps/LoRaMac/periodic-uplink-lpp&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;periodic-uplink-lpp&lt;/a&gt;
.&lt;/p&gt;
&lt;h2 id=&#34;porting-loramac-node&#34;&gt;Porting LoRaMac-node&lt;/h2&gt;
&lt;p&gt;&lt;a href=&#34;https://github.com/Lora-net/LoRaMac-node&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;LoRaMac-node&lt;/a&gt;
 is included in the project as a git submodule in &lt;code&gt;src/LoRaMac-node&lt;/code&gt;. The library is highly configurable using compile time variable definitions. Since I use CMake to generate the project, setting those variables is very easy. In this case, I enabled the EU868 region and the &lt;em&gt;SOFT_SE&lt;/em&gt; implementation of the secure element.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;compile_definitions.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;p&gt;LoRaMac-node already supports multiple MCUs (STM32, SAML21) and boards NAMote, SAMR34, SKiM,… I don’t know much about those boards, but they are supported out of the box by the project.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;loramac-boards.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;p&gt;But the project does not support anything related to the NRF52, so I’ll have to implement support for this MCU by myself. Fortunately, the library is well written, and porting the library is mostly a matter of writing low-level code and drivers specific to the MCU and the board it’ll be running on.&lt;/p&gt;
&lt;p&gt;In the folder &lt;em&gt;&lt;a href=&#34;https://github.com/Lora-net/LoRaMac-node/tree/master/src/boards&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;src/boards&lt;/a&gt;
&lt;/em&gt; of LoRaMac-node, we notice that there are many header files (.h) : &lt;em&gt;delay-board.h&lt;/em&gt;, &lt;em&gt;gpio-board.h&lt;/em&gt;, &lt;em&gt;spi-board.h&lt;/em&gt;,… Then, in the folder of each specific board, you’ll find the implementation corresponding to those headers:&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;boards-code-files.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;p&gt;So, basically, all we need to do is to provide an implementation for the functions declared in those headers. For this project, you’ll find the implementation in &lt;a href=&#34;https://codeberg.org/JF002/xiao-lora-water-meter/src/branch/master/src/loramac-drivers&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;src/loramac-drivers&lt;/a&gt;
:&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;loramac-drivers.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;board.c&lt;/em&gt; implements functions related to the board: board initialization, reset, low power management,…&lt;/li&gt;
&lt;li&gt;&lt;em&gt;delay-board.c&lt;/em&gt; implements a delay function (sleep for a specified amount of milliseconds).&lt;/li&gt;
&lt;li&gt;&lt;em&gt;eeprom-board&lt;/em&gt; contains functions related to the eeprom. Since my board does not have an eeprom, I implemented non-volatile memory using a LittleFS filesystem in the external SPI flash memory.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;gpio-board&lt;/em&gt; contains functions related to GPIO management (initialization, read, write, interrupts,…).&lt;/li&gt;
&lt;li&gt;&lt;em&gt;rtc-board&lt;/em&gt; implements functions to drive an RTC. The NRF52 does not have a complete RTC peripheral (that manage the date, time, calendar,…) but I managed to emulate the behavior using the RTC timer from the MCU.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;spi-board&lt;/em&gt; contains the SPI driver needed to communicate with the SX1262 (the LoRa module) and also the external SPI flash memory.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;sx1262-board.c&lt;/em&gt; implement a few functions related to the SX1262 that might change from a board to another.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The most difficult part in the step was probably figuring out what LoRaMac-node expected from the RTC in order to implement the correct behavior. The rest was mostly straightforward.&lt;/p&gt;
&lt;p&gt;Once all the functions are implemented, the library should build just fine. Now, we just have to provide a &lt;code&gt;main()&lt;/code&gt; function. In this case, I mostly copied/pasted the &lt;em&gt;periodic-uplink-lpp&lt;/em&gt; example, as it does exactly what I need (monitor signals and periodically send data over LoRaWAN). The original example monitors the battery voltage. I just needed to add the monitoring of the number of pulses detected. The data is first serialized using &lt;a href=&#34;https://github.com/myDevicesIoT/CayenneLPP&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Cayenne Low Power Payload&lt;/a&gt;
. The message contains a message number, the number of pulses detected since the board was powered on and the battery voltage.&lt;/p&gt;
&lt;p&gt;Pulses are detected using the IRQ of the GPIO pin. The handler is implemented in &lt;code&gt;ButtonIrqHandler()&lt;/code&gt; and provide a very simple debounce functionality:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kt&#34;&gt;int&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;btnCpt&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kt&#34;&gt;uint32_t&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;debounceCounter&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kt&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;ButtonIrqHandler&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kt&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kt&#34;&gt;void&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;kt&#34;&gt;uint32_t&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentTimerValue&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;RtcGetTimerValue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;currentTimerValue&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;-&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;debounceCounter&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;5&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;debounceCounter&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;currentTimerValue&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;n&#34;&gt;btnCpt&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;++&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;nf&#34;&gt;NRF_LOG_INFO&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;Pulse detected. Total : %d&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;btnCpt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Battery voltage measurement is triggered by a timer:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;static&lt;/span&gt; &lt;span class=&#34;kt&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;OnBatteryTimerEvent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kt&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kt&#34;&gt;void&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;nf&#34;&gt;battery_init&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;NRF_SAADC_INPUT_AIN7&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;14&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;nf&#34;&gt;TimerStart&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;BatteryTimer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And frame transmission is implemented in &lt;code&gt;PrepareTxFrame()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;static&lt;/span&gt; &lt;span class=&#34;kt&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;PrepareTxFrame&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kt&#34;&gt;void&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;LmHandlerIsBusy&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;true&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;kt&#34;&gt;uint8_t&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;channel&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;n&#34;&gt;AppData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Port&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;LORAWAN_APP_PORT&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;nf&#34;&gt;CayenneLppReset&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;nf&#34;&gt;CayenneLppAddDigitalInput&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;channel&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;++&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;messageCount&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;++&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;nf&#34;&gt;CayenneLppAddDigitalInput&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;channel&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;++&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;btnCpt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;nf&#34;&gt;CayenneLppAddAnalogInput&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;channel&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;++&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;battery_getVoltage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;/&lt;/span&gt; &lt;span class=&#34;mf&#34;&gt;1000.0f&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;nf&#34;&gt;CayenneLppCopy&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;AppData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Buffer&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;n&#34;&gt;AppData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;BufferSize&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;CayenneLppGetSize&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;LmHandlerSend&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;AppData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;LmHandlerParams&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;IsTxConfirmed&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;LORAMAC_HANDLER_SUCCESS&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;c1&#34;&gt;// TX confirmed
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;  &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;power-optimizations&#34;&gt;Power optimizations&lt;/h2&gt;
&lt;p&gt;For this project to run as long as possible on a single battery charge, I needed to ensure that the MCU would go to sleep mode as often as possible and use as little current as possible, especially in sleep mode.&lt;/p&gt;
&lt;p&gt;The PineTime community did a great job &lt;a href=&#34;https://wiki.pine64.org/wiki/PineTime#Reducing_power_consumption&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;documenting everything that must be done to reduce the power usage of the PineTime&lt;/a&gt;
. Since the NRF52832 that powers the PineTime is very similar to the NRF52840 used in this project, all these recommandations also applies to this MCU.&lt;/p&gt;
&lt;p&gt;So first, we need to enable the internal DCDC converter and the low frequency clock, in &lt;code&gt;BoardInitMcu()&lt;/code&gt; (&lt;em&gt;&lt;a href=&#34;https://codeberg.org/JF002/xiao-lora-water-meter/src/branch/master/src/loramac-drivers/board.c&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;src/loramac-drivers/board.c&lt;/a&gt;
):&lt;/em&gt;&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Enable internal DCDC converter, improves power usage
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;  &lt;span class=&#34;n&#34;&gt;NRF_POWER&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;DCDCEN&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;c1&#34;&gt;// Init clocks
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;  &lt;span class=&#34;nf&#34;&gt;nrf_drv_clock_init&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;nf&#34;&gt;nrf_drv_clock_lfclk_request&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;NULL&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;nf&#34;&gt;nrfx_clock_lfclk_start&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;k&#34;&gt;while&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;!&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;nrf_clock_lf_is_running&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;())&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then, in &lt;code&gt;BoardLowPowerHandler()&lt;/code&gt; (also in &lt;a href=&#34;https://codeberg.org/JF002/xiao-lora-water-meter/src/branch/master/src/loramac-drivers/board.c&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;src/loramac-drivers/board.c&lt;/a&gt;
), we ensure that the SPI flash and SPI peripheral are correctly put to sleep mode and disabled before calling &lt;code&gt;_WFI()&lt;/code&gt; to actually put the MCU to sleep. The SPI bus and flash memory obviously need to we woken up when the CPU recovers from sleep mode.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kt&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;BoardLowPowerHandler&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;kt&#34;&gt;void&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;c1&#34;&gt;// Put the SPI flash and SPI peripheral to sleep
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;  &lt;span class=&#34;nf&#34;&gt;SpiFlash_Sleep&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;nf&#34;&gt;SpiMaster_sleep&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;loraSpiContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;c1&#34;&gt;// Put the CPU to sleep
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;  &lt;span class=&#34;nf&#34;&gt;__WFI&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;c1&#34;&gt;// CPU has just woken up, wake the SPI peripheral and flash memory up
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;  &lt;span class=&#34;nf&#34;&gt;SpiMaster_wakeup&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;loraSpiContext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;  &lt;span class=&#34;nf&#34;&gt;SpiFlash_Wakeup&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Using this implementation, the firmware uses&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;70mA&lt;/strong&gt; for 1.5s at startup&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;8mA&lt;/strong&gt; for 1.8s and then &lt;strong&gt;2mA&lt;/strong&gt; for 750ms during the OTAA join operation&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img  src=&#34;power-init-join.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;17µA&lt;/strong&gt; in sleep mode&lt;/li&gt;
&lt;li&gt;68mA for 1s during TX and 8mA for 2s during RX&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img  src=&#34;power-tx.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;p&gt;All in all, it uses an average of 45.33µA when sending a message every 10 minutes.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;power-10-minutes.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;p&gt;I’ll probably improve the software in the near future to increase the TX period, and ensure that new messages are sent only if new data is available (if new pulses were detected since the last message, that is).&lt;/p&gt;
&lt;p&gt;However, with those current number, the device should theoretically run on a single 1000mAh battery charge for more than 2.5 years, which is already quite nice!&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part5&#34;&gt;In the next part, I&amp;rsquo;ll explain how I integrated this sensor with TTN, Node-RED and Home-Assistant!&lt;/a&gt;
&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>DIY low-power LoRa water-meter - Part 3 : Hardware design</title>
      <link>https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part3/</link>
      <pubDate>Sat, 05 Aug 2023 14:00:00 +0100</pubDate>
      
      <guid>https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part3/</guid>
      <description>&lt;p&gt;This blog post is part of a series of posts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part1&#34;&gt;Part 1 : Introduction&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part2&#34;&gt;Part 2 : From prototypes to V1.0&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part3&#34;&gt;Part 3 : Hardware design&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part4&#34;&gt;Part 4 : Software&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part5&#34;&gt;Part 5 : Integration with TTN, Node-RED and Home-Assistant&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h1 id=&#34;hardware-design&#34;&gt;Hardware design&lt;/h1&gt;
&lt;p&gt;&lt;strong&gt;Important notice&lt;/strong&gt; : I’m a software engineer, and I have very limited knowledge and experience in anything related to hardware design and electronics. I can read a datasheet and a schematics, and I can plug components on a breadboard. But I’ve never designed complete electronic boards and PCB so.. bear with me ;-)&lt;/p&gt;
&lt;p&gt;The first prototype was very messy, but it allowed me to implement the whole firmware and check that everything was working as expected.&lt;/p&gt;
&lt;p&gt;As you can see on the following picture, I’m using a &lt;a href=&#34;https://www.nordicsemi.com/Products/Development-hardware/nRF52-DK&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;NRF52DK board&lt;/a&gt;
 as a SWD probe (to flash and debug the firmware). I also use the &lt;a href=&#34;https://www.nordicsemi.com/Products/Development-hardware/Power-Profiler-Kit-2&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;NRF PPKII&lt;/a&gt;
 as power source and power profiler, and a logic analyzer, &lt;a href=&#34;https://eur.saleae.com/products/saleae-logic-8&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;the Saleae Logic 8&lt;/a&gt;
, to probe various signals from the MCU and LoRa module. There’s also a 4600mAh battery (which was replaced later on by a 1000mAh one).&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;breadboard-proto.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;p&gt;Once everything was working properly, I cleaned the setup and used a single breadboard. This allowed me to run longer tests.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;cleaner-breadboard-proto.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;p&gt;I was very happy with the software but the hardware setup wasn’t “production ready”. This setup on a breadboard is very fragile and unreliable. Plus, I wanted to use this &lt;a href=&#34;https://docs.pycom.io/datasheets/accessories/ip67case/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;waterproof case&lt;/a&gt;
 I bought when I thought I would use the Lopy board.&lt;/p&gt;
&lt;p&gt;So I decided to try something completely new to me : design a print a PCB! I have never done that, but I remembered that a colleague told me about &lt;a href=&#34;https://fritzing.org/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Fritzing&lt;/a&gt;
, an open source tool that allows you to draw your circuit on a breadboard and then to design the coresponding PCB. They also run &lt;a href=&#34;https://aisler.net/partners/fritzing&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Fritzing Fab&lt;/a&gt;
 a service that builds (prints?) the PCB for you at a affordable price.&lt;/p&gt;
&lt;p&gt;So first, I replicated the actual breadboard setup in Fritzing:&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;fritzing-breadboard-view.png&#34;
        alt=&#34;pcb_bb.png&#34;/&gt;&lt;/p&gt;
&lt;p&gt;Then I switched to the PCB view and tried to route all the traces on 2 layers (top and bottom). It’s a lot more complicated than I expected : traces cannot cross other traces from the same layer! I spent way more time than expected on this step, but I have to say I’m quite happy with the result.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;fritzing-pcb-view.png&#34;
        alt=&#34;pcb_pcb.png&#34;/&gt;&lt;/p&gt;
&lt;p&gt;Before placing the order to print the PCB, I double checked that my measurements were correct by… printing the PCB on a piece of paper, drilling holes for the screws and components and checking that everything fit nicely:&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;drill.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;p&gt;And here is the results after a few adjustements:&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;paper-pcb.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;p&gt;Then, I placed an order to Aisler to build the PCB. The whole process was very easy, even for me! The minium quantity is 3 PCBs, and it cost 24€ (+ shipping) to print them.&lt;/p&gt;
&lt;p&gt;I received the PCBs a few days later:&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;pcb-delivered.jpg&#34;
        alt=&#34;20230720_124544.jpg&#34;/&gt;&lt;/p&gt;
&lt;p&gt;Next step : solder everything together!&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;pcb-with-accessories.jpg&#34;
        alt=&#34;20230720_212639.jpg&#34;/&gt;&lt;/p&gt;
&lt;p&gt;And put everything in the case (the battery is under the PCB):&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;pcb-in-case.jpg&#34;
        alt=&#34;20230720_224035.jpg&#34;/&gt;&lt;/p&gt;
&lt;p&gt;Awesome! Everything fit nicely in the case, and works as expected!&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part4&#34;&gt;I&amp;rsquo;ll provide more details on the software implementation in the next part of this series!&lt;/a&gt;
&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>DIY low-power LoRa water-meter - Part 2 : From prototypes to the final product</title>
      <link>https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part2/</link>
      <pubDate>Sat, 05 Aug 2023 13:00:00 +0100</pubDate>
      
      <guid>https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part2/</guid>
      <description>&lt;p&gt;This blog post is part of a series of posts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part1&#34;&gt;Part 1 : Introduction&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part2&#34;&gt;Part 2 : From prototypes to V1.0&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part3&#34;&gt;Part 3 : Hardware design&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part4&#34;&gt;Part 4 : Software&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part5&#34;&gt;Part 5 : Integration with TTN, Node-RED and Home-Assistant&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;p&gt;As I previously said, I started this project more than 2 years ago. I obviously didn’t work on it full time since then, but I nevertheless tried many solutions to implement my idea.&lt;/p&gt;
&lt;p&gt;I’ll introduce the different options I experimented with in this section.&lt;/p&gt;
&lt;h2 id=&#34;pycom-lopy--micropython&#34;&gt;PyCom Lopy &amp;amp; MicroPython&lt;/h2&gt;
&lt;p&gt;I bought a kit of 2 &lt;a href=&#34;https://docs.pycom.io/datasheets/development/lopy/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;PyCom Lopy  boards&lt;/a&gt;
 a few years ago. Those boards, based on the ESP32, are equipped with a LoRa module. I bought these to experiment with LoRa and LoRaWAN, but never built an actual project with them.&lt;/p&gt;
&lt;p&gt;The software support for those boards is really great. You can easily program them using &lt;a href=&#34;https://micropython.org/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;MicroPython&lt;/a&gt;
, and PyCom provides all the libraries needed to use all onboard peripherals, the LoRa module included.&lt;/p&gt;
&lt;p&gt;The idea was to use one of the boards are an emitter : it counts the pulses from the water meter and sends the data over &lt;em&gt;raw&lt;/em&gt; LoRa. The second board is a receiver : it receives the data from the first one and publish them over MQTT (using the WiFi radio from the ESP32). I can then use Home-Assistant to process and display the data.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;prototype1.jpg&#34;
        alt=&#34;A picture of the boards I took while working on the software. The board on the left is the emitter. The button allows to simulate a pulse from the water meter. The board on the right is the receiver.&#34;/&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;A picture of the boards I took while working on the software. The board on the left is the emitter. The button allows to simulate a pulse from the water meter. The board on the right is the receiver.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;prototype-ha-grafana.jpg&#34;
        alt=&#34;Debugging the software : logic analyzer on the top, Home-Assistant and MQTT on the left, Grafana on the right.&#34;/&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Debugging the software : logic analyzer on the top, Home-Assistant and MQTT on the left, Grafana on the right.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I was able to build this prototype over the course of a week-end, and it worked great. Except that the battery life was really bad : the emitter ran for 10 days on a 10000mAh battery.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;prototype-test.jpg&#34;
        alt=&#34;The emitter board installed in a plastic board and connected to the watermeter in the shed.&#34;/&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;The emitter board installed in a plastic board and connected to the watermeter in the shed.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I tried to reduce the power usage of this build, with not much success. If I remember correctly, the only way to reduce the power usage was to put the system in deep sleep mode, which needed a full reset of the MCU to wake it up. Since the ESP32 doesn’t have any RAM retention functionality, I couldn’t find a way to store any information that would survive a reset.&lt;/p&gt;
&lt;p&gt;Since I had already figured that my water installation was fine, I did not try to overcome this limitation and put the project on the side to focus on other projects like &lt;a href=&#34;https://github.com/InfiniTimeOrg/InfiniTime&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;InfiniTime&lt;/a&gt;
.&lt;/p&gt;
&lt;h2 id=&#34;risc-v-bouffalo-bl602-bl702-bl616&#34;&gt;RISC-V, Bouffalo BL602, BL702, BL616&lt;/h2&gt;
&lt;p&gt;As you may know, I’m quite invested in the Pine64 community. In 2020, Pine64 released the &lt;a href=&#34;https://wiki.pine64.org/wiki/Pinecil#Pinecil_V1&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Pinecil&lt;/a&gt;
, a soldering iron based on a RISC-V MCU. 2020 was also the year of the Nutcracker challenge : the goal was to incentive developers to work on a FOSS BLE/WIFI stack for the BL602 RISC-V MCU from &lt;a href=&#34;http://bouffalolab.com/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Bouffalo&lt;/a&gt;
. They would send a PineCone (development board based on the BL602) to any developer interested in this challenge. Since then, Pine64 confirmed their interested in RISC-V by releasing the &lt;a href=&#34;https://wiki.pine64.org/wiki/Pinecil#Pinecil_V2&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Pinecil V2&lt;/a&gt;
 (based on the BL706), the &lt;a href=&#34;https://wiki.pine64.org/wiki/STAR64&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Star64&lt;/a&gt;
 (SBC), the &lt;a href=&#34;https://wiki.pine64.org/wiki/Ox64&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;OX64&lt;/a&gt;
 (MCU/SBC based on the BL808) and the &lt;a href=&#34;https://wiki.pine64.org/wiki/PineTab-V&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;PineTab-V&lt;/a&gt;
 (RISC-V tablet, based on the same MCU that the Star64).&lt;/p&gt;
&lt;p&gt;TL Lim, founder of Pine64 also sent me a few prototypes of the &lt;a href=&#34;https://wiki.pine64.org/wiki/Pinedio&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;PineDio&lt;/a&gt;
 (development kit based on the BL604, equipped with LoRa, battery charger, solar panel, GPS,…) and a development board for the BL618, which could be useful for future projects.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;lvgl-demo.jpg&#34;
        alt=&#34;The PineDio STACK prototype running some LVGL demo.&#34;/&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;The PineDio STACK prototype running some LVGL demo.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;This shows how Pine64, TL and I are interested in experimenting with RISC-V, Bouffalo chips and LoRa!&lt;/p&gt;
&lt;p&gt;Since my watermeter project is a quite simple project, I wanted to implement it on one of those RISC-V chips. I did many attempts but haven’t been able put everything together. The issue here is documentation and software support. Their SDKs (yes, there are mutliple SDK) are quite new and… they are a mess. At that time, they had 2 SDKs : the IOT and the MCU SDK. The IOT SDK was bloated and difficult to work with, but it provided support for BLE and WIFI. The MCU SDK was a bit lighter and easier to work with, but far from ideal, still. Both of them where missing drivers and examples for multiple low level functionalities of the MCU like the power management, for example.&lt;/p&gt;
&lt;p&gt;The situation improved later on, both SDKs were merged into a single one : the &lt;a href=&#34;https://github.com/bouffalolab/bouffalo_sdk&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Bouffalo SDK&lt;/a&gt;
. From what I can tell, the state of the SDK improves regularly, they add missing functionalities, clean the architecture,… but last time I checked, power management wasn’t integrated in the SDK yet, and it’s also missing from the documentation.&lt;/p&gt;
&lt;p&gt;I did some tests, manage to hack a few tests to put the MCU in sleep/hibernate mode, but couldn’t use it relibably enough.&lt;/p&gt;
&lt;p&gt;That’s a shame since I could implement all other functionalities : SPI driver, communication with the SX1262 (LoRa module), pulse counting, port the LoRaMAC stack,…&lt;/p&gt;
&lt;p&gt;Bouffalo will hopefully improve their SDK and documentation so that I can build this project on their chips at some point in the future!&lt;/p&gt;
&lt;h2 id=&#34;nrf52&#34;&gt;NRF52&lt;/h2&gt;
&lt;p&gt;I restarted this project when I learnt about the &lt;a href=&#34;https://www.seeedstudio.com/catalog/category/view/s/XIAO-c-1964/id/1964/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Seeed Studio Xiao boards&lt;/a&gt;
. Those are minimalist and cheap development boards for multiples MCU : RP2040, SAMD21, ESP32C3 and the NRF52840. I was especially interested in the later one, since Seeed Studio claims that it sips only 5µA in sleep mode and that it’s small and cheap. Moreover, working with the NRF52840 will be very easy for me because it’s the big brother of the NRF52832 which I very well thanks to the &lt;a href=&#34;https://wiki.pine64.org/wiki/PineTime&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;PineTime&lt;/a&gt;
 and &lt;a href=&#34;https://github.com/InfiniTimeOrg/InfiniTime&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;InfiniTime&lt;/a&gt;
 projects.&lt;/p&gt;
&lt;p&gt;Eventually, over the course of a few weeks, I went from this prototype on a breadboard…&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;breadboard.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;p&gt;… to this nice V1 of the project :&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;final.png&#34;
        alt=&#34;Untitled&#34;/&gt;&lt;/p&gt;
&lt;p&gt;I’ll dedicate the rest of this series to this implementation of the project : hardware design, software implementation, tests, integration with TTN and Home-Assistant,… &lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part3&#34;&gt;The next part will focus on the hardware design&lt;/a&gt;
.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>DIY low-power LoRa water-meter - Part 1 : Introduction</title>
      <link>https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part1/</link>
      <pubDate>Sat, 05 Aug 2023 12:00:00 +0100</pubDate>
      
      <guid>https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part1/</guid>
      <description>&lt;p&gt;This blog post is part of a series of posts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part1&#34;&gt;Part 1 : Introduction&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part2&#34;&gt;Part 2 : From prototypes to V1.0&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part3&#34;&gt;Part 3 : Hardware design&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part4&#34;&gt;Part 4 : Software&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part5&#34;&gt;Part 5 : Integration with TTN, Node-RED and Home-Assistant&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h1 id=&#34;introduction&#34;&gt;Introduction&lt;/h1&gt;
&lt;p&gt;My water consumption increased by a lot (+20%) in 2020-2021. Even if water isn&amp;rsquo;t the most expensive of my utility bills, I was still a bit concerned : maybe there’s leak in my water installation. This would be not good for my home and for the planet.&lt;/p&gt;
&lt;p&gt;Most people I know would have simply checked their water meter regularly to see if the meter reading was rising overnight or while they were at work, for example.&lt;/p&gt;
&lt;p&gt;But… I’m not one of these people, and I want to automate this manual operation. Plus, my water meter is installed in a small shed in the garden, away from the house, which makes this operation even more annoying.&lt;/p&gt;
&lt;p&gt;Then, some of the people I know might have tried to find off-the-shelf components to do the job : it’s probably possible to find ‘something’ that plugs on the water meter and that sends the water consumption to some proprietary cloud service or even Home-Assistant.&lt;/p&gt;
&lt;p&gt;But… my case is a bit more difficult : the shed where my water meter is installed is away from the house, which means my WiFi network doesn’t go that far and there’s no power available near that shed.&lt;/p&gt;
&lt;p&gt;So yeah, yeah, I could have installed a WiFi extender and extended my electrical installation, but that’s not the goal : I’m a software engineer and I like to thinker with cheap MCU so… I’ll just build my own water meter that works in my use-cases.&lt;/p&gt;
&lt;p&gt;What do I need, then ? It’s simple : I need a low power battery operated pulse counter (my water meter supports an add-on module that generates one pulse for each liter that flows through it) that does not rely on WiFi or wired communication to send the results.&lt;/p&gt;
&lt;p&gt;This serie of articles is about this amazing (to me, at least) project I started 2 years ago which allowed me to learn about LoRa, the nRF52840 MCU and its low-power modes, electronics, PCB design and manufacturing,…&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;final-product.jpg&#34;
        alt=&#34;V1.0&#34;/&gt;&lt;/p&gt;
&lt;p&gt;Oh! By the way… there was no leak in my water installation. The increased water usage was probably caused by the fact that my wife and myself were stuck at home for more than a year because of the pandemic!&lt;/p&gt;
&lt;h1 id=&#34;general-overview&#34;&gt;General overview&lt;/h1&gt;
&lt;p&gt;As I said in the introduction, I want to be able to monitor my water usage. My water meter provides a pulse output : a pulse is generated each time a liter flowed trough the meter. To get this data into &lt;a href=&#34;https://www.home-assistant.io/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Home-Assistant&lt;/a&gt;
 (my home automation platform), I need &lt;em&gt;something&lt;/em&gt; that sits in between the water meter and my Home-Assistant instance running on my local network.&lt;/p&gt;
&lt;p&gt;This &lt;em&gt;something&lt;/em&gt; will need to be able to do 3 things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Count the pulses on a GPIO&lt;/li&gt;
&lt;li&gt;Send them over a low power wireless communication&lt;/li&gt;
&lt;li&gt;Run on a battery, which means that it must use as little power as possible&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To achieve this goal, I have to choose an MCU that will run the whole process, and a &lt;em&gt;low power wireless communication protocol&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;I decided to use the &lt;strong&gt;&lt;a href=&#34;https://www.seeedstudio.com/Seeed-XIAO-BLE-nRF52840-p-5201.html&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;SeeedStudio XIAO NRF52840&lt;/a&gt;
&lt;/strong&gt; MCU board. It’s cheap, it contains the bare minimum needed to run the MCU (which means that it’ll use less power than more crowded boards) and I’m already familiar with the NRF52 MCU family.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;xiao-board-front.png&#34;
        alt=&#34;Front view of the Seeed Studio XIAO NRF52840 board&#34;/&gt;&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;xiao-board-back.png&#34;
        alt=&#34;Back view of the Seeed Studio XIAO NRF52840 board&#34;/&gt;&lt;/p&gt;
&lt;p&gt;For the wireless communication protocol, I decided to use LoRaWAN since I’m really interested in this technology and I already own a few LoRa modules. For this project, I’ll use a LoRa module from &lt;a href=&#34;https://pine64.org&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Pine64&lt;/a&gt;
. I don’t think it’s available in the store right now, but it’s very similar to the well known RFM90 module.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;lora-module-front.png&#34;
        alt=&#34;Front view of the Pine64 LoRa module&#34;/&gt;&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;lora-module-back.png&#34;
        alt=&#34;Back view of the Pine64 LoRa module&#34;/&gt;&lt;/p&gt;
&lt;p&gt;LoRaWAN also needs a LoRaWAN gateway to connect the end device (this project) to the network. I already own the &lt;a href=&#34;https://wiki.pine64.org/wiki/Pinedio#Gateway&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Pine64 LoRaWAN gateway&lt;/a&gt;
, so I’ll use it for this project.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;lora-gw.png&#34;
        alt=&#34;The Pine64 LoRa gateway&#34;/&gt;&lt;/p&gt;
&lt;p&gt;I’ll connect this gateway to &lt;a href=&#34;https://www.thethingsnetwork.org/&#34;target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;The Things Network (TTN)&lt;/a&gt;
. It’s a global collaborative LoRaWAN network. Anyone is free to use the network and to add their own gateway to extend its range.&lt;/p&gt;
&lt;p&gt;&lt;img  src=&#34;ttn-gw.png&#34;
        alt=&#34;My LoRa gateway in the TTN console&#34;/&gt;&lt;/p&gt;
&lt;p&gt;And here is the high level view of the project:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Pulses are generated by the watermeter&lt;/li&gt;
&lt;li&gt;They are counted by the MCU&lt;/li&gt;
&lt;li&gt;Results are sent over LoRaWAN via the LoRa module&lt;/li&gt;
&lt;li&gt;The LoRaWAN gateway forwards the data to The Things Network&lt;/li&gt;
&lt;li&gt;TTN processes the data and publishes them on a MQTT endpoint&lt;/li&gt;
&lt;li&gt;Home-Assistant receives the data from MQTT&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img  src=&#34;global-overview-diagram.png&#34;
        alt=&#34;Global overview diagram&#34;/&gt;&lt;/p&gt;
&lt;p&gt;In the next part of this series, &lt;a href=&#34;https://codingfield.com/blog/2023-08/diy-low-power-lora-water-meter-part2&#34;&gt;I&amp;rsquo;ll give more details on the history of the project, how I went from prototype to V1.0&lt;/a&gt;
.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>