[原创] LinKIt ONE 测评(3)

ihalin   2017-2-28 09:04 楼主
接着上贴:LinKIt ONE 测评(2)

首先将wifi天线接上去
1488242327946.jpg
添加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并获取论坛网站的程序

  1. #include <LWiFi.h>
  2. #include <LWiFiClient.h>

  3. #define WIFI_AP "135"
  4. #define WIFI_PASSWORD "mima"
  5. #define WIFI_AUTH LWIFI_WPA  // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.
  6. #define SITE_URL "bbs.eeworld.com.cn"

  7. LWiFiClient c;

  8. void setup()
  9. {
  10.   LWiFi.begin();
  11.   Serial.begin(115200);

  12.   // keep retrying until connected to AP
  13.   Serial.println("Connecting to AP");
  14.   while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
  15.   {
  16.     delay(1000);
  17.   }

  18.   // keep retrying until connected to website
  19.   Serial.println("Connecting to WebSite");
  20.   while (0 == c.connect(SITE_URL, 80))
  21.   {
  22.     Serial.println("Re-Connecting to WebSite");
  23.     delay(1000);
  24.   }

  25.   // send HTTP request, ends with 2 CR/LF
  26.   Serial.println("send HTTP GET request");
  27.   c.println("GET / HTTP/1.1");
  28.   c.println("Host: " SITE_URL);
  29.   c.println("Connection: close");
  30.   c.println();

  31.   // waiting for server response
  32.   Serial.println("waiting HTTP response:");
  33.   while (!c.available())
  34.   {
  35.     delay(100);
  36.   }
  37. }

  38. boolean disconnectedMsg = false;

  39. void loop()
  40. {
  41.   // Make sure we are connected, and dump the response content to Serial
  42.   while (c)
  43.   {
  44.     int v = c.read();
  45.     if (v != -1)
  46.     {
  47.       Serial.print((char)v);
  48.     }
  49.     else
  50.     {
  51.       Serial.println("no more content, disconnect");
  52.       c.stop();
  53.       while (1)
  54.       {
  55.         delay(1);
  56.       }
  57.     }
  58.   }

  59.   if (!disconnectedMsg)
  60.   {
  61.     Serial.println("disconnected by server");
  62.     disconnectedMsg = true;
  63.   }
  64.   delay(500);
  65. }

QQ截图20170228090221.png
wifi成功连接AP 成功连接到eeworld 官网

此内容由EEWORLD论坛网友ihalin原创,如需转载或用于商业用途需征得作者同意并注明出处


回复评论 (1)

汇总贴在此:
LinKIt ONE 测评——by ihalin
https://bbs.eeworld.com.cn/forum. ... 1510&fromuid=536508
玩板看这里: https://bbs.eeworld.com.cn/elecplay.html EEWorld测评频道众多好板等你来玩,还可以来频道许愿树许愿说说你想要玩的板子,我们都在努力为大家实现!
点赞  2017-2-28 11:05
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复