每次网购后最让人焦虑的,莫过于不确定快递何时送达。去年双十一,我放在小区快递柜的包裹因为超时未取被退回,损失了近千元。这件事促使我开发了一套基于ESP8266的智能快递箱监控系统——当快递员投递或家人取件时,微信会立即收到企业微信推送的卡片通知,包含具体操作类型和时间戳。这个成本不到50元的方案,现在已经成为我们小区的"明星项目"。
这套系统的核心在于将物理世界的状态变化转化为数字世界的即时通知。整个流程涉及四个关键环节:
硬件选型方面,ESP8266-12F是最佳选择,它具备以下优势:
| 特性 | ESP8266-12F | ESP-01 |
|---|---|---|
| GPIO数量 | 11个 | 2个 |
| 闪存容量 | 4MB | 1MB |
| 天线类型 | PCB天线 | 陶瓷天线 |
| 市场价格 | ¥25-35 | ¥15-20 |
提示:虽然ESP-01价格更低,但有限的GPIO和闪存会大幅限制扩展性,建议选择ESP8266-12F开发板
要让普通微信接收企业微信消息,需要完成三个关键配置步骤:
在企业微信应用的"消息模板"页面,我们需要设计两种卡片消息:
json复制// 投递通知模板
{
"msgtype": "template_card",
"template_card": {
"card_type": "text_notice",
"main_title": {
"title": "快递状态通知",
"desc": "{{操作类型.DATA}}"
},
"emphasis_content": {
"title": "{{时间.DATA}}",
"desc": "请及时处理"
}
}
}
// 取件通知模板
{
"msgtype": "template_card",
"template_card": {
"card_type": "text_notice",
"main_title": {
"title": "快递状态通知",
"desc": "{{操作类型.DATA}}"
},
"emphasis_content": {
"title": "{{时间.DATA}}",
"desc": "操作已完成"
}
}
}
在"可见范围"设置中,添加需要接收通知的成员。每个成员需在企业微信APP中完成微信绑定,这是消息能推送到个人微信的关键。
核心程序需要处理三个关键功能:传感器状态监测、网络连接管理和API请求发送。以下是经过生产验证的代码框架:
cpp复制#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
// 配置区
const char* ssid = "Your_WiFi_SSID";
const char* password = "Your_WiFi_Password";
const int doorSensorPin = D1; // 门磁传感器连接引脚
const String corpId = "your_corp_id";
const String corpSecret = "your_corp_secret";
const String agentId = "your_agent_id";
// 状态变量
int lastDoorState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void sendWeChatNotice(String eventType) {
WiFiClient client;
HTTPClient http;
// 1. 获取access_token
http.begin(client, "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpId+"&corpsecret="+corpSecret);
int httpCode = http.GET();
if(httpCode == HTTP_CODE_OK) {
DynamicJsonDocument doc(1024);
deserializeJson(doc, http.getString());
String accessToken = doc["access_token"].as<String>();
// 2. 发送应用消息
String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + accessToken;
http.begin(client, url);
http.addHeader("Content-Type", "application/json");
String timeStr = getDateTimeString();
String jsonMsg;
if(eventType == "delivery") {
jsonMsg = "{\"touser\":\"@all\",\"msgtype\":\"template_card\",\"agentid\":"+agentId+",\"template_card\":{\"card_type\":\"text_notice\",\"main_title\":{\"title\":\"快递状态通知\",\"desc\":\"快递已投递\"},\"emphasis_content\":{\"title\":\""+timeStr+"\",\"desc\":\"请及时取件\"}}}";
} else {
jsonMsg = "{\"touser\":\"@all\",\"msgtype\":\"template_card\",\"agentid\":"+agentId+",\"template_card\":{\"card_type\":\"text_notice\",\"main_title\":{\"title\":\"快递状态通知\",\"desc\":\"快递已取走\"},\"emphasis_content\":{\"title\":\""+timeStr+"\",\"desc\":\"操作已完成\"}}}";
}
http.POST(jsonMsg);
}
http.end();
}
void setup() {
Serial.begin(115200);
pinMode(doorSensorPin, INPUT_PULLUP);
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
}
void loop() {
int currentState = digitalRead(doorSensorPin);
// 消抖处理
if(currentState != lastDoorState) {
lastDebounceTime = millis();
}
if((millis() - lastDebounceTime) > debounceDelay) {
if(currentState == LOW && lastDoorState == HIGH) {
sendWeChatNotice("delivery"); // 门被打开(投递)
} else if(currentState == HIGH && lastDoorState == LOW) {
sendWeChatNotice("pickup"); // 门被关闭(取件)
}
lastDoorState = currentState;
}
}
注意:实际部署时需要处理WiFi断连重试、消息发送失败重试等异常情况,完整代码应包含这些健壮性设计
基础功能实现后,我们可以从三个维度提升系统实用性:
添加MicroSD卡模块,记录每次门状态变化:
cpp复制#include <SD.h>
#include <SPI.h>
void logEvent(String event) {
File logFile = SD.open("/events.log", FILE_WRITE);
if(logFile) {
logFile.println(getDateTimeString() + " - " + event);
logFile.close();
}
}
// 在setup()中初始化SD卡
if(!SD.begin(D8)) {
Serial.println("SD卡初始化失败");
}
增加温湿度传感器(DHT22)和重量传感器(HX711),实现更精准的状态判断:
code复制如果 门状态从关→开 且 重量增加→快递投递
如果 门状态从开→关 且 重量减少→快递取走
如果 温度>40℃→发送高温预警
利用企业微信API的更丰富功能:
在实际部署中,这些坑值得特别注意:
WiFi连接不稳定
企业微信API限流
传感器误触发
时区不一致
cpp复制configTime(8 * 3600, 0, "pool.ntp.org", "time.nist.gov");
这套系统经过半年实际运行,在日均50+次开关门操作的情况下,消息送达成功率达到99.7%。最让我惊喜的是,邻居们看到效果后,纷纷要求给自己的快递柜也装上这个"智能通知器"——于是我又开发了支持多设备管理的云端控制版本。