<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Jim&#39;s Blog</title>
<link>https://jcarr.codeberg.page/blog/</link>
<atom:link href="https://jcarr.codeberg.page/blog/index.xml" rel="self" type="application/rss+xml"/>
<description>Musings...</description>
<generator>quarto-1.9.37</generator>
<lastBuildDate>Sat, 13 Sep 2025 04:00:00 GMT</lastBuildDate>
<item>
  <title>Memory Debugging in C</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2025/memory_debug_c.html</link>
  <description><![CDATA[ 






<table>
<tbody><tr>
<td>
The landscape of memory safety in low-level programming languages, particularly C and C++, is increasingly under scrutiny due to the significant security vulnerabilities associated with unsafe memory practices. Recent reports and initiatives highlight the urgent need for a shift towards memory-safe languages to mitigate these risks.
</td>
<td>
<img src="https://jcarr.codeberg.page/blog/posts/2025/images/sherlock-holmes.png">
</td>
</tr>
</tbody></table>
<section id="key-findings-on-memory-safety-vulnerabilities" class="level1">
<h1>Key Findings on Memory Safety Vulnerabilities</h1>
<ol type="1">
<li><strong>Prevalence of Memory Safety Issues</strong>:
<ul>
<li>A staggering <strong>70%</strong> of vulnerabilities in products from major tech companies like Microsoft and Google are attributed to memory safety issues. This includes common problems such as buffer overflows and use-after-free bugs.</li>
<li>In mobile operating systems, studies indicate that <strong>60-90%</strong> of vulnerabilities are related to memory safety.</li>
</ul></li>
<li><strong>Types of Memory Safety Errors</strong>:
<ul>
<li><strong>Out-of-Bounds Errors</strong>: Occur when a program accesses memory outside the allocated range, potentially leading to data corruption or unauthorized access.</li>
<li><strong>Use-After-Free Bugs</strong>: Happen when a program continues to use memory after it has been freed, which can lead to unpredictable behavior and security breaches.</li>
</ul></li>
<li><strong>Historical Context</strong>:
<ul>
<li>Notable security incidents, such as the <strong>Heartbleed</strong> vulnerability, which compromised sensitive data across numerous websites, were rooted in memory safety flaws. Such incidents underscore the critical need for improved memory management practices.</li>
</ul></li>
</ol>
<section id="recommendations-and-initiatives" class="level2">
<h2 class="anchored" data-anchor-id="recommendations-and-initiatives">Recommendations and Initiatives</h2>
<ol type="1">
<li><strong>Adoption of Memory-Safe Languages</strong>:
<ul>
<li>The <strong>U.S. White House Office of the National Cyber Director</strong> has recommended transitioning to memory-safe languages like <strong>Rust</strong>, <strong>Go</strong>, <strong>C#</strong>, <strong>Java</strong>, and <strong>Swift</strong>. These languages incorporate built-in protections that help prevent common memory-related vulnerabilities.</li>
<li>The <strong>National Security Agency (NSA)</strong> and <strong>Cybersecurity and Infrastructure Security Agency (CISA)</strong> have jointly emphasized the importance of adopting memory-safe languages as part of a comprehensive cybersecurity strategy.</li>
</ul></li>
<li><strong>The Memory Safety Continuum</strong>:
<ul>
<li>Released by the <strong>Open Source Security Foundation (OpenSSF)</strong>, this framework provides a structured approach for organizations to assess and improve their memory safety practices. It encourages incremental improvements rather than viewing memory safety as a binary state.</li>
</ul></li>
<li><strong>Challenges in Transition</strong>:
<ul>
<li>While the shift to memory-safe languages is encouraged, organizations face challenges such as performance considerations, the complexity of legacy codebases, and the need for developer training. Immediate adoption may not be feasible, necessitating a phased approach.</li>
</ul></li>
</ol>
<blockquote class="blockquote">
<p><mark><em>But</em>, with a massive existing codebase, along with a talent pool with many decades of experience, what steps can we take to improve the quality and safety of code in older languages, C in particular?</mark></p>
<p><mark>Let’s talk about that!</mark></p>
</blockquote>
</section>
</section>
<section id="tools" class="level1">
<h1>Tools</h1>
<ul>
<li><strong>GCC</strong>, the GNU C compiler. It’s preinstalled on most Linux distributions.</li>
<li><strong>Clang Tidy</strong>, provides linting and code modernization.</li>
<li><strong>Address Sanitizer</strong>, a compile flag built into the gcc compiler.</li>
<li><strong>Valgrind</strong>, a tool used for memory debugging, memory leak detection, and profiling.</li>
</ul>
<section id="install-tools" class="level2">
<h2 class="anchored" data-anchor-id="install-tools">Install Tools</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sudo</span> apt install clang-tidy valgrind</span></code></pre></div></div>
<p>Verify that Clang Tidy and Valgrind are installed and available:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">clang-tidy</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--version</span></span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">valgrind</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--version</span></span></code></pre></div></div>
<p>Output (your versions may differ):</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb3-1">Ubuntu LLVM version 18.1.3</span>
<span id="cb3-2">  Optimized build.</span>
<span id="cb3-3"></span>
<span id="cb3-4">valgrind-3.22.0</span></code></pre></div></div>
</section>
</section>
<section id="memory-debugging" class="level1">
<h1>Memory Debugging</h1>
<p>Let’s walk through some scenarios.</p>
<section id="buffer-overflow" class="level2">
<h2 class="anchored" data-anchor-id="buffer-overflow">Buffer Overflow</h2>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>buffer_overflow.c</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" data-filename="buffer_overflow.c" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb4-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;stdio.h&gt;</span></span>
<span id="cb4-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;string.h&gt;</span></span>
<span id="cb4-3"></span>
<span id="cb4-4"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> vulnerableFunction<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb4-5">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> buffer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// A buffer that can hold 10 characters</span></span>
<span id="cb4-6">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Enter some text: "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb4-7">    gets<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>buffer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Unsafe function that does not check buffer size</span></span>
<span id="cb4-8">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"You entered: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%s\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> buffer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb4-9"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb4-10"></span>
<span id="cb4-11"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb4-12">    vulnerableFunction<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb4-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-14"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</div>
<p>Compile it:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gcc</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-g</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> buffer_overflow buffer_overflow.c</span></code></pre></div></div>
<p>We immediately get a warning from the compiler, as <code>gets()</code> is inherently unsafe and has been deprecated in the C11 standard:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb6-1">buffer_overflow.c: In function ‘vulnerableFunction’:</span>
<span id="cb6-2">buffer_overflow.c:8:5: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]</span>
<span id="cb6-3">    8 |     gets(buffer); // Unsafe function that does not check buffer size</span>
<span id="cb6-4">      |     ^~~~</span>
<span id="cb6-5">      |     fgets</span>
<span id="cb6-6">/usr/bin/ld: /tmp/ccbCyqjb.o: in function `vulnerableFunction':</span>
<span id="cb6-7">buffer_overflow.c:(.text+0x3c): warning: the `gets' function is dangerous and should not be used.</span></code></pre></div></div>
<p>Let’s “fix” it, but make a mistake on purpose:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>buffer_overflow.c</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" data-filename="buffer_overflow.c" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb7-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;stdio.h&gt;</span></span>
<span id="cb7-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;string.h&gt;</span></span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> vulnerableFunction<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span>
<span id="cb7-5"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-6">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> buffer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb7-7"></span>
<span id="cb7-8">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Enter some text: "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-9">    fgets<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>buffer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> stdin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-10"></span>
<span id="cb7-11">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"You entered: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%s\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> buffer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-12"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-13"></span>
<span id="cb7-14"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span>
<span id="cb7-15"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-16">    vulnerableFunction<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb7-17"></span>
<span id="cb7-18">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-19"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</div>
<p>We’ve switched to the safe fgets function, but “accidently” specified a size that’s too large. When we compile:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gcc</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-g</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> buffer_overflow_updated buffer_overflow_updated.c</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb9-1">buffer_overflow_updated.c: In function ‘vulnerableFunction’:</span>
<span id="cb9-2">buffer_overflow_updated.c:9:5: warning: ‘fgets’ writing 20 bytes into a region of size 10 overflows the destination [-Wstringop-overflow=]</span>
<span id="cb9-3">    9 |     fgets(buffer, 20, stdin);</span>
<span id="cb9-4">      |     ^~~~~~~~~~~~~~~~~~~~~~~~</span>
<span id="cb9-5">buffer_overflow_updated.c:6:10: note: destination object ‘buffer’ of size 10</span>
<span id="cb9-6">    6 |     char buffer[10];</span>
<span id="cb9-7">      |          ^~~~~~</span>
<span id="cb9-8">In file included from buffer_overflow_updated.c:1:</span>
<span id="cb9-9">/usr/include/stdio.h:654:14: note: in a call to function ‘fgets’ declared with attribute ‘access (write_only, 1, 2)’</span>
<span id="cb9-10">  654 | extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)</span>
<span id="cb9-11">      |              ^~~~~</span></code></pre></div></div>
<p>In this scenario, the compiler does a good job of catching the mistake. Let’s update it and do it the right way:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>buffer_overflow.c</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" data-filename="buffer_overflow.c" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb10-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;stdio.h&gt;</span></span>
<span id="cb10-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;string.h&gt;</span></span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> vulnerableFunction<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span>
<span id="cb10-5"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-6">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> buffer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb10-7"></span>
<span id="cb10-8">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Enter some text: "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb10-9">    fgets<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>buffer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">sizeof</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>buffer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span> stdin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb10-10"></span>
<span id="cb10-11">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"You entered: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%s\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> buffer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb10-12"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb10-13"></span>
<span id="cb10-14"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span>
<span id="cb10-15"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-16">    vulnerableFunction<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb10-17"></span>
<span id="cb10-18">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-19"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</div>
<p>The code now compiles with no errors. For this scenario, we didn’t need to use any additional tools to find the problem. But, the compiler can’t catch everything. Consider this example:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>buffer_overflow_dynamic.c</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" data-filename="buffer_overflow_dynamic.c" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb11-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;stdio.h&gt;</span></span>
<span id="cb11-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;string.h&gt;</span></span>
<span id="cb11-3"></span>
<span id="cb11-4"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> vulnerableFunction<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb11-5"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-6">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> buffer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// A buffer that can hold 10 characters</span></span>
<span id="cb11-7">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> input<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Variable-length input buffer</span></span>
<span id="cb11-8"></span>
<span id="cb11-9">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Enter some text: "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb11-10"></span>
<span id="cb11-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Unsafe use of fgets: size is determined at runtime:</span></span>
<span id="cb11-12">    fgets<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>input<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> stdin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Read input into the variable-length buffer</span></span>
<span id="cb11-13"></span>
<span id="cb11-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Potential buffer overflow if input exceeds buffer size</span></span>
<span id="cb11-15">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// No bounds checking on the copy</span></span>
<span id="cb11-16">    strcpy<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>buffer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> input<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Unsafe copy that can overflow buffer</span></span>
<span id="cb11-17"></span>
<span id="cb11-18">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"You entered: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%s\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> buffer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb11-19"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-20"></span>
<span id="cb11-21"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span>
<span id="cb11-22"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-23">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-24"></span>
<span id="cb11-25">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Call the vulnerable function with the size value:</span></span>
<span id="cb11-26">    vulnerableFunction<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb11-27">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-28"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</div>
<p>In the code above, the size of the input buffer is set to 20 (too large), but the compiler evaluates <code>vulnerableFunction</code>, where the input buffer size is treated as dynamic, and so the error isn’t caught: This code compiles with no errors or warnings, but running it gives us something like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb12-1">Enter some text: Some text that is too large on purpose!</span>
<span id="cb12-2">You entered: Some text that is t</span>
<span id="cb12-3">*** stack smashing detected ***: terminated</span>
<span id="cb12-4">Aborted (core dumped)</span></code></pre></div></div>
<p>If we run the code through <code>clang-tidy</code>, we get some hints about unsafe behavior:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb13-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">clang-tidy</span> buffer_overflow_dynamic.c</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb14-1">1 warning generated.</span>
<span id="cb14-2">buffer_overflow_dynamic.c:16:5: warning: Call to function 'strcpy' is insecure as it does not provide bounding of the memory buffer. Replace unbounded copy functions with analogous functions that support length arguments such as 'strlcpy'. CWE-119 [clang-analyzer-security.insecureAPI.strcpy]</span>
<span id="cb14-3">   16 |     strcpy(buffer, input); // Unsafe copy that can overflow buffer</span>
<span id="cb14-4">      |     ^~~~~~</span></code></pre></div></div>
<p>If we re-compile with the address sanitizer flag:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gcc</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-g</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-fsanitize</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>address <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-g</span> buffer_overflow_dynamic.c <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> buffer_overflow_dynamic</span></code></pre></div></div>
<p>…and then run, the failure does get reported, again with information about the point of failure:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb16-1">Enter some text: Some text that is too large on purpose!</span>
<span id="cb16-2">=================================================================</span>
<span id="cb16-3">==1999384==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x728e9730002a at pc 0x728e998a7923 bp 0x7fff2dc230e0 sp 0x7fff2dc22888</span>
<span id="cb16-4">WRITE of size 20 at 0x728e9730002a thread T0</span>
<span id="cb16-5">    #0 0x728e998a7922 in strcpy ../../../../src/libsanitizer/asan/asan_interceptors.cpp:563</span>
<span id="cb16-6">    #1 0x64151ad9347d in vulnerableFunction buffer_overflow_dynamic.c:16</span>
<span id="cb16-7">    #2 0x64151ad9353a in main buffer_overflow_dynamic.c:26</span>
<span id="cb16-8">    #3 0x728e9942a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58</span>
<span id="cb16-9">    #4 0x728e9942a28a in __libc_start_main_impl ../csu/libc-start.c:360</span>
<span id="cb16-10">    #5 0x64151ad931e4 in _start (buffer_overflow_dynamic+0x11e4) (BuildId: 953df7f0607850ac9a18932ea140e86278e57fa6)</span>
<span id="cb16-11"></span>
<span id="cb16-12">Address 0x728e9730002a is located in stack of thread T0 at offset 42 in frame</span>
<span id="cb16-13">    #0 0x64151ad932b8 in vulnerableFunction buffer_overflow_dynamic.c:5</span>
<span id="cb16-14"></span>
<span id="cb16-15">  This frame has 1 object(s):</span>
<span id="cb16-16">    [32, 42) 'buffer' (line 6) &lt;== Memory access at offset 42 overflows this variable</span>
<span id="cb16-17">HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork</span>
<span id="cb16-18">      (longjmp and C++ exceptions *are* supported)</span>
<span id="cb16-19">SUMMARY: AddressSanitizer: stack-buffer-overflow ../../../../src/libsanitizer/asan/asan_interceptors.cpp:563 in strcpy</span>
<span id="cb16-20">Shadow bytes around the buggy address:</span>
<span id="cb16-21">  0x728e972ffd80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span id="cb16-22">  0x728e972ffe00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span id="cb16-23">  0x728e972ffe80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span id="cb16-24">  0x728e972fff00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span id="cb16-25">  0x728e972fff80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span id="cb16-26">=&gt;0x728e97300000: f1 f1 f1 f1 00[02]f3 f3 00 00 00 00 00 00 00 00</span>
<span id="cb16-27">  0x728e97300080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span id="cb16-28">  0x728e97300100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span id="cb16-29">  0x728e97300180: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span id="cb16-30">  0x728e97300200: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span id="cb16-31">  0x728e97300280: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span id="cb16-32">Shadow byte legend (one shadow byte represents 8 application bytes):</span>
<span id="cb16-33">  Addressable:           00</span>
<span id="cb16-34">  Partially addressable: 01 02 03 04 05 06 07 </span>
<span id="cb16-35">  Heap left redzone:       fa</span>
<span id="cb16-36">  Freed heap region:       fd</span>
<span id="cb16-37">  Stack left redzone:      f1</span>
<span id="cb16-38">  Stack mid redzone:       f2</span>
<span id="cb16-39">  Stack right redzone:     f3</span>
<span id="cb16-40">  Stack after return:      f5</span>
<span id="cb16-41">  Stack use after scope:   f8</span>
<span id="cb16-42">  Global redzone:          f9</span>
<span id="cb16-43">  Global init order:       f6</span>
<span id="cb16-44">  Poisoned by user:        f7</span>
<span id="cb16-45">  Container overflow:      fc</span>
<span id="cb16-46">  Array cookie:            ac</span>
<span id="cb16-47">  Intra object redzone:    bb</span>
<span id="cb16-48">  ASan internal:           fe</span>
<span id="cb16-49">  Left alloca redzone:     ca</span>
<span id="cb16-50">  Right alloca redzone:    cb</span>
<span id="cb16-51">==1999384==ABORTING</span></code></pre></div></div>
<p>So, in this case (similar to clang-tidy) we do at least get the failure point, which gives us something to work backwards from.</p>
<p>Finally, if we run through Valgrind:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">valgrind</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--leak-check</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>full <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--show-leak-kinds</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>all ./buffer_overflow_dynamic</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb18-1">==2008869== Memcheck, a memory error detector</span>
<span id="cb18-2">==2008869== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.</span>
<span id="cb18-3">==2008869== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info</span>
<span id="cb18-4">==2008869== Command: ./buffer_overflow_dynamic</span>
<span id="cb18-5">==2008869== </span>
<span id="cb18-6">Enter some text: Some text that is too long on purpose!</span>
<span id="cb18-7">You entered: Some text that is t</span>
<span id="cb18-8">*** stack smashing detected ***: terminated</span>
<span id="cb18-9">==2008869== </span>
<span id="cb18-10">==2008869== Process terminating with default action of signal 6 (SIGABRT)</span>
<span id="cb18-11">==2008869==    at 0x490EB2C: __pthread_kill_implementation (pthread_kill.c:44)</span>
<span id="cb18-12">==2008869==    by 0x490EB2C: __pthread_kill_internal (pthread_kill.c:78)</span>
<span id="cb18-13">==2008869==    by 0x490EB2C: pthread_kill@@GLIBC_2.34 (pthread_kill.c:89)</span>
<span id="cb18-14">==2008869==    by 0x48B527D: raise (raise.c:26)</span>
<span id="cb18-15">==2008869==    by 0x48988FE: abort (abort.c:79)</span>
<span id="cb18-16">==2008869==    by 0x48997B5: __libc_message_impl.cold (libc_fatal.c:134)</span>
<span id="cb18-17">==2008869==    by 0x49A6C18: __fortify_fail (fortify_fail.c:24)</span>
<span id="cb18-18">==2008869==    by 0x49A7EA3: __stack_chk_fail (stack_chk_fail.c:24)</span>
<span id="cb18-19">==2008869==    by 0x1092C8: vulnerableFunction (buffer_overflow_dynamic.c:19)</span>
<span id="cb18-20">==2008869==    by 0x1092EB: main (buffer_overflow_dynamic.c:26)</span>
<span id="cb18-21">==2008869== </span>
<span id="cb18-22">==2008869== HEAP SUMMARY:</span>
<span id="cb18-23">==2008869==     in use at exit: 2,048 bytes in 2 blocks</span>
<span id="cb18-24">==2008869==   total heap usage: 2 allocs, 0 frees, 2,048 bytes allocated</span>
<span id="cb18-25">==2008869== </span>
<span id="cb18-26">==2008869== 1,024 bytes in 1 blocks are still reachable in loss record 1 of 2</span>
<span id="cb18-27">==2008869==    at 0x4846828: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)</span>
<span id="cb18-28">==2008869==    by 0x48F51B4: _IO_file_doallocate (filedoalloc.c:101)</span>
<span id="cb18-29">==2008869==    by 0x4905523: _IO_doallocbuf (genops.c:347)</span>
<span id="cb18-30">==2008869==    by 0x4902F8F: _IO_file_overflow@@GLIBC_2.2.5 (fileops.c:745)</span>
<span id="cb18-31">==2008869==    by 0x4903AAE: _IO_new_file_xsputn (fileops.c:1244)</span>
<span id="cb18-32">==2008869==    by 0x4903AAE: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1197)</span>
<span id="cb18-33">==2008869==    by 0x48D0CC8: __printf_buffer_flush_to_file (printf_buffer_to_file.c:59)</span>
<span id="cb18-34">==2008869==    by 0x48D0CC8: __printf_buffer_to_file_done (printf_buffer_to_file.c:120)</span>
<span id="cb18-35">==2008869==    by 0x48DB742: __vfprintf_internal (vfprintf-internal.c:1545)</span>
<span id="cb18-36">==2008869==    by 0x48D01B2: printf (printf.c:33)</span>
<span id="cb18-37">==2008869==    by 0x10926A: vulnerableFunction (buffer_overflow_dynamic.c:9)</span>
<span id="cb18-38">==2008869==    by 0x1092EB: main (buffer_overflow_dynamic.c:26)</span>
<span id="cb18-39">==2008869== </span>
<span id="cb18-40">==2008869== 1,024 bytes in 1 blocks are still reachable in loss record 2 of 2</span>
<span id="cb18-41">==2008869==    at 0x4846828: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)</span>
<span id="cb18-42">==2008869==    by 0x48F51B4: _IO_file_doallocate (filedoalloc.c:101)</span>
<span id="cb18-43">==2008869==    by 0x4905523: _IO_doallocbuf (genops.c:347)</span>
<span id="cb18-44">==2008869==    by 0x4902883: _IO_file_underflow@@GLIBC_2.2.5 (fileops.c:486)</span>
<span id="cb18-45">==2008869==    by 0x49055D1: _IO_default_uflow (genops.c:362)</span>
<span id="cb18-46">==2008869==    by 0x48F6F79: _IO_getline_info (iogetline.c:60)</span>
<span id="cb18-47">==2008869==    by 0x48F5BD3: fgets (iofgets.c:53)</span>
<span id="cb18-48">==2008869==    by 0x109282: vulnerableFunction (buffer_overflow_dynamic.c:12)</span>
<span id="cb18-49">==2008869==    by 0x1092EB: main (buffer_overflow_dynamic.c:26)</span>
<span id="cb18-50">==2008869== </span>
<span id="cb18-51">==2008869== LEAK SUMMARY:</span>
<span id="cb18-52">==2008869==    definitely lost: 0 bytes in 0 blocks</span>
<span id="cb18-53">==2008869==    indirectly lost: 0 bytes in 0 blocks</span>
<span id="cb18-54">==2008869==      possibly lost: 0 bytes in 0 blocks</span>
<span id="cb18-55">==2008869==    still reachable: 2,048 bytes in 2 blocks</span>
<span id="cb18-56">==2008869==         suppressed: 0 bytes in 0 blocks</span>
<span id="cb18-57">==2008869== </span>
<span id="cb18-58">==2008869== For lists of detected and suppressed errors, rerun with: -s</span>
<span id="cb18-59">==2008869== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)</span>
<span id="cb18-60">Aborted (core dumped)</span></code></pre></div></div>
<p>Again, we get a decent amount of detail around where things went wrong.</p>
<p>Overall, a tricky problem!</p>
</section>
<section id="out-of-bounds-access" class="level2">
<h2 class="anchored" data-anchor-id="out-of-bounds-access">Out of Bounds Access</h2>
<p>We’ll be investigating an Out of Bounds error, but there are actually two problems with this code. Do you see them?</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>out_of_bounds.c</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19" data-filename="out_of_bounds.c" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb19-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;stdlib.h&gt;</span></span>
<span id="cb19-2"></span>
<span id="cb19-3"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span>
<span id="cb19-4"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb19-5">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> malloc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb19-6">    p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb19-7"></span>
<span id="cb19-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb19-9"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</div>
<p>First, let’s see if the linter can find anything:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb20-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">clang-tidy</span> out_of_bounds.c</span></code></pre></div></div>
<p>Output:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb21-1">1 warning generated.</span>
<span id="cb21-2">out_of_bounds.c:8:5: warning: Potential leak of memory pointed to by 'p' [clang-analyzer-unix.Malloc]</span>
<span id="cb21-3">    8 |     return 0;</span>
<span id="cb21-4">      |     ^</span>
<span id="cb21-5">out_of_bounds.c:5:15: note: Memory is allocated</span>
<span id="cb21-6">    5 |     char *p = malloc(16);</span>
<span id="cb21-7">      |               ^~~~~~~~~~</span>
<span id="cb21-8">out_of_bounds.c:8:5: note: Potential leak of memory pointed to by 'p'</span>
<span id="cb21-9">    8 |     return 0;</span>
<span id="cb21-10">      |     ^</span></code></pre></div></div>
<p>Not a lot of detail, but we do get some hints about possible problems with our memory allocation.</p>
<p>Let’s go ahead and compile with <strong>gcc</strong>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb22-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gcc</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-g</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> out_of_bounds out_of_bounds.c</span></code></pre></div></div>
<p>The code compiles with no errors. When we run, it also runs with no errors (probably!) and reports nothing. I say probably because we are making two mistakes in this code:</p>
<ol type="1">
<li>Writing data outside the bounds of the space allocated for <code>p</code>, and</li>
<li>Not freeing the allocated space after we’re done.</li>
</ol>
<p>Let’s try again, this time with the sanitize flag:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb23-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gcc</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-fsanitize</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>address <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-g</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> out_of_bounds out_of_bounds.c</span></code></pre></div></div>
<p>Now when we run, we actually get some useful feedback:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb24-1">==2642974==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x50200000003a at pc 0x55a1ade4e1ff bp 0x7ffcaaeb3380 sp 0x7ffcaaeb3370</span>
<span id="cb24-2">WRITE of size 1 at 0x50200000003a thread T0</span>
<span id="cb24-3">    #0 0x55a1ade4e1fe in main out_of_bounds.c:6</span>
<span id="cb24-4">    #1 0x70fe6c22a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58</span>
<span id="cb24-5">    #2 0x70fe6c22a28a in __libc_start_main_impl ../csu/libc-start.c:360</span>
<span id="cb24-6">    #3 0x55a1ade4e0e4 in _start (out_of_bounds+0x10e4) (BuildId: ec7069e9292b8acada6ab57a772da4c32c98eeeb)</span>
<span id="cb24-7"></span>
<span id="cb24-8">0x50200000003a is located 26 bytes after 16-byte region [0x502000000010,0x502000000020)</span>
<span id="cb24-9">allocated by thread T0 here:</span>
<span id="cb24-10">    #0 0x70fe6c6fd9c7 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69</span>
<span id="cb24-11">    #1 0x55a1ade4e1be in main out_of_bounds.c:5</span>
<span id="cb24-12">    #2 0x70fe6c22a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58</span>
<span id="cb24-13">    #3 0x70fe6c22a28a in __libc_start_main_impl ../csu/libc-start.c:360</span>
<span id="cb24-14">    #4 0x55a1ade4e0e4 in _start (out_of_bounds+0x10e4) (BuildId: ec7069e9292b8acada6ab57a772da4c32c98eeeb)</span>
<span id="cb24-15"></span>
<span id="cb24-16">SUMMARY: AddressSanitizer: heap-buffer-overflow out_of_bounds.c:6 in main</span>
<span id="cb24-17">Shadow bytes around the buggy address:</span>
<span id="cb24-18">  0x501ffffffd80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span id="cb24-19">  0x501ffffffe00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span id="cb24-20">  0x501ffffffe80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span id="cb24-21">  0x501fffffff00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span id="cb24-22">  0x501fffffff80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00</span>
<span id="cb24-23">=&gt;0x502000000000: fa fa 00 00 fa fa fa[fa]fa fa fa fa fa fa fa fa</span>
<span id="cb24-24">  0x502000000080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa</span>
<span id="cb24-25">  0x502000000100: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa</span>
<span id="cb24-26">  0x502000000180: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa</span>
<span id="cb24-27">  0x502000000200: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa</span>
<span id="cb24-28">  0x502000000280: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa</span>
<span id="cb24-29">Shadow byte legend (one shadow byte represents 8 application bytes):</span>
<span id="cb24-30">  Addressable:           00</span>
<span id="cb24-31">  Partially addressable: 01 02 03 04 05 06 07 </span>
<span id="cb24-32">  Heap left redzone:       fa</span>
<span id="cb24-33">  Freed heap region:       fd</span>
<span id="cb24-34">  Stack left redzone:      f1</span>
<span id="cb24-35">  Stack mid redzone:       f2</span>
<span id="cb24-36">  Stack right redzone:     f3</span>
<span id="cb24-37">  Stack after return:      f5</span>
<span id="cb24-38">  Stack use after scope:   f8</span>
<span id="cb24-39">  Global redzone:          f9</span>
<span id="cb24-40">  Global init order:       f6</span>
<span id="cb24-41">  Poisoned by user:        f7</span>
<span id="cb24-42">  Container overflow:      fc</span>
<span id="cb24-43">  Array cookie:            ac</span>
<span id="cb24-44">  Intra object redzone:    bb</span>
<span id="cb24-45">  ASan internal:           fe</span>
<span id="cb24-46">  Left alloca redzone:     ca</span>
<span id="cb24-47">  Right alloca redzone:    cb</span>
<span id="cb24-48">==2642974==ABORTING</span></code></pre></div></div>
<p>Running with the sanitize flag, we get information about the out-of-bounds write. Highlights:</p>
<ul>
<li>heap-buffer-overflow on address 0x50200000003a</li>
<li>#0 0x55a1ade4e1fe in main out_of_bounds.c:6</li>
<li>0x50200000003a is located 26 bytes after 16-byte region [0x502000000010,0x502000000020)</li>
</ul>
<p>With this, we actually have some actionable information. But, compiling with the sanitize flag does <em>not</em> identify the missing <code>free</code>.</p>
<p>Let’s see what Valgrind can find. Recompile, and then pass it to Valgrind:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb25-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gcc</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-g</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> out_of_bounds out_of_bounds.c</span>
<span id="cb25-2"></span>
<span id="cb25-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">valgrind</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--leak-check</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>full ./out_of_bounds</span></code></pre></div></div>
<p>Output:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb26-1">==2648121== Memcheck, a memory error detector</span>
<span id="cb26-2">==2648121== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.</span>
<span id="cb26-3">==2648121== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info</span>
<span id="cb26-4">==2648121== Command: ./out_of_bounds</span>
<span id="cb26-5">==2648121== </span>
<span id="cb26-6">==2648121== Invalid write of size 1</span>
<span id="cb26-7">==2648121==    at 0x10916B: main (out_of_bounds.c:6)</span>
<span id="cb26-8">==2648121==  Address 0x4a8506a is 26 bytes after a block of size 16 in arena "client"</span>
<span id="cb26-9">==2648121== </span>
<span id="cb26-10">==2648121== </span>
<span id="cb26-11">==2648121== HEAP SUMMARY:</span>
<span id="cb26-12">==2648121==     in use at exit: 16 bytes in 1 blocks</span>
<span id="cb26-13">==2648121==   total heap usage: 1 allocs, 0 frees, 16 bytes allocated</span>
<span id="cb26-14">==2648121== </span>
<span id="cb26-15">==2648121== 16 bytes in 1 blocks are definitely lost in loss record 1 of 1</span>
<span id="cb26-16">==2648121==    at 0x4846828: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)</span>
<span id="cb26-17">==2648121==    by 0x10915E: main (out_of_bounds.c:5)</span>
<span id="cb26-18">==2648121== </span>
<span id="cb26-19">==2648121== LEAK SUMMARY:</span>
<span id="cb26-20">==2648121==    definitely lost: 16 bytes in 1 blocks</span>
<span id="cb26-21">==2648121==    indirectly lost: 0 bytes in 0 blocks</span>
<span id="cb26-22">==2648121==      possibly lost: 0 bytes in 0 blocks</span>
<span id="cb26-23">==2648121==    still reachable: 0 bytes in 0 blocks</span>
<span id="cb26-24">==2648121==         suppressed: 0 bytes in 0 blocks</span>
<span id="cb26-25">==2648121== </span>
<span id="cb26-26">==2648121== For lists of detected and suppressed errors, rerun with: -s</span>
<span id="cb26-27">==2648121== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)</span></code></pre></div></div>
<p>Looking at the highlights, we see that both problems have been identified:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 31%">
<col style="width: 68%">
</colgroup>
<thead>
<tr class="header">
<th>Error</th>
<th>Explanation</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Address 0x4a8506a is 26 bytes after a block of size 16 in arena “client”</td>
<td>This refers to the <code>p[42] = 100</code> statement. A value is being written outside of the allocated block of memory.</td>
</tr>
<tr class="even">
<td>16 bytes in 1 blocks are definitely lost in loss record 1 of 1</td>
<td>We allocated a block of 16 bytes with <code>malloc(16)</code>, but we never freed it.</td>
</tr>
</tbody>
</table>
<p>Fix the code:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>out_of_bounds.c</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb27" data-filename="out_of_bounds.c" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb27-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;stdlib.h&gt;</span></span>
<span id="cb27-2"></span>
<span id="cb27-3"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span>
<span id="cb27-4"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb27-5">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> malloc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb27-6">    p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/* write within the allocated block */</span></span>
<span id="cb27-7">    free<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/* free the allocated block*/</span></span>
<span id="cb27-8"></span>
<span id="cb27-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb27-10"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</div>
<p>Compile and check it again:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb28-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gcc</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> out_of_bounds out_of_bounds.c</span>
<span id="cb28-2"></span>
<span id="cb28-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">valgrind</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--leak-check</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>full ./out_of_bounds</span></code></pre></div></div>
<p>Output:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb29-1">==2651821== Memcheck, a memory error detector</span>
<span id="cb29-2">==2651821== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.</span>
<span id="cb29-3">==2651821== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info</span>
<span id="cb29-4">==2651821== Command: ./out_of_bounds</span>
<span id="cb29-5">==2651821== </span>
<span id="cb29-6">==2651821== </span>
<span id="cb29-7">==2651821== HEAP SUMMARY:</span>
<span id="cb29-8">==2651821==     in use at exit: 0 bytes in 0 blocks</span>
<span id="cb29-9">==2651821==   total heap usage: 1 allocs, 1 frees, 16 bytes allocated</span>
<span id="cb29-10">==2651821== </span>
<span id="cb29-11">==2651821== All heap blocks were freed -- no leaks are possible</span>
<span id="cb29-12">==2651821== </span>
<span id="cb29-13">==2651821== For lists of detected and suppressed errors, rerun with: -s</span>
<span id="cb29-14">==2651821== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)</span></code></pre></div></div>
<p>And now, we’re good!</p>
</section>
</section>
<section id="whats-next" class="level1">
<h1>What’s Next?</h1>
<p>Some additional memory debugging topics you might want to explore:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 31%">
<col style="width: 68%">
</colgroup>
<thead>
<tr class="header">
<th>Topic</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Use after free</td>
<td>This occurs when a program continues to use a memory location after it has been freed or deallocated. This can lead to undefined behavior, crashes, or security vulnerabilities, as the memory may be reallocated for other purposes or may contain invalid data.</td>
</tr>
<tr class="even">
<td>Stack/Heap exhaustion</td>
<td>This occurs when the call stack (a special region of memory that stores information about the active subroutines of a computer program) or heap (used for dynamic memory allocation) runs out of available space.</td>
</tr>
<tr class="odd">
<td>Null pointer dereferences</td>
<td>This occurs when a program attempts to access or manipulate data through a pointer that has not been initialized or has been set to <code>null</code>.</td>
</tr>
<tr class="even">
<td>Dangling pointers</td>
<td>This describes a pointer that does not point to a valid object of the appropriate type. This situation arises when the object that the pointer was pointing to has been deleted or deallocated, but the pointer itself has not been updated to reflect this change. As a result, the pointer still holds the memory address of the deallocated object, leading to undefined behavior if accessed.</td>
</tr>
</tbody>
</table>
<p>Also, here are some additional tools to consider:</p>
<ul>
<li><strong>Splint</strong>, a tool for statically checking C programs for security vulnerabilities and coding mistakes.</li>
<li><strong>Cppcheck</strong> is a static analysis tool that can detect various issues in C/C++ code, including potential memory leaks.</li>
<li><strong>Electric Fence</strong>, a linkable library that provides a <strong>malloc</strong> debugger.</li>
<li><strong>Dr.&nbsp;Memory</strong>, a memory monitoring tool capable of identifying memory-related programming errors such as accesses of uninitialized memory, accesses to unaddressable memory (including outside of allocated heap units and heap underflow and overflow), accesses to freed memory, double frees, memory leaks, and (on Windows) handle leaks, GDI API usage errors, and accesses to un-reserved thread local storage slots.</li>
</ul>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>Non-memory safe languages provide a lot of <a href="https://en.wiktionary.org/wiki/footgun">footguns</a>, but with careful planning and judicious use of supporting tools, you can greatly improve the safety of your code.</p>


