From 1d8f49e40fb2c335e4db4e6a1736d6538f1286a5 Mon Sep 17 00:00:00 2001 From: quasont1337 Date: Thu, 7 Sep 2023 13:26:49 -0700 Subject: [PATCH 1/2] new: added ipy stuff --- SoilMoistureLesson.ipynb | 260 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 260 insertions(+) create mode 100644 SoilMoistureLesson.ipynb diff --git a/SoilMoistureLesson.ipynb b/SoilMoistureLesson.ipynb new file mode 100644 index 0000000..4bdbd42 --- /dev/null +++ b/SoilMoistureLesson.ipynb @@ -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\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 +} From cee442282af51efd240ae6003dddbbd60582aab1 Mon Sep 17 00:00:00 2001 From: quasont1337 Date: Thu, 7 Sep 2023 13:51:20 -0700 Subject: [PATCH 2/2] New: created env file to set conda env to run less --- environment.yml | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 environment.yml diff --git a/environment.yml b/environment.yml new file mode 100644 index 0000000..acbd402 --- /dev/null +++ b/environment.yml @@ -0,0 +1,8 @@ +name: SoilMoistureLesson +channels: + - defaults + - conda-forge +dependencies: + - python + - jupyter + \ No newline at end of file