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#实现RSA加密与解密、签名与认证(转)
一.RSA简介 RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的.RSA取名来自开发他们三者的名字.RSA是目前最有影响力 ...
- ssr 之Nuxt.js
ssr:server side rendering(服务端渲染),目的是为了解决单页面应用的 SEO 的问题,对于一般网站影响不大,但是对于论坛类,内容类网站来说是致命的,搜索引擎无法抓取页面相关内容 ...
- JS之iscroll.js的使用详解
入门 Scroll是一个类,每个需要使用滚动功能的区域均要进行初始化.每个页面上的iScroll实例数目在设备的CPU和内存能承受的范围内是没有限制的. 尽可能保持DOM结构的简洁.iScroll使用 ...
- 洛谷P4563 [JXOI2018]守卫(dp)
题意 题目链接 Sol 非常有意思的题目. 我们设\(f[l][r]\)表示区间\([l,r]\)的答案. 显然\(r\)位置一定有一个保镖 同时不难观察到一个性质:拿\([1, n]\)来说,设其观 ...
- 洛谷P2572 [SCOI2010]序列操作(ODT)
题解 题意 题目链接 Sol ODT板子题..... // luogu-judger-enable-o2 #include<bits/stdc++.h> #define LL long l ...
- 【读书笔记】iOS-强类型与弱类型
id类型是一个通用类型,OC使用id表示任意类型的对象,它可以作为一个占位符表示这是一个不确定的类型的对象或者引用.因此,所有的对象都 可以用id来表示.这很有用,想象一下,如果你需要实现一个通用的链 ...
- 【读书笔记】iOS-深入解剖对等网络
协议本身是一个运行在UDP之上的定制协议.我所以决定使用一个定制协议很简单.首先,当前这个任务看起来足够简单,因此与尝试改进一个现在协议相比,直接构建一个定制协议更为容易.其次,定制协议可以将开销减少 ...
- Salesforce小知识:累计汇总字段类型
累计汇总字段类型的定义 Salesforce中可以在两个对象之间建立关系.一个对象的某一条记录可以有若干条相关联的其他对象记录. 累计汇总字段可以将这些相关联的记录中的某些字段值汇总起来,显示给用户. ...
- Python 基于python实现单例模式
基于python实现单例模式 by:授客 QQ:1033553122 概念 简单说,单例模式(也叫单件模式)的作用就是保证在整个应用程序的生命周期中,任何一个时刻,单例类的实例都只存在一个(当然也 ...
- python第六十天-----RabbitMQ
RabbitMQ消息队列:默认为消息轮循模式,按client端启动是顺序接收 server端 import pika connection = pika.BlockingConnection(pika ...