mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-18 00:45:55 +08:00
sdk完善
This commit is contained in:
@@ -8,18 +8,14 @@
|
||||
|
||||
#include "ApConfig.h"
|
||||
|
||||
String randomName="wumei-device"+(String)random(1000);
|
||||
const char *ap_ssid =randomName.c_str();
|
||||
const char *ap_password = ""; //开放式网络
|
||||
|
||||
char sta_ssid[32] = {0};
|
||||
char sta_password[64] = {0};
|
||||
char sta_user_id[32] = {0};
|
||||
String randomName = "wumei-device" + (String)random(1000);
|
||||
const char *ap_ssid = randomName.c_str();
|
||||
//开放式网络,不设置密码
|
||||
const char *ap_password = "";
|
||||
|
||||
IPAddress local_IP(192, 168, 4, 1);
|
||||
IPAddress gateway(192, 168, 4, 1);
|
||||
IPAddress subnet(255, 255, 255, 0);
|
||||
|
||||
ESP8266WebServer server(80);
|
||||
|
||||
/**
|
||||
@@ -27,10 +23,11 @@ ESP8266WebServer server(80);
|
||||
*/
|
||||
void startApConfig()
|
||||
{
|
||||
ledStatus(true);
|
||||
WiFi.mode(WIFI_AP_STA);
|
||||
WiFi.softAPConfig(local_IP, gateway, subnet);
|
||||
WiFi.softAP(ap_ssid, ap_password);
|
||||
printMsg("已启动AP配网,IP地址:" + WiFi.softAPIP().toString()+", 热点名称:"+(String)ap_ssid);
|
||||
printMsg("已启动AP配网,IP地址:" + WiFi.softAPIP().toString() + ", 热点名称:" + (String)ap_ssid);
|
||||
// 启动web服务
|
||||
startWebServer();
|
||||
}
|
||||
@@ -40,7 +37,7 @@ void startApConfig()
|
||||
*/
|
||||
void startWebServer()
|
||||
{
|
||||
isApMode=true;
|
||||
isApMode = true;
|
||||
server.on("/status", HTTP_GET, handleStatus);
|
||||
server.on("/config", HTTP_POST, handleConfig);
|
||||
server.onNotFound(handleNotFound);
|
||||
@@ -52,25 +49,35 @@ void startWebServer()
|
||||
/**
|
||||
* 检测设备接口
|
||||
*/
|
||||
void handleStatus(){
|
||||
server.send(200, "text/plain;charset=utf-8", "AP配网已准备就绪");
|
||||
}
|
||||
|
||||
void handleStatus()
|
||||
{
|
||||
server.send(200, "text/plain;charset=utf-8", "AP配网已准备就绪");
|
||||
}
|
||||
|
||||
/**
|
||||
* AP配网接口
|
||||
*/
|
||||
void handleConfig()
|
||||
{
|
||||
printMsg("进入配网......");
|
||||
config_type config;
|
||||
// wifi名称、wifi密码、用户编号
|
||||
if (server.hasArg("SSID") && server.hasArg("password") && server.hasArg("userId"))
|
||||
{
|
||||
strcpy(sta_ssid, server.arg("SSID").c_str());
|
||||
strcpy(sta_password, server.arg("password").c_str());
|
||||
strcpy(sta_user_id, server.arg("userId").c_str());
|
||||
printMsg("收到WIFI名称:" + (String)sta_ssid);
|
||||
printMsg("收到WIFI密码:" + (String)sta_password);
|
||||
printMsg("收到用户编号:" + (String)sta_user_id);
|
||||
// 分配空间
|
||||
wifiSsid=(char *)malloc(32*sizeof(char));
|
||||
wifiPwd=(char *)malloc(64*sizeof(char));
|
||||
userId=(char *)malloc(16*sizeof(char));
|
||||
strcpy(config.stassid, server.arg("SSID").c_str());
|
||||
strcpy(wifiSsid, server.arg("SSID").c_str());
|
||||
strcpy(config.stapsw, server.arg("password").c_str());
|
||||
strcpy(wifiPwd, server.arg("password").c_str());
|
||||
strcpy(config.userId, server.arg("userId").c_str());
|
||||
strcpy(userId, server.arg("userId").c_str());
|
||||
|
||||
printMsg("收到WIFI名称:" + (String)config.stassid);
|
||||
printMsg("收到WIFI密码:" + (String)config.stapsw);
|
||||
printMsg("收到用户编号:" + (String)config.userId);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -81,22 +88,28 @@ void handleConfig()
|
||||
// 可选字段
|
||||
if (server.hasArg("deviceNum"))
|
||||
{
|
||||
deviceNum=(char *)malloc(32*sizeof(char));
|
||||
strcpy(config.deviceNum, server.arg("deviceNum").c_str());
|
||||
strcpy(deviceNum, server.arg("deviceNum").c_str());
|
||||
printMsg("收到设备编号:" + server.arg("deviceNum"));
|
||||
}
|
||||
if (server.hasArg("authCode"))
|
||||
{
|
||||
authCode=(char *)malloc(32*sizeof(char));
|
||||
strcpy(config.authCode, server.arg("authCode").c_str());
|
||||
strcpy(authCode, server.arg("authCode").c_str());
|
||||
printMsg("收到产品授权码:" + server.arg("authCode"));
|
||||
}
|
||||
if (server.hasArg("extra"))
|
||||
{
|
||||
printMsg("收到补充信息:" + server.arg("extra"));
|
||||
}
|
||||
}
|
||||
server.send(200, "text/plain;charset=utf-8", "设备已更新WIFI配置,开始连接WIFI...");
|
||||
|
||||
// 连接Wifi,关闭web服务
|
||||
isApMode=false;
|
||||
// 存储配置
|
||||
saveConfig(config);
|
||||
// 连接Wifi
|
||||
connectWifi();
|
||||
server.close();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,4 +131,3 @@ void handleNotFound()
|
||||
server.send(404, "text/plain;charset=utf-8", "请求的地址找不到或无法访问");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user