---
# System prepended metadata

title: DistanceGlow

---

# Tutorial on how to build a distanceGlow
by Hampus Hall hh223ma

My IoT project for the summer course of 2024 aims to bridge the gap between me and my distant girlfriend while I'm on my exchange semester in Australia. In this tutorial, you can follow the steps I took to finalize this project.

## 🎯 Objective
Since I'm going abroad to Australia, where there's a 9+ hour time difference, I want to develop a system to enhance communication and connection between me and my partner, given we'll be in different time zones. This system will include LED indicators that switch between green and red based on the time in Australia: green for daytime and red for nighttime. Additionally, the devices will send notifications via Discord. These messages will include the current time in Australia, the number of days until I arrive home, as well as indoor temperature and humidity. We can further use Discord for communication during daytime hours.

## ✅ Cheklist to get it done
:::info
- [ ] Gather material
- [ ] Install software
- [ ] Set up Pico
- [ ] Make Circut
- [ ] Code it
- [ ] Connect it
- [ ] Visualize it
:::
By following these steps the project will take an estimate of 3 - 6 hours depending on previous skills.

## 📦 Materials
For this project, I utilized components from the starter kit provided by Electrokit. Additionally, a pulse sensor was purchased with the intention for my girlfriend to send her pulse alongside a personalized message. However, this pulse sensor was later found unreliable, resulting in its exclusion from the project.

All components were purchased from electrokit.se (except for the adapter).

