|
本帖最後由 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");
}
}
} |
|