{"id":21884508,"url":"https://github.com/kientech/iot-plant-watering-system","last_synced_at":"2025-03-22T01:29:31.269Z","repository":{"id":260740294,"uuid":"882180575","full_name":"kientech/IoT-Plant-Watering-System","owner":"kientech","description":null,"archived":false,"fork":false,"pushed_at":"2024-11-02T07:53:15.000Z","size":53,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-26T19:44:05.687Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kientech.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-02T04:59:02.000Z","updated_at":"2024-11-02T07:53:18.000Z","dependencies_parsed_at":"2024-11-02T08:25:26.368Z","dependency_job_id":"cfa1be77-01fa-4e2f-9ec4-448c6c5319d9","html_url":"https://github.com/kientech/IoT-Plant-Watering-System","commit_stats":null,"previous_names":["kientech/iot-plant-watering-system"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kientech%2FIoT-Plant-Watering-System","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kientech%2FIoT-Plant-Watering-System/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kientech%2FIoT-Plant-Watering-System/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kientech%2FIoT-Plant-Watering-System/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kientech","download_url":"https://codeload.github.com/kientech/IoT-Plant-Watering-System/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244892728,"owners_count":20527487,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-28T10:14:22.484Z","updated_at":"2025-03-22T01:29:31.246Z","avatar_url":"https://github.com/kientech.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Automatic Plant Watering System 🌿\n\nThis project builds a smart plant watering system that automatically maintains soil moisture at optimal levels without manual intervention. Using a soil moisture sensor and a motor controlled via relay, the system automatically turns on the motor to water plants when the soil moisture drops below a set threshold. Users can also control the motor manually through a web interface.\n\n## 🛠️ Components and Technologies\n\n- **Hardware:**\n  - ESP32 (for Wi-Fi connection and motor control)\n  - Soil moisture sensor\n  - Relay module (to control motor)\n  - 5V motor (for water pumping)\n  - Optional: Push button and LED for status indication\n  \n- **Software:**\n  - **Frontend:** ReactJS (user interface)\n  - **Backend:** WebSocket Server (Node.js)\n  - **Communication Protocol:** WebSocket (ESP32 and server communication)\n\n## 📋 System Requirements\n\n1. **Hardware:**\n   - ESP32\n   - Relay module and 5V motor\n   - Soil moisture sensor\n   - Jumper wires and breadboard for connections\n\n2. **Software:**\n   - Node.js v14+ and npm (for running the WebSocket server and React frontend)\n   - Arduino IDE (for programming ESP32)\n\n## 📑 Hardware Wiring Diagram\n\n| Component               | ESP32 Pin | Notes                               |\n|-------------------------|-----------|-------------------------------------|\n| Soil Moisture Sensor (D0)| D2        | Reads soil moisture level           |\n| Relay Module            | D4        | Controls motor on/off               |\n| Motor                   | 5V power  | Pumps water when relay is on        |\n\n## 🔧 Setup and Configuration\n\n### Step 1: Program the ESP32\n\n1. **Set up ESP32 support** in Arduino IDE:\n   - Go to **File \u003e Preferences** and add the ESP32 boards manager URL: `https://dl.espressif.com/dl/package_esp32_index.json`\n   - Go to **Tools \u003e Board** and select **ESP32**.\n\n2. **Install the WebSocket library**:\n   - In **Tools \u003e Manage Libraries**, search for and install `ArduinoWebsockets`.\n\n3. **Upload ESP32 code**:\n\n   Open a new Arduino file, paste in the following code, and replace `Your_WiFi_SSID` and `Your_WiFi_Password` with your Wi-Fi credentials. Update `WebSocket_Server_IP` with your server's IP address.\n\n   ```cpp\n   #include \u003cWiFi.h\u003e\n   #include \u003cArduinoWebsockets.h\u003e\n\n   using namespace websockets;\n\n   const char* ssid = \"Your_WiFi_SSID\";\n   const char* password = \"Your_WiFi_Password\";\n   const char* websocket_server = \"WebSocket_Server_IP\";\n   const int websocket_port = 5000;\n\n   WebsocketsClient webSocket;\n   const int sensorPin = 2;  // Soil moisture sensor\n   const int motorPin = 4;   // Relay control pin\n\n   void setup() {\n       Serial.begin(115200);\n       pinMode(motorPin, OUTPUT);\n       digitalWrite(motorPin, LOW); // Initially keep the motor off\n\n       WiFi.begin(ssid, password);\n       while (WiFi.status() != WL_CONNECTED) {\n           delay(1000);\n           Serial.println(\"Connecting to WiFi...\");\n       }\n       Serial.println(\"Connected to WiFi\");\n\n       webSocket.connect(websocket_server, websocket_port, \"/\");\n\n       webSocket.onMessage([](WebsocketsMessage message) {\n           String data = message.data();\n           Serial.println(\"Message received: \" + data);\n           if (data == \"MOTOR_ON\") {\n               digitalWrite(motorPin, HIGH);\n           } else if (data == \"MOTOR_OFF\") {\n               digitalWrite(motorPin, LOW);\n           }\n       });\n   }\n\n   void loop() {\n       int humidity = analogRead(sensorPin);\n       String jsonPayload = \"{\\\"type\\\":\\\"humidityData\\\",\\\"value\\\":\" + String(humidity) + \"}\";\n       webSocket.send(jsonPayload);\n\n       if (webSocket.available()) {\n           webSocket.poll();\n       }\n       delay(1000);\n   }\n   ```\n\n4. **Upload the code** to ESP32.\n\n### Step 2: Set Up WebSocket Server\n\n1. **Create the WebSocket Server**:\n   - In the root of your project folder, create a file named `server.js` and paste the code below:\n\n     ```javascript\n     import WebSocket, { WebSocketServer } from \"ws\";\n\n     const wss = new WebSocketServer({ port: 5000 });\n     let motorState = false;\n\n     wss.on(\"connection\", (ws) =\u003e {\n       console.log(\"Client connected\");\n\n       ws.on(\"message\", (message) =\u003e {\n         const data = JSON.parse(message);\n         if (data.type === \"humidityData\") {\n           console.log(`Humidity: ${data.value}`);\n           if (data.value \u003c 300 \u0026\u0026 !motorState) {\n             motorState = true;\n             ws.send(JSON.stringify({ type: \"motorControl\", state: \"MOTOR_ON\" }));\n           }\n         } else if (data.type === \"manualControl\") {\n           motorState = data.state === \"ON\";\n           ws.send(JSON.stringify({ type: \"motorControl\", state: motorState ? \"MOTOR_ON\" : \"MOTOR_OFF\" }));\n         }\n       });\n     });\n\n     console.log(\"WebSocket server running on ws://localhost:5000\");\n     ```\n\n2. **Run the WebSocket Server**:\n   Open a terminal in the project folder and run:\n   ```bash\n   node server.js\n   ```\n\n### Step 3: Set Up Frontend (React)\n\n1. **Create a React project**:\n   ```bash\n   npx create-react-app plant-watering\n   cd plant-watering\n   ```\n\n2. **Add Frontend Code**:\n   - Replace `App.js` with the code below:\n\n     ```javascript\n     import React, { useEffect, useState } from \"react\";\n     import \"./App.css\";\n\n     function App() {\n       const [humidity, setHumidity] = useState(null);\n       const [motorState, setMotorState] = useState(false);\n       const [ws, setWs] = useState(null);\n\n       useEffect(() =\u003e {\n         const websocket = new WebSocket(\"ws://WebSocket_Server_IP:5000\");\n         setWs(websocket);\n\n         websocket.onopen = () =\u003e {\n           console.log(\"Connected to WebSocket server\");\n         };\n\n         websocket.onmessage = (event) =\u003e {\n           const data = JSON.parse(event.data);\n           if (data.type === \"humidityData\") {\n             setHumidity(data.value);\n           } else if (data.type === \"motorControl\") {\n             setMotorState(data.state === \"MOTOR_ON\");\n           }\n         };\n\n         return () =\u003e {\n           websocket.close();\n         };\n       }, []);\n\n       const handleMotorControl = (state) =\u003e {\n         if (ws \u0026\u0026 ws.readyState === WebSocket.OPEN) {\n           const controlMessage = JSON.stringify({ type: \"manualControl\", state });\n           ws.send(controlMessage);\n           setMotorState(state === \"ON\");\n         }\n       };\n\n       return (\n         \u003cdiv className=\"App\"\u003e\n           \u003ch1\u003eAutomatic Plant Watering System\u003c/h1\u003e\n           \u003cdiv\u003eSoil Humidity: {humidity ?? \"Loading...\"}\u003c/div\u003e\n           \u003cdiv\u003eMotor State: {motorState ? \"ON\" : \"OFF\"}\u003c/div\u003e\n           \u003cbutton onClick={() =\u003e handleMotorControl(\"ON\")}\u003eTurn ON Motor\u003c/button\u003e\n           \u003cbutton onClick={() =\u003e handleMotorControl(\"OFF\")}\u003eTurn OFF Motor\u003c/button\u003e\n         \u003c/div\u003e\n       );\n     }\n\n     export default App;\n     ```\n\n3. **Start the frontend**:\n   ```bash\n   npm start\n   ```\n\n## 🧪 Testing and Operation\n\n1. **Check Wi-Fi connection**: Ensure the ESP32 connects to Wi-Fi successfully.\n2. **Start the WebSocket Server** and open the frontend in your browser.\n3. **Test**: View soil moisture data and control the motor from the interface.\n\n## 📋 Notes\n\n- **Low Moisture**: When soil moisture falls below the threshold, the motor automatically turns on.\n- **Manual Control**: Users can also turn the motor on or off through the web interface.\n\n## 📞 Support\n\nIf you encounter any issues, please open an **Issue** on GitHub or reach out via email.\n\nGood luck with your project! 🎉","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkientech%2Fiot-plant-watering-system","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkientech%2Fiot-plant-watering-system","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkientech%2Fiot-plant-watering-system/lists"}