ESP12F Arduino 代码:连接 Wi-Fi 并发送企业微信消息
本示例代码展示如何使用 Arduino 编写代码,让 ESP12F 模块连接到名为'7-4' 的 Wi-Fi 网络,并使用 POST 方法访问企业微信 API 发送文本消息。
代码示例:
#include <ESP8266WiFi.h>
#include <HTTPClient.h>
const char* ssid = "7-4";
const char* password = "120712071207";
const char* accessToken = "ACCESS_TOKEN";
const char* apiUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
}
void loop() {
HTTPClient http;
// 设置请求头
http.begin(apiUrl);
http.addHeader("Content-Type", "application/json");
// 设置请求数据
String jsonData = "{\"touser\":\"@all\",\"toparty\":\"@all\",\"totag\":\"@all\",\"msgtype\":\"text\",\"agentid\":1000002,\"text\":{\"content\":\"有漏水请尽快处理\"},\"safe\":0,\"enable_id_trans\":0,\"enable_duplicate_check\":0,\"duplicate_check_interval\":1800}";
// 发送 POST 请求
int httpCode = http.POST(jsonData);
if (httpCode > 0) {
Serial.printf("HTTP Response Code: %d\n", httpCode);
String response = http.getString();
Serial.println(response);
} else {
Serial.printf("Error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
delay(10000); // 每 10 秒发送一次消息
}
说明:
- 将
ACCESS_TOKEN替换为实际的企业微信访问令牌。 - 修改代码中的
content字段以设置要发送的文本消息内容。 - 确保 ESP12F 模块已连接到互联网。
- 此代码仅供参考,具体实现可能需要根据实际情况进行调整。
原文地址: https://www.cveoy.top/t/topic/nlWT 著作权归作者所有。请勿转载和采集!