<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Charles Christensen</title>
    <description>The latest articles on DEV Community by Charles Christensen (@charlesrc019).</description>
    <link>https://dev.to/charlesrc019</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1605164%2F30a3eea4-58ce-4186-aee6-4c5aa18b3790.jpg</url>
      <title>DEV Community: Charles Christensen</title>
      <link>https://dev.to/charlesrc019</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/charlesrc019"/>
    <language>en</language>
    <item>
      <title>IT441: RaspberryPi Stoplight</title>
      <dc:creator>Charles Christensen</dc:creator>
      <pubDate>Thu, 13 Jun 2024 17:19:42 +0000</pubDate>
      <link>https://dev.to/charlesrc019/it441-raspberrypi-stoplight-235h</link>
      <guid>https://dev.to/charlesrc019/it441-raspberrypi-stoplight-235h</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F55riq9y7jjojx1gmb1zk.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F55riq9y7jjojx1gmb1zk.jpeg" alt="Image description" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Overview
&lt;/h1&gt;

&lt;p&gt;The main purpose of this project was to learn the basics of developing software in an IoT environment by creating a wifi-controlled stoplight with a Raspberry Pi. The principles learned in this project are as follows.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setup and configure a Raspberry Pi for initial use.&lt;/li&gt;
&lt;li&gt;Develop non-blocking, multi-threaded code to complete multiple operations at the same time.&lt;/li&gt;
&lt;li&gt;Become familiar with using general purpose input-output (GPIO) pins.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Materials
&lt;/h1&gt;

&lt;p&gt;The physical materials used in this project are as follows.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Personal Computer&lt;/li&gt;
&lt;li&gt;RaspberryPi 3+ (preconfigured to use)&lt;/li&gt;
&lt;li&gt;(1) Breadboard&lt;/li&gt;
&lt;li&gt;(1) Circuit-Printed Stoplight&lt;/li&gt;
&lt;li&gt;(1) RaspberryPi Jumper Cable Bus&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Resources
&lt;/h1&gt;

&lt;p&gt;The software, services, and code libraries used in this project are as follows.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Python 3&lt;/li&gt;
&lt;li&gt;Threading&lt;/li&gt;
&lt;li&gt;Time&lt;/li&gt;
&lt;li&gt;GPIO&lt;/li&gt;
&lt;li&gt;Flask&lt;/li&gt;
&lt;li&gt;UIKit&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  References
&lt;/h1&gt;

&lt;p&gt;The following guides and references were very useful in the completion of this project.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;RaspberryPi.org&lt;/strong&gt; Create a basic website using Python and Flask.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ThePiHut&lt;/strong&gt; Basic interaction with the GPIO pins via Python.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;StackOverflow&lt;/strong&gt; Create a separate thread in a Python script. And, exit a blocked thread in Python.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;State Machine 1&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F107z4vj4vk1cafq0kja9.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F107z4vj4vk1cafq0kja9.jpeg" alt="Image description" width="800" height="616"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;State Machine 2&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0gi9wqim6qsnz7wn14h7.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0gi9wqim6qsnz7wn14h7.jpeg" alt="Image description" width="800" height="615"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Procedures
&lt;/h1&gt;

