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 ...
随机推荐
- T-SQL :联接查询练习 (杂)
1.每个客户返回一行订单 日期在~到~之间 SELECT E.empid, , ') AS dt FROM HR.Employees AS E CROSS JOIN Nums AS D ORDER B ...
- javascript如何处理很多数据,类似分页切换
需求:一个用户列表数据,如果对应列表数据大于10个,就每10个保存到二维数组,后面不足10个的依然放在二维数组尾部 用处:模拟分页,或者局部刷新 在线DEMO:戳这里 var obj=[ { &quo ...
- Nginx负载均衡配置调优
worker_processes ; error_log logs/error.log crit; #日志位置和日志级别 pid logs/nginx.pid; worker_rlimit_nofil ...
- tomcat8 manager页面限制IP访问
tomcat8 manager页面限制IP访问 配置tomcat8/webapps/manager/META-INF/context.xml <Context antiResourceLocki ...
- linux 系统filezilla无法上传文件 553 Could not create
做网站过程中遇见了很多问题,解决了但是解决方法过一段时间就会遗忘,整理出来以便以后查看. 响应: 553 Could not create file.错误: 严重文件传输错误 解决方案: 一.必须将站 ...
- Python 类的祖宗--metaclass
1.Python 中一切事物都是对象 2.类都是 type 类的对象 类的两种申明方法 # 方法一: class Foo: def func(self): print(666) obj = Foo() ...
- JS之iscroll.js的使用详解
入门 Scroll是一个类,每个需要使用滚动功能的区域均要进行初始化.每个页面上的iScroll实例数目在设备的CPU和内存能承受的范围内是没有限制的. 尽可能保持DOM结构的简洁.iScroll使用 ...
- 你的BI应用处于什么阶段?解读Gartner BI成熟度模型
文 | 帆软数据应用研究院 水手哥 本文出自:知乎专栏<帆软数据应用研究院>——数据干货&资讯集中地 无论国内还是国外,多数企业的BI和分析平台建设之路并不平坦:一是对自身的环 ...
- 一. Redis 常用命令
键值相关命令 1. KETS 查询所有的key 127.0.0.1:6379> keys * 1) "tony"2) "hexu1"3) "he ...
- Android滑动菜单使用(MenuDrawer和SlidingMenu)
项目地址: https://github.com/gokhanakkurt/android-menudrawer https://github.com/jfeinstein10/SlidingMe ...