用arduino和w5100怎么网页外网控制led灯?需要什么技术?求指点

2025-05-05 21:43:56
推荐回答(1个)
回答(1):

#include 
#include 
 
int  red = 9;       //设置输出端口
int i ;
int red_color,asc;//定义各颜色的PWM值参数 
String POST = "";
String SET = "";
// 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);
  Ethernet.begin(mac, ip);
  server.begin();
  pinMode(red, OUTPUT);
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  //设置端口为输出
}
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();
        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 = "";
         // int p = 1;
          while(client.available())
          {           
            c = client.read();
            // save the variables somewhere
            POST += c;
          }
          // send a standard http response header
          //打印输出网页代码
         client.println("Lamaq Color palette"
                        ""
                        ""
                        "
Color palette"
                        ""
                        ""
                        ""); 
        /*********************************************************/        
         client.println("");
         client.println("");
         client.print("");            
         client.print("Led color:");
         client.print("         /*******************************************************************/
         //  for (int j=0; j<10; j++){
                      i = int(POST[4]);
                      if(i==49){red_color=240;}
                      if(i==48){red_color=0;  }
           //       }
          // i = int(POST[5]);
          //  {red_color=240;client.print(POST[5]);}
          /*********************************************************/           
                       client.print("'>");           
         client.println(""
                        "
");    
          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);
}

相关问答