材料准备
ESP8266一块
手机一块
第一下载ESP8266示例(arduino ide编程开发)
下载地址:http://www.cloud.bemfa.com/zip/be_wechat.zip
本demo 是利用arduino IDE开发,开发板:NodeMCU,关于arduino IDE 的ESP8266环境配置论坛有很多了,或者可参考:环境配置: http://bbs.bemfa.com/6
需要修改的地方:
/******************************************************************************/ #define DEFAULT_STASSID "HTC" //WIFI名称 #define DEFAULT_STAPSW "abc123456" //WIFI密码 String uid = "f49412ea7657d8f738cafe29bc245c14"; // 用户私钥,巴法云控制台获取 String type = "1"; // 1表示是预警消息,默认即可 String device = "人体红外传感器设备"; // 设备名称,可随意更改 String msg = "检测到班主任已站在窗户边,立即放下手机假装学习"; //发送的消息,可随意更改 String msg2 = "小样"; //消息备注,可为空,可随意更改 int delaytime = 0; //为了防止被设备“骚扰”,可设置贤者时间,单位是秒,如果设置了该值,在该时间内不会发消息到微信,设置为0立即推送。 String ApiUrl = "http://ai.bemfa.com/api/wechat/v1/"; //默认 api 网址 /******************************************************************************/
用户私钥可以巴法云控制台 获取,微信扫码登陆后,即可在巴法创客云控制台获取。
邮箱注册的用户,需要在控制台点击“绑定微信”,进行绑定,不然没法推送消息。
登陆完成后,可在控制台看到自己的**私钥UID **,如下所示:
示例程序讲解
示例代码是检测D7引脚,当有高电平时,调用发送预警信息的函数,发送预警信息。
大家玩的时候,想要发送信息可以自定义更改,可以阈值报警,消息提醒等,脑洞自由发挥。
/* * 微信通知提醒 * 2019-10-17 * QQ 1217882800 */ #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266HTTPClient.h> #define sensor D7 //红外传感器输入口 /******************************************************************************/ #define DEFAULT_STASSID "HTC" //WIFI名称 #define DEFAULT_STAPSW "abc123456" //WIFI密码 String uid = "f49412ea7657d8f738cafe29bc245c14"; // 用户私钥,巴法云控制台获取 String type = "1"; // 1表示是预警消息,默认即可 String device = "人体红外传感器设备"; // 设备名称 String msg = "检测到班主任已站在窗户边,立即放下手机假装学习"; //发送的消息 String msg2 = "小样"; //消息备注,可为空 int delaytime = 0; //为了防止被设备“骚扰”,可设置贤者时间,单位是秒,如果设置了该值,在该时间内不会发消息到微信,设置为0立即推送。 String ApiUrl = "http://ai.bemfa.com/api/wechat/v1/"; //默认 api 网址 /******************************************************************************/ static uint32_t lastWiFiCheckTick = 0; //======================================================================= // WIFI重新连接函数 //======================================================================= void startSTA(){ WiFi.disconnect(); WiFi.mode(WIFI_STA); WiFi.begin(DEFAULT_STASSID, DEFAULT_STAPSW); } //======================================================================= // WIFI状态检测函数,如果WIFI断开自动重连 //======================================================================= void doWiFiTick(){ if ( WiFi.status() != WL_CONNECTED ) { //未连接1s重连 if (millis() - lastWiFiCheckTick > 1000) { lastWiFiCheckTick = millis(); startSTA(); } } } //======================================================================= // 初始化 //======================================================================= void setup() { pinMode(sensor, INPUT); // declare sensor as input delay(1000); Serial.begin(115200); WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect) delay(1000); WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi hotspot WiFi.begin(DEFAULT_STASSID, DEFAULT_STAPSW); //Connect to your WiFi router Serial.println(""); Serial.print("Connecting"); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } //If connection successful show IP address in serial monitor Serial.println(""); Serial.print("Connected to "); Serial.println(DEFAULT_STASSID); Serial.print("IP address: "); Serial.println(WiFi.localIP()); //IP address assigned to your ESP } //======================================================================= // 主循环 //======================================================================= void loop() { doWiFiTick(); long state = digitalRead(sensor); if(state == HIGH) { Serial.println("people here"); doHttpStick();//在想推送消息的地方执行推送函数即可 delay(1000); } else { Serial.println("no people"); delay(1000); } } //******微信消息推送函数********// void doHttpStick(){ //微信消息推送函数 HTTPClient http; //Declare object of class HTTPClient String postData; //Post Data postData = "uid="+uid+"&type=" + type +"&time="+delaytime+"&device="+device+"&msg="+msg+"&msg2="+msg2; http.begin(ApiUrl); //Specify request destination http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header int httpCode = http.POST(postData); //Send the request String payload = http.getString(); //Get the response payload Serial.println(httpCode); //Print HTTP return code Serial.println(payload); //Print request response payload http.end(); //Close connection Serial.println("send success"); } //=======================================================================