arduino 接收数据一向挺麻烦的!其实在arduino的实例库中有 w5100网络模块的实例!只是大家可能对PHP不怎么了解!其实php是可以发送udp报文的,所以今天写了这个文章给大家一点启发!高手请掠过,

<?php
////////////////////////////////////////////////////////////////
/**************************************************************/
/*                                                            */
/*       By:Exploit   arduino数据php发送端                    */
/*                                                            */
/**************************************************************/
////////////////////////////////////////////////////////////////

function udp_msg($value) //自定义 发送数据函数
{
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$msg = $value;
$len = strlen($msg);
socket_sendto($sock, $msg, $len, 0, '192.168.1.177', 8888);//IP地址因接收端地址设置值设定
return socket_close($sock)."成功";
}
udp_msg("exploit"); //运行函数 将发送 exploit字符串 发送中文会出现乱码的问题还是没有搞定 如果有告诉搞定了请告诉我一下

?>

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

这里是官方udp实例

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008

// 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 };
IPAddress ip(192, 168, 1, 177);

unsigned int localPort = 8888;      // local port to listen on

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

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

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);

  Serial.begin(9600);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize); //包大小

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

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

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