Using TXT CivetWeb server as general web server (part 2)

Introduction

In this part I will show how to create a CGI. With C/C++ and/or with Python.
The C/C++ projects need to be compiled with the same Eclipse configuration as used for the download programs an the SLI programs, see the installation section in: 
https://github.com/fischertechnik/txt_demo_c_download/blob/master/README.md orhttps://github.com/fischertechnik/txt_demo_ROBOPro_SLI/blob/master/README.md

Startpoint

We have already setup a Civetweb server as we have shown in de post “Using TXT CivetWeb server as general web server (part 1A)”

First expriment

Add some files to the document_root. And discover the CGI-script with Python or a pre-compiled C/C++ CGI-script.

Example of a simple C/C++ script

filenaam: cpptest.cgi, in /var/www, don’t forget to adjust the file attributes.

This file needs to be compiled with the same Eclipse configuration as used for the download programs an the SLI programs, see the installation section in: https://github.com/fischertechnik/txt_demo_c_download/blob/master/README.md

Compiles the main.cpp to TxtCgiTest01.cgi
Web page: TxtCgiTest01.html

/**********************************************************
 *  Compiler:   gcc / g++
 *
 * Simple CGI example
 *
  **********************************************************/
#include <stdio.h>          // for printf()
#include <unistd.h>         // for sleep()
//#include "KeLibTxtDl.h"     // TXT Lib
//#include "FtShmem.h"        // TXT Transfer Area
int main(void)
{
     printf("Content-type: text/html\n\n") ;
     /** Print the HTML response page to STDOUT. **/
     printf("<html>\n") ;
     printf("<head><title>CGI Output</title></head>\n") ;
     printf("<body>\n") ;
     printf("<h1>Hello, world.</h1>\n") ;
     printf("</body>\n") ;
     printf("</html>\n") ;
 return 0;
}

Example of a simple Python script

filenaam: pytest.cgi, in /var/www, don’t forget to adjust the file attributes.

This example make use of the already pre-installed FtRoboPy. It is also possible to use the fischertechnik C/C+ libraries directly from a C/C++ CGI-script.

module#!/usr/bin/python3
# Import modules for CGI handling
import cgi, cgitb
import ftrobopy
import sys
print ("Content-type:text/html\r\n\r\n")
print ('<html>')
print ('<head>')
print ('<title>Hello Word - First CGI Program</title>')
print ('</head>')
print ('<body>')
print ('<h2>Hello Word! This is my first CGI program</h2>')
print ('<p>')
txt=ftrobopy.ftrobopy('auto')
print ("1<br/>")
txt.play_sound(int(1),int(0))
print ("1A<br/>")
while not txt.sound_finished():   pass
print ("2<br/>")
Motor_links  = txt.motor(2)
print ("3<br/>")
Motor_links.setSpeed(512)
print ("4<br/>")
Motor_links.setDistance(500, syncto=0)
print ('5<br/>')
while not Motor_links.finished():
  print ('r')
print ("<br />")
txt.stopOnline()
print ("End FT program<br/>")
print ('</p>')
print ('</body>')
print ('</html>')

There are more options to use the public data area as document_root for the CivetWeb server.

  • Change the document_root in a way that it point to under the user: ROBOPro accessible data area.
  • start a new instance of the CivetWeb server on a different port (8081) and document_root in a way that it point to under the user: ROBOPro accessible data area.
Print Friendly, PDF & Email