Monday, March 7, 2016

YellowJacket Arduino with WiShield Wifi (Part 9)

Setup Ad Hoc network WiServer on YJ56 and Ad Hoc WiClient on YJ57 — 
Ref: http://www.howtogeek.com/180649/htg-explains-whats-the-difference-between-ad-hoc-and-infrastructure-mode/
An Ad-hoc network allows devices to communicate directly without a central access point, e.g. a router. All Ad-hoc traffic goes to all devices on the Ad-hoc network which is a slower network. Only devices set up on the Ad-hoc network can communicate. Alternatively, an Infrastructure network allows devices to be connected via a central access point.
Connecting Two YellowJackets for Data Transfer
Hardware:
  • YJ Arduino board (YJ56) at ip=192.168.1.56
  • YJ Arduino board (YJ57) at ip=192.168.1.57
  • Local ADHOC network gateway ip=192.168.1.1
  • ssid[] = {“arduino”}
  • security_type = 0
Software: WiClient YJ57 requests web page from WiServer YJ56.
  1. Load sketch SimpleAdhocServer_V3 into YJ56. Sketch uses “WiServer.init(sendMyPage);” to transmit web page content by Wifi.
  2. Load sketch SimpleWiClientReadAdhocWiServer_V3 into YJ57. Sketch uses GetRequest to Host: “192.168.1.56”, URL=“/“
Result: WiServer web page is requested every 10 seconds by WiClient and is displayed on Serial Monitor:
=== Initializing WiClient to display Adhoc WiServer web page on Serial Monitor ===
 
=== Looking for WiServer YJ56 running on ip: 192.168.1.56
=== WiClient YJ57 is running on ip: 192.168.1.57
>>> Request response on 10 second intervals... 
 
Ended connection with 192.168.1.56
 .
Connected to 192.168.1.56
TX 64 bytes
RX 0 bytes from 192.168.1.56
RX 75 bytes from 192.168.1.56
HTTP/1.0 200 OK

<html>Hello from Wifi MRF24 SimpleAdhocServer_V3!</html> .
Ended connection with 192.168.1.56
 .
Connected to 192.168.1.56
TX 64 bytes
RX 0 bytes from 192.168.1.56
RX 75 bytes from 192.168.1.56
HTTP/1.0 200 OK

<html>Hello from Wifi MRF24 SimpleAdhocServer_V3!</html> .
Ended connection with 192.168.1.56
 .
SimpleAdhocServer_V3 sketch:
/* 
 *  SimpleAdhocServer_V3
 *  
 * A simple sketch that uses WiServer to serve a web page on Ad Hoc network
 * between two Arduino Wifi's
 * 
 * Install this WiServer on YJ56
 * 
 * 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 <WiServerIO.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

unsigned char local_ip[] = {192,168,1,56}; // IP address of WiShield
unsigned char gateway_ip[] = {192,168,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 = {"arduino"}; // max 32 bytes

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

const char* myhost = "YJ56";
char* myhost_ip = "192.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 wireless_mode = WIRELESS_MODE_ADHOC;

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 Wifi MRF24 SimpleAdhocServer_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(">> SimpleAdhocServer_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);
}


SimpleWiClientReadAdhocWiServer_V3 sketch:
/*  SimpleWiClientReadAdhocWiServer_V3
 *
 * A simple sketch that uses MRF24 WiClient to read web page from MRF24 WiServer
 * using ADHOC network
 * 
 * Install WiServer on YJ56
 * Install this WiClient on YJ57
 *
 * Compiled with WiShield_V3 library
 *
 * Uses apps-conf.h "#define APP_WISERVER"
 *
 *  Connecting Two YellowJackets for Data Transfer
 *
 *  Hardware:
 *    YJ Arduino board (YJ56) at ip=192.168.1.56
 *    YJ Arduino board (YJ57) at ip=192.168.1.57
 *    Local ADHOC network gateway ip 192.168.1.1
 *    ssid[] = {"arduino"}
 *    security_type = 0
 *
 *  Example 1: WiClient YJ57 requests web page from WiServer YJ56.
 *    a) Load sketch SimpleAdhocServer_V3 into YJ56. Sketch uses
 *      “WiServer.init(sendMyPage);” to transmit web page content by Wifi.
 *    b) Load sketch SimpleWiClientReadWiAdhocServer_V3 into YJ57. Sketch uses
 *      GetRequest to Host: “192.168.1.56”, URL=“/“
 *    c) Result: WiServer web page is requested every 10 seconds by WiClient
 *
 */

#include <WiServerIO.h>

#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168, 1, 57}; // IP address of WiClient
unsigned char gateway_ip[] = {192,168, 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 = {"arduino"};   // max 32 bytes

const char* myhost = "YJ56";
char* myhost_ip = "192.168.1.56";
const char* me = "YJ57";
char* me_ip = "192.168.1.57";

unsigned char security_type = 0;  // 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_ADHOC;
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++));
  }
  Serial.println (" ."); // terminate messages with CR
}

uint8 server_ip[] = {192, 168, 1, 56}; // IP Address for WiServer YJ56
// A request that gets web page from server
GETrequest getWebPage(server_ip, 80, myhost_ip, "/");

void setup() {

  // Enable Serial output
  Serial.begin(115200);

  Serial.println (" ");
  Serial.println (F("=== Initializing WiClient to display Adhoc WiServer web page on Serial Monitor ==="));

  // 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
  getWebPage.setReturnFunc(printData);

  Serial.println (" ");
  Serial.print (F("=== Looking for WiServer "));
  Serial.print (myhost);
  Serial.print (F(" running on ip: "));
  Serial.println (myhost_ip);
  Serial.print (F("=== WiClient "));
  Serial.print (me);
  Serial.print (F(" is running on ip: "));
  Serial.println (me_ip);
  Serial.println (F(">>> Request response on 10 second intervals... "));
  Serial.println (" ");

}

// Time (in millis) when the data should be retrieved
unsigned long updateTime = 0;
unsigned long waitTime = 1000UL * 10UL; // * UL unsigned long number format used to avoid overflow

void loop() {

  // Check if it's time to get an update
  if (millis() >= updateTime) {
    getWebPage.submit();
    // Get another update 10 seconds from now
    updateTime += waitTime;
  }

  // Run WiServer
  WiServer.server_task();

  delay(10);
}

(Mar 7, 2016)


No comments:

Post a Comment