w5100与php通信,arduino w5100网络模块双向通信
#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++;
}
總結
以上是生活随笔為你收集整理的w5100与php通信,arduino w5100网络模块双向通信的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: H.264几大开源编码器简介
- 下一篇: 收集 QQ旋风离线下载帐号