|
|
新手刚接触arduino和w5100,想实现网页局域网控制led开关,于是在网上找了一个帖子《利用arduino+w5100 把网页上的颜色输出到RGB LED上显示 》(http://www.geek-workshop.com/for ... thread&tid=8511)。我想实现最终的是在网页文本框里,输入1,灯开;输入2,灯关。于是我自己改了一下。能力有限改着改着不会改了。
1、希望大神给我不懂得代码一点点指点(代码面有//的都不怎么明白,如果没时间那就帮我解释一下//后面有数字的吧)。
2、如果我想实现最终的是在网页文本框里,输入1,灯开;输入2,灯关。该怎么改?希望大神给修改一下
代码如下:
#include <SPI.h>
#include <Ethernet.h>
int red = 9; //设置输出端口
int blue = 6;
int green = 5;
int i ;
int red_color,asc;//定义各颜色的PWM值参数
String POST = "";
String SET = "";
////网页上的颜色参数,每六字母为一种颜色,每个网页上有6个颜色,也就是36个字母
char color[]={"ff000000ff000000ffffff0000ffffffffff"
};
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//服务端IP地址
IPAddress ip(192,168,1,178);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
//设置端口为输出
pinMode(red, OUTPUT);
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available(); //
if (client) { //
Serial.println("new client"); //
// an http request ends with a blank line
boolean currentLineIsBlank = true; //
while (client.connected()) { //
if (client.available()) { //
char c = client.read(); //什么意思? c是改变的吗?接受的值是什么?
Serial.write(c); //什么意思?
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank){
// it is after the double cr-lf that the variables are
// read another line!
String POST = ""; //POST是干什么的?
// int p = 1;
while(client.available()) //
{
c = client.read(); //
// save the variables somewhere
POST += c; //
}
// send a standard http response header
//打印输出网页代码
client.println("<html><head><title>Lamaq Color palette</title>"
"<style>*{font-family:Arial;}body{background-color:#f2f2f2;}h1{color:#222;}</style>"
"<body><div style='text-align:center;'>"
"<div>Color palette"
"<table width='30%' align='center'>"
"<tr><td>"
"<table width='100%' height='100' cellspacing='1'><tbody><tr>");
/*********************************************************/
client.println("</tr></tbody></table>");
client.println("<table width='100%' id='led'>");
client.print("<tbody><tr>");
client.print("<form method='post' id='ledform'>Led color:");
client.print("<input type='text' name='led' id='led' value='");
/****网页表单采用POST方式传送到服务端,
服务端从POST参数中的第7至第12的字符中得到鼠标点击的颜色值,
并转换成ASCII码值,码值减去87或46后可得到实际数值,通过计算得到RGB的PWM值****/
for (int j=7; j<13; j++){
i = int(POST[j]);
if(i > 96 && i < 103){
asc = 87;
}else if(i > 47 && i < 58){asc = 48;
}else{client.print("#ff0000");
break;}
if(j==7){red_color = ((i-asc)*16);}
if(j==8){red_color += (i-asc);
client.print("#00ff00");}
}
/*********************************************************/
client.print("'>");
client.println("<input type='submit' value='Submit'>"
"</form></tr></tbody></table></td></tr></table></div></div></body></html>");
break;
}
if (c == '\n') { //
// you're starting a new line
currentLineIsBlank = true; //
}
else if (c != '\r') { //
// you've gotten a character on the current line
currentLineIsBlank = false; //
}
}
}
// give the web browser time to receive the data
delay(1); //
// close the connection:
client.stop(); //
Serial.println("client disonnected");
// i=1;
}
analogWrite(red, red_color);
} |
|