【转】ESP8266搭建简易安防系统(智能门磁)
ESP8266可谓一款无敌的神器,为什么说无敌呢,一方面是价格只有10块,另一方面模块内置的MCU性能很不错,还有不少IO口可用,用作物联网主机(arduino都省了)搓搓有余,可塑性很高啊。近期因LP大人要赴异地工作,特使用此款神器造了一个门磁,以防贼,防狼,防变态....
先用一个逻辑图来描述一下整个项目:
http://image.geek-workshop.com/forum/201609/26/165313pahn91ef1yj1nyjy.png
这会邀请主角:(外壳是用的一个LED驱动改的,size刚好合适我的nodemuc板)
这个开发板板集成了串口芯片,方便开发调试,也引出了所有的IO口。其实用ESP8266模块也是一样的,只是手头没有arduino板,索性就直接买块开发板回来,可以串口调试。
http://image.geek-workshop.com/forum/201609/26/165354jb6m6t36bxfmncs0.jpg
还有我们的得力小助手:干簧管传感器(这枚屌丝的作用是和门磁一样的,磁铁靠近就闭合)
这枚传感器有三个脚,分别为VCC GND OUT 。和人体红外模块一样使用的,判断是否有磁场输出高低电平。
http://image.geek-workshop.com/forum/201609/26/165414qyxgyx3za773a5ya.jpg
还有这枚屌丝的搭档:磁铁先生
http://image.geek-workshop.com/forum/201609/26/165429tzcgggdupuufywt3.jpg
连线非常简单,就三条线....模块VCC---3.3V 模块GND---GNDOUT----D5
供电方面,我是直接在洞洞板背面焊了一个AMS1117-5.0上去,通过DC座取电(9V输入),再输出到开发板的VIN口。
下面是安装好之后的全家福(磁铁自己找了一个小塑料盒装进去,方便固定在门边)
http://image.geek-workshop.com/forum/201609/26/165443xyggyg2bb82dng9y.jpg
有能力的同学可以自己搭建PHP端对上传的数据进行存储和处理,或者结合公众号开发
因为主动推送消息和硬件平台接入都要求认证过后的公众号,所以没有做这一部分的开发。
有已经认证过公众平台号的朋友可以尝试,可以做到微信用户无缝接入,具体参考:微信硬件平台应用场景
http://image.geek-workshop.com/forum/201609/26/165520ig0005yf2c8i5g4e.jpg
http://image.geek-workshop.com/forum/201609/26/165530gzrcaauqaauaqz03.jpg
这个是airkiss软件 可以通过它来配置模块的wifi信息。
http://image.geek-workshop.com/forum/201609/26/165543esm7sxlxtftx2hrt.jpg
相关链接:微信硬件平台
下面上代码:(编译环境arduino IDE 1.6.5)
程序PS:上电自动连接WIFI(读取上一次成功配置的SSID,PWD),20秒内链接失败则开启Smartconfig模式等待接收SSID和PWD信息。
连接WIFI成功后,每5S传输一次传感器状态,每10S传输一次握手信息(检测主机是否死机)
[*]#include <ESP8266WiFi.h>
[*]#define LED 2
[*]unsigned long wificonnet_time=20000;//wifi连接超时时间
[*]const char* ssid;
[*]const char* password;
[*]
[*]//因为是自己搭建的PHP端,所以这里的设置和其他物联网平台不太一样
[*]//如果是接入物联网平台,可按照平台标准去修改
[*]const char* host = "***";
[*]const char* controller = "***";
[*]const char* action = "***";
[*]const char* wdaction = "***";
[*]const char* Key = "***";
[*]
[*]unsigned long startPost,wdtime,seniortime;
[*]bool flagFirstPost = true;
[*]bool Firsthandshaking = true;
[*]int RS1_state = 0;
[*]int RS_1 = D5;//传感器端口
[*]//int RS_2 = 0;
[*]
[*]//智能配置Airkiss
[*]void smartConfig()
[*]{
[*]WiFi.mode(WIFI_STA);
[*]// Serial.println("\r\nWait for Smartconfig");
[*]WiFi.beginSmartConfig();
[*]while (1)
[*]{
[*] //Serial.print(".");
[*] digitalWrite(LED, 0);
[*] delay(500);
[*] digitalWrite(LED, 1);
[*] delay(500);
[*] if (WiFi.smartConfigDone())
[*] {
[*] //Serial.println("SmartConfig Success");
[*] //Serial.println(WiFi.SSID().c_str());
[*] //Serial.println(WiFi.psk().c_str());
[*] break;
[*] }
[*]}
[*]}
[*]
[*]void setup() {
[*]pinMode(LED, OUTPUT);
[*]pinMode(RS_1,INPUT);
[*]//pinMode(RS_2,INPUT);
[*]//Serial.begin(115200);
[*]digitalWrite(LED, 0);
[*]//Serial.println();
[*]//Serial.print("Connecting to ");
[*]//Serial.println(ssid);
[*]WiFi.begin(ssid, password);
[*]while (WiFi.status() != WL_CONNECTED) {
[*] unsigned long nowtime=millis();
[*] if(nowtime>wificonnet_time){
[*] smartConfig();
[*] delay(10);
[*] if(WiFi.smartConfigDone()){
[*]//Serial.println();
[*]//Serial.println();
[*]//Serial.print("Connecting to ");
[*]//Serial.println(ssid);
[*]
[*]WiFi.begin(ssid, password);
[*]
[*]while (WiFi.status() != WL_CONNECTED) {
[*] delay(500);
[*] //Serial.print(".");
[*]}
[*]
[*]//Serial.println("");
[*]//Serial.println("WiFi connected");
[*]//Serial.println("IP address: ");
[*]//Serial.println(WiFi.localIP());
[*]}
[*] }
[*] delay(500);
[*] //Serial.print(".");
[*]}
[*]//Serial.println("");
[*]//Serial.println("WiFi connected");
[*]//Serial.println("IP address: ");
[*]//Serial.println(WiFi.localIP());
[*]}
[*]
[*]
[*]
[*]void handshaking(){
[*]// Serial.print("handshaking to ");
[*]//Serial.println(host);
[*]
[*]// Use WiFiClient class to create TCP connections
[*]WiFiClient client;
[*]const int httpPort = 80;
[*]if (!client.connect(host, httpPort)) {
[*] //Serial.println("handshaking failed");
[*] return;
[*]}
[*]
[*]// We now create a URI for the request
[*]String url = "/af/index.php/Home/";
[*]url += controller;
[*]url += "/";
[*]url += wdaction;
[*]url += "?health=1";
[*]url += "&key=";
[*]url += Key;
[*]//Serial.print("Handshaking URL: ");
[*]//Serial.println(url);
[*]
[*]// This will send the request to the server
[*]client.print(String("GET ") + url + " HTTP/1.1\r\n" +
[*] "Host: " + host + "\r\n" +
[*] "Connection: close\r\n\r\n");
[*]delay(10);
[*]
[*]// Read all the lines of the reply from server and print them to Serial
[*]while(client.available()){
[*] String line = client.readStringUntil('\r');
[*] // Serial.print(line);
[*]}
[*]
[*]//Serial.println();
[*]//Serial.println("closing connection");
[*]}
[*]
[*]void RS1_up(){
[*]//Serial.print("connecting to ");
[*]//Serial.println(host);
[*]
[*]// Use WiFiClient class to create TCP connections
[*]WiFiClient client;
[*]const int httpPort = 80;
[*]if (!client.connect(host, httpPort)) {
[*] //Serial.println("connection failed");
[*] return;
[*]}
[*]
[*]// We now create a URI for the request
[*]String url = "/af/index.php/Home/";
[*]url += controller;
[*]url += "/";
[*]url += action;
[*]url += "?RS_id=1";
[*]url += "&RS_state=";
[*]url += RS1_state;
[*]url += "&key=";
[*]url += Key;
[*]//Serial.print("Requesting URL: ");
[*]//Serial.println(url);
[*]
[*]// This will send the request to the server
[*]client.print(String("GET ") + url + " HTTP/1.1\r\n" +
[*] "Host: " + host + "\r\n" +
[*] "Connection: close\r\n\r\n");
[*] delay(10);
[*]
[*]// Read all the lines of the reply from server and print them to Serial
[*]while(client.available()){
[*] String line = client.readStringUntil('\r');
[*] //Serial.print(line);
[*]}
[*]
[*]//Serial.println();
[*]//Serial.println("closing connection");
[*]}
[*]void loop() {
[*]if (millis()-startPost>5000 || flagFirstPost){
[*] RS1_up();
[*] startPost = millis();
[*] flagFirstPost = false;
[*] delay(10);
[*]}else if(millis()-wdtime>15000 || Firsthandshaking){
[*] handshaking();
[*] wdtime = millis();
[*] Firsthandshaking = false;
[*] delay(10);
[*]}else if(digitalRead(RS_1)==HIGH){
[*] RS1_state = 1;
[*] seniortime = millis();
[*] delay(10);
[*]}else if(digitalRead(RS_1)==LOW){
[*] if(millis()-seniortime>12000){
[*] RS1_state = 0;
[*] delay(10);
[*] }else{
[*] return;
[*] }
[*]}else{
[*] return;
[*]}
[*]}
复制代码
所有代码托管在:GitHub
PHP端已经整合了微信公众号类和阿里大于短信API
需要微信公众号一个(具有自定义菜单功能的)
需要注册阿里大于账号(有淘宝号可以直接登录),注册后要新建应用,新建短信签名,新建短信模板,用于短信提醒发送。
服务端基于和[微信公众平台类]快速开发。
Thinkphp框架下载:http://www.thinkphp.cn/
微信公众平台类下载:http://www.thinkphp.cn/extend/588.html
页:
[1]