&lt;p&gt;In the development environment of your choice (for me, this was an Azure VM running Visual Studio Code),&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use the “write, test, modify, repeat” method of development to create a Python-based website to change the states of the stoplight. For simplicity, use a different URL path to specify each state of the stoplight. (I recommend using the Flask library. It made it very easy for me.)&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Optional.&lt;/em&gt; Implement UIKit elements into your website to make it pretty.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Optional.&lt;/em&gt; Find images of stoplight states to use as visual indicators on your website.
Using your website as the input method, implement State Machine 1 in Python code.&lt;/li&gt;
&lt;li&gt;Build upon your new state machine to add an “automatic mode” where the stoplight will run on a timer. (State Machine 2) It is important that all blocking elements of this code run in a separate thread. The main thread should never be blocked as it will be running the website. (I accomplished this by using the time and threading libraries.)
To the best of your ability, add the necessary components to your code to interact with GPIO pins. (I made the pin numbers in my code global variables so that they could easily be changed on the Pi itself.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On your RaspberryPi,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Transfer over your already-created code and verify that it works. (I did this by reinstalling the Flask and Threading libraries to my Pi. Then, I transferred my code with the SFTP protocol.)
Connect your RaspberryPi to the breadboard, using your GPIO ribbon cable and cobbler.&lt;/li&gt;
&lt;li&gt;Add connect your stoplight LEDs to the breadboard, taking note of the pins that the lights are connected to. If you are using individual LEDs, make sure to add a resistor into the circuit before it connects to ground. (In my case, the resistors were built into the stoplight itself, so this was not needed.)&lt;/li&gt;
&lt;li&gt;Modify your code so that the pins specified for red, yellow, and green match those that the are connected on the board. Make sure to take note of whether or not you are using the Broadcom or GPIO specified numbers. By default, the GPIO library will use the GPIO numbers which inconveniently do not match those specified on the board. (In my case, I solved this problem by searching online for a Broadcom schematic of the GPIO pin numbers.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you’ve done everything correctly, you should now have a fully-functioning wifi-controlled stoplight.&lt;/p&gt;

&lt;h1&gt;
  
  
  Appendix
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Thought Questions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What are some practical applications for a device like this?&lt;/strong&gt; Strictly speaking, a device exactly like this might not have a whole lot of practical application since it enables going directly from green to red without any delay. However, many devices somewhat similar to this could be and are used be city administrators to control traffic flow remotely.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What are some enhancements that could be made to this device?&lt;/strong&gt; The primary way to enhance this device would be to add functionality for a cross-street stoplight to work in tandem with the first stoplight. Also, a camera of proximity sensor could be added to this device to enable it to intelligently change from one state to another as needed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How much time did it take to complete this project?&lt;/strong&gt; It took me about three hours to complete this specific project. (In my case, I had additional preparation and post-work that took me more time to complete.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Code
&lt;/h1&gt;

&lt;p&gt;All code for this project can be found in &lt;a href="https://github.com/charlesrc019"&gt;my repo on GitHub&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>IT441: Arduino Stoplight</title>
      <dc:creator>Charles Christensen</dc:creator>
      <pubDate>Thu, 13 Jun 2024 17:03:17 +0000</pubDate>
      <link>https://dev.to/charlesrc019/it441-arduino-stoplight-2e0c</link>
      <guid>https://dev.to/charlesrc019/it441-arduino-stoplight-2e0c</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4mqg2mofhej38iso38gx.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4mqg2mofhej38iso38gx.jpeg" alt="Image description" width="800" height="532"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Overview
&lt;/h1&gt;

&lt;p&gt;The main purpose of this project was to optimize my previous distance stoplight by making network communication more lightweight and by adding power-saving functionality. Additional principles learned in this project are as follows.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implemented an MQTT event hub for publish/subscribe notifications between devices.&lt;/li&gt;
&lt;li&gt;Developed a communications protocol for devices across the event bus.&lt;/li&gt;
&lt;li&gt;Established more complex conditions for the actuator involving multiple sensors.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Materials
&lt;/h1&gt;

&lt;p&gt;The physical materials used in this project are as follows.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Personal Computer&lt;/li&gt;
&lt;li&gt;Personal Cloud Server&lt;/li&gt;
&lt;li&gt;(3) Arduino WEMOS D1 mini&lt;/li&gt;
&lt;li&gt;(3) Breadboard&lt;/li&gt;
&lt;li&gt;(1) LED Stoplight&lt;/li&gt;
&lt;li&gt;(1) Ultrasonic Distance Sensor (HC-SR04)&lt;/li&gt;
&lt;li&gt;(1) Magnetic Door Switch Sensor&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Resources
&lt;/h1&gt;

&lt;p&gt;The software, services, and code libraries used in this project are as follows.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arduino IDE&lt;/li&gt;
&lt;li&gt;C Libraries&lt;/li&gt;
&lt;li&gt;ESP8266WiFi&lt;/li&gt;
&lt;li&gt;WiFiManager&lt;/li&gt;
&lt;li&gt;PubSubClient&lt;/li&gt;
&lt;li&gt;NewPing&lt;/li&gt;
&lt;li&gt;Mosquitto MQTT Broker&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  References
&lt;/h1&gt;

&lt;p&gt;The following guides and references were very useful in the completion of this project.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bald Engineer. Detailed instructions on writing better MQTT callback functions.&lt;/li&gt;
&lt;li&gt;Instructables. Great explanation of using magnetic door sensors with Arduino. And nice overview of how to use the PubSub MQTT library with WifiManager on Arduino.&lt;/li&gt;
&lt;li&gt;Randon Nerd Tutorials. Troubleshooting guide for testing MQTT communications from the broker itself.&lt;/li&gt;
&lt;li&gt;Steve’s Internet Guide. Simple explanation of how to set up a MQTT server in the cloud.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Procedure
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Part 1: Setup a MQTT Broker
&lt;/h2&gt;

&lt;p&gt;My last project was a success, but using HTTP to communicate between devices was ridiculously slow. So, I used MQTT for this project. The main benefit of doing this was that it offloaded the complexity of managing the network connections to a more adept/powerful device.&lt;/p&gt;

&lt;p&gt;Find a computer that you can use as your MQTT broker. The broker is basically the gateway for all your MQTT communications, so it will need to 1) have a semi-permanent network address and 2) be reachable by all the IoT devices that you might want to connect. In my project, I used the Moqsuitto MQTT broker, which also requires 3) a multi-threaded operating system.&lt;/p&gt;

