Fireduino Buy

Dual-Core Cortex-M3 processor, integrated high-quality audio Codec and WiFi module, have IOT expansion performance, perfectly compatible with the Arduino interface and Arduino IDE programming, and supports FireBlock graphics programming.

WiFi UDP

Update time:2018-04-13 Views:5657

WiFi UDP

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

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 onchar packetBuffer[255]; //buffer to hold incoming packetchar  ReplyBuffer[] = "acknowledged";       // a string to send backWiFiUDP Udp;void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  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: ");Serial.println(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:delay(10000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();
  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  Udp.begin(localPort);}void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize) {Serial.print("Received packet of size ");Serial.println(packetSize);Serial.print("From ");IPAddress remoteIp = Udp.remoteIP();Serial.print(remoteIp);Serial.print(", port ");Serial.println(Udp.remotePort());// read the packet into packetBuffferint len = Udp.read(packetBuffer, 255);if (len > 0) {  packetBuffer[len] = 0;}Serial.println("Contents:");Serial.println(packetBuffer);// send a reply, to the IP address and port that sent us the packet we receivedUdp.beginPacket(Udp.remoteIP(), Udp.remotePort());Udp.write(ReplyBuffer);Udp.endPacket();
  }}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");}