# The smart compost
**Angelica Marker** (am225cs)
I've created a compost monitor using a Raspberry Pi Pico W and various sensors. The goal was to track and monitor the conditions of my compost heap to ensure it decomposes efficiently.
**Keywords**: *Raspbery Pi Pico, PyMakr, VsCode, AWS IoT, Lambda functions, DynamoDB, Firebase Hosting, React, Open AI API, OpenWeather API*
⏳ **Estimated time to build with this tutorial:** 1-2 days, depending on you prior experience with coding.
**Links to my repositories**:
- [Frontend](https://github.com/MarkerAnn/smart_compost_frontend.git)
- [PICO](https://github.com/MarkerAnn/Smart-Compost.git)
## Objective
I chose to build a compost monitor because it combines several interesting aspects of IoT technology with a practical, real-world application. Composting is an environmentally friendly way to recycle organic waste, and monitoring the composting process can enhance its efficiency and effectiveness. This project allows me to work with various sensors and learn how to integrate them into a cohesive system, providing a hands-on experience with both hardware and software components.
The primary purpose of this compost monitor is to track the key parameters that influence the composting process, such as temperature, moisture, gas levels, and decomposition progress. By gathering data on these parameters, the device helps in maintaining optimal conditions for composting, ensuring that the process is efficient and that the compost produced is of high quality.
By analyzing the data collected from the compost monitor, I hope to gain new insights into the composting process. Specifically, I aim to understand:
**Optimal Conditions**: Identifying the ideal temperature and moisture levels for different stages of composting.
**Decomposition Rates**: Understanding how various factors, like temperature and moisture, affect the rate at which organic matter breaks down.
**Gas Emissions**: Measuring carbon dioxide levels to ensure they are within safe limits and to potentially reduce greenhouse gas output from the composting process..
**Efficiency Improvements**: Using the data to optimize the composting process, reducing the time needed to produce high-quality compost.
These insights can help improve composting practices, making them more efficient and environmentally friendly. Additionally, this project serves as a valuable educational experience, teaching me about sensor integration, data analysis, and IoT applications in sustainability.
## Material
#### Micro Controller
- Rasperry Pi Pico W
- [Link to electro:kit](https://www.electrokit.com/raspberry-pi-pico?gad_source=1&gclid=CjwKCAjw-O6zBhASEiwAOHeGxZdFNycyq3Xcm8-w-W00emxv5sj6XtywITYSrNfze3FQxmExAu8DpxoC6E0QAvD_BwE)
- Price:59 SEK
- Purpose: The brain of the entire operation🧠
- 
#### Sensors
- Jevina SCD40
- [Amazon Link](https://www.amazon.se/Jevina-gasssensormodul-detekterar-koldioxidtemperatur-fuktighet/dp/B0CPHX4DSG?source=ps-sl-shoppingads-lpcontext&ref_=fplfs&psc=1&smid=AF9VDGNF0P7CX)
- Price: 311 SEK
- Purpose: Measure the carbon dioxid in the barrel (CO2), the temperature in the barrel and the humidity.
- 
- JHD162A
- [Link to electro:kit](https://www.electrokit.com/lcd-2x16-tecken-jhd162a-stn-bla/vit-led)
- Price: 99 SEK
- Purpose: LCD, 2x16 characters. To display the values inside the compost
- - 
- I2C interface for LCD
- [Link to electro:kit](https://www.electrokit.com/i2c-interface-for-lcd)
- Price: 42 SEK
- Purpose: The I2C interface significantly reduces the number of connections needed between the Raspberry Pi Pico W and the LCD screen. Instead of requiring multiple data pins, the I2C interface uses only two wires (SDA and SCL), making the setup cleaner and easier to manage.
- 
- Adafruit STEMMA Soil Sensor - I2C Capacitive Moisture Sensor
- [Link to electro:kit](https://www.electrokit.com/jordfuktighetssensor-kapacitiv-i2c)
- Price: 115 SEK
- Purpose: Measures the moisture in the compost heap
- 
- Kabel med JST-PH 2.0mm 4-pol hona / 0.64mm hylsor 200mm
- [Link to electro:kit](https://www.electrokit.com/kabel-med-jst-ph-4-pol-hona-/-0.64mm-hylsor-200mm)
- Price: 24 SEK
- Purpose: Connects to the Adafruit STEMMA soil sensor

- DS18B20
- [Link to electro:kit](https://www.electrokit.com/temperatursensor-vattentat-metall-ds18b20)
- Price: 99 SEK
- Purpose: Waterproof temperature sensors. Measures the temperature in the heap.
- 
- HC-SR04
- [Link to electro:kit](https://www.electrokit.com/avstandsmatare-ultraljud-hc-sr04-2-400cm)
- Price: 59 SEK
- Purpose: Measure distance through ultrasound. Will notice when and if the heap is sinking
- 
#### Other
- Breadboard
- [Link to electro:kit](https://www.electrokit.com/kopplingsdack-840-anslutningar)
- Price: 69 SEK
- Purpose: It allows for easy insertion and removal of components
- 
- Jumper wires
- 22AWG solid core wire
- [Link på amazon](https://www.amazon.se/dp/B09BFFJRST?ref=ppx_yo2ov_dt_b_product_details&th=1)
- Price: 219 SEK
- Purpose: Makes it easy to get a slim wire design
- 
- Power bank
- I used two I had lying around at home
## Computer setup
**Chosen IDE** VSCode
I chose VSCode because I have prior experience with it and find it comfortable to use. Its extensive features and customization options make it a powerful tool for development. Additionally, VSCode's built-in Git integration allows me to commit my code to GitHub easily, ensuring that my work is versioned and backed up.
### Computer Setup and Device Programming Guide
**Chosen IDE:** Visual Studio Code (VSCode) with Pymakr plugin
This guide will provide a detailed walkthrough on setting up your computer to program the Raspberry Pi Pico W using Visual Studio Code (VSCode) and Pymakr. It will include steps for flashing the firmware, installing necessary plugins, and uploading code to the device. Additionally, it will cover setting up GitHub integration for easy code versioning and collaboration.
### Installation Guide
#### Install Visual Studio Code (VSCode)
1. **Download VSCode:** Visit the [official VSCode website](https://code.visualstudio.com/) and download the installer for your operating system.
2. **Install VSCode:** Follow the on-screen installation instructions for your OS.
#### Install Node.js
1. **Download Node.js:** Go to the [Node.js website](https://nodejs.org/) and download the installer.
2. **Install Node.js:** Run the installer and follow the installation instructions.
3. **Verify Installation:** Open a terminal or command prompt and type the following commands to check that Node.js and npm (Node Package Manager) are installed correctly:
```sh
node -v
npm -v
```
#### Install Pymakr Plugin in VSCode
1. **Open VSCode:** Launch VSCode.
2. **Access Extensions:** Click the Extensions icon in the Activity Bar on the side of the window or press `Ctrl+Shift+X`.
3. **Search for Pymakr:** Type "Pymakr" in the search bar.
4. **Install Pymakr:** Click the Install button for the Pymakr plugin.
#### Flash MicroPython Firmware to Raspberry Pi Pico W
1. **Download Firmware:** Get the latest MicroPython UF2 firmware for the Raspberry Pi Pico W from the [MicroPython website](https://micropython.org/download/rp2-pico/).
2. **Prepare Pico W:** Press and hold the BOOTSEL button on the Pico W and connect it to your computer via USB.
3. **Release BOOTSEL Button:** The Pico will mount as a mass storage device named "RPI-RP2".
4. **Flash Firmware:** Drag and drop the downloaded UF2 file onto the "RPI-RP2" volume. The Pico will reboot automatically and be ready for MicroPython.
#### Configure Pymakr
1. **Open Pymakr Settings:** In VSCode, click the Pymakr icon in the status bar at the bottom of the window. Or to the left menubar.
2. You will find your Pico unde devices (down - left)
3. Click on the lightning to connect your device
4. Now you can hover over your decive and click the terminal button to open a terminal window.
#### Upload Code to Raspberry Pi Pico W
1. **Create a New File:** In VSCode, create a new file with a `.py` extension (e.g., `main.py`).
2. **Write Code:** Enter your MicroPython code. For example:
```python
from machine import Pin
import time
led = Pin(25, Pin.OUT)
while True:
led.toggle()
time.sleep(1)
```
3. **Upload Code:** Use the Pymakr toolbar to upload the code to your device by clicking the "Upload" button.
#### 8. Set Up GitHub Integration
1. **Install Git:** Download and install Git from the [official website](https://git-scm.com/).
2. **Configure Git:** Open a terminal or command prompt and configure Git with your name and email:
```sh
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
```
3. **Initialize a Git Repository:** In VSCode, open the integrated terminal (`Ctrl+```) and initialize a new Git repository:
```sh
git init
```
4. **Commit Your Code:** Add your code files to the repository and commit them:
```sh
git add .
git commit -m "Initial commit"
```
5. **Push to GitHub:** Create a new repository on GitHub, copy the repository URL, and push your local repository to GitHub:
```sh
git remote add origin https://github.com/yourusername/your-repo.git
git branch -M main
git push -u origin main
```
## Putting everything together

**LCD (I2C)**
VCC: 5V (VBUS)
GND: GND
SDA: GPIO 0
SCL: GPIO 1
**SCD40 (I2C)**
VCC: 3.3V
GND: GND
SDA: GPIO 0
SCL: GPIO 1
**Stemma Soil Moisture Sensor (I2C)**
VCC: 3.3V
GND: GND
SDA: GPIO 0
SCL: GPIO 1
**DS18B20 Temperature Sensor**
VCC: 3.3V
GND: GND
Data: GPIO 4 (with 4.7kΩ pull-up resistor to 3.3V)
**HC-SR04 Ultrasonic Sensor**
VCC: 3.3V
GND: GND
Trigger: GPIO 22
Echo: GPIO 21
**Note**: The I2C devices (LCD, Soil Moisture Sensor, and Jevina SCD40) are all connected to the same SDA and SCL lines on the I2C bus, which allows multiple devices to communicate over the same two wires.
### Electrical Calculations and Pull-Up Resistor
**Pull-Up Resistor for DS18B20**
The DS18B20 sensor requires a pull-up resistor on the data line to ensure that the line remains high when no device is pulling it low.
#### Calculation
A typical value for the pull-up resistor is 4.7kΩ. This value ensures reliable communication without drawing too much current.
- **Voltage `V`**: 3.3V (from the Raspberry Pi Pico)
- **Resistance `R`**: 4.7kΩ

This calculation shows that the current through the pull-up resistor is approximately 0.7mA, which is well within the safe operating range for the GPIO pin on the Pico.
#### Purpose of the Pull-Up Resistor
The pull-up resistor is essential for the DS18B20 data line because it ensures the line is at a high voltage level when no sensor is pulling it low. This allows the microcontroller to correctly detect the high and low states of the data line, enabling reliable communication.
#### Power consumption
**Components consumption:**
JHD162A, constant power consumption (since it's always on with backlight) `25mA`
I2C interface for LCD, `1mA`
Capacitive Soil Moisture Sensor I2C, average power consumption `6mA` (Only active during measurement)
DS18B20, average power consumption `1.5mA` (Only active during measurement)
HC-SR04, average power consumption `15mA` (Only active during measurement)
Jevina SCD40, average power consumption `15mA` (Only active during measurement)
Raspberry Pi Pico, constant power consumption `30mA`, with WI-FI `100mA`, wifi is only active during measurements.
**Power consumption during normal operation (10-second loop without WI-FI)**
In my code i have my LCD always turned on, I change the page every 10 second.
```
LCD: 25mA
I2C interface for LCD: 1mA
Rapsberry Pi Pico W: 30mA
Total constant power consumption = 25 + 1 + 30 = 56mA
```
**Power consumption during measurement and data transmission**
In my loop that runs every 6th hour, I connect and disconnect WI-FI and MQTT-broker. This is also when the measurements takes place.
```
JHD162A: 25mA
I2C interface for LCD: 1mA
Capacitive Soil Moisture Sensor I2C: 6mA
DS18B20: 1.5mA
HC-SR04: 15mA
Jevina SCD40: 15mA
Raspberry Pi Pico W (WI-FI connected): 100mA
Total power consumption during measurement and Wi-Fi = 100 + 6 + 1.5 + 15 + 15 + 25 + 1 = 163.5 mA
```
**Calculating the average power consumption**
To find the average power consumption over 6 hours (360 minutes):
Power consumption during 359 minutes (normal operation)
`56mA x 359 minutes = 20024mA-minutes`
Power consumption during 1 minute (measurement and Wi-Fi active):
`163.5mA x 1 minute = 153.5mA-minutes`
Total power consumption over 6 hours:
`20024mA-minutes + 153.5mA-minutes = 20187.5mA-minutes`
Average consumption per minute:
$$
\frac{20187.5 \text{ mA-minutes}}{360 \text{ minutes}} = 56.1 \text{ mA}
$$
**Calculating battery life**
I have two Power banks, but i will do this calculation based on the one with 20000mAh
$$
\text{Battery Life (hours)} = \frac{\text{Battery Capacity (mAh)}}{\text{Average Power Consumption (mA)}} = \frac{20000 \text{ mAh}}{56.1 \text{ mA}} \approx 356.51 \text{ hours}
$$
$$
\frac{356.51 \text{ hours}}{24 \text{ hours/day}} \approx 14.85 \text{ days}
$$
**Conclusion**
This means that my power bank will be able to run for approximately 14 days
### Soldering
**Soldering the I2C Interface to the LCD**
To connect the I2C interface to the LCD, I had to solder the I2C interface board to the LCD. Here's a brief overview of how that is done:
1. **Align the Pins**: Place the I2C interface board on the back of the LCD, aligning the pins of the I2C interface with the corresponding pins on the LCD.
2. **Secure the Boards**: Ensure the boards are securely positioned, and the pins are making good contact.
3. **Heat the Soldering Iron**: Heat your soldering iron to the appropriate temperature.
4. **Apply Solder**: Touch the soldering iron to each pin and apply a small amount of solder. Ensure each connection is clean and shiny without any cold solder joints.
5. **Inspect the Joints**: Check all the soldered joints to make sure there are no bridges between pins and that each joint is properly connected.

*pactice makes perfect....one day, I guess*
**Soldering the Pins to the Jevina SCD40**
To connect the Jevina SCD40 sensor, I also had to solder the pins to the sensor board. Here's how to do it:
1. **Align the Pins**: Place the pin header onto the Jevina SCD40 sensor, ensuring the pins align with the corresponding holes on the sensor.
2. **Secure the Sensor**: Hold the sensor and pins securely in place. I used a cardboard box and pressed down the pins, I also used a tweezers to hold the back of the sensor up/stabil.
3. **Heat the Soldering Iron**: Heat your soldering iron to the appropriate temperature.
4. **Apply Solder**: Touch the soldering iron to each pin and apply a small amount of solder to each joint. Ensure each connection is clean and shiny.
5. **Inspect the Joints**: Check all the soldered joints to ensure there are no solder bridges between pins and that each joint is properly connected.

#### Tips for Soldering
- **Clean Iron Tip**: Keep the tip of the soldering iron clean by regularly wiping it on a damp sponge.
- **Steady Hands**: Use a helping hand tool or a vice to hold the components steady while soldering.
- **LESS IS MORE**
## Platform
I chose AWS IoT as my platform because it offers a wide range of possibilities for my frontend, allowing me to create a robust and flexible system. One of the key features I needed was the ability to access old data and view it from anywhere in the world, which is crucial since my project involves monitoring the compost in my cabin, which is only accessible via an internet connection, when home.
### Platform Choice and Comparison
Initially, I wanted to use Google Cloud IoT, but they discontinued this service in August last year. After this, I considered using the TIG stack (Telegraf, InfluxDB, and Grafana). However, I realized that Docker wasn't an option since I don't have a computer at the cabin. I also looked into Adafruit IO, but it wasn't suitable because I wanted to create my own frontend with features like API integrations.
### Cloud vs. Local Installation
AWS IoT is a cloud-based platform, which aligns perfectly with my requirement to access data remotely. A local installation wouldn't provide the same level of accessibility. Additionally, maintaining a local server would require more effort and resources.
### Subscription Model
For now, I am using the free tier of AWS services, which provides sufficient resources for development and testing. If my project scales, I can easily switch to a paid subscription, which offers more features and higher limits.
### Scaling the Idea
To scale my project, I have several options:
1. **Increase Resource Limits:** By moving to a higher-tier subscription, I can access more storage, computing power, and additional services.
2. **Multi-Region Deployment:** AWS allows deploying services in multiple regions to reduce latency and increase reliability.
3. **Serverless Architecture:** Utilizing AWS Lambda for processing helps manage the load dynamically, making it easier to scale up or down based on demand.
### Functionality and Reasons for Choosing AWS IoT
AWS IoT provides a comprehensive set of features that cater to my project needs:
- **Device Management:** Easy onboarding, management, and monitoring of IoT devices.
- **Security:** Built-in security features to ensure data integrity and privacy.
- **Scalability:** Ability to handle numerous devices and large volumes of data without significant reconfiguration.
- **Integration:** Smooth integration with other AWS services like Lambda and DynamoDB.
In conclusion, AWS IoT provides the ideal combination of functionality, scalability, and ease of use, making it the perfect choice for my compost monitoring project.
## The Code
**Links to my repositories**:
*Since I have already exceeded the maximum number of characters*
- [Frontend](https://github.com/MarkerAnn/smart_compost_frontend.git)
- [PICO](https://github.com/MarkerAnn/Smart-Compost.git)
- ---
### How it all connects
#### 1. Sensors Measure Data
- **Sensors**: Temperature, moisture, etc.
- **Measurements**: The sensors measure environmental data in the compost (e.g., temperature, moisture).
#### 2. Send Data to MQTT Broker via Wi-Fi
- **Raspberry Pi Pico W**: Collects measurements from the sensors.
- **Wi-Fi**: The Raspberry Pi Pico W connects to Wi-Fi and sends data to the MQTT broker. Every 6th hour (e.g, 4 times per day)
```python=
def initialize_mqtt_client():
client = MQTTClient(
MQTT_CLIENT_ID,
MQTT_BROKER,
keepalive=60,
ssl=True,
ssl_params={
"key": key,
"cert": cert,
"server_hostname": MQTT_BROKER,
"cert_reqs": ssl.CERT_REQUIRED,
"cadata": ca,
})
return client
```
I used [simple.py](https://github.com/micropython/micropython-lib/blob/master/micropython/umqtt.simple/umqtt/simple.py) as my mqtt library and initialize my MQTT client in my `main.py`.
#### 3. MQTT Broker on AWS IoT
- **MQTT Broker**: Receives data from the Raspberry Pi Pico W.
- **AWS IoT Core**: Manages MQTT connections and secure communication.
#### 4. Lambda Function is Triggered
- **Lambda Function**: An AWS Lambda function is triggered each time new data arrives at the MQTT broker.
- **Processing**: The Lambda function processes the incoming data and sends it to DynamoDB.

#### 5. Data is Stored in DynamoDB
- **DynamoDB**: The AWS Lambda function stores the data in a DynamoDB table.
- **Storage**: All data are stored with timestamps.
#### 6. Frontend Fetches Data
- **React Frontend**: A web page built with React fetches data from DynamoDB.
- **AWS SDK V3**: Used to interact with DynamoDB and retrieve data from the past 7 days.
#### 7. Data is Displayed on the Web Page
- **Visualization**: Data from the past 7 days is displayed on the web page.
- **User Interface**: Users can see the latest temperature and moisture information in real-time.
- Hosted on Firebase as an web app
---
#### Graphical representation of the process:
**Collect the data:**

**Data presentation and backend operations:**

## Transmsitting the data / connectivity
In my project, I chose to use MQTT over WiFi for data transmission due to the lack of LoRaWAN coverage in the area where my cabin is located. Installing a dedicated LoRaWAN gateway would have added complexity and cost.
WiFi with MQTT
Range:
WiFi typically provides a limited range of around 100 meters indoors and a bit more outdoors, depending on obstacles and interference. In my case, WiFi coverage was already in place at the cabin, so extending the range was not necessary.
Battery Consumption:
WiFi is known for its high power consumption, which can drain batteries faster than low-power protocols. Given that my Raspberry Pi Pico is powered by a power bank in a remote location, I optimized data transmission to occur only four times a day. This minimizes the active time of the WiFi module, conserving battery life. The MQTT broker and WiFi connection are only active during these transmission intervals.
LoRaWAN as an Alternative
Range:
LoRaWAN is designed for long-range communication, making it ideal for remote locations like my cabin.
Battery Consumption:
LoRaWAN excels in low-power consumption, allowing devices to potentially run for months or even years on a single battery charge, especially with infrequent data transmission.
Considerations:
Although LoRaWAN could have provided superior range and battery efficiency, the lack of existing LoRaWAN coverage in my area posed a significant challenge. Setting up a private LoRaWAN gateway would have required additional resources.
Conclusion
WiFi with MQTT was chosen due to practical constraints, despite LoRaWAN's potential advantages in battery efficiency and range. The existing WiFi infrastructure and the need to avoid the complexity of setting up a new gateway influenced this decision. For future projects in similar remote locations, deploying a LoRaWAN gateway could be beneficial to leverage its advantages in battery consumption and long-range communication.
## Presenting the data
The presentation part of my project involves displaying sensor data on a web-based dashboard built using a React application. The AWS SDK V3 fetches data from DynamoDB, chosen for its seamless integration with AWS IoT and efficient handling of time-series data.
**Components:**
**WeatherComponent:** Displays current weather conditions.
**CompostAnalysis:** Provides analysis based on the latest sensor data.
**LatestReading:** Shows the most recent readings for each sensor.
**SensorChart:** Plots historical data for each sensor over the past week.
Data is fetched and updated in real-time, ensuring the dashboard displays the most current information. Data is saved to DynamoDB four times a day, balancing up-to-date information with power and network usage considerations.
**Choice of Database:**
Seamless Integration: DynamoDB integrates naturally with AWS IoT.
- Scalability: Efficiently handles large volumes of data.
- Low Maintenance: Fully managed service reducing maintenance overhead.
- Real-time Data Access: Provides fast read/write operations essential for real-time monitoring.
- Automation/Triggers:
AWS Lambda functions are set up to automate data processing and ensure real-time updates. These functions handle:
- **Data Storage:** Saves incoming sensor data to DynamoDB.
Real-time Data Fetching: Fetches the latest data for the dashboard, including readings from the past 7 days.
- **Planned Enhancements:**
Data Aggregation: Summarizing data for longer periods (e.g., daily/weekly averages).
Alert Generation: Triggering alerts if sensor readings exceed predefined thresholds.



## Finalyzing the design
When I first ordered my sensors, I had no idea what I was getting into. The journey from being clueless to actually building a functioning system has been incredibly rewarding. I've been questioned my choice of education, wondering if I should have pursued a more technical field instead of a scientific one. Because this project made me realize how much I enjoy hands-on work and the creative freedom it offers.
**Final Thoughts:**
Overall, I am very pleased with how the project turned out. It was extremely satisfying to see the system work as intended and to be able to monitor environmental data from my cabin in real time.
**What Could Have Been Done Differently:**
Database Choice: While DynamoDB worked well, exploring other databases like InfluxDB, which is specifically designed for time-series data, could have been beneficial.
More Robust Error Handling: Implementing more comprehensive error handling in both the data collection and the dashboard could improve system reliability.
LoRaWAN Integration: If I had access to a LoRaWAN gateway, using LoRaWAN instead of WiFi could have improved battery life and range. This is something I might explore in future projects.
Additional Features: Incorporating more features like data aggregation, historical trend analysis, and automated alerts could enhance the system's functionality.
**"Summa summarum":** I enjoyed the flexibility to create something with my hands and the minimal constraints from teachers allowed for a lot of creativity. I found this aspect particularly fun compared to the structured nature of scientific studies. This project was a great learning experience and has sparked a newfound interest in IoT and hardware projects. I look forward to applying what I've learned to future endeavors and exploring more technical and hands-on projects.
## Photos
*well, it ain't gonna win a beauty contest...*
 

 
 