4 DOMAIN NAME SERVICE (DNS)
6.3 USING THE WEB SERVER
6.3.6 Creating Web Pages Based on KAREL Programs
This section contains information about how you can integrate KAREL programs into your robot home page, and how you can use your web browser to pass parameters to a KAREL program. Example 6.3.6(a) through Example 6.3.6(c) in this section contain an example KAREL program (demo.kl) to provide an example of one way you can use KAREL programs to access system variables on your robot from a remote web browser.
Example 6.3.6 (a) Demo.kl -- Example File Access Program
%nolockgroup CONST
HDR = 'HTTP/1.0 200 OK, request succeeded' NAK = 'HTTP/1.0 404 File Not Found'
SERVER_ERR = 'HTTP/1.0 500 Server Error'
NOSUPPORT = 'HTTP/1.0 503 Service Unavailable' ERRHDR = '<HTML><BODY><P><H2>'
ERRTRLR = '</H2></BODY></HTML>' HDRHTML = 'Content-type: text/html' HDRTEXT = 'Content-type: text/plain' HDRJPEG = 'Content-type: image/jpeg' HDRGIF = 'Content-type: image/gif'
HDRBIN = 'Content-type: application/octet-stream' HDRWAV = 'Content-type: audio/basic'
DEFAULTFILE = 'INDEX.HTM' DEFDEV = 'FR:'
SCRHDEV = 'RD:' SYSDEV = 'FRS:'
TEXTHDR = '<HTML> <BODY> <PRE>' TEXTTRLR = '</PRE> </BODY> </HTML>'
GETDHDR = '<HTML> <BODY> <H2> Get_data:</H2><BR><BR><OL><LI>' GETDTRLR = '</LI></OL><BR> </BODY> </HTML>'
PAGEHDR = '<HTML> <BODY> <H2> Post data:</H2><BR><BR>' PAGETRLR = '<BR> </BODY> </HTML>'
-- graphics and forms used in MD_FILES.HTM BACKGROUND = 'FRS/EARTHBG.GIF'
Example 6.3.6 (b)
OPEN FILE file1 ('RW', 'RD:RESPONSE.HTM')
write file1('<HTML><HEAD><TITLE>ASG_DEMO.HTM</TITLE></HEAD>',cr) --
-- *** Example of adding some graphics content to page ***
-- *** Be sensitive to file sizes! ***
--
-- write file1('<BODY BACKGROUND="../') -- write file1(BACKGROUND)
-- write file1('">',cr)
-- write file1('<CENTER> <H1><A NAME="TOP"><IMG SRC="../') -- write file1(PIC1,cr)
-- write file1('" WIDTH="593" HEIGHT="153"></A></H1> </CENTER>',cr) write file1('<H1><CENTER><BOLD>IMPORTANT CUSTOMER ',cr,cr)
write file1('</CENTER></H1>',cr)
write file1('<H1><CENTER><BOLD>Production Counts for : ') write file1 ('PRESS1',cr)
write file1('</CENTER></H1>',cr)
Example 6.3.6 (c)
write file1('<H1><CENTER><BOLD>ProductionCount: ') write file1(count1,cr)
write file1('</BOLD></CENTER></H1>',cr)
write file1('<H1><CENTER><BOLD>Error Count: ') write file1(count2,cr)
write file1('</BOLD></CENTER></H1>',cr) -- write file1(TEXTTRLR,cr)
CLOSE FILE file1 END demo
To use this feature from a browser, set KAREL as the device and the program to run as the filename (demo in this example).
Example 6.3.6 (d) Integrating KAREL Programs into the Web Browser http://robot1/KAREL/demo
Using a Web Page to Pass Parameters to a KAREL Program
You can create applications that can pass parameters from a form in the browser to the KAREL program.
The KAREL program is invoked based on the "submit" action in the form and parameters included in the form are passed with the URL. The FANUC Robotics web server complies with standards found in the HTTP 1.0 Specification. Note that only the HTTP "GET" method is supported at this time. The KAREL program must declare string variables whose names match any parameter names being passed from the form in order to access it. An additional string variable called "URL" should be declared to see the complete URL request sent from the browser (for debugging).
Example 6.3.6 (e) Variable Declaration for Using a Web Page to Pass Parameters to a KAREL Program var
URL : string[128]
Textbox1 : string[12]
These declarations in the KAREL program invoked by the browser will give the KAREL program access to the complete URL (if less than 128 bytes) for debugging and fill in the variable Textbox1 with the data from "Textbox1" from the form..
Note that checkboxes are only sent from a form on the browser if they are checked. Forms can be configured to always send the checkbox value as "false" in a hidden field first, or the KAREL program can always reset the KAREL variable to the default state at the end of the KAREL program. Both methods are shown in the example in Figure 6.3.6(a) through Figure 6.3.6(b) .
Fig. 6.3.6(a) Example KAREL based web page using parameters
Example 6.3.6 (f) Demo Form Interface to a KAREL Program
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Microsoft FrontPage 2.0">
<title>Web Demo</title>
</head>
<body bgcolor="#FFFF00">
<p> j </p>
<p><font color="#FF0000" size="5"><strong><u>Demo Form Interface to Karel Program</u></strong></font></p>
<p>  </p>
<form action="http://palrob/karel/web_demo" method="GET" name="uif_demo">
<input type="hidden" name="C1" value="OFF">
<input type="hidden" name="str1" value="string1">
<input type="hidden" name="str2" value="string2">
<p> j </p>
<p>There are three hidden fields included on this form : C1=off, STR1=string1,
STR2=string2</p>
<p>You need to set checkbox to OFF first (always) since value is only sent if ON!!</p>
<p>This can be done by sending hidden variable with same name as checkbox, or resetting checkbox variable to</p>
<p>a default state at end of karel program (both approaches shown in this example).</p>
<p><input type="text" size="20" name="str3" value="string3">Example text box (STR3)</p>
<p><input type="checkbox" name="C1" value="ON"> Example Checkbox (C1)</p>
<p><input type="radio" checked name="R1" value="V1"> Example Radio Button (R1, value = V1) - Show Count1</p>
<p><input type="radio" name="R1" value="V2"> Example Radio Button (R1, value=V2) - Show Count2</p>
<p><select name="D1" size="1">
<option>Jim</option>
<option>Joe</option>
<option>Harry</option>
</select> Example dropdown box</p>
<p><input type="submit" name="B1" value="Submit"></p>
</form>
</body>
</html>
Fig. 6.3.6(b) Results of the demo form interface to a KAREL program
Example 6.3.6 (g) Example KAREL program
-- Example karel program to respond to a form called web_demo.htm created in -- frontpage. Note that form data is populated in corresponding karel
-- variables, IF variables are declared. A string variable called URL should -- be declared to see exactly what is provided from browser which is useful -- for debugging.
-- NOTE : variables which are included in URL are populated each time -- the program is called. Some form variables (eg. checkbox) are only -- sent if they are checked. This behavior can be handled by always -- passing a "hidden" variable of same name with default value from -- form, or by resetting variables with this nature to a default state -- after program runs (see c1 variable assignment at end of this program).
-- Program variables are uninitialized the first time a program runs -- (aside from ones which are set by URL, since any variables included in -- URL are set before program is called).
--
PROGRAM web_demo
%nolockgroup CONST
TEXTHDR = '<HTML> <BODY>' TEXTTRLR = '</BODY> </HTML>'
BACKGROUND = 'FRS/EARTHBG.GIF' -- used in MD_FILES.HTM
PIC1 = 'FR/PICTURE.GIF' -- some picture for top of response file VAR
-- Good practice to check for uninitialized variables before using --them
if uninit(count1) then count1 = 0; endif if uninit(str1) then str1 = ''; endif
count1 = count1 + 1 -- these might be production counts from another program count2 = count1 * 2 -- they are just included as examples
OPEN FILE file1 ('RW', 'RD:RESPONSE.HTM')
write file1('<HTML><HEAD><TITLE>WEB_DEMO.HTM</TITLE></HEAD>',cr) write file1('<BODY BACKGROUND="../')
write file1(BACKGROUND) write file1('">',cr)
-- Could add a graphic to top of response file
-- write file1('<CENTER> <H1><A NAME="TOP"><IMG SRC="../') -- write file1(PIC1,cr)
-- write file1('" WIDTH="400" HEIGHT="100"></A></H1> </CENTER>',cr) -- write file1('"></A></H1> </CENTER>',cr)
write file1('<H1><CENTER><BOLD>Results of form request :',cr,cr) write file1('</CENTER></H1>',cr)
-- checkbox only sent if checked so send default state always write file1('<H2><CENTER><BOLD>Received c1 (hidden) : ') write file1(c1,cr)
write file1('</BOLD></CENTER></H2>',cr)
Example 6.3.6 (i) write file1('<H2><CENTER><BOLD>Received str3 : ') write file1(str3,cr)
write file1('</BOLD></CENTER></H2>',cr) if (c1='ON') then
write file1('<H2><CENTER><BOLD>Received Checkbox : ') write file1(c1,cr)
write file1('</BOLD></CENTER></H2>',cr) endif
write file1('<H2><CENTER><BOLD>Received Radio button : ') write file1(r1,cr)
write file1('</BOLD></CENTER></H2>',cr)
write file1('<H2><CENTER><BOLD>Received dropdown box : ') write file1(d1,cr)
write file1('</BOLD></CENTER></H2>',cr)
write file1('<H2><CENTER><BOLD>Received URL : ') write file1(URL,cr)
write file1('</BOLD></CENTER></H2>',cr) if (r1='V1') then
write file1('<H2><CENTER><BOLD>Count1 value is : ') write file1(count1,cr)
write file1('</BOLD></CENTER></H2>',cr) else
write file1('<H2><CENTER><BOLD>Count2 value is : ') write file1(count2,cr)
write file1('</BOLD></CENTER></H2>',cr) endif
-- If default value of checkbox is not sent as hidden variable, another -- alternative is to reset checkbox variable to default state after -- program runs. As with all karel programs, global variables retain -- their value between each execution
c1 = 'OFF'
write file1(TEXTTRLR,cr) CLOSE FILE file1
END web_demo