Project 4.1.1: National Smart Agriculture Pilot
Required Components
![]() Water pumps |
![]() Relay modules |
![]() Humidity sensors |
![]() Temperature sensors |
![]() Soil moisture sensors |
![]() Raspberry Pi Pico 2 W |
![]() Breadboards |
![]() Jumper wires |
| Computers with Thonny IDE |
Circuit Connections
| Component Pin | Connects To | Pico GPIO / Physical Pin Number | Notes |
|---|---|---|---|
| Soil Moisture Sensor | ADC0 (Pin 26) | GPIO 26 / Physical Pin 31 | Analog output |
| Temperature Sensor | ADC1 (Pin 27) | GPIO 27 / Physical Pin 32 | Analog output |
| Humidity Sensor | ADC2 (Pin 28) | GPIO 28 / Physical Pin 33 | Analog output |
| Relay IN | GPIO15 (Pin 20) | GPIO 15 / Physical Pin 20 | Relay control signal |
| Relay NO | Water Pump positive | -- | Through relay switch |
| Pump negative | External power ground | -- | External pump ground |
Step-by-Step Assembly
Phase 1: Layer 1 - Edge Node Development
Hardware Setup
Connect Sensors to Pico 1 (Edge Node): 1. Soil Moisture Sensor → ADC0 (Pin 26) 2. Temperature Sensor → ADC1 (Pin 27) 3. Humidity Sensor → ADC2 (Pin 28) Connect Relay for Pump Control: 4. Relay IN → GPIO15 (Pin 20) 5. Relay NO → Water Pump positive 6. Pump negative → External power ground
Phase 2: Layer 2 - Decision Logic Development
Phase 3: Layer 3 - System Layer
Testing Individual Components
- Test edge node data collection
- Verify risk calculation accuracy
- Test regional aggregation
- Validate national reporting
Full Project Code
Layer 1: Edge Node Development
import network
import socket
import time
from machine import Pin, I2C, ADC
import bme280
SSID = 'YOUR_WIFI_NAME'
PASSWORD = 'YOUR_WIFI_PASSWORD'
SENSOR_WET_THRESHOLD = 45000
PUMP_RUN_SECONDS = 5
REFRESH_INTERVAL = 2
i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000)
bme = bme280.BME280(i2c=i2c)
soil = ADC(Pin(26))
relay = Pin(15, Pin.OUT)
relay.off()
print('National Smart Agriculture Pilot ready')
print('Wait 10 seconds for sensors to stabilise')
time.sleep(10)
Layer 2: Decision Logic Development
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
print('Connecting to Wi-Fi...')
for _ in range(15):
if wlan.isconnected():
break
time.sleep(1)
if not wlan.isconnected():
raise RuntimeError('Wi-Fi connection failed')
ip_address = wlan.ifconfig()[0]
print('Connected. Open http://{} in your browser'.format(ip_address))
farm_state = {
'region': 'Region-A',
'node': 'Edge-01',
'temperature': 0,
'humidity': 0,
'soil_moisture': 0,
'pump_active': False,
'last_watered': 'Never'
}
def read_sensors():
farm_state['temperature'] = bme.values[0]
farm_state['humidity'] = bme.values[2]
farm_state['soil_moisture'] = soil.read_u16()
def decide_irrigation():
if farm_state['soil_moisture'] < SENSOR_WET_THRESHOLD:
relay.on()
farm_state['pump_active'] = True
print('Pump ON - soil moisture low:', farm_state['soil_moisture'])
time.sleep(PUMP_RUN_SECONDS)
relay.off()
farm_state['pump_active'] = False
farm_state['last_watered'] = time.ticks_ms()
print('Pump OFF - watering cycle complete')
Layer 3: System Layer
def build_dashboard():
pump_status = 'ACTIVE' if farm_state['pump_active'] else 'IDLE'
watered_seconds = time.ticks_diff(time.ticks_ms(), farm_state['last_watered']) // 1000 if farm_state['last_watered'] != 'Never' else 'N/A'
return """<!DOCTYPE html>
<html>
<head>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<meta http-equiv='refresh' content='2'>
<title>National Smart Agriculture Pilot</title>
</head>
<body style='font-family:Arial;text-align:center;padding:20px;background:#f4f4f9'>
<h1 style='color:#2e7d32'>National Smart Agriculture Pilot</h1>
<h2 style='color:#1565c0'>{region} / {node}</h2>
<table style='margin:auto;border-collapse:collapse'>
<tr><td style='padding:10px;border:1px solid #ccc'><b>Temperature</b></td><td style='padding:10px;border:1px solid #ccc'>{temp} C</td></tr>
<tr><td style='padding:10px;border:1px solid #ccc'><b>Humidity</b></td><td style='padding:10px;border:1px solid #ccc'>{hum} %</td></tr>
<tr><td style='padding:10px;border:1px solid #ccc'><b>Soil Moisture</b></td><td style='padding:10px;border:1px solid #ccc'>{soil}</td></tr>
<tr><td style='padding:10px;border:1px solid #ccc'><b>Pump Status</b></td><td style='padding:10px;border:1px solid #ccc'>{pump}</td></tr>
<tr><td style='padding:10px;border:1px solid #ccc'><b>Last Watered</b></td><td style='padding:10px;border:1px solid #ccc'>{watered} s</td></tr>
</table>
<p>Open this page on any device on the same Wi-Fi network.</p>
</body>
</html>""".format(
region=farm_state['region'],
node=farm_state['node'],
temp=farm_state['temperature'],
hum=farm_state['humidity'],
soil=farm_state['soil_moisture'],
pump=pump_status,
watered=watered_seconds
)
address = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
server = socket.socket()
server.bind(address)
server.listen(1)
server.settimeout(0.2)
while True:
read_sensors()
decide_irrigation()
try:
client, client_address = server.accept()
except OSError:
time.sleep(0.1)
continue
print('Client connected from', client_address)
client.recv(1024)
response = build_dashboard()
client.send(
'HTTP/1.1 200 OK\r\n'
'Content-Type: text/html\r\n'
'Connection: close\r\n\r\n'.encode()
)
client.sendall(response.encode())
client.close()
How the Code Works
| Code Section | What It Does | Why It Matters |
|---|---|---|
| BME280 I2C driver (GPIO 8/9) | Reads temperature and humidity from the environmental sensor | Provides the data needed to monitor crop conditions |
| Soil moisture ADC (GPIO 26) | Reads raw soil water content from the analog sensor | Lets the system decide when irrigation is required |
| Relay control (GPIO 15) | Turns the water pump on or off | Automates irrigation without manual intervention |
Expected Result
- The Shell prints an IP address after connecting to Wi-Fi.
- The browser page loads with temperature, humidity, soil moisture, and pump status.
- When soil moisture drops below the threshold, the relay clicks and the pump runs for several seconds.
- The dashboard updates "Last Watered" after each irrigation cycle.
- Multiple Pico nodes can be deployed with different region names to simulate a national network.
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| BME280 not detected | Wrong I2C address or missing bme280 library | Run the I2C scanner test; confirm bme280.py is on the Pico |
| No soil moisture change | Sensor wired to wrong pin or not powered | Check GPIO 26 connection and 3.3V power |
| Relay does not click | Wrong GPIO pin or insufficient voltage | Check GPIO 15 wiring; ensure relay VCC matches the module spec |
| Pump does not run | Relay logic inverted or external pump off | Adjust relay.on() / relay.off() or check the external pump power |
| Web page does not load | Wrong Wi-Fi credentials or 5 GHz network | Use 2.4 GHz SSID and password; confirm IP in the Shell |






