0

程式碼如下:

 

我原本是在loop()裡面去call  wifi_udp(0);

然後用udp test tool 去傳送...就可以正確的得到字串...

但是改成用thread的方式....就沒有辦法得到正確的字串....

 

#include <WiFi.h>
#include <WiFiUdp.h>
char ssid[] = "jacky_0328"; // your network SSID (name)
char pass[] = "0975128810"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status

WiFiUDP Udp;

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

char packetBuffer[255]; //buffer to hold incoming packet
void setup() {
Serial.begin(57600);
// put your setup code here, to run once:
wifi_setup();
os_thread_create(wifi_udp,0, OS_PRIORITY_REALTIME, 1024);
}
void wifi_setup(){

// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}

String fv = WiFi.firmwareVersion();
if (fv != "1.1.0") {
Serial.println("Please upgrade the firmware");
}

// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);
}
printWifiData();

Serial.println("\nStarting connection to server...");
Udp.begin(localPort);
}
void printWifiData() {
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

}
void loop() {
// put your main code here, to run repeatedly:
//wifi_udp(0);
}

void wifi_udp(const void *arg){
String str_rece;
int packetSize;

//Serial.println("thread begin");

while(1){
packetSize = Udp.parsePacket();
// Serial.println(packetSize);
if (packetSize) {
int len = Udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
Serial.println(packetBuffer);
str_rece = packetBuffer;
if(str_rece=="Reset")
{
Serial.println("receive Reset");

}else if(str_rece=="Forward"){
Serial.println("receive Forward");

}else if(str_rece=="Backward"){
Serial.println("receive Backward");

}
}
}
//delay(20);
}
}

sean 回答了