极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 17224|回复: 5

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

[复制链接]
发表于 2014-3-5 21:59:54 | 显示全部楼层 |阅读模式
现在能实现局域网控制led灯的开关,现在求怎么通过外网控制led开关?需要服务器吗?需要什么技术 求指点
回复

使用道具 举报

 楼主| 发表于 2014-3-5 22:07:02 | 显示全部楼层

  1. #include <SPI.h>
  2. #include <Ethernet.h>

  3. int  red = 9;       //设置输出端口
  4. int i ;
  5. int red_color,asc;//定义各颜色的PWM值参数
  6. String POST = "";
  7. String SET = "";
  8. // Enter a MAC address and IP address for your controller below.
  9. // The IP address will be dependent on your local network:
  10. byte mac[] = {
  11.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  12.   //服务端IP地址
  13. IPAddress ip(192,168,1,178);

  14. // Initialize the Ethernet server library
  15. // with the IP address and port you want to use
  16. // (port 80 is default for HTTP):
  17. EthernetServer server(80);

  18. void setup() {
  19. // Open serial communications and wait for port to open:
  20.   Serial.begin(9600);
  21.   Ethernet.begin(mac, ip);
  22.   server.begin();
  23.   pinMode(red, OUTPUT);
  24.   Serial.print("server is at ");
  25.   Serial.println(Ethernet.localIP());
  26.   //设置端口为输出
  27. }
  28. void loop() {
  29.   // listen for incoming clients
  30.   EthernetClient client = server.available();
  31.   if (client) {
  32.     Serial.println("new client");
  33.     // an http request ends with a blank line-----------
  34.     boolean currentLineIsBlank = true;
  35.     while (client.connected()) {
  36.       if (client.available()) {
  37.         char c = client.read();
  38.         Serial.write(c);
  39.         // if you've gotten to the end of the line (received a newline
  40.         // character) and the line is blank, the http request has ended,
  41.         // so you can send a reply
  42.         if (c == '\n' && currentLineIsBlank){
  43.           // it is after the double cr-lf that the variables are
  44.           // read another line!
  45.           String POST = "";
  46.          // int p = 1;
  47.           while(client.available())
  48.           {           
  49.             c = client.read();
  50.             // save the variables somewhere
  51.             POST += c;
  52.           }
  53.           // send a standard http response header
  54.           //打印输出网页代码
  55.          client.println("<html><head><title>Lamaq Color palette</title>"
  56.                         "<style>*{font-family:Arial;}body{background-color:#f2f2f2;}h1{color:#222;}</style>"
  57.                         "<body><div style='text-align:center;'>"
  58.                         "<div>Color palette"
  59.                         "<table width='30%'  align='center'>"
  60.                         "<tr><td>"
  61.                         "<table width='100%' height='100'  cellspacing='1'><tbody><tr>");
  62.         /*********************************************************/        
  63.          client.println("</tr></tbody></table>");
  64.          client.println("<table  width='100%'  id='led'>");
  65.          client.print("<tbody><tr>");            
  66.          client.print("<form method='post' id='ledform'>Led color:");
  67.          client.print("<input type='text' name='led' id='led' value='");
  68.          /*******************************************************************/
  69.          //  for (int j=0; j<10; j++){
  70.                       i = int(POST[4]);
  71.                       if(i==49){red_color=240;}
  72.                       if(i==48){red_color=0;  }
  73.            //       }
  74.           // i = int(POST[5]);
  75.           //  {red_color=240;client.print(POST[5]);}
  76.           /*********************************************************/           
  77.                        client.print("'>");           
  78.          client.println("<input type='submit' value='Submit'>"
  79.                         "</form></tr></tbody></table></td></tr></table></div></div></body></html>");   
  80.           break;
  81.         }
  82.         if (c == '\n') {
  83.           // you're starting a new line
  84.           currentLineIsBlank = true;
  85.         }
  86.         else if (c != '\r') {
  87.           // you've gotten a character on the current line
  88.           currentLineIsBlank = false;
  89.         }
  90.       }
  91.     }
  92.     // give the web browser time to receive the data
  93.     delay(1);
  94.     // close the connection:
  95.    client.stop();
  96.     Serial.println("client disonnected");
  97.    // i=1;
  98.   }
  99.     analogWrite(red, red_color);
  100. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-3-5 22:07:33 | 显示全部楼层
上面是局域网代码?怎么弄能实现 外网控制?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-3-5 23:26:44 | 显示全部楼层
急需,各位大师,给点指点。
回复 支持 反对

使用道具 举报

发表于 2014-11-29 09:19:41 | 显示全部楼层
现在做到了吗?
回复 支持 反对

使用道具 举报

发表于 2014-11-30 14:18:23 | 显示全部楼层
fuhome.net

费用就是一个arduino 再加一个网络模块
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-16 18:56 , Processed in 0.035143 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表