SocketApp sketch
From AsyncLabs Documentation
The SocketApp sketch included in the WiShield library implements an application which starts a server listening on TCP port 1000 waiting for connection from other clients. The main purpose of the sketch is to demonstrate a simple server application which can be used to communicate between two network devices. It does not serve up a webpage and does not require a web browser to communicate. The connection handler for the sketch is defined in the socketapp.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 SocketApp 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 both server and client applications. The individual apps are included as sample sketches but to switch between the two 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_SOCKAPP in the apps-conf.h file in the WiShield directory.
- Delete all the object files (files with an extension of '.o') in the WiShield directory.
- Delete the 'applet' directory in the WiShield/examples/SocketApp/ 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 SocketApp sketch and the WiShield library.
// client.c // A stream socket client demo #include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #define PORT 1000 // the port WiShield is listening on #define MAXDATASIZE 100 // max number of bytes we can get at once from WiShield int main() { int sockfd, numbytes; char buf[MAXDATASIZE]; struct sockaddr_in srv; unsigned char buf_len; memset(&srv, 0, sizeof(srv)); // setup IP address of WiShield srv.sin_family = AF_INET; srv.sin_port = htons(PORT); inet_pton(AF_INET, "192.168.1.2", &srv.sin_addr); // setup socket and connect if ((sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { perror("socket"); exit(0); } if (connect(sockfd, (const void*)&srv, sizeof(srv)) == -1) { close(sockfd); perror("connect"); exit(0); } printf("connected to WiShield\n"); // Wait for welcome message from WiShield if ((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) { perror("recv"); exit(1); } buf[numbytes] = '\0'; printf("%s", buf); // Get name from user scanf("%s", buf); buf_len = strlen(buf); buf[buf_len] = '\n'; // Send user response if (send(sockfd, buf, buf_len+1, 0) == -1) { perror("send"); close(sockfd); exit(0); } // We loop here until we receive the entire response // indicated by a newline character do { if ((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) { perror("recv"); exit(1); } buf[numbytes] = '\0'; printf("%s", buf); // check for newline character if (buf[numbytes-1] != '\n') { continue; } else { break; } } while(1); // close socket close(sockfd); return 0; }