WiServer Serving Images from in chip FLASH

Discussions related to the WiServer add-on code for WiShield. WiServer is a friendly, easy-to-use front end for webserver and webclient usages.

Moderator: shard7

WiServer Serving Images from in chip FLASH

Postby kingofl337 » Sat Sep 25, 2010 11:37 am

I have a WiShield 1.0, so I don't have the upgraded serial flash module. But, I only need a few very small 1k images for my page. I would like to serve these up but haven't had any luck. I downloaded the old WiShield 1.1 code and tried to display favicon.ico using the encoded favicon.h and calling it in html with <img src="favicon.ico"> and WiServer.print_P. Here are some snippets of code.

in the favicon.h file
Code: Select all
const prog_char favicon[] PROGMEM = { some hex};


I'm seeing the serial data that the icon is being called for on the terminal, this a copy and paste from the example.
Code: Select all
 
// Check if request is for the favicon
  if (strcmp(URL, "/favicon.ico") == 0) {
    // Write the bytes of the favicon that are stored in flash as defined
    // by favicon.h.  Always use the special _P functions to write data that's
    // stored in flash
    WiServer.print_P(favicon);
    Serial.println("The Image has been requested");    //This is being called when the page loads
    return true;
  }



Code: Select all
WiServer.print("<img src=\"favicon.ico\">");


The full program, note the code is borrowed from a post I read here and most of which is not my own, though I do understand how it works.
Code: Select all
/*
* A simple sketch that uses WiServer to serve a web page !! that kim messed with.
*/


#include <WiServer.h>
#include <string.h>
#include "favicon.h"

#define WIRELESS_MODE_INFRA   1
#define WIRELESS_MODE_ADHOC   2

#define ledPin1 5
#define ledPin2 6
#define ledPin3 7

#define tempraturePin 0
#define batteryVoltagePin 1

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {
  192,168,1,1};   // 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 prog_char ssid[] PROGMEM = {
  "BAJA"};      // max 32 bytes

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

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

// WEP 128-bit keys
// sample HEX keys
prog_uchar 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 ----------------------------------------

boolean states[3]; //holds led states
char stateCounter; //used as a temporary variable
char tmpStrCat[64]; //used in processing the web page
char stateBuff[4]; //used in text processing around boolToString()
char numAsCharBuff[2];
char ledChange;

void boolToString (boolean test, char returnBuffer[4])
{
  returnBuffer[0] = '\0';
  if (test)
  {
    strcat(returnBuffer, "On");
  }
  else
  {
    strcat(returnBuffer, "Off");
  }
}

void printStates()
{
        for (stateCounter = 0 ; stateCounter < 3; stateCounter++)
        {
            boolToString(states[stateCounter], stateBuff);
           
            Serial.print("State of ");
            Serial.print(stateCounter);
            Serial.print(": ");
            Serial.println(stateBuff);
        }
}

void writeStates()
{
        //set led states
        digitalWrite(ledPin1, states[0]);
        digitalWrite(ledPin2, states[1]);
        digitalWrite(ledPin3, states[2]);
}

// This is our page serving function that generates web pages
boolean sendPage(char* URL) {
 
   // Check if request is for the favicon
  if (strcmp(URL, "/favicon.ico") == 0) {
    // Write the bytes of the favicon that are stored in flash as defined
    // by favicon.h.  Always use the special _P functions to write data that's
    // stored in flash
    WiServer.print_P(favicon);
    Serial.println("The Image has been requested");   
    return true;
  }

  Serial.println("Page printing begun");

    printStates();
    writeStates();
   
  //check whether we need to change the led state
  if (URL[1] == '?' && URL[2] == 'L' && URL[3] == 'E' && URL[4] == 'D') //url has a leading /
  {
    ledChange = (int)(URL[5] - 48); //get the led to change.
   
    for (stateCounter = 0 ; stateCounter < 3; stateCounter++)
    {
      if (ledChange == stateCounter)
      {
        states[stateCounter] = !states[stateCounter];
            Serial.print("Have changed ");
            Serial.println(ledChange);
      }
    }
   
    //after having change state, return the user to the index page.
    WiServer.print("<HTML><HEAD><meta http-equiv='REFRESH' content='0;url=/'></HEAD></HTML>");
    return true;
  }

  if (strcmp(URL, "/") == false) //why is this not true?
   {
      WiServer.print("<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Baja Control Panel</title></head>");
      WiServer.print("<meta name=\"viewport\" content=\"width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;\">");
      WiServer.print("<body bgcolor=\"black\" text=\"white\"><center><font color=\"white\"><b>Status</b></font><center>\n<center>");
      for (stateCounter = 0; stateCounter < 3; stateCounter++) //for each led
      {
        numAsCharBuff[0] = (char)(stateCounter + 49); //as this is displayed use 1 - 3 rather than 0 - 2
        numAsCharBuff[1] = '\0'; //strcat expects a string (array of chars) rather than a single character.
                                 //This string is a character plus string terminator.
       
        tmpStrCat[0] = '\0'; //initialise string
        strcat(tmpStrCat, "<a href=?LED"); //start the string
        tmpStrCat[12] = (char)(stateCounter + 48); //add the led number
        tmpStrCat[13] = '\0'; //terminate the string properly for later.
   
        strcat(tmpStrCat, ">Led ");
        strcat(tmpStrCat, numAsCharBuff);
        strcat(tmpStrCat, ": ");
       
        boolToString(states[stateCounter], stateBuff);
        strcat(tmpStrCat, stateBuff);
        strcat(tmpStrCat, "</a><br> "); //we now have something in the range of <a href=?LED0>Led 0: Off</a>
   
        WiServer.print(tmpStrCat);
      }
        WiServer.print("<br>Cabin Temprature: ");
        WiServer.print(analogRead(tempraturePin)); //Read Cabin Temp
        WiServer.print("    Battery Voltage: ");
        WiServer.print(analogRead(batteryVoltagePin)); //Read Battery Voltage
        WiServer.print("<img src=\"favicon.ico\">");
        WiServer.print("</html> ");
        return true;
   }
}

