6651浏览
查看: 6651|回复: 2

[进阶] 教你如何完美使用Ethernet模块

[复制链接]
本帖最后由 何处不江南 于 2013-11-21 13:50 编辑

今天在QQ空间找到了Arduino炫项目,是通过web来控制电器,地址为:http://user.qzone.qq.com/1485592 ... &pos=1366299761,分享给大家,以下是正文,颤抖吧:大致步骤:

访问服务器端WEB主页面》》》
主页面向Arduino发送POST请求数据》》》
Arduino接收并解码主页的请求,同时将变更的值进行合成出子页面,传给主页面的iframe》》》

主页面延时一定时间,等待Arduino反馈的页面并进行局部iframe刷新>OK!

恩,我自己看都觉得复杂,不过响应速度相当好!(平均20ms内)

老子好像变成程序员了 = =!

教你如何完美使用Ethernet模块图1

WEB端程序:

  1. <!doctype html>
  2. <html lang="en">
  3. <head><meta charset="utf-8" />
  4. <meta name="viewport" content="width=200px"/>
  5. <title>Light-Microduino</title>
  6. <link rel="stylesheet" href="jquery-ui.css" />
  7. <style>
  8. .STYLE4 {
  9.     color:#CCCCCC;
  10.     margin: 0 auto;
  11.     bottom: 10%;
  12.     left:42%;
  13. }
  14. .STYLE6 {
  15.     font-size: medium;
  16.     color: #CCCCCC;
  17.     font-family: Arial, Helvetica, sans-serif;
  18. }
  19. .STYLE8 {
  20.     font-size: x-large;
  21.     color: #CCCCCC;
  22.     font-family: Arial, Helvetica, sans-serif;
  23. }
  24. .STYLE9 {font-size: large}
  25. .STYLE10 {font-size: small}
  26. </style>
  27. <script src="jquery-1.9.1.js"></script>
  28. <script src="jquery-ui.js"></script>
  29. <script>
  30. function show(){
  31.         $("#s1").attr("src", "http://192.168.1.122");
  32.         document.getElementById("#s1").contentDocument.location.reload(true);
  33. }
  34.     $(document).ready(function (){
  35.         $("#button10").click(function(){
  36.         status1=10;
  37.         $.post("http://192.168.1.122",{"lightstatus":status1});
  38.         t = setTimeout("show()",60);
  39.         });
  40.         $("#button11").click(function(){
  41.         status1=11;
  42.         $.post("http://192.168.1.122",{"lightstatus":status1});
  43.         t = setTimeout("show()",60);
  44.         });
  45.         $("#button20").click(function(){
  46.         status2=20;
  47.         $.post("http://192.168.1.122",{"lightstatus":status2});
  48.         t = setTimeout("show()",60);
  49.         });
  50.         $("#button21").click(function(){
  51.         status2=21;
  52.         $.post("http://192.168.1.122",{"lightstatus":status2});
  53.         t = setTimeout("show()",60);
  54.         });
  55.         
  56.         $("#button30").click(function(){
  57.         status3=30;
  58.         $.post("http://192.168.1.122",{"lightstatus":status3});
  59.         t = setTimeout("show()",60);
  60.         });
  61.         $("#button31").click(function(){
  62.         status3=31;
  63.         $.post("http://192.168.1.122",{"lightstatus":status3});
  64.         t = setTimeout("show()",60);
  65.         });
  66.         
  67.         $("#button40").click(function(){
  68.         status4=40;
  69.         $.post("http://192.168.1.122",{"lightstatus":status4});
  70.         t = setTimeout("show()",60);
  71.         });
  72.         $("#button41").click(function(){
  73.         status4=41;
  74.         $.post("http://192.168.1.122",{"lightstatus":status4});
  75.         t = setTimeout("show()",60);
  76.         });
  77.         
  78.         clearTimeout(t);
  79.         
  80.         });
  81. </script>
  82. </head>
  83. <div id="zoom">
  84. <body style="background-color:black";>
  85. <div style="width:540px; height:920px; top:44%; margin:0 auto; padding-left:1%; padding-right:1%; "  >
  86. <div style="margin:0 auto; margin-left: 7px; margin-top:-46px;">
  87.     <iframe id="s1" name="s1" src="http://192.168.1.122" frameborder="0" scrolling="no" height="115" width="520"></iframe>
  88. </div>
  89. <div style="font:bold 14px verdana; text-align:left; margin-top: -36px;">
  90. <br/>
  91. <br/>
  92. <table width="510" height="182" border="0">
  93.   <tr>
  94.     <td><button type="button" id="button10" style="background:url('OFF.png');border-style:none;width:115px;height:44px;
  95. background-repeat:no-repeat;"/>?</td>
  96.     <td><button type="button" id="button20" style="background:url('OFF.png');border-style:none;width:115px;height:44px;
  97. background-repeat:no-repeat;"/>?</td>
  98.     <td><button type="button" id="button30" style="background:url('OFF.png');border-style:none;width:115px;height:44px;
  99. background-repeat:no-repeat;"/>?</td>
  100.     <td><button type="button" id="button40" style="background:url('OFF.png');border-style:none;width:115px;height:44px;
  101. background-repeat:no-repeat;"/>?</td>
  102.   </tr>
  103.   <tr>
  104.     <td><button type="button" id="button11" style="background:url('ON.png');border-style:none;width:115px;height:44px;
  105. background-repeat:no-repeat;"/>?</td>
  106.     <td><button type="button" id="button21" style="background:url('ON.png');border-style:none;width:115px;height:44px;
  107. background-repeat:no-repeat;"/>?</td>
  108.     <td><button type="button" id="button31" style="background:url('ON.png');border-style:none;width:115px;height:44px;
  109. background-repeat:no-repeat;"/>?</td>
  110.     <td><button type="button" id="button41" style="background:url('ON.png');border-style:none;width:115px;height:44px;
  111. background-repeat:no-repeat;"/>?</td>
  112.   </tr>
  113.   <tr>
  114.     <td  >    <div align="center"><span class="STYLE6">NO.1</span> <span class="STYLE8">通风</span></div></td>
  115.     <td>    <div align="center"><span class="STYLE6">NO.2</span> <span class="STYLE8">加热</span></div></td>
  116.     <td>    <div align="center"><span class="STYLE6">NO.3</span> <span class="STYLE8">加湿</span></div></td>
  117.     <td>    <div align="center"><span class="STYLE6">NO.4</span> <span class="STYLE8">采光</span></div></td>
  118.   </tr>
  119. </table>
  120. </div>
  121.     <div class="STYLE4">
  122.     <div align="center" style="margin:0 auto; width: 100%;">
  123.     <span class="STYLE9">智能农业系统 </span><span class="STYLE10">Via_JJU</span>    </div>
  124. </div>
  125. </div>
  126. </body>
  127. </div>
  128. </html>
