Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 2448|回復: 2
打印 上一主題 下一主題

[請教]arduino seeed wifi shield

[複製鏈接]
跳轉到指定樓層
1#
發表於 2015-4-23 15:54:08 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
小弟我最近使用Seeed廠所出的 Arduino wifi shield V1.2,我使用他架設網站,每次都有Pin到但是卻哪部開網頁,
請教各位大大是為什麼?


以下是我的程式碼


#include <OneWire.h>           // 引用DS18B20 Library
#include <DallasTemperature.h> // 引用DS18B20 Library
#include <Servo.h>             // 引用 Servo Library
#include <SoftwareSerial.h>
#include "WiFly.h"

#define SSID      "KenMiao"    // 輸入自己的 WiFi ID
#define KEY       "0919388489" // 輸入自己的 WiFi possword
// check your access point's security mode, mine was WPA20-PSK
// if yours is different you'll need to change the AUTH constant, see the file WiFly.h for avalable security codes
#define AUTH      WIFLY_AUTH_WPA2_PSK // WiFi 傳輸格式



int servopin = 0;       // 建立一個 Servo 物件 第33腳
int value ;                // 設定Servo初始角度
Servo myservo;                 //指定myservo為Servo的旋轉角度
long previousTimeMyservo = 0;  // 用來保存前一次 Servo 更新狀態的時間
long Myservointerval = 6000;  // Servo延遲時間


const byte speed = 100;        // Pump PWM 輸出值
long distance;                 // 暫存接收訊號的高電位持續時間
const byte EA = 6;            // Pump A 的致能接腳
const byte IA = 7;            // Pump A 的正反轉接腳

OneWire oneWire = 25;          //設定DS18B20輸出為第7腳
DallasTemperature sensors(&oneWire);//設定為可寫入腳===================
long previousTimeoneWire = 0;  // 用來保存前一次 DS18B20 更新狀態的時間
long OneWireinterval = 1000;   // DS18B20 延遲時間
float xxx=0;                   //設定輸出值已符點數儲存

int flag = 0;

// Pins' connection
// Arduino       WiFly
//  10    <---->    TX
//  11    <---->    RX

SoftwareSerial wiflyUart(10, 11); // WiFi RX TX 腳
WiFly wifly(&wiflyUart); // pass the wifi siheld serial object to the WiFly class

void setup()
{

  myservo.attach(servopin);     // 控制伺服馬達接腳 pin 32

  pinMode(IA, OUTPUT);
  digitalWrite(IA,LOW);

  pinMode(30,OUTPUT);           // Pump Switch
  digitalWrite(30,LOW);

  sensors.begin();              // 為DS18B20輸入腳
  pinMode(24,OUTPUT);
  digitalWrite(24,LOW);

  pinMode(13,OUTPUT);           // LED Switch
  digitalWrite(13,LOW);

  pinMode(32,OUTPUT);           // Servo Switch
  digitalWrite(32,LOW);

  wiflyUart.begin(9600); // start wifi shield uart port

  Serial.begin(9600); // start the arduino serial port
  Serial.println("--------- WIFLY Webserver --------");

  // wait for initilization of wifly
  delay(3000);

  wifly.reset(); // reset the shield

  Serial.println("Join " SSID );
  if (wifly.join(SSID, KEY, AUTH)) {
    Serial.println("OK");
  } else {
    Serial.println("Failed");
  }

  // get WiFly params

  wifly.sendCommand("set ip local 80\r"); // set the local comm port to 80
  delay(1000);

  wifly.sendCommand("set ip port 80\r"); // set the comm port to 80
  delay(1000);

  wifly.sendCommand("set comm remote 0\r"); // do not send a default string when a connection opens
  delay(1000);

  wifly.sendCommand("set comm open *OPEN*\r"); // set the string that the wifi shield will output when a connection is opened
  delay(3000);

  wifly.sendCommand("get ip\r");
  char c;

  while (wifly.receive((uint8_t *)&c, 1, 300) > 0) { // print the response from the get ip command
    Serial.print((char)c);
  }

  Serial.println("Web server ready");




}

