300 lines
29 KiB
HTML
300 lines
29 KiB
HTML
<!DOCTYPE html>
|
||
<html class="writer-html5" lang="en" >
|
||
<head>
|
||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||
<title>Arduino pico — Ithamar's AD documentation</title>
|
||
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
|
||
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
|
||
<!--[if lt IE 9]>
|
||
<script src="_static/js/html5shiv.min.js"></script>
|
||
<![endif]-->
|
||
|
||
<script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
|
||
<script src="_static/jquery.js"></script>
|
||
<script src="_static/underscore.js"></script>
|
||
<script src="_static/_sphinx_javascript_frameworks_compat.js"></script>
|
||
<script src="_static/doctools.js"></script>
|
||
<script src="_static/sphinx_highlight.js"></script>
|
||
<script src="_static/js/theme.js"></script>
|
||
<link rel="index" title="Index" href="genindex.html" />
|
||
<link rel="search" title="Search" href="search.html" />
|
||
<link rel="next" title="Werkplaats Realiseren" href="P4_realiseren.html" />
|
||
<link rel="prev" title="escape-room ithamar" href="escape_room.html" />
|
||
</head>
|
||
|
||
<body class="wy-body-for-nav">
|
||
<div class="wy-grid-for-nav">
|
||
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
|
||
<div class="wy-side-scroll">
|
||
<div class="wy-side-nav-search" >
|
||
<a href="index.html" class="icon icon-home"> Ithamar's AD
|
||
</a>
|
||
<div role="search">
|
||
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
|
||
<input type="text" name="q" placeholder="Search docs" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
|
||
<p class="caption" role="heading"><span class="caption-text">Overview:</span></p>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="agenda.html">Ithamar’s Agenda</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="cijfers.html">Cijferlijst</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="cijfers.html#herkansingen">herkansingen</a></li>
|
||
</ul>
|
||
<p class="caption" role="heading"><span class="caption-text">2022-2023:</span></p>
|
||
<ul class="current">
|
||
<li class="toctree-l1"><a class="reference internal" href="escape_room.html">escape-room ithamar</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="escape_room.html#code">code</a></li>
|
||
<li class="toctree-l1 current"><a class="current reference internal" href="#">Arduino pico</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="P4_realiseren.html">Werkplaats Realiseren</a></li>
|
||
</ul>
|
||
<p class="caption" role="heading"><span class="caption-text">2023-2024:</span></p>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="Stage_J2P1.html">Stage/Internship</a></li>
|
||
</ul>
|
||
|
||
</div>
|
||
</div>
|
||
</nav>
|
||
|
||
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
|
||
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
||
<a href="index.html">Ithamar's AD</a>
|
||
</nav>
|
||
|
||
<div class="wy-nav-content">
|
||
<div class="rst-content">
|
||
<div role="navigation" aria-label="Page navigation">
|
||
<ul class="wy-breadcrumbs">
|
||
<li><a href="index.html" class="icon icon-home"></a></li>
|
||
<li class="breadcrumb-item active">Arduino pico</li>
|
||
<li class="wy-breadcrumbs-aside">
|
||
<a href="_sources/pico.rst.txt" rel="nofollow"> View page source</a>
|
||
</li>
|
||
</ul>
|
||
<hr/>
|
||
</div>
|
||
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
|
||
<div itemprop="articleBody">
|
||
|
||
<section id="arduino-pico">
|
||
<h1>Arduino pico<a class="headerlink" href="#arduino-pico" title="Permalink to this heading"></a></h1>
|
||
<p>This is where the pico ledstrip projects are store, for a bit of context: for year 1 period 2 i am tasked with working with the arduino pico and a ledstrip (w2812B).</p>
|
||
<p>This project mostly revolves around making the ledstrip light up certain leds and patterns, the fundamental lesson that the school is trying to teach us is
|
||
learning to work with loops and statements. Through these projects we will be required to rely less on simple inputs and imports and more on thinking out of the box.</p>
|
||
<p>The files refferenced can be found in <a class="reference external" href="https://git.herreweb.nl/IthamarHerrewijnen/Ithamar_code/src/branch/main/documentation/source">https://git.herreweb.nl/IthamarHerrewijnen/Ithamar_code/src/branch/main/documentation/source</a> or the git repository.
|
||
Apart from micropython no other packages have to be installed.</p>
|
||
<blockquote>
|
||
<div><div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">discolight_sl</span> <span class="kn">import</span> <span class="o">*</span>
|
||
|
||
<span class="c1"># DiscoLed()</span>
|
||
<span class="n">police</span><span class="p">()</span>
|
||
<span class="c1"># shutoff()</span>
|
||
<span class="c1"># Riveting stuff i know</span>
|
||
</pre></div>
|
||
</div>
|
||
</div></blockquote>
|
||
<p>What i did for this period code-wise is simply write a new function or script everytime i needed something, from there i’d just call said function in an adequetly named “main”
|
||
file to test the function live. To this extent i only made a few files/functions to accomodate this class subject listed below:</p>
|
||
<ul class="simple">
|
||
<li><p>Main.py: As the name suggests this file’s only purpose (as shown above) is to call upon a function from another file.</p></li>
|
||
<li><p>Colorwave: This file is used to define color to send to a ledstrip. This file is also equipped to test a led strip if the file is run as main.</p></li>
|
||
</ul>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">neopixel</span> <span class="kn">import</span> <span class="n">Neopixel</span>
|
||
|
||
<span class="n">numpix</span> <span class="o">=</span> <span class="mi">30</span>
|
||
<span class="n">strip</span> <span class="o">=</span> <span class="n">Neopixel</span><span class="p">(</span><span class="n">numpix</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">28</span><span class="p">,</span> <span class="s2">"GRB"</span><span class="p">)</span>
|
||
<span class="c1"># strip = Neopixel(numpsix, 0, 0, "GRBW")</span>
|
||
|
||
<span class="n">red</span> <span class="o">=</span> <span class="p">(</span><span class="mi">255</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
|
||
<span class="n">orange</span> <span class="o">=</span> <span class="p">(</span><span class="mi">255</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
|
||
<span class="n">yellow</span> <span class="o">=</span> <span class="p">(</span><span class="mi">255</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
|
||
<span class="n">green</span> <span class="o">=</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">255</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
|
||
<span class="n">blue</span> <span class="o">=</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">255</span><span class="p">)</span>
|
||
<span class="n">indigo</span> <span class="o">=</span> <span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">90</span><span class="p">)</span>
|
||
<span class="n">violet</span> <span class="o">=</span> <span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span>
|
||
<span class="n">colors_rgb</span> <span class="o">=</span> <span class="p">[</span><span class="n">red</span><span class="p">,</span> <span class="n">orange</span><span class="p">,</span> <span class="n">yellow</span><span class="p">,</span> <span class="n">green</span><span class="p">,</span> <span class="n">blue</span><span class="p">,</span> <span class="n">indigo</span><span class="p">,</span> <span class="n">violet</span><span class="p">]</span>
|
||
|
||
<span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
|
||
<span class="n">strip</span><span class="o">.</span><span class="n">rotate_right</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
|
||
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.15</span><span class="p">)</span>
|
||
<span class="n">strip</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Trivia</p>
|
||
<p>Colorwave: This is one of the files required to work with the ledstrip. It defines colors for easier use and can be used as a test file to see if a strip works or not. This code
|
||
contains a lot of callable intergers to be used as a reference sheet to avoid clutter in the main code, it also gives the function to the ledstrip to move from left to right.</p>
|
||
<p>NeoPixel: this file actually programs directly into the ledstrip rather than give it some intergers. This file is required to operate the ledstrip to begin with.
|
||
therefore it is a rather large file (text wise) with a full documentation on <cite>github <https://github.com/adafruit/Adafruit_NeoPixel></cite>.
|
||
This code contains all the technical functions of the ledstrip script. The interesting part for the user are functions such as <strong>brightness</strong>, <strong>show</strong> and <strong>rotate</strong>. Whereas
|
||
colorwave gave the user the colors for the ledstrip, Neopixel gives the user all the <em>fun</em> functions to create your own custom lighting.</p>
|
||
<p>discolight_sl: This is the main file i’m currently using to stash all my functions in. For the moment this only includes some code i got from some <a class="reference external" href="https://git.herreweb.nl/EljakimHerrewijnen">guy</a>
|
||
called disco. My own code which is called police and a shutoff function. Because i’m too lazy to scroll for the functions i call them in a different file.
|
||
This code has very few interesting notes on how it functions: In the function: “discolight” we use a randomizer to set every pixel to some random rgb value. In the function:
|
||
<strong>police</strong> we use a simpole calculation to get the even numbers for one set of leds and the very same formula to get the uneven leds. After that we simply make the leds take
|
||
turns in who gets to light up first, creating a policelight effect.</p>
|
||
<p>The Exams</p>
|
||
<p>I’ll tell you straight up i failed the exams almost thrice due to a mixture of circumstance, lack of cricital knowledge, tunnel vision and as it’s called in my own generation’s
|
||
words; <em>an issue of the skill</em> or <em>skill issue</em>. Since i generally find the fault with myself i tend to agree with the latter reason even if one exam was actually scuffed.</p>
|
||
<p>The exams revolved around being able to fetch data from a website, filter this data out to only show the data you want such as the latest uploaded temperature and wind direction.
|
||
This data would be printed and the leds would be programmed to respond to the data with different collors (cold = 1-15 blue, hot 1-15 red stuff like that). The time given for this
|
||
was about 3 days to figure out and turn into an essay with some proof. Now i knew my leds better than i knew everything else which was exactly the problem the first time,
|
||
I could not figure out how to filter data and this caused me to crash with the grace of a piece of satelite space debris.</p>
|
||
<p>The second time i did an exam i was with one other guy. This time i actually had a decent idea of what i was supposed to do but there was a minor ever so slight inconvenience:
|
||
The data you were supposed to collect was set up with a mqtt connection through a ssl connection. Now for context when i figured out this problem at the end of the 2nd day (roughly
|
||
16-24 hours of work in). The ssl certificate system (in hindsight was a simple fix) was never mentioned or provided in the readers which caused me great annoyance. If it looks
|
||
bad right now don’t worry it gets worse. I mailed my teacher about it who neither knew nor cared about the issue at the time so i tried to just make things work by generating
|
||
data myself and working out every solution i could.</p>
|
||
<p>Whilst doing all of this i learned a lot more about python and found out i’m very easily electrically charged. Yeah AD study’s are something sometimes, just not a good something.</p>
|
||
<p>So i mentioned it would get worse and a random guy, now i made an essay with proof and an unhealthy dosis of “office passive aggression” to which I was asked and i quote: “How do
|
||
you put so much malevolence and hatred in an essay?”. Well i showed up hoping my extensive work would at least get me a pass on the subject, I was wrong: My teacher said that due
|
||
asking the AI to generate data to test my code he couldn’t trust my work even though i could not collect the data myself, to add insult to injury he blatanly ignored his connection
|
||
mishap pulling the bureacratic move called “pleading oopsie daisy” and “It’s a hbo study you should figure it out yourself bozo”. To top it off the other guy had sent nothing
|
||
in stating “if it didn’t work he couldn’t send it” and gave up early on. So to sum it up i spent 3 days debugging something unsolvable at the time and got another shot for my trouble,
|
||
the other guy also got another shot for doing absolutely nothing. So the teacher would send another exam (3rd) later that week where he’d solve the connection part to ensure there was
|
||
no faulty information or code on our side. Now i knew the other guy was notorious for using AI from which i restrain so my frustration was immense and understandable at this point. Since
|
||
i got told to my face that my effort didn’t matter in the slightest I opted to bureacraticly engage my teacher to a breaking point from that point on to ensure he would not pull any shenanigans
|
||
like this again. (I continue this practice to this day.)</p>
|
||
<p>The third exam i did pass a week later and it felt hollow as a dead piece of bark. The code i wrote changed quite a lot since i copied from github and AI alike if i didn’t understand something
|
||
since at this point i didn’t really give a hoot whether my code was or looked authentic since i got memed on by my teachers so I decided to return similar effort. This marks the beginning of
|
||
the time where i stopped caring about how i got my code provided it worked first.</p>
|
||
<p>Much like how I started my code i divided everything up in functions but did throw at intergers into a singular file since at this point i did not care anymore. To quickly summarize everything;
|
||
The code is given all functions to connect to the local wifi and to the mqtt broker afterwards. The last exam did not care for the ledstrip and just wanted to see a led output so it
|
||
has a place alongside the print lines to show if the water was rising or falling. One of the tasks was to write the data of every hour or so to a csv file, for this I used the time of the
|
||
watermeter against itself since it only updated every 10-15 minutes and made the pico write the data down every 5th time. I will say that the tricks employed in this code are interesting
|
||
since it has a heavy focus on just being lazy but efficient which is a programmer’s quota.</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">time</span>
|
||
<span class="kn">import</span> <span class="nn">network</span>
|
||
<span class="kn">from</span> <span class="nn">machine</span> <span class="kn">import</span> <span class="n">Pin</span>
|
||
<span class="kn">from</span> <span class="nn">simple</span> <span class="kn">import</span> <span class="n">MQTTClient</span>
|
||
<span class="c1"># import csv #i can't figure out why yet but csv disables the whole script</span>
|
||
|
||
<span class="n">wlan</span> <span class="o">=</span> <span class="n">network</span><span class="o">.</span><span class="n">WLAN</span><span class="p">(</span><span class="n">network</span><span class="o">.</span><span class="n">STA_IF</span><span class="p">)</span>
|
||
<span class="n">wlan</span><span class="o">.</span><span class="n">active</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
|
||
<span class="n">wlan</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">"ssid"</span><span class="p">,</span><span class="s2">"ssidpwd"</span><span class="p">)</span>
|
||
|
||
<span class="n">ledup</span> <span class="o">=</span> <span class="n">Pin</span><span class="p">(</span><span class="mi">16</span><span class="p">,</span> <span class="n">Pin</span><span class="o">.</span><span class="n">OUT</span><span class="p">)</span>
|
||
<span class="n">leddown</span> <span class="o">=</span> <span class="n">Pin</span><span class="p">(</span><span class="mi">17</span><span class="p">,</span> <span class="n">Pin</span><span class="o">.</span><span class="n">OUT</span><span class="p">)</span>
|
||
|
||
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="n">wlan</span><span class="o">.</span><span class="n">isconnected</span><span class="p">())</span>
|
||
|
||
<span class="c1"># Use the correct MQTT server address and topic</span>
|
||
<span class="n">mqtt_server</span> <span class="o">=</span> <span class="s1">'825c912987534f048c3fbc28bb6ad943.s2.eu.hivemq.cloud'</span>
|
||
<span class="n">topic_sub</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">'waterstand'</span>
|
||
|
||
|
||
<span class="k">def</span> <span class="nf">sub_cb</span><span class="p">(</span><span class="n">topic</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
|
||
<span class="k">global</span> <span class="n">preval</span><span class="p">,</span> <span class="n">counter</span> <span class="c1">#create all variables used to avoid undefined errors</span>
|
||
<span class="n">topic_str</span> <span class="o">=</span> <span class="n">topic</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s2">"utf-8"</span><span class="p">)</span> <span class="c1"># remove unwanted characters using the strip() method https://www.w3schools.com/python/ref_string_strip.asp</span>
|
||
<span class="c1"># remove unwanted characters using the strip() method</span>
|
||
<span class="n">msg_str</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s2">"utf-8"</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"New message on topic </span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">topic_str</span><span class="p">))</span>
|
||
<span class="c1"># value = msg_str[-4:].replace(';', '') # this is the first method i used to ensure if the leds function worked, this however had problems with slicing giving me characters back which crashed the program</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Received Data: Topic = </span><span class="si">{</span><span class="n">topic_str</span><span class="si">}</span><span class="s2">, Msg = </span><span class="si">{</span><span class="n">msg_str</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
<span class="c1"># get the last item of the resulting list sorted by ;</span>
|
||
<span class="n">value</span> <span class="o">=</span> <span class="n">msg_str</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">";"</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Value: "</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
|
||
<span class="n">newval</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
|
||
<span class="k">if</span> <span class="n">newval</span> <span class="o">!=</span> <span class="n">preval</span><span class="p">:</span> <span class="c1"># compare new value with previous value</span>
|
||
<span class="k">if</span> <span class="n">newval</span> <span class="o">></span> <span class="n">preval</span><span class="p">:</span>
|
||
<span class="n">ledup</span><span class="o">.</span><span class="n">on</span><span class="p">()</span>
|
||
<span class="n">leddown</span><span class="o">.</span><span class="n">off</span><span class="p">()</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"value going up"</span><span class="p">)</span>
|
||
<span class="k">elif</span> <span class="n">newval</span> <span class="o"><</span> <span class="n">preval</span><span class="p">:</span>
|
||
<span class="n">leddown</span><span class="o">.</span><span class="n">on</span><span class="p">()</span>
|
||
<span class="n">ledup</span><span class="o">.</span><span class="n">off</span><span class="p">()</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"value going down"</span><span class="p">)</span>
|
||
<span class="n">preval</span> <span class="o">=</span> <span class="n">newval</span> <span class="c1"># update previous value</span>
|
||
|
||
<span class="n">counter</span> <span class="o">+=</span> <span class="mi">1</span>
|
||
<span class="k">if</span> <span class="n">counter</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span>
|
||
<span class="n">csvfile2</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">"data.csv"</span><span class="p">,</span> <span class="s2">"a"</span><span class="p">)</span> <span class="c1">#turns out write overwrites the entire file and i'm just a moron</span>
|
||
<span class="c1"># to ensure this works i'm using the stripped string version so no b or /n but ; stays</span>
|
||
<span class="n">csvfile2</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">msg_str</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">';'</span><span class="p">,</span> <span class="s1">','</span><span class="p">)</span> <span class="o">+</span> <span class="s2">","</span><span class="p">)</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s1">'Data written to CSV file'</span><span class="p">)</span>
|
||
<span class="c1"># Reset counter</span>
|
||
<span class="n">counter</span> <span class="o">=</span> <span class="mi">0</span>
|
||
<span class="c1"># data = [] #initially i thought i needed a list and to call or clear the last function every time</span>
|
||
<span class="c1"># # # hoe kom je van de b" en de \n af? zoek dat op.</span>
|
||
|
||
<span class="k">def</span> <span class="nf">connectMQTT</span><span class="p">():</span>
|
||
<span class="n">client</span> <span class="o">=</span> <span class="n">MQTTClient</span><span class="p">(</span><span class="n">client_id</span><span class="o">=</span><span class="sa">b</span><span class="s1">'1061348'</span><span class="p">,</span>
|
||
<span class="n">server</span><span class="o">=</span><span class="n">mqtt_server</span><span class="p">,</span>
|
||
<span class="n">port</span><span class="o">=</span><span class="mi">8883</span><span class="p">,</span>
|
||
<span class="n">user</span><span class="o">=</span><span class="sa">b</span><span class="s2">"racdata"</span><span class="p">,</span>
|
||
<span class="n">password</span><span class="o">=</span><span class="sa">b</span><span class="s2">"herkansingpython2"</span><span class="p">,</span>
|
||
<span class="n">keepalive</span><span class="o">=</span><span class="mi">7200</span><span class="p">,</span>
|
||
<span class="n">ssl</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
|
||
<span class="n">ssl_params</span><span class="o">=</span><span class="p">{</span><span class="s1">'server_hostname'</span><span class="p">:</span> <span class="n">mqtt_server</span><span class="p">}</span>
|
||
<span class="p">)</span>
|
||
<span class="n">client</span><span class="o">.</span><span class="n">set_callback</span><span class="p">(</span><span class="n">sub_cb</span><span class="p">)</span>
|
||
<span class="n">client</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s1">'Connected to </span><span class="si">%s</span><span class="s1"> MQTT Broker'</span> <span class="o">%</span> <span class="p">(</span><span class="n">mqtt_server</span><span class="p">))</span>
|
||
<span class="k">return</span> <span class="n">client</span>
|
||
|
||
<span class="c1"># If failed to connect to the MQTT broker, reset the machine</span>
|
||
|
||
|
||
<span class="k">def</span> <span class="nf">reconnect</span><span class="p">():</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s1">'Failed to connect to MQTT Broker. Reconnecting...'</span><span class="p">)</span>
|
||
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
|
||
<span class="n">machine</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>
|
||
|
||
|
||
<span class="c1"># Try to connect to the MQTT broker, if failed, reconnect</span>
|
||
<span class="k">try</span><span class="p">:</span>
|
||
<span class="n">client</span> <span class="o">=</span> <span class="n">connectMQTT</span><span class="p">()</span>
|
||
<span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
|
||
<span class="n">reconnect</span><span class="p">()</span>
|
||
|
||
<span class="n">preval</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># old first value</span>
|
||
<span class="n">counter</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># Initialize the counter</span>
|
||
<span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
|
||
<span class="c1"># ledup.on()</span>
|
||
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
|
||
<span class="c1"># ledup.off()</span>
|
||
<span class="n">client</span><span class="o">.</span><span class="n">subscribe</span><span class="p">(</span><span class="n">topic_sub</span><span class="p">)</span> <span class="c1"># subscribe to the topic</span>
|
||
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>What you can take away from all this is that much like the modern workplace hard work does not actually pay, efficient, lazy, idiotproof and preresearched work pays. It’s not
|
||
about how hard you try or how committed you are it’s about if it works and how good it works. The strongest sword wains when the wielder is weak so be smart and disciplined.</p>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
|
||
<a href="escape_room.html" class="btn btn-neutral float-left" title="escape-room ithamar" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
|
||
<a href="P4_realiseren.html" class="btn btn-neutral float-right" title="Werkplaats Realiseren" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
|
||
</div>
|
||
|
||
<hr/>
|
||
|
||
<div role="contentinfo">
|
||
<p>© Copyright 2022, Ithamar.</p>
|
||
</div>
|
||
|
||
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
|
||
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
|
||
provided by <a href="https://readthedocs.org">Read the Docs</a>.
|
||
|
||
|
||
</footer>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
</div>
|
||
<script>
|
||
jQuery(function () {
|
||
SphinxRtdTheme.Navigation.enable(true);
|
||
});
|
||
</script>
|
||
|
||
</body>
|
||
</html> |