WebServer Sketch help

Discussion about any of the included sketches (i.e. WebServer), or user-generated applications.

WebServer Sketch help

Postby dukeofdoom » Thu Sep 02, 2010 1:58 pm

I got the sketch loaded fine, got to the page fine as well. But I am having trouble figuring out how to pass variables to and from the web page. The web page line that comes with it doesn’t give me enough clues to figure it out.
const prog_char webpage[] PROGMEM = {"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<center><h1>Hello World!! I am WiShield</h1><form method=\"get\" action=\"0\">Toggle LED:<input type=\"submit\" name=\"0\" value=\"LED1\"></input></form></center>"};
It would be nice as well if someone could tell me how to get example for working too. I cannot run WiServer because I have a 168 chip or I would have used the sample that’s available for that.
Any help will be appreciated.




-Paul
dukeofdoom
 
Posts: 8
Joined: Thu Sep 02, 2010 1:15 pm
Location: RI

Re: WebServer Sketch help

Postby GregEigsti » Fri Sep 03, 2010 8:01 am

You are going to have to be comfy with C and HTML to achieve your goal (as I understand).
C - to be able to manipulate your incoming and outgoing data (as well as general Arduino programming).
HTML - to provide the web page based UI that you desire.

You may find the uIP docs helpful to better understand what is going on under the covers (see my signature for a link to the uIP docs).

The webserver sample sketch is made up of two files - the .pde and a .c file - most of the heavy lifting is done in the .c file; you should read through that to understand what is happening (again this is where the uIP docs will come in handy).

In this example the "basic" web page template lives in PROGMEM (think read only memory); when it is requested by a client it is loaded into a buffer (read/write) and sent out over the socket. This is a pretty good place to make minor mods to the data that is sent out (there are other ways to achieve this as well). As I see it you will want to set up your basic "web page template" in the PROGMEM string and when the code is called to send it out make whatever minor mods you need to before sending it out.

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: WebServer Sketch help

Postby dukeofdoom » Fri Sep 03, 2010 8:45 am

Thanks for replying!
That’s just it I can seem figure out how to get a simple variable in to the PROGMEM line. I keep getting various errors. I know it’s my lack of knowledge of arrays and var types keeping me from doing this. The HTML part is easy, once I get the var in. And really just trying to get single digit numbers variables into the HTML code. Again any help is a appreciated.
dukeofdoom
 
Posts: 8
Joined: Thu Sep 02, 2010 1:15 pm
Location: RI

Re: WebServer Sketch help

Postby GregEigsti » Fri Sep 03, 2010 9:01 am

The PROGMEM array is read only - which means that you cannot change it from code. There are a couple of things you can try; in order of my preference ;)

1) In webserver.c -> fill_buf() you can modify the contents of uip_appdata after you have copied webpage into it. This will require some knowledge/use of C string functions (e.g. sprintf(), etc.).
Code: Select all
unsigned short fill_buf(void* blk)
{
   unsigned short webpage_len;

   webpage_len = (strlen_P(webpage)>uip_mss())?uip_mss():strlen_P(webpage);

   memcpy_P(uip_appdata, webpage, webpage_len);

   // modify uip_appdata here
   // make sure to also update webpage_len here since the length of the web page will likely change

   return webpage_len;
}


