<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mohamed Nehad on Medium]]></title>
        <description><![CDATA[Stories by Mohamed Nehad on Medium]]></description>
        <link>https://medium.com/@MohammedNehad?source=rss-c8a36c971a5f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Ku02yI44_GPqqywS4qYt_g@2x.jpeg</url>
            <title>Stories by Mohamed Nehad on Medium</title>
            <link>https://medium.com/@MohammedNehad?source=rss-c8a36c971a5f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 08:20:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@MohammedNehad/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How I Built an Automation System for My Garden’s Irrigation]]></title>
            <link>https://medium.com/@MohammedNehad/how-i-built-an-automation-system-for-my-gardens-irrigation-199c30199527?source=rss-c8a36c971a5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/199c30199527</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <dc:creator><![CDATA[Mohamed Nehad]]></dc:creator>
            <pubDate>Sat, 29 Jul 2023 18:12:45 GMT</pubDate>
            <atom:updated>2023-07-29T18:31:06.774Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Introduction</strong></h3><p>Irrigating plants can be a time consuming task, not to mentions it has to be done in a timely manner. So summer was around the corner, I was going away from home, And in the hot summer days of Egypt, My home garden needed to be irrigated regularly, after getting help setting up the water line, and with the help of an Amazon bought irrigation kit I hooked everything with electric valves to be able to automate the irrigation while I was away.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2dWbD7G1TVlpoyCMwlAEUA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*hwfudIH9VwNnTTHA3ykYYA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*FI-FuYhvrejhIl4tXV5hkQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z0WduvznlGjP59g43b58Yw.jpeg" /><figcaption>Pictures of my garden</figcaption></figure><p>The hardware setup was fairly straight forward, A Raspberry Pi connected to a relay board via the GPIO pins.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DXwO-Xbe2McbMwa_MWhb7w.jpeg" /><figcaption>Raspberry pi 2 hooked to a 16-channel relay board &amp; RTC module</figcaption></figure><h3><strong>The Software</strong></h3><p>At first I used a simple Python script with a cronjob, It proved effective at first, However, I needed something more customizable and user-friendly so anyone in my household can use it</p><p>My original solution, Called <a href="https://github.com/mohamednehad450/pi-irrigation-server">pi-irrigation-server</a>, was hastily developed with a limited user interface. Although it solved some of the issues with the original script, it was still challenging to work with. It only allowed one active sequence and a single static weekly schedule, requiring manual adjustments each season. Additionally, it lacked the ability to stop a running sequence, and couldn’t create complex sequences.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vl5bMLkSNgtAOfrI40txrQ.png" /><figcaption>Screenshots of pi-irrigation-server</figcaption></figure><p>Now that I have clear requirements, I need to be able do define GPIO channels and a label them, Easily create and execute complex sequences, Define seasonal schedules to trigger those sequences, And log events to check them later.</p><h3><strong>1. The Core utility</strong></h3><p>Defining the `pins` is simple enough, it’s just a label with a <a href="https://www.raspberrypi.com/documentation/computers/raspberry-pi.html">GPIO number</a>, and whether the on state is `HIGH` or `LOW`.</p><pre>{<br>  label: string<br>  channel: number         // Valid GPIO number<br>  onState: &quot;HIGH&quot; | &quot;LOW&quot;<br>}</pre><p>As for the `sequences`, to achieve the desired sequence complexity, each sequence has an arbitrary number of `orders` with a `channel`, `duration`, and `offset`</p><pre>{<br>  id: number<br>  name: string<br>  active: boolean<br>  lastRun?: string // ISO date string<br>  orders: {<br>    channel: number   // Pre-defined channel<br>    offset: number    // milliseconds off set from start<br>    duration: number  // milliseconds to stay on after offset<br>  }[]<br>  ...<br>}</pre><p>Therefor running a sequence is fairly straight forward, after validating the `orders` for overlapping, I just convert them into a series of `setTimeout` calls. And to prevent overlap between different sequences, I implemented a channel reserving system.</p><p>As for the scheduling, I used <a href="https://github.com/kelektiv/node-cron">cron</a>, a tool that allows cron style scheduling in JavaScript, and added to every cron expression a label.</p><pre>{<br>  id: number<br>  label: string<br>  cron: string   // A valid cron expression<br>}</pre><p>And by creating a many-to-many relation between `sequences` and `crons`, all the `crons` are loaded on startup, and when they get triggered, the database is checked for active, linked sequences to run them with the least priority to the recently ran sequence.</p><p>Adding event logs is fairly simple, I needed to log what’s happening with each sequences so it can be viewed later in chronological order.</p><pre>{<br>  id: number<br>  sequenceId: number<br>  eventType: &quot;run&quot; | &quot;stop&quot; | &quot;finish&quot; | &quot;activate&quot; | &quot;deactivate&quot;<br>  date: string // ISO date string<br>}</pre><h3><strong>2. The Database</strong></h3><p>I initially chose <a href="https://www.prisma.io/">Prisma</a> as the database ORM. However, the Prisma team doesn’t provide a pre-compiled binaries of the prisma engines for arm/v7, and compiling them myself was too mush of a hassle, which means older Pi models won’t be supported, Consequently, I switched to <a href="https://github.com/drizzle-team/drizzle-orm">Drizzle</a>, a TypeScript-based ORM, which worked flawlessly, had better TypeScript support, no code gen, and integrated well with <a href="https://zod.dev/">Zod</a>.</p><h3><strong>3. The API</strong></h3><p>The REST API is built using <a href="https://expressjs.com/">Express</a> to manage CRUD operation for `sequences`, `crons`, `pins`, retrieve `events`, and expose device actions and status. And <a href="https://github.com/auth0/node-jsonwebtoken">JWT</a> was used for user authentication.</p><p><a href="https://socket.io/">Socket.io</a> was used for the real-time updates and the Express api was used as a fallback.</p><h3><strong>4. The UI</strong></h3><p>For the frontend I used <a href="https://nextjs.org/">Next.js</a>, To implement the UI quickly I used <a href="https://mantine.dev/">Mantine</a>, which is a great component library which gave me accessible, responsive, and themeable components.</p><p>The Dashboard shows the realtime channels status, running and active sequences, and device time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2WxkmfG0rLQ010M2pB_VjQ.png" /><figcaption>Screenshot of the dashboard</figcaption></figure><p>The Device page allows for `pins` and `schedules` management, and view all events.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5xSd1dNYXyz_qGEt23zAtw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2h_fMMGb7cTdWLKDBz-aoQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*obDv4Iw71nOovP-JJpdS1A.png" /><figcaption>Screenshots of the Device page</figcaption></figure><p>The sequences page shows all defined `sequences`, and take you to individual `sequence` page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n7KV4ZsXnF8uDC2GiaUO-A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F6EzLIm1-oj3fCK8XX0dqA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6Zxcg3MAgrB3CgSJZa0yCQ.png" /><figcaption>Screenshots of the Sequences page</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7ol3GRvZQ341KfBVdabXXA.png" /><figcaption>Overview of application architecture</figcaption></figure><h3><strong>Installation</strong></h3><p>To make installation as easy as possible, I containerized the entire thing using <a href="https://www.docker.com/">Docker</a>.</p><p>To set up the application, follow these steps:</p><p>Setup a fresh install <a href="https://www.raspberrypi.com/software/">Raspberry Pi OS</a></p><ol><li>Install docker</li></ol><pre>curl -sSL https://get.docker.com | sh<br><br># Allow Docker to be used without being a root<br>sudo usermod -aG docker $USER<br><br># Restart<br>reboot</pre><p>2. Create the docker container</p><pre>docker container create \<br>--name scheduler_container \<br>-v /sys:/sys \<br>-p 8000:8000 \<br>-e TOKEN_KEY=$(head /dev/urandom | tr -dc A-Za-z0–9 | head -c 50) \<br>mohamednehad450/scheduler-frontend</pre><p>3. Start the app</p><pre>docker start scheduler_container</pre><p>and now the application is running on port 8000</p><h3>Demo</h3><p>After successfully setting up the application, defining channels and sequences, we sit back, relax, and watch it go.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*-VWCOkm_ztfF4e5iTmg1Mw.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*tsFibD44Eil4IbEijck9pQ.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*oA5Qq1jsfBoVemB8lMlICA.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*IwbHEToXxrFPPdqUC6daQA.gif" /><figcaption>Demos of different sequences</figcaption></figure><h3><strong>Conclusion</strong></h3><p>In conclusion, Building this project was so much fun and successfully solved my irrigation problem using of the shelf components, and the software added a user-friendly interface, allowing for relay’s channel management, complex sequence creation, and seasonal scheduling.</p><p>However, keep in mind that alternative solutions exist that offer smart home integration like <a href="https://tasmota.github.io/docs/">Tasmota</a> for ESP devices.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=199c30199527" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>