Friday, February 26, 2016

YellowJacket Arduino with WiShield Wifi (Part 5)

YellowJacket WiShield SimpleClient Example — 
In Part 4 of this series, the SimpleServer sketch successfully accepted an http request over wifi from the Mac. Then the YellowJacket Arduino responded to the Mac over wifi by sending an http page to the Mac’s Safari browser. In this Part 5, “#define APP_WISERVER” will again be used in “apps-conf.h” as it was in Part 4. All other code modules must be commented out to remove them from the compiled code.
// WiShield Library module apps-conf.h must be modified to use WiServer code
//Include the header file for the application(s) we use in our project.
//define APP_WEBSERVER
//#define APP_WEBCLIENT
//#define APP_SOCKAPP
//#define APP_UDPAPP
#define APP_WISERVER
2) SimpleClient example WiServer sketch: SimpleClient is one of the WiShield Library example sketches that uses WiServer to request a web page from a remote host and return it to the client YellowJacket through Wifi. The YJ sketch sends a request over wifi to the the Network SSID (the Apple router in the local network) at the gateway_ip address. The request is then sent over TCP_IP through the internet to the remote weather server. The remote weather server sends back the http web page to the local Network SSID which then transmits the web page over wifi to the YJ. The YJ sketch then sends the response over the FTDI/USB cable to the Mac for display on the Serial Monitor. The example SimpleClient sketch (named “SimpleGetWeatherClient_V3”) was edited to use the YellowJacket local_ip and local network Wifi login info, and saved as SimpleGetWeatherClient_V3.ino. After downloading the compiled sketch to the YJ Arduino, the Arduino Serial Monitor on the Mac displays the progress of the YJ Arduino app. After the “WiServer.init(NULL);”, there will be a 30 second delay. Just be patient, the green LED (pin D9) will light on the YJ board and the app will send the request then listen for the server response over wifi.
 
=== Initializing WiServer as Client to display WX web page ===
=== WiServer is running... ===
>>> Request response on 10 sec intervals... 
The request “GETrequest getWeather(wx_ip, 80, “w1.weather.gov”, “/data/METAR/KLAX.1.txt”);”  is sent to the wx_ip address, which then responds with an http web page which is sent by the local Network SSID over wifi to YJ56 and displayed on Serial Monitor:
Connected to w1.weather.gov
TX 87 bytes
RX 0 bytes from w1.weather.gov
RX 329 bytes from w1.weather.gov
HTTP/1.0 200 OK
Server: Apache/2.2.15 (Red Hat)
X-NIDS-ServerID: www6.mo
Last-Modified: Fri, 26 Feb 2016 14:57:04 GMT
Accept-Ranges: bytes
Content-Length: 116
Content-Type: text/plain; charset=iso-8859-1
Date: Fri, 26 Feb 2016 15:00:12 GMT Connection: close 

000 
SAUS80 KWBC 221400 RRH 
MTRLAX METAR KLAX 221453Z 00000KTRX 56 bytes from w1.weather.gov 
 10SM FEW020 15/10 A3006 RMK AO2 SLP179 
T01500100 53010 
Ended connection with w1.weather.gov
The Arduino SimpleGetWeatherClient_V3 sketch code:
/* SimpleGetWeatherClient_V3
 *
 * A simple sketch that uses WiServer to get the 10-second weather data from LAX and prints
 * it via the Serial API
 *
 * Compiled with WiShield_V3 library
 *
 * Uses apps-conf.h "#define APP_WISERVER"
 *
 * Serial Monitor Response from
 * "GETrequest getWeather(wx_ip, 80, "w1.weather.gov", "/data/METAR/KLAX.1.txt");"
 *
 */
//#include <WiShield.h>
#include <WiServer.h>

#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {10,0,1,56};  // IP address of WiShield
unsigned char gateway_ip[] = {10,0,1,1};  // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0};  // subnet mask for the local network
const char ssid[] PROGMEM = {"Network"};   // max 32 bytes

const char* myhost = "WiServer";
char* myhost_ip = "10.0.1.56";

unsigned char security_type = 3;  // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const char security_passphrase[] PROGMEM = {"Network_Passcode"};  // max 64 characters

// WEP 128-bit keys
// sample HEX keys
const uint8_t wep_keys[] PROGMEM = {  0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
                  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
                  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
                  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  // Key 3
                };

// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------

// Function that prints data from the server
void printData(char* data, int len) {

 // Print the data returned by the server
 // Note that the data is not null-terminated, may be broken up into smaller packets, and
 // includes the HTTP header.
 while (len-- > 0) {
 Serial.print(*(data++));
 }
}

//uint8 wx_ip[] = {140,90,113,200}; // old IP Address for www.weather.gov
uint8_t wx_ip[] = {23, 67, 242, 42}; // IP Address for w1.weather.gov (2016-02-25)

// A request that gets the latest METAR weather data for LAX
GETrequest getWeather(wx_ip, 80, "w1.weather.gov", "/data/METAR/KLAX.1.txt");

void setup() {
 
 // Enable Serial output
 Serial.begin(115200);

 Serial.println (" ");
 Serial.println ("=== Initializing WiServer as Client to display WX web page ===");

 // Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages)
 WiServer.init(NULL);

 // Ask WiServer to generate log messages (optional)
 WiServer.enableVerboseMode(true);

 // Have the processData function called when data is returned by the server
 getWeather.setReturnFunc(printData);

 Serial.println (" ");
 Serial.println ("=== WiServer is running... ===");
 Serial.println (">>> Request response on 10 sec intervals... ");
 Serial.println (" ");

}

// Time (in millis) when the data should be retrieved 
unsigned long tempTime = millis();
unsigned long updateTime = tempTime;
unsigned long waitTime = 1000UL * 10UL ; // must use unsigned long numbers

void loop(){

 tempTime = millis();
 
 // Check if it's time to get an update
 if (tempTime >= updateTime) {
 Serial.println (" "); // add blank line to Serial Monitor display
 getWeather.submit(); 
 /* DEBUG
 Serial.println (" ");
 Serial.print (" >> If 1: millis() = ");
 Serial.print ( tempTime );
 Serial.print (" updateTime = ");
 Serial.print ( updateTime );
 Serial.print (" waitTime = ");
 Serial.println ( waitTime );
 End DEBUG */
 // Get another update 10 seconds from now
 updateTime += waitTime;
 /* DEBUG
 Serial.print (" Revised updateTime = ");
 Serial.println ( updateTime );
 End DEBUG */
 }
 
 // Run WiServer
 WiServer.server_task();
 
 delay(10);
}
 (Feb 26, 2016)

No comments:

Post a Comment