2) Move the webpage array out of PROGMEM (and make it a char array) so that it is read/write and you will be able to modify it more freely in more places. This will affect your overall usable RAM since it has moved out of flash (or wherever PROGMEM puts it - I don't recall now).

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: WebServer Sketch help

Postby dukeofdoom » Fri Sep 03, 2010 12:51 pm

Thanks for the insight I think I have enough to make this work I'll post somthing when it does. Thanks again!
dukeofdoom
 
Posts: 8
Joined: Thu Sep 02, 2010 1:15 pm
Location: RI

Re: WebServer Sketch help

Postby GregEigsti » Fri Sep 03, 2010 1:11 pm

Excellent! Was going to try to give you a code sample but that would require some work :lol:
I am on vacay today making this a long, long weekend and have had some errands to run. Got to do some more tasks but will try to provide a little more help - no promises :D

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: WebServer Sketch help

Postby dukeofdoom » Sun Sep 05, 2010 11:58 am

Well I got it working. It "ain't" pretty or elegant but it works.
My C skills are rusty I am more of a KSH/perl kinda guy.

My goal was simple I have a relay off a garage door that controls a switch I wanted that switch to control a digital pin on the Arduino. Then simply display a 1 or a 0 for each door. I have a Linux node that picks up the door states and does the heavy lifting. So the page on the Arduino needed to be basic at best. It works but I will be cleaning it up as my skills increase.
I do appeal to async labs though to make a starting webpage that does this sort of function and also makes the form that they had supplied work, doing this would help newbies like me get a better jump start.

I used the standard webserver sketch and made the following edits.

IN the PDE file after IP setup:
//---------------------------------------------------------------------------

void setup()
{
// Serial.begin(9600);
pinMode(6, INPUT); // ADDED this to initialize pin 6 as input, as a note many pins are unusable for I/O due to the WISHIELD using the pins.
WiFi.init();
}

// This is the webpage that is served up by the webserver

//{"HTTP/1.1 200 OK\r\n Content-Type: text/html\r\n\r\n<center><h1>Hello World!! I am WiShield</h1> <form method=\"get\" action=\"0\">Toggle LED:<input type=\"submit\" name=\"0\" value=\"LED1\"></input></form></center>"}; //original supplied line.

const prog_char webpage[] PROGMEM = {"HTTP/1.1 200 OK\r\n Content-Type: text/html\r\n\r\n<br> VAR1 AAAA <BR> VAR2 BBBB"}; //this is what the webpage will look like, the variables don’t get added here this is just used to determine a character length of the webpage to be display. That length is used later.
void loop()
{
out1 = digitalRead(6); //set variable out1 to hold reading of pin 1.
delay(100); //added a delay
WiFi.run();
}



.C file:
I only made changes to the fill_buf function.

unsigned short fill_buf(void* blk)
{
unsigned short webpage_len;

webpage_len = (strlen_P(webpage)>uip_mss())?uip_mss():strlen_P(webpage);

memcpy_P(uip_appdata, webpage, webpage_len);
int jag = sprintf( uip_appdata, "HTTP/1.1 200 OK\r\n Content-Type: text/html\r\n\r\nn<br> VAR1 %d <BR> VAR2 BBBB", out1 ); //uip_appdata is the var that holds the webpage. Due to the weird way PROGMEM works and my C incompetence I bypassed it all together. This is where I add the variables (so far just one) to the page. Yes its double work but hey it works. It important that this HTTP line doesn’t have more characters then the HTTP line in the webpage variable.

return webpage_len;
}

I tested and the webpage updates the VAR1 field with a 1 or 0 as the garage door is either up or down.
dukeofdoom
 
Posts: 8
Joined: Thu Sep 02, 2010 1:15 pm
Location: RI

Re: WebServer Sketch help

Postby GregEigsti » Sun Sep 05, 2010 4:13 pm

Excellent glad its working for you!

So now here comes the wrench... :lol:

Why use the overhead of having a web page at all? If you have a "linux node" that does all of the heavy lifting why not just use a much simpler socket connection that passes back either a 0 or a 1? So instead of generating a web page on the WiShield/Arduino, requesting it from the linux box, parsing it on the linux box, etc. you might just have the WiShield/Arduino listen on on a socket and return only the pertinent data (a 0 or a 1). This cuts out the web page creation and parsing - which greatly simplifies your work. Take a look at my completed wifi projects as this is the approach that I take - if I want a purty web page I'll let the more capable "PC" do that work.

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: WebServer Sketch help

Postby dukeofdoom » Sun Sep 05, 2010 7:55 pm

Had to rain on my parade eh? :D

I had saw the socket examples but figured it be a lot of work. I knew how to grab from web pages already. But since now I got somthing working I can futher improve it with your suggestion by dropping the http alltogether.
I was unable to get to your slacklab site to see your examples, not sure if the site is down or somthing wrong on my end.
dukeofdoom
 
Posts: 8
Joined: Thu Sep 02, 2010 1:15 pm
Location: RI

Re: WebServer Sketch help

Postby GregEigsti » Sun Sep 05, 2010 10:46 pm

Thanks for the heads up! WordPress keeps dieing on me :( Fixed now. Actually the socket app would be less work but hey you go go with what you know! Glad you got it working!

Maybe I'll give the WordPress manual update a go, auto update keeps failing :(

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


Return to Sketches and Applications

Who is online

Users browsing this forum: No registered users and 1 guest