void setup() {
  // Initialize WiServer and have it use the sendMyPage function to serve pages
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);

  Serial.begin(9600);
  WiServer.init(sendPage);
  states[0] = false;
  states[1] = false;
  states[2] = false;
}

void loop(){
  // Run WiServer
  WiServer.server_task();

  delay(10);
}


Any help would awesome.
kingofl337
 
Posts: 4
Joined: Tue Sep 21, 2010 7:13 pm

Re: WiServer Serving Images from in chip FLASH

Postby kingofl337 » Sat Sep 25, 2010 1:51 pm

I also just tried

Code: Select all
WiServer.write_P((char*)favicon,favicon_size);
kingofl337
 
Posts: 4
Joined: Tue Sep 21, 2010 7:13 pm

Re: WiServer Serving Images from in chip FLASH

Postby GregEigsti » Sat Sep 25, 2010 8:54 pm

Are the *print*() collection of APIs meant to print text - or is binary data ok? If they are only intended for text one possible problem with sending an icon (binary data) is that in binary land an embedded 0 (zero) is ok - in text land an embedded 0 (zero) is typically a marker for the end of data. So if these functions are expecting text and getting binary data it is possible that the binary data has a 0 midstream which would make the text handling APIs think that they should just stop sending as they have reached the end of the text data. Dunno about the internals of these functions - but the source should be available and it would be pretty easy to make a determination. My guess is that they are meant for text but I don't really know.

Also, was thinking about sending favicon.ico. I have not looked into the exact protocol/process used by a webserver to do so but I don't think it is via an image embedded in the page - rather my guess is that this is something handled out of band by the web server. Might be fruitful to look into the guts of how favicon.ico is handled by a web server to see how to proceed.

Or are you not trying to do the typical favicon.ico thing and just using it as a "sample" image for your page? What is the "encoded favicon.h"? Is this a textual representation of the binary data in your favicon.ico which could be sent by a "text data only" function?

I don't play with web server/clients on the Arduino/WiShield - I tend to favor TCP/UDP packets and playing directly with the uIP stack.
Greg
Check out the wiki!
uIP Stack Docs
Compatible Access Point List
WiShield user contrib branch - DNS, DHCP, AP Scanning, bug fixes, etc.
SlackLab.org - My geek projects blog.
User avatar
GregEigsti
 
Posts: 1067
Joined: Sun Aug 02, 2009 5:23 pm
Location: Sammamish WA USA (near Seattle)
  • Website

Re: WiServer Serving Images from in chip FLASH

Postby spidermonkey04 » Sun Oct 03, 2010 6:52 pm

You would need a "/" in there to say it's in the root directory.
Code: Select all
<img src=\"/favicon.ico\"/>
But you shouldn't need to explicitly reference the favicon anyway. Browsers will automatically look for a .ico file after loading the page. I've noticed some versions of IE only look for it when you bookmark the page. In these cases you might try adding '<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />' in your HEAD section to force it. (I never do)

Not sure if it matters, but this is what I use.
Code: Select all
WiServer.write_P((prog_char*)favicon, favicon_size);

---Jared
spidermonkey04
 
Posts: 66
Joined: Thu Oct 29, 2009 6:45 pm

Re: WiServer Serving Images from in chip FLASH

Postby GregEigsti » Sun Oct 03, 2010 7:21 pm

Well there you go! I tell you this web stuff is just a flash in the pan :lol:
Check out the wiki!
uIP Stack Docs
Compatible Access Point List
WiShield user contrib branch - DNS, DHCP, AP Scanning, bug fixes, etc.
SlackLab.org - My geek projects blog.
User avatar
GregEigsti
 
Posts: 1067
Joined: Sun Aug 02, 2009 5:23 pm
Location: Sammamish WA USA (near Seattle)
  • Website

Re: WiServer Serving Images from in chip FLASH

Postby shard7 » Tue Oct 19, 2010 6:45 pm

Yup, using write_P is the key for binary data like this. The WiServer print functions are for strings that use a 0 value to indicate the last character. Binary data like images can have 0's all over the place, so you need to use write_P.

Here's the code for QPID that serves up a favicon:

Code: Select all
// Check if request is for the favicon
    if (strcmp(URL, "/favicon.ico") == 0) {
      // Write the bytes of the favicon in flash
      WiServer.write_P((char*)favicon, favicon_size);
      return true;
    }


And this is what the data looks like:

Code: Select all

int favicon_size=1406;
const prog_char favicon[] PROGMEM = {
0x00,0x00,0x01,0x00,0x01,0x00,0x10,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x68,
0x05,0x00,0x00,0x16,0x00,0x00,0x00,0x28,0x00,0x00,0x00,0x10,0x00,0x00,0x00,
0x20,0x00,0x00,0x00,0x01,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x0f,0x0f,0x0f,0x00,0x05,0x31,0xf7,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,...
shard7
 
Posts: 64
Joined: Wed May 06, 2009 11:30 am


Return to WiServer

Who is online

Users browsing this forum: No registered users and 1 guest