void loop()
{
  checkMyservoAction();       // 檢查Servo是否超過指定時間
  checkOneWireAction();       // 檢查DS18B20是否超過指定時間
  checkforward();             // 啟動幫浦


  if(wifly.available())
  { // the wifi shield has data available

    if(wiflyUart.find("*OPEN*")) // see if the data available is from an open connection by looking for the *OPEN* string
    {
      Serial.println("New Browser Request!");
      delay(1000); // delay enough time for the browser to complete sending its HTTP request string

      if(wiflyUart.find("pin=")) // look for the string "pin=" in the http request, if it's there then we want to control the LED
      {
        Serial.println("Switch Control");

        //  解讀wifi數值
        int pinNumber = (wiflyUart.read()-48); // get first number i.e. if the pin 13 then the 1st number is 1
        int secondNumber = (wiflyUart.read()-48);
        if(secondNumber>=0 && secondNumber<=9)
        {                      //============================還原數值============================
          pinNumber*=10;
          pinNumber +=secondNumber; // get second number, i.e. if the pin number is 13 then the 2nd number is 3, then add to the first number
        }

        digitalWrite(pinNumber, !digitalRead(pinNumber)); // switch pin

        // Build pinstate string. The Arduino replies to the browser with this string.
        String pinState = "Pin ";
        pinState+=pinNumber;
        pinState+=" is ";
        if(digitalRead(pinNumber)) // check if the pin is ON or OFF
        {
          pinState+="ON"; // the pin is on
        }
        else
        {
         pinState+="OFF";  // the pin is off
        }
        // build HTTP header Content-Length string.
        String contentLength="Content-Length: ";

        contentLength+=pinState.length(); // 長度的值是Arduino是答覆與瀏覽器的字符串lenght。

        // send HTTP header
        wiflyUart.println("HTTP/1.1 200 OK");
        wiflyUart.println("Content-Type: text/html; charset=UTF-8");
        wiflyUart.println(contentLength); // 將 arduino 所輸出的 lenght 傳到這
        wiflyUart.println("Connection: close");
        wiflyUart.println();
        // send response
        wiflyUart.print(pinState);
      }
      else
      {
        // send HTTP header (網頁開頭)
        wiflyUart.println("HTTP/1.1 200 OK");
        wiflyUart.println("Content-Type: text/html; charset=UTF-8");
        //wiflyUart.println("Content-Length: 571");  // length of HTML code
        wiflyUart.println("Content-Length: 611");
        wiflyUart.println("Connection: close");
        wiflyUart.println();

        // send webpage's HTML code (網頁指令)
        wiflyUart.print("<html>");
        wiflyUart.print("<head>");
        wiflyUart.println("<meta http-equiv=\"refresh\" content=\"15\">");        
        wiflyUart.print("<title>WiFi Shield Webpage</title>");
        wiflyUart.print("</head>");
        wiflyUart.print("<body>");
        wiflyUart.print("<h1>Switch Webpage</h1>");
  
  // In the <button> tags, the ID attribute is the value sent to the arduino via the "pin" GET parameter
        wiflyUart.print("<button id=\"13\" class=\"sw\">LED Switch</button> ");          // button for pin 11 (控制 LED 開關)
        wiflyUart.print("<button id=\"30\" class=\"sw\">Pump</button> ");                // button for pin 30 (控制 Pump 開關)
        wiflyUart.print("<button id=\"24\" class=\"sw\">DS18B20</button> ");             // button for pin 24 (控制 DS18B20 開關)   
        wiflyUart.print("<button id=\"32\" class=\"sw\">SERVO</button> ");   
        wiflyUart.print("<script src=\"http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js\"></script>");
        wiflyUart.print("<script type=\"text/javascript\">");
        wiflyUart.print("$(document).ready(function(){");
        wiflyUart.print("$(\".sw\").click(function(){");
        wiflyUart.print("var p = $(this).attr('id');"); // 判斷開關腳位
        // send HTTP GET request to the IP address with the parameter "pin" and value "p", then execute the function
  
// IMPORTANT: dont' forget to replace the IP address and port with YOUR shield's IP address and port
        wiflyUart.print("$.get(\"192.168.43.14:80/a\", {pin:p},function(data){alert(data)});");// execute get request. Upon return execute the "function" (display an alert with the "data" send back to the browser.
        //==============================需要更改為自己的IP===========================
        wiflyUart.print("});");
        wiflyUart.print("});");
        wiflyUart.print("</script>");
        wiflyUart.print("</br>");
        wiflyUart.print("</br>");
        wiflyUart.print(xxx);             // 輸出溫度
        wiflyUart.print("</body>");
        wiflyUart.print("</html>");
      }
      Serial.println("Data sent to browser");

    }

  }


}
2#
發表於 2015-4-24 02:08:39 | 只看該作者
本帖最後由 vegewell 於 2015-4-24 02:10 編輯