&lt;p&gt;Install your MQTT broker and integrate into your IoT devices. The exact “hows” of doing this depend a lot on the computer that you chose in step 1. In my case, I used a virtual Linux server in Microsoft Azure. The install process was fairly simple; it involved running a couple of commands and opening port 1883 to the public.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa&lt;br&gt;
sudo apt-get update&lt;br&gt;
sudo apt-get install mosquitto&lt;br&gt;
sudo apt-get install mosquitto-clients&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Installing MQTT on Arduino. You need to take note of the network address with which your broker can be reached. The easiest way to do this is by creating a public DNS record that will point to your broker, regardless of where it is. (In my case, this was easy because Azure provides a customizable public DNS record.) If that’s not feasible for you, try setting an assigned/static IP that will be easy for you to remember.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 2: Create a Door Sensor
&lt;/h2&gt;

&lt;p&gt;Here’s another improvement on my last project. If my distance stoplight was actually used in a garage, it wouldn’t need to be doing anything most of the time; it would only be needed for a few minutes while someone is parking their car. So, in an attempt to save power, this project also includes a door sensor that will put the other devices to sleep when it reports that the garage door has been closed.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Wire the door sensor to interface with Arduino. Most door sensors are very simple; they take voltage from one end and allow/don’t allow it to pass to the other end. Therefore, one wire from the sensor needs to be connected to a known, constant signal and other end to an input pin. (In my case, I used D8/GPIO15 and the 3.3 voltage pin, so as to avoid complications from using ground and needing a pull-down resistor.)&lt;/li&gt;
&lt;li&gt;Code the door sensor functionality. This device is relatively simple to use, but for the inter-device communication you’ll need to add quite a few more components. Functionality-wise, this is the order of how you’ll want to stand-up the various functionalities in your code.&lt;/li&gt;
&lt;li&gt;Serial console connectivity. Absolutely essential for development troubleshooting. The commands are Serial.begin(FREQ) to set it up and Serial.println("text") to print text to the console.
Door sensor functionality. Before you get into all the network connection mumbo-jumbo, get the main functionality of this device figured out. Instructables has a great guide on using door sensors with Arduino.&lt;/li&gt;
&lt;li&gt;WiFi connectivity. A Google search will find you tons of guides on how to do this. If you want, you can also reference my guide on doing this for the WEMOS D1 mini as part of my simple stoplight project.&lt;/li&gt;
&lt;li&gt;MQTT connectivity. Again, this is very common and can readily be found online; Instructables has a great explanation of how to do this.. Basically, you’ll need to have two basic functions for this specific device: a connection/reconnection function that will handle connecting to your broker (Remember that semi-permanent network address? You’ll need to use that here.) and a publish function that will transmit changes in the door sensor’s state.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Part 3: Create a Distance Sensor
&lt;/h2&gt;