| Image | Component            | Description | Cost   |
| ----- | -------------------- | ----------- | ------ |
|![10160840](https://hackmd.io/_uploads/BJVWXDt8R.jpg)|Breadboard|Allows me to connect all components in an easy way      |69kr    |
|![DHT11](https://hackmd.io/_uploads/H1bEQPFUR.jpg)|DHT11|Provides a serial data stream that can be read using a digital input on a microcontroller.             | 49kr       | 
|![40300051](https://hackmd.io/_uploads/SJ5rmwFUR.jpg)|Red/Green LED         |Indicates daytime or nighttime status             |5kr        |
|![KW-100_0-1532x1022h](https://hackmd.io/_uploads/ByJmSvK80.jpg)|Micro-USB Cable       |Power cable for Raspberry Pi Pico WH             |39kr        |
|![unisynk-3-portars-usb-c-hubb-v2-2](https://hackmd.io/_uploads/B1J37PFLR.jpg)|Adapter               |Used to connect power cable USB-A to mac USB-C|249kr                             |             |        |                      |             |        |
|![700x700-product_41019_41019114_PICO-WH-HERO](https://hackmd.io/_uploads/H1qyNvt8C.jpg)| Raspberry Pi Pico WH | Microcontroller for data processing        | 109kr |
![700x700-product_41012_41012684_41012684](https://hackmd.io/_uploads/B1jeVvtLA.jpg)| Jumper Wires|Used for making connections between different parts of the electronic circuit|49kr|
![1](https://hackmd.io/_uploads/rysW4DKLA.png)|Resistor 330ohm|Used to limit the current flowing through LED's|1kr|

## 💻 Computer setup

**Mac OS Sonoma 14.2.1**
The operating system for my macbook air 2020 M1
**Visual Studio Code** 
A code editor used to edit code. It supports a variety of valuable extensions, such as PyMakr, which simplifies the setup and configuration of the IoT device
**Node.JS**
Node.js is a runtime environment that allows you to execute JavaScript code outside of a web browser. It's particularly valuable in IoT projects due to its efficiency in handling asynchronous operations, which are common in IoT applications that involve sensor data, real-time communication, and network interactions.
**Pymakr**
Pymakr offers software tools and libraries that helps me connect and set up the IoT device. It makes deploying IoT applications more accessible and efficient.
**Adafruit**
Adafruit IO is a cloud-based service. It allows me to connect my IoT device to the internet and manage data streams easily. It also helps me with visualsing the data via their user-friendly dashboard. 
**API's**
 - TimeAPI
 Used to retrive the current Melbourne time. This was used in order for the IoT to always retrive the correct time.
 - SunriseSunsetAPI
 Used to retrive sunrise and sunset time for Melbourne
 
## 🏁 Getting started
Before building the prototype we need to set up the enviroment for our IoT project. 
### ☇ 1. Install Node.JS
I already had Noe.JS installed on my computer but if you dont have it theres a few different step you can take when using Mac:

#### 🕸️ Option 1
**Download the Installer from web**: Visit the [Node.js download page](https://https://nodejs.org/en/download/package-manager).
Click on the macOS Installer (.pkg) to download the LTS version or the Current version.

**Run the Installer**: Open the downloaded .pkg file and run the installer.
Follow the prompts in the Node.js Installer.

#### 🍺 Option 2
If you have homebrew installed on your mac, run: "brew install node" to install nod

### 🆚 2. Install Visual Studio Code and PyMakr
In order to uload and run code on your Rapsberry Pi Pico WH we need a code editor to write and upload code. I decided to go with VS Code since iv'e used it before and it feels familiar. VS Code can be installed by visiting [their website](https://https://code.visualstudio.com/download) and follow the installation instructions

Once installed press cmd+Shift+X to open up the extension tab. Search for PyMakr and install. The PyMakr icon should now be visable in the tab on the left. 

### 3.⚡️ Flash MicroPython onto the Raspberry Pi Pico W
We now need to prepare our Raspberry Pi Pico WH. 
- Download the latest version of micropython
- While pressing the BOOTSEL button, plug your Pico WH.
- Your Pico WH is now set up with MicroPython

### 📁 4. Set up file system
- Create a folder on your computer and open the folder in VS Code. Inside this folder create another folder called "lib". This is the folder where you can save code from different modules or sensors.
- Create a file called main.py and one called boot.py

### 🛜 5. Connect to WiFi
Connecting the Raspberry Pi Pico WH to wifi is a crucial step to ensure the device can access the API's to retrive data, but also to upload data to Adafruit IO. 

These are the steps to set up WiFi-connection:

Create a file called secrets.py in this file you put the following:

> WIFI_SSID = 'Your_SSID'
> WIFI_PASS = 'Your_Password'

Replace 'Your_SSID' with the name of your home wifi and 'Your_Password' with your WiFi password.

In your boot.py file, paste the [followig code](https://https://hackmd.io/@lnu-iot/rJVQizwUh)

:::success
You should now be connected to your home WiFi
:::


### 📊 6. Visualize in Adafruit IO and Webhooks
By setting up visualization and event-triggering in Adafruit IO we can do utalize the following
- **Dashboard** - Visualize the data in an user friendly dashboard
- **Feeds** - Send data and store them in feeds
- **Actions** - Trigger action based on the data that gets sent and send webhooks to a discordserver

:::info

[Follow this tutorial to set up Adafruit IO](https://https://hackmd.io/@lnu-iot/r1yEtcs55)
:::

## 🔌 Putting everyting togheter
![Circut](https://hackmd.io/_uploads/B1ZbnzhLA.jpg)

In the above picture you can se a circut example.
- Black Wires represents ground
- Red Wires represent power 3,3V
- White Wires reprsent signal


## Platform
I decided to go with Adafruit IO for visual representation, primarily because of its user-friendly interface and the convenience of setting things up. They offer both free and paid versions; for this project, the free version was sufficient. I did experiment with Ubidots, but after getting Adafruit IO to work quickly, I decided to continue with it for the project.

## 🧑🏼‍💻 The Code, Files & File Structure

#### 📁 File structure
I wanted the file-structure to be clean and easy to understand. The goal was to make the [main.py](https://github.com/hampushall/IoTProject/blob/651d83a018b4dc5525e5e086d5e52f52a2ee0fc3/main.py) easy to understand. Press the file to follow the link to each file in the Github.

:::    info
Press file names to view code in github
:::

IoTProject
|-- lib/
| |---- [melbourne_time.py](https://github.com/hampushall/IoTProject/blob/651d83a018b4dc5525e5e086d5e52f52a2ee0fc3/lib/melbourne_time.py)
│ |---- [mqtt.py](https://github.com/hampushall/IoTProject/blob/651d83a018b4dc5525e5e086d5e52f52a2ee0fc3/lib/mqtt.py)
│ |---- [Sunrise_sunset.py](https://github.com/hampushall/IoTProject/blob/651d83a018b4dc5525e5e086d5e52f52a2ee0fc3/lib/sunrise_sunset.py)
│ |---- [temperature_sensor.py](https://github.com/hampushall/IoTProject/blob/651d83a018b4dc5525e5e086d5e52f52a2ee0fc3/lib/temperature_sensor.py)
|-- [main.py](https://github.com/hampushall/IoTProject/blob/651d83a018b4dc5525e5e086d5e52f52a2ee0fc3/main.py)
|-- [boot.py](https://github.com/hampushall/IoTProject/blob/651d83a018b4dc5525e5e086d5e52f52a2ee0fc3/boot.py)
|-- secrets.py


**[main.py](https://github.com/hampushall/IoTProject/blob/651d83a018b4dc5525e5e086d5e52f52a2ee0fc3/main.py)**: Main program that sets up an IoT device that connects to the Adafruit IO platform to publish various sensor data (such as temperature, humidity, current time, and days until a target date) and control LEDs based on sunrise and sunset times
**secrets.py**: Store sensitive information like wifi-password.
**lib/**: Library directory containing specialized modules
**[melbourne_time.py](https://github.com/hampushall/IoTProject/blob/651d83a018b4dc5525e5e086d5e52f52a2ee0fc3/lib/melbourne_time.py)**: Manages time functions related to Melbourne via API call.
**[mqtt.py](https://github.com/hampushall/IoTProject/blob/651d83a018b4dc5525e5e086d5e52f52a2ee0fc3/lib/mqtt.py)**: Handles MQTT communication.
**[Sunrise_sunset.py](https://github.com/hampushall/IoTProject/blob/651d83a018b4dc5525e5e086d5e52f52a2ee0fc3/lib/sunrise_sunset.py)**: Retrieves sunrise and sunset times via an API call.
**[temperature_sensor.py](https://github.com/hampushall/IoTProject/blob/651d83a018b4dc5525e5e086d5e52f52a2ee0fc3/lib/temperature_sensor.py)**: Sets up the DHT11 sensor.


## 📬 Transmitting the data / connectivity
The data is transmitted to the internet using WiFi and MQTT (Message Queuing Telemetry Transport) protocols.

**Data Collection and Preparation**
The data from my sensor and device is collected and prepared for transmission.

**WiFi Connectivity**
My device connects to my home WiFi, chosen for its simplicity and widespread availability, making it ideal for real-time data transmission.

**MQTT Protocol**
MQTT is a lightweight messaging protocol designed for constrained devices and low-bandwidth, high-latency networks. The data payload is packaged into an MQTT message, which the device then publishes to an MQTT broker. In this case, Adafruit IO is used as the MQTT broker to manage and route messages.

**Transmission to Adafruit IO Cloud**
The MQTT message is transmitted over the WiFi network to Adafruit IO Cloud. Adafruit IO provides a platform for data collection, storage, and visualization. Authentication and connection to Adafruit IO are handled through unique keys and feed names provided by the Adafruit account.

**Adafruit IO to Discord via Webhooks**
A webhook is an HTTP callback that sends a request to a specified URL. When new data arrives, Adafruit IO sends an HTTP POST request to a Discord webhook URL, containing the data payload. The Discord webhook processes the request and posts the data to my Discord channel.

**Time**
The device is set up to send data every 30 minutes. This interval was chosen to balance the need for timely updates with considerations for data storage requirements and bandwidth usage.

## Presenting the data
![Screenshot 2024-06-30 at 13.58.55](https://hackmd.io/_uploads/S1S9O6AUA.png)

You can send up to 30 data points per minute. The data is then saved to Adafruit IO as soon as it is received from the device (in case of the current project every 30 minutes). Adafruit IO stores the data for 30 days.

## Finalizing the design
Using the delivery box from Electrokit, I made two holes for the LEDs on the outside. The LEDs were glued to the box, and the front of the box was painted pink. Now, it fits perfectly in our bookshelf.

![IMG_8448](https://hackmd.io/_uploads/B1ZRQRRUR.jpg)
![IMG_8449](https://hackmd.io/_uploads/ByWAQRRL0.jpg)
![Screenshot 2024-06-30 at 14.50.22](https://hackmd.io/_uploads/S1goNCR8R.png)



## Final toughts
This has been a fun project to work on over the past few weeks. I think the course setup has been superb, with great content that is easy to follow and up-to-date. The TAs have been very kind and helpful, and there have been plenty of workshops to attend.

Some things I struggled with:
Using an adapter to connect the Raspberry Pi Pico WH to my Mac has sometimes been challenging. Occasionally, it takes a long time to connect, and it freezes, requiring me to unplug and try again. I've had problems with uploading and running the code; the Pico sometimes keeps running the old code even though I uploaded the new one. Additionally, I encountered errors with the same code that worked the previous day. Another struggle was getting the Pico's RTC to work. I needed to utilize the clock to count the days until I travel home, but we couldn't get it to function correctly even with the TAs' help.

As mentioned before, I tried to connect a pulse sensor from Joy-IT to my device, but I couldn't get it to work due to unstable readings. After consulting with the TAs, I decided not to use the sensor.

Overall, this has been a fun course, different from many of the other courses I have taken. It was also enjoyable that you could choose to spend more money and build more complex projects if you wanted.

Hampus Hall