复制代码


Arduino端程序:

  1. #include <EtherCard.h>
  2. #define NUM    4
  3. #define PIN1    A0
  4. #define PIN2    A1
  5. #define PIN3    A2
  6. #define PIN4    A3
  7. int RELAY_PIN[NUM];
  8. uint8_t lightStatus[NUM];
  9. int light;
  10. static byte mymac[] = {
  11.   0x74,0x69,0x69,0x2D,0x30,0x31 };
  12. static byte myip[] = {
  13.   192,168,1,122 };
  14. static byte gwip[] = {
  15.   192,168,1,1};
  16. byte Ethernet::buffer[900];
  17. void setup () {
  18.   Serial.begin(9600);
  19.   Serial.println("Light Microduino");
  20.   if(!ether.begin(sizeof Ethernet::buffer, mymac,10))
  21.     Serial.println( "Failed to access Ethernet controller");
  22.   else
  23.     Serial.println("Ethernet controller initialized");
  24.   if(ether.staticSetup(myip, gwip)){
  25.     Serial.println("Set static IP ok!");
  26.   }
  27.   else{
  28.     Serial.println("Set static IP Failed!");  
  29.   }
  30.   RELAY_PIN[0]=PIN1;
  31.   RELAY_PIN[1]=PIN2;
  32.   RELAY_PIN[2]=PIN3;
  33.   RELAY_PIN[3]=PIN4;
  34.   for(int a=0;a<NUM;a++)
  35.   {
  36.     pinMode(RELAY_PIN[a], OUTPUT);
  37.     digitalWrite(RELAY_PIN[NUM], LOW);  
  38.     lightStatus[a]=0;
  39.   }
  40. }
  41. void loop() {
  42.   word len = ether.packetReceive();
  43.   word pos = ether.packetLoop(len);
  44.   if(pos) {
  45.     if(strstr((char *)Ethernet::buffer + pos, "POST") !=0) {
  46.       sscanf((char *)strstr((char *)Ethernet::buffer + pos, "lightstatus"),"lightstatus=%d",&light);
  47.     }
  48.     switch(light)
  49.     {
  50.     case 10:lightStatus[0]=0;break;
  51.     case 11:lightStatus[0]=1;break;
  52.     case 20:lightStatus[1]=0;break;
  53.     case 21:lightStatus[1]=1;break;
  54.     case 30:lightStatus[2]=0;break;
  55.     case 31:lightStatus[2]=1;break;
  56.     case 40:lightStatus[3]=0;break;
  57.     case 41:lightStatus[3]=1;break;
  58.     }
  59.     for(int a=0;a<NUM;a++)
  60.     {
  61.       if (lightStatus[a]){
  62.         digitalWrite(RELAY_PIN[a], HIGH);
  63.       }
  64.       else{
  65.         digitalWrite(RELAY_PIN[a], LOW);
  66.       }   
  67.         Serial.print(lightStatus[a]);  
  68.         Serial.print(",");  
  69.     }
  70.       Serial.println(" ");  
  71.     BufferFiller bfill = ether.tcpOffset();
  72.     bfill.emit_p(PSTR("HTTP/1.1 200 OK\r\n"
  73.       "Content-Type: text/html\r\n"
  74.       "Pragma: no-cache\r\n\r\n"
  75.       "<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"/>"
  76.       "<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">"
  77.       "<meta name="viewport" content="width=device-width,initial-scale=1.0">"
  78.       "<title>Button</title><style type="text/css"> body {background-color: #000000;} </style></head>"
  79.       "<body><table width="500" border="0"><tr>"
  80.       "<td><img src="http://www.mudi-china.com/pkj/arduino/GREEN/$D.jpg"></td><td><img src="http://www.mudi-china.com/pkj/arduino/GREEN/$D.jpg"></td>"
  81.       "<td><img src="http://www.mudi-china.com/pkj/arduino/GREEN/$D.jpg"></td><td><img src="http://www.mudi-china.com/pkj/arduino/GREEN/$D.jpg"></td>"
  82.     "</tr></table></body></html>"),lightStatus[0],lightStatus[1],lightStatus[2],lightStatus[3]);
  83.     ether.httpServerReply(bfill.position());
  84.   }
  85. }
复制代码





lauren  高级技师

发表于 2013-11-21 13:13:58

不错,收藏,web端家里测试能运行在电脑上吗?
回复

使用道具 举报

何处不江南  初级技匠
 楼主|

发表于 2013-11-21 13:43:47

lauren 发表于 2013-11-21 13:13
不错,收藏,web端家里测试能运行在电脑上吗?

arduino端可以编译,Web端真心无解。。 不会测试。。  求大神测试,附上Arduino端的lib下载附件ethercard_master.zip

回复

使用道具 举报

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

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail