我正在尝试通过 Wi-Fi 网络将信息从 arduino 板发送到我的计算机。出于我项目的目的,它必须是一个 UDP 连接,我使用“发送和接收 UDP 字符串”示例(http://arduino.cc/en/Tutorial/WiFiSendReceiveUDPString)进行了一些更改:
#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>
int status = WL_IDLE_STATUS;
char ssid[] = "itay_net"; //  your network SSID (name) 
char pass[] = "0527414540";    // your network password (use for WPA, or use as key for WEP)
unsigned int localPort = 50505;      // local port to listen on
IPAddress remote_ip(192, 168, 1, 100);
unsigned int remote_port = 50505;
char  ReplyBuffer[] = "acknowledged";       // a string to send back
WiFiUDP Udp;
void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600); 
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present"); 
    // don't continue:
    while(true);
  } 
  // 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);
  }
  // you're connected now, so print out the data:
  Serial.print("You're connected to the network");
  delay(10000);
  printWifiStatus();
  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  Udp.begin(localPort);  
}
void loop() {
  int bite_send;
  Udp.beginPacket(remote_ip, remote_port);
  bite_send = Udp.write("hello");
  Udp.endPacket();
  Serial.println("the packet was sent");
  Serial.println(bite_send);
  delay(1000);
}
void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}
它编译并连接到网络就好了。唯一的问题是我无法判断数据包是否已发送,因为我在 Wireshark 上看不到它的踪迹。我还在 java 上编写了一个套接字,它侦听端口(50505)并应该显示来自数据包的消息,但它也不起作用。(我可以在此处复制 Java 代码,但我可以向您保证这不是问题,因为我使用不同的 Java 服务器对其进行了测试并且它工作正常,所以问题应该出在 Arduino 方面)
缩小范围的一些细节:我相信“远程 ip”是正确的,但即使不是 - 我仍然应该在 Wireshark 中看到它,所以这不是问题。我应该提一下 Wi-Fi 防护罩的工作原理,我成功发送了 ping 并运行了其他示例(例如 SimpleWebServerWifi)。
我使用的是原装 Arduino Uno R3 板和原装 Wi-Fi 扩展板。arduino IDE 是最新版本。我使用在 GitHub 上找到的最新更新更新了 Wi-Fi 防护罩。
我还在我的以太网屏蔽上运行了相同的“发送和接收 UDP 字符串”代码(进行了必要的更改),它确实有效。
我不知道还能尝试什么 - 请帮忙。任何帮助将不胜感激。
伊泰