w5100与php通信,arduino w5100网络模块双向通信

该博客介绍了一个使用Arduino进行UDP网络通信的示例,结合ProtoThreads库实现多线程处理。代码中展示了如何接收和发送UDP数据包,并通过串口打印接收到的数据。此外,还展示了如何周期性地将模拟读取的数据发送到远程服务器,实现了基本的物联网设备数据上传功能。
摘要由CSDN通过智能技术生成

#include //ProtoThreads必须包含的头文件

#include          // 加载SPI

#include      //加载网络库

#include   // 加载UDP 库UDP library from: [email]bjoern@cs.stanford.edu[/email] 12/30/2008

/***************************************UDP AND BOOT*****************************************************/

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(192, 168, 1, 177); //设置本机IP

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;

/********************************************************************************************/

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

static int protothread1(struct pt *pt) //线程1,

{

PT_BEGIN(pt);  //线程开始

while(1) //每个线程都不会死

{

PT_WAIT_UNTIL(pt, counter1==2); //如果时间满了1秒,则继续执行,否则记录运行点,退出线程1

counter1=0; //计数器置零

/************************************************************************************/

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();

}

/

}

PT_END(pt); //线程结束

}

static int protothread2(struct pt *pt) //线程2,

{

PT_BEGIN(pt); //线程开始

while(1) {    //每个线程都不会死

PT_WAIT_UNTIL(pt, counter2==2); //如果时间满了5秒,则继续执行,否则记录运行点,退出线程2

counter2=0;  //计数清零

EthernetClient client;

/**************************************************************************************/

String data;

data+="";

data+="data="+analogRead(A1); // Use HTML encoding for comma's

data+="&submit=Submit"; // Submitting data

if (client.connect("www.iiexe.com",80)) {

Serial.println("connected");

client.println("POST /arduino/post.php HTTP/1.1");

client.println("Host: www.iiexe.com");

client.println("Content-Type: application/x-www-form-urlencoded");

client.println("Connection: close");

client.print("Content-Length: ");

client.println(data.length());

client.println();

client.print(data);

client.println();

//Prints your post request out for debugging

Serial.println("Host: www.iiexe.com");

}

if (client.connected()) {

Serial.println();

Serial.println("disconnecting.");

client.stop();

}

/***************************************************************************************/

}

PT_END(pt);  //线程结束

}

static struct pt pt1, pt2;

void setup()

{

/***********************************初始化*********************************************/

//设置 初始化Ethernet and UDP  start the Ethernet and UDP:

PT_INIT(&pt1);  //线程1初始化

PT_INIT(&pt2);  //线程2初始化

Ethernet.begin(mac,ip);

Udp.begin(localPort);

Serial.begin(9600);

/*********************************************************************************/

pinMode(12,OUTPUT);//设置引脚D12

pinMode(13,OUTPUT);//设置引脚D13

}

void loop () //这就是进行线程调度的地方

{

protothread1(&pt1);  //执行线程1

protothread2(&pt2);  //执行线程2

delay(1000);  //时间片,每片1秒,可根据具体应用设置大小

counter1++;

counter2++;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值