Freebasic for web CGI use

I’ve been tinkering around with Freebasic looking at using it for web CGI use. I made as simple GCI application from an example given at the free basic forum and it appears to work well. This may be a simple way to do web based robotic stuff if desired. I’ll see if I can make a setup to do serial port I/O to see how it works. Below is the test cgi application code.

'' Simple CGI example
'' - return request back to client

Dim As String method, request
Dim As Integer length

method = Environ("REQUEST_METHOD")

Select Case Ucase(method)
Case "HEAD"
  Print "Content-type: text/html"
  Print
  End

Case "GET"
  request = Environ("QUERY_STRING")
  
Case "POST"
  length = Val(Environ("CONTENT_LENGTH"))
  Open cons For Input As #1
  request = Space( length )
  Get #1,,request
  Close #1

Case Else
  Print "Content-type: text/html"
  Print
  Print "<html><body>Invalid Request</body></html>"
  End

End Select

'' send content

Print "Content-type: text/html"
Print
print "<html><body>"
Print "request:<br>"
Print "<pre>"
Print request
Print "</pre>"
Print "</body></html>"

End
  

Doing serial I/O with any language is pretty trivial. The problem comes when you want to read input from sensors. To do web base robotics implies that you want to control the robot from the web and/or be able to have it send information back to the web server.

Is this the type of thing you have in mind or do you just want to control the robot via the web? Controlling the robot should not be too difficult as long as you can get information from the web page to the robot control program and then to the robot.

There is already a Python module for Apache that allows one to write web scripts in Python. Python is really quite easy to learn and is available for most platforms, including Windows. The serial and parallel I/O modules are also available for the same platforms as Python. I believe there is also a n Apache module for the Ruby language although I have not used it, and of course PHP.

I am already in the process of writing some robotics software in Python, but just need to be able to read sensors such as the Sharp IR Ranger and others that return analog or PWM type results. I just need a microcontroller module that can talk i2c, which may mean I have to create one myself using a PIC chip since I haven’t seen any stock units that will allow operation as an i2c slave.

8-Dale

I’ve been controlling my mini-ssc II via the web for ~6 years now. The only thing I haven’t been able to do easily is to get input from a serial port and send the data back to a web brouser (q/quick basic doesn’t workwell with XP). I’ve looked at perl, python, PHP, and they all have a lot of excess overhead for doing what I want to do. The posted code compiles into a 20k stand alone exe that is put in the apache cgi-bin folder, and it is done. The compiled exe for doing the serial I/O would probably be about the same size. This might be of use to those that want to get the most use from a ssc-32 with out having to make a GUI with VB and such.

Below is an update on my tinkering with freebasic making a cgi application for use with the ssc-32 servo controller (the code is setup for testing with out the web server). I compiled it and put in the apache cgi-bin folder. I don’t have an ssc-32, so I have to connect and disconnect the serial port tx/rx lines. It seems to work as desired. As the # chracter doesn’t travel well in query_string, I substitutes a - for it, then convert the - into # before sending the string out the serial port. For web use, one would mke a page with two frames, one for a streaming webcam video, and the other for controls and data return from the ssc. Below is what would be sent to the web server.

127.0.0.1/cgi-bin/cgi3.exe?-5P16 … P2250T2000

[code]Dim As String qs, dat
Dim idx As Integer

'qs = “”
qs = “-5P1600-17P750S500-2P2250T2000”
'qs = Environ(“QUERY_STRING”)
if qs = “” goto nodata

again:
idx = Instr(qs, “-”)
Mid(qs, idx, 1) = “#”
if idx > 0 goto again

qs = qs + Chr(13)

