|
接着上贴:LinKIt ONE 测评(2)
首先将wifi天线接上去
添加WIFI函数库
#include
#include
将WIFI模块连接AP
API:
首先启动 : LWiFi.begin();
支持没密码的: LWiFi.connect(WIFI_AP);
支持WEP加密的 :LWiFi.connectWEP(WIFI_AP, WIFI_PWD);
支持WPA加密的: LWiFi.connectWPA(WIFI_AP, WIFI_PWD);
如果连接失败 connect()会回传负值。
下面写个连接WIFI并获取论坛网站的程序
- #include <LWiFi.h>
- #include <LWiFiClient.h>
- #define WIFI_AP "135"
- #define WIFI_PASSWORD "mima"
- #define WIFI_AUTH LWIFI_WPA // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.
- #define SITE_URL "bbs.eeworld.com.cn"
- LWiFiClient c;
- void setup()
- {
- LWiFi.begin();
- Serial.begin(115200);
- // keep retrying until connected to AP
- Serial.println("Connecting to AP");
- while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
- {
- delay(1000);
- }
- // keep retrying until connected to website
- Serial.println("Connecting to WebSite");
- while (0 == c.connect(SITE_URL, 80))
- {
- Serial.println("Re-Connecting to WebSite");
- delay(1000);
- }
- // send HTTP request, ends with 2 CR/LF
- Serial.println("send HTTP GET request");
- c.println("GET / HTTP/1.1");
- c.println("Host: " SITE_URL);
- c.println("Connection: close");
- c.println();
- // waiting for server response
- Serial.println("waiting HTTP response:");
- while (!c.available())
- {
- delay(100);
- }
- }
- boolean disconnectedMsg = false;
- void loop()
- {
- // Make sure we are connected, and dump the response content to Serial
- while (c)
- {
- int v = c.read();
- if (v != -1)
- {
- Serial.print((char)v);
- }
- else
- {
- Serial.println("no more content, disconnect");
- c.stop();
- while (1)
- {
- delay(1);
- }
- }
- }
- if (!disconnectedMsg)
- {
- Serial.println("disconnected by server");
- disconnectedMsg = true;
- }
- delay(500);
- }
复制代码
wifi成功连接AP 成功连接到eeworld 官网
此内容由EEWORLD论坛网友ihalin原创,如需转载或用于商业用途需征得作者同意并注明出处
|
|