https://item.taobao.com/item.htm?spm=a230r.1.14.20.eYblO3&id=521945102409&ns=1&abbucket=7#detail
手机一键配置esp8266上网,然后发送命令。
1 esp8266烧录代码
代码逐步添加,方便以后回看
1.1 一键自动配置连WIFI+固定IP
#include <ESP8266WiFi.h>
#define LED 2
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.printf("SSID:%s\r\n", WiFi.SSID().c_str());// WIFI 名 Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP()); // 分配的动态地址&自己设置的静态地址 Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
break;
}
}
}
void setwifi(){ }
void setup()
{
Serial.begin(115200);
Serial.println("Start module");
pinMode(LED, OUTPUT);
digitalWrite(LED, 0);
setwifi();
smartConfig(); IPAddress staticIP(192,168,1,22);
IPAddress gateway(192,168,1,22);
IPAddress subnet(255,255,255,0);
WiFi.config(staticIP, gateway, subnet);
Serial.print("Connected, IP change address: ");
Serial.println(WiFi.localIP()); // 分配的动态地址&自己设置的静态地址
}
void loop()
{
// delay(100);
// Serial.println("Start module");
}
1.2 添加服务端 端口80
#include <ESP8266WiFi.h>
#define LED 2 WiFiServer server(80); 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.printf("SSID:%s\r\n", WiFi.SSID().c_str());// WIFI 名 Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP()); // 分配的动态地址&自己设置的静态地址 Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
break;
}
}
}
void setwifi(){ }
void setup()
{
Serial.begin(115200);
Serial.println("Start module");
pinMode(LED, OUTPUT);
digitalWrite(LED, 0);
setwifi();
smartConfig(); IPAddress staticIP(192,168,1,22);
IPAddress gateway(192,168,1,22);
IPAddress subnet(255,255,255,0);
WiFi.config(staticIP, gateway, subnet);
Serial.print("Connected, IP change address: ");
Serial.println(WiFi.localIP()); // 分配的动态地址&自己设置的静态地址 server.begin();
Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str());
}
void loop()
{
WiFiClient client = server.available();
if (client)
{
Serial.println("\n[Client connected]");
while (client.connected())
{
// read line by line what the client (web browser) is requesting
if (client.available())
{
String line = client.readStringUntil('\r');
Serial.print(line);
// wait for end of client's request, that is marked with an empty line
if (line.length() == 1 && line[0] == '\n')
{
// client.println(prepareHtmlPage());
break;
}
}
}
delay(1); // give the web browser time to receive the data
// close the connection:
client.stop();
Serial.println("[Client disonnected]");
} }
1.3 添加网页,游览器输入IP 返回网页
#include <ESP8266WiFi.h>
#define LED 2 WiFiServer server(80); 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.printf("SSID:%s\r\n", WiFi.SSID().c_str());// WIFI 名 Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP()); // 分配的动态地址&自己设置的静态地址 Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
break;
}
}
} // prepare a web page to be send to a client (web browser)
String prepareHtmlPage()
{
String htmlPage =
String("HTTP/1.1 200 OK\r\n") +
"Content-Type: text/html\r\n" +
"Connection: close\r\n" + // the connection will be closed after completion of the response
"Refresh: 5\r\n" + // refresh the page automatically every 5 sec
"\r\n" +
"<!DOCTYPE HTML>" +
"<html>" +
"Analog input: " + String(analogRead(A0)) +
"</html>" +
"\r\n";
return htmlPage;
}
void setwifi(){ }
void setup()
{
Serial.begin(115200);
Serial.println("Start module");
pinMode(LED, OUTPUT);
digitalWrite(LED, 0);
setwifi();
smartConfig(); IPAddress staticIP(192,168,1,22);
IPAddress gateway(192,168,1,22);
IPAddress subnet(255,255,255,0);
WiFi.config(staticIP, gateway, subnet);
Serial.print("Connected, IP change address: ");
Serial.println(WiFi.localIP()); // 分配的动态地址&自己设置的静态地址 server.begin();
Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str());
}
void loop()
{
WiFiClient client = server.available();
if (client)
{
Serial.println("\n[Client connected]");
while (client.connected())
{
// read line by line what the client (web browser) is requesting
if (client.available())
{
String line = client.readStringUntil('\r');
Serial.print(line);
// wait for end of client's request, that is marked with an empty line
if (line.length() == 1 && line[0] == '\n')
{
client.println(prepareHtmlPage());
break;
}
}
}
delay(1); // give the web browser time to receive the data
// close the connection:
client.stop();
Serial.println("[Client disonnected]");
} }
1.4 添加 串口回传数据 电脑串口-esp-client-WIFI路由器-手机-sever
电脑串口 发送1
手机收到 49 (ASCLL码)
#include <ESP8266WiFi.h>
#define LED 2 WiFiServer server(80); 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.printf("SSID:%s\r\n", WiFi.SSID().c_str());// WIFI 名 Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP()); // 分配的动态地址&自己设置的静态地址 Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
break;
}
}
} // prepare a web page to be send to a client (web browser)
String prepareHtmlPage()
{
String htmlPage =
String("HTTP/1.1 200 OK\r\n") +
"Content-Type: text/html\r\n" +
"Connection: close\r\n" + // the connection will be closed after completion of the response
"Refresh: 5\r\n" + // refresh the page automatically every 5 sec
"\r\n" +
"<!DOCTYPE HTML>" +
"<html>" +
"Analog input: " + String(analogRead(A0)) +
"</html>" +
"\r\n";
return htmlPage;
}
void setup()
{
Serial.begin(9600);
Serial.println("Start module");
pinMode(LED, OUTPUT);
digitalWrite(LED, 0); smartConfig(); IPAddress staticIP(192,168,1,4);
IPAddress gateway(192,168,1,4 );
IPAddress subnet(255,255,255,0);
WiFi.config(staticIP, gateway, subnet);
Serial.print("Connected, IP change address: ");
Serial.println(WiFi.localIP()); // 分配的动态地址&自己设置的静态地址 server.begin();
Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str());
}
void loop()
{
WiFiClient client = server.available();
if (client)
{
Serial.println("\n[Client connected]");
while (client.connected())
{ if (Serial.available()) {
Serial.print("1");
client.println(Serial.read());
}
// read line by line what the client (web browser) is requesting
if (client.available())
{ String line = client.readStringUntil('\r');
Serial.print(line);
// wait for end of client's request, that is marked with an empty line
if (line.length() == 1 && line[0] == '\n')
{
client.println(prepareHtmlPage());
break;
}
}
}
delay(1); // give the web browser time to receive the data
// close the connection:
client.stop();
Serial.println("[Client disonnected]");
} }
1.5被用一段代码 用于分割收到的命令,提取我们想要的数据
// First line of HTTP request looks like "GET /path HTTP/1.1" // 收到的命令格式(可修改),解析跟着改
// Retrieve the "/path" part by finding the spaces
int addr_start = req.indexOf(' ');// 找到第一个空格
int addr_end = req.indexOf(' ', addr_start + 1); // 找到第二个空格
if (addr_start == -1 || addr_end == -1) {
Serial.print("Invalid request: ");
Serial.println(req);
return;
}
req = req.substring(addr_start + 1, addr_end);// 截取两个空格间字符
Serial.print("Request: ");
Serial.println(req);
自动保存WIFI信息自渎取 http://blog.csdn.net/sadshen/article/details/47832551
1. 路由器参数的保存和查询
在DEMO中,设置完STATION模式后,就直接smartconfig。我们应当在smartconfig之前做一个参数判断,判断是否有保存的路由器信息。有保存,就连接路由器。没保存,才smartconfig。
void user_init(void)
{
os_printf("SDK version:%s\n", system_get_sdk_version());
wifi_set_opmode(STATION_MODE);
smartconfig_start(smartconfig_done);
}
关于这个路由器参数存储,查了下官方手册,发现SDK还是很贴心的,已经有API可以直接保存和查询了,而不需要我们再去做flash参数存储。
两个关键API摆出来。
保存
wifi_station_set_config
功能:设置 WiFi station 接口的配置参数,并保存到 flash
查询
wifi_station_get_config_default
功能:查询 WiFi station 接口保存在 flash 中的配置参数。
三、代码操作
1.初始化时判断FLASH中是否存有有效的路由器参数。
void user_init(void)
{
wifi_set_opmode(STATION_MODE);
wifi_station_get_config_default(&s_staconf);
if (os_strlen(s_staconf.ssid) != 0) {
os_printf("user_scan\n");
system_init_done_cb(user_scan);
} else {
os_printf("smartcfg\n");
smartconfig_start(smartconfig_done);
}
}
2.已存有路由器参数,去搜索看看AP当前是否存在。
void ICACHE_FLASH_ATTR
user_scan(void)
{
struct scan_config config;
os_memset(&config, 0, sizeof(config));
config.ssid = s_staconf.ssid;
wifi_station_scan(&config, user_scan_done);
}
3.之后的操作就看个人处理了。
规范的处理是要开启一个线程,周期性检测AP的连接状态,处理不同的错误状态。我就不展开了。
1.6烧录程序
淘宝;https://detail.tmall.com/item.htm?id=536504922008&spm=a1z09.2.0.0.5e160c66c76b3g&_u=e1qf7bf52e27
产品资料:http://www.kbgogo.com/forum.php?mod=viewthread&tid=64&extra=
烧录时版型和参数选择
2 手机一键配置上网软件
2.1 网页资料
https://www.espressif.com/zh-hans/products/software/esp-touch/resources
下载ESPTOUCH软件:https://github.com/EspressifApp/IOT-Espressif-Apk
ESPTOUCH的源码:https://github.com/EspressifApp/EsptouchForAndroid
2.2 使用过程
2.1 手机连接路由器wifi,打开ESPTOUCH软件
自动读出WIFI名,手动输入密码,确认。
esp8266会自动接收这个信息存下,然后自动连接这个路由器wif.
至此,手机和esp8266都连在同一个WIFI下,在一个局域网内。
点击确认后,esp将连接信息打印给电脑串口。
3 手机测试软件
上个软件使得esp8266一键连上网,这个软件开始和esp通信发命令。
(两个软件可以合并在一个里面,再加上美化界面,此为调试阶段,故没有合并,能用就行。)
此时手机也连接在WIFI上,和esp在同个WIFI上,这个软件建立客户端请求访问。
IP 我们已经在esp的程序里固定,手机可以写死,用户不用自己输入。端口也是。
3.1 esp通过wifi接收到数据后,串口打印给电脑
之后的开发
1命令直接控制esp8266自身引脚,LED pwm,,,
2命令串口给另一个单片机,由它来执行任务。
样例: arduino 软串口收命令执行任务 http://www.cnblogs.com/kekeoutlook/default.html?page=2
3.2 手机网页打开地址192.168.1.22
之后的开发
可以开发网页控制,使用互联网远程控制,上述都只在一个局域网内。