<?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 Paulo Daniel on Medium]]></title>
        <description><![CDATA[Stories by Paulo Daniel on Medium]]></description>
        <link>https://medium.com/@pdgonzalez872?source=rss-8b929dd87802------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*m3aNrA1gmA3qIzxa.jpg</url>
            <title>Stories by Paulo Daniel on Medium</title>
            <link>https://medium.com/@pdgonzalez872?source=rss-8b929dd87802------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 10:45:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pdgonzalez872/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[Accessing named GenServers from anywhere in a BEAM cluster]]></title>
            <link>https://medium.com/@pdgonzalez872/accessing-named-genservers-from-anywhere-in-a-beam-cluster-ffc062df526c?source=rss-8b929dd87802------2</link>
            <guid isPermaLink="false">https://medium.com/p/ffc062df526c</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Paulo Daniel]]></dc:creator>
            <pubDate>Fri, 03 May 2019 18:57:58 GMT</pubDate>
            <atom:updated>2019-05-03T21:09:20.617Z</atom:updated>
            <content:encoded><![CDATA[<p>## An example using named `global` Agents:</p><p>You can create named Agents that can be called from any connected node in a<br> cluster. This technique can be helpful when you have a cluster, don’t<br> necessarily know which node will handle the work and you want to ensure<br> consistency between nodes by avoiding duplication between nodes. If you<br> don’t, you can run into a situation where a you have multiple Agents with<br> different states in different nodes.</p><p>If you ever choose to use this technique, consider using a Supervision tree<br> that tries to keep its state across nodes. If one of your Agents goes<br> down, you’d probably want to handle that somehow. There are libraries that<br> currently support this functionality. <a href="https://github.com/derekkraan/horde">Horde</a> is one of them. On to the example:</p><p>Create two named `iex` instances in two separate terminal instances:</p><pre># On terminal 1:<br> iex — name t1@127.0.0.1</pre><pre># On terminal 2:<br> iex — name t2@127.0.0.1</pre><pre># Connect instances together. From either t1 or t2, connect to the other<br> # instance:</pre><pre># from t2<br> Node.connect(:”t1@127.0.0.1&quot;) #=&gt; true</pre><pre># Assert we are connected. Check from either instance:<br> # from t2<br> Node.list() #=&gt; [:”t1@127.0.0.1&quot;]</pre><pre># from t1<br> Node.list() #=&gt; [:”t2@127.0.0.1&quot;]</pre><pre># Now, let’s create an Agent from from `t1`:<br> Agent.start_link(fn -&gt; %{t1: true, t2: false} end, name: {:global, “choose a name”})</pre><pre>`# from `t2`<br> Agent.get({:global, “choose a name”}, &amp; &amp;1) #=&gt; %{t1: true, t2: false}</pre><pre>## Visual<br><br>|--------|    |--------|<br>|   t1   |----|   t2   |<br>|--------|    |--------|<br>|        |    |        |<br>|   At1  |    |        |<br>|        |    |        |<br>|--------|    |--------|<br><br>- At1 -&gt; `global` named Agent<br><br>## Conclusion:<br>t2 can communicate with At1 in a connected BEAM cluster</pre><p>Thanks for reading, PDG</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ffc062df526c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Raspberry Pi 3 for Christmas!]]></title>
            <link>https://medium.com/@pdgonzalez872/raspberry-pi-3-for-christmas-56bfa787d856?source=rss-8b929dd87802------2</link>
            <guid isPermaLink="false">https://medium.com/p/56bfa787d856</guid>
            <category><![CDATA[scratch]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <dc:creator><![CDATA[Paulo Daniel]]></dc:creator>
            <pubDate>Sat, 24 Nov 2018 22:58:37 GMT</pubDate>
            <atom:updated>2018-11-24T23:03:56.464Z</atom:updated>
            <content:encoded><![CDATA[<h3>Raspberry Pi for Christmas!</h3><p>During Thanksgiving this year, I got to spend some time with one of Kels’ cousin’s son, Bentley (8). He is a very clever kid, so I decided to get him into programming with <a href="https://scratch.mit.edu/">Scratch</a>.</p><p>We go to Kels’ grandpa’s laptop and start to check it out. We get into it, I sit by him navigating while he is in control of the machine. I give hints, I tell him to check some stuff out, nudging him to (what I think) was reasonable to try out.</p><p>I ask if he had a computer at home and he said he used his dad’s laptop sometimes. Bentley has a ton of video games and is fairly tech savvy. Definitely a lot more than me at that age.</p><p>During the drive home, I was thinking about how cool it is to have access to this type of technology and how it is possible in today’s day and age. With the Internet, things are out there for you to consume if you have the right mindset and have a little bit of support when you are stuck.</p><p>A while ago, I promised myself that I would always help folks that are trying to get started with computers because I would have loved that type of help growing up. It makes a huge difference to not be alone.</p><p>So, let’s get Bentley started with computers!</p><p>I thought a little bit about the problem: I wanted to give him a good starting point to learn about the machine and programming. He didn’t need to be able to play huge games, as he already has a way of doing that. I also wanted to see how economical I could make this project. My solution and minimal viable product was: <em>Make the most affordable yet usable intro computer for a kid to play with programming</em>.</p><h4>Buying the parts:</h4><p>I like shopping locally when possible, so I love going to <a href="https://www.microcenter.com/">Micro Center</a>. Here are the parts for the project. I’ll actually just show you the receipt:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*PKUT8UVITieZNyHK74Jq5w.jpeg" /><figcaption>Parts, $69 bucks</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TYJCo-EGD3HmMVm8qYbFFg.jpeg" /><figcaption>Here they are laid out</figcaption></figure><p>I thought a big part of the project was to make it relatively inexpensive. We were able to get it below $70, which for a computer is pretty good. I wanted to show Bentley that you don’t need a lot of money to build cool stuff that you can reuse for a long time. I basically bought the their private label (Inland) as much as possible, with the exception of the Raspberry Pi (RPi from now on).</p><h3>Setting things up:</h3><p>We need to add the operating system for the RPi. Here are the steps: <a href="https://www.raspberrypi.org/documentation/installation/installing-images/README.md">https://www.raspberrypi.org/documentation/installation/installing-images/README.md</a></p><ul><li>Download the Raspbian image here: <a href="https://www.raspberrypi.org/downloads/raspbian/">https://www.raspberrypi.org/downloads/raspbian/</a></li><li>Download Etcher, use the instructions for your operating system. I used these: <a href="https://github.com/balena-io/etcher#debian-and-ubuntu-based-package-repository-gnulinux-x86x64">https://github.com/balena-io/etcher#debian-and-ubuntu-based-package-repository-gnulinux-x86x64</a></li><li>Use Etcher to write the Raspbian image on the SD card.</li><li>When complete, insert the SD card and plug things where they have to go: keyboard, mouse, power and HDMI to the RPi, the power to the outlet and the other end of the HDMI cable to a TV or monitor.</li></ul><p>The RPi will boot up immediately after you connect it to power. You should be able to go to the HDMI input you selected on the TV and see the below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KNMvdB-P0sjKTbw3ogxiLw.jpeg" /><figcaption>The complete set</figcaption></figure><p>These new <a href="https://www.raspberrypi.org/products/raspberry-pi-3-model-b-plus/">RPis (this is the 3 B+)</a> are pretty good machines. They feel fast:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F_Jp3IVUuSi0&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D_Jp3IVUuSi0&amp;image=http%3A%2F%2Fi.ytimg.com%2Fvi%2F_Jp3IVUuSi0%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad659f9fcb47a5057ab6f3d77a9aa7b2/href">https://medium.com/media/ad659f9fcb47a5057ab6f3d77a9aa7b2/href</a></iframe><p>I went through the “Configure Pi” prompt that comes up after the first boot. It asks you to change the password, your language settings, basic stuff.</p><p>I didn’t install anything extra on the RPi. That’s for Bentley to do and tell me about it. The defaults are great. Scratch 1 and 2, Python, a terminal, and lots of other cool things are already installed. I probably should have installed Elixir so he can get going with the best programming language out there :)</p><p>Now we get to wait for Christmas. I think it will be a fun one!</p><p>PG</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=56bfa787d856" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dimmable LED light]]></title>
            <link>https://medium.com/@pdgonzalez872/dimmable-led-light-64363482a88f?source=rss-8b929dd87802------2</link>
            <guid isPermaLink="false">https://medium.com/p/64363482a88f</guid>
            <dc:creator><![CDATA[Paulo Daniel]]></dc:creator>
            <pubDate>Mon, 23 Jul 2018 13:51:32 GMT</pubDate>
            <atom:updated>2018-07-23T13:51:32.202Z</atom:updated>
            <content:encoded><![CDATA[<p>One of our LED lights stopped working this weekend, so I took it apart to look at what is inside.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IUIsl6pBg9TQMWKH-jXgBw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c8A4_7F0xJ3S-6xeuB0E8g.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5D9uo6NaiMWmv3OArzSwkQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eIQEw8J5DPnvTO3g5mkHOw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dQDWt3zDPWVrbhUgb8MqCQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fVXQZtwcn0ugmdw1Z2EK4A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uSZtXIBjcZhmNWhOp385Sw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kx65K895Jok7VI62AJDc7A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G451_yCXhGVKDNZfBOhGrg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MXAS3n-HDi0K94fG3cB7Ew.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qRnYxwsu8Suuv6ZXO8UUbA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o9-I-2Q91NPjv1tbOvPaIQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Buqxpg4o_5I21mEfDvyoeg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nuQJq8P_lLC364U3A6y6vA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xNI506C7hGfdDaDX0HcFmw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z_Fh4QCML5dTn5O2oKB74g.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=64363482a88f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ATP earnings project]]></title>
            <link>https://medium.com/@pdgonzalez872/atp-earnings-project-5969c95070a1?source=rss-8b929dd87802------2</link>
            <guid isPermaLink="false">https://medium.com/p/5969c95070a1</guid>
            <category><![CDATA[tennis]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Paulo Daniel]]></dc:creator>
            <pubDate>Mon, 18 Jun 2018 23:19:31 GMT</pubDate>
            <atom:updated>2023-10-06T02:24:28.803Z</atom:updated>
            <content:encoded><![CDATA[<p>I wanted to share this project because I saw Fernando Meligeni (a retired tennis player — and legend — I follow on LinkedIn) mention a documentary series called <a href="https://www.youtube.com/channel/UC_J3DrSWxG2EJpLD3gVXI9w">“Lejos de las Luces”</a>. A rough translation could be “Away from the Spotlight”. I highly suggest the documentary even if you are a non-speaker. The captions are fairly good. Here is the first <a href="https://github.com/pdgonzalez872/atp_data_elixir/blob/master/www.youtube.com/watch?v=0d4S611Z1h0&amp;t=1s">episode</a>. The documentary highlights the struggles of different tennis players to make it in the professional tour. It is a very competitive scene.</p><p>This project shows the discrepancy between earnings in the ATP tennis circuit. We gather data from the <a href="http://www.atpworldtour.com/en/rankings/singles">list of currently ranked singles players</a> in the ATP (for the current season) and display the most recent amount for each of the players on the list. Here is a screenshot that shows the distribution:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/977/1*cZ_0z_pUf7rpFZ6WI2FzFA.png" /></figure><p>The idea of scraping career earnings from the ATP website was my first programming project. I’ve rewritten it in multiple languages. This particular one uses Elixir and does have some more parts added to it, rather than only displaying the name and amount as my original project did. Good times :)</p><h3>Tech used</h3><p>This app is written in Elixir, uses Phoenix to handle web requests, Postgres to persist the data and Chart.js to create the charts. At this time, I manually run a task to gather the data, there hasn’t been a need to automate this part yet.</p><p>Source code is here: <a href="https://github.com/pdgonzalez872/atp_data_elixir">https://github.com/pdgonzalez872/atp_data_elixir</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5969c95070a1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Nerves with Rpi3, wifi and Picam.]]></title>
            <link>https://medium.com/@pdgonzalez872/using-nerves-with-rpi3-wifi-and-picam-3b04cee78d22?source=rss-8b929dd87802------2</link>
            <guid isPermaLink="false">https://medium.com/p/3b04cee78d22</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[nerve]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <dc:creator><![CDATA[Paulo Daniel]]></dc:creator>
            <pubDate>Mon, 14 May 2018 01:17:33 GMT</pubDate>
            <atom:updated>2018-06-19T23:08:54.676Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Disclaimer</strong>: This is a copy/paste of the ReadMe here: <a href="https://github.com/pdgonzalez872/nerves_rpi3_wifi_picam">https://github.com/pdgonzalez872/nerves_rpi3_wifi_picam</a>. Working code is there if needed, but I suggest you code along and go through the steps.</p><h3>Introduction:</h3><p>A friend of mine asked me about a remote device to monitor a physical machine in a production line. We discussed about it being able to gather some data about the performance of the machine, usage and such. I’ve been thinking about this problem off and on for a while and eventually wanted to learn more about Nerves. Maybe this could in fact become a the project my friend and I were talking and we tackle it professionally. The tools do seem to be there and it seems like a great learning experience for me.</p><p>This guide is somewhat guided towards beginners in the electronics world. I compiled some notes after looking at Nerves last weekend and during this week. It’s a good exercise to put them together and hopefully someone can benefit from this.</p><h3>Overall idea:</h3><p>Use <a href="https://nerves-project.org/">Nerves</a> to set up a local server (avaliable on your network) that streams something you’d like to watch (such as outside traffic, your pets or whatever you’d like) via wifi.</p><h3>Hardware needed:</h3><ul><li><a href="http://www.microcenter.com/product/506089/Raspberry_Pi_3_Model_B_Complete_Starter_Kit_-_16_GB_Edition">Raspberry Pi 3 and accessories, including mini SD card</a></li><li><a href="http://www.microcenter.com/product/465935/raspberry_pi_camera_module_v2">Raspberry Pi Camera Module V2</a></li></ul><p>Note: You can find the above anywhere, get it where you think is optimal. Try to support Microcenter though! :)</p><h3>Software needed:</h3><p>I’m using a Mac, Erlang 20 and Elixir 1.6 Your miles may vary depending on your setup.</p><p>Look at the below, do it and come back when you are done:</p><ul><li>If you don’t have Elixir on your computer, go to <a href="https://elixir-lang.org/install.html">https://elixir-lang.org/install.html</a></li><li>If you don’t have Nerves on your computer, go to <a href="https://hexdocs.pm/nerves/installation.html">https://hexdocs.pm/nerves/installation.html</a></li></ul><h3>Important Note:</h3><p>I’m going to be using the exact commands as I&#39;m using it on my machine. I&#39;ll only omit keys/passwords. I find this to be one of the biggest pain points in starting out something you don&#39;t know about. Namespace clashes, a typo somewhere, ommitting things that are assumed to be known by everyone. For changes, I will show the output of git diff as I think that&#39;s a great way to see what actually was done. I&#39;m not sure that&#39;s the best way of displaying differences, but I like it and I&#39;m going to try it here.</p><h3>Steps and tools:</h3><p>We are going to use Nerves and incrementally add features to our device. In order:</p><ul><li>Setup a new project with Nerves</li><li>Add and configure a basics package that saves us a lot of time by allowing remote updates to the Raspberry Pi (Rpi from now on) and configures the networking (wifi included) on the Rpi. This is the <a href="https://github.com/nerves-project/nerves_init_gadget">nerves_init_gadget</a> package.</li><li>Deploy the app and connect to it’s BEAM node through another node (one on our machine). This doesn’t really set us toward our main goal, but I think it is a good way to learn more about OTP.</li><li>Add and configure <a href="https://github.com/electricshaman/picam">Picam</a>. We will first take a picture and make sure things are working correctly.</li><li>Add a server layer with <a href="https://hexdocs.pm/plug/readme.html">Plug</a>. We will leverage the <a href="https://github.com/electricshaman/picam/tree/master/examples/picam_http">example</a> and add it to our project.</li></ul><h3>Step — Setting up project</h3><p>Go to a folder of choice in your machine and run:</p><pre>mix nerves.new nerves_rpi3_wifi_picam<br>cd nerves_rpi3_wifi_picam</pre><p>Type y when prompted to fetch dependencies.</p><h3>Step — nerves_init_gadget</h3><p>One thing that I had already setup before but can see how it may trip some folks up is generating an authorized key to make it more secure to talk to our Rpi over the network. So if you have not set that up yet, do it. This is what I used to have mine setup: <a href="https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/">https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/</a></p><p>Then, follow the <a href="https://github.com/nerves-project/nerves_init_gadget#installation-for-a-new-project">installation steps</a> from the readme. :nerves_network is a dependency here and we set both up below. Here are the files changes and their diffs:</p><pre>## mix.exs</pre><pre>diff --git a/mix.exs b/mix.exs<br>   defp deps(target) do<br>     [<br>-      {:nerves_runtime, &quot;~&gt; 0.4&quot;}<br>+      {:nerves_runtime, &quot;~&gt; 0.4&quot;},<br>+      {:nerves_init_gadget, &quot;~&gt; 0.3&quot;}<br>     ] ++ system(target)<br>   end</pre><pre>## config/config.exs</pre><pre>diff --git a/config/config.exs b/config/config.exs<br> config :shoehorn,<br>-  init: [:nerves_runtime],<br>+  init: [:nerves_runtime, :nerves_init_gadget],<br>   app: Mix.Project.config()[:app]</pre><pre>+config :logger, backends: [RingLogger]<br>+<br>+config :nerves_firmware_ssh,<br>+  authorized_keys: [<br>+    File.read!(Path.join(System.user_home!, &quot;.ssh/id_rsa.pub&quot;))<br>+  ]<br>+<br>+config :logger, backends: [RingLogger]<br>+<br>+config :nerves_rpi3_wifi_picam, interface: :wlan0, port: 4001<br>+<br>+config :nerves_init_gadget,<br>+  node_name: :target01,<br>+  mdns_domain: &quot;nerves.local&quot;,<br>+  address_method: :dhcp,<br>+  ifname: &quot;wlan0&quot;<br>+<br>+key_mgmt = System.get_env(&quot;NERVES_NETWORK_KEY_MGMT&quot;) || &quot;WPA-PSK&quot;<br>+<br>+config :nerves_network, :default,<br>+  wlan0: [<br>+    ssid: System.get_env(&quot;NERVES_NETWORK_SSID&quot;),<br>+    psk: System.get_env(&quot;NERVES_NETWORK_PSK&quot;),<br>+    key_mgmt: String.to_atom(key_mgmt)<br>+  ],<br>+  eth0: [<br>+    ipv4_address_method: :dhcp<br>+  ]</pre><p>We are about ready to burn the firmware on the mini SD card (SD card from now on). Before doing so, flash the SD card. On my Mac, I went to Disk Utility, selected the SD card on the left and clicked &quot;Erase&quot;. Here are more details on that: <a href="https://www.youtube.com/watch?v=UmIanMx1HDM">https://www.youtube.com/watch?v=UmIanMx1HDM</a></p><p>Get the SD card and (its converter) and insert it in the computer.</p><p>Now, we can run these commands in succession:</p><pre>export NERVES_NETWORK_SSID=ButlerKasey<br>export NERVES_NETWORK_PSK=your_network_password<br>export MIX_TARGET=rpi3<br>mix deps.get<br>mix firmware</pre><p>Note:</p><ul><li>the NERVES_NETWORK_SSID is your wifi network name. In my case, ButlerKasey, my cat and dog. :)</li><li>Make sure to replace the your_network_password with your password.</li></ul><p>If success, run:</p><pre>mix firmware.burn</pre><p>It will ask you to confirm the SD card it found, type y and then my machine also prompted me for my login password.</p><p>Once burned, eject the SD card, place it in the device and wait a couple of minutes.</p><p>Now, we should be in a great spot, because we should be able to push changes to the device remotely. No more physically burning the SD card. I personally find this mind blowing, I hope you do too.</p><p>When we set up nerves_init_gadget, we also set a couple of options that are going to be useful now: node_name and mdns_domain. These combined are used for setting up the device on the network. Instead of using its ip, you can use its name -&gt; target01@nerves.local</p><h3>Making sure things are connected with OTP:</h3><p>Open up the project and see which cookie it created for this specific project. It will be in rel/vm.args. It will be different for your project.</p><pre># in the rel/vm.args file:</pre><pre>-setcookie 1N1T5csB6k6gHtX47aQX4eZL07xEDknMK0JmOi42q67rhMyxMguQsV02ST/lsjoj</pre><p>Use that cookie and run the below in a separate terminal instance in your computer. This will create a named node in the VM network and pass it the cookie we need to connect to our target node.</p><pre>iex --name host@0.0.0.0 --cookie &quot;1N1T5csB6k6gHtX47aQX4eZL07xEDknMK0JmOi42q67rhMyxMguQsV02ST/lsjoj&quot;</pre><p>You will get an IEx shell and you are in Elixir land. Now, we connect the nodes by running the below inside the IEx shell:</p><pre>Interactive Elixir (1.6.0) - press Ctrl+C to exit (type h() ENTER for help)<br>iex(host@0.0.0.0)1&gt; Node.connect(:&quot;target01@nerves.local&quot;)<br>true<br>iex(host@0.0.0.0)2&gt; Node.list<br>[:&quot;target01@nerves.local&quot;]<br>iex(host@0.0.0.0)3&gt; :rpc.call(:&quot;target01@nerves.local&quot;, NervesRpi3WifiPicam, :hello, [])<br>:world</pre><p>Please note that the rpc function call was made on the host but it was computed in the target! We can be sure of that because our IEx session on the host is a simple one, meaning it doesn&#39;t know about the NervesRpi3WifiPicam project at all.</p><p>If you have an HDMI cable and a keyboard/mouse setup, do the connection exercise above when connected to a monitor/tv. You will see that when you connect to the target, you can do Node.list on the target and it will list the hostnode. This is pretty awesome.</p><h3>Push changes to the device remotely:</h3><p>Now that our target is online, we can push changes to the device remotely. Try it. Edit the NervesRpi3WifiPicam.hello/0function in nerves_rpi3_wifi_picam/lib/nerves_rpi3_wifi_picam.ex to return &quot;not world&quot; and run the below:</p><pre>export NERVES_NETWORK_SSID=ButlerKasey &amp;&amp; export NERVES_NETWORK_PSK=your_network_password &amp;&amp; export MIX_TARGET=rpi3 &amp;&amp; mix firmware &amp;&amp; mix firmware.push nerves.local</pre><p>The gotcha for me here was the argument of mix firmware.push, which in our case is nerves.local. Not the full name.</p><p>Wait a little bit, go back to the connected IEx session and try Node.connect(:&quot;target01@nerves.local&quot;). It will return true when you are connected. To confirm your changes, and call</p><pre>:rpc.call(:&quot;target01@nerves.local&quot;, NervesRpi3WifiPicam, :hello, [])</pre><p>We should get “not world”. Try it.</p><h3>Use ssh to connect to the device:</h3><p>We can also SSH into the device. Folks are working on doing this in a simpler way on the Nerves side, but here we see an alternative: <a href="https://elixirforum.com/t/connect-to-a-nerves-system-using-ssh/10723/4">link</a></p><p>We can run:</p><pre>ssh -p 8989 target01@nerves.local</pre><p>If prompted about authenticity, type yes. We have access to the Erlang shell. In order to get the Elixir shell, run:</p><pre>&#39;Elixir.IEx&#39;:start().</pre><p>One thing I found was to disconned the session I closed my terminal instance. If I did not, I had issues pushing changes to the target as @ConnorRigby mentioned I would.</p><p>So, this is a neat feature, but not quite necessary for my needs yet.</p><h3>Step — Picam</h3><p>Now that we can push changes, lets integrate Picam <a href="https://github.com/electricshaman/picam">using a library in Elixir</a>. The changes to our project are:</p><pre># mix.exs</pre><pre>diff --git a/mix.exs b/mix.exs<br>   defp deps(target) do<br>     [<br>       {:nerves_runtime, &quot;~&gt; 0.4&quot;},<br>-      {:nerves_init_gadget, &quot;~&gt; 0.3&quot;}<br>+      {:nerves_init_gadget, &quot;~&gt; 0.3&quot;},<br>+      {:picam, &quot;~&gt; 0.2.0&quot;}<br>     ] ++ system(target)<br>   end</pre><pre># lib/nerves_rpi3_wifi_picam.ex</pre><pre>+  def take_and_read_picture() do<br>+    Picam.Camera.start_link<br>+<br>+    Picam.next_frame<br>+    |&gt; Base.encode64()<br>+    |&gt; IO.puts()<br>+  end</pre><p>My goal here was to make sure Picam was working correctly. So, I wanted to take a picture and see that it worked. Turns out that the best way of doing that is a hack Connor Rigby told me about: We can take a picture with Picam, create a base 64 string from it and and pipe that to IO.puts(). We can access that through ssh, take the string and convert it to an image with <a href="https://codebeautify.org/base64-to-image-converter#">https://codebeautify.org/base64-to-image-converter#</a></p><p>Haha!</p><p>To make this work, do: Push new changes to the device, assert that the device is up, ssh into it, start an IEx process and call the function we created:</p><pre># Here is the flow<br>  ~ :&gt; ssh -p 8989 target01@nerves.local<br>    Eshell V9.3  (abort with ^G)<br>    (target01@nerves.local)1&gt; &#39;Elixir.IEx&#39;:start().<br>    &lt;0.501.0&gt;<br>    Interactive Elixir (1.6.0) - press Ctrl+C to exit (type h() ENTER for help)<br>    iex(target01@nerves.local)1&gt; NervesRpi3WifiPicam.take_and_read_picture()<br>    .<br>    . long string here, copy it to your clipboard<br>    .</pre><p>We now have a picture. Now that our camera works, we will start to work on getting the camera to stream and displaying that on a browser. There is an example on the <a href="https://github.com/electricshaman/picam/tree/master/examples/picam_http">Picam repo</a> that shows exactly what to do. We will integrate those changes to our project. As usual, we will go step by step, starting with integration a web layer to our project.</p><h3>Step — Server layer using Cowboy and Plug</h3><p>We will use <a href="https://github.com/ninenines/cowboy">Cowboy</a> to create an HTTP server and use <a href="https://github.com/elixir-plug/plug">Plug</a> to connect and interact with Cowboy. There was a great article about Plug this last week: <a href="https://www.pompecki.com/post/plugs-demystified/">https://www.pompecki.com/post/plugs-demystified/</a>. To add Plug and Cowboy to our system, do:</p><pre># mix.exs<br>diff --git a/mix.exs b/mix.exs<br>-      {:picam, &quot;~&gt; 0.2.0&quot;}<br>+      {:picam, &quot;~&gt; 0.2.0&quot;},<br>+      {:cowboy, &quot;~&gt; 1.0.0&quot;},<br>+      {:plug, &quot;~&gt; 1.0&quot;}<br>     ] ++ system(target)</pre><pre># edit lib/nerves_rpi3_wifi_picam/application.ex<br>   def children(_target) do<br>     [<br>-      # Starts a worker by calling: NervesRpi3WifiPicam.Worker.start_link(arg)<br>-      # {NervesRpi3WifiPicam.Worker, arg},<br>+      Plug.Adapters.Cowboy.child_spec(scheme: :http, plug: NervesRpi3WifiPicam.Router, options: [port: 4001])<br>     ]<br>   end<br> end</pre><pre># create lib/router.ex</pre><pre>+defmodule NervesRpi3WifiPicam.Router do<br>+  use Plug.Router<br>+<br>+  plug :match<br>+  plug :dispatch<br>+<br>+  get &quot;/&quot; do<br>+    markup = &quot;&quot;&quot;<br>+    &lt;html&gt;<br>+    &lt;head&gt;<br>+      &lt;title&gt;Picam Video Stream&lt;/title&gt;<br>+    &lt;/head&gt;<br>+    &lt;body&gt;<br>+      &lt;p&gt;Hi!&lt;/&gt;<br>+    &lt;/body&gt;<br>+    &lt;/html&gt;<br>+    &quot;&quot;&quot;<br>+    conn<br>+    |&gt; put_resp_header(&quot;Content-Type&quot;, &quot;text/html&quot;)<br>+    |&gt; send_resp(200, markup)<br>+  end<br>+end</pre><p>Deploy the changes to the device, make sure it is up and visit: http://nerves.local:4001/. We should see Hi rendered back to us.</p><h3>Step — Picam streamer</h3><p>Now that we have a working server, let’s finish the exercise by adding the streaming feature with Picam. Again, we go back to the existing example in the Picam repo (see above for link) and create the Streamer exactly as the example has:</p><pre># edit lib/nerves_rpi3_wifi_picam/application.ex<br># to start the Picam process</pre><pre>+    Picam.Camera.start_link<br>     opts = [strategy: :one_for_one, name: NervesRpi3WifiPicam.Supervisor]<br>     Supervisor.start_link(children(@target), opts)<br>   end</pre><pre># edit lib/router.ex<br># to forward requests to the streamer</pre><pre>     |&gt; put_resp_header(&quot;Content-Type&quot;, &quot;text/html&quot;)<br>     |&gt; send_resp(200, markup)<br>   end<br>+<br>+  forward &quot;/video.mjpg&quot;, to: NervesRpi3WifiPicam.Streamer<br> end</pre><pre># create streamer.ex</pre><pre>+defmodule NervesRpi3WifiPicam.Streamer do<br>+  @moduledoc &quot;&quot;&quot;<br>+  Plug for streaming an image<br>+  &quot;&quot;&quot;<br>+  import Plug.Conn<br>+<br>+  @behaviour Plug<br>+  @boundary &quot;w58EW1cEpjzydSCq&quot;<br>+<br>+  def init(opts), do: opts<br>+<br>+  def call(conn, _opts) do<br>+    conn<br>+    |&gt; put_resp_header(&quot;Age&quot;, &quot;0&quot;)<br>+    |&gt; put_resp_header(&quot;Cache-Control&quot;, &quot;no-cache, private&quot;)<br>+    |&gt; put_resp_header(&quot;Pragma&quot;, &quot;no-cache&quot;)<br>+    |&gt; put_resp_header(&quot;Content-Type&quot;, &quot;multipart/x-mixed-replace; boundary=#{@boundary}&quot;)<br>+    |&gt; send_chunked(200)<br>+    |&gt; send_pictures<br>+  end<br>+<br>+  defp send_pictures(conn) do<br>+    send_picture(conn)<br>+    send_pictures(conn)<br>+  end<br>+<br>+  defp send_picture(conn) do<br>+    jpg = Picam.next_frame<br>+    size = byte_size(jpg)<br>+    header = &quot;------#{@boundary}\r\nContent-Type: image/jpeg\r\nContent-length: #{size}\r\n\r\n&quot;<br>+    footer = &quot;\r\n&quot;<br>+    with {:ok, conn} &lt;- chunk(conn, header),<br>+         {:ok, conn} &lt;- chunk(conn, jpg),<br>+         {:ok, conn} &lt;- chunk(conn, footer),<br>+      do: conn<br>+  end<br>+end</pre><pre># edit lib/router.exs<br># to put it all together, display the video<br>   &lt;body&gt;<br>-    &lt;p&gt;Hi!&lt;/&gt;<br>+    &lt;img src=&quot;video.mjpg&quot; /&gt;<br>   &lt;/body&gt;</pre><p>Cool! Deploy… and verify the url! Neat right?</p><p>Here are some pictures:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vzwd55i06dbHmlOgIK1DPQ.jpeg" /><figcaption>The device — Rpi3 + Camera</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*crbANmkKdHvLm6jQe9xDxA.jpeg" /><figcaption>Screenshot of what I saw on my phone</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-XJdHhJNMym1DvMzKafwaQ.jpeg" /><figcaption>Recursion :)</figcaption></figure><h3>General thoughts and thanks:</h3><p>I’m sure there are many other ways of doing this exercise. This is just the way I did it. This was a practical, step by step combination of my notes.</p><p>Here is an example of things I know so far that could be better:</p><ul><li>I don’t have to set ENV variables every time I run a command. Once I had that command, that was all I ran. Ctrl+p in the terminal, was smooth.</li><li>I don’t like where the Picam.Camera.start_link call was made. I&#39;m not sure where to put it. It works where it is at, but there must be a better way.</li></ul><p>Special thanks to some folks from the Nerves community that spent some time helping me these last few days (in no particular order):</p><ul><li>Connor Rigby — @ConnorRigby</li><li>Justin Schneck — @mobileoverlord</li><li>Jeff Smith — @electricshaman</li><li>Tim Mecklem — @tmecklem</li></ul><p>Thanks for reading!</p><p>— PD</p><p>Edit: Thanks [Ryan](<a href="https://github.com/mendokusai">https://github.com/mendokusai</a>) for spotting a typo -&gt; should be streamer.ex` instead of streamer.exs`. Fixed! Thank you!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3b04cee78d22" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>