</section>

 ]]></description>
  <category>C</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2025/memory_debug_c.html</guid>
  <pubDate>Sat, 13 Sep 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>CGI Programming</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2025/cgi_programming.html</link>
  <description><![CDATA[ 






<p>In computing, <strong>Common Gateway Interface (CGI)</strong> is an interface specification that enables web servers to execute an external program to process HTTP or HTTPS user requests.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jcarr.codeberg.page/blog/posts/2025/images/cgi/cgi_logo.png" class="img-fluid figure-img"></p>
<figcaption>The official CGI logo from the spec announcement</figcaption>
</figure>
</div>
<p>The name CGI comes from the early days of the Web, where webmasters wanted to connect legacy information systems such as databases to their Web servers. The CGI program was executed by the server and provided a common “gateway” between the Web server and the legacy information system.</p>
<p>While it was once a dominant method for creating dynamic web content, modern web development has introduced various alternatives that often outperform CGI in terms of efficiency and scalability.</p>
<section id="a-brief-history-of-cgi-programming" class="level1">
<h1>A Brief History of CGI Programming</h1>
<p>In the early days of the web (early 1990s) CGI was introduced as a standard protocol for web servers to interact with executable programs. This allowed web servers to generate dynamic content by executing scripts or programs in response to user requests. The first widely used web server, <strong>NCSA HTTPd</strong>, implemented CGI, which quickly became a cornerstone of web development.</p>
<section id="language-support" class="level2">
<h2 class="anchored" data-anchor-id="language-support">Language Support</h2>
<p>Initially, CGI scripts were primarily written in C, as it was one of the most popular programming languages at the time, but many languages are supported, including:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Programming Language</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Perl</td>
<td>One of the most popular languages for CGI due to its text processing capabilities and ease of use.</td>
</tr>
<tr class="even">
<td>Python</td>
<td>Known for its readability and simplicity, Python is widely used for CGI scripting.</td>
</tr>
<tr class="odd">
<td>PHP</td>
<td>While often used as a module within web servers, PHP can also be run as a CGI script.</td>
</tr>
<tr class="even">
<td>Ruby</td>
<td>Ruby can be used for CGI, especially with frameworks like <a href="https://sinatrarb.com/">Sinatra</a>.</td>
</tr>
<tr class="odd">
<td>C/C++</td>
<td>These languages can be used for high-performance CGI applications, though they require more complex setup.</td>
</tr>
<tr class="even">
<td>Shell Scripting</td>
<td>Shell scripts can be executed as CGI scripts, particularly in Unix/Linux environments.</td>
</tr>
<tr class="odd">
<td>Java</td>
<td>Java can be used for CGI through <a href="https://en.wikipedia.org/wiki/Jakarta_Servlet">servlets</a>, though it’s more common to use Java with web frameworks.</td>
</tr>
<tr class="even">
<td>Tcl</td>
<td>Tcl is another scripting language that can be used for CGI.</td>
</tr>
</tbody>
</table>
<p>The ability to create dynamic web pages revolutionized how information was shared online, leading to the development of interactive websites and web applications.</p>
</section>
</section>
<section id="writing-cgi" class="level1">
<h1>Writing CGI</h1>
<p>Let’s explore the process of creating and utilizing some CGI programs!</p>
<p>We’ll be using Python for our local development server (it supports CGI), but in keeping with the legacy spirit, we’ll use C and Perl to create our CGI.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>This assumes a Linux environment, with C (gcc), Python 3, and Perl installed.</p>
</div>
</div>
<section id="development-server" class="level2">
<h2 class="anchored" data-anchor-id="development-server">Development Server</h2>
<p>Create a directory to hold your web page. I’ll use <strong>www_root</strong>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> www_root</span></code></pre></div></div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>The standard/default directory for CGI programs is <strong>cgi-bin</strong>.</p>
</div>
</div>
<p>Inside <strong>www_root</strong>, create a <strong>cgi-bin</strong> directory:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> www_root</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> cgi-bin</span></code></pre></div></div>
<p>Start Python’s development server, with CGI support:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">python3</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-m</span> http.server <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--cgi</span></span></code></pre></div></div>
</section>
<section id="simple-page-render" class="level2">
<h2 class="anchored" data-anchor-id="simple-page-render">Simple Page Render</h2>
<p>For our first example, we’ll render an entire page using a CGI program written in C.</p>
<p>First create the CGI source file:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>simple_cgi.c</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" data-filename="simple_cgi.c" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb4-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;stdio.h&gt;</span></span>
<span id="cb4-2"></span>
<span id="cb4-3"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb4-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Print the HTTP header</span></span>
<span id="cb4-5">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Content-Type: text/html</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb4-6"></span>
<span id="cb4-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Print the HTML content</span></span>
<span id="cb4-8">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;html&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb4-9">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;head&gt;&lt;title&gt;Simple CGI Example&lt;/title&gt;&lt;/head&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb4-10">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;body&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb4-11">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;h1&gt;Hello, World!&lt;/h1&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb4-12">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;p&gt;This is a simple CGI program written in C.&lt;/p&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb4-13">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;/body&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb4-14">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;/html&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb4-15"></span>
<span id="cb4-16">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-17"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</div>
<p>Compile it and make sure it’s executable, then copy the binary to your cgi-bin directory:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gcc</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> simple_cgi simple_cgi.c</span>
<span id="cb5-2"></span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chmod</span> 755 simple_cgi</span>
<span id="cb5-4"></span>
<span id="cb5-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cp</span> simple_cgi /path/to/project/www_root/cgi-bin</span></code></pre></div></div>
<p>Navigate to <a href="http://127.0.0.1:8000/cgi-bin/simple_cgi" class="uri">http://127.0.0.1:8000/cgi-bin/simple_cgi</a> in your browser, and you’ll see this:</p>
<p><img src="https://jcarr.codeberg.page/blog/posts/2025/images/cgi/hello_world_c.jpg" class="img-fluid"></p>
<p>To implement the same example in Perl, first create the Perl script in the cgi-bin directory:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>simple_cgi_p.pl</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" data-filename="simple_cgi_p.pl" style="background: #f1f3f5;"><pre class="sourceCode pl code-with-copy"><code class="sourceCode perl"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">#!/usr/bin/perl</span></span>
<span id="cb6-2"></span>
<span id="cb6-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">strict</span>;</span>
<span id="cb6-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">warnings</span>;</span>
<span id="cb6-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> CGI <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">qw(</span>:standard<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">)</span>; <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Include the CGI module, along with a standard set of commonly-used functions</span></span>
<span id="cb6-6"></span>
<span id="cb6-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a new CGI object</span></span>
<span id="cb6-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$cgi</span> = CGI-&gt;new;</span>
<span id="cb6-9"></span>
<span id="cb6-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Print the HTTP header</span></span>
<span id="cb6-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$cgi</span>-&gt;<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">header</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">text/html</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>);</span>
<span id="cb6-12"></span>
<span id="cb6-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Print the HTML content</span></span>
<span id="cb6-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$cgi</span>-&gt;<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">start_html</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">Simple CGI Example</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>);</span>
<span id="cb6-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$cgi</span>-&gt;<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">h1</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">Hello, World!</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>);</span>
<span id="cb6-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$cgi</span>-&gt;<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">p</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">This is a simple CGI program written in Perl.</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>);</span>
<span id="cb6-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$cgi</span>-&gt;<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">end_html</span>;</span></code></pre></div></div>
</div>
<p>Then, navigate to <a href="http://127.0.0.1:8000/cgi-bin/simple_cgi_p.pl" class="uri">http://127.0.0.1:8000/cgi-bin/simple_cgi_p.pl</a> in your browser, and you’ll see this:</p>
<p><img src="https://jcarr.codeberg.page/blog/posts/2025/images/cgi/hello_world_perl.jpg" class="img-fluid"></p>
</section>
<section id="a-more-complex-example-handling-form-posting" class="level2">
<h2 class="anchored" data-anchor-id="a-more-complex-example-handling-form-posting">A More Complex Example: Handling Form Posting</h2>
<p>A prevalent application of CGI is to capture the data from web forms, which can be used for purposes such as email submissions and storing information in a database.</p>
<p>First, create the source file for the CGI:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>form_capture.c</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" data-filename="form_capture.c" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb7-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;stdio.h&gt;</span></span>
<span id="cb7-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;stdlib.h&gt;</span></span>
<span id="cb7-3"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;string.h&gt;</span></span>
<span id="cb7-4"></span>
<span id="cb7-5"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#define MAX_DATA_SIZE </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1024</span></span>
<span id="cb7-6"></span>
<span id="cb7-7"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Print the HTTP header</span></span>
<span id="cb7-9">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Content-Type: text/html</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-10"></span>
<span id="cb7-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Buffer to hold the input data</span></span>
<span id="cb7-12">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> getenv<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CONTENT_LENGTH"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-13">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> content_length <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> atoi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-14"></span>
<span id="cb7-15">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Check if content length is valid</span></span>
<span id="cb7-16">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>content_length <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> content_length <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> MAX_DATA_SIZE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-17">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> input_data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAX_DATA_SIZE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb7-18">        fread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>input_data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> content_length<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> stdin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-19">        input_data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>content_length<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\0</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Null-terminate the string</span></span>
<span id="cb7-20"></span>
<span id="cb7-21">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Print the HTML response</span></span>
<span id="cb7-22">        printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;html&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-23">        printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;head&gt;&lt;title&gt;Form Data Received&lt;/title&gt;&lt;/head&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-24">        printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;body&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-25">        printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;h1&gt;Form Data Received&lt;/h1&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-26">        printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;p&gt;Your input:&lt;/p&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-27">        printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;pre&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%s</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/pre&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> input_data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-28">        printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;/body&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-29">        printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;/html&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-30">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-31">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Handle error for invalid content length</span></span>
<span id="cb7-32">        printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;html&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-33">        printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;head&gt;&lt;title&gt;Error&lt;/title&gt;&lt;/head&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-34">        printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;body&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-35">        printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;h1&gt;Error&lt;/h1&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-36">        printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;p&gt;Invalid input data.&lt;/p&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-37">        printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;/body&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-38">        printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;/html&gt;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-39">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-40"></span>
<span id="cb7-41">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-42"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</div>
<p>Compile it and make it executable, then copy the binary to your cgi-bin directory:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gcc</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> form_capture form_capture.c</span>
<span id="cb8-2"></span>
<span id="cb8-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chmod</span> 755 form_capture</span>
<span id="cb8-4"></span>
<span id="cb8-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cp</span> form_capture /path/to/project/www_root/cgi-bin</span></code></pre></div></div>
<p>In <strong>www_root</strong>, create an HTML file for the form:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>form.html</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" data-filename="form.html" style="background: #f1f3f5;"><pre class="sourceCode html code-with-copy"><code class="sourceCode html"><span id="cb9-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;!DOCTYPE</span> html<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb9-2"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">html</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb9-3"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">head</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb9-4">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">title</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>Simple Form<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">title</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb9-5"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">head</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb9-6"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">body</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb9-7">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">h1</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>Submit Your Data<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">h1</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb9-8">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">form</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> action</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/cgi-bin/form_capture"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> method</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"post"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb9-9">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">label</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>Enter some data:<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">label</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">br</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb9-10">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">input</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> name</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">br</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">br</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb9-11">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">input</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"submit"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> value</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Submit"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb9-12">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">form</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb9-13"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">body</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb9-14"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">html</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span></code></pre></div></div>
</div>
<p>Navigate to <a href="http://127.0.0.1:8000/form.html" class="uri">http://127.0.0.1:8000/form.html</a> and enter some data:</p>
<p><img src="https://jcarr.codeberg.page/blog/posts/2025/images/cgi/form.jpg" class="img-fluid"></p>
<p>Click “Submit”, and you’ll see this:</p>
<p><img src="https://jcarr.codeberg.page/blog/posts/2025/images/cgi/form_result.jpg" class="img-fluid"></p>
<p>Accomplishing the same thing in Perl is a bit more straightforward. In <strong>cgi-bin</strong>, create your form capture CGI script (Perl version):</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>form_capture_p.pl</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" data-filename="form_capture_p.pl" style="background: #f1f3f5;"><pre class="sourceCode pl code-with-copy"><code class="sourceCode perl"><span id="cb10-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">#!/usr/bin/perl</span></span>
<span id="cb10-2"></span>
<span id="cb10-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">strict</span>;</span>
<span id="cb10-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">warnings</span>;</span>
<span id="cb10-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> CGI <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">qw(</span>:standard<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">)</span>;</span>
<span id="cb10-6"></span>
<span id="cb10-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a new CGI object</span></span>
<span id="cb10-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$cgi</span> = CGI-&gt;new;</span>
<span id="cb10-9"></span>
<span id="cb10-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Print the HTTP header</span></span>
<span id="cb10-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$cgi</span>-&gt;<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">header</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">text/html</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>);</span>
<span id="cb10-12"></span>
<span id="cb10-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get the form data</span></span>
<span id="cb10-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$data</span> = <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$cgi</span>-&gt;<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">param</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">data</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>);</span>
<span id="cb10-15"></span>
<span id="cb10-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Print the HTML response</span></span>
<span id="cb10-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$cgi</span>-&gt;<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">start_html</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">Form Data Received</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>);</span>
<span id="cb10-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$cgi</span>-&gt;<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">h1</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">Form Data Received</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>);</span>
<span id="cb10-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$cgi</span>-&gt;<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">p</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">Your input:</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>);</span>
<span id="cb10-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$cgi</span>-&gt;<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">pre</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$data</span> // <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">No data received</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>);  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Display data or a message if no data</span></span>
<span id="cb10-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$cgi</span>-&gt;<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">end_html</span>;</span></code></pre></div></div>
</div>
<p>Make the script executable:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chmod</span> 755 form_capture_p.pl</span></code></pre></div></div>
<p>Then, update the action attribute in form.html to use the Perl script instead:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode html code-with-copy"><code class="sourceCode html"><span id="cb12-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">form</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> action</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/cgi-bin/form_capture_p.pl"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> method</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"post"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span></code></pre></div></div>
<p>Return to your browser, refresh <a href="http://127.0.0.1:8000/form.html" class="uri">http://127.0.0.1:8000/form.html</a>, enter some data, and click “Submit”. You’ll see similar results.</p>
</section>
</section>
<section id="the-waning-popularity-of-cgi" class="level1">
<h1>The Waning Popularity of CGI</h1>
<p>Despite its historical significance, CGI programming has seen a decline in popularity for several reasons:</p>
<ul>
<li><strong>Performance Issues</strong>: CGI creates a new process for each request, which can lead to significant overhead and slower response times, especially under heavy load.</li>
<li><strong>Emergence of New Technologies</strong>: Frameworks and technologies such as <strong>FastCGI</strong>, <strong>mod_perl</strong>, and <strong>ASP.NET</strong> have emerged, offering better performance and more features. These alternatives allow for persistent processes, reducing the overhead associated with CGI.</li>
<li><strong>Rise of Modern Web Frameworks</strong>: Languages like <strong>JavaScript</strong> (with Node.js), <strong>Ruby on Rails</strong>, and <strong>Django</strong> have gained traction, providing developers with more powerful tools for building dynamic web applications. These frameworks often come with built-in features for routing, templating, and database interaction, making them more efficient than traditional CGI.</li>
<li><strong>Shift to Client-Side Rendering</strong>: The rise of single-page applications (SPAs) and client-side frameworks like <strong>React</strong> and <strong>Angular</strong> has shifted the focus from server-side scripting to client-side rendering, further diminishing the role of CGI.</li>
</ul>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>CGI programming laid the groundwork for dynamic web content and played a crucial role in the early days of the internet. While it has largely been replaced by more efficient and modern technologies, understanding its history and functionality provides valuable context for the evolution of web development. As we continue to innovate and improve web technologies, the lessons learned from CGI will remain relevant in shaping the future of web applications.</p>


</section>

 ]]></description>
  <category>Retro</category>
  <category>C</category>
  <category>Perl</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2025/cgi_programming.html</guid>
  <pubDate>Fri, 05 Sep 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>The Dark Forest</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2025/the_dark_forest.html</link>
  <description><![CDATA[ 






<p>The <strong>Dark Forest Theory</strong> is a fascinating concept that delves into the reasons behind the apparent silence of the universe. It suggests that the cosmos is akin to a dark forest, where every civilization is a silent hunter, hiding from others to avoid potential threats.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jcarr.codeberg.page/blog/posts/2025/images/dark_forest.jpg" class="img-fluid figure-img"></p>
<figcaption>A dark forest</figcaption>
</figure>
</div>
<section id="the-fermi-paradox" class="level2">
<h2 class="anchored" data-anchor-id="the-fermi-paradox">The Fermi Paradox</h2>
<p>The Fermi Paradox refers to the apparent contradiction between the high probability of extraterrestrial life in the universe and the lack of evidence or contact with such civilizations. Named after physicist Enrico Fermi, the paradox raises the question: “If the universe is so vast and old, where is everybody?”</p>
<section id="key-points-of-the-fermi-paradox" class="level3">
<h3 class="anchored" data-anchor-id="key-points-of-the-fermi-paradox">Key Points of the Fermi Paradox</h3>
<ul>
<li><strong>Vastness of the Universe</strong>: The universe contains billions of galaxies, each with billions of stars, many of which have planets in the habitable zone where life could potentially exist.</li>
<li><strong>Age of the Universe</strong>: The universe is approximately 13.8 billion years old, providing ample time for intelligent life to develop and potentially become advanced enough to communicate or travel across space.</li>
<li><strong>Drake Equation</strong>: This equation estimates the number of active, communicative extraterrestrial civilizations in the Milky Way galaxy. While the equation suggests a high number of potential civilizations, we have yet to find any.</li>
<li><strong>Lack of Evidence</strong>: Despite extensive searches, such as the <strong>SETI (Search for Extraterrestrial Intelligence)</strong> program, we have not detected any signals or signs of intelligent life.</li>
</ul>
</section>
<section id="possible-explanations-for-the-paradox" class="level3">
<h3 class="anchored" data-anchor-id="possible-explanations-for-the-paradox">Possible Explanations for the Paradox</h3>
<p>The <strong>Dark Forest Theory</strong> offers a possible explanation for the Fermi Paradox. It is one of several:</p>
<ul>
<li><strong>Rare Earth Hypothesis</strong>: Suggests that the conditions for life are extremely rare, making Earth an exceptional case.</li>
<li><strong>Great Filter</strong>: Proposes that there is a stage in the evolution of life that is extremely hard to surpass, which could be behind us (e.g., the emergence of intelligent life) or ahead of us (e.g., self-destruction).</li>
<li><strong>Technological Limitations</strong>: Advanced civilizations may use technologies that are beyond our current understanding, making their signals undetectable.</li>
<li><strong>Self-Destruction</strong>: Civilizations may tend to self-destruct before they can establish contact with others, due to wars, environmental collapse, or other catastrophic events.</li>
<li><strong>Non-Interference</strong>: Advanced civilizations might choose not to contact us, adhering to a principle of non-interference with less developed societies.</li>
<li><strong>Transient Civilizations</strong>: Civilizations may be short-lived, arising and falling before they can communicate with others.</li>
</ul>
</section>
</section>
<section id="understanding-the-dark-forest-theory" class="level2">
<h2 class="anchored" data-anchor-id="understanding-the-dark-forest-theory">Understanding the Dark Forest Theory</h2>
<p>The Dark Forest Theory was popularized by the Chinese science fiction author <strong>Liu Cixin</strong> in his novel “The Dark Forest,” which is the second book in the “Three-Body Problem” trilogy. The theory posits that:</p>
<ul>
<li><strong>Civilizations are like hunters</strong>: In a dark forest, every civilization is a hunter trying to survive. They must remain silent to avoid detection by others who may pose a threat.</li>
<li><strong>The universe is dangerous</strong>: The theory suggests that any civilization that reveals its location risks being destroyed by more advanced civilizations. This creates a paradox where civilizations choose to remain quiet rather than risk annihilation.</li>
<li><strong>Communication is risky</strong>: Sending signals into space could attract unwanted attention, leading to a preemptive strike from a more advanced civilization.</li>
</ul>
</section>
<section id="implications-of-the-dark-forest-theory" class="level2">
<h2 class="anchored" data-anchor-id="implications-of-the-dark-forest-theory">Implications of the Dark Forest Theory</h2>
<ul>
<li><strong>Fermi Paradox</strong>: As I mentioned above, the Dark Forest Theory offers a potential solution to the Fermi Paradox, which questions why we have not yet encountered extraterrestrial life despite the vastness of the universe. If civilizations are deliberately silent, it explains the lack of contact.</li>
<li><strong>Humanity’s Future</strong>: As humanity continues to explore space, the theory raises ethical questions about our own communication efforts. Should we broadcast our presence, or is silence the safer option?</li>
<li><strong>Existential Risks</strong>: The theory highlights the existential risks associated with advanced technology. As civilizations develop, the potential for conflict increases, leading to a cycle of fear and silence.</li>
</ul>
</section>
<section id="critiques-and-counterarguments" class="level2">
<h2 class="anchored" data-anchor-id="critiques-and-counterarguments">Critiques and Counterarguments</h2>
<p>While the Dark Forest Theory is compelling, it is not without its critiques:</p>
<ul>
<li><strong>Optimism about Cooperation</strong>: Some argue that civilizations may be more inclined to cooperate rather than compete. The potential for mutual benefit could encourage communication rather than silence.</li>
<li><strong>Technological Limitations</strong>: The theory assumes that all civilizations are technologically advanced enough to pose a threat. However, many civilizations may be at different stages of development, leading to a more complex interaction landscape.</li>
<li><strong>The Nature of Life</strong>: The theory is based on human assumptions about survival and competition. Other forms of life may have different motivations and behaviors that do not align with the hunter-prey dynamic.</li>
</ul>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>The Dark Forest Theory invites us to ponder the nature of existence and the potential for life beyond Earth. It challenges our understanding of communication, survival, and the universe itself. As we continue to explore the cosmos, the questions raised by this theory will remain relevant, urging us to consider the implications of our actions in the vast, dark forest of the universe.</p>


</section>

 ]]></description>
  <category>Astronomy</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2025/the_dark_forest.html</guid>
  <pubDate>Tue, 02 Sep 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Evaluating Zig</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2025/evaluating_zig.html</link>
  <description><![CDATA[ 






<p>The results of the 2025 Stack Overflow Developer Survey were <a href="https://survey.stackoverflow.co/2025/">recently published</a> and I noticed that <a href="https://survey.stackoverflow.co/2025/technology#admired-and-desired-language-desire-admire">interest in the Zig programming language is rising</a>. I’d heard of Zig, but knew nothing about the details.</p>
<p>Let’s check it out!</p>
<section id="what-is-zig" class="level1">
<h1>What is Zig?</h1>
<p><img src="https://jcarr.codeberg.page/blog/posts/2025/images/zig-logo.png" class="img-fluid"></p>
<p>A summary from the <a href="https://ziglang.org">Zig home page</a> and other sources:</p>
<p>Zig is a general-purpose programming language that emphasizes <strong>performance</strong>, <strong>safety</strong>, and <strong>maintainability</strong>. It was created by Andrew Kelley and has gained traction for its ability to provide low-level control over system resources while maintaining a modern syntax and features that enhance developer productivity. Zig is particularly well-suited for systems programming, game development, and applications where performance is critical.</p>
<div class="quarto-video ratio ratio-16x9"><iframe data-external="1" src="https://www.youtube.com/embed/Gv2I7qTux7g" title="" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div>
<section id="key-features-of-zig" class="level2">
<h2 class="anchored" data-anchor-id="key-features-of-zig">Key Features of Zig</h2>
<ul>
<li><strong>Manual Memory Management</strong>: Zig allows developers to manage memory manually, similar to C, but with added safety features. This gives developers fine-grained control over memory allocation and deallocation, which is essential for performance-critical applications.</li>
<li><strong>Error Handling</strong>: Zig introduces a unique approach to error handling that avoids exceptions and instead uses a simple return value mechanism. This makes it easier to reason about error states and ensures that errors are handled explicitly.</li>
<li><strong>Cross-Compilation</strong>: Zig is designed with cross-compilation in mind, making it easy to build applications for different platforms from a single codebase. This is particularly useful for developers targeting multiple operating systems or architectures.</li>
<li><strong>Interoperability with C</strong>: Zig can directly call C functions and use C libraries without the need for wrappers or bindings. This makes it easy to integrate Zig into existing C projects or leverage existing C libraries.</li>
</ul>
</section>
<section id="why-choose-zig" class="level2">
<h2 class="anchored" data-anchor-id="why-choose-zig">Why Choose Zig?</h2>
<p>Zig stands out in a crowded field of programming languages for several reasons:</p>
<ul>
<li><strong>Performance</strong>: With its low-level capabilities and manual memory management, Zig is designed for high-performance applications, making it an excellent choice for systems programming and game development.</li>
<li><strong>Simplicity</strong>: Zig’s syntax is clean and straightforward, making it accessible for new developers while still providing the power needed for experienced programmers.</li>
<li><strong>Safety</strong>: The language’s focus on safety features, such as compile-time checks and explicit error handling, helps prevent common programming errors that can lead to crashes or security vulnerabilities.</li>
<li><strong>Active Community</strong>: Zig has a growing community of developers who contribute to its development and provide support through forums, documentation, and tutorials.</li>
</ul>
</section>
</section>
<section id="installation-direct-download" class="level1">
<h1>Installation (Direct Download)</h1>
<ol type="1">
<li>Download the latest version from <a href="https://ziglang.org/download/" class="uri">https://ziglang.org/download/</a></li>
<li>Extract and move to <strong>~/bin/zig/</strong></li>
<li>Add Zig to the PATH in .profile: <code>export PATH=$PATH:~/bin/zig</code></li>
</ol>
<p>Test the compiler:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> hello-world</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> hello-world</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">zig</span> init</span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">zig</span> build run</span></code></pre></div></div>
<p>Output:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb2-1">All your codebase are belong to us.</span>
<span id="cb2-2">Run `zig build test` to run the tests.</span></code></pre></div></div>
</section>
<section id="vs-code" class="level1">
<h1>VS Code</h1>
<p>Install the <a href="https://marketplace.visualstudio.com/items?itemName=ziglang.vscode-zig">Zig Language extension</a>. It includes a code runner, code debugger, and test runners.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>Install the extension (along with any other supporting extensions) inside a separate profile dedicated solely to Zig development.</p>
<p>If you aren’t already using profiles in VS Code, I <em>highly</em> recommend it. Profiles make it easy to construct curated development environments and are also great for evaluating new extensions.</p>
</div>
</div>
</section>
<section id="get-started" class="level1">
<h1>Get Started</h1>
<section id="hello" class="level2">
<h2 class="anchored" data-anchor-id="hello">Hello!</h2>
<p>Let’s start with the simplest possible example:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>hello.zig</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" data-filename="hello.zig" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb3-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> std <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">@import</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"std"</span>);</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> {</span>
<span id="cb3-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> stdout <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>io<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>getStdOut()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>writer();</span>
<span id="cb3-5"></span>
<span id="cb3-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span> stdout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello, World!</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>{});</span>
<span id="cb3-7">}</span></code></pre></div></div>
</div>
<p>Since this is a standalone source file (not a project), we use <strong>build-exe</strong> instead of <strong>build</strong>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">zig</span> build-exe hello.zig</span></code></pre></div></div>
<p>This produces two files:</p>
<ul>
<li><strong>hello.o</strong> (object file), and</li>
<li><strong>hello</strong> (executable)</li>
</ul>
</section>
<section id="code-breakdown" class="level2">
<h2 class="anchored" data-anchor-id="code-breakdown">Code Breakdown</h2>
<section id="importing-the-standard-library" class="level3">
<h3 class="anchored" data-anchor-id="importing-the-standard-library">Importing the Standard Library</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb5-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> std <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">@import</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"std"</span>);</span></code></pre></div></div>
<p>This line imports the standard library of Zig, which provides various functionalities, including input/output operations, memory management, and more. The <code>std</code> constant will be used to access these functionalities.</p>
</section>
<section id="main-function-definition" class="level3">
<h3 class="anchored" data-anchor-id="main-function-definition">Main Function Definition</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> {</span></code></pre></div></div>
<p>This line defines the <code>main</code> function, which is the entry point of the program. The <code>pub</code> keyword makes it public, allowing it to be called from outside the module. The <code>!void</code> return type indicates that the function can return an error (denoted by <code>!</code>) or nothing (<code>void</code>).</p>
</section>
<section id="getting-the-standard-output-writer" class="level3">
<h3 class="anchored" data-anchor-id="getting-the-standard-output-writer">Getting the Standard Output Writer</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb7-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> stdout <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>io<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>getStdOut()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>writer();</span></code></pre></div></div>
<p>Here, the program retrieves the standard output stream using <code>std.io.getStdOut()</code>. The <code>writer()</code> method is called on this output stream to get a writer object that can be used to print text to the console.</p>
</section>
<section id="printing-to-standard-output" class="level3">
<h3 class="anchored" data-anchor-id="printing-to-standard-output">Printing to Standard Output</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb8-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span> stdout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello, World!</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>{});</span></code></pre></div></div>
<p>This line attempts to print the string “Hello, World!” followed by a newline character (<code>\n</code>) to the standard output. The <code>try</code> keyword is used to handle any potential errors that may occur during the printing process. If an error occurs, it will propagate up to the caller of <code>main</code>.</p>
</section>
</section>
</section>
<section id="performance-and-safety" class="level1">
<h1>Performance and Safety</h1>
<p>Zig has four <a href="https://ziglang.org/documentation/master/#Build-Mode">build modes</a>:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 18%">
<col style="width: 10%">
<col style="width: 22%">
<col style="width: 22%">
<col style="width: 25%">
</colgroup>
<thead>
<tr class="header">
<th>Parameter</th>
<th>Debug</th>
<th>ReleaseSafe</th>
<th>ReleaseFast</th>
<th>ReleaseSmall</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Optimizations - improve speed, harm debugging, harm compile time</td>
<td></td>
<td>On</td>
<td>On</td>
<td>On</td>
</tr>
<tr class="even">
<td>Runtime Safety Checks - harm speed, harm size, crash instead of undefined behavior</td>
<td>On</td>
<td>On</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p>Regardless of the build mode, here’s what an integer overflow looks like at compile time:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>overflow.zig</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" data-filename="overflow.zig" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb9-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> {</span>
<span id="cb9-2">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u8</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>;</span>
<span id="cb9-3"></span>
<span id="cb9-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// The underscore indicates that the value is unused:</span></span>
<span id="cb9-5">    _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>;</span>
<span id="cb9-6">}</span></code></pre></div></div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb10-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">zig</span> build-exe overflow.zig</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb11-1">overflow.zig:4:11: error: overflow of integer type 'u8' with value '256'</span>
<span id="cb11-2">    _ = x + 1;</span>
<span id="cb11-3">        ~~^~~</span></code></pre></div></div>
<p>Zig also has a <code>test</code> keyword that can be used to define unit tests:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>overflow.zig</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" data-filename="overflow.zig" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb12-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">test</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"integer overflow at compile time"</span> {</span>
<span id="cb12-2">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u8</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>;</span>
<span id="cb12-3"></span>
<span id="cb12-4">    _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>;</span>
<span id="cb12-5">}</span></code></pre></div></div>
</div>
<p>With <code>zig test</code>, zig will locate all unit tests and execute them:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb13-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">zig</span> test overflow.zig </span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb14-1">overflow.zig:4:11: error: overflow of integer type 'u8' with value '256'</span>
<span id="cb14-2">    _ = x + 1;</span>
<span id="cb14-3">        ~~^~~</span></code></pre></div></div>
</section>
<section id="manual-memory-management" class="level1">
<h1>Manual Memory Management</h1>
<p>Manual memory management in Zig is a key feature that allows developers to have fine-grained control over memory allocation and deallocation. Unlike languages with automatic garbage collection, Zig requires programmers to explicitly manage memory.</p>
<p>Here’s an example:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>allocator.zig</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" data-filename="allocator.zig" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb15-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> std <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">@import</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"std"</span>);</span>
<span id="cb15-2"></span>
<span id="cb15-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> {</span>
<span id="cb15-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> allocator <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>heap<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>page_allocator;</span>
<span id="cb15-5"></span>
<span id="cb15-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var</span> number <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span> allocator<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>alloc(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>);</span>
<span id="cb15-7"></span>
<span id="cb15-8">    number[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>;</span>
<span id="cb15-9"></span>
<span id="cb15-10">    std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>debug<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The value is: {}</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>{number[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]});</span>
<span id="cb15-11"></span>
<span id="cb15-12">    allocator<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>free(number);</span>
<span id="cb15-13">}</span></code></pre></div></div>
</div>
<p>Output:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb16-1">The value is: 42</span></code></pre></div></div>
<section id="code-breakdown-1" class="level2">
<h2 class="anchored" data-anchor-id="code-breakdown-1">Code Breakdown</h2>
<section id="importing-standard-library" class="level3">
<h3 class="anchored" data-anchor-id="importing-standard-library">Importing Standard Library</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb17-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> std <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">@import</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"std"</span>);</span></code></pre></div></div>
<p>This line imports the standard library, allowing access to various utilities, including memory management functions.</p>
</section>
<section id="main-function" class="level3">
<h3 class="anchored" data-anchor-id="main-function">Main Function</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb18-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> {</span></code></pre></div></div>
<p>The <code>main</code> function is the entry point of the program. The <code>!void</code> return type indicates that the function can return an error.</p>
</section>
<section id="allocating-memory" class="level3">
<h3 class="anchored" data-anchor-id="allocating-memory">Allocating Memory</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb19-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> allocator <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>heap<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>page_allocator;</span></code></pre></div></div>
<p>Here, the code defines <code>allocator</code> as the page allocator from the standard library, which is used for allocating memory.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb20-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var</span> number <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span> allocator<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>alloc(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>);</span></code></pre></div></div>
<p>This line allocates memory for a single integer (<code>i32</code>). The <code>try</code> keyword is used to handle any potential errors that may occur during allocation. If the allocation fails, the function will return an error.</p>
</section>
<section id="setting-and-printing-the-value" class="level3">
<h3 class="anchored" data-anchor-id="setting-and-printing-the-value">Setting and Printing the Value</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb21-1">number[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>;</span></code></pre></div></div>
<p>After successfully allocating memory, this line sets the value of the allocated integer to <strong>42</strong>. The allocated memory is treated as an array, so <code>number[0]</code> accesses the first (and only) element.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb22-1">std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>debug<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The value is: {}</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>{number[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]});</span></code></pre></div></div>
<p>This line prints the value of the integer to the console. The <code>{}</code> is a placeholder for the value, which is provided in the second argument as a tuple.</p>
</section>
<section id="deallocating-memory" class="level3">
<h3 class="anchored" data-anchor-id="deallocating-memory">Deallocating Memory</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb23-1">allocator<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>free(number);</span></code></pre></div></div>
<p>Finally, this line deallocates the memory that was previously allocated for the integer.</p>
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Important
</div>
</div>
<div class="callout-body-container callout-body">
<p>With great power comes great responsibility!</p>
<p>It’s important to free allocated memory to prevent memory leaks. Explicit allocation and deallocation makes it easy to see exactly what the code’s doing, but this places the burden of responsible memory management on you!</p>
</div>
</div>
</section>
</section>
</section>
<section id="object-orientation" class="level1">
<h1>Object Orientation</h1>
<p>In Zig, classes as found in object-oriented languages like C++ or Java do not exist. Instead, Zig uses a combination of structs and functions to achieve similar functionality. (This is very similar to Rust.)</p>
<p>You can define a struct to hold data and then create functions that operate on that data, effectively mimicking class behavior:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>struct.zig</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb24" data-filename="struct.zig" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb24-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> std <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">@import</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"std"</span>);</span>
<span id="cb24-2"></span>
<span id="cb24-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> MyClass <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">struct</span> {</span>
<span id="cb24-4">    value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb24-5"></span>
<span id="cb24-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> new(value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span>) MyClass {</span>
<span id="cb24-7">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> MyClass{ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> value };</span>
<span id="cb24-8">    }</span>
<span id="cb24-9"></span>
<span id="cb24-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> increment(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>MyClass) <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> {</span>
<span id="cb24-11">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>;</span>
<span id="cb24-12">    }</span>
<span id="cb24-13"></span>
<span id="cb24-14">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> getValue(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>MyClass) <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span> {</span>
<span id="cb24-15">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>value;</span>
<span id="cb24-16">    }</span>
<span id="cb24-17">};</span>
<span id="cb24-18"></span>
<span id="cb24-19"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> {</span>
<span id="cb24-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var</span> myObject <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> MyClass<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>new(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>);</span>
<span id="cb24-21">    myObject<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>increment();</span>
<span id="cb24-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> currentValue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> myObject<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>getValue();</span>
<span id="cb24-23">    std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>debug<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Current Value: {}</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>{currentValue});</span>
<span id="cb24-24">}</span></code></pre></div></div>
</div>
<p>Output:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb25-1">Current Value: 11</span></code></pre></div></div>
<section id="code-breakdown-2" class="level2">
<h2 class="anchored" data-anchor-id="code-breakdown-2">Code Breakdown</h2>
<section id="defining-myclass" class="level3">
<h3 class="anchored" data-anchor-id="defining-myclass">Defining MyClass</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb26-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> MyClass <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">struct</span> {</span>
<span id="cb26-2">    value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span></code></pre></div></div>
<p>Here, <code>MyClass</code> is defined as a public structure (<code>struct</code>) that contains a single field, <code>value</code>, which is of type <code>i32</code> (32-bit integer).</p>
</section>
<section id="constructor-method" class="level3">
<h3 class="anchored" data-anchor-id="constructor-method">Constructor Method</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb27-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> new(value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span>) MyClass {</span>
<span id="cb27-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> MyClass{ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> value };</span>
<span id="cb27-3">}</span></code></pre></div></div>
<ul>
<li>This is a public function <code>new</code> that acts as a constructor for <code>MyClass</code>.</li>
<li>It takes an <code>i32</code> parameter <code>value</code> and returns a new instance of <code>MyClass</code> initialized with that value.</li>
</ul>
</section>
<section id="increment-method" class="level3">
<h3 class="anchored" data-anchor-id="increment-method">Increment Method</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb28-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> increment(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>MyClass) <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> {</span>
<span id="cb28-2">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>;</span>
<span id="cb28-3">}</span></code></pre></div></div>
<ul>
<li>The <code>increment</code> function is a public method that takes a mutable pointer to <code>MyClass</code> (<code>self: *MyClass</code>).</li>
<li>It increments the <code>value</code> field of the instance by 1.</li>
</ul>
</section>
<section id="getter-method" class="level3">
<h3 class="anchored" data-anchor-id="getter-method">Getter Method</h3>
<pre><code>pub fn getValue(self: *MyClass) i32 {
    return self.value;
}</code></pre>
<ul>
<li>The <code>getValue</code> function is a public method that returns the current value of the <code>value</code> field.</li>
<li>It also takes a mutable pointer to <code>MyClass</code>.</li>
</ul>
</section>
<section id="main-function-1" class="level3">
<h3 class="anchored" data-anchor-id="main-function-1">Main Function</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb30-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> {</span>
<span id="cb30-2">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var</span> myObject <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> MyClass<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>new(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>);</span>
<span id="cb30-3">    myObject<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>increment();</span>
<span id="cb30-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> currentValue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> myObject<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>getValue();</span>
<span id="cb30-5">    std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>debug<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Current Value: {}</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>{currentValue});</span>
<span id="cb30-6">}</span></code></pre></div></div>
<ul>
<li>The <code>main</code> function is the entry point of the program.</li>
<li>It creates an instance of <code>MyClass</code> called <code>myObject</code>, initialized with the value <code>10</code>.</li>
<li>It calls the <code>increment</code> method on <code>myObject</code>, which increases its <code>value</code> from <code>10</code> to <code>11</code>.</li>
<li>Finally, it retrieves the current value using <code>getValue</code> and prints it to the console using <code>std.debug.print</code>.</li>
</ul>
</section>
</section>
</section>
<section id="cross-compilation" class="level1">
<h1>Cross-Compilation</h1>
<p>Returning to our “Hello World” program:</p>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>hello.zig</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb31" data-filename="hello.zig" style="background: #f1f3f5;"><pre class="sourceCode zig code-with-copy"><code class="sourceCode zig"><span id="cb31-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> std <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">@import</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"std"</span>);</span>
<span id="cb31-2"></span>
<span id="cb31-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> {</span>
<span id="cb31-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> stdout <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>io<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>getStdOut()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>writer();</span>
<span id="cb31-5"></span>
<span id="cb31-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span> stdout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello, World!</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>{});</span>
<span id="cb31-7">}</span></code></pre></div></div>
</div>
<p>Since I’m running in Linux, building without specifying a target produces a Linux binary:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb32-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">zig</span> build-exe hello.zig</span>
<span id="cb32-2"></span>
<span id="cb32-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file</span> hello</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb33-1">hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, with debug_info, not stripped</span></code></pre></div></div>
<p>But, we can easily target other architectures:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb34-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">zig</span> build-exe hello.zig <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-target</span> x86_64-windows</span>
<span id="cb34-2"></span>
<span id="cb34-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file</span> hello.exe</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb35-1">hello.exe: PE32+ executable (console) x86-64, for MS Windows, 7 sections</span></code></pre></div></div>
</section>
<section id="weaknesses-and-limitations" class="level1">
<h1>Weaknesses and Limitations</h1>
<p>Zig is a fairly new language. I expect that many of these issues will be resolved or improved over time. But, for the time being, there are quite a few issues to consider.</p>
<section id="limited-ecosystem-and-libraries" class="level2">
<h2 class="anchored" data-anchor-id="limited-ecosystem-and-libraries">Limited Ecosystem and Libraries</h2>
<ul>
<li><strong>Fewer Libraries</strong>: Compared to more established languages like Python or Java, Zig has a smaller ecosystem of libraries and frameworks. This can make it challenging to find pre-built solutions for common problems.</li>
<li><strong>Community Size</strong>: The community is still growing, which means fewer resources, tutorials, and community support compared to more mature languages.</li>
</ul>
</section>
<section id="learning-curve" class="level2">
<h2 class="anchored" data-anchor-id="learning-curve">Learning Curve</h2>
<ul>
<li><strong>Syntax and Concepts</strong>: While Zig aims for simplicity, its syntax and concepts may be unfamiliar to developers coming from other languages, particularly those used to garbage-collected languages.</li>
<li><strong>Manual Memory Management</strong>: Zig requires developers to manage memory manually, which can lead to increased complexity and potential for errors, especially for those not accustomed to this approach.</li>
</ul>
</section>
<section id="tooling-and-ide-support" class="level2">
<h2 class="anchored" data-anchor-id="tooling-and-ide-support">Tooling and IDE Support</h2>
<ul>
<li><strong>Limited IDE Support</strong>: The tooling and IDE support for Zig are not as robust as those for more established languages. This can affect productivity, especially for developers who rely heavily on integrated development environments. (The Zig extension in VS Code does provide a decent experience, though.)</li>
<li><strong>Build System</strong>: Zig’s build system is still evolving, and while it offers unique features, it may not be as mature or user-friendly as those found in other languages.</li>
</ul>
</section>
</section>
<section id="summary" class="level1">
<h1>Summary</h1>
<p>I’m not sure it’s ready for “prime time”, but it’s definitely an interesting language and ecosystem. I’ll be thinking about some small starter projects I can use to get more familiar with it and test its limitations.</p>


