esp8266-01s加人体热释红外探头做的手机远程报警器

材料:1,esp8266-01s一个或者esp8266miniD1一个。
2,人体热释红外感应探头一个
3,杜邦线三根
在这里插入图片描述

工作原理:人体热释红外探头检测到人体信号,通过esp8266-01s上传到巴法云,然后给微信发送报警信息,如果你在家门口按装一个报警器,当有人进入探头检测范围内(3/5米)。报警器通过esp8266给微信发送报警信息,从而实现远程报警。

源程序:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>

#define sensor 2 //红外传感器输入口
/******************************************************************************/
#define DEFAULT_STASSID “LP_360-wifi” //WIFI名称
#define DEFAULT_STAPSW “123456” //WIFI密码

String uid = “285293a244b640e5d7de1944ee684f”; // 用户私钥,巴法云控制台获取
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)
{
if(millis() - lastWiFiCheckTick > 1000)
{
lastWiFiCheckTick = millis();
startSTA();
}
}
}
void setup() {
pinMode(sensor, INPUT); // declare sensor as input
delay(1000);
Serial.begin(115200); //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”);
}
//=======================================================================[/mw_shl_code]

![graph TD;
A-->B;
B-->C;](https://img-blog.csdnimg.cn/20211004201551102.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTQ0MjE5OA==,size_16,color_FFFFFF,t_70)

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45442198/article/details/120607421
今日推荐