低成本智能家居系统:树莓派/Orange Pi + MediaTek LinkIt + Azure云端
组件与物料清单
| 组件 | 数量 | 备注 |
|---|---|---|
| Raspberry Pi 2 Model B / Orange Pi | 1 | 本地服务器,$15级SBC即可 |
| XBee 模块 | 1 | 或任意低成本无线收发模块 |
| Arduino UNO | 1 | 配合传感器节点 |
云平台服务
- Microsoft Azure 存储服务(可选,本地存储亦可)
系统架构概述

本项目演示如何搭建超低成本智能家居数据采集系统:
- 传感器节点:各类传感器(温湿度、光照、气体、PIR、土壤湿度等)通过Arduino/MediaTek采集数据
- 无线传输:XBee或廉价433MHz/LoRa模块将数据汇聚到MediaTek LinkIt网关
- 边缘网关:MediaTek LinkIt封装JSON,通过WiFi POST至本地服务器
- 本地预处理服务器:$15 Orange Pi运行Node.js,接收数据、添加UTC时间戳、签名、过滤清洗
- 云端存储:本地服务器转发至Azure表存储(可选,也可仅本地存储)
数据模型:统一JSON格式
采用desired字段表示设备期望状态,便于前端同步:
单传感器示例
// 温湿度
{ "deviceid": "iot/myhome/livingroom/thermostat",
"desired": { "temperature": "24", "humidity": "80" },
"timeutc": "2015-11-23T00:41:48.824Z",
"signedBy": "localserver" }
// 麦克风声强
{ "deviceid": "iot/myhome/livingroom/mic",
"desired": { "intensity": "66" },
"timeutc": "...", "signedBy": "localserver" }
// 超声波测距
{ "deviceid": "iot/myhome/livingroom/obstacle",
"desired": { "proximity": "150" }, ... }
// PIR人体红外
{ "deviceid": "iot/myhome/livingroom/pir",
"desired": { "motion_start": "2500", "motion_end": "5345" }, ... }
// GPS定位
{ "deviceid": "iot/mydevice/location",
"desired": { "lat": "25.5", "long": "32.2" }, ... }
// 土壤湿度
{ "deviceid": "iot/myhome/garden/moisture",
"desired": { "moisture": "20" }, ... }
// 燃气泄漏
{ "deviceid": "iot/myhome/kitchen/gas",
"desired": { "leak": "No" }, ... }
// 光照强度
{ "deviceid": "iot/myhome/livingroom/light",
"desired": { "intensity": "60" }, ... }
房间级聚合示例
// 客厅聚合
{ "deviceid": "/iot/myhome/livingroom",
"desired": {
"temperature": "25", "humidity": "80",
"window": "open", "door": "closed",
"light": "80", "noise": "low"
}}
// 卧室聚合
{ "deviceid": "/iot/myhome/bedroom",
"desired": {
"temperature": "24", "humidity": "70",
"window": "closed", "door": "closed",
"light": "80", "ac": "on", "noise": "low"
}}
MediaTek LinkIt 网关代码
完整代码见GitHub仓库,核心逻辑:连接WiFi → 循环读取/模拟传感器 → 构建JSON → HTTP POST到本地服务器 → 处理超时重连。
// 关键片段:发送温度数据
void updateTemperature() {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
JsonObject& desired = root.createNestedObject("desired");
desired["temperature"] = String(random(20,40)); // 替换为真实读数
desired["humidity"] = String(random(60,80));
root["deviceid"] = "/iot/myhome/livingroom/thermostat";
root["timeutc"] = ""; // 由本地服务器填充
root["signedby"] = "";
String len = String(root.measureLength());
c.println("POST /iot/myhome/livingroom/thermostat HTTP/1.1");
c.println("Host: " SITE_URL);
c.println("Content-Type: application/json");
c.println("Cache-Control: no-cache");
c.print("Content-Length: "); c.println(len);
c.println("Connection: close"); c.println();
root.printTo(c);
// ... 等待响应超时处理
}
本地服务器
硬件:Orange Pi ($15) + Arch Linux ARM + Node.js
部署:
- 克隆GitHub仓库
iotapp npm install安装依赖- 修改
index.js中的云端API地址 - 运行
node app.js监听3000端口
Node.js 路由示例
router.post('/iot/myhome/livingroom/thermostat', function(req, res) {
var state = req.body;
state.timeutc = new Date();
state.signedby = "orangepi";
// 转发至Azure云端API
request.post({
url: 'https://your-azure-domain/api/iot/myhome/livingroom/thermostat',
json: true, body: state
}, function(err, resp, body) { ... });
res.send('Living room temperature recorded');
});
// ... 其他传感器路由同理
云端ASP.NET Core Web API
Visual Studio 2015创建ASP.NET 5项目,Entity Framework写入Azure表存储。每类传感器对应一个Controller,示例:
[Route("api/iot/myhome/livingroom/[Controller]")]
public class ThermoStatController : IoTController {
[HttpPost]
public IActionResult Post([FromBody]IotTemperature data) {
// 写入Azure Table Storage
data.PartitionKey = "thermostat";
data.RowKey = Guid.NewGuid().ToString();
table.Execute(TableOperation.Insert(data));
return Ok("Logged to Azure");
}
}
// IotTemperature 含 deviceid, timeutc, signedby, desired{temperature,humidity}
完整Controller列表:ThermoStat, PIR, Moisture, LivingRoom, Light, GPS, Gas, Distance, BedRoom。
原理图与接线



项目源码仓库
GitHub: iot-mediatek-linkIt - 含MediaTek接收端、Node.js本地服务器、ASP.NET Core云端API全套代码
总结
- 证明了$15 SBC足以胜任IoT边缘预处理
- JSON统一数据模型便于扩展与前端绑定
- 本地服务器可离线运行,云端同步可选
- 代码有重构空间,欢迎PR改进
制造工艺