创作者:damahua | 更新日期:2023-09-08 | 在线时长:1368天
改装电控锁 增加手机解锁 wifi接近解锁
esp8266 12f 3.3降压模块
库:ArduinoBIGIOT
#if defined ESP32
#include <WiFi.h>
#elif defined ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#else
#error "Only support espressif esp32/8266 chip"
#endif
#include "bigiot.h"
#define banben "210320" //版本
#define lock 5 //控制锁
#define led 2
#define data_ID "7436"
#include <ESP8266httpUpdate.h>
int a = 0;
int b = 0;
int c = 0;
const char *id = "8502";
const char *apikey = "b46a922d3";
const char *usrkey = "456819f2a7";
const char *apssid = "x-x";
const char *appassword = "19890123";
unsigned long previousMillis = 0;
const long interval = 60000; //延时
bool blinkFlag;
ADC_MODE(ADC_VCC);
WiFiClient client;
BIGIOT bigiot(client);
ESP8266WiFiMulti wifiMulti;
//portal认证监听53端口
const byte DNS_PORT = 53;
IPAddress apIP(192, 168, 4, 1);
DNSServer dnsServer;
ESP8266WebServer webServer(80);
//portal认证页面
String htmlPage = ""
"<html>\n"
"<meta content=\"width=device-width,user-scalable=no\" name=\"viewport\">\n"
"<style>\n"
".b{background-color:#008CBA;border:none;color:white;padding:15px 32px;text-align:center;text-decoration:none;display:inline-block;font-size:16px;margin:4px 2px;}\n"
"</style>\n"
"</head>\n"
"<body>"
"<a href=\"up\" class=\"b\" target=\"h\">updata</a>\n"
"<a href=\"open\" class=\"b\" target=\"h\">open</a><br>\n"
"<iframe name=\"h\" width=\"300\" height=\"100\" ></iframe>\n"
"</body>\n"
"</html>"
"\r\n";
WiFiEventHandler probeRequestPrintHandler;//实例化WIFI事件对象
void eventCallback(const int devid, const int comid, const char *comstr, const char *slave)
{ Serial.println(comstr);
if (comstr == "play") {
digitalWrite(lock, HIGH);
bigiot.upload(data_ID, "已开锁");
delay(900);
digitalWrite(lock, LOW);
} else if (comstr == "up") {
Toupdate(1);
} else if (comstr == "v") {
bigiot.upload(data_ID, banben);
} else if (comstr == "offOn") {
digitalWrite(led, !digitalRead(led));
} else if (comstr == "test") {
}
}
//
void disconnectCallback(BIGIOT & obj)
{ digitalWrite(2, LOW);
Serial.println("offline");
}
void connectCallback(BIGIOT & obj)
{ digitalWrite(2, HIGH);
Serial.println("online");
delay(500);
bigiot.upload(data_ID, banben);//发送版本
}
//===============================================
void setup() {
Serial.begin(115200);
delay(100);
Serial.println("-");
Serial.println("set>pin");
pinMode(led, OUTPUT);
pinMode(lock, OUTPUT);
pinMode(hwpin, INPUT);
digitalWrite(led, LOW);
digitalWrite(lock, LOW);
ESP.wdtEnable(WDTO_8S); //打开看门狗 8秒重启
Serial.println("set>wifi");
WiFi.mode(WIFI_AP_STA);//设置模式为AP+STA
WiFi.softAP(apssid, appassword); //信道1 隐藏ssid0 1
WiFi.setOutputPower(1);//降低ap发射功率
wifiMulti.addAP("####", "a7261039");
wifiMulti.addAP("chen", "15059393360");
wifiMulti.addAP("ChinaNet-jluI", "ibpli7tp");
wifi_set_sleep_type(MODEM_SLEEP_T);//休眠模式
while (wifiMulti.run() != WL_CONNECTED) {
Serial.print(">");
delay(500);
}
//==================================
bigiot.eventAttach(eventCallback);
bigiot.disconnectAttack(disconnectCallback);
bigiot.connectAttack(connectCallback);
bigiot.login(id, apikey, usrkey);
probeRequestPrintHandler = WiFi.onSoftAPModeProbeRequestReceived(&onProbeRequestPrint); //AP模式下收到探针请求时
// 吧所有的dns请求都转到apip
dnsServer.start(DNS_PORT, "*", apIP);
// 给所有请求都回复认证页面
webServer.onNotFound([]() {
webServer.send(200, "text/html", htmlPage);
});
webServer.on("/open", []() {
c = 1;//开锁标记
digitalWrite(led, LOW);
previousMillis = millis();
webServer.send(200, "text/plain", ">>>OK!");
});
webServer.on("/up", []() {
Toupdate(0);
});
webServer.begin();
Serial.println("set>end");
digitalWrite(led, HIGH);
}
void onProbeRequestPrint(const WiFiEventSoftAPModeProbeRequestReceived & evt) { //wifi探针
if (a == 0 && evt.mac[0] == 0x4c && evt.mac[1] == 0x49 && evt.mac[2] == 0xe3 && evt.mac[3] == 0xc8 && evt.rssi > -55) {
a = 1;//延时标记
c = 1;//开锁标记
previousMillis = millis();
digitalWrite(led, LOW);
}
}
void Toupdate(int u) { //更新
ESP.wdtDisable();//关闭看门狗
digitalWrite(led, LOW);
String url;
if (u == 1) {
url = "http://6.666250.xyz/lock.ino.bin";
} else {
url = "http://192.168.4.2/lock.ino.bin";
}
Serial.println("正在更新...");
bigiot.upload(data_ID, "正在更新...");
t_httpUpdate_return ret = ESPhttpUpdate.update(url);
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.println("升级失败");
bigiot.upload(data_ID, "升级失败");
Toupdate(0);
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("固件无更新,无需升级");
bigiot.upload(data_ID, "固件无更新,无需升级");
break;
case HTTP_UPDATE_OK:
Serial.println("升级成功");
bigiot.upload(data_ID, "升级成功");
break;
default:
Serial.println("未知错误");
bigiot.upload(data_ID, "未知错误");
break;
}
}
void loop()
{
ESP.wdtFeed();//喂狗
//监听客户端请求
dnsServer.processNextRequest();
webServer.handleClient();
if (wifiMulti.run() == WL_CONNECTED) {
bigiot.handle();
} else {
digitalWrite(led, HIGH);
}
//延时
if (a == 1) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
a = 0;
digitalWrite(led, HIGH);
}
}
//
if (c == 1) {
c = 0;
digitalWrite(lock, HIGH);
delay(700);
digitalWrite(lock, LOW);
Serial.println("---开锁---");
}
delay(50);
}