Arduino 门磁传感器:PHPoC WiFi Shield 经 IFTTT 推送 Messenger 实时告警
⚠️ 重要提示
IFTTT 已于 2023 年停止 Facebook Messenger 服务(因 Meta 政策变更),本项目原演示功能**不再可用**。文档保留供学习 Arduino + WiFi Shield + IFTTT Webhooks 通信架构参考,可替换为 Telegram、Email、Slack 等仍支持的 Action 服务。
项目架构
Arduino UNO + PHPoC WiFi Shield 监测门磁开关 (A0) 状态变化 → 触发 HTTPS GET 请求至 maker.ifttt.com Webhooks 端点 → IFTTT 触发下游 Action(原为发送 Messenger 消息)。
硬件清单
- Arduino UNO ×1
- PHPoC WiFi Shield for Arduino ×1 — 内置 SSL/TLS、PHP 解释器,简化 HTTPS 请求
- 磁性门窗传感器 (干簧管) ×1 — 一脚接 GND,一脚接 A0 (内部上拉)
软件与库
- Arduino IDE
- PHPoC 库 — 随 Shield 安装
- ezButton 库 — 按钮去抖动处理
- IFTTT 账号 — 创建 Webhooks Applet
IFTTT Applet 配置步骤(通用模板)
- Create Applet → If This → **Webhooks** → Receive a web request → Event Name:
door_open - Then That → 选择目标服务(如 **Telegram** → Send message、Email** → Send email、Slack** → Post to channel)
- 消息内容可嵌入
{{OccurredAt}}等 Ingredient - Finish → 获取 Webhooks Key:
https://ifttt.com/maker_webhooks→ Documentation
接线说明
- 门磁传感器:一线接 GND,一线接 A0
- PHPoC Shield 直接插在 UNO 顶部,无需额外接线
核心代码逻辑
#include <Phpoc.h>
#include <ezButton.h>
String IFTTT_WEBHOOKS_KEY = "你的_Key";
char server[] = "maker.ifttt.com";
PhpocClient client;
ezButton button(A0); // 内部上拉,低电平触发
void sendNotification() {
if (client.connectSSL(server, 443)) {
client.println("GET /trigger/door_open/with/key/" + IFTTT_WEBHOOKS_KEY + " HTTP/1.1");
client.println("Host: maker.ifttt.com");
client.println("Connection: close");
client.println();
// 读取响应并串口打印
while (client.connected()) {
if (client.available()) Serial.write(client.read());
}
client.stop();
}
}
void setup() {
Serial.begin(9600);
Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
button.setDebounceTime(100);
}
void loop() {
button.loop();
if (button.isPressed()) {
Serial.println("door is opened");
sendNotification();
delay(100);
} else if (button.isReleased()) {
Serial.println("door is closed");
}
}
关键技术点
- PHPoC Shield 优势:硬件级 SSL/TLS,无需在 2KB RAM 的 UNO 上实现加密协议栈,
connectSSL()一行搞定 HTTPS - ezButton 去抖:100 ms 去抖动,避免门磁震荡触发多次
- 事件驱动:仅在状态变化时发送请求,节省流量与电量
- 固定 IP 可选:代码示例展示
WiFi.config()设置静态 IP