&lt;p&gt;Wire the ultrasonic distance sensor to interface with Arduino. If you need more information on this, take a quick peek at how I did it in my distance stoplight project. Remember, the sensor performs best when it is connected to 5 volts, instead of 3.3 volts.&lt;br&gt;
Code the distance sensor functionality. This will also be a lot like the code in my distance stoplight project. Since this project uses MQTT, the distance sensor device will need to handle the state changes between distance ranges itself, instead of providing distance information upon request from other devices. In general, this is how you’ll want to build/rebuild your distance sensor functionality.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Serial console connectivity.&lt;/li&gt;
&lt;li&gt;Distance sensor functionality. As mentioned above, this functionality not only needs to read distance information from the sensor, but also move the sensor between the different distance distance range states to indicate green, yellow, red, or too close.&lt;/li&gt;
&lt;li&gt;WiFi connectivity.&lt;/li&gt;
&lt;li&gt;MQTT connectivity. On this device, you’ll need three functions for MQTT communications: a connection/reconnection function, a state publish function, and a callback/”message received” function to change the sensor sleep state based on information from the door sensor. Bald Engineer has a great tutorial on how to use and respond to MQTT callbacks.
Power-saving functionality. Basically, all that needs to be done here is to build a option in your infinite loop that will stop the ultrasonic sensor from sampling distances until the door sensor reports that it is open again.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Part 4: Create the Stoplight
&lt;/h2&gt;

&lt;p&gt;Wire the stoplight to interface with Arduino. The best way to do this is probably to wire each specific light and ground. (In my case, I took the lazy route and connected it in a spot on the board where the the pins were already lined up in the order I needed. The only caveat to this was that changing the stoplight’s colors also changed built-in LED’s state.)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Code the stoplight functionality. Here’s a overview of what you’ll need to do to get this functionality up and running. (In my case, I just trimmed down my stoplight code from previous projects since I off-loaded a lot of the functions to MQTT and the distance sensor itself.)&lt;/li&gt;
&lt;li&gt;Serial console connectivity.&lt;/li&gt;
&lt;li&gt;WiFi connectivity.&lt;/li&gt;
&lt;li&gt;MQTT connectivity. You’ll only need two functions for MQTT communications: a connection/reconnection function and a callback function to change the stoplight state and the sleep state.
Stoplight functionality. Unlike the previous two devices, the stoplight’s functionality can only come after implementing MQTT because it relies on the state information that it gets there in order to change the lights.
Power-saving functionality.&lt;/li&gt;
&lt;/ol&gt;
&lt;h1&gt;
  
  
  Appendix