回復 1# benlee21

先單純使用以下程式碼測試 就知道是否跟SERVO碼有關?

=== Example 5: Controlling The Arduino Digital Pins From a Webpage (Toggling LEDs From an Webpage) ===
In this example we will create a webpage with three buttons to control three different digital pins in the Arduino.

For this tutorial follow the steps below. We have also created a video where we explain the code in more detail.

[https://www.youtube.com/watch?v=ek63patAl80 Video - WiFi Shield Arduino Digital Pin Control From Webpage]

'''Step 1: Hardware'''

Connect three LEDs and resistor to digital pins 11, 12, and 13 as shown in the schematic below:
[[File:Wifi-shield-led-control-schematic.png|600px|thumbnail|center|Three LEDs and 1k resistors connected to pins 11, 12, and 13.]]

'''Step 2: Arduino Sketch'''

Upload the following code to your Arduino board but replace "mySSID" and "myPassword" with your access point's SSID name and password:

<syntaxhighlight lang="arduino">
#include <SoftwareSerial.h>
#include "WiFly.h"

#define SSID      "KenMiao"    // 輸入自己的 WiFi ID
#define KEY       "0919388489" // 輸入自己的 WiFi possword
// check your access point's security mode, mine was WPA20-PSK
// if yours is different you'll need to change the AUTH constant, see the file WiFly.h for avalable security codes
#define AUTH      WIFLY_AUTH_WPA2_PSK

int flag = 0;

// Pins' connection
// Arduino       WiFly
//  2    <---->    TX
//  3    <---->    RX

SoftwareSerial wiflyUart(2, 3); // create a WiFi shield serial object
WiFly wifly(&wiflyUart); // pass the wifi siheld serial object to the WiFly class

void setup()
{

  pinMode(11,OUTPUT);
  digitalWrite(11,LOW);

  pinMode(12,OUTPUT);
  digitalWrite(12,LOW);

  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);

  wiflyUart.begin(9600); // start wifi shield uart port

  Serial.begin(9600); // start the arduino serial port
  Serial.println("--------- WIFLY Webserver --------");

  // wait for initilization of wifly
  delay(3000);

  wifly.reset(); // reset the shield

  Serial.println("Join " SSID );
  if (wifly.join(SSID, KEY, AUTH)) {
    Serial.println("OK");
  } else {
    Serial.println("Failed");
  }

  // get WiFly params

  wifly.sendCommand("set ip local 80\r"); // set the local comm port to 80
  delay(100);

  wifly.sendCommand("set ip port 80\r"); // set the comm port to 80
  delay(100);

  wifly.sendCommand("set comm remote 0\r"); // do not send a default string when a connection opens
  delay(100);

  wifly.sendCommand("set comm open *OPEN*\r"); // set the string that the wifi shield will output when a connection is opened

  wifly.sendCommand("get ip\r");
  char c;

  while (wifly.receive((uint8_t *)&c, 1, 300) > 0) { // print the response from the get ip command
    Serial.print((char)c);
  }

  Serial.println("Web server ready");

}

