Fireduino/WiFi Udp

来自Firefly wiki
跳转至: 导航搜索

Fireduino WiFi UDP

Freaduino SDK provides a comprehensive API for wifi.This API is compatible with the Arduino WiFi Shield example.

Fireduino WiFi UDP process

UDP is a connectionless protocol,the source side and terminal does not establish a connection before the transmission data,when it wants to transmit simply to grab data from the application,and threw it on the network ASAP.In the sender,the limitations of UDP transmission speed is only the speed of data generated by the application,the processor's processing capacity and transmission bandwidth;at the receiving end,UDP segment on each message queue,the application each read a paragraph from the message queue.

The first step:

Create a WiFiUDP object,then call the begin object function,to initialize the UDP object and began to listen on port,and the parameters for the need to monitor the port number.

The second step:

Received data

If the UDP port upon receiving the data sent by the remote,by calling the parsePacket object function can obtain packet size.Then by calling the read object function to read data.

Transmission data

Send UDP packets need the beginPacket object function to specify the remote host IP address and port number,after the call write object function to write data,and called endPacket object function to complete set of package,then sent into the network.

The three step:

After completing the data transfer can be called the stop function of UDP object to release the UDP resources.

Example program --UDP Receive & Send Message

#include <WiFi.h>
#include <WiFiUdp.h>
int status = WL_IDLE_STATUS;
char ssid[] = "yourNetwork"; //  your network SSID (name)
char pass[] = "secretPassword";    // your network password
unsigned int localPort = 2390;      // local port to listen on
char packetBuffer[255]; //buffer to hold incoming packet
char  ReplyBuffer[] = "acknowledged";       // a string to send back
WiFiUDP Udp;
void setup() {
  //Initialize serial and wait for port to open:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port 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);
  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 SSID: ");
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
  Serial.println("Connected to wifi");
  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    Serial.print("Received packet of size ");
    Serial.print("From ");
    IPAddress remoteIp = Udp.remoteIP();
    Serial.print(", port ");
    // read the packet into packetBufffer
    int len =, 255);
    if (len > 0) {
      packetBuffer[len] = 0;
    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(" dBm");