极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 34140|回复: 4

arduino w5100网络模块双向通信

[复制链接]
发表于 2014-3-19 15:24:57 | 显示全部楼层 |阅读模式
           我在描述我所做的内容的时候可能出现不准确的地方

     最近查了很多资料研究w5100的网络双向通信,代码研究了很多但是发现问题也不少。
1.大多是上传数据到服务器 HTTP GET AND POST方式上传数据 实现起来很简单 我在apache web server 上写了个发送电子邮件的脚本 接收post数据后发送邮件
2.反向控制 arduino 一直是个问题原因是在网络上很难找到 相关资料!看看arduino ide 的实例中有udp传送协议的实例程序,自己在网络上使用php发送udp包的方法发送数据到arduino
3.效率问题 在将代码一番整合以后 发现效率极低 并严重影响程序的正常运行。自己在网络上一番查找之后确定使用 pt库(Protothreads)开发类并行程序 当然我对arduino的编程语言很不在行!所以我在描述我所做的内容的时候可能出现不准确的地方

硬件环境 ARDUINO UNO R3  + W5100 网络盾板

结合以上几点写了一个程序 请看下面代码

UDP 发送部分 复制php代码请 点击这里查看 http://www.iiexe.com/13336.html



因为arduino 只能在内网进行服务 因其没有拨号能力只能使用路由器进行端口转发或者类似操作!这里我测试时是在内网电脑上架设的apache2 php 服务器 设置的arduino ip 192.168.1.177

具体代码请看 http://www.iiexe.com/13338.html
  1. #include <pt.h>//ProtoThreads必须包含的头文件
  2. #include <SPI.h>         // 加载SPI
  3. #include <Ethernet.h>     //加载网络库
  4. #include <EthernetUdp.h>  // 加载UDP 库UDP library from: [email][email protected][/email] 12/30/2008
  5. /***************************************UDP AND BOOT*****************************************************/
  6. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  7. IPAddress ip(192, 168, 1, 177); //设置本机IP

  8. unsigned int localPort = 8888;      // 监听端口local port to listen on

  9. // buffers for receiving and sending data
  10. char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
  11. char  ReplyBuffer[] = "acknowledged";       // a string to send back

  12. // An EthernetUDP instance to let us send and receive packets over UDP
  13. EthernetUDP Udp;

  14. /********************************************************************************************/
  15. static int counter1,counter2,state1=0,state2=0; //counter为定时计数器,state为状态

  16. static int protothread1(struct pt *pt) //线程1,
  17. {  
  18.   PT_BEGIN(pt);  //线程开始
  19.   while(1) //每个线程都不会死
  20.   {  
  21.     PT_WAIT_UNTIL(pt, counter1==2); //如果时间满了1秒,则继续执行,否则记录运行点,退出线程1
  22.     counter1=0; //计数器置零
  23.    /************************************************************************************/
  24.    int packetSize = Udp.parsePacket();
  25.   if(packetSize)
  26.   {
  27.     Serial.print("Received packet of size ");
  28.     Serial.println(packetSize); //包大小

  29.     // read the packet into packetBufffer
  30.     Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);

  31.     Serial.println(packetBuffer); //输出字符串 可以对输出的字符串进行分析并使arduino 做出相应动作

  32.     // send a reply, to the IP address and port that sent us the packet we received
  33.     Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  34.     Udp.write(ReplyBuffer);
  35.     Udp.endPacket();
  36.   }

  37.    /////////////////////////////////////////////////////////////////////////////////////
  38.   }

  39.   PT_END(pt); //线程结束
  40. }

  41. static int protothread2(struct pt *pt) //线程2,
  42. {
  43.   PT_BEGIN(pt); //线程开始
  44.   while(1) {    //每个线程都不会死

  45.     PT_WAIT_UNTIL(pt, counter2==2); //如果时间满了5秒,则继续执行,否则记录运行点,退出线程2
  46.     counter2=0;  //计数清零
  47.     EthernetClient client;
  48.   /**************************************************************************************/
  49. String data;
  50. data+="";
  51. data+="data="+analogRead(A1); // Use HTML encoding for comma's
  52. data+="&submit=Submit"; // Submitting data

  53. if (client.connect("www.iiexe.com",80)) {
  54. Serial.println("connected");
  55. client.println("POST /arduino/post.php HTTP/1.1");
  56. client.println("Host: www.iiexe.com");
  57. client.println("Content-Type: application/x-www-form-urlencoded");
  58. client.println("Connection: close");
  59. client.print("Content-Length: ");
  60. client.println(data.length());
  61. client.println();
  62. client.print(data);
  63. client.println();

  64. //Prints your post request out for debugging
  65. Serial.println("Host: www.iiexe.com");

  66. }

  67. if (client.connected()) {
  68. Serial.println();
  69. Serial.println("disconnecting.");
  70. client.stop();
  71. }

  72.   /***************************************************************************************/
  73.   }
  74.   PT_END(pt);  //线程结束
  75. }

  76. static struct pt pt1, pt2;
  77. void setup()
  78. {
  79. /***********************************初始化*********************************************/
  80.    //设置 初始化Ethernet and UDP  start the Ethernet and UDP:
  81.   PT_INIT(&pt1);  //线程1初始化
  82.   PT_INIT(&pt2);  //线程2初始化
  83.   Ethernet.begin(mac,ip);
  84.   Udp.begin(localPort);
  85.   Serial.begin(9600);

  86. /*********************************************************************************/
  87.   pinMode(12,OUTPUT);//设置引脚D12
  88.   pinMode(13,OUTPUT);//设置引脚D13

  89. }

  90. void loop () //这就是进行线程调度的地方
  91. {
  92.     protothread1(&pt1);  //执行线程1
  93.     protothread2(&pt2);  //执行线程2
  94.     delay(1000);  //时间片,每片1秒,可根据具体应用设置大小
  95.     counter1++;
  96.     counter2++;
  97.   }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2014-3-20 11:41:23 | 显示全部楼层
谢谢分享 学习了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-3-20 13:19:26 | 显示全部楼层
共同努力..................
回复 支持 反对

使用道具 举报

发表于 2014-11-15 11:51:02 | 显示全部楼层
服务器端的代码是什么能否贴一下
回复 支持 反对

使用道具 举报

发表于 2015-5-29 20:07:30 | 显示全部楼层
收下了
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-16 21:14 , Processed in 0.036957 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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