void loop()
{

  if(wifly.available())
  { // the wifi shield has data available

    if(wiflyUart.find("*OPEN*")) // see if the data available is from an open connection by looking for the *OPEN* string
    {
      Serial.println("New Browser Request!");
      delay(1000); // delay enough time for the browser to complete sending its HTTP request string

      if(wiflyUart.find("pin=")) // look for the string "pin=" in the http request, if it's there then we want to control the LED
      {
        Serial.println("LED Control");

        // the user wants to toggle the LEDs
        int pinNumber = (wiflyUart.read()-48); // get first number i.e. if the pin 13 then the 1st number is 1
        int secondNumber = (wiflyUart.read()-48);
        if(secondNumber>=0 && secondNumber<=9)
        {
          pinNumber*=10;
          pinNumber +=secondNumber; // get second number, i.e. if the pin number is 13 then the 2nd number is 3, then add to the first number
        }

        digitalWrite(pinNumber, !digitalRead(pinNumber)); // toggle pin

        // Build pinstate string. The Arduino replies to the browser with this string.
        String pinState = "Pin ";
        pinState+=pinNumber;
        pinState+=" is ";
        if(digitalRead(pinNumber)) // check if the pin is ON or OFF
        {
          pinState+="ON"; // the pin is on
        }
        else
        {
         pinState+="OFF";  // the pin is off
        }
        // build HTTP header Content-Length string.
        String contentLength="Content-Length: ";
        contentLength+=pinState.length(); // the value of the length is the lenght of the string the Arduino is replying to the browser with.

        // send HTTP header
        wiflyUart.println("HTTP/1.1 200 OK");
        wiflyUart.println("Content-Type: text/html; charset=UTF-8");
        wiflyUart.println(contentLength); // length of HTML code
        wiflyUart.println("Connection: close");
        wiflyUart.println();
        // send response
        wiflyUart.print(pinState);
      }
      else
      {
        // send HTTP header
        wiflyUart.println("HTTP/1.1 200 OK");
        wiflyUart.println("Content-Type: text/html; charset=UTF-8");
        wiflyUart.println("Content-Length: 540"); // length of HTML code
        wiflyUart.println("Connection: close");
        wiflyUart.println();

        // send webpage's HTML code
        wiflyUart.print("<html>");
        wiflyUart.print("<head>");
        wiflyUart.print("<title>WiFi Shield Webpage</title>");
        wiflyUart.print("</head>");
        wiflyUart.print("<body>");
        wiflyUart.print("<h1>LED Toggle Webpage</h1>");
  
  // In the <button> tags, the ID attribute is the value sent to the arduino via the "pin" GET parameter
        wiflyUart.print("<button id=\"11\" class=\"led\">Toggle Pin 11</button> "); // button for pin 11
        wiflyUart.print("<button id=\"12\" class=\"led\">Toggle Pin 12</button> "); // button for pin 12
        wiflyUart.print("<button id=\"13\" class=\"led\">Toggle Pin 13</button> "); // button for pin 13
        wiflyUart.print("<script src=\"http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js\"></script>");
        wiflyUart.print("<script type=\"text/javascript\">");
        wiflyUart.print("$(document).ready(function(){");
        wiflyUart.print("$(\".led\").click(function(){");
        wiflyUart.print("var p = $(this).attr('id');"); // get id value (i.e. pin13, pin12, or pin11)
        // send HTTP GET request to the IP address with the parameter "pin" and value "p", then execute the function
  
// IMPORTANT: dont' forget to replace the IP address and port with YOUR shield's IP address and port
        wiflyUart.print("$.get(\"http://192.168.43.14:80/a\", {pin:p},function(data){alert(data)});");// execute get request. Upon return execute the "function" (display an alert with the "data" send back to the browser.
        wiflyUart.print("});");
        wiflyUart.print("});");
        wiflyUart.print("</script>");
        wiflyUart.print("</body>");
        wiflyUart.print("</html>");
      }
      Serial.println("Data sent to browser");
    }
  }

}
3#
 樓主| 發表於 2015-4-27 16:12:08 | 只看該作者
我使用了官網公佈的範例,但我使用手機分享AP仍然無法開啟網頁,但使用家裡的分享器連線就可以開啟網頁。
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-6-5 21:37 , Processed in 0.162226 second(s), 8 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表