Thursday, February 25, 2016

YellowJacket Arduino with WiShield Wifi (Part 4)

YellowJacket WiShield SimpleServer Example — 
Hardware Network: The network setup for software development was an Apple ethernet/wifi router at ip address //10.0.1.1/, a MacBook Pro (wifi ip address //10.0.1.64/), and two YellowJacket Arduino/Wifi boards (YJ56 with wifi ip address //10.0.1.56/, and YJ57 with wifi ip address //10.0.1.57). The Arduino IDE 1.6.5 was run on the MacBook, and compiled sketches were downloaded to the YJ boards through a USB/FTDI cable to the header pins on the YJ boards. The USB/FTDI cable provided the +5V to the YJ board, the means for downloading compiled code to the YJ Arduino microprocessor, and the means for the YJ Arduino to send debugging information back to the Arduino IDE Serial Monitor on the Mac. The compiled sketch code then executed on the YJ Arduino which controlled the on-board wifi backpack hardware which handled the wifi communication with the external MacBook.
WiShield Software: Initially, no example apps distributed with the AsyncLabs WiShield library would compile. One of the first websites found which gave some clues as to the problems with the library was “Ref: http://www.sundh.com/blog/2012/02/make-wishield-work-in-latest-arduino-ide/”. After much investigation, the best library as of 2016-02-25, was the “Ref: https://github.com/kaptk2/ZG2100BasedWiFiShield” library, which was saved locally as WiShield_V3 library in the Arduino user directory. This library, with code fixes included, worked with Arduino IDE 1.6.5 software release.
The WiShield library is a large and complex set of code. To allow the code to be as small as possible for use with the Arduino’s limited memory, different code modules must be included and excluded for specific purposes. The WiShield Library code module “apps-conf.h” must be edited to use the proper code to be included during software compile of a specific Arduino sketch. In the following code snippet, “#define APP_WISERVER” is not commented out, so it will be used with the sketch code being compiled. All other code modules must be commented out to remove them from the compiled 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
A second code module, “uip-conf.h”,  also needs attention when using UDP. There is a code switch in lines 105-115 that must be set properly. For all applications not using UDP, this switch must be set to zero.
/**
 * UDP support on or off
 *
 * \hideinitializer
 *
 * If the UIP_CONF_UDP flag is set improperly the following error will result
 * uip.h:1250:3: error: unknown type name 'uip_udp_appstate_t' uip_udp_appstate_t appstate;
 *
 */
 #define UIP_CONF_UDP      0     // default = 0
 // #define UIP_CONF_UDP   1  // jung from 0 to 1 for APP_UDPAPP only
1) SimpleServer example WiServer sketch: SimpleServer is one of the WiShield Library example sketches that uses WiServer to serve a web page from a YellowJacket through Wifi. The example SimpleServer sketch was edited to use the YellowJacket local_ip and local network Wifi login info, and saved as SimpleServer_V3.ino. After downloading the compiled sketch to the YJ Arduino, the Arduino Serial Monitor on the Mac will display the progress of the YJ Arduino app. After the “WiServer init called” response, 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 listen for wifi communication.
>> SimpleServer_V3 sketch 
>> WiServer.server_task initialized on WiServer, ip: 10.0.1.56
>> Listening for connections... 
Typing “http://10.0.1.56:80/“ in the Safari browser on the Mac sent an http request over wifi to YJ56, which then responded over wifi to Safari on the Mac:
Hello from YellowJacket Wifi MRF24 SimpleServer_V3!
and the Serial Monitor displayed:
Server connected
Processing request for /
TX 83 bytes
Server connection closed 
The Arduino SimpleServer_V3 sketch code:
/* 
 * SimpleServer_V3
 * A simple sketch that uses WiServer to serve a web page
 * 
 * This code is run with WiShield_V3 library
 * ref: https://github.com/linksprite/ZG2100BasedWiFiShield
 * The library was edited as described in 
 * ref: https://github.com/kaptk2/ZG2100BasedWiFiShield/commit/dea4af4b50e3700668365df37d9533b7fd31cf0a
 * 
 * 
 * V3 Library files required updates for Arduino V1.6.5
 * apps-conf.h #define APP_WISERVER
 * config.h
 * strings.c V3 mods required for web page serving
 * WiServer.cpp
 * witypes.h
 * 
 * This sketch code required edits to prog_char and prog_uchar
 * The primary web page can be requested by http://10.0.1.56:80/
 * The web page displays properly.
 * WiServer.cpp Verbose provides Serial Monitor monitoring of http requests.
 * 
 */

//#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;
//---------------------------------------------------------------------------


// This is our page serving function that generates web pages
boolean sendMyPage(char* URL) {
  
    // Check if the requested URL matches "/"
    if (strcmp(URL, "/") == 0) {
        // Use WiServer's print and println functions to write out the page content
        WiServer.print("<html>");
        WiServer.print("Hello from YellowJacket Wifi MRF24 SimpleServer_V3!");
        WiServer.print("</html>");
        
        // URL was recognized
        return true;
    }
    // URL not found
    return false;
}


void setup() {
  // Enable Serial output
  Serial.begin(115200);
  // /*
  Serial.println("");
  Serial.println(">> SimpleServer_V3 sketch ");  
  Serial.print(">> WiServer.server_task initialized on ");  
  Serial.print(myhost);
  Serial.print(", ip: ");
  Serial.println(myhost_ip);
  // */
  
  // Initialize WiServer and have it use the sendMyPage function to serve pages
  WiServer.init(sendMyPage);
  
  // Verbose=true >> ask WiServer to generate log messages to Serial Monitor
  WiServer.enableVerboseMode(true);
  
  Serial.println("");
  Serial.println(F(">> Listening for connections..."));

}

void loop(){

  // Run WiServer
  WiServer.server_task();
 
  delay(10);
}
 (Feb 25, 2016)

No comments:

Post a Comment