ESP32接Pushover:传感器报警推送到手机

2026-11-24 13:59:17 42阅读 所属项目:ESP32 开发板

ESP32与Pushover推送通知

做监控类项目最憋屈的时刻:传感器数据都在手上,人却不在现场。Telegram、邮件各有各的折腾,这里介绍一个更省事的通道——Pushover,一个专门做消息推送的付费服务(有 7 天试用,一次性买断 5 美元),API 极简,一条 POST 就能把消息送到 iOS/安卓手机。这篇实测 ESP32 + ArduinoJson 组装 JSON、走 HTTPS 提交推送请求的完整流程。

Pushover创建应用页面

准备工作:拿到两个 Token

Pushover 需要两个凭证:用户 Key(user key,标识你这个人)和应用 API Token(标识哪个程序在发消息)。注册登录后进控制台首页就能看到 user key;然后点 Create an Application/API Token,填应用名和用途,创建后得到一串 30 位的 API Token。注意 API Token 只能发消息,别和 user key 混用。

Pushover应用API Token页面

组装 JSON:必填三项,可选项随需

推送接口是 https://api.pushover.net/1/messages.json,接受 POST 的 JSON。必填字段只有 token(API Token)、user(user key)、message(正文),其余都是增强项:title 是通知标题,sound 选提示音,priority 控制打扰级别(2 为需确认的紧急推送,会反复响铃直到你点掉)。用 ArduinoJson 的 StaticJsonDocument 组装即可:

StaticJsonDocument<512> notification;
notification["token"] = apiToken;      //required
notification["user"] = userToken;      //required
notification["message"] = "Hello from ESP32"; //required
notification["title"] = "ESP32 Notification"; //optional
notification["sound"] = "cosmic";      //optional

HTTPS 与证书校验

接口是 HTTPS,ESP32 上用 WiFiClientSecure 并通过 setCACert() 载入根证书做校验——别图省事用 setInsecure() 跳过校验,那等于把 Token 裸奔在公网。Pushover 用的是 DigiCert 的 Global Root CA,证书串比较长,直接整段贴进代码:

const char *PUSHOVER_ROOT_CA = "-----BEGIN CERTIFICATE-----\n"
"MIIDjjCCAnagAwIBAgIQAzrx5qcRqaC7KGSxHQn65TANBgkqhkiG9w0BAQsFADBh\n"
"(中间内容太长,完整证书串见文末源码仓库,替换自 Pushover 官方示例)\n"
"-----END CERTIFICATE-----\n";

WiFiClientSecure client;
client.setCACert(PUSHOVER_ROOT_CA);
HTTPClient https;
https.begin(client, "https://api.pushover.net/1/messages.json");
https.addHeader("Content-Type", "application/json");

完整流程代码

把连 Wi-Fi、组 JSON、发请求串起来,完整主体如下(凭证换成自己的,证书串用上面 DigiCert G2 的完整版本):

#include "WiFi.h"
#include "HTTPClient.h"
#include "WiFiClientSecure.h"
#include "ArduinoJson.h"

const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

const char* apiToken = "API_TOKEN";
const char* userToken = "USER_TOKEN";

const char* pushoverApiEndpoint = "https://api.pushover.net/1/messages.json";

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  StaticJsonDocument<512> notification;
  notification["token"] = apiToken;
  notification["user"] = userToken;
  notification["message"] = "Hello from ESP32";
  notification["title"] = "ESP32 Notification";
  notification["sound"] = "cosmic";

  String jsonStringNotification;
  serializeJson(notification, jsonStringNotification);

  WiFiClientSecure client;
  client.setCACert(PUSHOVER_ROOT_CA);
  HTTPClient https;
  https.begin(client, pushoverApiEndpoint);
  https.addHeader("Content-Type", "application/json");

  int httpResponseCode = https.POST(jsonStringNotification);
  if (httpResponseCode > 0) {
    Serial.printf("HTTP response code: %d\n", httpResponseCode);
    Serial.println(https.getString());
  } else {
    Serial.printf("HTTP response code: %d\n", httpResponseCode);
  }
  https.end();
}

void loop() {
  delay(10000);
}

烧录后打开串口监视器,返回 HTTP 200 且响应里 status 为 1 就说明推送成功:

串口监视器返回推送结果

手机端几秒内就能收到通知:

手机收到的Pushover通知

实用建议

  • 免费额度:试用到期后必须一次性买断(5 美元/平台),不限消息条数,比订阅制省心;
  • priority 用法:日常数据汇报用 0,仅报警类用 2 并配 retry/expire 参数,别滥用;
  • JSON 尺寸:StaticJsonDocument 512 字节够单条通知,消息内容动态拼接时注意留余量;
  • 频率控制:loop 里别无脑连发,配定时器或事件触发,Pushover 对滥用账号有限制。

这个通道最大的好处是"零服务端":不需要自建服务器、不需要 Bot 注册流程,ESP32 直连推送。下一篇我们看一块 WiFi 和 LoRa 一体的开发板——Heltec WiFi LoRa 32 V3。


作者:工位上的小周
链接:https://www.aiddithome.com/p/568b48318d8ef.html
本作版权归作者所有,未经允许请勿转载。若存在违规行为,您可以点击 “举报”。


登录 后发表评论
4条评论
阿强焊点总飞
1楼 · 2026-11-24 13:59:36

setInsecure跳过校验那个坑必须再强调一遍,之前见过有人Token被盗刷,CA证书校验多几行代码值得。

调参到凌晨
2楼 · 2026-11-24 13:59:36

priority 2的retry和expire参数记得单位是秒且retry最低60,我第一次用直接被API拒绝了。

刷视频进来围观
3楼 · 2026-11-24 13:59:36

正在做一个冰箱门报警器,温度超阈值就推手机,这篇的JSON组装部分直接可以抄作业了。

电烙铁爱好者
4楼 · 2026-11-24 13:59:35

Pushover买断制这点确实香,我微信推送方案换了三茬都因为接口变动失效,这个服务十年没换过地址。