打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Arduino WiFi Shield
多虧於公司intern的實驗室,
我才有榮幸玩到Arduino的wifi套件!
不然一塊三千多塊,實在是買不下去阿!

這篇主要教大家,
怎麼用這塊wifi shield去做到下列的事情

  1. scan network AP
  2. connect to a network AP
  3. send a request
  4. create a WebServer using exists connection


恩,在開始之前,請先準備好環境!
注意!在我寫文章的這個時候!
arduino已經出到了1.0.5版!
最一開始我是用1.0.1版開發,但1.0.1版是沒有wifi的lib,
所以我自己去github把wifi的lib抓下來,再放在我的lib裡面!
的確!大部份功能都可以work!
除了建立webserver以外!
但是不要天真的以為用1.0.5版就可以建立webserver,
根據撞牆了幾個小時,發現要downgrade至1.0.2版才能建立webserver...
其實1.0.5版也可以,只是要做一些操作!實在太麻煩!
總而言之,請使用1.0.2



1. 把wifi shield直接插在你的arduino板上!

官網其實有備註,如果你是用比較舊的板子(UNO R3),還要稍微接一下線!
請看這篇吧!



2. include necessary lib

#include


3. 開始寫code

3.1 掃描附近所有的ap

void setup(){
Serial.begin(9600);
}

void loop(){
fnScanNetwork();
}

void fnScanNetwork(){

int iNum = WiFi.scanNetworks();
Serial.print('=========number of available networks:');
Serial.print(iNum);
Serial.println('=========');

// print the each SSID and Signal
for (int i = 0; i
Serial.print(WiFi.SSID(i)); // pring SSID
Serial.print('\tSignal: ');
Serial.print(WiFi.RSSI(i)); // print Signal
Serial.print(' dBm');
Serial.print('\tEncryption: ');
Serial.println(WiFi.encryptionType(i)); // print Encryption Type
}

}



上面比較需要注意的大概就是WiFi.RSSI(i),
這隻method就是回傳該AP訊號的強弱!
記得值越大訊號越強!

再來就是WiFi.encryptionType(i),
這是回傳該AP的加密形態是什麼?
內容如下:
* TKIP (WPA) = 2
* WEP = 5
* CCMP (WPA) = 4
* NONE = 7
* AUTO = 8


3.2 連線至某個ap

char szSsid[] = 'kerkerker'; // network SSID (name)
char szPass[] = 'safesync'; // network password

int iStatus = WL_IDLE_STATUS;

void setup(){
Serial.begin(9600);
fnConnectToAP();
}

void loop(){
}

void fnConnectToAP(){
while ( iStatus != WL_CONNECTED) {
Serial.println('Connecting to network...');
Serial.print('SSID: ');
Serial.println(ssid);
delay(1000);
iStatus = WiFi.begin(ssid, pass); // connect
}

Serial.print('Connected successfully. IP address:');
IPAddress myAddress = WiFi.localIP();
Serial.println(myAddress);
}


WiFi.begin這個method就是在進行連線!
如果成功的話會看到下面兩張圖!
首先你的wifi shield上的link那顆燈會變成綠色!


而你的Serial Monitor會長得像下面一樣!





3.3 傳送一個request至一個server

首先在setup裡面先連線至ap,
接著就送出一個request,
然後就在loop裡面進行讀資料的動作!
如果成功的話,你應該會在你的Serial Monitor看到一大堆回傳的資料才對!

// connect to ap
int iStatus = WL_IDLE_STATUS;
char szSsid[] = 'kerkerker'; // network SSID (name)
char szPass[] = 'safesync'; // network password


// send a request
char szServername[]='www.google.com'; // remote server we will connect to
WiFiClient client;

void setup(){
Serial.begin(9600);
fnConnectToAP();
fnSendRequest();
}

void loop(){
fnReadData();
}

void fnSendRequest(){
if (client.connect(szServername, 80)) {
Serial.println('connected');

// Send a HTTP request:
client.println('GET /search?q=arduino HTTP/1.1');
client.println('Host: www.google.com');
client.println();
}

}
void fnReadData(){
while (client.available()) {
char c = client.read();
Serial.print(c);
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println('disconnecting from server.');
client.stop();

// do nothing forevermore:
while(true);
}
}




3.4 建立一個webserver供人連線

輸入下列的code以後,接著只要用browser輸入你的ip,
就會看到Hello World了!

// webserver
WiFiServer server(80);

void setup(){
Serial.begin(9600);
fnConnectToAP();
// fnSendRequest();
fnStartServer();
}

void loop(){
fnWaitingRequest();
// fnReadData();
}


void fnStartServer(){
delay(1000);
server.begin();
}

void fnWaitingRequest(){
WiFiClient client = server.available();

if (client) {
Serial.println('new client comming');

boolean bCurrentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && bCurrentLineIsBlank) {
// send a standard http response header
client.println('HTTP/1.1 200 OK');
client.println('Content-Type: text/html');
client.println('Connection: close'); // the connection will be closed after completion of the response
client.println();
client.println('');
client.println('');
client.print('Hello World ');

client.println('');
break;
}
if (c == '\n') {
// you're starting a new line
bCurrentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
bCurrentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);

// close the connection:
client.stop();
Serial.println('client disonnected');
}

}



source code: https://github.com/Ken-Yang/ArduinoWiFiShield

基本上蠻簡單的,
code也很好理解!
只是有些tricky的地方,
像是1.0.5版需要update firmware!
官網上也有很多豐富的example!
有空可以去看看!


如果有空,下一篇會教大家怎麼結合wifi+遙控車!















本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
如何仅花20元成本用arduino平台建立自己的物联网应用
前方高能,教大家制作一个能手机控制的继电器开关!
ESP8266-1S服务器推送环境温度
ESP8266模块使用
用ESP-01开发板制作一个智能家居万能控制器
使用Arduino开发ESP32(03):WiFi基本功能使用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服