Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new: added ipy stuff #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
260 changes: 260 additions & 0 deletions SoilMoistureLesson.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "8997db79",
"metadata": {},
"source": [
"# Introduction: \n",
"This lesson covers how to navigate the Raspberry Pi Pico/W, the capacitive soil moisture sensor, and MicroPython, and analyze the data collected. "
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "96d4528b",
"metadata": {},
"source": [
"# Materials:\n",
"\n",
"* Raspberry Pi Pico W\n",
"* Capacitive Soil Moisture Sensor v1.2\n",
"* Sparkfun Qwiic pHAT Extension\n",
"* Five male-to-female jumper wires\n",
"* One 330 Ohm resistor\n",
"\n",
"\n",
"## Other Materials:\n",
"* 8 cups\n",
"* 2(A-B)-4(A-D) different types of soil\n",
"\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "47fbd239",
"metadata": {},
"source": [
"*Note: Include Thonny Set Up???"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "45e558a8",
"metadata": {},
"source": [
"# Wiring Diagram: ???"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "7f377583",
"metadata": {},
"source": [
"# MicroPython Implementation & Data Collection:\n",
"\n",
"Copy and paste the code below into Thonny and upload into capacitive soil moisture sensor"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "320fca2b",
"metadata": {},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'machine'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[1], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mmachine\u001b[39;00m \u001b[39mimport\u001b[39;00m Pin, ADC\n\u001b[0;32m 2\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mjson\u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mutime\u001b[39;00m\n",
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'machine'"
]
}
],
"source": [
"from machine import Pin, ADC\n",
"import json\n",
"\n",
"import utime\n",
"#\n",
"adc = ADC(Pin(28))\n",
"print(adc.read_u16())\n",
"#print(adc.read_uv())\n",
"\n",
"class Moisture_Sensor:\n",
" def __init__(self, mqtt_handle=None, pin_num=28, sensor_id=\"soil_moisture_1\", topic=\"sensor/moisture\", indicator_pin=\"LED\"):\n",
" utime.sleep_us(100)\n",
"\n",
" self.sensor_id = sensor_id\n",
" self.topic = topic\n",
" self.pin = ADC(Pin(pin_num))\n",
" self._raw_value = 0\n",
" self.calibration_value_low = 0\n",
" self.calibration_value_high = 65535\n",
" self.indicator_pin = Pin(indicator_pin, Pin.OUT)\n",
" self.mqtt_handle = mqtt_handle\n",
"\n",
" def toggle_indicator(self):\n",
" self.indicator_pin.toggle()\n",
"\n",
" def read_moisture(self):\n",
" self.indicator_pin.on()\n",
" self._raw_value = self.pin.read_u16()\n",
" utime.sleep(1)\n",
" self.indicator_pin.off()\n",
" print(self._raw_value)\n",
" if self.mqtt_handle is None:\n",
" return self._raw_value\n",
" else:\n",
" return None\n",
" # return self._raw_value.to_bytes(2, 'big')\n",
"\n",
" def publish(self):\n",
" if self.mqtt_handle is not None:\n",
" self.read_moisture()\n",
" msg = json.dumps({self.sensor_id : (self._raw_value / self.calibration_value_high)})\n",
" self.mqtt_handle.connect()\n",
" self.mqtt_handle.publish(self.topic, msg)\n",
" self.mqtt_handle.disconnect()\n",
" else:\n",
" raise AttributeError(\"No MQTT handler defined for this sensor\")\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" test_ms = Moisture_Sensor()\n",
"\n",
" while True:\n",
" reading = test_ms.read_moisture()\n",
" print(reading)\n",
" utime.sleep(2)"
]
},
{
"cell_type": "markdown",
"id": "93bac4c2",
"metadata": {},
"source": [
"# Lesson Instructions:\n",
"1. Copy and paste code into Thonny, and upload into soil moisture sensor\n",
"2. Gently insert soil sensor into soil into cup A. Make sure sensor is fully submeerged in soil.\n",
"3. Begin a timer or stopwatch to measure for two minutes. Allow sensor to collect data for two minutes and move onto next cup.\n",
"4. After collecting all cup measurements, examine and compare moisture measurements.\n",
"5. Identify which soil had the most soil moisture levels and record results.\n",
"6. Consider the implications of the moisture levels you've measured. For example, you can use this information to determine which type of soil retains the most moisture, which can be valuable for gardening or plant care.\n",
"7. Repeat as Necessary: If you have more soil cups or want to verify your results, repeat the measurement process for additional soil samples."
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "812d39f0",
"metadata": {},
"source": [
"# Data Analysis:\n",
"Based on the data collected we can determine some factors that may have affected the soil moisture content."
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "0b4d44e3",
"metadata": {},
"source": [
"**Question 1.** The dataset is in a file called `soil_data.csv`. Load it into a table named `soil_data`.\n",
"\n",
"1. What was the largest increase in moisture based on the data set?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eb0dedf9",
"metadata": {},
"outputs": [],
"source": [
"#Load CSV File\n",
"soil_data = Table.read_table(\"soil_data.csv\").column(\"data\")\n",
"soil_data.show(15)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "afa0a39a",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'soil_data' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_20380\\4174363898.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;31m#Create a \"for loop\": used to iterate/repeat a specific/known amount of times\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdata\u001b[0m \u001b[1;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msoil_data\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 6\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mi\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[0mprevious_num\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mNameError\u001b[0m: name 'soil_data' is not defined"
]
}
],
"source": [
"#prime loop: establishing the first conditional check in a loop\n",
"biggest_increase = 0\n",
"\n",
"#Create a \"for loop\": used to iterate/repeat a specific/known amount of times\n",
"#enumerate funtion keeps track of the amount of iterations\n",
"for i, data in enumerate(soil_data):\n",
" if i == 0:\n",
" previous_num = data\n",
" else:\n",
" #prime\n",
" change = data - previous_num\n",
" if change > 0:\n",
" if change > biggest_increase:\n",
" biggest_increase = change\n",
" #reprime loop\n",
" previous_num = data\n",
"\n",
"#Print calculated increase\n",
"print(\"Biggest increase is: \", biggest_increase)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "7de0722b",
"metadata": {},
"source": [
"**Question 2.** \n",
"\n",
"2. From the dashboard, compare your graph to others that used different types of soil. Can you make any inferences based on the data compared?"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
8 changes: 8 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: SoilMoistureLesson
channels:
- defaults
- conda-forge
dependencies:
- python
- jupyter