我正试图通过网页与我的Arduino以太网交谈并更改led的rgb值。我按照我想要的方式设置了网页。我唯一的问题是使“设置颜色”按钮返回文本框中的红色,绿色和蓝色值,并设置这些值以使LED更改颜色。如何将我输入的值输入文本框并在getColors()中设置它们?
#include <SPI.h>
#include <Ethernet.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = { /*MAC Address*/};
IPAddress ip(/*IP of Arduino server*/);
EthernetServer server(80); // create a server at port 80
String fileName="test.txt";
int ledRed = 3;
int ledGreen = 5;
int ledBlue = 6;
int sRed,sGreen,sBlue;
void setup()
{
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
pinMode(ledRed, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledBlue, OUTPUT);
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if(client) // got client?
{
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// send web page
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Arduino Web Page</title>");
client.println("</head>");
client.println("<body>");
client.println("<h1>Hello from Arduino!</h1>");
client.println("<p>A web page from my Arduino server</p>");
client.println("<p style=\"color:red\">Red<form><input type=\"text\"></form></p>");
client.println("<p style=\"color:blue\">Blue<form><input type=\"text\"></form></p>");
client.println("<p style=\"color:green\">Green<form><input type=\"text\"></form></p>");
client.println("<button type=\"button\"onclick=\"GetColors()\">Set Color</button>");
client.println("</body>");
client.println("</html>");
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
void getColors(EthernetClient cl)
{
if(Serial.available())
{
//sRed=???
//sBlue=???
//sGreen=???
analogWrite(ledRed,sRed);
analogWrite(ledBlue,sBlue);
analogWrite(ledGreen,sGreen);
}
else
Serial.println("Unacceptable; try again.");
}
答案 0 :(得分:0)
您正在创建一个网页。现在,您需要使用get / post请求获取该网页的HTML,并将您获得的结果解析为文本(或构建/查找您可以使用的现有解析器)并获取所需的值。
您也可以使用您希望从发布的网页获取的任何标记/属性/文本来执行此操作。
虽然如果您是发布网页的人,您已经知道这些价值观,除非可以通过网络浏览器进行更改,然后您需要执行上面所写的操作。
答案 1 :(得分:0)
您可以使用此库https://github.com/sirleech/Webduino。 它有一个RGB值的例子。
或者如果您不介意让计算机运行网络服务器,您可以建立一个漂亮的网页并通过telnet将数据发送到arduino。 (PHP)