; httpform.vnm ; Illustrates HTML forms in Venom ; ; In an application where the VM2 is running a web server, ; HTML forms and buttons with GET parameters in their links ; are two means for user input to the VM2 ; ; This simple application illutates these by enabling the user to control ; the colour of a panel by adjusting the red, green and blue components ; ; Techniques used: ; * Buttons with direct links back to the same page to change one parameter at at time ; * a form in which three numerical values are edited and then sent with a submit button ; * use of numerical values to alter the colour and shape of HTML elements, in this case ; creating a simple bar graph ; ; *** This code only works on Venom 2 release 2013 01 10 or later *** ; ; Some experience of HTML is inevitably required to create applications like this. ; ; to test this program, run on your local LAN, and use a web browser to view ; http://vm2web/form.html ; or (if that doesn't work) ; http://aaa.bbb.dccc.ddd/form.html ; where aaa.bbb.ccc.ddd is the IP address displayed when the program starts ; The VM2 obtains its IP address from a DHCP server on your LAN. ; This IP address is not guaranteed to stay the same from run to run. ; For real applications you may need to configure the DHCP server on your ; LAN to allocate a fixed IP address to the VM2. To init Make fs FileSystem("fla") ; some graphics files will be in flash file system ; attempt to register with DHCP Make eth Ethernet(Nil, "vm2web") ; set initial colour red := 100 green := 200 blue := 150 End To main myipaddress := EstablishConnection Print "server running on IP address ", myipaddress:"IP", CR Start server_task(1) Start server_task(2) ; At this point we could run a control system concurrently with the web server tasks ; Here it's just an idle loop Forever Wait(10) End ; Try to make an Ethernet connection and then return our IP address. To EstablishConnection While eth.Connect IsFalse [ Print "Ethernet connection failed - trying again...",CR Wait 1000 ] Return eth.Address('i') End To server_task(n) AutoDestruct Local http := New HTTPServer(fs) Local req := New String(100) ;Local s := New String(20) Local r, g, b ; temporary colour values ;Local textcolor Print "server task ", n:1, CR Forever [ http.Get(req) Print "req: from ", http.Address:"IP", " : ", req, CR If req.Compare("form.html") = 0 OrElse req.Compare("/") = 0 [ ; test for existence of each value by requesting as a string first r := http.Value("red", red) g := http.Value("green", green) b := http.Value("blue", blue) ; sanitize input: ignore values out of range If r >= 0 AndAlso r <= 255 red := r If g >= 0 AndAlso g <= 255 green := g If b >= 0 AndAlso b <= 255 blue := b ; make sure browser requests data each time instead of using cache http.Period := 0 http.PrintF("