IoT เกษตรอัจฉริยะ
IoT เกษตร Smart Farm Sensor Gateway Cloud Platform ระบบรดน้ำอัตโนมัติ Soil Moisture Temperature Humidity ลดต้นทุน เพิ่มผลผลิต ประหยัดน้ำ ESP32 MQTT
| Sensor | วัดอะไร | ช่วง | Protocol |
|---|---|---|---|
| DHT22 | อุณหภูมิ/ความชื้น | -40~80°C, 0-100% | Digital |
| Capacitive Soil | ความชื้นดิน | 0-100% | Analog |
| BH1750 | แสง (Lux) | 1-65535 lux | I2C |
| pH Sensor | ค่า pH | 0-14 | Analog |
| EC Sensor | ค่าการนำไฟฟ้า | 0-20 mS/cm | Analog |
| Rain Sensor | ฝน | 0/1 | Digital |
ESP32 Smart Farm
# === ESP32 Smart Farm (MicroPython) ===
# MicroPython on ESP32
# import machine
# import dht
# import network
# import time
# from umqtt.simple import MQTTClient
#
# # WiFi Connection
# wlan = network.WLAN(network.STA_IF)
# wlan.active(True)
# wlan.connect('FARM_WIFI', 'password')
# while not wlan.isconnected():
# time.sleep(1)
# print('Connected:', wlan.ifconfig())
#
# # Sensor Setup
# dht_sensor = dht.DHT22(machine.Pin(4))
# soil_adc = machine.ADC(machine.Pin(34))
# soil_adc.atten(machine.ADC.ATTN_11DB)
# relay_pump = machine.Pin(25, machine.Pin.OUT)
#
# # MQTT Setup
# client = MQTTClient('esp32-farm', 'mqtt.example.com')
# client.connect()
#
# # Main Loop
# MOISTURE_LOW = 40 # เปิดปั๊ม
# MOISTURE_HIGH = 70 # ปิดปั๊ม
#
# while True:
# dht_sensor.measure()
# temp = dht_sensor.temperature()
# humidity = dht_sensor.humidity()
# soil_raw = soil_adc.read()
# soil_pct = 100 - (soil_raw / 4095 * 100)
#
# # Auto Irrigation
# if soil_pct < MOISTURE_LOW:
# relay_pump.on()
# status = "WATERING"
# elif soil_pct > MOISTURE_HIGH:
# relay_pump.off()
# status = "IDLE"
#
# # Publish to MQTT
# import json
# data = json.dumps({
# "temp": temp, "humidity": humidity,
# "soil_moisture": soil_pct, "pump": status
# })
# client.publish('farm/sensors', data)
# time.sleep(60)
from dataclasses import dataclass
from typing import List
from datetime import datetime
@dataclass
class SensorReading:
timestamp: str
temperature: float
humidity: float
soil_moisture: float
light_lux: float
ph: float
pump_status: str
readings = [
SensorReading("08:00", 28.5, 72, 45, 15000, 6.5, "IDLE"),
SensorReading("10:00", 32.1, 65, 38, 45000, 6.5, "WATERING"),
SensorReading("12:00", 35.2, 58, 65, 80000, 6.4, "IDLE"),
SensorReading("14:00", 34.8, 60, 55, 60000, 6.4, "IDLE"),
SensorReading("16:00", 31.5, 68, 42, 25000, 6.5, "WATERING"),
SensorReading("18:00", 29.0, 75, 68, 5000, 6.5, "IDLE"),
]
print("=== Farm Sensor Dashboard ===")
for r in readings:
print(f" [{r.timestamp}] Temp: {r.temperature}°C | Humidity: {r.humidity}%")
print(f" Soil: {r.soil_moisture}% | Light: {r.light_lux} lux | "
f"pH: {r.ph} | Pump: {r.pump_status}")
Cloud Platform และ Dashboard
# === Cloud IoT Platform ===
# ThingsBoard Setup (Docker)
# docker run -d \
# --name thingsboard \
# -p 1883:1883 -p 8080:8080 \
# -v tb-data:/data \
# thingsboard/tb-postgres
# MQTT to ThingsBoard
# mosquitto_pub -h thingsboard.example.com \
# -t "v1/devices/me/telemetry" \
# -u "$ACCESS_TOKEN" \
# -m '{"temperature":32,"humidity":65,"soil":45}'
# Python Data Analytics
# import pandas as pd
# import matplotlib.pyplot as plt
#
# # Read sensor data
# df = pd.read_csv('farm_data.csv')
# df['timestamp'] = pd.to_datetime(df['timestamp'])
#
# # Daily averages
# daily = df.resample('D', on='timestamp').mean()
#
# # Water usage report
# watering_events = df[df['pump_status'] == 'WATERING']
# total_minutes = len(watering_events)
# water_liters = total_minutes * 5 # 5 L/min flow rate
#
# # Plot
# fig, axes = plt.subplots(3, 1, figsize=(12, 8))
# axes[0].plot(df['timestamp'], df['temperature'], 'r-')
# axes[0].set_ylabel('Temperature (°C)')
# axes[1].plot(df['timestamp'], df['soil_moisture'], 'b-')
# axes[1].set_ylabel('Soil Moisture (%)')
# axes[2].plot(df['timestamp'], df['light_lux'], 'y-')
# axes[2].set_ylabel('Light (lux)')
# plt.savefig('farm_report.png')
platforms = {
"ThingsBoard": {"type": "Open Source", "protocol": "MQTT CoAP HTTP", "cost": "ฟรี (CE)"},
"AWS IoT Core": {"type": "Cloud", "protocol": "MQTT HTTPS", "cost": "$0.08/M msg"},
"Blynk": {"type": "SaaS", "protocol": "MQTT HTTP", "cost": "Free tier + Paid"},
"NETPIE": {"type": "Thai Platform", "protocol": "MQTT REST", "cost": "ฟรี (จำกัด)"},
"Firebase": {"type": "Google Cloud", "protocol": "HTTPS WebSocket", "cost": "Free tier"},
}
print("\nIoT Platforms:")
for name, info in platforms.items():
print(f" [{name}] {info['type']}")
print(f" Protocol: {info['protocol']} | Cost: {info['cost']}")
ระบบควบคุมและ Alert
# === Farm Control & Alert System ===
@dataclass
class AlertRule:
name: str
condition: str
action: str
priority: str
rules = [
AlertRule("ดินแห้ง", "soil_moisture < 30%", "เปิดปั๊มน้ำ + แจ้ง LINE", "สูง"),
AlertRule("อุณหภูมิสูง", "temperature > 38°C", "เปิดพัดลม + แจ้ง LINE", "สูง"),
AlertRule("ฝนตก", "rain_detected = true", "ปิดปั๊มน้ำ", "กลาง"),
AlertRule("pH ผิดปกติ", "pH < 5.5 OR pH > 7.5", "แจ้ง LINE + หยุดรดน้ำ", "สูง"),
AlertRule("แสงน้อย", "light < 1000 lux (กลางวัน)", "เปิดไฟเสริม", "ต่ำ"),
AlertRule("Sensor Offline", "no_data > 10 min", "แจ้ง LINE ตรวจสอบ", "สูง"),
]
print("Alert Rules:")
for r in rules:
print(f" [{r.priority}] {r.name}")
print(f" IF {r.condition} THEN {r.action}")
# Cost Savings
savings = {
"น้ำ": {"before": "100 ลบ. ม./เดือน", "after": "55 ลบ. ม./เดือน", "save": "45%"},
"ปุ๋ย": {"before": "50 กก./เดือน", "after": "35 กก./เดือน", "save": "30%"},
"แรงงาน": {"before": "8 ชม./วัน", "after": "2 ชม./วัน", "save": "75%"},
"ผลผลิต": {"before": "100 กก./ไร่", "after": "140 กก./ไร่", "save": "+40%"},
"ค่าไฟ": {"before": "3,000 บ./เดือน", "after": "2,000 บ./เดือน", "save": "33%"},
}
print(f"\n\nSmart Farm Savings:")
for item, data in savings.items():
print(f" [{item}] {data['before']} -> {data['after']} ({data['save']})")
เคล็ดลับ
- ESP32: ใช้ ESP32 ราคาถูก มี WiFi Bluetooth ในตัว
- MQTT: ใช้ MQTT Protocol ประหยัดแบตเตอรี่ เหมาะ IoT
- Solar: ใช้ Solar Panel สำหรับ Sensor Node ไม่ต้องเดินสาย
- Waterproof: เลือก Sensor กันน้ำ IP67 สำหรับใช้กลางแจ้ง
- Calibrate: Calibrate Sensor ทุก 3 เดือน ค่าเพี้ยนตามเวลา
การนำความรู้ไปประยุกต์ใช้งานจริง
แหล่งเรียนรู้ที่แนะนำ ได้แก่ Official Documentation ที่อัพเดทล่าสุดเสมอ Online Course จาก Coursera Udemy edX ช่อง YouTube คุณภาพทั้งไทยและอังกฤษ และ Community อย่าง Discord Reddit Stack Overflow ที่ช่วยแลกเปลี่ยนประสบการณ์กับนักพัฒนาทั่วโลก
IoT เกษตร คืออะไร
Internet of Things เกษตร Sensor Gateway Cloud ระบบรดน้ำอัตโนมัติ ลดต้นทุน เพิ่มผลผลิต ประหยัดน้ำ Smart Farm
Sensor ที่ใช้ในเกษตรมีอะไรบ้าง
Soil Moisture Temperature Humidity pH Light EC Rain Wind CO2 Camera GPS วัดสภาพดิน อากาศ น้ำ แสง
ระบบรดน้ำอัตโนมัติทำอย่างไร
Soil Moisture Sensor ESP32 Threshold ต่ำกว่า 40% เปิดวาล์ว สูงกว่า 70% ปิด Cloud Dashboard มือถือ ประหยัดน้ำ 30-50%
Cloud Platform สำหรับ IoT เกษตร ใช้อะไร
AWS IoT Core Enterprise ThingsBoard Open Source ฟรี Blynk Maker ง่าย Firebase Prototype NETPIE ไทย ตามขนาดฟาร์ม
สรุป
IoT เกษตร Smart Farm Sensor ESP32 MQTT Gateway Cloud ThingsBoard ระบบรดน้ำอัตโนมัติ Soil Moisture Dashboard Alert LINE ประหยัดน้ำ ลดต้นทุน เพิ่มผลผลิต Solar Calibrate
