UDPApp sketch
From AsyncLabs Documentation
The UDPApp sketch included in the WiShield library implements an application which starts a UDP endpoint on UDP port 12344 waiting for data from other UDP endpoints. The main purpose of the sketch is to demonstrate a simple UDP application which can be used to setup communication between two network devices using the UDP protocol. The connection handler for the sketch is defined in the udpapp.c file. A simple PC application is provided below which shows how the sketch can be used to talk to the Arduino over WiFi. The UDPApp sketch exposes all the parameters (wireless and stack related) that need to be configured to use the WiShield.
IMPORTANT NOTE:
The WiShield library includes support for multiple applications. The individual apps are included as sample sketches but switching between the applications will require rebuilding the WiShield library as it requires reconfiguration of some variables. The library is configured as a webserver by default. To switch between modes:
- Uncomment the appropriate mode, APP_UDPAPP in the apps-conf.h file in the WiShield directory.
- Modify uip-conf.h as follows to enable UDP in the network stack,
#define UIP_CONF_UDP 1 #define UIP_CONF_UDP_CHECKSUMS 1 #define UIP_CONF_BROADCAST 1
- Delete all the object files (files with an extension of '.o') in the WiShield directory.
- Delete the 'applet' directory in the WiShield/examples/UDPApp/ directories, if one exists.
- Restart the Arduino IDE to rebuild the library in the appropriate mode.
Configuration parameters:
- local_ip : This is the static IP address that is assigned to the WiShield. The stack does not include support for DHCP and therefore a static IP address is used in the sketch. Assign a valid IP address to this parameter considering the configuration of your network.
- gateway_ip : This is the IP address of the router or gateway device attached to your network. This IP address will be used by the WiShield if it has to talk to another device which is on a different network or on the Internet.
- subnet_mask : This is the subnet mask of the network.
- SSID : This is the SSID of the wireless network
- security_type : This parameter specifies the type of security used on the wireless network. Assign a value to match the configuration of your network.
- security_passphrase : This parameter is used if WPA/WPA2 security is used on the wireless network. This parameter has to match the passphrase used for your wireless network
- wep_keys : This parameter specifies the WEP keys that are used if WEP security is enabled on the wireless network. The example included is for a WEP 128-bit configuration. The length of the table corresponds to the length required to store 4 WEP keys. If your network uses more than one key or if it uses WEP 64-bit keys then modify the table accordingly. The length of the table should not change.
- wireless_mode : This parameter can be WIRELESS_MODE_INFRA or WIRELESS_MODE_ADHOC. It specifies the mode used by WiShield to setup the network. If WIRELESS_MODE_INFRA is selected, the WiShield connects to an AP and if WIRELESS_MODE_ADHOC is selected, the WiShield connects to an adhoc network if one exists or else will start a new adhoc network with the specified SSID.
Sample PC network application:
This sample code can be compiled to run on a PC and used to communicate with the Arduino over WiFi using the UDPApp sketch and the WiShield library.
/* * udp_endp.c * * a UDP endpoint demo application for use with WiShield UDPApp sketch */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #define REMOTE_PORT 12344 // port on Arduino + WiShield #define LOCAL_PORT 12345 // port on the PC #define MAXDATASIZE 100 int main() { int sockfd, numbytes; char buf[MAXDATASIZE] = "Hello"; struct sockaddr_in remote, local; size_t addr_len; unsigned char buf_len; memset(&remote, 0, sizeof(remote)); // setup IP address of WiShield remote.sin_family = AF_INET; remote.sin_port = htons(REMOTE_PORT); inet_pton(AF_INET, "192.168.1.2", &remote.sin_addr); // setup socket and connect if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { perror("socket"); exit(0); } // bind to a specific port on the PC local.sin_family = AF_INET; local.sin_port = htons(LOCAL_PORT); local.sin_addr.s_addr = INADDR_ANY; if ( bind(sockfd, (struct sockaddr *)&local, sizeof(local)) == -1) { perror("bind"); exit(0); } // send "Hello" to WiShield if ((numbytes = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&remote, sizeof(remote))) == -1) { perror("sendto"); exit(1); } buf[numbytes] = '\0'; printf("%s\n", buf); // Wait for response addr_len = sizeof(remote); if ((numbytes = recvfrom(sockfd, buf, MAXDATASIZE-1 , 0, (struct sockaddr *)&remote, &addr_len)) == -1) { perror("recvfrom"); exit(1); } buf[numbytes] = '\0'; printf("%s", buf); // Get name from user scanf("%s", buf); buf_len = strlen(buf); buf[buf_len] = '\n'; // Send response to WiShield if ((numbytes = sendto(sockfd, buf, buf_len+1, 0, (struct sockaddr *)&remote, sizeof(remote))) == -1) { perror("sendto"); exit(1); } // Wait for greeting addr_len = sizeof(remote); if ((numbytes = recvfrom(sockfd, buf, MAXDATASIZE-1 , 0, (struct sockaddr *)&remote, &addr_len)) == -1) { perror("recvfrom"); exit(1); } buf[numbytes] = '\0'; printf("%s", buf); close(sockfd); return 0; }