Open Com “COM1: 9600,N,8,1,BIN,CD,CS,DS,RS” For Binary As #1
Put #1, qs
Sleep 200
dat = Input$(loc(1), #1)
Close #1

if dat = “” goto nodata
if dat <> “” goto gotdata

nodata:
Print “status: 204”
Print
Print
goto fini

gotdata:
Print “Content-type: text/html”
Print
print “”
Print dat
Print “”
goto fini

fini:
'end
sleep[/code]

I’ve put the compiled test cgi application at the below link for those that might be interested.

geocities.com/zoomkat/files/ssc-32cgi.zip

Well, Its been ~3 years since my origional post. I now have an ssc-32 for testing. I’ve compiled the below code into cgiC8B9.exe and put it in my apache cgi-bin folder. Using the bottom ssc-32 web page I can control my servos and get voltage on the ssc-32 analog inputs via a web page. The code is ~crude, but seems to work. In the Arduino people are banging their heads against a wall trying to do the same using PHP. I think this is much simpler. More to be done.

cgiC8B9.bas

[code]Dim As String qs, dat, qs1
Dim As Integer idx
Dim As Double aaa

qs = “ver” 'for testing, comment out for compiling
'qs = “vc” 'for testing, comment out for compiling
'qs = Environ(“QUERY_STRING”) 'uncomment for compiling

if qs = “” goto nodata

again:
idx = Instr(qs, “-”)
Mid(qs, idx, 1) = “#”
if idx > 0 goto again

qs1 = qs + chr(13)

'set your com port and baude rate here
Open Com “COM8: 9600,N,8,1,BIN,CD,CS,DS,RS” For Binary As #1
print #1, qs1
Sleep 200
dat = Input$(loc(1), #1)
Close #1

if dat = “” goto nodata

if len(dat) > 1 goto gotdata
if len(qs) = 2 goto voltage
if len(qs) = 3 goto position

voltage:
aaa = asc(dat)
'print aaa
aaa = (aaa * 5) / 255
dat = str(aaa)
dat = left(dat, 5)
dat = "Voltage: " + dat
goto gotdata

position:
aaa = asc(dat)
print aaa
aaa = aaa * 10
dat = str(aaa)
goto gotdata

gotdata:
Print “Content-type: text/html”
Print
print “”
print dat
print “”
goto fini

nodata:
Print “status: 204”
Print
Print
goto fini

fini:
'end 'uncomment for compiling
sleep 'for testing, comment out for compiling
[/code]
ssc-32.htm

[code]

Zoomkat's cgi test Zoomkat's SSC-32 cgi test 2/28/10
Position Servo:
Servo 0:
1200 * * * * * * * * * 1600 * * * * * * * * * 2000

Control continous rotation servo speed/direction:
Servo 4: R * * * * * * * * * STOP * * * * * * * * * F

Get data from SSC-32:
|VER| VA| VB| VC| VD| Boom|
[/code]

I can tell you this is rather professionaly.
May look here, if you are interested:
phpforms.net/tutorial/html-basics/form-creator.html

I found a sample program of a simple multithreaded httpserver (fbchttpd) written in freebasic in the following link:

freebasic-world.narod.ru/simplewebserver.html

It can be compiled and packaged as an executable (exe/dmg/deb/rpm/tbz2) and installed in the respective os, after which it can be run and used either as standalone server or as a proxy server behind apache2 using combination of modproxy and modrewrite modules. Then there is support for httpheader module, which also has to be used in any case such as the examples above, but I couldn’t find any examples using the gcc’s curl library module (as per the freebasic documents, anything available in gcc should be available in freebasic unless the gcc-stack integration is not fully complete and as per freebasic documents it is very much available and usable but there is no example of curl usage provided unlike scriptbasic). An example of using cgi html template, http header and curl should make understanding things more simple, and make freebasic more powerful.

This (fbchttpd) reminded me of the eszter engine (sbhttpd) of scriptbasic which can be used for heavy duty web applications, so I thought this (or similar module) should have already been included as an additional library or module (fbchttpd), and maybe borrow the feature of “”" “”" (triple quotes) and var and __var all of python and $ (dollar) and @ variables both of perl and #{ } of ruby to make web applications more easier into freebasic just the same way as scriptbasic has already implemented. Since freebasic is more oops oriented (like c++ and ruby) than scriptbasic, it can also import some ruby features such as (<<, <==>, =~, etc) to make web applications more easier (although in case of scriptbasic, if we include the ‘t module’, we can already use oops in scriptbasic and writing of web applications look easier, but freebasic’s feature of usage of replacing of goto with oop function call alternative makes it look more elegant).

This way we can improve the server side web pages using options other than the simple cgi module of webservers, considering the fact that modbasic (modfreebasic and modscriptbasic) should not give them any speed advantage over modcgi (since basic dialects always run commands line by line rather than programs or blocks, which means even the web modules has to execute commands line by line thereby slowing down the process).

Then there also needs to be support for inclusion (addition) of the json module (javascript server object notation) into freebasic, so as to acquire its ability to run client side freebasic web programs exchanging data from server side freebasic cgi web programs, or at least an interaction between freebasic-json-client programs with embedded scriptbasic-sbhttpd-cgi-server programs (as currently freebasic and scriptbasic both lead the traditional basic and using both together covers all the possible requirements for any task, or rather some kind of hybrid of both these dialects looks even better, hope to come across something like that in obasic (openbasic)). Also, we need also to add the support for memcache module to speeden up the freebasic based web programs.

So far the support for json and memcache module are missing even in scriptbasic (but there is a non-json alternative support for javascript, such as by using a combination of dom’s mydiv, html_forms_element’s myselect and XmlHttpRequest’s ajax.request, only creating and grouping together these and similar functions, soap, rest and others under json remains), and as such the web toolbox options are incomplete without these.

Also, once when the simplehttpserver module (fbchttpd) gets included into freebasic, eventually the application server framework similar to ruby-on-rails, and eventually also a content-management-framework should be targeted next for all basic dialects, whether freebasic or scriptbasic. If fbchttpd runs then at least one can hope for at least one of the two, i.e. to use freebasic-fbchttpd-cgi-server programs with scriptbasic-json-client programs (once again embedded into freebasic) at the moment.

Without features such as json, memcache, “”" “”", $, #{ }, =~, etc would mean using freebasic almost as an almost boilerplate template or a local gui container interface (except for the curl module and the simplewebserver) somewhat relying on embedded scriptbasic code to do some critical web related tasks, because in real scenarios even databases forbid direct access to clients without channeling via webserver or framework so its existing set of tools cannot be made use of unless and until those features are available. Or at least if FBIde and FBEdit also offer to add scriptbasic compiler directory, and callback toolkit for guiding of scriptbasic embedding into freebasic. In contrast to this in case of scriptbasic there is only one major thing missing – i.e. the scriptbasic ide itself (otherwise everything appears to be possible with scriptbasic), and plus the further development of the ‘t module’ (oop) with more inbuilt functions and subroutines to make it look as elegant as freebasic code, developing a more elegant alternative to the use of goto (e.g. for exception and error handling), etc.