Arduino IDE for ESP8266 项目(4)HTTP客户端+服务端
Arduio for esp8266
官网API:http://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/readme.html
很有价值的参考文献:http://blog.csdn.net/sadshen/article/details/46883245
参考的例程:https://github.com/SmartArduino/ESPDuino/tree/master/Book
网友参考:http://www.arduino.cn/thread-18958-1-1.html
手机一键配置软件
下载ESPTOUCH软件:https://github.com/EspressifApp/IOT-Espressif-Apk
ESPTOUCH的源码:https://github.com/EspressifApp/EsptouchForAndroid
要想端到端通信两步
1 AP或STA,将自己接入某网络或者自己当网路。
2 建立一个server 或者 client ,在第一里形成的网络里去通信。
1 SF1 STA模式-连接路由器-设置AP-建立sever-手机通过软件 ip+端口访问
/* WiFiTelnetToSerial - 使用通用异步收发传输器(Universal Asynchronous Receiver/Transmitter,UART)与ESPDUINO进行TCP透传的例子 */ #include <ESP8266WiFi.h> //定义可连接的客户端数目最大值 #define MAX_SRV_CLIENTS 1 const char* ssid = "Doit"; const char* password = "doit3305"; WiFiServer server(23); WiFiClient serverClients[MAX_SRV_CLIENTS]; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); Serial.print("\nConnecting to "); Serial.println(ssid); uint8_t i = 0; while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(500); if(i == 21){ //超时(20x500=10000,10秒钟),提示连接失败 Serial.print("Could not connect to"); Serial.println(ssid); while(1) delay(500); } //启动UART传输和服务器 server.begin(); server.setNoDelay(true); Serial.print("Ready! Use 'telnet "); Serial.print(WiFi.localIP());//获得服务器本地IP Serial.println(" 23' to connect"); } void loop() { uint8_t i; //检测服务器端是否有活动的客户端连接 if (server.hasClient()){ for(i = 0; i < MAX_SRV_CLIENTS; i++){ //查找空闲或者断开连接的客户端,并置为可用 if (!serverClients[i] || !serverClients[i].connected()){ if(serverClients[i]) serverClients[i].stop(); serverClients[i] = server.available(); Serial.print("New client: "); Serial.println(i); continue; } } //若没有可用客户端,则停止连接 WiFiClient serverClient = server.available(); serverClient.stop(); } //检查客户端的数据 for(i = 0; i < MAX_SRV_CLIENTS; i++){ if (serverClients[i] && serverClients[i].connected()){ if(serverClients[i].available()){ //从Telnet客户端获取数据,并推送到URAT端口 while(serverClients[i].available()) Serial.write(serverClients[i].read()); } } } //检查UART端口数据 if(Serial.available()){ size_t len = Serial.available(); uint8_t sbuf[len]; Serial.readBytes(sbuf, len); //将UART端口数据推送到所有已连接的telnet客户端,实现双向通信 for(i = 0; i < MAX_SRV_CLIENTS; i++){ if (serverClients[i] && serverClients[i].connected()){ serverClients[i].write(sbuf, len); delay(1); } } } }
1.2 SF1 STA模式-连接路由器-设置AP-建立sever-手机通过 网页+软件IP+端口 访问
#include <ESP8266WiFi.h> const char* ssid = "dongdong";
const char* password = "dongdong"; WiFiServer server(80); void setup()
{
Serial.begin(115200);
Serial.println();
IPAddress staticIP(192,168,1,22);
IPAddress gateway(192,168,1,9);
IPAddress subnet(255,255,255,0);
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password); WiFi.config(staticIP, gateway, subnet); while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected"); server.begin();
Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str());
} // 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 loop()
{
WiFiClient client = server.available();
// wait for a client (web browser) to connect
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]");
}
}
2 SF2
2.1 SAT模式-连接WIFI-建立client+远程访问IP+端口
#include <ESP8266WiFi.h> const char* ssid = "Doit"; //change to your own ssid const char* password = "doit3305";//change to your own password const char* serverIP = "115.29.109.104"; int serverPort = 6535; WiFiClient client; bool bConnected = false; char buff[512]; int nm = 0; void setup() { Serial.begin(115200); delay(10); 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()); } void loop() { delay(1); if (bConnected == false) { if (!client.connect(serverIP, serverPort)) { Serial.println("connection failed"); delay(5000); return; } bConnected = true; Serial.println("connection ok"); } else if (client.available()) { Serial.println("Data is coming"); while (client.available()) { buff[nm++] = client.read(); if (nm >= 511) break; } buff[nm] = 0x00; nm=0; Serial.print(buff); client.print(buff); client.flush(); } }
2.2 SAT模式-连接WIFI-建立client+远程访问网页
#include <ESP8266WiFi.h> const char* ssid = "********";
const char* password = "********"; const char* host = "www.example.com"; void setup()
{
Serial.begin(115200);
Serial.println(); Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
} void loop()
{
WiFiClient client; Serial.printf("\n[Connecting to %s ... ", host);
if (client.connect(host, 80))
{
Serial.println("connected]"); Serial.println("[Sending a request]");
client.print(String("GET /") + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n" +
"\r\n"
); Serial.println("[Response:]");
while (client.connected())
{
if (client.available())
{
String line = client.readStringUntil('\n');
Serial.println(line);
}
}
client.stop();
Serial.println("\n[Disconnected]");
}
else
{
Serial.println("connection failed!]");
client.stop();
}
delay(5000);
}
3 AF1 AP-Sever
#include <ESP8266WiFi.h> const char *ssid = "Charlie Testing AP";
const char *password = "12345678";
WiFiServer server(80);
void setup()
{
Serial.begin(115200);
Serial.println(); Serial.print("Setting soft-AP ... "); IPAddress softLocal(192,168,128,1);
IPAddress softGateway(192,168,128,1);
IPAddress softSubnet(255,255,255,0); WiFi.softAPConfig(softLocal, softGateway, softSubnet); WiFi.softAP(ssid, password); IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
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]");
} } // 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;
}
Arduino IDE for ESP8266 项目(4)HTTP客户端+服务端的更多相关文章
- Arduino IDE for ESP8266 项目云盒子(2)一键自配置+网页服务器
https://item.taobao.com/item.htm?spm=a230r.1.14.20.eYblO3&id=521945102409&ns=1&abbucket= ...
- Arduino IDE for ESP8266 项目(1) 点亮灯+按键LED+pwm
官方文档 http://esp8266.github.io/Arduino/versions/2.1.0/doc/libraries.html 引脚口说明 http://yfrobot.com/thr ...
- Arduino IDE for ESP8266 项目云盒子(3)外网访问
互联网访问esp8266 https://item.taobao.com/item.htm?spm=a230r.1.14.20.eYblO3&id=521945102409&ns=1& ...
- Arduino IDE for ESP8266 项目云盒子 (1)AP直接模式
手机直接连接esp8266辐射的WIFI,通信. https://item.taobao.com/item.htm?spm=a230r.1.14.20.eYblO3&id=5219451024 ...
- Arduino IDE for ESP8266 项目(3)创建AP+STA
官网API:http://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/readme.html STA (客户端)手机连接路由器 S1 *简 ...
- Arduino IDE for ESP8266 项目云盒子(4)组网
- Arduino IDE for ESP8266 项目(2)wifi扫描
#include "ESP8266WiFi.h" void setup() { Serial.begin(115200); //设定WiFi为STA模式,如果先前已连接上AP,则与 ...
- ESP8266开发之旅 进阶篇② 闲聊Arduino IDE For ESP8266烧录配置
授人以鱼不如授人以渔,目的不是为了教会你具体项目开发,而是学会学习的能力.希望大家分享给你周边需要的朋友或者同学,说不定大神成长之路有博哥的奠基石... QQ技术互动交流群:ESP8266&3 ...
- Arduino IDE搭建ESP8266开发环境,文件下载过慢解决方法 | ESP-01制作WiFi开关教程,改造宿舍灯
1. Arduino IDE配置ESP8266环境 参考:https://www.jianshu.com/p/cb0274d612b5 首先从 Arduino 官网 下载最新版本的 Arduino I ...
随机推荐
- 你不知道的Linux(持续更新中)
1.关于GNU.Linux.GNU/Linux三者的关系 GNU 项目创始于一九八四年,旨在开发一个类似 Unix ,且为自由软件的完整的操作系统: GNU 系统.(也可把GNU看成一个自由软件工程) ...
- SQL Server 中的 NOLOCK 到底是什么意思?
以前遇到过,但仅限于听同事说加上NOLOCK好一些,今天仔细研究测试了下,终于理解了,那么加与不加到底区别在哪呢? 我先说下其区别,之后再做测试. 大家都知道,每新建一个查询,都相当于创建一个会话,在 ...
- SQL 查看表每一个列的名字以及类型
select a.name,b.name from sys.columns as a join sys.types as b on a.system_type_id=b.system_type_id ...
- Mybatis 3 配置 Log4j
Mybatis与Log4j 最常用的日志输出是Log4j,将相应的jar包和配置文件放到相应的位置,Mybatis就可以通过Log4j将SQL语句打印出来. 配置Log4j.properties 将l ...
- [android] 调用系统照相机和摄像机
查看系统照相机源码,找到清单文件查看 查看意图过滤器,action是android.media.action.IMAGE_CAPTURE category是android.intent.categor ...
- Django Rest Framework之版本控制
基本代码结构 settings.py: REST_FRAMEWORK = { 'DEFAULT_VERSION': 'v1', # 默认版本 'ALLOWED_VERSIONS': ['v1', 'v ...
- js-MediumGrade-base.js
// 1.JavaScript 中的类型包括 Number(数字) String(字符串) Boolean(布尔) Symbol(符号)(第六版新增) Object(对象) Function(函数) ...
- vue-cli脚手架之build文件夹上半部
好,接下来一起分析分析配置文件^o^. build.js作用:命令npm run build的入口配置文件,主要用于生产环境. build.js中具体含义标注(vue-cli脚手架官方文件解释,大家可 ...
- float清除浮动
清除浮动: 在非IE浏览器下,当容器的高度为auto,且容器的内容中有浮动(float为left或right)的元素,在这种情况下,容器的高度不能自动伸长以适应内容的高度,使得内容溢出到容器外面而影响 ...
- parseInt转换
function parse2Int(num) { return parseInt(num,10); } 如果 string 以 "0x" 开头,parseInt() 会把 str ...