</section>

 ]]></description>
  <category>Zig</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2025/evaluating_zig.html</guid>
  <pubDate>Sat, 16 Aug 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Learning Fortran</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2025/learning_fortran.html</link>
  <description><![CDATA[ 






<p>I recently caught up with an old friend who asked about the personal projects I’ve been involved in. I shared that I’ve been working on my <a href="https://github.com/stars/jfcarr/lists/practical-astronomy">Practical Astronomy language ports</a> and recently completed C++, PHP, and Java. I expressed that I felt I had wrapped up that project since there weren’t any other languages I was eager to port. However, I jokingly suggested that “maybe I should think about Fortran.”</p>
<p>I kept pondering that idea and eventually thought, “Why not?”</p>
<section id="history" class="level1">
<h1>History</h1>
<p><img src="https://jcarr.codeberg.page/blog/posts/2025/images/fortran-acs-cover.jpg" class="img-fluid"></p>
<p>Fortran is one of the oldest programming languages, with a first proposal being submitted by John Backus in 1953, and the first compiler delivered in 1957.</p>
<p>Fortran has had <a href="https://en.wikipedia.org/wiki/Fortran#Evolution">many major revisions</a>, including:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 24%">
<col style="width: 27%">
<col style="width: 48%">
</colgroup>
<thead>
<tr class="header">
<th>Version</th>
<th>Released</th>
<th>Major features</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Fortran II</td>
<td>1958</td>
<td>Procedural programming (subroutines and functions)</td>
</tr>
<tr class="even">
<td>Fortran IV</td>
<td>1962</td>
<td>Removal of machine-dependent features; added boolean expressions; added logical IF statement</td>
</tr>
<tr class="odd">
<td>Fortran 66</td>
<td>1966</td>
<td>Standardization</td>
</tr>
<tr class="even">
<td>Fortran 77</td>
<td>1978</td>
<td>IF / END IF blocks; DO loops; direct-access file I/O</td>
</tr>
<tr class="odd">
<td>Fortran 90</td>
<td>1991</td>
<td>Free-form source input; lowercase keywords; inline comments</td>
</tr>
<tr class="even">
<td>Fortran 95</td>
<td>1997</td>
<td>FORALL and nested WHERE constructs; default initializations; removed some obsolete features</td>
</tr>
<tr class="odd">
<td>Fortran 2003</td>
<td>2004</td>
<td>Object-oriented programming support; C language interoperability</td>
</tr>
<tr class="even">
<td>Fortran 2008</td>
<td>2010</td>
<td>Sub-modules; parallel execution model</td>
</tr>
<tr class="odd">
<td>Fortran 2018</td>
<td>2018</td>
<td>Improved interoperability with C; additional parallel features</td>
</tr>
<tr class="even">
<td>Fortran 2023</td>
<td>2023</td>
<td>Addresses minor error corrections and omissions for Fortran 2018</td>
</tr>
</tbody>
</table>
</section>
<section id="the-port" class="level1">
<h1>The Port</h1>
<p>I’ve only just begun the Fortran version of Practical Astronomy, so I don’t have a lot to share about that. But, with Fortran being a new language for me, I thought I’d share some of the challenges and idiosyncrasies I’ve encountered.</p>
<p>Coincidentally, I’ve kicked off this project on Easter Sunday 2025, and the first algorithm to port happens to be “date of Easter”. This algorithm takes a year value as input, and produces a full date as a result. For example, given an input year of 2025, the algorithm should give a result of 4/20/2025.</p>
<p>Since the result has three values, the first thing I needed to consider was how to return them: Either as a complex custom type, or as individual values (like a tuple). I decided to implement both to see how they compare in terms of complexity.</p>
<section id="complex-custom-type" class="level2">
<h2 class="anchored" data-anchor-id="complex-custom-type">Complex Custom Type</h2>
<section id="custom-type" class="level3">
<h3 class="anchored" data-anchor-id="custom-type">Custom Type</h3>
<p>The custom type used to hold our return value looks like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb1-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> CivilDate</span>
<span id="cb1-2">   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">integer</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> month</span>
<span id="cb1-3">   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> day</span>
<span id="cb1-4">   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">integer</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> year</span>
<span id="cb1-5"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">end type</span></span></code></pre></div></div>
<p>The <code>type</code> statement indicates that we’re creating a complex data structure that can encapsulate multiple data items of different types. The <code>CivilDate</code> type contains three data items: <code>month</code> and <code>year</code> are integers, and <code>day</code> is a real, since we sometimes need access to fractional parts of days.</p>
</section>
<section id="the-function" class="level3">
<h3 class="anchored" data-anchor-id="the-function">The Function</h3>
<p>Using the custom type in a function is a little tricky, since Fortran functions require custom types to be returned as pointers. Here’s the complete function:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span> get_date_of_easter(input_year) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">result</span>(custom_obj)</span>
<span id="cb2-2">   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type(CivilDate)</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">pointer</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> custom_obj</span>
<span id="cb2-3">   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">integer</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> input_year</span>
<span id="cb2-4">   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> year, a, b, c, d, e, f, g, h, i, k, l, m, n, p, day, month</span>
<span id="cb2-5"> </span>
<span id="cb2-6">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">allocate</span>(custom_obj)</span>
<span id="cb2-7"> </span>
<span id="cb2-8">   year <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> input_year</span>
<span id="cb2-9">   a <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>(year, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">19.0</span>)</span>
<span id="cb2-10">   b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>(year <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb2-11">   c <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>(year, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100.0</span>)</span>
<span id="cb2-12">   d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>(b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb2-13">   e <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>(b , <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>)</span>
<span id="cb2-14">   f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>((b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)</span>
<span id="cb2-15">   g <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>((b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb2-16">   h <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>( ((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">19</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> a) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> g <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>) , <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30.0</span>)</span>
<span id="cb2-17">   i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>(c <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb2-18">   k <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>(c , <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>)</span>
<span id="cb2-19">   l <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>( (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> (e <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> i) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> h <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> k) , <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.0</span>)</span>
<span id="cb2-20">   m <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>((a <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> h) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> l)) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">451</span>)</span>
<span id="cb2-21">   n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>((h <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> l <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> m) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">114</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</span>)</span>
<span id="cb2-22">   p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>((h <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> l <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> m) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">114</span>) , <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">31.0</span>)</span>
<span id="cb2-23"> </span>
<span id="cb2-24">   day <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb2-25">   month <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> n</span>
<span id="cb2-26"> </span>
<span id="cb2-27">   custom_obj<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>month <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>(month)</span>
<span id="cb2-28">   custom_obj<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>day <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> day</span>
<span id="cb2-29">   custom_obj<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>year <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> input_year</span>
<span id="cb2-30"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end function</span></span></code></pre></div></div>
<p>Here’s what’s going on in the first four lines. First, we declare a variable named <code>custom_obj</code> as a pointer to an instance of the custom <code>CivilDate</code> type:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb3-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type(CivilDate)</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">pointer</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> custom_obj</span></code></pre></div></div>
<p>Next, we declare the <code>input_year</code> parameter variable:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb4-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">integer</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> input_year</span></code></pre></div></div>
<p>Then, a series of variables that will be used in calculations:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb5-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> year, a, b, c, d, e, f, g, h, i, k, l, m, n, p, day, month</span></code></pre></div></div>
<p>Finally, we allocate space for <code>custom_object</code>, similar to how we’d use <code>malloc</code> in C:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">allocate</span>(custom_obj)</span></code></pre></div></div>
<p>The next few lines use a series of (mostly) modulo and floor operations to calculate the date values:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb7-1">year <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> input_year</span>
<span id="cb7-2">a <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>(year, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">19.0</span>)</span>
<span id="cb7-3">b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>(year <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb7-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">c = mod(year, 100.0)</span></span>
<span id="cb7-5">d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>(b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb7-6">e <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>(b , <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>)</span>
<span id="cb7-7">f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>((b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)</span>
<span id="cb7-8">g <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>((b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb7-9">h <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>( ((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">19</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> a) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> g <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>) , <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30.0</span>)</span>
<span id="cb7-10">i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>(c <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb7-11">k <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>(c , <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>)</span>
<span id="cb7-12">l <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>( (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> (e <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> i) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> h <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> k) , <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.0</span>)</span>
<span id="cb7-13">m <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>((a <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> h) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> l)) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">451</span>)</span>
<span id="cb7-14">n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>((h <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> l <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> m) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">114</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</span>)</span>
<span id="cb7-15">p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>((h <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> l <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> m) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">114</span>) , <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">31.0</span>)</span>
<span id="cb7-16"> </span>
<span id="cb7-17">day <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb7-18">month <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> n</span></code></pre></div></div>
<p>Finally, we assign the calculated date elements to the corresponding date items in our pointer object:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">custom_obj%month = floor(month)</span></span>
<span id="cb8-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">custom_obj%day = day</span></span>
<span id="cb8-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">custom_obj%year = input_year</span></span></code></pre></div></div>
</section>
<section id="calling-the-function" class="level3">
<h3 class="anchored" data-anchor-id="calling-the-function">Calling the Function</h3>
<p>First, we declare two variables, an integer we’ll use to provide the input year, and a pointer to hold the result:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb9-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">integer</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> input_year</span>
<span id="cb9-2"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type(CivilDate)</span>,<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">pointer</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> civil_date</span></code></pre></div></div>
<p>Next, we call the function and assign the result:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">civil_date =&gt; get_date_of_easter(input_year)</span></span></code></pre></div></div>
<p>Finally, we print our results, and then free the pointer:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span>,civil_date<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>month</span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span>,civil_date<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>day</span>
<span id="cb11-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span>,civil_date<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>year</span>
<span id="cb11-4"> </span>
<span id="cb11-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">deallocate</span>(civil_date)</span></code></pre></div></div>
<p>Given an input year of 2025, here is our output:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb12-1">           4</span>
<span id="cb12-2">   20.0000000    </span>
<span id="cb12-3">        2025</span></code></pre></div></div>
<p>This is the complete subroutine that handles our test:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb13-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">subroutine</span> test_date_of_easter(input_year)</span>
<span id="cb13-2">   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">integer</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> input_year</span>
<span id="cb13-3">   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">type(CivilDate)</span>,<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">pointer</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> civil_date</span>
<span id="cb13-4"> </span>
<span id="cb13-5">   civil_date <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> get_date_of_easter(input_year)</span>
<span id="cb13-6"> </span>
<span id="cb13-7">   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span>,civil_date<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>month</span>
<span id="cb13-8">   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span>,civil_date<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>day</span>
<span id="cb13-9">   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span>,civil_date<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>year</span>
<span id="cb13-10"> </span>
<span id="cb13-11">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">deallocate</span>(civil_date)</span>
<span id="cb13-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end subroutine</span></span></code></pre></div></div>
</section>
</section>
<section id="individual-values" class="level2">
<h2 class="anchored" data-anchor-id="individual-values">Individual Values</h2>
<p>If we prefer not to use a complex type, we can return individual values instead. Fortran doesn’t support tuples, but it does allow you to specify arguments to a subroutine as being input or output values through the use of the intent keyword.</p>
<section id="the-subroutine" class="level3">
<h3 class="anchored" data-anchor-id="the-subroutine">The Subroutine</h3>
<p>Here’s the complete subroutine:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb14-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">subroutine</span> get_date_of_easter(input_year, month, day, year)</span>
<span id="cb14-2">   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">integer</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">intent(in)</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> input_year</span>
<span id="cb14-3">   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">integer</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">intent(out)</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> month</span>
<span id="cb14-4">   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">intent(out)</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> day</span>
<span id="cb14-5">   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">integer</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">intent(out)</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> year</span>
<span id="cb14-6"> </span>
<span id="cb14-7">   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> year1, a, b, c, d, e, f, g, h, i, k, l, m, n, p</span>
<span id="cb14-8"> </span>
<span id="cb14-9">   year1 <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> input_year</span>
<span id="cb14-10">   a <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>(year1, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">19.0</span>)</span>
<span id="cb14-11">   b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>(year1 <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb14-12">   c <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>(year1, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">100.0</span>)</span>
<span id="cb14-13">   d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>(b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb14-14">   e <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>(b , <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>)</span>
<span id="cb14-15">   f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>((b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)</span>
<span id="cb14-16">   g <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>((b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb14-17">   h <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>(((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">19</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> a) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> g <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>) , <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30.0</span>)</span>
<span id="cb14-18">   i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>(c <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb14-19">   k <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>(c, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>)</span>
<span id="cb14-20">   l <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> (e <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> i) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> h <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> k) , <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.0</span>)</span>
<span id="cb14-21">   m <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>((a <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> h) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> l)) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">451</span>)</span>
<span id="cb14-22">   n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>((h <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> l <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> m) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">114</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</span>)</span>
<span id="cb14-23">   p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>((h <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> l <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> m) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">114</span>) , <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">31.0</span>)</span>
<span id="cb14-24"> </span>
<span id="cb14-25">   day <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb14-26">   month <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>(n)</span>
<span id="cb14-27">   year <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> input_year</span>
<span id="cb14-28"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end subroutine</span></span></code></pre></div></div>
<p>In the first line, we indicate that <code>input_year</code> is an input value:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb15-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">integer</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">intent(in)</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> input_year</span></code></pre></div></div>
<p>Next, we indicate that <code>month</code>, <code>day</code>, and <code>year</code> are output values (values that will be returned to calling code):</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb16-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">integer</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">intent(out)</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> month</span>
<span id="cb16-2"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">intent(out)</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> day</span>
<span id="cb16-3"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">integer</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">intent(out)</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> year</span></code></pre></div></div>
<p>In the next series of lines, we declare variables that will be used for our internal calculations, and perform those calculations:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb17-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> year1, a, b, c, d, e, f, g, h, i, k, l, m, n, p</span>
<span id="cb17-2"> </span>
<span id="cb17-3">year1 <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> input_year</span>
<span id="cb17-4">a <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>(year1, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">19.0</span>)</span>
<span id="cb17-5">b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>(year1 <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb17-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">c = mod(year1, 100.0)</span></span>
<span id="cb17-7">d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>(b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb17-8">e <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>(b , <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>)</span>
<span id="cb17-9">f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>((b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)</span>
<span id="cb17-10">g <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>((b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb17-11">h <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>(((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">19</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> a) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> g <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>) , <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30.0</span>)</span>
<span id="cb17-12">i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>(c <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb17-13">k <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>(c, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>)</span>
<span id="cb17-14">l <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>((<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> (e <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> i) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> h <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> k) , <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.0</span>)</span>
<span id="cb17-15">m <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>((a <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> h) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> l)) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">451</span>)</span>
<span id="cb17-16">n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>((h <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> l <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> m) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">114</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</span>)</span>
<span id="cb17-17">p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">mod</span>((h <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> l <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span> m) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">114</span>) , <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">31.0</span>)</span></code></pre></div></div>
<p>Finally, we assign the output/return values:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb18-1">day <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb18-2">month <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">floor</span>(n)</span>
<span id="cb18-3">year <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=</span> input_year</span></code></pre></div></div>
</section>
<section id="calling-the-subroutine" class="level3">
<h3 class="anchored" data-anchor-id="calling-the-subroutine">Calling the Subroutine</h3>
<p>First, we declare a variable to hold our input value:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">! input</span></span>
<span id="cb19-2"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">integer</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> input_year</span></code></pre></div></div>
<blockquote class="blockquote">
<p>In Fortran, lines prefixed with ! are treated as comments.</p>
</blockquote>
<p>Next, the variables to hold the results:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb20-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">! outputs</span></span>
<span id="cb20-2"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">integer</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> month</span>
<span id="cb20-3"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> day</span>
<span id="cb20-4"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">integer</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> year</span></code></pre></div></div>
<p>Then, we call the subroutine, and print the results:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">call get_date_of_easter(input_year, month, day, year)</span></span>
<span id="cb21-2"> </span>
<span id="cb21-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span>, month</span>
<span id="cb21-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span>, day</span>
<span id="cb21-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span>, year</span></code></pre></div></div>
<p>Given an input of 2025, this is our output:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb22-1">           4</span>
<span id="cb22-2">   20.0000000    </span>
<span id="cb22-3">        2025</span></code></pre></div></div>
<p>This is the complete subroutine that handles our test:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode fortran code-with-copy"><code class="sourceCode fortranfixed"><span id="cb23-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">subroutine</span> test_date_of_easter(input_year)</span>
<span id="cb23-2">   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">! input</span></span>
<span id="cb23-3">   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">integer</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> input_year</span>
<span id="cb23-4"> </span>
<span id="cb23-5">   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">! outputs</span></span>
<span id="cb23-6">   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">integer</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> month</span>
<span id="cb23-7">   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> day</span>
<span id="cb23-8">   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">integer</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span> year</span>
<span id="cb23-9"> </span>
<span id="cb23-10">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">call</span> get_date_of_easter(input_year, month, day, year)</span>
<span id="cb23-11"> </span>
<span id="cb23-12">   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span>, month</span>
<span id="cb23-13">   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span>, day</span>
<span id="cb23-14">   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">*</span>, year</span>
<span id="cb23-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end subroutine</span></span></code></pre></div></div>
</section>
</section>
</section>
<section id="my-thoughts" class="level1">
<h1>My Thoughts</h1>
<p>I think that the second option (using <code>intent</code>) is the most concise and easiest to understand, although the first option wasn’t too bad once I grasped the syntax of the pointer.</p>
<p>Now I have to decide if I want to proceed with the rest of the Practical Astronomy port. This feels like it will be a pretty heavy lift.</p>


</section>

 ]]></description>
  <category>Retro</category>
  <category>Fortran</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2025/learning_fortran.html</guid>
  <pubDate>Sun, 20 Apr 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Observing Plan for the April Astronomy Program at Garber Forest</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2025/garber_observing_plan.html</link>
  <description><![CDATA[ 






<blockquote class="blockquote">
<p><strong>IMPORTANT ANNOUNCEMENT</strong> Sadly, the weather got us again. With rain and thunderstorms in the forecast, we’re forced to cancel the astronomy program for 4/5.</p>
</blockquote>
<p>On April 5th, 2025, I will be at the <a href="https://maps.app.goo.gl/Wqxny94ZTLmi6igm6">Garber Forest near Lewisburg Ohio</a>, equipped with my telescope to discuss astronomy and lead an exploration of the night sky - weather permitting, of course! (The <a href="../../posts/2024/garber_observing_plan.html">first attempt didn’t work out</a>.)</p>
<p><img src="https://jcarr.codeberg.page/blog/posts/2025/images/observing_plan/under_the_stars_1.jpg" class="img-fluid"></p>
<p>This event is organized by the <a href="https://preblecountyparks.org/">Preble County Park District</a>.</p>
<p><img src="https://jcarr.codeberg.page/blog/posts/2025/images/observing_plan/under_the_stars_2.jpg" class="img-fluid"></p>
<section id="what-well-look-for" class="level1">
<h1>What We’ll Look For</h1>
<p>These are our main targets, but we’ll look for other things too!</p>
<section id="pm" class="level2">
<h2 class="anchored" data-anchor-id="pm">8pm</h2>
<p>The sky will still be in twilight, making it difficult to spot many celestial objects, but the moon will already make a great target!</p>
<p>The moon will be slightly over half full and the craters along the terminator line will be in shadow, making them stand out.</p>
<blockquote class="blockquote">
<p>A <strong>terminator line</strong> is the boundary that separates the illuminated side from the dark side of a celestial body, such as the Earth or the Moon. It marks where day transitions into night and is often associated with twilight conditions.</p>
</blockquote>
</section>
<section id="pm-1" class="level2">
<h2 class="anchored" data-anchor-id="pm-1">9pm</h2>
<p><img src="https://jcarr.codeberg.page/blog/posts/2025/images/observing_plan/arcturus.jpg" class="img-fluid"></p>
<p>Rising in the East: Arcturus, part of the constellation Bootes.</p>
<hr>
<p><img src="https://jcarr.codeberg.page/blog/posts/2025/images/observing_plan/moon_mars.jpg" class="img-fluid"></p>
<p>Overhead, Mars will be close to the Moon.</p>
<hr>
<p><img src="https://jcarr.codeberg.page/blog/posts/2025/images/observing_plan/orion_sirius_jupiter_taurus_pleiades.jpg" class="img-fluid"></p>
<p>To the West, the constellation Orion, with its bright stars Betelgeuse and Rigel and the Orion nebula.</p>
<p>To the left of Orion is the bright star Sirius, part of the constellation Canis Major.</p>
<p>To the right of Orion is the bright star Aldebaran, part of the constellation Taurus.</p>
<p>Just above Aldebaran is Jupiter, and to the right of Aldebaran is the Pleiades, a star cluster.</p>
<hr>
<p><img src="https://jcarr.codeberg.page/blog/posts/2025/images/observing_plan/dippers_cassiopeia.jpg" class="img-fluid"></p>
<p>To the North, the “dippers” (Ursa Minor and Ursa Major) and Cassiopeia.</p>
</section>
</section>
<section id="more-information" class="level1">
<h1>More Information</h1>
<section id="magnitude" class="level2">
<h2 class="anchored" data-anchor-id="magnitude">Magnitude</h2>
<p>In astronomy, “magnitude” refers to the brightness of celestial objects, such as stars, planets, and galaxies. There are two main types of magnitude:</p>
<ol type="1">
<li><p><strong>Apparent Magnitude</strong>: This measures how bright an object appears from Earth. The scale is logarithmic and inversely related, meaning that lower numbers indicate brighter objects. For example, a star with an apparent magnitude of 1 is brighter than a star with an apparent magnitude of 6. The scale also includes negative values for very bright objects, such as the Sun and the Moon.</p></li>
<li><p><strong>Absolute Magnitude</strong>: This measures the intrinsic brightness of a celestial object, defined as how bright the object would appear if it were located at a standard distance of 10 parsecs (about 32.6 light-years) from Earth. Absolute magnitude allows astronomers to compare the true brightness of different objects without the effects of distance.</p></li>
</ol>
<p>The concept of magnitude is crucial for understanding the luminosity and distance of celestial bodies, as well as for classifying stars and other astronomical phenomena.</p>
</section>
<section id="constellation" class="level2">
<h2 class="anchored" data-anchor-id="constellation">Constellation</h2>
<p>A constellation is a recognized pattern of stars in the night sky that forms a specific shape or figure, often representing mythological characters, animals, or objects. Constellations serve as a way to organize and identify groups of stars, making it easier for astronomers and stargazers to navigate the sky.</p>
<p>There are 88 officially recognized constellations, as defined by the International Astronomical Union (IAU). These constellations cover the entire celestial sphere and are used in various ways, including for navigation, storytelling, and as a framework for locating celestial objects. Some well-known constellations include Orion, Ursa Major, and Cassiopeia.</p>
</section>
<section id="asterism" class="level2">
<h2 class="anchored" data-anchor-id="asterism">Asterism</h2>
<p>In astronomy, “asterism” refers to a recognizable pattern or group of stars that may not necessarily form a formal constellation. Asterisms can be made up of stars from one or more constellations and are often more easily recognizable or familiar to the casual observer.</p>
<p>For example, the Big Dipper is an asterism that is part of the larger constellation Ursa Major. Similarly, the Summer Triangle is another well-known asterism formed by three bright stars from three different constellations: Vega (Lyra), Deneb (Cygnus), and Altair (Aquila).</p>
<p>Asterisms are useful for amateur astronomers and stargazers as they can serve as reference points for locating other stars and celestial objects in the night sky.</p>
</section>
<section id="altitude-and-azimuth" class="level2">
<h2 class="anchored" data-anchor-id="altitude-and-azimuth">Altitude and Azimuth</h2>
<p>Altitude and azimuth are two coordinates used in astronomy to specify the position of an object in the sky as observed from a specific location on Earth.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jcarr.codeberg.page/blog/posts/2025/images/observing_plan/altitude-and-azimuth.jpg" class="img-fluid figure-img"></p>
<figcaption>Altitude and Azimuth</figcaption>
</figure>
</div>
<ol type="1">
<li><p><strong>Altitude</strong>: This is the angle between the object in the sky and the observer’s local horizon. It is measured in degrees, ranging from 0° (the horizon) to 90° (the zenith, which is directly overhead). An object with an altitude of 0° is on the horizon, while an object with an altitude of 90° is directly above the observer.</p></li>
<li><p><strong>Azimuth</strong>: This is the angle measured clockwise from a reference direction, typically true north. Azimuth is also measured in degrees, ranging from 0° to 360°. For example, an azimuth of 0° corresponds to north, 90° to east, 180° to south, and 270° to west.</p></li>
</ol>
<p>Together, altitude and azimuth provide a way to pinpoint the location of celestial objects in the sky relative to an observer’s position on Earth. This system is particularly useful for navigation, stargazing, and astronomical observations.</p>
</section>
<section id="the-moon" class="level2">
<h2 class="anchored" data-anchor-id="the-moon">The Moon</h2>
<p>The Moon is Earth’s only natural satellite and is the fifth largest moon in the solar system. It has a significant impact on Earth, influencing tides and stabilizing the planet’s axial tilt. The Moon’s surface is covered with craters, mountains, and flat plains called maria, which were formed by ancient volcanic activity.</p>
<p>The Moon has a synchronous rotation, meaning it rotates on its axis in the same time it takes to orbit Earth, which is why we always see the same side from our planet. The far side of the Moon, often mistakenly called the “dark side,” is not permanently dark; it just faces away from Earth.</p>
<p>The Moon has been a subject of human fascination for centuries, inspiring myths, art, and scientific exploration. The Apollo missions in the late 1960s and early 1970s marked the first time humans set foot on the Moon, with Apollo 11 being the most famous mission, landing Neil Armstrong and Buzz Aldrin on its surface in 1969.</p>
<section id="moon-phases" class="level3">
<h3 class="anchored" data-anchor-id="moon-phases">Moon Phases</h3>
<p>Waxing and waning refer to the phases of the moon as it orbits the Earth, specifically how the visible portion of the moon changes over time.</p>
<ol type="1">
<li><strong>Waxing</strong>: This term describes the period when the moon is increasing in illumination. After the new moon phase, the moon begins to wax, meaning more of its surface becomes visible from Earth. The phases during this period include:
<ul>
<li><strong>Waxing Crescent</strong>: A small sliver of the moon is illuminated.</li>
<li><strong>First Quarter</strong>: Half of the moon is illuminated.</li>
<li><strong>Waxing Gibbous</strong>: More than half of the moon is illuminated but not yet full.</li>
</ul></li>
<li><strong>Waning</strong>: This term describes the period when the moon is decreasing in illumination. After the full moon phase, the moon begins to wane, meaning less of its surface is visible. The phases during this period include:
<ul>
<li><strong>Waning Gibbous</strong>: More than half of the moon is illuminated but decreasing.</li>
<li><strong>Last Quarter</strong>: Half of the moon is illuminated, but the opposite side compared to the first quarter.</li>
<li><strong>Waning Crescent</strong>: A small sliver of the moon is illuminated, leading back to the new moon phase.</li>
</ul></li>
</ol>
<p>These cycles occur roughly every 29.5 days, which is the time it takes for the moon to complete one full orbit around the Earth and return to the same phase.</p>
</section>
</section>
<section id="planets" class="level2">
<h2 class="anchored" data-anchor-id="planets">Planets</h2>
<p>There are eight recognized planets in the solar system, which are divided into two categories:</p>
<ul>
<li><strong>Terrestrial planets</strong>: Mercury, Venus, Earth, and Mars. These are rocky and have solid surfaces.</li>
<li><strong>Gas giants</strong>: Jupiter and Saturn, which are primarily composed of hydrogen and helium.</li>
<li><strong>Ice giants</strong>: Uranus and Neptune, which have a larger proportion of “ices” such as water, ammonia, and methane.</li>
</ul>
<p><strong>Dwarf Planets</strong>: These include Pluto, Eris, Haumea, Makemake, and Ceres. Dwarf planets are similar to regular planets but do not clear their orbital paths of other debris.</p>
<section id="jupiter" class="level3">
<h3 class="anchored" data-anchor-id="jupiter">Jupiter</h3>
<p>Jupiter is the largest planet in our solar system, known for its massive size, strong magnetic field, and distinctive features. Here are some key points about Jupiter:</p>
<ol type="1">
<li><p><strong>Size and Composition</strong>: Jupiter is a gas giant, primarily composed of hydrogen and helium. It has a diameter of about 86,881 miles (139,822 kilometers), making it more than 11 times wider than Earth.</p></li>
<li><p><strong>Great Red Spot</strong>: One of Jupiter’s most famous features is the Great Red Spot, a giant storm that has been raging for at least 350 years. It is larger than Earth and is characterized by its reddish color.</p></li>
<li><p><strong>Moons</strong>: Jupiter has a large number of moons, with 79 confirmed as of now. The four largest moons, known as the Galilean moons, are Io, Europa, Ganymede, and Callisto. Ganymede is the largest moon in the solar system.</p></li>
<li><p><strong>Rings</strong>: Jupiter has a faint ring system composed mainly of dust particles and small rocks. The rings are not as prominent as those of Saturn.</p></li>
<li><p><strong>Magnetic Field</strong>: Jupiter has the strongest magnetic field of any planet in the solar system, which is generated by the motion of metallic hydrogen in its interior.</p></li>
<li><p><strong>Exploration</strong>: Several spacecraft have visited Jupiter, including the Galileo orbiter and the Juno spacecraft, which is currently studying the planet’s atmosphere, magnetic field, and structure.</p></li>
<li><p><strong>Atmosphere</strong>: Jupiter’s atmosphere is known for its colorful bands and storms, including the aforementioned Great Red Spot. The planet’s atmosphere is dynamic and experiences high-speed winds.</p></li>
</ol>
</section>
<section id="jupiters-moons" class="level3">
<h3 class="anchored" data-anchor-id="jupiters-moons">Jupiter’s Moons</h3>
<section id="callisto" class="level4">
<h4 class="anchored" data-anchor-id="callisto">Callisto</h4>
<p>Callisto is one of the largest moons of Jupiter and is the third-largest moon in the solar system. It has a diameter of about 4,820 kilometers (2,995 miles) and is known for its heavily cratered surface, which suggests it has been geologically inactive for a long time. Callisto is composed primarily of ice and rock, and its surface features include a mix of old impact craters and some younger, less cratered regions.</p>
<p>One of the most interesting aspects of Callisto is its potential subsurface ocean. While there is no direct evidence of this ocean, some scientists believe that beneath its icy crust, there may be a salty ocean that could harbor conditions suitable for life.</p>
<p>Callisto has a very thin atmosphere, primarily composed of carbon dioxide, and it has a low level of radiation compared to other Galilean moons, making it a more favorable location for future exploration and potential human missions. It was discovered by Galileo Galilei in 1610, along with the other three largest moons of Jupiter: Io, Europa, and Ganymede.</p>
</section>
<section id="europa" class="level4">
<h4 class="anchored" data-anchor-id="europa">Europa</h4>
<p>Europa is one of Jupiter’s largest moons and is the sixth-largest moon in the solar system. It is particularly interesting to scientists because it is believed to have a subsurface ocean beneath its icy crust, which could potentially harbor conditions suitable for life. Here are some key features of Europa:</p>
<ol type="1">
<li><p><strong>Surface and Composition</strong>: Europa’s surface is primarily composed of water ice, and it features a smooth, bright appearance with few impact craters, suggesting a relatively young surface. The cracks and ridges on its surface indicate tectonic activity.</p></li>
<li><p><strong>Subsurface Ocean</strong>: Scientists believe that beneath Europa’s icy shell lies a salty ocean that may be in contact with the moon’s rocky mantle. This ocean could provide the necessary chemical ingredients for life.</p></li>
<li><p><strong>Potential for Life</strong>: The combination of liquid water, a stable energy source (from tidal heating due to Jupiter’s gravitational pull), and essential chemical elements makes Europa a prime candidate in the search for extraterrestrial life.</p></li>
<li><p><strong>Exploration</strong>: Europa has been studied by several spacecraft, including the Galileo orbiter and the Hubble Space Telescope.</p></li>
<li><p><strong>Atmosphere</strong>: Europa has a very thin atmosphere composed mainly of oxygen, but it is far too thin to support human life.</p></li>
</ol>
<p>Europa continues to be a focus of astrobiological research and exploration due to its intriguing characteristics and the possibility of finding life beyond Earth.</p>
<blockquote class="blockquote">
<p><em>Europa Clipper</em>, a space probe developed by NASA, is currently on its way to Europa and is expected to arrive in the Jupiter system in 2030.</p>
<p>The goals of Europa Clipper are to explore Europa, investigate its habitability, and aid in the selection of a landing site for the proposed Europa Lander. This exploration is focused on understanding the three main requirements for life: liquid water, chemistry, and energy. Specifically, the objectives are to study:</p>
<ul>
<li>Ice shell and ocean: Confirm the existence and characterize the nature of water within or beneath the ice, and study processes of surface-ice-ocean exchange.</li>
<li>Composition: Distribution and chemistry of key compounds and the links to ocean composition.</li>
<li>Geology: Characteristics and formation of surface features, including sites of recent or current activity.</li>
</ul>
<p><a href="https://en.wikipedia.org/wiki/Europa_Clipper#Objectives">source</a></p>
</blockquote>
</section>
<section id="ganymede" class="level4">
<h4 class="anchored" data-anchor-id="ganymede">Ganymede</h4>
<p>Ganymede is the largest moon of Jupiter and the largest moon in the solar system. It is one of the four Galilean moons, which were discovered by Galileo Galilei in 1610. Ganymede has a diameter of about 5,268 kilometers (3,273 miles), making it even larger than the planet Mercury, although it does not have enough mass to be classified as a planet.</p>
<p>Ganymede is unique among moons because it has a magnetic field, which is likely generated by a liquid iron or iron-sulfide core. The surface of Ganymede is a mix of two types of terrain: bright, icy regions and darker, heavily cratered areas. It is believed to have a subsurface ocean beneath its icy crust, which raises the possibility of conditions that could support life.</p>
<p>In addition to its geological features, Ganymede has a thin atmosphere composed mostly of oxygen, although it is far too thin to support human life. The moon has been studied by various spacecraft, including the Galileo orbiter and the Hubble Space Telescope.</p>
</section>
<section id="io" class="level4">
<h4 class="anchored" data-anchor-id="io">Io</h4>
<p>Io is one of the four largest moons of Jupiter. It is the most geologically active body in the solar system, with hundreds of volcanoes, some of which are still erupting. Io’s surface is covered with sulfur and sulfur dioxide, giving it a colorful appearance with shades of yellow, red, and white.</p>
<p>The intense geological activity on Io is primarily due to the gravitational pull from Jupiter and the other Galilean moons (Europa, Ganymede, and Callisto), which creates tidal heating. This process generates enough internal heat to drive volcanic activity.</p>
<p>Io has a thin atmosphere composed mainly of sulfur dioxide, and its surface temperature can vary widely. The moon also has a very weak magnetic field and is known to have a significant interaction with Jupiter’s magnetosphere.</p>
</section>
</section>
<section id="mars" class="level3">
<h3 class="anchored" data-anchor-id="mars">Mars</h3>
<p>Mars is the fourth planet from the Sun in our solar system and is often referred to as the “Red Planet” due to its reddish appearance, which is caused by iron oxide (rust) on its surface. Here are some key facts about Mars:</p>
<ol type="1">
<li><p><strong>Atmosphere</strong>: Mars has a thin atmosphere, composed mostly of carbon dioxide, with traces of nitrogen and argon. This thin atmosphere contributes to its cold temperatures and makes it difficult for liquid water to exist on the surface.</p></li>
<li><p><strong>Surface Features</strong>: Mars is home to the largest volcano in the solar system, Olympus Mons, and a massive canyon system called Valles Marineris. The planet also has polar ice caps that grow and recede with the changing seasons.</p></li>
<li><p><strong>Water</strong>: Evidence suggests that liquid water once flowed on Mars, and there are signs of ancient riverbeds and lake beds. Currently, water exists mostly in the form of ice, and there are indications of briny liquid water in some areas.</p></li>
<li><p><strong>Exploration</strong>: Mars has been the target of numerous robotic missions, including orbiters, landers, and rovers. Notable missions include NASA’s Mars rovers, such as Spirit, Opportunity, Curiosity, and Perseverance, which have been studying the planet’s geology and searching for signs of past life.</p></li>
<li><p><strong>Potential for Life</strong>: Scientists are particularly interested in Mars because of its potential to have supported microbial life in the past. Ongoing research aims to understand the planet’s habitability.</p></li>
<li><p><strong>Future Missions</strong>: There are plans for future missions to Mars, including potential human exploration. NASA’s Artemis program aims to return humans to the Moon as a stepping stone for future crewed missions to Mars.</p></li>
</ol>
</section>
</section>
<section id="bright-stars" class="level2">
<h2 class="anchored" data-anchor-id="bright-stars">Bright Stars</h2>
<section id="aldebaran" class="level3">
<h3 class="anchored" data-anchor-id="aldebaran">Aldebaran</h3>
<p>Aldebaran is a prominent star located in the constellation Taurus. It is often referred to as the “Eye of the Bull” and is one of the brightest stars in the night sky. Aldebaran is classified as a red giant star and is approximately 65 light-years away from Earth. It has a distinct orange hue and is part of the Hyades star cluster, although it is not physically part of the cluster itself. Aldebaran is notable for its brightness and is often used in navigation and astronomy.</p>
</section>
<section id="arcturus" class="level3">
<h3 class="anchored" data-anchor-id="arcturus">Arcturus</h3>
<p>Arcturus is one of the brightest stars in the night sky and is located in the constellation Bootes. It is classified as a red giant star and is approximately 36.7 light-years away from Earth. Arcturus is notable for its orange hue, which is a result of its cooler surface temperature compared to hotter stars like our Sun. The name “Arcturus” comes from the Greek word meaning “bear watcher,” as it is associated with the nearby constellation Ursa Major (the Great Bear).</p>
<p>In addition to its brightness, Arcturus is also significant in astronomy for its role in the study of stellar evolution, as it represents a later stage in the life cycle of a star.</p>
</section>
<section id="betelgeuse" class="level3">
<h3 class="anchored" data-anchor-id="betelgeuse">Betelgeuse</h3>
<p>Betelgeuse is a red supergiant star located in the constellation Orion. It is one of the largest and most luminous stars known, with a diameter estimated to be about 1,000 times that of the Sun. Betelgeuse is notable for its distinct reddish color and is often used as a reference point in the night sky.</p>
<p>As a supergiant, Betelgeuse is in the later stages of its stellar evolution and is expected to eventually explode as a supernova. This event could happen within the next million years, although it’s difficult to predict exactly when.</p>
<p>In addition to its astronomical significance, Betelgeuse has captured the imagination of many cultures and is often featured in literature and popular media.</p>
<p><a href="../../posts/2020/betelgeuse.html">Betelgeuse was in the news recently</a>.</p>
</section>
<section id="polaris" class="level3">
<h3 class="anchored" data-anchor-id="polaris">Polaris</h3>
<p>“Polaris,” also known as the North Star, is the brightest star in the constellation Ursa Minor. It is located nearly directly above the North Pole, making it a crucial point of reference for navigation in the Northern Hemisphere. Polaris is a supergiant star and is approximately 433 light-years away from Earth.</p>
</section>
<section id="rigel" class="level3">
<h3 class="anchored" data-anchor-id="rigel">Rigel</h3>
<p>Rigel is a prominent star located in the constellation Orion. It is one of the brightest stars in the night sky and is classified as a blue supergiant. Rigel is approximately 860 light-years away from Earth and has a surface temperature of around 12,000 Kelvin, which gives it its blue color. It is often used in navigation and is notable for its brightness and distinctive position in the Orion constellation.</p>
</section>
<section id="sirius" class="level3">
<h3 class="anchored" data-anchor-id="sirius">Sirius</h3>
<p>Sirius is the brightest star in the night sky and is part of the constellation Canis Major. It is often referred to as the “Dog Star” because of its position in this constellation, which represents a dog. Sirius is actually a binary star system, consisting of two stars: Sirius A, a main-sequence star, and Sirius B, a white dwarf. The star is located about 8.6 light-years from Earth and has been significant in various cultures throughout history, often associated with myths and agricultural calendars.</p>
</section>
</section>
<section id="deep-sky-objects" class="level2">
<h2 class="anchored" data-anchor-id="deep-sky-objects">Deep Sky Objects</h2>
<section id="orion-nebula" class="level3">
<h3 class="anchored" data-anchor-id="orion-nebula">Orion Nebula</h3>
<p>The Orion Nebula, also known as M42, is one of the most famous and studied nebulae in the night sky. It is located in the Milky Way, approximately 1,344 light-years away from Earth in the Orion constellation. The nebula is a stellar nursery, where new stars are being born from the surrounding gas and dust.</p>
<p>Key features of the Orion Nebula include:</p>
<ol type="1">
<li><p><strong>Brightness</strong>: It is one of the brightest nebulae visible to the naked eye and can be seen as a fuzzy patch in the sword of Orion.</p></li>
<li><p><strong>Size</strong>: The Orion Nebula spans about 24 light-years across and contains a vast amount of gas and dust.</p></li>
<li><p><strong>Star Formation</strong>: The nebula is home to a young cluster of stars known as the Trapezium, which consists of four massive stars that illuminate the surrounding gas and dust.</p></li>
<li><p><strong>Observation</strong>: The Orion Nebula is a popular target for amateur astronomers and astrophotographers due to its brightness and the intricate structures within it.</p></li>
<li><p><strong>Scientific Importance</strong>: It provides valuable insights into the processes of star formation and the dynamics of interstellar matter.</p></li>
</ol>
<p>The Orion Nebula is a stunning example of the beauty and complexity of the universe, making it a favorite subject for both professional and amateur astronomers.</p>
</section>
<section id="the-pleiades" class="level3">
<h3 class="anchored" data-anchor-id="the-pleiades">The Pleiades</h3>
<p>The Pleiades, also known as the Seven Sisters, is a star cluster located in the constellation Taurus. It is one of the nearest star clusters to Earth and is easily visible to the naked eye. The cluster contains several hundred stars, but the seven brightest are typically the ones referred to as the “Seven Sisters.” These stars are named Alcyone, Asterope, Electra, Maia, Merope, Taygeta, and Pleione.</p>
<p>The Pleiades has been significant in various cultures throughout history, often associated with mythology and folklore. In astronomy, it is known for its beauty and is often used as a reference point for navigation. The cluster is also of interest to astronomers studying star formation and the evolution of stars.</p>


</section>
</section>
</section>

 ]]></description>
  <category>Astronomy</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2025/garber_observing_plan.html</guid>
  <pubDate>Sat, 01 Mar 2025 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Implement a REST Service on a Nano RP2040</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2025/nano_rp2040_rest.html</link>
  <description><![CDATA[ 






<p>The Nano RP2040 is an Arduino Nano board with a Raspberry Pi RP2040 microcontroller that supports Bluetooth, WiFi, and machine learning. It has an onboard accelerometer, gyroscope, microphone, and temperature sensor.</p>
<p><img src="https://jcarr.codeberg.page/blog/posts/2025/images/nano_rp2040.png" class="img-fluid"></p>
<p>With the built-in sensors and WiFi, it’s a good candidate for a remote monitoring solution. We’ll implement a simple REST service that returns the current temperature.</p>
<blockquote class="blockquote">
<p>NOTE: I’m maintaining and improving a more feature-rich version of this project <a href="https://github.com/jfcarr/nano-rp2040-rest">here</a>.</p>
</blockquote>
<section id="toolset" class="level2">
<h2 class="anchored" data-anchor-id="toolset">Toolset</h2>
<p>Here’s what I’ll be using:</p>
<ul>
<li><a href="https://www.amazon.com/dp/B095J4KFVT">Nano RP2040 board with a micro USB cable</a></li>
<li><a href="https://docs.arduino.cc/arduino-cli/">Arduino CLI</a></li>
<li><a href="https://code.visualstudio.com/">Visual Studio Code</a> with the <a href="https://marketplace.visualstudio.com/items?itemName=vscode-arduino.vscode-arduino-community">Arduino Community Edition extension</a>. This is a community fork of Microsoft’s (<a href="https://github.com/microsoft/vscode-arduino/issues/1757">deprecated</a>) Arduino extension.</li>
</ul>
</section>
<section id="board-setup" class="level2">
<h2 class="anchored" data-anchor-id="board-setup">Board Setup</h2>
<p>Plug in your Nano RP2040 board and issue the following command:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">arduino-cli</span> board list</span></code></pre></div></div>
<p>You should see something like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb2-1">Port         Protocol Type              Board Name                  FQBN                                Core</span>
<span id="cb2-2">/dev/ttyACM0 serial   Serial Port (USB) Arduino Nano RP2040 Connect arduino:mbed_nano:nanorp2040connect arduino:mbed_nano</span></code></pre></div></div>
<p>You will probably need to install the core:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">arduino-cli</span> core install arduino:mbed_nano</span></code></pre></div></div>
<p>We’ll be using three additional libraries:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 26%">
<col style="width: 73%">
</colgroup>
<thead>
<tr class="header">
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Arduino_LSM6DSOX</td>
<td>Access the IMU for accelerometer, gyroscope, and embedded temperature sensor.</td>
</tr>
<tr class="even">
<td>ArduinoJson</td>
<td>A simple and efficient JSON library for embedded C++.</td>
</tr>
<tr class="odd">
<td>WiFiNINA</td>
<td>With this library you can instantiate Servers, Clients and send/receive UDP packets through WiFi.</td>
</tr>
</tbody>
</table>
<p>Install them:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">arduino-cli</span> lib install Arduino_LSM6DSOX</span>
<span id="cb4-2"></span>
<span id="cb4-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">arduino-cli</span> lib install ArduinoJson</span>
<span id="cb4-4"></span>
<span id="cb4-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">arduino-cli</span> lib install WiFiNINA</span></code></pre></div></div>
</section>
<section id="project-structure-setup" class="level2">
<h2 class="anchored" data-anchor-id="project-structure-setup">Project Structure / Setup</h2>
<p>Create a directory named <strong>web_server_rest_temp</strong>.</p>
<p>Open the directory in VS Code.</p>
<p>Create a Makefile containing the following content:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode makefile code-with-copy"><code class="sourceCode makefile"><span id="cb5-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">ARDCMD</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">arduino-cli</span></span>
<span id="cb5-2"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">FQBNSTR</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">arduino:mbed_nano:nanorp2040connect</span></span>
<span id="cb5-3"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">PORT</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/dev/ttyACM0</span></span>
<span id="cb5-4"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">SKETCHNAME</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">web_server_rest_temp</span></span>
<span id="cb5-5"></span>
<span id="cb5-6"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">default:</span></span>
<span id="cb5-7"><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">    </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">@</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">echo </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Targets:'</span></span>
<span id="cb5-8">    <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">@</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">echo </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'  compile  -- Compile sketch, but don''t upload it.'</span></span>
<span id="cb5-9">    <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">@</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">echo </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'  upload   -- Compile and upload sketch.'</span></span>
<span id="cb5-10">    <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">@</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">echo </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'  monitor  -- Open the serial port monitor.'</span></span>
<span id="cb5-11"></span>
<span id="cb5-12"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">compile:</span></span>
<span id="cb5-13"><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">    </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">$(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">ARDCMD</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span> compile --fqbn <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">$(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">FQBNSTR</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">$(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">SKETCHNAME</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb5-14"></span>
<span id="cb5-15"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">upload:</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;"> compile</span></span>
<span id="cb5-16"><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">    </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">$(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">ARDCMD</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span> upload -p <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">$(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">PORT</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span> --fqbn <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">$(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">FQBNSTR</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">$(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">SKETCHNAME</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb5-17"></span>
<span id="cb5-18"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">monitor:</span></span>
<span id="cb5-19"><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">    </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">$(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">ARDCMD</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span> monitor -p <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">$(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">PORT</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>You can type out individual commands for compiling, uploading, and monitoring instead, but using a Makefile is more convenient.</p>
<p>Create a subdirectory, also named <strong>web_server_rest_temp</strong>.</p>
<p>In the subdirectory, create a file named <strong>arduino_secrets.h</strong> containing the following contents:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb6-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#define SECRET_SSID </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"wireless_network_name"</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">           </span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// The name of your wireless network</span></span>
<span id="cb6-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#define SECRET_PASSWORD </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"wireless_network_password"</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">   </span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// The password for your wireless network</span></span>
<span id="cb6-3"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#define SECRET_PORT </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8090</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">                              </span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// The port that the REST service will run on</span></span></code></pre></div></div>
<p>Update the defines to match your network settings.</p>
<p>For example, if you have a wireless network named “MyHomeWifi” and you use the password “MyN3tw0rkP@ssw0rd” to connect to it, your <strong>arduino_secrets.h</strong> file would look like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb7-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#define SECRET_SSID </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MyHomeWifi"</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">              </span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// The name of your wireless network</span></span>
<span id="cb7-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#define SECRET_PASSWORD </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MyN3tw0rkP@ssw0rd"</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">   </span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// The password for your wireless network</span></span>
<span id="cb7-3"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#define SECRET_PORT </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8090</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">                      </span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// The port that the REST service will run on</span></span></code></pre></div></div>
<p>In the same subdirectory, create a file named <strong>web_server_rest_temp.ino</strong> and give it the boilerplate Arduino contents:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb8-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> setup<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span>
<span id="cb8-2"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb8-3"></span>
<span id="cb8-4"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb8-5"></span>
<span id="cb8-6"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> loop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span>
<span id="cb8-7"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb8-8"></span>
<span id="cb8-9"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Your project layout should now look like this:</p>
<p><img src="https://jcarr.codeberg.page/blog/posts/2025/images/nano_project_layout.jpg" class="img-fluid"></p>
<p>Keep <strong>web_server_rest_temp.ino</strong> open for editing.</p>
</section>
<section id="main-sketch" class="level2">
<h2 class="anchored" data-anchor-id="main-sketch">Main Sketch</h2>
<p>We’ll update <strong>web_server_rest_temp.ino</strong> incrementally and explain each update, then show a complete version at the end.</p>
<p>First, add your includes:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb9-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">"arduino_secrets.h"</span></span>
<span id="cb9-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;Arduino_LSM6DSOX.h&gt;</span></span>
<span id="cb9-3"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;ArduinoJson.h&gt;</span></span>
<span id="cb9-4"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;WiFiNINA.h&gt;</span></span></code></pre></div></div>
<p>Then, initialize network settings:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb10-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> ssid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SECRET_SSID<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// network SSID (name)</span></span>
<span id="cb10-2"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> pass<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SECRET_PASSWORD<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//  network password</span></span>
<span id="cb10-3"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> keyIndex <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>              <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// network key index number (needed only for WEP)</span></span>
<span id="cb10-4"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> port <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SECRET_PORT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div></div>
<p>Set the initial status and instantiate the server:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb11-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> status <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> WL_IDLE_STATUS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-2">WiFiServer server<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>port<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span></code></pre></div></div>
<p>Inside <code>setup()</code>, initialize serial communication at 9600 baud. (The serial monitor uses this)</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb12-1">Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>begin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9600</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span></code></pre></div></div>
<p>Make sure the IMU is available:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb13-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>IMU<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>begin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">())</span></span>
<span id="cb13-2"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb13-3">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Failed to initialize IMU!"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb13-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb13-5">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb13-6"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<blockquote class="blockquote">
<p>An inertial measurement unit (IMU) is an electronic device that measures and reports a body’s specific force, angular rate, and sometimes the orientation of the body, using a combination of accelerometers, gyroscopes, and sometimes magnetometers. When the magnetometer is included, IMUs are referred to as IMMUs.</p>
</blockquote>
<p>Make sure the WiFi module is available:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb14-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>WiFi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>status<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> WL_NO_MODULE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb14-2"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb14-3">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Communication with WiFi module failed!"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb14-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb14-5">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb14-6"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>See if the WiFi firmware is up to date:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb15-1">String fv <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> WiFi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>firmwareVersion<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb15-2"></span>
<span id="cb15-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>fv <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> WIFI_FIRMWARE_LATEST_VERSION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb15-4"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb15-5">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Please upgrade the firmware"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb15-6"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>If it isn’t, then display a message, but still continue.</p>
<p>Connect to the WiFi network:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb16-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>status <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> WL_CONNECTED<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb16-2"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb16-3">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Attempting to connect to Network named: "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb16-4">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ssid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// print the network name (SSID)</span></span>
<span id="cb16-5"></span>
<span id="cb16-6">    status <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> WiFi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>begin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ssid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> pass<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb16-7"></span>
<span id="cb16-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// wait 5 seconds for the connection:</span></span>
<span id="cb16-9">    delay<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb16-10"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Start the server, then print the WiFi status:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb17-1">server<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>begin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb17-2"></span>
<span id="cb17-3">printWifiStatus<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span></code></pre></div></div>
<p>The <code>printWifiStatus()</code> function is a new custom function that we need to add. It displays useful information about our new connection:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb18-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> printWifiStatus<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span>
<span id="cb18-2"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// print the SSID of the network you're attached to:</span></span>
<span id="cb18-4">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SSID: "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb18-5">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>WiFi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>SSID<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">());</span></span>
<span id="cb18-6"></span>
<span id="cb18-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// print your board's IP address:</span></span>
<span id="cb18-8">    IPAddress ip <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> WiFi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>localIP<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb18-9">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"IP Address: "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb18-10">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ip<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb18-11"></span>
<span id="cb18-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// print the received signal strength:</span></span>
<span id="cb18-13">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> rssi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> WiFi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>RSSI<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb18-14">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"signal strength (RSSI): "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb18-15">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>rssi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb18-16">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" dBm"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb18-17"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>That concludes our <code>setup()</code> code. Now we’re ready to move on to <code>loop()</code>.</p>
<p>First, instantiate a <code>WiFiClient</code>. It will listen for incoming clients:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb19-1">WiFiClient client <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> server<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>available<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span></code></pre></div></div>
<p>Check to see when a client connects:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb20-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb20-2"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb20-3"></span>
<span id="cb20-4"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>When a client connects, print a message, then initialize a String that will hold incoming data from the client:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb21-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb21-2"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb21-3">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"new client"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb21-4"></span>
<span id="cb21-5">    String currentLine <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb21-6"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Prepare to perform some operations while the client is connected, but only while the client is still available:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb22-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb22-2"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb22-3">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"new client"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb22-4"></span>
<span id="cb22-5">    String currentLine <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb22-6"></span>
<span id="cb22-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>connected<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">())</span></span>
<span id="cb22-8">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb22-9">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>available<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">())</span></span>
<span id="cb22-10">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb22-11">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb22-12">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb22-13"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Inside the <code>client.available()</code> block:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb23-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Read one character of the client request at a time:</span></span>
<span id="cb23-2"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb23-3"></span>
<span id="cb23-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// If the byte is a newline character:</span></span>
<span id="cb23-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb23-6"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb23-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// If currentline has been cleared, the request is finished, but it wasn't a known endpoint, so send a generic response:</span></span>
<span id="cb23-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>currentLine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>length<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb23-9">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb23-10">        sendResponse<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello from Arduino RP2040! Valid endpoints are /Temperature/Current/F and /Temperature/Current/C"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">99</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"invalid"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb23-11">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb23-12">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb23-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span></span>
<span id="cb23-14">        currentLine <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// If you got a newline, then clear currentLine</span></span>
<span id="cb23-15"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb23-16"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\r</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb23-17">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// If you got anything else but a carriage return character, add it to the end of the currentLine:</span></span>
<span id="cb23-18">    currentLine <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div></div>
<p><code>sendResponse()</code> is another new custom function. It receives the following as arguments:</p>
<ol type="1">
<li>A reference to the WiFiClient,</li>
<li>a text message,</li>
<li>a value, and</li>
<li>a status</li>
</ol>
<p>Inside the function:</p>
<ol type="1">
<li>A standard HTTP response is sent.</li>
<li>A JSON document object is created.</li>
<li>The JSON document is populated with the message, value, and status.</li>
<li>The JSON object is serialized back to the client.</li>
</ol>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb24-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> sendResponse<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>WiFiClient <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> message<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[],</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> status<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[])</span></span>
<span id="cb24-2"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb24-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Send a standard HTTP response</span></span>
<span id="cb24-4">    client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HTTP/1.1 200 OK"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb24-5">    client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Content-type: application/json"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb24-6">    client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Connection: close"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb24-7">    client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb24-8"></span>
<span id="cb24-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Create a JSON object</span></span>
<span id="cb24-10">    StaticJsonDocument<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> doc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb24-11"></span>
<span id="cb24-12">    doc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"message"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> message<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb24-13">    doc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb24-14">    doc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> status<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb24-15"></span>
<span id="cb24-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Serialize JSON to client</span></span>
<span id="cb24-17">    serializeJson<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>doc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb24-18"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Returning to the <code>loop()</code> function, still inside the <code>client.available()</code> block, we now check to see if a specific endpoint was called by the client:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb25-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> request_unit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'X'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb25-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>currentLine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>indexOf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GET /Temperature/Current/F"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb25-3">    request_unit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'F'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb25-4"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>currentLine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>indexOf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GET /Temperature/Current/C"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb25-5">    request_unit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'C'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div></div>
<p>We use request_unit to track calls to specific endpoints. If the client has asked for the current temperature in Fahrenheit or Celsius, we’ll want to respond accordingly:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb26-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>request_unit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'F'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> request_unit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'C'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb26-2"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb26-3">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> current_temperature <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>request_unit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'F'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> getTemperature<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> getTemperature<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">false</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb26-4">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> temp_units<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb26-5">    sprintf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>temp_units<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"°</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%s</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>request_unit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'F'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb26-6"></span>
<span id="cb26-7">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> message<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb26-8">    sprintf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>message<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Current temperature is </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%s</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> current_temperature<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> temp_units<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb26-9"></span>
<span id="cb26-10">    sendResponse<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> message<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> current_temperature<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"success"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb26-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb26-12"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>If the client has asked for the temperature, we retrieve it with <code>getTemperature()</code>, format the data to return to the client, then send the response.</p>
<p><code>getTemperature()</code> is another new function. It makes sure the IMU module is available, then retrieves the current temperature value from the IMU temperature sensor. The IMU returns the temperature in Celsius units, so the value is converted to Fahrenheit, if requested.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb27-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> getTemperature<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">bool</span> as_fahrenheit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb27-2"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb27-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>IMU<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>temperatureAvailable<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">())</span></span>
<span id="cb27-4">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb27-5">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> temperature_deg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb27-6">        IMU<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>readTemperature<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>temperature_deg<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb27-7"></span>
<span id="cb27-8">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>as_fahrenheit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb27-9">            temperature_deg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> celsiusToFahrenheit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>temperature_deg<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb27-10"></span>
<span id="cb27-11">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> temperature_deg<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb27-12">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb27-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span></span>
<span id="cb27-14">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb27-15">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">99</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb27-16">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb27-17"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>The <code>celsiusToFahrenheit()</code> function is also new:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb28-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> celsiusToFahrenheit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> celsius_value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb28-2"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb28-3">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>celsius_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">))</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb28-4"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Returning to the loop() function, after the <code>while (client.connected())</code> block, we perform some cleanup after the client disconnects:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb29-1">client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>stop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb29-2">Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"client disconnected"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span></code></pre></div></div>
<p>And that’s it! Our full sketch now looks like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode c code-with-copy"><code class="sourceCode c"><span id="cb30-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">"arduino_secrets.h"</span></span>
<span id="cb30-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;Arduino_LSM6DSOX.h&gt;</span></span>
<span id="cb30-3"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;ArduinoJson.h&gt;</span></span>
<span id="cb30-4"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;WiFiNINA.h&gt;</span></span>
<span id="cb30-5"></span>
<span id="cb30-6"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> ssid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SECRET_SSID<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// network SSID (name)</span></span>
<span id="cb30-7"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> pass<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SECRET_PASSWORD<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// network password</span></span>
<span id="cb30-8"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> keyIndex <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>              <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// network key index number (needed only for WEP)</span></span>
<span id="cb30-9"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> port <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SECRET_PORT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb30-10"></span>
<span id="cb30-11"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> setup<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span>
<span id="cb30-12"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb30-13">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>begin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9600</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-14"></span>
<span id="cb30-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>IMU<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>begin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">())</span></span>
<span id="cb30-16">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb30-17">        Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Failed to initialize IMU!"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-18">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb30-19">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb30-20">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb30-21"></span>
<span id="cb30-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>WiFi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>status<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> WL_NO_MODULE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb30-23">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb30-24">        Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Communication with WiFi module failed!"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-25">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb30-26">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb30-27">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb30-28"></span>
<span id="cb30-29">    String fv <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> WiFi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>firmwareVersion<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb30-30"></span>
<span id="cb30-31">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>fv <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> WIFI_FIRMWARE_LATEST_VERSION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb30-32">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb30-33">        Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Please upgrade the firmware"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-34">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb30-35"></span>
<span id="cb30-36">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>status <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> WL_CONNECTED<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb30-37">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb30-38">        Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Attempting to connect to Network named: "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-39">        Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ssid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// print the network name (SSID);</span></span>
<span id="cb30-40"></span>
<span id="cb30-41">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Connect to WPA/WPA2 network. Change this line if using open or WEP network:</span></span>
<span id="cb30-42">        status <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> WiFi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>begin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ssid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> pass<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-43">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// wait 5 seconds for connection:</span></span>
<span id="cb30-44">        delay<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-45">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb30-46"></span>
<span id="cb30-47">    server<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>begin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb30-48">    printWifiStatus<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb30-49"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb30-50"></span>
<span id="cb30-51"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> loop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span>
<span id="cb30-52"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb30-53">    WiFiClient client <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> server<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>available<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb30-54"></span>
<span id="cb30-55">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb30-56">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb30-57">        Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"new client"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-58"></span>
<span id="cb30-59">        String currentLine <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb30-60"></span>
<span id="cb30-61">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>connected<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">())</span></span>
<span id="cb30-62">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb30-63">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>available<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">())</span></span>
<span id="cb30-64">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb30-65">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Read one character of the client request at a time:</span></span>
<span id="cb30-66">                <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb30-67"></span>
<span id="cb30-68">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// If the byte is a newline character:</span></span>
<span id="cb30-69">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb30-70">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb30-71">                    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// If currentline has been cleared, the request is finished, but it wasn't a known endpoint, so send a generic response:</span></span>
<span id="cb30-72">                    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>currentLine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>length<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb30-73">                    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb30-74">                        sendResponse<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello from Arduino RP2040! Valid endpoints are /Temperature/Current/F and /Temperature/Current/C"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">99</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"invalid"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-75">                        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb30-76">                    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb30-77">                    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span></span>
<span id="cb30-78">                        currentLine <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// If you got a newline, then clear currentLine</span></span>
<span id="cb30-79">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb30-80">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\r</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb30-81">                    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// If you got anything else but a carriage return character, add it to the end of the currentLine:</span></span>
<span id="cb30-82">                    currentLine <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb30-83"></span>
<span id="cb30-84">                <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> request_unit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'X'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb30-85">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>currentLine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>indexOf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GET /Temperature/Current/F"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb30-86">                    request_unit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'F'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb30-87">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>currentLine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>indexOf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GET /Temperature/Current/C"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb30-88">                    request_unit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'C'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb30-89"></span>
<span id="cb30-90">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>request_unit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'F'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> request_unit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'C'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb30-91">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb30-92">                    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> current_temperature <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>request_unit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'F'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> getTemperature<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> getTemperature<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">false</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-93">                    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> temp_units<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb30-94">                    sprintf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>temp_units<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"°</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%s</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>request_unit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'F'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-95"></span>
<span id="cb30-96">                    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> message<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb30-97">                    sprintf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>message<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Current temperature is </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%s</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> current_temperature<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> temp_units<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-98"></span>
<span id="cb30-99">                    sendResponse<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> message<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> current_temperature<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"success"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-100">                    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb30-101">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb30-102">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb30-103">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb30-104"></span>
<span id="cb30-105">        client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>stop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb30-106">        Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"client disconnected"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-107">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb30-108"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb30-109"></span>
<span id="cb30-110"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> sendResponse<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>WiFiClient <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> message<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[],</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> status<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[])</span></span>
<span id="cb30-111"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb30-112">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Send a standard HTTP response</span></span>
<span id="cb30-113">    client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HTTP/1.1 200 OK"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-114">    client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Content-type: application/json"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-115">    client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Connection: close"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-116">    client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb30-117"></span>
<span id="cb30-118">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Create a JSON object</span></span>
<span id="cb30-119">    StaticJsonDocument<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> doc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb30-120"></span>
<span id="cb30-121">    doc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"message"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> message<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb30-122">    doc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb30-123">    doc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> status<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb30-124"></span>
<span id="cb30-125">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Serialize JSON to client</span></span>
<span id="cb30-126">    serializeJson<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>doc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-127"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb30-128"></span>
<span id="cb30-129"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> printWifiStatus<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span></span>
<span id="cb30-130"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb30-131">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// print the SSID of the network you're attached to:</span></span>
<span id="cb30-132">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SSID: "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-133">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>WiFi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>SSID<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">());</span></span>
<span id="cb30-134"></span>
<span id="cb30-135">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// print your board's IP address:</span></span>
<span id="cb30-136">    IPAddress ip <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> WiFi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>localIP<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb30-137">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"IP Address: "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-138">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ip<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-139"></span>
<span id="cb30-140">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// print the received signal strength:</span></span>
<span id="cb30-141">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> rssi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> WiFi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>RSSI<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb30-142">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"signal strength (RSSI): "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-143">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>rssi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-144">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" dBm"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-145"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb30-146"></span>
<span id="cb30-147"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> getTemperature<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">bool</span> as_fahrenheit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb30-148"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb30-149">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>IMU<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>temperatureAvailable<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">())</span></span>
<span id="cb30-150">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb30-151">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> temperature_deg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb30-152">        IMU<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>readTemperature<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>temperature_deg<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-153"></span>
<span id="cb30-154">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>as_fahrenheit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb30-155">            temperature_deg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> celsiusToFahrenheit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>temperature_deg<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb30-156"></span>
<span id="cb30-157">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> temperature_deg<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb30-158">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb30-159">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span></span>
<span id="cb30-160">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb30-161">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">99</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb30-162">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb30-163"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</section>
<section id="compile-and-upload" class="level2">
<h2 class="anchored" data-anchor-id="compile-and-upload">Compile and Upload</h2>
<p>Make sure the board is connected, then open a terminal.</p>
<p>Compile:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb31-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make</span> compile</span></code></pre></div></div>
<p>or</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb32-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">arduino-cli</span> compile <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--fqbn</span> arduino:mbed_nano:nanorp2040connect web_server_rest_temp</span></code></pre></div></div>
<p>You should see something similar to this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb33-1">Sketch uses 112214 bytes (0%) of program storage space. Maximum is 16777216 bytes.</span>
<span id="cb33-2">Global variables use 44552 bytes (16%) of dynamic memory, leaving 225784 bytes for local variables. Maximum is 270336 bytes.</span>
<span id="cb33-3"></span>
<span id="cb33-4">Used library     Version</span>
<span id="cb33-5">Arduino_LSM6DSOX 1.1.2</span>
<span id="cb33-6">Wire</span>
<span id="cb33-7">SPI</span>
<span id="cb33-8">ArduinoJson      7.3.0</span>
<span id="cb33-9">WiFiNINA         1.9.0</span>
<span id="cb33-10"></span>
<span id="cb33-11">Used platform     Version</span>
<span id="cb33-12">arduino:mbed_nano 4.2.1 </span></code></pre></div></div>
<p>Upload:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb34-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make</span> upload</span></code></pre></div></div>
<p>or</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb35-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">arduino-cli</span> upload <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> /dev/ttyACM0 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--fqbn</span> arduino:mbed_nano:nanorp2040connect web_server_rest_temp</span></code></pre></div></div>
<p>You should see something similar to this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb36-1">...</span>
<span id="cb36-2">New upload port: /dev/ttyACM0 (serial)</span></code></pre></div></div>
<p>Start the monitor to check the status of the running server:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb37-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make</span> monitor</span></code></pre></div></div>
<p>or</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb38-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">arduino-cli</span> monitor <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> /dev/ttyACM0</span></code></pre></div></div>
<p>Results will be similar to this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb39-1">Using default monitor configuration for board: arduino:mbed_nano:nanorp2040connect</span>
<span id="cb39-2">Monitor port settings:</span>
<span id="cb39-3">  baudrate=9600</span>
<span id="cb39-4">  bits=8</span>
<span id="cb39-5">  dtr=on</span>
<span id="cb39-6">  parity=none</span>
<span id="cb39-7">  rts=on</span>
<span id="cb39-8">  stop_bits=1</span>
<span id="cb39-9"></span>
<span id="cb39-10">Connecting to /dev/ttyACM0. Press CTRL-C to exit.</span>
<span id="cb39-11">SSID: (network name)</span>
<span id="cb39-12">IP Address: (server ip address)</span>
<span id="cb39-13">signal strength (RSSI): -40 dBm</span></code></pre></div></div>
</section>
<section id="call-the-service" class="level2">
<h2 class="anchored" data-anchor-id="call-the-service">Call the Service</h2>
<p>Now that we’ve completed our code, flashed the device, and our server is running, we’re ready to test it.</p>
<p>First, note the server address from the monitor above. I’ll use an example of 192.168.0.186.</p>
<p>There are many options for calling the service. You could use cURL:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb40-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">curl</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--request</span> GET <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--url</span> http://192.168.0.186:8090/Temperature/Current/F</span></code></pre></div></div>
<p>If you’re using a REST runner that recognizes .http files, your request will look something like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb41-1">GET http://192.168.0.186:8090/Temperature/Current/F</span></code></pre></div></div>
<p>You could also use a REST client like Postman or Insomnia. Since these are simple GET requests, you can even put the URL directly into a web browser. Regardless of how you call the service, though, you should see a response similar to this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode json code-with-copy"><code class="sourceCode json"><span id="cb42-1"><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">HTTP/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">OK</span></span>
<span id="cb42-2"><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Content-type:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">application/json</span></span>
<span id="cb42-3"><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Connection:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">close</span></span>
<span id="cb42-4"></span>
<span id="cb42-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb42-6">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"message"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Current temperature is 70 °F"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb42-7">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"value"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb42-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"status"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"success"</span></span>
<span id="cb42-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>If you call the service with an endpoint it doesn’t recognize, you’ll see this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb43" style="background: #f1f3f5;"><pre class="sourceCode json code-with-copy"><code class="sourceCode json"><span id="cb43-1"><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">HTTP/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">OK</span></span>
<span id="cb43-2"><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Content-type:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">application/json</span></span>
<span id="cb43-3"><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">Connection:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">close</span></span>
<span id="cb43-4"></span>
<span id="cb43-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb43-6">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"message"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello from Arduino RP2040! Valid endpoints are /Temperature/Current/F and /Temperature/Current/C"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb43-7">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"value"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-99</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb43-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">"status"</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"invalid"</span></span>
<span id="cb43-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</section>
<section id="next-steps" class="level2">
<h2 class="anchored" data-anchor-id="next-steps">Next Steps</h2>
<p>Now that your temperature monitoring service is up and running, a fun next step might be to go fully remote. You can easily test this by using a charger block. For example, I plugged my Nano RP2040 into a <a href="https://www.amazon.com/gp/product/B07T2NRK8G">BLAVOR Solar Charger Power Bank</a>.</p>


</section>

 ]]></description>
  <category>C</category>
  <category>Embedded and IoT</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2025/nano_rp2040_rest.html</guid>
  <pubDate>Sun, 02 Feb 2025 05:00:00 GMT</pubDate>
</item>
<item>
  <title>ESP8266 MicroPython Project with OLED</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2025/esp8266_oled.html</link>
  <description><![CDATA[ 






<p>In my <a href="../../posts/2025/esp8266_uv.html">last post</a>, we walked through setup of an ESP8266 project managed with UV. Now we’ll construct a more complex project, adding a simple OLED display.</p>
<section id="hardware" class="level1">
<h1>Hardware</h1>
<p>Parts needed:</p>
<ul>
<li>esp8266 board</li>
<li>OLED 0.96 inch Display</li>
<li>jumper wires</li>
<li>breadboard</li>
</ul>
<p>(All of this is included in the <a href="https://www.amazon.com/gp/product/B0BVZBTP8V">ESP32 Basic Starter Kit</a>).</p>
<p>Follow this schematic:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jcarr.codeberg.page/blog/posts/2025/images/es8266-oled_bb-1.jpg" class="img-fluid figure-img"></p>
<figcaption>Credit: <a href="https://randomnerdtutorials.com/micropython-oled-display-esp32-esp8266/" class="uri">https://randomnerdtutorials.com/micropython-oled-display-esp32-esp8266/</a></figcaption>
</figure>
</div>
<p>Connections:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>OLED</th>
<th>ESP8266</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>VCC</td>
<td>3.3V</td>
</tr>
<tr class="even">
<td>GND</td>
<td>GND</td>
</tr>
<tr class="odd">
<td>SCL</td>
<td>GPIO 5 (D1)</td>
</tr>
<tr class="even">
<td>SDA</td>
<td>GPIO 4 (D2)</td>
</tr>
</tbody>
</table>
</section>
<section id="software" class="level1">
<h1>Software</h1>
<section id="initialize-project" class="level2">
<h2 class="anchored" data-anchor-id="initialize-project">Initialize Project</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> esp8266_oled</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> esp8266_oled</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> init</span></code></pre></div></div>
</section>
<section id="add-dependencies" class="level2">
<h2 class="anchored" data-anchor-id="add-dependencies">Add Dependencies</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> add esptool</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> add adafruit-ampy</span></code></pre></div></div>
<p>Make sure you can communicate with the esp8266:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> tool run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--from</span> esptool esptool.py chip_id</span></code></pre></div></div>
</section>
<section id="flash-micropython-to-the-esp8266" class="level2">
<h2 class="anchored" data-anchor-id="flash-micropython-to-the-esp8266">Flash MicroPython to the ESP8266</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wget</span> https://micropython.org/resources/firmware/ESP8266_GENERIC-20241129-v1.24.1.bin</span>
<span id="cb4-2"></span>
<span id="cb4-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> tool run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--from</span> esptool esptool.py erase_flash</span>
<span id="cb4-4"></span>
<span id="cb4-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> tool run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--from</span> esptool esptool.py <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--baud</span> 460800 write_flash <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--flash_size</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>detect 0 ESP8266_GENERIC-20241129-v1.24.1.bin</span></code></pre></div></div>
</section>
<section id="create-scripts" class="level2">
<h2 class="anchored" data-anchor-id="create-scripts">Create Scripts</h2>
<p>The MicroPython standard library doesn’t include support for the OLED by default. We’ll create a new <code>ssd1306.py</code> file and update it with the required code.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">touch</span> ssd1306.py</span></code></pre></div></div>
<p>Copy the contents of <a href="https://github.com/RuiSantosdotme/ESP-MicroPython/raw/master/code/Others/OLED/ssd1306.py" class="uri">https://github.com/RuiSantosdotme/ESP-MicroPython/raw/master/code/Others/OLED/ssd1306.py</a> into the file:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># MicroPython SSD1306 OLED driver, I2C and SPI interfaces created by Adafruit</span></span>
<span id="cb6-2"></span>
<span id="cb6-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> time</span>
<span id="cb6-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> framebuf</span>
<span id="cb6-5"></span>
<span id="cb6-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># register definitions</span></span>
<span id="cb6-7">SET_CONTRAST        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> const(<span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x81</span>)</span>
<span id="cb6-8">SET_ENTIRE_ON       <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> const(<span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0xa4</span>)</span>
<span id="cb6-9">SET_NORM_INV        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> const(<span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0xa6</span>)</span>
<span id="cb6-10">SET_DISP            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> const(<span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0xae</span>)</span>
<span id="cb6-11">SET_MEM_ADDR        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> const(<span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x20</span>)</span>
<span id="cb6-12">SET_COL_ADDR        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> const(<span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x21</span>)</span>
<span id="cb6-13">SET_PAGE_ADDR       <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> const(<span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x22</span>)</span>
<span id="cb6-14">SET_DISP_START_LINE <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> const(<span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x40</span>)</span>
<span id="cb6-15">SET_SEG_REMAP       <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> const(<span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0xa0</span>)</span>
<span id="cb6-16">SET_MUX_RATIO       <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> const(<span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0xa8</span>)</span>
<span id="cb6-17">SET_COM_OUT_DIR     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> const(<span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0xc0</span>)</span>
<span id="cb6-18">SET_DISP_OFFSET     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> const(<span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0xd3</span>)</span>
<span id="cb6-19">SET_COM_PIN_CFG     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> const(<span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0xda</span>)</span>
<span id="cb6-20">SET_DISP_CLK_DIV    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> const(<span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0xd5</span>)</span>
<span id="cb6-21">SET_PRECHARGE       <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> const(<span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0xd9</span>)</span>
<span id="cb6-22">SET_VCOM_DESEL      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> const(<span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0xdb</span>)</span>
<span id="cb6-23">SET_CHARGE_PUMP     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> const(<span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x8d</span>)</span>
<span id="cb6-24"></span>
<span id="cb6-25"></span>
<span id="cb6-26"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> SSD1306:</span>
<span id="cb6-27">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, width, height, external_vcc):</span>
<span id="cb6-28">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> width</span>
<span id="cb6-29">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.height <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> height</span>
<span id="cb6-30">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.external_vcc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> external_vcc</span>
<span id="cb6-31">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.pages <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.height <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span></span>
<span id="cb6-32">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Note the subclass must initialize self.framebuf to a framebuffer.</span></span>
<span id="cb6-33">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This is necessary because the underlying data buffer is different</span></span>
<span id="cb6-34">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># between I2C and SPI implementations (I2C needs an extra byte).</span></span>
<span id="cb6-35">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.poweron()</span>
<span id="cb6-36">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.init_display()</span>
<span id="cb6-37"></span>
<span id="cb6-38">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> init_display(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb6-39">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> cmd <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> (</span>
<span id="cb6-40">            SET_DISP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x00</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># off</span></span>
<span id="cb6-41">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># address setting</span></span>
<span id="cb6-42">            SET_MEM_ADDR, <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x00</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># horizontal</span></span>
<span id="cb6-43">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># resolution and layout</span></span>
<span id="cb6-44">            SET_DISP_START_LINE <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x00</span>,</span>
<span id="cb6-45">            SET_SEG_REMAP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x01</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># column addr 127 mapped to SEG0</span></span>
<span id="cb6-46">            SET_MUX_RATIO, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.height <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb6-47">            SET_COM_OUT_DIR <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x08</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># scan from COM[N] to COM0</span></span>
<span id="cb6-48">            SET_DISP_OFFSET, <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x00</span>,</span>
<span id="cb6-49">            SET_COM_PIN_CFG, <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x02</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.height <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x12</span>,</span>
<span id="cb6-50">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># timing and driving scheme</span></span>
<span id="cb6-51">            SET_DISP_CLK_DIV, <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x80</span>,</span>
<span id="cb6-52">            SET_PRECHARGE, <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x22</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.external_vcc <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0xf1</span>,</span>
<span id="cb6-53">            SET_VCOM_DESEL, <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x30</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.83*Vcc</span></span>
<span id="cb6-54">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># display</span></span>
<span id="cb6-55">            SET_CONTRAST, <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0xff</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># maximum</span></span>
<span id="cb6-56">            SET_ENTIRE_ON, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># output follows RAM contents</span></span>
<span id="cb6-57">            SET_NORM_INV, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># not inverted</span></span>
<span id="cb6-58">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># charge pump</span></span>
<span id="cb6-59">            SET_CHARGE_PUMP, <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x10</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.external_vcc <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x14</span>,</span>
<span id="cb6-60">            SET_DISP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x01</span>): <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># on</span></span>
<span id="cb6-61">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.write_cmd(cmd)</span>
<span id="cb6-62">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.fill(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb6-63">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.show()</span>
<span id="cb6-64"></span>
<span id="cb6-65">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> poweroff(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb6-66">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.write_cmd(SET_DISP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x00</span>)</span>
<span id="cb6-67"></span>
<span id="cb6-68">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> contrast(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, contrast):</span>
<span id="cb6-69">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.write_cmd(SET_CONTRAST)</span>
<span id="cb6-70">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.write_cmd(contrast)</span>
<span id="cb6-71"></span>
<span id="cb6-72">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> invert(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, invert):</span>
<span id="cb6-73">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.write_cmd(SET_NORM_INV <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> (invert <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb6-74"></span>
<span id="cb6-75">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> show(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb6-76">        x0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb6-77">        x1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb6-78">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>:</span>
<span id="cb6-79">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># displays with width of 64 pixels are shifted by 32</span></span>
<span id="cb6-80">            x0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span></span>
<span id="cb6-81">            x1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span></span>
<span id="cb6-82">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.write_cmd(SET_COL_ADDR)</span>
<span id="cb6-83">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.write_cmd(x0)</span>
<span id="cb6-84">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.write_cmd(x1)</span>
<span id="cb6-85">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.write_cmd(SET_PAGE_ADDR)</span>
<span id="cb6-86">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.write_cmd(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb6-87">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.write_cmd(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.pages <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb6-88">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.write_framebuf()</span>
<span id="cb6-89"></span>
<span id="cb6-90">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> fill(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, col):</span>
<span id="cb6-91">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.framebuf.fill(col)</span>
<span id="cb6-92"></span>
<span id="cb6-93">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> pixel(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, x, y, col):</span>
<span id="cb6-94">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.framebuf.pixel(x, y, col)</span>
<span id="cb6-95"></span>
<span id="cb6-96">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> scroll(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, dx, dy):</span>
<span id="cb6-97">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.framebuf.scroll(dx, dy)</span>
<span id="cb6-98"></span>
<span id="cb6-99">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> text(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, string, x, y, col<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb6-100">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.framebuf.text(string, x, y, col)</span>
<span id="cb6-101"></span>
<span id="cb6-102"></span>
<span id="cb6-103"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> SSD1306_I2C(SSD1306):</span>
<span id="cb6-104">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, width, height, i2c, addr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x3c</span>, external_vcc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>):</span>
<span id="cb6-105">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.i2c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i2c</span>
<span id="cb6-106">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.addr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> addr</span>
<span id="cb6-107">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.temp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb6-108">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add an extra byte to the data buffer to hold an I2C data/command byte</span></span>
<span id="cb6-109">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># to use hardware-compatible I2C transactions.  A memoryview of the</span></span>
<span id="cb6-110">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># buffer is used to mask this byte from the framebuffer operations</span></span>
<span id="cb6-111">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># (without a major memory hit as memoryview doesn't copy to a separate</span></span>
<span id="cb6-112">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># buffer).</span></span>
<span id="cb6-113">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">buffer</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>(((height <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> width) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb6-114">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">buffer</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x40</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set first byte of data buffer to Co=0, D/C=1</span></span>
<span id="cb6-115">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.framebuf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> framebuf.FrameBuffer1(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">memoryview</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">buffer</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:], width, height)</span>
<span id="cb6-116">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(width, height, external_vcc)</span>
<span id="cb6-117"></span>
<span id="cb6-118">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> write_cmd(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, cmd):</span>
<span id="cb6-119">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.temp[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x80</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Co=1, D/C#=0</span></span>
<span id="cb6-120">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.temp[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cmd</span>
<span id="cb6-121">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.i2c.writeto(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.addr, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.temp)</span>
<span id="cb6-122"></span>
<span id="cb6-123">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> write_framebuf(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb6-124">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Blast out the frame buffer using a single I2C transaction to support</span></span>
<span id="cb6-125">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># hardware I2C interfaces.</span></span>
<span id="cb6-126">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.i2c.writeto(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.addr, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">buffer</span>)</span>
<span id="cb6-127"></span>
<span id="cb6-128">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> poweron(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb6-129">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pass</span></span>
<span id="cb6-130"></span>
<span id="cb6-131"></span>
<span id="cb6-132"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> SSD1306_SPI(SSD1306):</span>
<span id="cb6-133">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, width, height, spi, dc, res, cs, external_vcc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>):</span>
<span id="cb6-134">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.rate <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1024</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1024</span></span>
<span id="cb6-135">        dc.init(dc.OUT, value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb6-136">        res.init(res.OUT, value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb6-137">        cs.init(cs.OUT, value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb6-138">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.spi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spi</span>
<span id="cb6-139">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dc</span>
<span id="cb6-140">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.res <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> res</span>
<span id="cb6-141">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.cs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cs</span>
<span id="cb6-142">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">buffer</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>((height <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> width)</span>
<span id="cb6-143">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.framebuf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> framebuf.FrameBuffer1(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">buffer</span>, width, height)</span>
<span id="cb6-144">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(width, height, external_vcc)</span>
<span id="cb6-145"></span>
<span id="cb6-146">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> write_cmd(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, cmd):</span>
<span id="cb6-147">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.spi.init(baudrate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.rate, polarity<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, phase<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb6-148">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.cs.high()</span>
<span id="cb6-149">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dc.low()</span>
<span id="cb6-150">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.cs.low()</span>
<span id="cb6-151">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.spi.write(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>([cmd]))</span>
<span id="cb6-152">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.cs.high()</span>
<span id="cb6-153"></span>
<span id="cb6-154">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> write_framebuf(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb6-155">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.spi.init(baudrate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.rate, polarity<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, phase<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb6-156">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.cs.high()</span>
<span id="cb6-157">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.dc.high()</span>
<span id="cb6-158">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.cs.low()</span>
<span id="cb6-159">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.spi.write(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">buffer</span>)</span>
<span id="cb6-160">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.cs.high()</span>
<span id="cb6-161"></span>
<span id="cb6-162">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> poweron(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb6-163">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.res.high()</span>
<span id="cb6-164">        time.sleep_ms(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb6-165">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.res.low()</span>
<span id="cb6-166">        time.sleep_ms(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb6-167">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.res.high()</span></code></pre></div></div>
<p>Create and update <code>main.py</code>. This is our entry point.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">touch</span> main.py</span></code></pre></div></div>
<ol type="1">
<li>Copy contents of <a href="https://github.com/RuiSantosdotme/ESP-MicroPython/raw/master/code/Others/OLED/main.py" class="uri">https://github.com/RuiSantosdotme/ESP-MicroPython/raw/master/code/Others/OLED/main.py</a> into the file, then</li>
<li>Comment the esp32 pin assignment and uncomment the esp8266 pin assignment</li>
</ol>
<p>Contents should end up like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Complete project details at https://RandomNerdTutorials.com/micropython-programming-with-esp32-and-esp8266/</span></span>
<span id="cb8-2"></span>
<span id="cb8-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> machine <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Pin, SoftI2C</span>
<span id="cb8-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ssd1306</span>
<span id="cb8-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> time <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> sleep</span>
<span id="cb8-6"></span>
<span id="cb8-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ESP32 Pin assignment</span></span>
<span id="cb8-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># i2c = SoftI2C(scl=Pin(22), sda=Pin(21)) # esp32</span></span>
<span id="cb8-9"></span>
<span id="cb8-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ESP8266 Pin assignment</span></span>
<span id="cb8-11">i2c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> SoftI2C(scl<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>Pin(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>), sda<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>Pin(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb8-12"></span>
<span id="cb8-13">oled_width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span></span>
<span id="cb8-14">oled_height <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span></span>
<span id="cb8-15">oled <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)</span>
<span id="cb8-16"></span>
<span id="cb8-17">oled.text(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Hello, World!'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb8-18">oled.text(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Hello, World 2!'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb8-19">oled.text(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Hello, World 3!'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span>
<span id="cb8-20"></span>
<span id="cb8-21">oled.show()</span></code></pre></div></div>
</section>
<section id="upload-scripts-to-esp3266" class="level2">
<h2 class="anchored" data-anchor-id="upload-scripts-to-esp3266">Upload Scripts to ESP3266</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb9-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> tool run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--from</span> adafruit-ampy ampy <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> /dev/ttyUSB0 put ssd1306.py</span>
<span id="cb9-2"></span>
<span id="cb9-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> tool run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--from</span> adafruit-ampy ampy <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> /dev/ttyUSB0 put main.py</span>
<span id="cb9-4"></span>
<span id="cb9-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> tool run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--from</span> adafruit-ampy ampy <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> /dev/ttyUSB0 ls</span></code></pre></div></div>
</section>
<section id="run-the-main-script" class="level2">
<h2 class="anchored" data-anchor-id="run-the-main-script">Run the Main Script</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb10-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> tool run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--from</span> adafruit-ampy ampy <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> /dev/ttyUSB0 run main.py</span></code></pre></div></div>
<p>On the OLED display you should see this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb11-1">Hello, World!</span>
<span id="cb11-2">Hello, World 2!</span>
<span id="cb11-3">Hello, World 3!</span></code></pre></div></div>
<p>You can learn more about what the code is doing here: <a href="https://randomnerdtutorials.com/micropython-oled-display-esp32-esp8266/" class="uri">https://randomnerdtutorials.com/micropython-oled-display-esp32-esp8266/</a></p>


</section>
</section>

 ]]></description>
  <category>Python</category>
  <category>Embedded and IoT</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2025/esp8266_oled.html</guid>
  <pubDate>Sun, 12 Jan 2025 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Using UV To Manage a ESP8266 MicroPython Project</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2025/esp8266_uv.html</link>
  <description><![CDATA[ 






<p>Following up from my <a href="../../posts/2025/single_file_depend_uv_python.html">previous UV post</a>, today I’ll walk through managing an ESP8266 microcontroller project with UV. I’ll stick to the basics with this one, but I’ll implement a more complex project in a future article.</p>
<section id="background" class="level2">
<h2 class="anchored" data-anchor-id="background">Background</h2>
<blockquote class="blockquote">
<p>The ESP8266 is a low-cost Wi-Fi microcontroller, with built-in TCP/IP networking software, and microcontroller capability, produced by Espressif Systems in Shanghai, China.</p>
<p>The chip was popularized in the English-speaking maker community in August 2014 via the ESP-01 module, made by a third-party manufacturer Ai-Thinker. This small module allows microcontrollers to connect to a Wi-Fi network and make simple TCP/IP connections using Hayes-style commands. However, at first, there was almost no English-language documentation on the chip and the commands it accepted. The very low price and the fact that there were very few external components on the module, which suggested that it could eventually be very inexpensive in volume, attracted many hackers to explore the module, the chip, and the software on it, as well as to translate the Chinese documentation.</p>
<p>from <a href="https://en.wikipedia.org/wiki/ESP8266">Wikipedia</a></p>
</blockquote>
<p>I’m using the <a href="https://www.amazon.com/gp/product/B0BVZBTP8V">ESP32 Basic Starter Kit</a>, which contains an ESP8266 module with the following specifications:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Description</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Model</td>
<td>ESP8266MOD</td>
</tr>
<tr class="even">
<td>Vendor</td>
<td>AI-THINKER</td>
</tr>
<tr class="odd">
<td>ISM</td>
<td>2.4 GHz</td>
</tr>
<tr class="even">
<td>PA</td>
<td>+25 dBm</td>
</tr>
<tr class="odd">
<td>Wireless</td>
<td>802.11b/g/n</td>
</tr>
</tbody>
</table>
</section>
<section id="project-setup" class="level2">
<h2 class="anchored" data-anchor-id="project-setup">Project Setup</h2>
<p>Adapted from here: <a href="https://docs.micropython.org/en/latest/esp8266/tutorial/intro.html" class="uri">https://docs.micropython.org/en/latest/esp8266/tutorial/intro.html</a></p>
<p>Make sure your ESP8266 is connected.</p>
<section id="initialize-project-install-esp-tools" class="level3">
<h3 class="anchored" data-anchor-id="initialize-project-install-esp-tools">Initialize Project, Install ESP Tools</h3>
<p>Create a project directory and cd into it:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> esp_test</span>
<span id="cb1-2"> </span>
<span id="cb1-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> esp_test</span></code></pre></div></div>
<p>Initialize the project:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> init</span></code></pre></div></div>
<p>The <a href="https://pypi.org/project/esptool/">esptool utility</a> is used to communicate with the ROM bootloader in Expressif chips.</p>
<p>Add the esptool package:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> add esptool</span></code></pre></div></div>
<p>Show available commands:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> tool run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--from</span> esptool esptool.py</span></code></pre></div></div>
<p>Commands:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 52%">
<col style="width: 47%">
</colgroup>
<thead>
<tr class="header">
<th>Command Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>load_ram</td>
<td>Download an image to RAM and execute</td>
</tr>
<tr class="even">
<td>dump_mem</td>
<td>Dump arbitrary memory to disk</td>
</tr>
<tr class="odd">
<td>read_mem</td>
<td>Read arbitrary memory location</td>
</tr>
<tr class="even">
<td>write_mem</td>
<td>Read-modify-write to arbitrary memory location</td>
</tr>
<tr class="odd">
<td>write_flash</td>
<td>Write a binary blob to flash</td>
</tr>
<tr class="even">
<td>run</td>
<td>Run application code in flash</td>
</tr>
<tr class="odd">
<td>image_info</td>
<td>Dump headers from a binary file (bootloader or application)</td>
</tr>
<tr class="even">
<td>make_image</td>
<td>Create an application image from binary files</td>
</tr>
<tr class="odd">
<td>elf2image</td>
<td>Create an application image from ELF file</td>
</tr>
<tr class="even">
<td>read_mac</td>
<td>Read MAC address from OTP ROM</td>
</tr>
<tr class="odd">
<td>chip_id</td>
<td>Read Chip ID from OTP ROM</td>
</tr>
<tr class="even">
<td>flash_id</td>
<td>Read SPI flash manufacturer and device ID</td>
</tr>
<tr class="odd">
<td>read_flash_status</td>
<td>Read SPI flash status register</td>
</tr>
<tr class="even">
<td>write_flash_status</td>
<td>Write SPI flash status register</td>
</tr>
<tr class="odd">
<td>read_flash</td>
<td>Read SPI flash content</td>
</tr>
<tr class="even">
<td>verify_flash</td>
<td>Verify a binary blob against flash</td>
</tr>
<tr class="odd">
<td>erase_flash</td>
<td>Perform Chip Erase on SPI flash</td>
</tr>
<tr class="even">
<td>erase_region</td>
<td>Erase a region of the flash</td>
</tr>
<tr class="odd">
<td>read_flash_sfdp</td>
<td>Read SPI flash SFDP (Serial Flash Discoverable Parameters)</td>
</tr>
<tr class="even">
<td>merge_bin</td>
<td>Merge multiple raw binary files into a single file for later flashing</td>
</tr>
<tr class="odd">
<td>get_security_info</td>
<td>Get some security-related data</td>
</tr>
<tr class="even">
<td>version</td>
<td>Print esptool version</td>
</tr>
</tbody>
</table>
<p>Get chip information:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> tool run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--from</span> esptool esptool.py chip_id</span></code></pre></div></div>
<p>Output:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb6-1">esptool.py v4.8.1</span>
<span id="cb6-2">Found 33 serial ports</span>
<span id="cb6-3">Serial port /dev/ttyUSB0</span>
<span id="cb6-4">Connecting....</span>
<span id="cb6-5">Detecting chip type... Unsupported detection protocol, switching and trying again...</span>
<span id="cb6-6">Connecting....</span>
<span id="cb6-7">Detecting chip type... ESP8266</span>
<span id="cb6-8">Chip is ESP8266EX</span>
<span id="cb6-9">Features: WiFi</span>
<span id="cb6-10">Crystal is 26MHz</span>
<span id="cb6-11">MAC: //(redacted)//</span>
<span id="cb6-12">Uploading stub...</span>
<span id="cb6-13">Running stub...</span>
<span id="cb6-14">Stub running...</span>
<span id="cb6-15">Chip ID: //(redacted)//</span>
<span id="cb6-16">Hard resetting via RTS pin...</span></code></pre></div></div>
</section>
<section id="install-micropython" class="level3">
<h3 class="anchored" data-anchor-id="install-micropython">Install MicroPython</h3>
<blockquote class="blockquote">
<p><strong>MicroPython</strong> is a software implementation of a programming language largely compatible with Python 3, written in C, that is optimized to run on a microcontroller.</p>
<p>MicroPython consists of a Python compiler to bytecode and a runtime interpreter of that bytecode. The user is presented with an interactive prompt (the REPL) to execute supported commands immediately. Included are a selection of core Python libraries; MicroPython includes modules which give the programmer access to low-level hardware.</p>
<p>from <a href="https://en.wikipedia.org/wiki/MicroPython">Wikipedia</a></p>
</blockquote>
<p>Erase flash:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> tool run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--from</span> esptool esptool.py erase_flash</span></code></pre></div></div>
<p>Get the MicroPython firmware:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wget</span> https://micropython.org/resources/firmware/ESP8266_GENERIC-20241129-v1.24.1.bin</span></code></pre></div></div>
<p>Flash the firmware to the ESP8266:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb9-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> tool run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--from</span> esptool esptool.py <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--baud</span> 460800 write_flash <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--flash_size</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>detect 0 ESP8266_GENERIC-20241129-v1.24.1.bin</span></code></pre></div></div>
</section>
<section id="test-micropython" class="level3">
<h3 class="anchored" data-anchor-id="test-micropython">Test MicroPython</h3>
<p>Install <a href="https://linux.die.net/man/8/picocom">picocom</a> (if needed) and connect to <a href="https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop">REPL</a>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sudo</span> apt install picocom</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb11-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">picocom</span> /dev/ttyUSB0 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-b115200</span></span></code></pre></div></div>
<p>Test the REPL:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb12-1">&gt;&gt;&gt; print('hello esp8266!')</span>
<span id="cb12-2"></span>
<span id="cb12-3">hello esp8266!</span></code></pre></div></div>
</section>
<section id="upload-and-run-a-python-script" class="level3">
<h3 class="anchored" data-anchor-id="upload-and-run-a-python-script">Upload and Run a Python Script</h3>
<p>Adapted from here: <a href="https://problemsolvingwithpython.com/12-MicroPython/12.06-Uploading-Code/" class="uri">https://problemsolvingwithpython.com/12-MicroPython/12.06-Uploading-Code/</a></p>
<blockquote class="blockquote">
<p>MicroPython Tool (ampy) is a utility to interact with a CircuitPython or MicroPython board over a serial connection.</p>
<p>Ampy is meant to be a simple command line tool to manipulate files and run code on a CircuitPython or MicroPython board over its serial connection. With ampy you can send files from your computer to the board’s file system, download files from a board to your computer, and even send a Python script to a board to be executed.</p>
<p>from <a href="https://pypi.org/project/adafruit-ampy/">PyPI</a></p>
</blockquote>
<p>Install ampy:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb13-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> add adafruit-ampy</span></code></pre></div></div>
<p>Upload a script, then list the file contents of the microcontroller to confirm it’s there:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb14-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> tool run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--from</span> adafruit-ampy ampy <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> /dev/ttyUSB0 put hello.py</span>
<span id="cb14-2"></span>
<span id="cb14-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> tool run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--from</span> adafruit-ampy ampy <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> /dev/ttyUSB0 ls</span></code></pre></div></div>
<p>Output:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb15-1">/boot.py</span>
<span id="cb15-2">/hello.py</span></code></pre></div></div>
<p>Run the uploaded script:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb16-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> tool run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--from</span> adafruit-ampy ampy <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> /dev/ttyUSB0 run hello.py</span></code></pre></div></div>
<p>Output:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb17-1">Hello from esp-test!</span></code></pre></div></div>
</section>
</section>
<section id="links" class="level2">
<h2 class="anchored" data-anchor-id="links">Links</h2>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Description</th>
<th>URL</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>ESP32 Technical Reference</td>
<td><a href="https://www.espressif.com/sites/default/files/documentation/esp8266-technical_reference_en.pdf" class="uri">https://www.espressif.com/sites/default/files/documentation/esp8266-technical_reference_en.pdf</a></td>
</tr>
<tr class="even">
<td>ESP8266MOD Datasheet PDF – Wi-Fi Module – Espressif</td>
<td><a href="https://www.datasheetcafe.com/esp8266mod-datasheet-wi-fi-module/" class="uri">https://www.datasheetcafe.com/esp8266mod-datasheet-wi-fi-module/</a></td>
</tr>
<tr class="odd">
<td>MicroPython Downloads</td>
<td><a href="https://micropython.org/download/" class="uri">https://micropython.org/download/</a></td>
</tr>
</tbody>
</table>


</section>

 ]]></description>
  <category>Python</category>
  <category>Embedded and IoT</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2025/esp8266_uv.html</guid>
  <pubDate>Sun, 05 Jan 2025 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Single-File Dependency Management in Python Script Using UV</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2025/single_file_depend_uv_python.html</link>
  <description><![CDATA[ 






<p><a href="https://docs.astral.sh/uv/">UV</a> deserves a comprehensive overview post, but today I’m going to discuss a specific convenience feature that I <em>love</em>.</p>
<p>In the past, whenever I’ve had a Python script with a dependency that I want to manage in an isolated environment, I’ve set up a separate Bash script to bootstrap it by activating the virtual environment and then running the Python script. Ugly and bloated, but it works. With UV, however, you can streamline this by using inline dependency management.</p>
<p>Let’s walk through an example. (Make sure you already have <a href="https://docs.astral.sh/uv/getting-started/installation/">uv installed</a>.)</p>
<p>I don’t have Numpy installed globally, so we’ll use that in our example script. Create a file named <code>numpy_version.py</code> with the following contents:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> show_numpy_version():</span>
<span id="cb1-4">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(np.__version__)</span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'__main__'</span>:</span>
<span id="cb1-7">    show_numpy_version()</span></code></pre></div></div>
<p>If we try to run it as-is (<code>python3 numpy_version.py</code>) we see this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb2-1">Traceback (most recent call last):</span>
<span id="cb2-2">  File "numpy_version.py", line 1, in &lt;module&gt;</span>
<span id="cb2-3">    import numpy as np</span>
<span id="cb2-4">ModuleNotFoundError: No module named 'numpy'</span></code></pre></div></div>
<p>We can use uv to add inline metadata declaring the dependencies:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> add <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--script</span> numpy_version.py numpy</span></code></pre></div></div>
<p>The script will be updated to look like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># /// script</span></span>
<span id="cb4-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># requires-python = "&gt;=3.12"</span></span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># dependencies = [</span></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#     "numpy",</span></span>
<span id="cb4-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ]</span></span>
<span id="cb4-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ///</span></span>
<span id="cb4-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb4-8"></span>
<span id="cb4-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> show_numpy_version():</span>
<span id="cb4-10">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(np.__version__)</span>
<span id="cb4-11"></span>
<span id="cb4-12"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'__main__'</span>:</span>
<span id="cb4-13">    show_numpy_version()</span></code></pre></div></div>
<p>Then, we use uv to run it:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uv</span> run numpy_version.py</span></code></pre></div></div>
<p>And we get this output:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb6-1">Reading inline script metadata from `numpy_version.py`</span>
<span id="cb6-2">2.2.1</span></code></pre></div></div>
<p>We can make our script directly runnable by adding a shebang line at the top of the file:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#!/usr/bin/env -S uv run</span></span></code></pre></div></div>
<p>…and making the script executable:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chmod</span> u+x numpy_version.py</span></code></pre></div></div>
<blockquote class="blockquote">
<p>The -S argument to env is required in order for the run argument to be handled correctly.</p>
</blockquote>
<p>Now we can run the script as <code>./numpy_version.py</code>.</p>
<p>When we run the script like this, uv automatically manages the dependencies in an isolated environment. We can test this by trying to run the updated script directly with Python again:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb9-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">python3</span> numpy_version.py</span></code></pre></div></div>
<p>We get this, indicating that in a global context Numpy is still not installed:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb10-1">Traceback (most recent call last):</span>
<span id="cb10-2">  File "numpy_version.py", line 10, in &lt;module&gt;</span>
<span id="cb10-3">    import numpy as np</span>
<span id="cb10-4">ModuleNotFoundError: No module named 'numpy'</span></code></pre></div></div>
<p>Our end result is a nice, clean, directly runnable, isolated script whose only execution requirement is that uv be installed.</p>
<p>The portability that this provides is <em>really</em> nice. I installed uv on a <a href="https://libre.computer/products/aml-s905x-cc/">Le Potato</a> and I was able to immediately run my script on the device with no changes. UV even automatically installed a CPython version to meet the Python version requirement. (The inline metadata specifies a Python version &gt;= 3.12, but the global Python interpreter on the SBC is 3.11)</p>



 ]]></description>
  <category>Python</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2025/single_file_depend_uv_python.html</guid>
  <pubDate>Wed, 01 Jan 2025 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Total Dissolved Solids Measurements Using Arduino</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2024/tds_sensor_arduino.html</link>
  <description><![CDATA[ 






<p>Total dissolved solids (TDS) refer to the combined content of all inorganic and organic substances contained in a liquid, typically water, that are present in a molecular, ionized, or micro-granular form. TDS is usually measured in milligrams per liter (mg/L) or parts per million (ppm). (I’ll be using ppm in this article)</p>
<p>The components of TDS can include a variety of substances, such as:</p>
<ul>
<li>Salts (e.g., sodium, calcium, magnesium, potassium)</li>
<li>Minerals (e.g., bicarbonates, sulfates, chlorides)</li>
<li>Organic matter (e.g., humic substances)</li>
</ul>
<p>TDS is an important parameter in water quality assessment, as it can affect the physical and chemical properties of water, including its taste, clarity, and suitability for drinking, irrigation, and industrial processes. High levels of TDS can indicate water pollution or the presence of excessive minerals, which may be harmful to health or the environment.</p>
<section id="hardware" class="level1">
<h1>Hardware</h1>
<ul>
<li><a href="https://store-usa.arduino.cc/products/arduino-uno-rev3">Arduino Uno board</a></li>
<li><a href="https://www.cqrobot.com/index.php?route=product/product&amp;product_id=1122">CQRobot TDS Sensor</a></li>
<li><a href="https://www.amazon.com/Premium-Breadboard-Jumper-100-Pack-Hellotronics/dp/B07GJLH7V1">male-to-male jumper wires</a></li>
</ul>
</section>
<section id="software" class="level1">
<h1>Software</h1>
<p>You are welcome to use whichever software combination you prefer. However, I will be using the following tools, and the instructions in this article will be based on that setup:</p>
<ul>
<li>Ubuntu Linux (<em>I’m using <a href="https://ubuntu-mate.org/">Ubuntu MATE</a>, specifically</em>)</li>
<li><a href="https://zed.dev/">Zed editor</a></li>
<li><a href="https://arduino.github.io/arduino-cli/">Arduino CLI</a></li>
</ul>
</section>
<section id="hardware-setup" class="level1">
<h1>Hardware Setup</h1>
<p>Connect the TDS Sensor controller to the Arduino’s 5V, Ground, and A1 pins, and suspend the sensor probe in a container of water:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jcarr.codeberg.page/blog/posts/2024/images/tds/CQRSENTDS01-107.jpg" class="img-fluid figure-img"></p>
<figcaption>Source: <a href="http://www.cqrobot.wiki/index.php/File:CQRSENTDS01-107.jpg">CQRobot wiki</a></figcaption>
</figure>
</div>
</section>
<section id="code-and-test" class="level1">
<h1>Code and Test</h1>
<p>Use the Arduino CLI to create a sketch:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">arduino-cli</span> sketch new tds_arduino</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> tds_arduino</span></code></pre></div></div>
<p>Create a new <code>Makefile</code>. This will simplify compiling, uploading, and monitoring our Arduino sketch:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode makefile code-with-copy"><code class="sourceCode makefile"><span id="cb2-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CORE</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> arduino:avr</span></span>
<span id="cb2-2"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">FQBN</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">$(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CORE</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">:uno</span></span>
<span id="cb2-3"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CLI_EXE</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> arduino-cli</span></span>
<span id="cb2-4"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">PORT</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> /dev/ttyACM0</span></span>
<span id="cb2-5"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">BAUD</span> <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> 115200</span></span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">default:</span></span>
<span id="cb2-8"><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">    </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">@</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">echo </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Targets:'</span></span>
<span id="cb2-9">    <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">@</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">echo </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'  list'</span></span>
<span id="cb2-10">    <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">@</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">echo </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'  compile'</span></span>
<span id="cb2-11">    <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">@</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">echo </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'  upload'</span></span>
<span id="cb2-12">    <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">@</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">echo </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'  monitor'</span></span>
<span id="cb2-13"></span>
<span id="cb2-14"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">list:</span></span>
<span id="cb2-15"><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">    </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">$(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CLI_EXE</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span> board list</span>
<span id="cb2-16"></span>
<span id="cb2-17"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">compile:</span></span>
<span id="cb2-18"><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">    </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">$(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CLI_EXE</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span> compile --fqbn <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">$(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">FQBN</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span> .</span>
<span id="cb2-19"></span>
<span id="cb2-20"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">upload:</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;"> compile</span></span>
<span id="cb2-21"><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">    </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">$(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CLI_EXE</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span> upload -p <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">$(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">PORT</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span> --fqbn <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">$(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">FQBN</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span> .</span>
<span id="cb2-22"></span>
<span id="cb2-23"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">monitor:</span></span>
<span id="cb2-24"><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">    </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">$(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">CLI_EXE</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span> monitor -p <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">$(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">PORT</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span> --config <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">$(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">BAUD</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>Update the contents of <code>tds_arduino.ino</code> as follows:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb3-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#define TdsSensorPin </span>A1</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> setup<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Initialize serial communication at a baud rate of 115200 bits per second:</span></span>
<span id="cb3-5">  Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>begin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">115200</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb3-6"></span>
<span id="cb3-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Set the mode of the pin to INPUT: This pin will be used to read data from</span></span>
<span id="cb3-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// the TDS sensor:</span></span>
<span id="cb3-9">  pinMode<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>TdsSensorPin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb3-10"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb3-11"></span>
<span id="cb3-12"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> loop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">static</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">unsigned</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> analogSampleTimepoint <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> millis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb3-14"></span>
<span id="cb3-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// take a reading and display it every 1/2 second:</span></span>
<span id="cb3-16">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>millis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> analogSampleTimepoint <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">U</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-17">    analogSampleTimepoint <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> millis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb3-18"></span>
<span id="cb3-19">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// get sensor pin value</span></span>
<span id="cb3-20">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// https://docs.arduino.cc/language-reference/en/functions/analog-io/analogRead/</span></span>
<span id="cb3-21">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> sensorValue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> analogRead<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>TdsSensorPin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb3-22"></span>
<span id="cb3-23">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sensor value: "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb3-24">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>sensorValue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb3-25">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb3-26"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Compile and upload the sketch, then run the monitor:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make</span> upload</span>
<span id="cb4-2"></span>
<span id="cb4-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make</span> monitor</span></code></pre></div></div>
<p>Results:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb5-1">Sensor value: 228</span>
<span id="cb5-2">Sensor value: 228</span>
<span id="cb5-3">Sensor value: 227</span>
<span id="cb5-4">Sensor value: 228</span>
<span id="cb5-5">Sensor value: 229</span>
<span id="cb5-6">Sensor value: 229</span>
<span id="cb5-7">Sensor value: 229</span>
<span id="cb5-8">Sensor value: 228</span></code></pre></div></div>
<p>The values we’re capturing at this point are <em>not</em> ppm of total dissolved solids: They’re voltage levels being reported from the sensor pin. Moreover, they aren’t <em>actual</em> voltage levels, but an abstraction of them. (The documentation for <code>analogRead</code> explains it well: <a href="https://docs.arduino.cc/language-reference/en/functions/analog-io/analogRead/" class="uri">https://docs.arduino.cc/language-reference/en/functions/analog-io/analogRead/</a>)</p>
<p>In order to be useful, we need to convert them. First we’ll add some global values:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb6-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> referenceVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb6-2">    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// analog reference voltage of the analog-to-digital convertor (ADC)</span></span>
<span id="cb6-3"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> tdsValue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div></div>
<p>Next, our conversion function:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb7-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> getTdsValue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> sensorValue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> temperatureAtCollection<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Convert the sensor pin reading to actual voltage:</span></span>
<span id="cb7-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> sensorVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>sensorValue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> referenceVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1024.0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-4"></span>
<span id="cb7-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// temperature compensation formula: fFinalResult(25^C) =</span></span>
<span id="cb7-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// fFinalResult(current)/(1.0+0.02*(fTP-25.0))</span></span>
<span id="cb7-7">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> compensationCoefficient <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>temperatureAtCollection <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-8"></span>
<span id="cb7-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// temperature compensation</span></span>
<span id="cb7-10">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> compensationVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sensorVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> compensationCoefficient<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-11"></span>
<span id="cb7-12">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// convert voltage value to tds value</span></span>
<span id="cb7-13">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> calculatedTdsValue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb7-14">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">133.42</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> compensationVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> compensationVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb7-15">           compensationVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span></span>
<span id="cb7-16">       <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">255.86</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> compensationVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> compensationVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-17">       <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">857.39</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> compensationVoltage<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb7-18">      <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-19"></span>
<span id="cb7-20">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> calculatedTdsValue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-21"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>The function takes in two values: the sensor reading and the temperature at the time of collection. (Since we aren’t working with a temperature sensor in this exercise, we’ll use a default value for that) The logic for voltage, temperature compensation, and voltage-to-tds come from the CQRobot wiki: <a href="http://www.cqrobot.wiki/index.php/TDS_(Total_Dissolved_Solids)_Meter_Sensor_SKU:_CQRSENTDS01#Arduino_Application" class="uri">http://www.cqrobot.wiki/index.php/TDS_(Total_Dissolved_Solids)_Meter_Sensor_SKU:_CQRSENTDS01#Arduino_Application</a></p>
<p>Also, the collection temperature must be in Celsius. Since we’re likely to be working with Fahrenheit, we’ll add a function for that conversion as well:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb8-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> fahrenheitToCelsius<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> temperature<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb8-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> celsiusValue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">((</span>temperature <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb8-3"></span>
<span id="cb8-4">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> celsiusValue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb8-5"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Finally, we’ll update our processing loop to use the new functions:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb9-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> loop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">static</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">unsigned</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> analogSampleTimepoint <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> millis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb9-3"></span>
<span id="cb9-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// take a reading and display it every 1/2 second:</span></span>
<span id="cb9-5">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>millis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> analogSampleTimepoint <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">U</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb9-6">    analogSampleTimepoint <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> millis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb9-7"></span>
<span id="cb9-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// get sensor pin value</span></span>
<span id="cb9-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// https://docs.arduino.cc/language-reference/en/functions/analog-io/analogRead/</span></span>
<span id="cb9-10">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> sensorValue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> analogRead<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>TdsSensorPin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb9-11"></span>
<span id="cb9-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Convert temperature to celsius, using a default temperature of 77 degrees</span></span>
<span id="cb9-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Fahrenheit:</span></span>
<span id="cb9-14">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> celsiusValue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fahrenheitToCelsius<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">77</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb9-15"></span>
<span id="cb9-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Calculate the actual TDS value:</span></span>
<span id="cb9-17">    tdsValue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> getTdsValue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>sensorValue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> celsiusValue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb9-18"></span>
<span id="cb9-19">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TDS -- PPM: "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb9-20">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>tdsValue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb9-21">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb9-22"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Now, our complete updated code looks like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb10-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#define TdsSensorPin </span>A1</span>
<span id="cb10-2"></span>
<span id="cb10-3"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> referenceVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb10-4">    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// analog reference voltage of the analog-to-digital convertor (ADC)</span></span>
<span id="cb10-5"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> tdsValue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-6"></span>
<span id="cb10-7"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> fahrenheitToCelsius<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> temperature<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> celsiusValue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">((</span>temperature <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-9"></span>
<span id="cb10-10">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> celsiusValue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-11"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb10-12"></span>
<span id="cb10-13"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> getTdsValue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> sensorValue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> temperatureAtCollection<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-14">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Convert the sensor pin reading to actual voltage:</span></span>
<span id="cb10-15">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> sensorVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>sensorValue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> referenceVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1024.0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-16"></span>
<span id="cb10-17">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// temperature compensation formula: fFinalResult(25^C) =</span></span>
<span id="cb10-18">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// fFinalResult(current)/(1.0+0.02*(fTP-25.0))</span></span>
<span id="cb10-19">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> compensationCoefficient <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>temperatureAtCollection <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb10-20"></span>
<span id="cb10-21">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// temperature compensation</span></span>
<span id="cb10-22">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> compensationVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sensorVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> compensationCoefficient<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-23"></span>
<span id="cb10-24">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// convert voltage value to tds value</span></span>
<span id="cb10-25">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> calculatedTdsValue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb10-26">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">133.42</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> compensationVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> compensationVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb10-27">           compensationVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span></span>
<span id="cb10-28">       <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">255.86</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> compensationVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> compensationVoltage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-29">       <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">857.39</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> compensationVoltage<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb10-30">      <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-31"></span>
<span id="cb10-32">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> calculatedTdsValue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-33"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb10-34"></span>
<span id="cb10-35"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> setup<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-36">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Initialize serial communication at a baud rate of  115200 bits per second:</span></span>
<span id="cb10-37">  Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>begin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">115200</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb10-38"></span>
<span id="cb10-39">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Set the mode of the pin to INPUT: This pin will be used to read data from</span></span>
<span id="cb10-40">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// the TDS sensor:</span></span>
<span id="cb10-41">  pinMode<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>TdsSensorPin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb10-42"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb10-43"></span>
<span id="cb10-44"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> loop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-45">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">static</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">unsigned</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> analogSampleTimepoint <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> millis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb10-46"></span>
<span id="cb10-47">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// take a reading and display it every 1/2 second:</span></span>
<span id="cb10-48">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>millis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> analogSampleTimepoint <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">U</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-49">    analogSampleTimepoint <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> millis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb10-50"></span>
<span id="cb10-51">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// get sensor pin value</span></span>
<span id="cb10-52">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// https://docs.arduino.cc/language-reference/en/functions/analog-io/analogRead/</span></span>
<span id="cb10-53">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> sensorValue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> analogRead<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>TdsSensorPin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb10-54"></span>
<span id="cb10-55">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Convert temperature to celsius, using a default temperature of 77 degrees</span></span>
<span id="cb10-56">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Fahrenheit:</span></span>
<span id="cb10-57">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">float</span> celsiusValue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fahrenheitToCelsius<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">77</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb10-58"></span>
<span id="cb10-59">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Calculate the actual TDS value:</span></span>
<span id="cb10-60">    tdsValue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> getTdsValue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>sensorValue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> celsiusValue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb10-61"></span>
<span id="cb10-62">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>print<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TDS -- PPM: "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb10-63">    Serial<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>println<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>tdsValue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb10-64">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb10-65"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>When we compile, upload, and run our serial monitor, we now get the PPM values we want:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make</span> upload</span>
<span id="cb11-2"></span>
<span id="cb11-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make</span> monitor</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb12-1">TDS -- PPM: 403.13</span>
<span id="cb12-2">TDS -- PPM: 405.03</span>
<span id="cb12-3">TDS -- PPM: 403.13</span>
<span id="cb12-4">TDS -- PPM: 403.13</span>
<span id="cb12-5">TDS -- PPM: 403.13</span>
<span id="cb12-6">TDS -- PPM: 405.03</span>
<span id="cb12-7">TDS -- PPM: 403.13</span></code></pre></div></div>


</section>

 ]]></description>
  <category>Embedded and IoT</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2024/tds_sensor_arduino.html</guid>
  <pubDate>Sun, 17 Nov 2024 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Observing Plan for the November Astronomy Program at Garber Forest</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2024/garber_observing_plan.html</link>
  <description><![CDATA[ 






<blockquote class="blockquote">
<p><strong>IMPORTANT UPDATE!</strong> Due to the forecast of clouds and showers on Saturday evening, I regret to inform you that I must cancel. So sorry! I’m hoping that I can re-schedule next Spring.</p>
</blockquote>
<p>On November 9th, 2024, I will be at the <a href="https://maps.app.goo.gl/Wqxny94ZTLmi6igm6">Garber Forest near Lewisburg Ohio</a>, equipped with my telescope to discuss astronomy and lead an exploration of the night sky - weather permitting, of course!</p>
<p><img src="https://jcarr.codeberg.page/blog/posts/2024/images/observing_plan/under_the_stars_1.jpg" class="img-fluid"></p>
<p>This event is organized by the <a href="https://preblecountyparks.org/">Preble County Park District</a>.</p>
<p><img src="https://jcarr.codeberg.page/blog/posts/2024/images/observing_plan/under_the_stars_2.jpg" class="img-fluid"></p>
<section id="what-well-look-for" class="level1">
<h1>What We’ll Look For</h1>
<section id="pm" class="level2">
<h2 class="anchored" data-anchor-id="pm">8pm</h2>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jcarr.codeberg.page/blog/posts/2024/images/observing_plan/view_1.jpg" class="img-fluid figure-img"></p>
<figcaption>Jupiter, Aldebaran, Pleiades, Capella</figcaption>
</figure>
</div>
<p>To the North-East-East, Jupiter has just risen and is about 8 degrees above the horizon, shining with a magnitude of -2.74 between the constellations of Auriga (“the charioteer”) and Taurus (“the bull”). It’s pretty low in the sky for observation, so we’ll come back to it later.</p>
<p>To the right of Jupiter is the bright star Aldebaran (the “eye” of Taurus) and directly above it (about 26 degrees above the horizon) is the Pleiades star cluster.</p>
<p>To the North-East, at about the same altitude as the Pleiades, is the bright star Capella, part of Auriga.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jcarr.codeberg.page/blog/posts/2024/images/observing_plan/view_2.jpg" class="img-fluid figure-img"></p>
<figcaption>Moon and Saturn</figcaption>
</figure>
</div>
<p>Looking to the South, a waxing Moon just over half full shines between the constellations Aquarius (“the water-carrier”) and Capricornus (“the horned goat”), at about 33 degrees above the horizon. Just up and to the left of the moon is Saturn (magnitude 0.85), in Aquarius. Looking closely, the rings of Saturn are almost edge-on, but hopefully we’ll be able to make them out.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jcarr.codeberg.page/blog/posts/2024/images/observing_plan/view_4_summer_triangle.jpg" class="img-fluid figure-img"></p>
<figcaption>The Summer Triangle</figcaption>
</figure>
</div>
<p>High in the sky to the west we see the three bright stars Altair, Deneb, and Vega. Although these stars are in separate constellations (Aquila, Lyra, and Cygnus, respectively) together they make up “The Summer Triangle”, a well-known asterism.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jcarr.codeberg.page/blog/posts/2024/images/observing_plan/view_5.jpg" class="img-fluid figure-img"></p>
<figcaption>Ursa Major, Ursa Minor, Polaris</figcaption>
</figure>
</div>
<p>Finally, to the North, Ursa Major (“the big dipper”) sits low on the horizon and above it is Ursa Minor (“the little dipper”) with Polaris (the steadfast “North Star”).</p>
</section>
<section id="pm-1" class="level2">
<h2 class="anchored" data-anchor-id="pm-1">9pm</h2>
<p>Jupiter will now be about 19 degrees above the horizon. Looking at it through the telescope, we should be able to make out its three brightest moons: Callisto, Europa, and Ganymede. As we get closer to 10pm, the moon Io may become visible for us as well.</p>
</section>
<section id="pm-2" class="level2">
<h2 class="anchored" data-anchor-id="pm-2">10pm</h2>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jcarr.codeberg.page/blog/posts/2024/images/observing_plan/view_6.jpg" class="img-fluid figure-img"></p>
<figcaption>Orion</figcaption>
</figure>
</div>
<p>The constellation Orion (“the hunter”) will have risen, but will still be low in the East. It’s easy to spot, with its bright stars Betelgeuse, Rigel, and Bellatrix, and the three distinct stars of Orion’s belt: Mintaka, Alnilam, and Alnitak. Will it be high enough for us to see? If so, we might also catch a glimpse of the Orion Nebula, the “sword” hanging from Orion’s belt.</p>
</section>
</section>
<section id="more-information" class="level1">
<h1>More Information</h1>
<section id="magnitude" class="level2">
<h2 class="anchored" data-anchor-id="magnitude">Magnitude</h2>
<p>In astronomy, “magnitude” refers to the brightness of celestial objects, such as stars, planets, and galaxies. There are two main types of magnitude:</p>
<ol type="1">
<li><p><strong>Apparent Magnitude</strong>: This measures how bright an object appears from Earth. The scale is logarithmic and inversely related, meaning that lower numbers indicate brighter objects. For example, a star with an apparent magnitude of 1 is brighter than a star with an apparent magnitude of 6. The scale also includes negative values for very bright objects, such as the Sun and the Moon.</p></li>
<li><p><strong>Absolute Magnitude</strong>: This measures the intrinsic brightness of a celestial object, defined as how bright the object would appear if it were located at a standard distance of 10 parsecs (about 32.6 light-years) from Earth. Absolute magnitude allows astronomers to compare the true brightness of different objects without the effects of distance.</p></li>
</ol>
<p>The concept of magnitude is crucial for understanding the luminosity and distance of celestial bodies, as well as for classifying stars and other astronomical phenomena.</p>
</section>
<section id="constellation" class="level2">
<h2 class="anchored" data-anchor-id="constellation">Constellation</h2>
<p>A constellation is a recognized pattern of stars in the night sky that forms a specific shape or figure, often representing mythological characters, animals, or objects. Constellations serve as a way to organize and identify groups of stars, making it easier for astronomers and stargazers to navigate the sky.</p>
<p>There are 88 officially recognized constellations, as defined by the International Astronomical Union (IAU). These constellations cover the entire celestial sphere and are used in various ways, including for navigation, storytelling, and as a framework for locating celestial objects. Some well-known constellations include Orion, Ursa Major, and Cassiopeia.</p>
</section>
<section id="asterism" class="level2">
<h2 class="anchored" data-anchor-id="asterism">Asterism</h2>
<p>In astronomy, “asterism” refers to a recognizable pattern or group of stars that may not necessarily form a formal constellation. Asterisms can be made up of stars from one or more constellations and are often more easily recognizable or familiar to the casual observer.</p>
<p>For example, the Big Dipper is an asterism that is part of the larger constellation Ursa Major. Similarly, the Summer Triangle is another well-known asterism formed by three bright stars from three different constellations: Vega (Lyra), Deneb (Cygnus), and Altair (Aquila).</p>
<p>Asterisms are useful for amateur astronomers and stargazers as they can serve as reference points for locating other stars and celestial objects in the night sky.</p>
</section>
<section id="altitude-and-azimuth" class="level2">
<h2 class="anchored" data-anchor-id="altitude-and-azimuth">Altitude and Azimuth</h2>
<p>Altitude and azimuth are two coordinates used in astronomy to specify the position of an object in the sky as observed from a specific location on Earth.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://jcarr.codeberg.page/blog/posts/2024/images/observing_plan/altitude-and-azimuth.jpg" class="img-fluid figure-img"></p>
<figcaption>Altitude and Azimuth</figcaption>
</figure>
</div>
<ol type="1">
<li><p><strong>Altitude</strong>: This is the angle between the object in the sky and the observer’s local horizon. It is measured in degrees, ranging from 0° (the horizon) to 90° (the zenith, which is directly overhead). An object with an altitude of 0° is on the horizon, while an object with an altitude of 90° is directly above the observer.</p></li>
<li><p><strong>Azimuth</strong>: This is the angle measured clockwise from a reference direction, typically true north. Azimuth is also measured in degrees, ranging from 0° to 360°. For example, an azimuth of 0° corresponds to north, 90° to east, 180° to south, and 270° to west.</p></li>
</ol>
<p>Together, altitude and azimuth provide a way to pinpoint the location of celestial objects in the sky relative to an observer’s position on Earth. This system is particularly useful for navigation, stargazing, and astronomical observations.</p>
</section>
<section id="the-moon" class="level2">
<h2 class="anchored" data-anchor-id="the-moon">The Moon</h2>
<p>The Moon is Earth’s only natural satellite and is the fifth largest moon in the solar system. It has a significant impact on Earth, influencing tides and stabilizing the planet’s axial tilt. The Moon’s surface is covered with craters, mountains, and flat plains called maria, which were formed by ancient volcanic activity.</p>
<p>The Moon has a synchronous rotation, meaning it rotates on its axis in the same time it takes to orbit Earth, which is why we always see the same side from our planet. The far side of the Moon, often mistakenly called the “dark side,” is not permanently dark; it just faces away from Earth.</p>
<p>The Moon has been a subject of human fascination for centuries, inspiring myths, art, and scientific exploration. The Apollo missions in the late 1960s and early 1970s marked the first time humans set foot on the Moon, with Apollo 11 being the most famous mission, landing Neil Armstrong and Buzz Aldrin on its surface in 1969.</p>
<section id="moon-phases" class="level3">
<h3 class="anchored" data-anchor-id="moon-phases">Moon Phases</h3>
<p>Waxing and waning refer to the phases of the moon as it orbits the Earth, specifically how the visible portion of the moon changes over time.</p>
<ol type="1">
<li><strong>Waxing</strong>: This term describes the period when the moon is increasing in illumination. After the new moon phase, the moon begins to wax, meaning more of its surface becomes visible from Earth. The phases during this period include:
<ul>
<li><strong>Waxing Crescent</strong>: A small sliver of the moon is illuminated.</li>
<li><strong>First Quarter</strong>: Half of the moon is illuminated.</li>
<li><strong>Waxing Gibbous</strong>: More than half of the moon is illuminated but not yet full.</li>
</ul></li>
<li><strong>Waning</strong>: This term describes the period when the moon is decreasing in illumination. After the full moon phase, the moon begins to wane, meaning less of its surface is visible. The phases during this period include:
<ul>
<li><strong>Waning Gibbous</strong>: More than half of the moon is illuminated but decreasing.</li>
<li><strong>Last Quarter</strong>: Half of the moon is illuminated, but the opposite side compared to the first quarter.</li>
<li><strong>Waning Crescent</strong>: A small sliver of the moon is illuminated, leading back to the new moon phase.</li>
</ul></li>
</ol>
<p>These cycles occur roughly every 29.5 days, which is the time it takes for the moon to complete one full orbit around the Earth and return to the same phase.</p>
</section>
</section>
<section id="planets" class="level2">
<h2 class="anchored" data-anchor-id="planets">Planets</h2>
<p>There are eight recognized planets in the solar system, which are divided into two categories:</p>
<ul>
<li><strong>Terrestrial planets</strong>: Mercury, Venus, Earth, and Mars. These are rocky and have solid surfaces.</li>
<li><strong>Gas giants</strong>: Jupiter and Saturn, which are primarily composed of hydrogen and helium.</li>
<li><strong>Ice giants</strong>: Uranus and Neptune, which have a larger proportion of “ices” such as water, ammonia, and methane.</li>
</ul>
<p><strong>Dwarf Planets</strong>: These include Pluto, Eris, Haumea, Makemake, and Ceres. Dwarf planets are similar to regular planets but do not clear their orbital paths of other debris.</p>
<section id="jupiter" class="level3">
<h3 class="anchored" data-anchor-id="jupiter">Jupiter</h3>
<p>Jupiter is the largest planet in our solar system, known for its massive size, strong magnetic field, and distinctive features. Here are some key points about Jupiter:</p>
<ol type="1">
<li><p><strong>Size and Composition</strong>: Jupiter is a gas giant, primarily composed of hydrogen and helium. It has a diameter of about 86,881 miles (139,822 kilometers), making it more than 11 times wider than Earth.</p></li>
<li><p><strong>Great Red Spot</strong>: One of Jupiter’s most famous features is the Great Red Spot, a giant storm that has been raging for at least 350 years. It is larger than Earth and is characterized by its reddish color.</p></li>
<li><p><strong>Moons</strong>: Jupiter has a large number of moons, with 79 confirmed as of now. The four largest moons, known as the Galilean moons, are Io, Europa, Ganymede, and Callisto. Ganymede is the largest moon in the solar system.</p></li>
<li><p><strong>Rings</strong>: Jupiter has a faint ring system composed mainly of dust particles and small rocks. The rings are not as prominent as those of Saturn.</p></li>
<li><p><strong>Magnetic Field</strong>: Jupiter has the strongest magnetic field of any planet in the solar system, which is generated by the motion of metallic hydrogen in its interior.</p></li>
<li><p><strong>Exploration</strong>: Several spacecraft have visited Jupiter, including the Galileo orbiter and the Juno spacecraft, which is currently studying the planet’s atmosphere, magnetic field, and structure.</p></li>
<li><p><strong>Atmosphere</strong>: Jupiter’s atmosphere is known for its colorful bands and storms, including the aforementioned Great Red Spot. The planet’s atmosphere is dynamic and experiences high-speed winds.</p></li>
</ol>
</section>
<section id="jupiters-moons" class="level3">
<h3 class="anchored" data-anchor-id="jupiters-moons">Jupiter’s Moons</h3>
<section id="callisto" class="level4">
<h4 class="anchored" data-anchor-id="callisto">Callisto</h4>
<p>Callisto is one of the largest moons of Jupiter and is the third-largest moon in the solar system. It has a diameter of about 4,820 kilometers (2,995 miles) and is known for its heavily cratered surface, which suggests it has been geologically inactive for a long time. Callisto is composed primarily of ice and rock, and its surface features include a mix of old impact craters and some younger, less cratered regions.</p>
<p>One of the most interesting aspects of Callisto is its potential subsurface ocean. While there is no direct evidence of this ocean, some scientists believe that beneath its icy crust, there may be a salty ocean that could harbor conditions suitable for life.</p>
<p>Callisto has a very thin atmosphere, primarily composed of carbon dioxide, and it has a low level of radiation compared to other Galilean moons, making it a more favorable location for future exploration and potential human missions. It was discovered by Galileo Galilei in 1610, along with the other three largest moons of Jupiter: Io, Europa, and Ganymede.</p>
</section>
<section id="europa" class="level4">
<h4 class="anchored" data-anchor-id="europa">Europa</h4>
<p>Europa is one of Jupiter’s largest moons and is the sixth-largest moon in the solar system. It is particularly interesting to scientists because it is believed to have a subsurface ocean beneath its icy crust, which could potentially harbor conditions suitable for life. Here are some key features of Europa:</p>
<ol type="1">
<li><p><strong>Surface and Composition</strong>: Europa’s surface is primarily composed of water ice, and it features a smooth, bright appearance with few impact craters, suggesting a relatively young surface. The cracks and ridges on its surface indicate tectonic activity.</p></li>
<li><p><strong>Subsurface Ocean</strong>: Scientists believe that beneath Europa’s icy shell lies a salty ocean that may be in contact with the moon’s rocky mantle. This ocean could provide the necessary chemical ingredients for life.</p></li>
<li><p><strong>Potential for Life</strong>: The combination of liquid water, a stable energy source (from tidal heating due to Jupiter’s gravitational pull), and essential chemical elements makes Europa a prime candidate in the search for extraterrestrial life.</p></li>
<li><p><strong>Exploration</strong>: Europa has been studied by several spacecraft, including the Galileo orbiter and the Hubble Space Telescope.</p></li>
<li><p><strong>Atmosphere</strong>: Europa has a very thin atmosphere composed mainly of oxygen, but it is far too thin to support human life.</p></li>
</ol>
<p>Europa continues to be a focus of astrobiological research and exploration due to its intriguing characteristics and the possibility of finding life beyond Earth.</p>
<blockquote class="blockquote">
<p><em>Europa Clipper</em>, a space probe developed by NASA, is currently on its way to Europa and is expected to arrive in the Jupiter system in 2030.</p>
<p>The goals of Europa Clipper are to explore Europa, investigate its habitability, and aid in the selection of a landing site for the proposed Europa Lander. This exploration is focused on understanding the three main requirements for life: liquid water, chemistry, and energy. Specifically, the objectives are to study:</p>
<ul>
<li>Ice shell and ocean: Confirm the existence and characterize the nature of water within or beneath the ice, and study processes of surface-ice-ocean exchange.</li>
<li>Composition: Distribution and chemistry of key compounds and the links to ocean composition.</li>
<li>Geology: Characteristics and formation of surface features, including sites of recent or current activity.</li>
</ul>
<p><a href="https://en.wikipedia.org/wiki/Europa_Clipper#Objectives">source</a></p>
</blockquote>
</section>
<section id="ganymede" class="level4">
<h4 class="anchored" data-anchor-id="ganymede">Ganymede</h4>
<p>Ganymede is the largest moon of Jupiter and the largest moon in the solar system. It is one of the four Galilean moons, which were discovered by Galileo Galilei in 1610. Ganymede has a diameter of about 5,268 kilometers (3,273 miles), making it even larger than the planet Mercury, although it does not have enough mass to be classified as a planet.</p>
<p>Ganymede is unique among moons because it has a magnetic field, which is likely generated by a liquid iron or iron-sulfide core. The surface of Ganymede is a mix of two types of terrain: bright, icy regions and darker, heavily cratered areas. It is believed to have a subsurface ocean beneath its icy crust, which raises the possibility of conditions that could support life.</p>
<p>In addition to its geological features, Ganymede has a thin atmosphere composed mostly of oxygen, although it is far too thin to support human life. The moon has been studied by various spacecraft, including the Galileo orbiter and the Hubble Space Telescope.</p>
</section>
<section id="io" class="level4">
<h4 class="anchored" data-anchor-id="io">Io</h4>
<p>Io is one of the four largest moons of Jupiter. It is the most geologically active body in the solar system, with hundreds of volcanoes, some of which are still erupting. Io’s surface is covered with sulfur and sulfur dioxide, giving it a colorful appearance with shades of yellow, red, and white.</p>
<p>The intense geological activity on Io is primarily due to the gravitational pull from Jupiter and the other Galilean moons (Europa, Ganymede, and Callisto), which creates tidal heating. This process generates enough internal heat to drive volcanic activity.</p>
<p>Io has a thin atmosphere composed mainly of sulfur dioxide, and its surface temperature can vary widely. The moon also has a very weak magnetic field and is known to have a significant interaction with Jupiter’s magnetosphere.</p>
</section>
</section>
<section id="saturn" class="level3">
<h3 class="anchored" data-anchor-id="saturn">Saturn</h3>
<p>Saturn is the sixth planet from the Sun and is known for its prominent ring system, which is the most extensive and complex in the solar system. It is a gas giant, primarily composed of hydrogen and helium, and has a diameter of about 86,881 miles (139,822 kilometers). Saturn has at least 82 moons, with Titan being the largest and one of the most interesting due to its thick atmosphere and surface lakes of liquid methane and ethane.</p>
<p>Saturn’s rings are made up of ice particles, rocky debris, and dust, and they vary in thickness and density. The planet has a relatively low density, meaning it could float in water if there were a body of water large enough to contain it. Saturn is also known for its strong winds, which can reach speeds of over 1,100 miles per hour (1,800 kilometers per hour) in its upper atmosphere.</p>
</section>
</section>
<section id="bright-stars" class="level2">
<h2 class="anchored" data-anchor-id="bright-stars">Bright Stars</h2>
<section id="aldebaran" class="level3">
<h3 class="anchored" data-anchor-id="aldebaran">Aldebaran</h3>
<p>Aldebaran is a prominent star located in the constellation Taurus. It is often referred to as the “Eye of the Bull” and is one of the brightest stars in the night sky. Aldebaran is classified as a red giant star and is approximately 65 light-years away from Earth. It has a distinct orange hue and is part of the Hyades star cluster, although it is not physically part of the cluster itself. Aldebaran is notable for its brightness and is often used in navigation and astronomy.</p>
</section>
<section id="altair" class="level3">
<h3 class="anchored" data-anchor-id="altair">Altair</h3>
<p>Altair is a prominent star located in the constellation Aquila, which represents an eagle. Here are some key facts about Altair:</p>
<ol type="1">
<li><p><strong>Brightness</strong>: Altair is one of the brightest stars in the night sky and is the 12th brightest star overall. It has an apparent magnitude of about 0.76.</p></li>
<li><p><strong>Distance</strong>: Altair is approximately 16.7 light-years away from Earth, making it one of the closest stars to our solar system.</p></li>
<li><p><strong>Spectral Type</strong>: Altair is classified as an A7V main-sequence star, which means it is a hot, white star that is in the stable phase of its life cycle, fusing hydrogen into helium in its core.</p></li>
<li><p><strong>Rotation</strong>: Altair is known for its rapid rotation, completing a full rotation approximately every 9 hours. This fast rotation causes it to have an oblate shape, meaning it is flattened at the poles and bulging at the equator.</p></li>
<li><p><strong>Cultural Significance</strong>: In various cultures, Altair has been associated with different myths and stories. In Chinese astronomy, it is known as “Zhi Nu,” the Weaving Girl, and is part of the famous “Cowherd and Weaving Girl” legend.</p></li>
<li><p><strong>Part of the Summer Triangle</strong>: Altair is one of the three stars that form the Summer Triangle, along with Vega (in Lyra) and Deneb (in Cygnus). This asterism is prominent in the summer sky in the Northern Hemisphere.</p></li>
</ol>
</section>
<section id="bellatrix" class="level3">
<h3 class="anchored" data-anchor-id="bellatrix">Bellatrix</h3>
<p>Bellatrix, also known as Gamma Orionis, is a prominent star located in the constellation Orion. Here are some key details about Bellatrix:</p>
<ol type="1">
<li><p><strong>Brightness</strong>: Bellatrix is one of the brightest stars in the night sky and is classified as a blue giant. It has an apparent magnitude of about 1.64, making it the third-brightest star in Orion, after Betelgeuse and Rigel.</p></li>
<li><p><strong>Distance</strong>: Bellatrix is approximately 240 light-years away from Earth.</p></li>
<li><p><strong>Spectral Type</strong>: It is classified as a B2 III star, indicating that it is a blue giant with a surface temperature of around 22,000 Kelvin.</p></li>
<li><p><strong>Characteristics</strong>: Bellatrix is known for its rapid rotation and is significantly more massive than the Sun. It is also in a later stage of stellar evolution compared to many other stars, having exhausted the hydrogen in its core and expanded in size.</p></li>
<li><p><strong>Name Origin</strong>: The name “Bellatrix” comes from the Latin word for “female warrior,” which is fitting given its position in the constellation representing a hunter.</p></li>
</ol>
<p>Bellatrix is often used in astronomy and stargazing due to its brightness and position in the well-known Orion constellation.</p>
</section>
<section id="betelgeuse" class="level3">
<h3 class="anchored" data-anchor-id="betelgeuse">Betelgeuse</h3>
<p>Betelgeuse is a red supergiant star located in the constellation Orion. It is one of the largest and most luminous stars known, with a diameter estimated to be about 1,000 times that of the Sun. Betelgeuse is notable for its distinct reddish color and is often used as a reference point in the night sky.</p>
<p>As a supergiant, Betelgeuse is in the later stages of its stellar evolution and is expected to eventually explode as a supernova. This event could happen within the next million years, although it’s difficult to predict exactly when.</p>
<p>In addition to its astronomical significance, Betelgeuse has captured the imagination of many cultures and is often featured in literature and popular media.</p>
<p><a href="../../posts/2020/betelgeuse.html">Betelgeuse was in the news recently</a>.</p>
</section>
<section id="capella" class="level3">
<h3 class="anchored" data-anchor-id="capella">Capella</h3>
<p>Capella is one of the brightest stars in the night sky and is located in the constellation Auriga. Here are some key facts about Capella:</p>
<ol type="1">
<li><p><strong>Brightness</strong>: Capella is the sixth-brightest star in the night sky and is classified as a bright giant star.</p></li>
<li><p><strong>Distance</strong>: It is approximately 42 light-years away from Earth.</p></li>
<li><p><strong>Binary System</strong>: Capella is actually a binary star system, consisting of two giant stars, Capella Aa and Capella Ab, which are both yellowish in color. They are orbiting each other and are surrounded by a faint third component, Capella C, which is a much fainter red dwarf star.</p></li>
<li><p><strong>Spectral Type</strong>: The primary stars (Capella Aa and Ab) are classified as G-type stars, similar to our Sun, but they are larger and more luminous.</p></li>
<li><p><strong>Cultural Significance</strong>: Capella has been known since ancient times and has been referenced in various cultures and mythologies. In Latin, “Capella” means “little goat,” which is a reference to its association with the goat in the constellation.</p></li>
</ol>
<p>Capella is often visible in the evening sky during the winter months in the Northern Hemisphere.</p>
</section>
<section id="deneb" class="level3">
<h3 class="anchored" data-anchor-id="deneb">Deneb</h3>
<p>Deneb is one of the brightest stars in the night sky and is part of the constellation Cygnus, also known as the Swan. It is located approximately 1,425 light-years away from Earth and is one of the vertices of the Summer Triangle, along with the stars Vega and Altair. Deneb is a supergiant star, classified as a spectral type A2 Ia, and is known for its significant luminosity and size. It is also notable for its role in various cultural and astronomical contexts, often associated with navigation and mythology.</p>
</section>
<section id="polaris" class="level3">
<h3 class="anchored" data-anchor-id="polaris">Polaris</h3>
<p>“Polaris,” also known as the North Star, is the brightest star in the constellation Ursa Minor. It is located nearly directly above the North Pole, making it a crucial point of reference for navigation in the Northern Hemisphere. Polaris is a supergiant star and is approximately 433 light-years away from Earth.</p>
</section>
<section id="rigel" class="level3">
<h3 class="anchored" data-anchor-id="rigel">Rigel</h3>
<p>Rigel is a prominent star located in the constellation Orion. It is one of the brightest stars in the night sky and is classified as a blue supergiant. Rigel is approximately 860 light-years away from Earth and has a surface temperature of around 12,000 Kelvin, which gives it its blue color. It is often used in navigation and is notable for its brightness and distinctive position in the Orion constellation.</p>
</section>
<section id="vega" class="level3">
<h3 class="anchored" data-anchor-id="vega">Vega</h3>
<p>Vega is one of the brightest stars in the night sky and is located in the constellation Lyra. Here are some key facts about Vega:</p>
<ol type="1">
<li><p><strong>Brightness</strong>: Vega is the fifth-brightest star in the night sky and is one of the three stars that form the Summer Triangle, along with Deneb and Altair.</p></li>
<li><p><strong>Distance</strong>: It is approximately 25 light-years away from Earth.</p></li>
<li><p><strong>Type</strong>: Vega is classified as an A-type main-sequence star (spectral type A0V). It is about twice as massive as the Sun and has a surface temperature of around 9,600 K, which gives it a bluish-white color.</p></li>
<li><p><strong>Rotation</strong>: Vega has a rapid rotation speed, completing a rotation in about 12.5 hours, which is relatively fast compared to the Sun.</p></li>
<li><p><strong>Historical Significance</strong>: Vega has been used as a reference point for the calibration of the brightness of other stars. It was also the first star other than the Sun to be photographed and has been studied extensively in various fields of astronomy.</p></li>
<li><p><strong>Cultural References</strong>: Vega has been significant in various cultures and mythologies. In Chinese mythology, it is associated with the weaver girl, and in Western culture, it has been referred to as the “Harp Star.”</p></li>
</ol>
</section>
</section>
<section id="deep-sky-objects" class="level2">
<h2 class="anchored" data-anchor-id="deep-sky-objects">Deep Sky Objects</h2>
<section id="orion-nebula" class="level3">
<h3 class="anchored" data-anchor-id="orion-nebula">Orion Nebula</h3>
<p>The Orion Nebula, also known as M42, is one of the most famous and studied nebulae in the night sky. It is located in the Milky Way, approximately 1,344 light-years away from Earth in the Orion constellation. The nebula is a stellar nursery, where new stars are being born from the surrounding gas and dust.</p>
<p>Key features of the Orion Nebula include:</p>
<ol type="1">
<li><p><strong>Brightness</strong>: It is one of the brightest nebulae visible to the naked eye and can be seen as a fuzzy patch in the sword of Orion.</p></li>
<li><p><strong>Size</strong>: The Orion Nebula spans about 24 light-years across and contains a vast amount of gas and dust.</p></li>
<li><p><strong>Star Formation</strong>: The nebula is home to a young cluster of stars known as the Trapezium, which consists of four massive stars that illuminate the surrounding gas and dust.</p></li>
<li><p><strong>Observation</strong>: The Orion Nebula is a popular target for amateur astronomers and astrophotographers due to its brightness and the intricate structures within it.</p></li>
<li><p><strong>Scientific Importance</strong>: It provides valuable insights into the processes of star formation and the dynamics of interstellar matter.</p></li>
</ol>
<p>The Orion Nebula is a stunning example of the beauty and complexity of the universe, making it a favorite subject for both professional and amateur astronomers.</p>
</section>
<section id="the-pleiades" class="level3">
<h3 class="anchored" data-anchor-id="the-pleiades">The Pleiades</h3>
<p>The Pleiades, also known as the Seven Sisters, is a star cluster located in the constellation Taurus. It is one of the nearest star clusters to Earth and is easily visible to the naked eye. The cluster contains several hundred stars, but the seven brightest are typically the ones referred to as the “Seven Sisters.” These stars are named Alcyone, Asterope, Electra, Maia, Merope, Taygeta, and Pleione.</p>
<p>The Pleiades has been significant in various cultures throughout history, often associated with mythology and folklore. In astronomy, it is known for its beauty and is often used as a reference point for navigation. The cluster is also of interest to astronomers studying star formation and the evolution of stars.</p>


</section>
</section>
</section>

 ]]></description>
  <category>Astronomy</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2024/garber_observing_plan.html</guid>
  <pubDate>Fri, 18 Oct 2024 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Detecting the Voyager Spacecraft in Green Bank Telescope Data</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2024/voyager_python.html</link>
  <description><![CDATA[ 






<section id="breakthrough-listen" class="level1">
<h1>Breakthrough Listen</h1>
<p><a href="https://seti.berkeley.edu/listen/">Breakthrough Listen</a> is a project to search for intelligent extraterrestial communications from deep space. The project uses radio wave observations from the <a href="https://en.wikipedia.org/wiki/Green_Bank_Observatory">Green Bank Observatory</a> and the <a href="https://en.wikipedia.org/wiki/Parkes_Observatory">Parkes Observatory</a>, and visible light observations from the <a href="https://en.wikipedia.org/wiki/Automated_Planet_Finder">Automated Planet Finder</a>. In this article, I’ll be focusing on radio data collected from the Green Bank Observatory.</p>
<section id="observing-strategy" class="level2">
<h2 class="anchored" data-anchor-id="observing-strategy">Observing Strategy</h2>
<p>The basic observing strategy for GBT data is:</p>
<ul>
<li>Observe a target (“star A”) from the primary target list for 5 minutes.</li>
<li>Observe another target (“star B”) at a nearby position on the sky for 5 minutes.</li>
<li>Return to star A for 5 minutes.</li>
<li>Observe another target (“star C”) from the secondary list for 5 minutes.</li>
<li>Another 5 minutes on star A.</li>
<li>5 minutes on another secondary target (“star D”)</li>
</ul>
<p>This ABACAD cadence is repeated for all of the “A” stars in the primary target list (around 1700 in total, although not all of these are visible from the Green Bank Telescope site).</p>
<p>A signal would be interesting from a SETI standpoint if it’s present in all three “A” (or ON primary source) observations, but absent in the “B/C/D” (or OFF primary source) observations. If someone was standing near the telescope with a cellphone switched on, we wouldn’t expect this interference to come and go as we move on and off the primary target on the sky, so this is a good way of ruling out the local interference that makes up the majority of RFI.</p>
<p><a href="https://github.com/UCBerkeleySETI/breakthrough/tree/master/GBT#1-the-source-is-localized-on-the-sky">source</a></p>
</section>
<section id="challenges" class="level2">
<h2 class="anchored" data-anchor-id="challenges">Challenges</h2>
<p>There are multiple challenges to contend with in the search for these signals, including:</p>
<ul>
<li>Human technology gives off signals like the ones being searched for (radio frequency interference)</li>
<li>Data volumes are too large to run brute force analysis on entire datasets.</li>
<li>The sky is big: There are many targets to evaluate.</li>
</ul>
<p>But, there’s another challenge:</p>
<blockquote class="blockquote">
<p>How do we calibrate instruments, and test our detection methods?</p>
<p>What we really need is a <em>known</em> faint signal from space. Luckily, we have one.</p>
</blockquote>
</section>
</section>
<section id="voyager-1" class="level1">
<h1>Voyager 1</h1>
<p>Voyager 1 is a space probe launched by NASA on September 5, 1977, as part of the Voyager program, aimed at exploring the outer Solar System and the interstellar space beyond the Sun’s heliosphere. It was launched 16 days after its twin, Voyager 2. The probe communicates with Earth via the NASA Deep Space Network (DSN) to receive commands and send data back. NASA and JPL provide real-time information on its distance and velocity. As of August 2024, Voyager 1 is located 164.0 AU (24.5 billion km; 15.2 billion mi) from Earth, making it the most distant human-made object in existence. During its mission, Voyager 1 conducted flybys of Jupiter, Saturn, and Saturn’s largest moon, Titan. NASA opted for a flyby of Titan over Pluto due to Titan’s significant atmosphere. The probe gathered valuable data on the weather, magnetic fields, and rings of the two gas giants and was the first to capture detailed images of their moons.</p>
<p>After 38 years, Voyager 1 continues to transmit telemetry data from the far reaches of interstellar space. This provides an excellent opportunity to test the Breakthrough Listen signal processing pipeline.</p>
</section>
<section id="gbt-data" class="level1">
<h1>GBT Data</h1>
<p>On December 30, 2015, the Greenbank X-band receiver (8.0-11.6 GHz) was utilized to observe the known location of Voyager 1. The BL digital signal processing system captures digitized data in a ‘raw’ format, which is then converted into ‘filterbank’ format - a straightforward binary file format described in the <a href="http://sigproc.sourceforge.net/sigproc.pdf">SIGPROC user guide</a>.</p>
<p>In this tutorial, we’ll walk through the setup and creation of a Jupyter notebook to load and interpret the filterbank file.</p>
<p>A more detailed version of this tutorial, featuring extra scientific information, can be found <a href="https://github.com/UCBerkeleySETI/breakthrough/blob/master/GBT/voyager/voyager.ipynb">here</a>. My aim in recreating this tutorial is to provide additional insights into the environment setup.</p>
</section>
<section id="environment" class="level1">
<h1>Environment</h1>
<p>This assumes a Linux environment, but it shouldn’t be difficult to adapt to others, if needed.</p>
<section id="visual-studio-code-and-pythonjupyter" class="level2">
<h2 class="anchored" data-anchor-id="visual-studio-code-and-pythonjupyter">Visual Studio Code and Python/Jupyter</h2>
<p>We’ll be using Jupyter notebooks, so we need an environment to run them in. I use Visual Studio Code with the Python extension, as it provides a very easy setup:</p>
<ol type="1">
<li>Install Visual Studio Code.</li>
<li>Open Visual Studio Code and install the Python extension.</li>
</ol>
<p>The Python extension includes comprehensive support for Jupyter notebooks.</p>
</section>
<section id="python-virtual-environment" class="level2">
<h2 class="anchored" data-anchor-id="python-virtual-environment">Python Virtual Environment</h2>
<p>We have very specific scientific library requirements, so we’ll isolate our setup in a virtual environment.</p>
<p>Open a terminal.</p>
<p>Create a virtual environment:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">python3</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-m</span> venv voyager_venv</span></code></pre></div></div>
<p>Switch to the environment directory and activate it:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> voyager_venv/</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">source</span> bin/activate</span></code></pre></div></div>
<p>Install Python dependencies for a scientific environment:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install numpy</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install scipy</span>
<span id="cb3-4"></span>
<span id="cb3-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install matplotlib</span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install astropy</span></code></pre></div></div>
<p>You’ll also need <a href="https://pypi.org/project/blimpy/">Blimpy</a>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install blimpy</span></code></pre></div></div>
</section>
<section id="source-prep" class="level2">
<h2 class="anchored" data-anchor-id="source-prep">Source Prep</h2>
<p>Create a src/ directory to hold our notebook and associated data:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> src/</span>
<span id="cb5-2"></span>
<span id="cb5-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> src/</span></code></pre></div></div>
<p>Download the filterbank file that includes the Voyager data:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wget</span> https://storage.googleapis.com/gbt_fil/voyager_f1032192_t300_v2.fil</span></code></pre></div></div>
<p>Open the directory in Visual Studio Code:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">code</span> .</span></code></pre></div></div>
</section>
</section>
<section id="create-the-notebook" class="level1">
<h1>Create the Notebook</h1>
<p>Create a new file named <code>voyager.ipynb</code>. VS Code will initialize it as a Jupyter notebook.</p>
<p>On the right side of the notebook interface you’ll see link named “Select Kernel”. Click it, select “Python Environments”, then select the bin/python inside your virtual environment. You’ll end up with something that looks like this:</p>
<p><img src="https://jcarr.codeberg.page/blog/posts/2024/images/voyager/python_environment.png" class="img-fluid"></p>
<p>Let’s make sure our Python environment is working correctly. Inside the empty cell at the top of notebook, type this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OK!"</span>)</span></code></pre></div></div>
<p>Then, click “Run All”. Since this is your first time executing Python code in a notebook in the environment, you may be prompted to install the ipykernel package. If so, go ahead and click “Install”.</p>
<p>After the kernel installs, the notebook will execute and you’ll see this:</p>
<p><img src="https://jcarr.codeberg.page/blog/posts/2024/images/voyager/test_ok.png" class="img-fluid"></p>
</section>
<section id="extract-the-data" class="level1">
<h1>Extract the Data</h1>
<p>Now that we’ve confirmed that our Python/Jupyter setup is working correctly, we can start the process of working with the Voyager data.</p>
<p>We want our plots to display inline in the notebook, so change the first cell to this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>matplotlib inline</span></code></pre></div></div>
<p>Add a new code block with the following imports:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pylab <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb10-2"></span>
<span id="cb10-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> blimpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Waterfall</span></code></pre></div></div>
<ul>
<li><code>pylab</code> is a convenience module that combines matplotlib’s plotting functions with numpy’s numerical functions.</li>
<li><code>blimpy</code> is a Python package used for working with radio astronomy data, particularly for handling and analyzing data from radio telescopes. The <code>Waterfall</code> class is typically used to represent and manipulate waterfall plots, which are a common way to visualize time-frequency data.</li>
</ul>
<p>Add another cell and initialize the plot size:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1">plt.figure(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span></code></pre></div></div>
<p>Add another cell with the following:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1">obs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Waterfall(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'voyager_f1032192_t300_v2.fil'</span>)</span></code></pre></div></div>
<p>This loads the Voyager observation data from the filterbank file.</p>
<p>Add another cell and get some information from the filterbank header:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1">obs.info()</span></code></pre></div></div>
<p>The output will look something like this:</p>
<p><img src="https://jcarr.codeberg.page/blog/posts/2024/images/voyager/obs_info.png" class="img-fluid"></p>
<blockquote class="blockquote">
<p>The specifics are discussed in detail in the <a href="http://sigproc.sourceforge.net/sigproc.pdf">SIGPROC user guide</a>. Briefly, astronomers use a Celestial coordinate system to specify the location of objects in outer space. The <code>src_raj</code> and <code>src_dej</code> specify the <a href="https://en.wikipedia.org/wiki/Epoch_%28astronomy%29#Julian_years_and_J2000">J2000</a> coordinates, in terms of Right Ascension and Declination (RA &amp; DEC), toward which the telescope is pointing. <code>tstart</code> specifies the <a href="https://en.wikipedia.org/wiki/Julian_day">Julian Date</a> of the observation, and <code>fch1</code> and <code>foff</code> specify the frequency of the first and frequency increment of each data channel respectively, in MHz.</p>
<p><a href="https://github.com/UCBerkeleySETI/breakthrough/blob/master/GBT/voyager/voyager.ipynb">source</a></p>
</blockquote>
<p>Add another cell and plot the observation data’s spectrum:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1">obs.plot_spectrum()</span></code></pre></div></div>
<p><img src="https://jcarr.codeberg.page/blog/posts/2024/images/voyager/spectrum_wide.png" class="img-fluid"></p>
<p>What you’re seeing is the power spectral density of the data contained in the filterbank file. There are discontinuities due to the BL digital filters, but not a lot else. Voyager is a very narrowband signal, so we can’t easily see it in this plot. In order to do that, we need to zoom in. We use <code>f_start</code> and <code>f_stop</code> arguments to specify the beginning and ending frequency of the slice we want to view:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1">obs.plot_spectrum(f_start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8420.18</span>, f_stop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8420.25</span>)</span></code></pre></div></div>
<p><img src="https://jcarr.codeberg.page/blog/posts/2024/images/voyager/spectrum_narrow.png" class="img-fluid"></p>
<p>There it is: the telemetry signal from Voyager 1! What you see is the carrier (center) and two sidebands that carry the data.</p>
</section>
<section id="what-have-we-accomplished" class="level1">
<h1>What Have We Accomplished?</h1>
<p>In 2015, Green Bank pointed their radio telescope in the direction of a tiny spacecraft, billions of miles away, emitting a very faint signal. They captured a broad swath of radio frequency spectrum into a raw data file.</p>
<p>That file was converted into <code>filterbank</code>, a more accessible format. We then used simple software tools to reach into that data and pull out a very thin slice of it, bringing the Voyager signal into view.</p>
</section>
<section id="links" class="level1">
<h1>Links</h1>
<p>You can learn more by visiting these sites:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><a href="https://seti.berkeley.edu/listen/">Breakthrough Listen</a></td>
<td>The official website of the Breakthrough Listen project.</td>
</tr>
<tr class="even">
<td><a href="https://github.com/UCBerkeleySETI/breakthrough">UC Berkeley Breakthrough Listen software and tutorials</a></td>
<td>This is the GitHub repository containing Breakthrough data and code samples. You can go directly to the Green Bank Telescope section <a href="https://github.com/UCBerkeleySETI/breakthrough/tree/master/GBT">here</a>. It includes a more detailed notebook for <a href="https://github.com/UCBerkeleySETI/breakthrough/blob/master/GBT/voyager/voyager.ipynb">Voyager</a>.</td>
</tr>
<tr class="odd">
<td><a href="https://greenbankobservatory.org/">Green Bank Observatory</a></td>
<td>Home of the Robert C. Byrd Green Bank Telescope.</td>
</tr>
</tbody>
</table>
<section id="software-libraries-and-tools" class="level2">
<h2 class="anchored" data-anchor-id="software-libraries-and-tools">Software Libraries and Tools</h2>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><a href="https://numpy.org/">NumPy</a></td>
<td>A Python library that adds support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.</td>
</tr>
<tr class="even">
<td><a href="https://scipy.org/">SciPy</a></td>
<td>A Python library that provides fundamental algorithms for optimization, integration, interpolation, statistics and more. It extends NumPy with specialized data structures and wraps fast implementations in low-level languages.</td>
</tr>
<tr class="odd">
<td><a href="https://matplotlib.org/">Matplotlib</a></td>
<td>Plotting library for the Python programming language and its numerical mathematics extension NumPy.</td>
</tr>
<tr class="even">
<td><a href="https://www.astropy.org/">AstroPy</a></td>
<td>A collection of software packages written in the Python programming language and designed for use in astronomy.</td>
</tr>
<tr class="odd">
<td><a href="https://github.com/UCBerkeleySETI/blimpy">Blimpy</a></td>
<td>Python 2/3 readers for interacting with Sigproc filterbank (.fil), HDF5 (.h5) and guppi raw (.raw) files, as used in the Breakthrough Listen search for intelligent life.</td>
</tr>
</tbody>
</table>


</section>
</section>

 ]]></description>
  <category>Python</category>
  <category>Astronomy</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2024/voyager_python.html</guid>
  <pubDate>Sun, 08 Sep 2024 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Modern Perl</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2024/modern_perl.html</link>
  <description><![CDATA[ 






<p>Last year, I wrote an article detailing <a href="../../posts/2023/perl_bad.html">my experience re-creating a Python script in Perl</a>. This was a pretty cumbersome exercise, but I didn’t use many of Perl’s newer features. I decided to re-visit this, leveraging some of these newer features to see how well it improves the process.</p>
<section id="enabling-features" class="level2">
<h2 class="anchored" data-anchor-id="enabling-features">Enabling Features</h2>
<p>To take advantage of specific features, use the aptly named <code>feature</code> pragma.</p>
<p>You can enable an individual feature by name, and multiple features at once with <code>qw()</code>. For example, this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode perl code-with-copy"><code class="sourceCode perl"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> feature <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">fc</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> feature <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">say</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>;</span></code></pre></div></div>
<p>Is the same as this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode perl code-with-copy"><code class="sourceCode perl"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> feature <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">qw(</span>fc say<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">)</span>;</span></code></pre></div></div>
<p>Full example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode perl code-with-copy"><code class="sourceCode perl"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> feature <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">qw(</span>fc say<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">)</span>;</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$x</span> = <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">Test</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb3-4"></span>
<span id="cb3-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">say</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">The case-folded version of </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$x</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> is: </span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span> . fc <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$x</span>;</span></code></pre></div></div>
<p>Output:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb4-1">The case-folded version of Test is: test</span></code></pre></div></div>
<p>Without <code>use feature</code>, you’d get this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb5-1">String found where operator expected (Do you need to predeclare "say"?) at ./test.pl line 7, near "say "The case-folded version of $x is: ""</span>
<span id="cb5-2">syntax error at ./test.pl line 7, near "say "The case-folded version of $x is: ""</span>
<span id="cb5-3">Execution of ./test.pl aborted due to compilation errors.</span></code></pre></div></div>
<p>You can also take advantage of <strong>feature bundles</strong>, enabling all of the features available as of a given version:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode perl code-with-copy"><code class="sourceCode perl"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># implicitly loads 5.36 feature bundle</span></span>
<span id="cb6-2"></span>
<span id="cb6-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> v5.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">36</span>;</span></code></pre></div></div>
<p>But, you must also have (at least) that version of Perl installed, of course:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode perl code-with-copy"><code class="sourceCode perl"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> v5.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>;</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb8-1">Perl v5.40.0 required--this is only v5.38.2, stopped at ./test.pl line 3.</span>
<span id="cb8-2">BEGIN failed--compilation aborted at ./test.pl line 3.</span></code></pre></div></div>
</section>
<section id="useful-features" class="level2">
<h2 class="anchored" data-anchor-id="useful-features">Useful Features</h2>
<p><a href="https://en.wikipedia.org/wiki/Perl_5_version_history">Lots of new features have been added in recent years</a>, but I’ll take advantage of only a small subset of them for this exercise. I’ll be using features available in 5.38.2 and earlier.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 32%">
<col style="width: 35%">
<col style="width: 32%">
</colgroup>
<thead>
<tr class="header">
<th>Feature</th>
<th>Version</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>New <code>class</code> feature</td>
<td>5.38.0</td>
<td>Adds support for object-oriented code.</td>
</tr>
<tr class="even">
<td>New <code>say</code> built-in</td>
<td>5.10.0</td>
<td>Works like <code>print</code>, adds a newline.</td>
</tr>
<tr class="odd">
<td>Subroutine signatures no longer considered experimental</td>
<td>5.36.0</td>
<td>Supports named parameters in subroutines.</td>
</tr>
</tbody>
</table>
</section>
<section id="refactoring-as-structured-code" class="level2">
<h2 class="anchored" data-anchor-id="refactoring-as-structured-code">Refactoring As Structured Code</h2>
<p>We’ll start with the first subroutine that’s called.</p>
<p>Here’s the original:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode perl code-with-copy"><code class="sourceCode perl"><span id="cb9-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">sub </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exec_backup_set</span> {</span>
<span id="cb9-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@source_paths</span> = <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@</span>{ <span class="wa" style="color: #5E5E5E;
background-color: null;
font-style: italic;">$_</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] };</span>
<span id="cb9-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target_path</span>  = <span class="wa" style="color: #5E5E5E;
background-color: null;
font-style: italic;">$_</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>];</span>
<span id="cb9-4"></span>
<span id="cb9-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">foreach</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$source_path</span> (<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@source_paths</span>) {</span>
<span id="cb9-6">        exec_backup( <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$source_path</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target_path</span> );</span>
<span id="cb9-7">    }</span>
<span id="cb9-8">}</span></code></pre></div></div>
<p>And here’s the new version, using the <code>signatures</code> feature to implement subroutine arguments as lexical variables:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode perl code-with-copy"><code class="sourceCode perl"><span id="cb10-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">sub </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exec_backup_set</span> ( <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target_path</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@source_paths</span> ) {</span>
<span id="cb10-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">foreach</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$source_path</span> (<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@source_paths</span>) {</span>
<span id="cb10-3">        exec_backup( <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$source_path</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target_path</span> );</span>
<span id="cb10-4">    }</span>
<span id="cb10-5">}</span></code></pre></div></div>
<p>You’ll notice that using named arguments in the method signature is a lot cleaner. But, we have to swap the <code>source_paths</code> and <code>target_path</code> arguments. Why? Since <code>source_paths</code> is an array, it’s a ‘slurpy’ argument, meaning it will reference all remaining arguments in the signature. So, if it were the first argument, it would also absorb the contents of <code>target_path</code>.</p>
<blockquote class="blockquote">
<p>A “slurpy” parameter is a list or hash parameter that “slurps up” all remaining arguments. Since any following parameters can’t receive values, there can be only one slurpy parameter.</p>
<p>Slurpy parameters must come at the end of the signature and they must be positional.</p>
<p>Slurpy parameters are optional by default.</p>
<p><a href="https://metacpan.org/pod/Method::Signatures#Slurpy-parameters" class="uri">https://metacpan.org/pod/Method::Signatures#Slurpy-parameters</a></p>
</blockquote>
<p>Next, we’ll refactor the <code>exec_backup</code> subroutine. Here’s the original:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode perl code-with-copy"><code class="sourceCode perl"><span id="cb11-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">sub </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exec_backup</span> {</span>
<span id="cb11-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> ( <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$source</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target</span> ) = <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@_</span>;</span>
<span id="cb11-3"></span>
<span id="cb11-4">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$proc_name</span> = <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">rsync -lrtv --delete \"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">${source}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\" \"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">${target}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>;</span>
<span id="cb11-5"></span>
<span id="cb11-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">unless</span> ( <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-d</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target</span> ) {</span>
<span id="cb11-7">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target</span>);</span>
<span id="cb11-8">    }</span>
<span id="cb11-9"></span>
<span id="cb11-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> ( <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-d</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target</span> ) {</span>
<span id="cb11-11">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Syncing </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">${source}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">...</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>);</span>
<span id="cb11-12">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$proc_name</span>);</span>
<span id="cb11-13">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Synced </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">${source}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>);</span>
<span id="cb11-14">    }</span>
<span id="cb11-15"></span>
<span id="cb11-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">----------</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>);</span>
<span id="cb11-17">}</span></code></pre></div></div>
<p>For our updated version, we’ll use the <code>signatures</code> feature again, along with the <code>say</code> feature:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode perl code-with-copy"><code class="sourceCode perl"><span id="cb12-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">sub </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exec_backup_modern</span> ( <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$source</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target</span> ) {</span>
<span id="cb12-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$proc_name</span> = <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">rsync -lrtv --delete \"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">${source}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\" \"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">${target}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>;</span>
<span id="cb12-3"></span>
<span id="cb12-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">unless</span> ( <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-d</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target</span> ) {</span>
<span id="cb12-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target</span>);</span>
<span id="cb12-6">    }</span>
<span id="cb12-7"></span>
<span id="cb12-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> ( <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-d</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target</span> ) {</span>
<span id="cb12-9">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">say</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Syncing </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">${source}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">...</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>);</span>
<span id="cb12-10"></span>
<span id="cb12-11">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$proc_name</span>);</span>
<span id="cb12-12">        </span>
<span id="cb12-13">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">say</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Synced </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">${source}</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>);</span>
<span id="cb12-14">    }</span>
<span id="cb12-15"></span>
<span id="cb12-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">say</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">----------</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>);</span>
<span id="cb12-17">}</span></code></pre></div></div>
<p>Now we have enough information to refactor our entire script. Here’s the “modern” version of our <a href="../../posts/2023/perl_bad.html">old script</a>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode perl code-with-copy"><code class="sourceCode perl"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">strict</span>;</span>
<span id="cb13-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">warnings</span>;</span>
<span id="cb13-3"></span>
<span id="cb13-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> feature <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">signatures</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb13-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> feature <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">say</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb13-6"> </span>
<span id="cb13-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">sub </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exec_backup</span> ( <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$source</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target</span> ) {</span>
<span id="cb13-8">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$proc_name</span> = <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">rsync -lrtv --delete \"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">${source}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\" \"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">${target}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>;</span>
<span id="cb13-9"></span>
<span id="cb13-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">unless</span> ( <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-d</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target</span> ) {</span>
<span id="cb13-11">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target</span>);</span>
<span id="cb13-12">    }</span>
<span id="cb13-13"></span>
<span id="cb13-14">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> ( <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-d</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target</span> ) {</span>
<span id="cb13-15">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">say</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Syncing </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">${source}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">...</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>);</span>
<span id="cb13-16"></span>
<span id="cb13-17">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$proc_name</span>);</span>
<span id="cb13-18"></span>
<span id="cb13-19">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">say</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Synced </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">${source}</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>);</span>
<span id="cb13-20">    }</span>
<span id="cb13-21"></span>
<span id="cb13-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">say</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">----------</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>);</span>
<span id="cb13-23">}</span>
<span id="cb13-24"></span>
<span id="cb13-25"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">sub </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exec_backup_set</span> ( <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target_path</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@source_paths</span> ) {</span>
<span id="cb13-26">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">foreach</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$source_path</span> (<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@source_paths</span>) {</span>
<span id="cb13-27">        exec_backup( <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$source_path</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target_path</span> );</span>
<span id="cb13-28">    }</span>
<span id="cb13-29">}</span>
<span id="cb13-30"> </span>
<span id="cb13-31"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@target_paths</span> =</span>
<span id="cb13-32">  ( <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/target1/</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>, <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/target2/</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span> );</span>
<span id="cb13-33"> </span>
<span id="cb13-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># regular file sets</span></span>
<span id="cb13-35"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@regular_files</span> = (</span>
<span id="cb13-36">    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/home/jimc/source1</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>,  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/home/jimc/source2</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb13-37">    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/home/jimc/source3</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>,  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/home/jimc/source4</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb13-38">);</span>
<span id="cb13-39">exec_backup_set( <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target_paths</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@regular_files</span> );</span>
<span id="cb13-40"> </span>
<span id="cb13-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># large file sets</span></span>
<span id="cb13-42"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@large_files</span> = ( <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/home/jimc/large1</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>, <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/home/jimc/large2</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span> );</span>
<span id="cb13-43">exec_backup_set( <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target_paths</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@large_files</span> );</span>
<span id="cb13-44"></span>
<span id="cb13-45"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sleep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>);  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pause before exiting</span></span></code></pre></div></div>
<p>With these changes, we have a script that’s more concise and readable.</p>
</section>
<section id="refactoring-as-object-oriented-code" class="level2">
<h2 class="anchored" data-anchor-id="refactoring-as-object-oriented-code">Refactoring As Object-Oriented Code</h2>
<p>We’ve improved the code, but what if we want to implement it using an object-oriented paradigm instead of structured? We can accomplish this using the <code>class</code> feature.</p>
<p>Let’s start with a very simple example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode perl code-with-copy"><code class="sourceCode perl"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">strict</span>;</span>
<span id="cb14-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">warnings</span>;</span>
<span id="cb14-3"></span>
<span id="cb14-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> feature <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">class</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb14-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> feature <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">say</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb14-6"></span>
<span id="cb14-7">class <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">MyUtil::Backup</span> {</span>
<span id="cb14-8"></span>
<span id="cb14-9">    method say_hello {</span>
<span id="cb14-10">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">say</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Hello!</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>);</span>
<span id="cb14-11">    }</span>
<span id="cb14-12">}</span>
<span id="cb14-13"></span>
<span id="cb14-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$backup_obj</span> = <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">MyUtil::Backup</span>-&gt;new();</span>
<span id="cb14-15"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$backup_obj</span>-&gt;<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">say_hello</span>;</span></code></pre></div></div>
<p>If you’re familiar with “old” Perl, this will look <em>very</em> unusual. We have a couple of new keywords we’re using, <code>class</code> and <code>method</code>. In this example, we’ve defined a new class <code>Backup</code> in a namespace <code>MyUtil</code>. Inside the class, we have a single callable method named <code>say_hello</code>. We create an instance of the class named <code>$backup_obj</code> and then call the <code>say_hello</code> method. We run the code and…</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb15-1">class is experimental at ./fullsync_oop.pl line 9.</span>
<span id="cb15-2">method is experimental at ./fullsync_oop.pl line 11.</span>
<span id="cb15-3">Hello!</span></code></pre></div></div>
<p>The code runs, but the interpreter is warning us about the experimental nature of the <code>class</code> and <code>method</code> keywords. We can suppress this with a <code>no warnings</code> directive. With our code now looking like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode perl code-with-copy"><code class="sourceCode perl"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">strict</span>;</span>
<span id="cb16-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">warnings</span>;</span>
<span id="cb16-3"></span>
<span id="cb16-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> feature <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">class</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb16-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> feature <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">say</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb16-6"></span>
<span id="cb16-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">no</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">warnings</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">experimental::class</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb16-8"></span>
<span id="cb16-9">class <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">MyUtil::Backup</span> {</span>
<span id="cb16-10">    method say_hello {</span>
<span id="cb16-11">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">say</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Hello!</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>);</span>
<span id="cb16-12">    }</span>
<span id="cb16-13">}</span>
<span id="cb16-14"></span>
<span id="cb16-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$backup_obj</span> = <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">MyUtil::Backup</span>-&gt;new();</span>
<span id="cb16-16"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$backup_obj</span>-&gt;<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">say_hello</span>;</span></code></pre></div></div>
<p>We see this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb17-1">Hello!</span></code></pre></div></div>
<p>Now let’s add a field to hold our name, and use it in our greeting:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode perl code-with-copy"><code class="sourceCode perl"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">strict</span>;</span>
<span id="cb18-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">warnings</span>;</span>
<span id="cb18-3"></span>
<span id="cb18-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> feature <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">class</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb18-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> feature <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">say</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb18-6"></span>
<span id="cb18-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">no</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">warnings</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">experimental::class</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb18-8"></span>
<span id="cb18-9">class <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">MyUtil::Backup</span> {</span>
<span id="cb18-10"></span>
<span id="cb18-11">    field <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$name</span> : param;</span>
<span id="cb18-12"></span>
<span id="cb18-13">    method say_hello {</span>
<span id="cb18-14">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">say</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Hello, </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$name</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">!</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>);</span>
<span id="cb18-15">    }</span>
<span id="cb18-16">}</span>
<span id="cb18-17"></span>
<span id="cb18-18"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$backup_obj</span> = <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">MyUtil::Backup</span>-&gt;new( name =&gt; <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">Jim</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span> );</span>
<span id="cb18-19"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$backup_obj</span>-&gt;<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">say_hello</span>;</span></code></pre></div></div>
<p>We’ve added a field named <code>$name</code>. <code>param</code> indicates that the field value will be initialized in the constructor, which we call as <code>MyUtil::Backup-&gt;new( name =&gt; 'Jim' )</code>.</p>
<p>When we run, we see this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb19-1">Hello, Jim!</span></code></pre></div></div>
<p>Now we’ve learned enough to refactor our structured script, and make it object-oriented:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode perl code-with-copy"><code class="sourceCode perl"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">strict</span>;</span>
<span id="cb20-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">warnings</span>;</span>
<span id="cb20-3"></span>
<span id="cb20-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> feature <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">class</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb20-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use</span> feature <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">say</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb20-6"></span>
<span id="cb20-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">no</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">warnings</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">experimental::class</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb20-8"></span>
<span id="cb20-9">class <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">MyUtil::Backup</span> {</span>
<span id="cb20-10"></span>
<span id="cb20-11">    method exec_backup ( <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$source</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target</span> ) {</span>
<span id="cb20-12">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$proc_name</span> = <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">rsync -lrtv --delete \"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">${source}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\" \"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">${target}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>;</span>
<span id="cb20-13"></span>
<span id="cb20-14">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">unless</span> ( <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-d</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target</span> ) {</span>
<span id="cb20-15">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target</span>);</span>
<span id="cb20-16">        }</span>
<span id="cb20-17"></span>
<span id="cb20-18">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> ( <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-d</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target</span> ) {</span>
<span id="cb20-19">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">say</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Syncing </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">${source}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">...</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>);</span>
<span id="cb20-20"></span>
<span id="cb20-21">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$proc_name</span>);</span>
<span id="cb20-22"></span>
<span id="cb20-23">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">say</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Synced </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">${source}</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>);</span>
<span id="cb20-24">        }</span>
<span id="cb20-25"></span>
<span id="cb20-26">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">say</span>(<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">----------</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>);</span>
<span id="cb20-27">    }</span>
<span id="cb20-28"></span>
<span id="cb20-29">    method exec_backup_set ( <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target_path</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@source_paths</span> ) {</span>
<span id="cb20-30">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">foreach</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$source_path</span> (<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@source_paths</span>) {</span>
<span id="cb20-31">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$self</span>-&gt;<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">exec_backup</span>( <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$source_path</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target_path</span> );</span>
<span id="cb20-32">        }</span>
<span id="cb20-33">    }</span>
<span id="cb20-34">}</span>
<span id="cb20-35"></span>
<span id="cb20-36"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$backup_obj</span> = <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">MyUtil::Backup</span>-&gt;new();</span>
<span id="cb20-37"></span>
<span id="cb20-38"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@target_paths</span> =</span>
<span id="cb20-39">  ( <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/target1/</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>, <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/target2/</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span> );</span>
<span id="cb20-40"> </span>
<span id="cb20-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># regular file sets</span></span>
<span id="cb20-42"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@regular_files</span> = (</span>
<span id="cb20-43">    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/home/jimc/source1</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>,  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/home/jimc/source2</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb20-44">    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/home/jimc/source3</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>,  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/home/jimc/source4</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb20-45">);</span>
<span id="cb20-46"></span>
<span id="cb20-47"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$backup_obj</span>-&gt;<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">exec_backup_set</span>( <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target_paths</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@regular_files</span> );</span>
<span id="cb20-48"> </span>
<span id="cb20-49"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># large file sets</span></span>
<span id="cb20-50"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">my</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@large_files</span> = ( <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/home/jimc/large1</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span>, <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/home/jimc/large2</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">"</span> );</span>
<span id="cb20-51"></span>
<span id="cb20-52"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$backup_obj</span>-&gt;<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">exec_backup_set</span>( <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">$target_paths</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">@large_files</span> );</span>
<span id="cb20-53"></span>
<span id="cb20-54"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sleep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>);  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pause before exiting</span></span></code></pre></div></div>
<p>This one’s a bit more verbose, but still pretty clean and readable.</p>
<blockquote class="blockquote">
<p>Note the syntax of the call to the <code>exec_backup</code> method from the <code>exec_backup_set</code> method: I had to prefix it with <code>$self</code> to indicate that a method of the current instance of the class is being called.</p>
</blockquote>
</section>
<section id="the-verdict" class="level2">
<h2 class="anchored" data-anchor-id="the-verdict">The Verdict</h2>
<p>I still don’t consider Perl to be the friendliest language in the world, but taking advantage of modern features definitely improves the experience!</p>
</section>
<section id="links-to-references" class="level2">
<h2 class="anchored" data-anchor-id="links-to-references">Links To References</h2>
<p><a href="https://perldoc.perl.org/feature" class="uri">https://perldoc.perl.org/feature</a></p>
<p><a href="https://perldoc.perl.org/5.38.0/perlclass" class="uri">https://perldoc.perl.org/5.38.0/perlclass</a></p>


</section>

 ]]></description>
  <category>Perl</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2024/modern_perl.html</guid>
  <pubDate>Mon, 01 Jul 2024 04:00:00 GMT</pubDate>
</item>
<item>
  <title>6502 Programming in C</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2024/6502_programming_c.html</link>
  <description><![CDATA[ 






<p>Would you like to learn about programming on a very old microprocessor?</p>
<p><img src="https://jcarr.codeberg.page/blog/posts/2024/images/mos_6502ad_4585_top.png" class="img-fluid"></p>
<p><a href="https://jfcarr.github.io/kbase/articles/6502_programming_c.html">In this Knowledge Base article</a>, I’ll discuss programming for the 6502:</p>
<ol type="1">
<li>Setting up a programming environment.</li>
<li>Targeting the 6502 using the C language (and some Assembly)</li>
<li>Testing using the 8-Bit Workshop IDE.</li>
</ol>



 ]]></description>
  <category>Retro</category>
  <category>C</category>
  <category>Assembly</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2024/6502_programming_c.html</guid>
  <pubDate>Sun, 16 Jun 2024 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Think Async</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2024/think_async.html</link>
  <description><![CDATA[ 






<p>Usually, when writing asynchronous code, benefits are incremental. It can be difficult to see significant results at the micro level, unless you’re willing to dig in with profiling tools. Sometimes, though, benefits are obvious. I love these, because they provide real motivation for me to take the time to write better code.</p>
<p>A while back, I wrote a simple application to get unread mail counts for a list of accounts. I originally wrote it in Python, but I had some issues with it not always playing nice when scheduled in <a href="https://github.com/brndnmtthws/conky">Conky</a>. I rewrote it as a console application in C#, and got much better stability.</p>
<p>Originally, it was structured as follows. Information about an account is populated in a MailEntry class:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode csharp code-with-copy"><code class="sourceCode cs"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> MailEntry</span>
<span id="cb1-2"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">MailEntry</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">string</span> hostUrl<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">string</span> userName<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">string</span> password<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb1-4">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-5">        HostUrl <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> hostUrl<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-6">        UserName <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> userName<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-7">        Password <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> password<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-8">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-9"> </span>
<span id="cb1-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">string</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> HostUrl <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">get</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">set</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">string</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> UserName <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">get</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">set</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-12">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">string</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> Password <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">get</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">set</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-13"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>I store my account info in appsettings, and populate a list:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode csharp code-with-copy"><code class="sourceCode cs"><span id="cb2-1">List<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>MailEntry<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> mailEntries <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb2-2"> </span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// code to retrieve from appsettings goes here.</span></span></code></pre></div></div>
<p>Then, I iterate through the list and use my MailHandler code to do the actual checking and reporting of unread counts:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode csharp code-with-copy"><code class="sourceCode cs"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">foreach</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>MailEntry mailEntry <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> mailEntries<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb3-2"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-3">    mailHandler<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">CheckMail</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span id="cb3-4">        mailEntry<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">HostUrl</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">??</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb3-5">        mailEntry<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">UserName</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">??</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb3-6">        mailEntry<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Password</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">??</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span></span>
<span id="cb3-7">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb3-8"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>But, you’ll notice that this code runs in a linear fashion. As in:</p>
<ol type="1">
<li>Make a call to check an account.</li>
<li>Wait for that call to complete.</li>
<li>Move to the next item in the list.</li>
<li>Repeat.</li>
</ol>
<p>I have 10 accounts that I’m checking, so this is not efficient at all. It wasn’t a big deal, but it started to bother me that it was taking 10 - 15 seconds to complete on each run. I decided to do some refactoring to support asynchronicity. First, I changed Main():</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode csharp code-with-copy"><code class="sourceCode cs"><span id="cb4-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">private</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">static</span> async Task <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Main</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">string</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[]</span> args<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>Then, I created a list to hold information about the state of each running task:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode csharp code-with-copy"><code class="sourceCode cs"><span id="cb5-1">List<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Task<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> tasks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">new</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span></code></pre></div></div>
<p>I changed the CheckMail method in MailHandler:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode csharp code-with-copy"><code class="sourceCode cs"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">public</span> async Task <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">CheckMail</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">string</span> hostUrl<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">string</span> userName<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">string</span> password<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>I checked each method I’m calling in the IMAP library I’m using to see which ones have an asynch version, and updated them accordingly:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode csharp code-with-copy"><code class="sourceCode cs"><span id="cb7-1">await client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">AuthenticateAsync</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>userName<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> password<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb7-2"> </span>
<span id="cb7-3">await client<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Inbox</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">OpenAsync</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>FolderAccess<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ReadOnly</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span></code></pre></div></div>
<p>Back in Main(), the check loop is updated to add each async task to the list:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode csharp code-with-copy"><code class="sourceCode cs"><span id="cb8-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">foreach</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>MailEntry mailEntry <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> mailEntries<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb8-2"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb8-3">    tasks<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Add</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span id="cb8-4">        mailHandler<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">CheckMail</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span id="cb8-5">            mailEntry<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">HostUrl</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">??</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb8-6">            mailEntry<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">UserName</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">??</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb8-7">            mailEntry<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Password</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">??</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span></span>
<span id="cb8-8">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb8-9">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb8-10"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>And then I wait for all the tasks to complete before proceeding:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode csharp code-with-copy"><code class="sourceCode cs"><span id="cb9-1">await Task<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">WhenAll</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>tasks<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span></code></pre></div></div>
<p>My application now runs in 2 - 4 seconds. The takeaway here is that it’s absolutely worth your time to look carefully at repeating logic in your code and optimize it to run asynchronously, if you can.</p>
<p>Full code is here: <a href="https://github.com/jfcarr/unread-mail-count" class="uri">https://github.com/jfcarr/unread-mail-count</a></p>



 ]]></description>
  <category>.NET</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2024/think_async.html</guid>
  <pubDate>Sun, 09 Jun 2024 04:00:00 GMT</pubDate>
</item>
<item>
  <title>DuckDuckGo AI Chat</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2024/ddg_ai_chat.html</link>
  <description><![CDATA[ 






<p>When you execute a search on <a href="https://start.duckduckgo.com/">DuckDuckGo</a>, you’ll now see a new option at the top of the results:</p>
<p><img src="https://jcarr.codeberg.page/blog/posts/2024/images/chat-ui-option-ddg.png" class="img-fluid"></p>
<p>Clicking “Chat” gives you a UI very similar to other AI Chatbots. (You can also send your query directly to the Chat UI by including a chat bang in your query text, e.g., <code>What are the pros and cons of AI? !chat</code>.)</p>
<p><img src="https://jcarr.codeberg.page/blog/posts/2024/images/ddg-ai-chat-ui.png" class="img-fluid"></p>
<p>When the Chat UI is displayed, you’ll notice a comforting bit of text on the left-hand side:</p>
<blockquote class="blockquote">
<p>Active Privacy Protection Your chats are private, never saved by us, and not used to train AI models.</p>
</blockquote>
<p>You can choose from two different chat models:</p>
<ul>
<li><a href="https://en.wikipedia.org/wiki/GPT-3">GPT-3.5 Turbo</a> from OpenAI</li>
<li><a href="https://en.wikipedia.org/wiki/Claude_(language_model)">Claude 1.2 Instant</a> from Anthropic</li>
</ul>
<p>I seem to be getting slightly better results from Claude.</p>
<section id="caveats" class="level2">
<h2 class="anchored" data-anchor-id="caveats">Caveats</h2>
<p>Before you use an AI chatbot, consider the following:</p>
<ul>
<li><strong>Ethical Concerns</strong> In order to build its <a href="https://en.wikipedia.org/wiki/Large_language_model">LLM</a>, an AI engine scrapes massive amounts of information from the web. There are two problems with this:
<ul>
<li>The quality of data sources varies a great deal. And, even when the quality is good, you’re at the mercy of the AI engine’s ability to contextualize it properly. It is, after all, only a large statistical model.</li>
<li>The AI engine doesn’t provide credit to the original source. (This is particularly problematic in code generation, as you may be using code provided by the AI without respecting the original license of the source it was scraped from.)</li>
</ul></li>
<li><strong>Overcompensation</strong> In an effort to “do the right thing”, results can <a href="https://www.theverge.com/2024/2/22/24079876/google-gemini-ai-photos-people-pause">seriously misfire</a>.</li>
<li><strong>Curation</strong> The answers you get are filtered through the lens of the AI. Unlike a web search, where you receive results from many sources and can pick-and-choose which of those you want to follow through on, asking your questions via the AI means the AI preselects the answers for you.</li>
</ul>
<p>If you still want to make use of AI Chat, but via a delivery that’s more privacy-centric, this seems to be a good choice.</p>


</section>

 ]]></description>
  <category>AI</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2024/ddg_ai_chat.html</guid>
  <pubDate>Mon, 15 Apr 2024 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Practical Astronomy Algorithms in Various Languages</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2024/practical_astro_languages.html</link>
  <description><![CDATA[ 






<p>For several years, I’ve worked off-and-on to implement astronomical algorithms from the Practical Astronomy book in various languages. I’ve just completed a <a href="https://github.com/jfcarr/practical-astronomy-javascript">JavaScript version</a>, and I’ve published it to <a href="https://www.npmjs.com/package/practical-astronomy-javascript">NPM</a>. I’ve included a couple of simple code examples in the <a href="https://github.com/jfcarr/practical-astronomy-javascript/blob/main/README.md">repo README</a>, including how to calculate details for the April 8 solar eclipse.</p>
<section id="background" class="level2">
<h2 class="anchored" data-anchor-id="background">Background</h2>
<p>The algorithms are described in detail in the <a href="https://www.amazon.com/Practical-Astronomy-your-Calculator-Spreadsheet/dp/1108436072">Practical Astronomy with your Calculator or Spreadsheet book</a>, by Peter Duffett-Smith. I highly recommend that you get a copy of the book, as it provides lots of explanations and context that you won’t get by looking at the code alone. I worked with the 4th edition.</p>
<p>My code is actually a translation of macros from <a href="https://www.cambridge.org/us/universitypress/subjects/physics/amateur-and-popular-astronomy/practical-astronomy-your-calculator-or-spreadsheet-4th-edition#resources">accompanying spreadsheet resources</a>. You can download the spreadsheets from <a href="https://www.cambridge.org/download_file/974644">here</a>. All Languages</p>
<p>These are all the languages for which I’ve implemented the Practical Astronomy algorithms, and the status of each function’s completion (<em>as of June 1, 2024</em>).</p>
<table class="caption-top table">
<colgroup>
<col style="width: 14%">
<col style="width: 4%">
<col style="width: 8%">
<col style="width: 14%">
<col style="width: 8%">
<col style="width: 12%">
<col style="width: 9%">
<col style="width: 19%">
<col style="width: 8%">
</colgroup>
<thead>
<tr class="header">
<th>Function</th>
<th>C</th>
<th>C++</th>
<th>.NET/C#</th>
<th>PHP</th>
<th>Python</th>
<th>Java</th>
<th>JavaScript</th>
<th>Rust</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Date/Time</strong></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="even">
<td>Calculate -&gt; Date of Easter</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td>Convert -&gt; Civil Date to Day Number</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td>Convert -&gt; Civil Time &lt;-&gt; Decimal Hours</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td>Extract -&gt; Hour, Minutes, and Seconds parts of Decimal Hours</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td>Convert -&gt; Local Civil Time &lt;-&gt; Universal Time</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td>Convert -&gt; Universal Time &lt;-&gt; Greenwich Sidereal Time</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td>Convert -&gt; Greenwich Sidereal Time &lt;-&gt; Local Sidereal Time</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td><strong>Coordinates</strong></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="even">
<td>Convert -&gt; Angle &lt;-&gt; Decimal Degrees</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td>Convert -&gt; Right Ascension &lt;-&gt; Hour Angle</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td>Convert -&gt; Equatorial Coordinates &lt;-&gt; Horizon Coordinates</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td>Calculate -&gt; Obliquity of the Ecliptic</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td>Convert -&gt; Ecliptic Coordinates &lt;-&gt; Equatorial Coordinates</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td>Convert -&gt; Equatorial Coordinates &lt;-&gt; Galactic Coordinates</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td>Calculate -&gt; Angle between two objects</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td>Calculate -&gt; Rising and Setting times for an object</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td>Calculate -&gt; Precession (corrected coordinates between two epochs)</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td>Calculate -&gt; Nutation (in ecliptic longitude and obliquity) for a Greenwich date</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td>Calculate -&gt; Effects of aberration for ecliptic coordinates</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td>Calculate -&gt; RA and Declination values, corrected for atmospheric refraction</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td>Calculate -&gt; RA and Declination values, corrected for geocentric parallax</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td>Calculate -&gt; Heliographic coordinates</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td>Calculate -&gt; Carrington rotation number</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td>Calculate -&gt; Selenographic (lunar) coordinates (sub-Earth and sub-Solar)</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td><strong>The Sun</strong></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="odd">
<td>Calculate -&gt; Approximate and precise positions of the Sun</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td>Calculate -&gt; Sun’s distance and angular size</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td>Calculate -&gt; Local sunrise and sunset</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td>Calculate -&gt; Morning and evening twilight</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td>Calculate -&gt; Equation of time</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td>Calculate -&gt; Solar elongation</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td><strong>Planets</strong></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="even">
<td>Calculate -&gt; Approximate position of planet</td>
<td>x</td>
<td>x</td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td>Calculate -&gt; Precise position of planet</td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td>Calculate -&gt; Visual aspects of planet (distance, angular diameter, phase, light time, position angle of bright limb, and apparent magnitude)</td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td><strong>Comets</strong></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="even">
<td>Calculate -&gt; Position of comet (elliptical)</td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td>Calculate -&gt; Position of comet (parabolic)</td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td><strong>Binary Stars</strong></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="odd">
<td>Calculate -&gt; Binary star orbit data</td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td><strong>The Moon</strong></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="odd">
<td>Calculate -&gt; Approximate and precise position of Moon</td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td>Calculate -&gt; Moon phase and position angle of bright limb</td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td>Calculate -&gt; Times of new Moon and full Moon</td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td>Calculate -&gt; Moon’s distance, angular diameter, and horizontal parallax</td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="odd">
<td>Calculate -&gt; Local moonrise and moonset</td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td><strong>Eclipses</strong></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr class="odd">
<td>Calculate -&gt; Lunar eclipse occurrence and circumstances</td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
<tr class="even">
<td>Calculate -&gt; Solar eclipse occurrence and circumstances</td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td></td>
<td>x</td>
<td>x</td>
</tr>
</tbody>
</table>
</section>
<section id="repositories" class="level2">
<h2 class="anchored" data-anchor-id="repositories">Repositories</h2>
<ul>
<li><a href="https://github.com/jfcarr/practical-astronomy-c">C</a></li>
<li><a href="https://github.com/jfcarr/practical-astronomy-cpp">C++</a></li>
<li><a href="https://github.com/jfcarr/practical-astronomy-dotnet">.NET/C#</a></li>
<li><a href="https://github.com/jfcarr/practical-astronomy-php">PHP</a></li>
<li><a href="https://github.com/jfcarr/practical-astronomy-python">Python</a></li>
<li><a href="https://github.com/jfcarr/practical-astronomy-java">Java</a></li>
<li><a href="https://github.com/jfcarr/practical-astronomy-javascript">JavaScript</a></li>
<li><a href="https://github.com/jfcarr/practical-astronomy-rust">Rust</a></li>
</ul>


</section>

 ]]></description>
  <category>Astronomy</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2024/practical_astro_languages.html</guid>
  <pubDate>Wed, 03 Apr 2024 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Modern .NET Tooling in VS Code</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2023/modern_dotnet_tooling.html</link>
  <description><![CDATA[ 






<p>.NET / VS Code session incoming! I’ve covered this topic before, but now there are some interesting newer features in the “C# Dev Kit” extension. <a href="https://www.meetup.com/gem-city-tech/events/tlfjbtyfcpbsb/">Come to the Innovation Hub in Dayton on November 14th</a>, and learn all about it!</p>
<p><img src="https://jcarr.codeberg.page/blog/posts/2023/images/modern_dotnet_tooling_vscode.jpg" class="img-fluid"></p>
<p>The Dayton .NET Developers group is part of GemCity TECH. There are groups covering a wide variety of technical topics and interests. <a href="https://www.meetup.com/gem-city-tech/events/">Check it out!</a></p>



 ]]></description>
  <category>.NET</category>
  <category>Visual Studio Code</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2023/modern_dotnet_tooling.html</guid>
  <pubDate>Sun, 29 Oct 2023 04:00:00 GMT</pubDate>
</item>
<item>
  <title>.NET IoT and Terminal GUI</title>
  <dc:creator>Jim Carr</dc:creator>
  <link>https://jcarr.codeberg.page/blog/posts/2023/dotnet_iot_terminal_gui.html</link>
  <description><![CDATA[ 






<p>I gave this talk for the Dayton .NET Developers Group at the Innovation Hub (in Dayton, Ohio) on August 8th. I’m really happy with how this one turned out! We were able to get through all of the content, and without technical glitches! Good conversation afterwards, too.</p>
<p><img src="https://jcarr.codeberg.page/blog/posts/2023/images/dotnet_iot_terminal_gui_talk.jpg" class="img-fluid"></p>
<p>If you’re interested in the content, all of the source code is available at <a href="https://github.com/jfcarr/dotnet-iot-tui" class="uri">https://github.com/jfcarr/dotnet-iot-tui</a>, along with high-level documentation at <a href="https://jfcarr.github.io/dotnet-iot-tui/" class="uri">https://jfcarr.github.io/dotnet-iot-tui/</a>.</p>
<p>Time to start thinking about what’s next!</p>



 ]]></description>
  <category>.NET</category>
  <category>Embedded and IoT</category>
  <guid>https://jcarr.codeberg.page/blog/posts/2023/dotnet_iot_terminal_gui.html</guid>
  <pubDate>Fri, 11 Aug 2023 04:00:00 GMT</pubDate>
</item>
</channel>
</rss>
