php传输数据arduino,使用php控制arduino UDP方法

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 // needed for Arduino versions later than 0018

#include

#include // 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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值