#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 的正反轉接腳
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
}
// 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: ";
// 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");
=== 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.
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:
#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
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 --------");
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
}
// 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.
// 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");
}
}