Linux系统基础环境配置
首先选择适合嵌入式场景或服务器场景的Linux发行版,智慧农业边缘节点通常选择Ubuntu Core、Debian lightweight版本,云端服务器可选择Ubuntu Server或CentOS Stream。安装完成后先更新系统软件源,执行以下命令:
# 更新软件源 sudo apt update # 升级已安装软件包 sudo apt upgrade -y # 安装基础工具 sudo apt install -y vim curl wget git net-tools

用户权限与远程访问配置
为物联网开发创建专用用户,避免使用root用户直接操作,降低系统安全风险:
# 创建智慧农业开发专用用户 sudo useradd -m -s /bin/bash agri_iot_user # 设置用户密码 sudo passwd agri_iot_user # 赋予用户sudo权限 sudo usermod -aG sudo agri_iot_user
如果需要远程管理设备,配置SSH服务并修改默认端口提升安全性:
# 安装SSH服务 sudo apt install -y openssh-server # 修改SSH配置文件 sudo vim /etc/ssh/sshd_config # 将Port 22修改为自定义端口,例如Port 2222,同时设置PermitRootLogin no禁止root远程登录 # 重启SSH服务 sudo systemctl restart sshd # 设置SSH服务开机自启 sudo systemctl enable sshd
农业物联网开发依赖环境安装
编程语言与运行环境
智慧农业开发常用Python、C/C++作为开发语言,优先安装对应环境:
# 安装Python3及开发依赖 sudo apt install -y python3 python3-pip python3-dev # 安装C/C++编译工具链 sudo apt install -y gcc g++ make cmake # 安装Node.js(如需开发前端可视化界面) curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - sudo apt install -y nodejs
物联网通信协议支持
农业物联网场景常用MQTT、CoAP、HTTP等协议传输传感器数据,需要安装对应服务与客户端工具:
- MQTT协议:安装Mosquitto作为MQTT broker,用于处理传感器上报数据
- CoAP协议:安装libcoap开发库,支持低功耗传感器的CoAP通信
- HTTP协议:安装Nginx作为反向代理,对接云端数据接口
# 安装Mosquitto MQTT服务 sudo apt install -y mosquitto mosquitto-clients # 启动Mosquitto服务并设置开机自启 sudo systemctl start mosquitto sudo systemctl enable mosquitto # 安装CoAP开发库 sudo apt install -y libcoap-dev # 安装Nginx sudo apt install -y nginx sudo systemctl start nginx sudo systemctl enable nginx
传感器与边缘计算相关配置
硬件接口支持
农业物联网设备常通过GPIO、I2C、SPI、UART等接口连接传感器,需要开启Linux系统对应接口支持:
# 开启I2C接口(以树莓派为例,其他嵌入式设备参考对应手册) sudo raspi-config # 进入Interface Options,开启I2C、SPI、UART接口 # 安装I2C工具 sudo apt install -y i2c-tools # 检测I2C设备 i2cdetect -y 1
边缘计算框架部署
智慧农业需要在边缘节点处理传感器数据,过滤无效数据后再上传云端,可部署轻量级边缘计算框架:
# 安装Docker(用于部署边缘计算容器) sudo apt install -y docker.io sudo systemctl start docker sudo systemctl enable docker # 将开发用户加入docker组,避免每次执行docker命令加sudo sudo usermod -aG docker agri_iot_user # 部署轻量级边缘计算框架Kuiper docker run -d --name kuiper -p 9081:9081 emqx/kuiper:latest
数据存储与监控配置
农业物联网会产生大量时序传感器数据,需要配置对应的存储与监控工具:
# 安装时序数据库InfluxDB,用于存储传感器时序数据 sudo apt install -y influxdb sudo systemctl start influxdb sudo systemctl enable influxdb # 安装监控工具Prometheus,监控设备运行状态 sudo apt install -y prometheus sudo systemctl start prometheus sudo systemctl enable prometheus
配置验证与测试
完成所有配置后,可通过以下步骤验证环境是否正常:
- 测试MQTT服务:使用mosquitto_pub发布消息,mosquitto_sub订阅消息,验证通信正常
- 测试传感器读取:连接温湿度传感器到I2C接口,运行Python读取脚本验证数据获取正常
- 测试边缘计算:向Kuiper发送模拟传感器数据,验证规则处理功能正常
# 模拟读取DHT11温湿度传感器的Python示例
import Adafruit_DHT
import time
sensor = Adafruit_DHT.DHT11
pin = 4 # GPIO4引脚
while True:
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
print(f"温度: {temperature}℃,湿度: {humidity}%")
else:
print("读取传感器数据失败")
time.sleep(5)