Arduino IDE for ESP8266 项目(3)创建AP+STA
官网API:http://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/readme.html
STA (客户端)手机连接路由器
S1 *简单的连接WIFI
自己当手机,连接wifi
#include <ESP8266WiFi.h>
void setup()
{
Serial.begin(115200);
Serial.println();
WiFi.begin("network-name", "pass-to-network");
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {}
S2 *添加WIFI备用,自动连接
/*
* This sketch trys to Connect to the best AP based on a given list
*
*/
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti;
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println();
delay(10);
wifiMulti.addAP("dongdong", "dongdong");
wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
Serial.println("Connecting Wifi...");
if(wifiMulti.run() == WL_CONNECTED) {
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
}
void loop() {
if(wifiMulti.run() != WL_CONNECTED) {
Serial.println("WiFi not connected!");
delay(1000);
}
}
S3 连接WIFI,主动设置静态地址
#include <ESP8266WiFi.h>
const char* ssid = "dongdong";
const char* password = "dongdong";
String name="DD_Station_01";
IPAddress staticIP(192,168,1,22);
IPAddress gateway(192,168,1,9);
IPAddress subnet(255,255,255,0);
void setup(void)
{
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s\n", ssid);
WiFi.begin(ssid, password);
WiFi.config(staticIP, gateway, subnet);
// 修改主机名
WiFi.hostname(name);
Serial.printf("New hostname: %s\n", WiFi.hostname().c_str());
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP()); // 分配的动态地址&自己设置的静态地址
Serial.printf("SSID: %s\n", WiFi.SSID().c_str());// 连接的WIFI名
}
void loop() {}
S4 http连接网络,访问网页
/*
* This sketch sends data via HTTP GET requests to data.sparkfun.com service.
*
* You need to get streamId and privateKey at data.sparkfun.com and paste them
* below. Or just customize this script to talk to other HTTP servers.
*
*/
#include <ESP8266WiFi.h>
const char* ssid = "Doit";
const char* password = "doit3305"; const char* host = "data.sparkfun.com";
const char* streamId = "ESPDUINO_STA";
const char* privateKey = "pzRb9dawqocbP9n0K0M9";
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
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());
}
int value = 0;
void loop() {
delay(5000);
++value;
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/input/";
url += streamId;
url += "?private_key=";
url += privateKey;
url += "&value=";
url += value;
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +"Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
delay(10);
// Read all the lines of the reply from server and print them to Serial while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
AP(服务器) 自己当WIFI
A1 自己当WIFI wifi名称+密码+ IP 不提供服务
#include <ESP8266WiFi.h>
const char *ssid = "Charlie Testing AP";
const char *password = "12345678";
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); }
void loop()
{
delay(3000);
}
A2 1主动设置自己的WIFI wifi名称+密码+ IP 2建立一个服务接收手机的请求和信息 3网页返回给手机
#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;
}
STA (客户端)+ AP(服务器)
#include <ESP8266WiFi.h> #include <WiFiUdp.h> /******************* STA 当手机 *****************************/
//设置STA网络参数
IPAddress sip(192, 168, 1, 29);//本地IP
IPAddress sip1(192, 168, 1, 1);//本地网关
IPAddress sip2(255, 255, 255, 0);//本地子网掩码 //设置STA
const char *ssid = "Netcore_wsn";
const char *password = "99325408322";
/**********************************************************/ /******************* AP 当wifi *****************************/
IPAddress xip(192, 168,2, 2);//下位远程IP
//设置AP网络参数
IPAddress lxip(192, 168,2, 1);//AP端IP
IPAddress lxip1(192, 168,2, 1);//AP端网关
IPAddress lxip2(255, 255,255, 0);//AP端子网掩码 //设置AP账号密码
const char *ssid1 = "Netcore_wsn1";//AP wifi名
const char *password1 = "99325408322";//AP wifi密码
/**********************************************************/ IPAddress Serverip(192, 168, 1, 4);//上位机远程IP
unsigned int localPort = 9999;//本地端口
unsigned int remoteport = 9999;//远程端口 WiFiUDP udp;
char packetBuffer[255];//收发缓冲区 void setup() { Serial.begin(115200);//初始化串口波特率
delay(5000);//延时5S WiFi.mode(WIFI_AP_STA);//设置模式为AP+STA /******************* AP 当WIFI *****************************/
WiFi.softAPConfig(lxip,lxip1,lxip2);//设置AP网络参数
WiFi.softAP(ssid1,password1,1);//设置AP账号密码
/******************************************************************/ Serial.print("apip:");
Serial.println(WiFi.softAPIP());// AP 自己当WIFI 自己设置的内网地址 /******************* STA 当手机连接WIFI *****************************/
WiFi.begin(ssid,password);//连接指定路由
WiFi.config(sip,sip1,sip2);//设置本地网络参数 Serial.print("Is connection routing, please wait"); while(WiFi.status()!=WL_CONNECTED)//等待路由连接
{
delay(500);
Serial.print(".");
} /******************************************************************/ Serial.println(" ");
udp.begin(localPort);//监听指定端口
Serial.print("ip:");
Serial.println(WiFi.localIP());// STA 当手机连接WIFI 自己设置的静态地址 } void loop()
{
if(udp.parsePacket())
{
udp.read(packetBuffer,255);//读取数据
udp.beginPacket(Serverip,remoteport);
udp.write(packetBuffer,255);
udp.endPacket(); Serial.println(packetBuffer); udp.beginPacket(xip,remoteport);
udp.write(packetBuffer,255);
udp.endPacket();
memset(packetBuffer, 0, 255);//清除缓冲器数值 }
}
Arduino IDE for ESP8266 项目(3)创建AP+STA的更多相关文章
- Arduino IDE for ESP8266教程(二) 创建WIFI AP模式
创建WIFI热点 #include <ESP8266WiFi.h> void setup() { Serial.begin ( 115200 ); Serial.println(" ...
- 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 项目云盒子(2)一键自配置+网页服务器
https://item.taobao.com/item.htm?spm=a230r.1.14.20.eYblO3&id=521945102409&ns=1&abbucket= ...
- Arduino IDE for ESP8266 项目(4)HTTP客户端+服务端
Arduio for esp8266 官网API:http://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/readme.html 很有 ...
- 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 项目(2)wifi扫描
#include "ESP8266WiFi.h" void setup() { Serial.begin(115200); //设定WiFi为STA模式,如果先前已连接上AP,则与 ...
- Arduino IDE for ESP8266 项目云盒子(4)组网
- ESP8266开发之旅 进阶篇② 闲聊Arduino IDE For ESP8266烧录配置
授人以鱼不如授人以渔,目的不是为了教会你具体项目开发,而是学会学习的能力.希望大家分享给你周边需要的朋友或者同学,说不定大神成长之路有博哥的奠基石... QQ技术互动交流群:ESP8266&3 ...
随机推荐
- C++异常的几种捕获方式
捕获指定的类型 这样的话可以对每种异常做出不同的处理,例如: #include <iostream> using namespace std; void A(int n){ int a = ...
- .Net实现微信公众平台开发接口(一) 之 “微信开发配置”
我们只要通过微信官方认证,成为开发者,才能实现微信提供的各种接口,否则即使调用了接口,微信也不会实现推送,功能也无法通过开发模式真正得到实现,所以需要正确配置微信信息,通过微信官方认证,成为开发者才可 ...
- .Net敏捷开发框架6.1.6.2版本,联系QQ:6539471
演示地址:www.fishcmonkey.com .NET敏捷开发框架 6.1.6.2 版本发布 新增手机流程-我的流程(可查看流程进度和表单内容) 新增手机流程-待办任务(可查看流程进度和表单内容, ...
- Select2插件的隐藏、设置宽度
<select id="selPrinvice" class="Select2 select2-hidden-accessible" style=&quo ...
- MVC 【ASPX视图引擎】
新建项目----ASP.NET MVC 4 Web 应用程序------选择模板(空).视图引擎(ASPX) 1.认识控制器Controller using System; using System. ...
- jQuery 【选择器】【动画】
jQuery 是一个快速.简洁的JavaScript框架,它封装JavaScript常用的功能代码,提供一种简便的JavaScript设计模式,优化HTML文档操作.事件处理.动画设计和Ajax交互. ...
- mvc导出excel记录
前言: 记录这篇使用记录,是为了方便以后学习查阅和让没有使用过的人了解一下,其中不足还请见谅.不是很全的文章,大神请绕行.在项目中我们或多或少的会遇到数据导出到excel表格以便线下查看或者记录一些需 ...
- ios --键盘监听JYKeyBoardListener
没有前言,就是一个简单的键盘监听,自动调整输入框的位置不被键盘遮挡 .h // // JYKeyBoardListener.h // // Created by JianF.Sun on 17/9/2 ...
- 【读书笔记】iOS-UDID
UIDevice类可以返回当前iOS设备的UDID,以前开发者通常使用UDID作为识别每台设备的唯一标识,然后从iOS5开始,苹果公司将这一功能标记为废止并不推荐使用,苹果公司在iOS6之后将这个功能 ...
- 限制Apache日志access.log、error.log文件大小
在 Windows 下的设置例子如下: # 限制错误日志文件为 1M ErrorLog "|bin/rotatelogs.exe -l logs/error-%Y-%m-%d.log 1M& ...