&lt;/h1&gt;
&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;I installed Mosquitto, but none of my devices are able to connect to it.&lt;/strong&gt;
First, verify that Mosquitto is running. On Linux, the following commands will report the status of the Mosquitto broker and enable it to automatically run on system startup, if wanted. Next, test the network connection to your broker; try pinging it. (You may need to open your firewall to ICMP requests in order for this to work.) If neither of those suggestions finds the problem, it is most likely in your code. Time to pull out your bug-finding glasses!
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;sudo service mosquitto status&lt;br&gt;
sudo systemctl enable mosquitto.service&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The code inside my infinite loop function is getting ridiculously complicated. Help!&lt;/strong&gt;
One of the main differences between programming for Arduino and other types of coding is that the code we are writing is essentially for a single-function state machine. Therefore, the common advice about not using global variables goes out the window; global variables are your states! To avoid complicated code, define all the states that you’ll need to keep track of as global variables. Then, write seperate functions that will update your global variables or respond to updates. Put as little in you infinite loop function as possible, and instead call your functions as needed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Thought Questions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;How does the communication between devices change with the shift to using an MQTT event hub? How does this facilitate greater scalability?&lt;/strong&gt; Using an MQTT event hub has a huge impact on the overall scalability of IoT projects. As mentioned before, using MQTT offloads all of the details of device communication to a single computer. Each individual device no longer needs to keep track of other IP addresses, serialize/deserialize data, or implement network timeout conditions; the MQTT broker handles all of that and does its best to guarantee that messages are delivered. Therefore, it is much easier to scale projects because each device’s functionality doesn’t need to change or improve. Communication limits can be reduced simply by modifying the broker.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What things did you do to learn to use MQTT?&lt;/strong&gt; The most useful thing to me in learning MQTT was a live demonstration I saw. In the demonstration, several Arduinos turned on/off their LEDs based on the messages sent to them from the command line of the MQTT topic publisher. Having no previous MQTT experience, just simply watching the MQTT traffic and the responses to it was an excellent way for me to learn how to incorporate MQTT into IoT devices.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What are strengths and weaknesses of the direct communication between the sensor and actuator? What are strengths and weaknesses of the event hub? Which do you feel is better for this application?&lt;/strong&gt; The strengths and weaknesses of MQTT versus direct communication can be summarized in the pros/cons of star versus mesh networks. MQTT event hubs are good because they reduce complexity and allow for a single point of upgrade, but on the downside they require an extra device and inherently have an single point of failure. Direct communication, however, is more complex but doesn’t have a single point of failure or require extra devices. (In my opinion, choosing the best option for an application like this rides on whether or not there are outside devices that I also want to hook into my MQTT broker. If there are, building/maintaining an MQTT broker would probably be worth it. Otherwise, it would be way to much overhead for this very simple task.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What was the biggest challenge you overcame in this project?&lt;/strong&gt; Completing this project required me to really dive down and figure out how to use char array like I never have before. Due to my C++ background, I am used to an easier way of managing strings, but the data provided by the MQTT functions was all char arrays. Thanks to MQTT tutorials mentioned above, I eventually got it figured out with the strcmp() and String.indexOf() functions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How much time did it take you to complete this project?&lt;/strong&gt; I was able to adapt a fair amount of code from my previous projects for use in this project, so it only took me about 4 hours to complete. After that, I spent an additional 4 hours writing this report.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Code
&lt;/h1&gt;

&lt;p&gt;All code for this project can be found in &lt;a href="https://github.com/charlesrc019"&gt;my repo on GitHub&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>IT441: StallSense</title>
      <dc:creator>Charles Christensen</dc:creator>
      <pubDate>Thu, 13 Jun 2024 16:17:30 +0000</pubDate>
      <link>https://dev.to/charlesrc019/it441-stallsense-50fm</link>
      <guid>https://dev.to/charlesrc019/it441-stallsense-50fm</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqtbmwitbrsdf4k0yz0m8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqtbmwitbrsdf4k0yz0m8.jpg" alt="Image description" width="792" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Overview
&lt;/h1&gt;

&lt;p&gt;Have you ever got up from your desk and walked over to the restroom, only to find that all of the stalls are full? Then you walked to the other restrooms in the building, only to find that all of them are full too? Enter StallSense, the bathroom occupancy detector to help you know to go.&lt;/p&gt;

&lt;h1&gt;
  
  
  Materials
&lt;/h1&gt;

&lt;p&gt;The physical materials used in this project are as follows.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Personal Computer&lt;/li&gt;
&lt;li&gt;Personal Cloud Server&lt;/li&gt;
&lt;li&gt;(1/sensor) Arduino WEMOS D1 mini&lt;/li&gt;
&lt;li&gt;(1/sensor) Breadboard&lt;/li&gt;
&lt;li&gt;(1/sensor) LED Stoplight&lt;/li&gt;
&lt;li&gt;(1/sensor) Adafruit Grideye Thermal Sensor&lt;/li&gt;
&lt;li&gt;(1/sensor) Magnetic Door Switch Sensor&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Resources
&lt;/h1&gt;

&lt;p&gt;The software, services, and code libraries used in this project are as follows.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arduino IDE&lt;/li&gt;
&lt;li&gt;C Libraries&lt;/li&gt;
&lt;li&gt;ESP8266WiFi&lt;/li&gt;
&lt;li&gt;WiFiManager&lt;/li&gt;
&lt;li&gt;PubSubClient&lt;/li&gt;
&lt;li&gt;ArduinoJson&lt;/li&gt;
&lt;li&gt;SPIFFS&lt;/li&gt;
&lt;li&gt;AdafruitAMG&lt;/li&gt;
&lt;li&gt;Mosquitto MQTT Broker&lt;/li&gt;
&lt;li&gt;Node.js&lt;/li&gt;
&lt;li&gt;Node.js Libraries&lt;/li&gt;
&lt;li&gt;Express&lt;/li&gt;
&lt;li&gt;Morgan&lt;/li&gt;
&lt;li&gt;MQTT&lt;/li&gt;
&lt;li&gt;Sequelize&lt;/li&gt;
&lt;li&gt;Socket.io&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  References
&lt;/h1&gt;

&lt;p&gt;The following guides and references were very useful in the completion of this project.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SteveFi. Simple reference to the GPIO pin layout on the WEMOS D1 mini.&lt;/li&gt;
&lt;li&gt;GitHub. The published Adafruit AMG library, used for collecting data from the thermal camera.&lt;/li&gt;
&lt;li&gt;Adafruit Learning. Demonstration of collecting and using data from the Adafruit thermal camera.&lt;/li&gt;
&lt;li&gt;Random Nerd Tutorials. Nice walk-through using custom parameters with the WifiManager library.&lt;/li&gt;
&lt;li&gt;Circuits4You. Explaination of how to reset an ESP8266 chip programatically.&lt;/li&gt;
&lt;li&gt;Mahesh Bhanushali. Refresher course on creating a Node.js website.&lt;/li&gt;
&lt;li&gt;Ramesh Prajapati. Help connecting Node.js to MySQL database.&lt;/li&gt;
&lt;li&gt;Rising Stack. Easy to understand explaination of using the MQTT library in Node.js.&lt;/li&gt;
&lt;li&gt;Rip Tutorial. Tutorial for running websockets in Node.js.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Procedure
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Part 1: Create Management Server
&lt;/h2&gt;

&lt;p&gt;While the individual sensors themselves are an important part of this project, the status server is what will really make the sensors most useful. I personally did not start by creating the server, and I wish I would have. It is the crux of the whole project.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Find a computer that you can use to run your Node.js server. You’ll need a system that can run Node.js, which is actually quite a lightweight service, and that is in a network location where the devices themselves can access it. Outside of that, feel free to use any setup you have at your disposal.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install Node.js. Install Node.js and Node Package Manager.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Initialize your StallSense server. I am not a constant user of Node.js, so I always forget how to do this. Mahesh Bhanushali has one of the best walkthroughs that I’ve found on getting up and running with your own Node.js webserver. He specifically uses Express Generator, EJS, and Bootstrap in his tutorial. That is what I did as well, but feel free to use your own setup if your are more comfortable with that.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create and implement the visual design of your website. I went with a simple table layout for my StallSense web interface, but you could do something fancier if you want. If you want to do something more complex, I’d suggest sketching up the layout of your website before trying to implement it in HTML/CSS code. Then, edit the index.ejs view file to reflect what you want your design to be. If you followed step 3 above, you'll have access to Bootstrap styling which makes implementing your design much easier. (Below is a screenshot of my website, for your reference.)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Part 2: Build a Thermal Sensor
&lt;/h2&gt;

&lt;p&gt;Wire/soder your thermal sensor to your Arduino. For this project, I used the Adafruit Grideye 8x8 thermal camera. It initially came without header pins sodered on, but since I was planning on using it for other IoT projects outside of this, I decided to add them for ease of use. Once the headers were attached, I put both the thermal camera and my Arduino on a breadboard, and then I ran wires between them as if they were placed on top of each other. I also added a stoplight to my contraption so that I could have visual feedback of the thermal state.&lt;/p&gt;

&lt;h2&gt;
  
  
  Part 3: Build a Door Sensor
&lt;/h2&gt;

&lt;p&gt;Wire your door sensor to your Arduino. Door sensors are a lot more common to come by, so I actually don’t know what kind of door sensor I used. You should be able to find a generic one on your own though. The door sensor needs to be connected on one end to a power pin and on the other to an input pin. I also added a stoplight to this Arduino for immediate feedback.&lt;/p&gt;

&lt;h1&gt;
  
  
  Appendix
&lt;/h1&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;I really don’t want to use Node.js. What are the alternatives?&lt;/strong&gt; I definately agree. Node.js can be a headache sometimes, espically if you don’t use it a lot. I chose Node.js because it I liked the fact that it has tons of feature extensions already developed and because it is web-centered. However, any multi-function language that has web, MQTT, and MySQL support should be able to do the job. Python and C# are the first alternative languages that come to my mind, amid others.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Thought Questions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;How well does the final product actually perform? It is worth it?&lt;/strong&gt; In the end, the stall sensors were a success! Definately worth it. The low-down is this: (1) The StallSense server added a lot to the functionality. Honestly, if all I had was one sensor, it still would have been worth it to me to write all that server code. (2) The thermal sensor needs more resolution. It worked just fine in my test bathroom, which was only a foot tall, until my roommate suck his hand underneath it and it didn’t go off. I don’t think that it would work in a normal-sized bathroom, espically with all the thick, heat-capturing clothing that people wear in the winter. (3) The door sensors are the way to go. If you want the most reliable and least expensive option, go with the door sensors. They are accurate, much easier to develop, and are less of a privacy concern.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What improvements would you like to make to StallSense?&lt;/strong&gt; Two main ideas here. One, I want to build commercial-type case for my sensors, just to see what that would be like. Would it be possible to make this contraption discreet enough that it wouldn’t look out-of-place in an actual bathroom? Second, it would be really cool to tear apart a toilet motion sensor (you know, the kind that automatically flush the toliet) and see if I could leverage it to also report to the stall occupancy status. If this idea were ever sold commercially, I think that having it embedded in a motion sensor would be the way to go.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How much time did it take to complete this project?&lt;/strong&gt; Unlike many of my previous projects, this project was almost completely from scratch. Of course, I did not have to write the WifiManger, AdafruitAMG, or Node Express libraries myself, but I did do the integrations between them myself. It took me about 8 hours to setup and program the thermal and door sensors. Then, it took an additional 8 hours to build the StallSense server. In total, about 16 hours of work time and an additional 4 hours to write this tutorial.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Code
&lt;/h1&gt;

&lt;p&gt;All code for this project can be found in &lt;a href="https://github.com/charlesrc019"&gt;my repo on GitHub&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
