Page 2 of 3

Re: Communication via http

Posted: Sat Jan 06, 2018 3:22 pm
by serge_girard
Great Work Marek!

Also wiating to see it!

Serge

Re: Communication via http

Posted: Sat Jan 06, 2018 7:47 pm
by tonton2
bonsoir tout le monde
bravo et bon courage marek
NB: j'utilise pour Arduino le langage LADDER

Re: Communication via http

Posted: Sun Jan 07, 2018 12:16 pm
by nekbmm
+1

Re: Communication via http

Posted: Sun Jan 07, 2018 2:53 pm
by mol
Here is the code waiting for http request. If I want to control lamp, I need to call computer running this app via internet browser or another app calling www site http://localhost:80/KOD?12345

Code: Select all

#include "hmg.ch"
#require "hbhttpd"

MEMVAR server, get, post, cookie, session

Function Main

	public oServer
	public cAdresKontrolera := "http://192.168.20.190"
	SET LANGUAGE TO POLISH
	SET EPOCH TO 2000
	set century off
	set date ansi

	/*
	DEFINE WINDOW Form_1 ;
		AT 0,0 ;
		WIDTH 640 HEIGHT 490 ;
		TITLE 'Odczyt kolektora' ;
		MAIN ;
		FONT 'Arial' SIZE 10 

		DEFINE BUTTON P_StartSerwer
			ROW	10
			COL	10
			CAPTION "START"
			ACTION StartServer() 
			WIDTH 120
			HEIGHT 27 
		END BUTTON

	
	END WINDOW

	CENTER WINDOW Form_1

	ACTIVATE WINDOW Form_1
	*/
	
	StartServer() 
	
Return Nil
*------------------------------
function StartServer
	
	nPort := 80
	public oLogAccess := UHttpdLog():New( "eshop_access.log" )

	IF ! oLogAccess:Add( "" )
		oLogAccess:Close()
		? "Access log file open error " + hb_ntos( FError() )
		RETURN
	ENDIF

	public oLogError := UHttpdLog():New( "eshop_error.log" )

   IF ! oLogError:Add( "" )
      oLogError:Close()
      oLogAccess:Close()
      ? "Error log file open error " + hb_ntos( FError() )
      RETURN
   ENDIF

   oServer := UHttpdNew()
   IF ! oServer:Run( { ;
         "FirewallFilter"      => "", ;
         "LogAccess"           => {| m | oLogAccess:Add( m + hb_eol() ) }, ;
         "LogError"            => {| m | oLogError:Add( m + hb_eol() ) }, ;
         "Trace"               => {| ... | QOut( ... ) }, ;
         "Port"                => nPort, ;
         "Idle"                => {| o | iif( hb_FileExists( ".uhttpd.stop" ), ( FErase( ".uhttpd.stop" ), o:Stop() ), NIL ) }, ;
         "PrivateKeyFilename"  => "private.key", ;
         "CertificateFilename" => "certificate.crt", ;
         "SSL"                 => .F., ;
         "Mount"          => { ;
         "/hello"            => {|| UWrite( "Hello!" ) }, ;
         "/info"             => {|| UwRITE("Sterowanie bramkami w magazynie Kern-Liebers") }, ;
		 "/KOD"	=> {|| OdczytKodu()};
         } } )
      oLogError:Close()
      oLogAccess:Close()
      ? "Server error:", oServer:cError
      ErrorLevel( 1 )
      RETURN
   ENDIF

   oLogError:Close()
   oLogAccess:Close()

   RETURN

function OdczytKodu
	local aKody := {}
	local cBramka
	
	aKody := HB_HKeys(get)

	for i:=1 to len(aKody)
		uWrite(aKody[i] + "<br>")
		cBramka := SomeTranslationFromCodeToGateNumber(aKody[i])
		SendRequestToArduino(cAdresKontrolera +"/BRAMKA=" +cBramka )
	next i
return
*----------------------
function SomeTranslationFromCodeToGateNumber
	cCode
	
return cCode
*-----------------

PROCEDURE SendRequestToArduino
	param cURL
	
	LOCAL oCon, i
	local ret := .f.
	local cOdp:=""

	for i:=1 to 3
		// liczba prób
		oCon := TipClientHttp():New( cUrl )
		IF oCon:Open()
			// tutaj wymuszam zadziałanie przełącznika :)
			cOdp := oCon:ReadAll()
			//msgdebug("Connected", cOdp)
			oCon:close()
			ret := .t.
			exit
		endif
	next i
	
	if !ret
		MsgBox("Nie mogę się połączyć, błąd" + oCon:lastErrorMessage())
	endif
RETURN ret
*------------------------
this is first preview only :)


Ps. My solution is based on eshop.prg from harbour contrib

Re: Communication via http

Posted: Sun Jan 07, 2018 3:19 pm
by edk
THX!

Re: Communication via http

Posted: Sun Jan 07, 2018 7:00 pm
by serge_girard
Thanks Marek for sharing!

Serge

Re: Communication via http

Posted: Sun Jan 07, 2018 8:51 pm
by mol
Tomorrow I' ll start production tests.
It's beautiful to join something material like lamps, readers with software.

Re: Communication via http

Posted: Sun Nov 01, 2020 12:22 am
by jairpinho
mol wrote: Sun Jan 07, 2018 2:53 pm Here is the code waiting for http request. If I want to control lamp, I need to call computer running this app via internet browser or another app calling www site http://localhost:80/KOD?12345

Code: Select all

#include "hmg.ch"
#require "hbhttpd"

MEMVAR server, get, post, cookie, session

Function Main

	public oServer
	public cAdresKontrolera := "http://192.168.20.190"
	SET LANGUAGE TO POLISH
	SET EPOCH TO 2000
	set century off
	set date ansi

	/*
	DEFINE WINDOW Form_1 ;
		AT 0,0 ;
		WIDTH 640 HEIGHT 490 ;
		TITLE 'Odczyt kolektora' ;
		MAIN ;
		FONT 'Arial' SIZE 10 

		DEFINE BUTTON P_StartSerwer
			ROW	10
			COL	10
			CAPTION "START"
			ACTION StartServer() 
			WIDTH 120
			HEIGHT 27 
		END BUTTON

	
	END WINDOW

	CENTER WINDOW Form_1

	ACTIVATE WINDOW Form_1
	*/
	
	StartServer() 
	
Return Nil
*------------------------------
function StartServer
	
	nPort := 80
	public oLogAccess := UHttpdLog():New( "eshop_access.log" )

	IF ! oLogAccess:Add( "" )
		oLogAccess:Close()
		? "Access log file open error " + hb_ntos( FError() )
		RETURN
	ENDIF

	public oLogError := UHttpdLog():New( "eshop_error.log" )

   IF ! oLogError:Add( "" )
      oLogError:Close()
      oLogAccess:Close()
      ? "Error log file open error " + hb_ntos( FError() )
      RETURN
   ENDIF

   oServer := UHttpdNew()
   IF ! oServer:Run( { ;
         "FirewallFilter"      => "", ;
         "LogAccess"           => {| m | oLogAccess:Add( m + hb_eol() ) }, ;
         "LogError"            => {| m | oLogError:Add( m + hb_eol() ) }, ;
         "Trace"               => {| ... | QOut( ... ) }, ;
         "Port"                => nPort, ;
         "Idle"                => {| o | iif( hb_FileExists( ".uhttpd.stop" ), ( FErase( ".uhttpd.stop" ), o:Stop() ), NIL ) }, ;
         "PrivateKeyFilename"  => "private.key", ;
         "CertificateFilename" => "certificate.crt", ;
         "SSL"                 => .F., ;
         "Mount"          => { ;
         "/hello"            => {|| UWrite( "Hello!" ) }, ;
         "/info"             => {|| UwRITE("Sterowanie bramkami w magazynie Kern-Liebers") }, ;
		 "/KOD"	=> {|| OdczytKodu()};
         } } )
      oLogError:Close()
      oLogAccess:Close()
      ? "Server error:", oServer:cError
      ErrorLevel( 1 )
      RETURN
   ENDIF

   oLogError:Close()
   oLogAccess:Close()

   RETURN

function OdczytKodu
	local aKody := {}
	local cBramka
	
	aKody := HB_HKeys(get)

	for i:=1 to len(aKody)
		uWrite(aKody[i] + "<br>")
		cBramka := SomeTranslationFromCodeToGateNumber(aKody[i])
		SendRequestToArduino(cAdresKontrolera +"/BRAMKA=" +cBramka )
	next i
return
*----------------------
function SomeTranslationFromCodeToGateNumber
	cCode
	
return cCode
*-----------------

PROCEDURE SendRequestToArduino
	param cURL
	
	LOCAL oCon, i
	local ret := .f.
	local cOdp:=""

	for i:=1 to 3
		// liczba prób
		oCon := TipClientHttp():New( cUrl )
		IF oCon:Open()
			// tutaj wymuszam zadziałanie przełącznika :)
			cOdp := oCon:ReadAll()
			//msgdebug("Connected", cOdp)
			oCon:close()
			ret := .t.
			exit
		endif
	next i
	
	if !ret
		MsgBox("Nie mogę się połączyć, błąd" + oCon:lastErrorMessage())
	endif
RETURN ret
*------------------------
this is first preview only :)


Ps. My solution is based on eshop.prg from harbour contrib
hello Marek, I would like to know how you added the core.prg files within HARBOR \ contrib \ hbhttpd. because i don't have anything inside this folder, so i found it on github but i don't know how to compile, this refro is the future for hmg works with webservice using restful, managing to compile this resource i'll follow the development of the rest server for hmg, however i'm stuck in this part. already use in another language and I will try in hmg

Re: Communication via http

Posted: Mon Nov 02, 2020 7:08 am
by mol
I don't remember any problems with compiling it.
my configuration file from this project:

Code: Select all

incpaths=
libpaths=
libs=hbtip
libs=curl
libs=hbcurls
libpaths=.
libs=curldll
gt=
mt=no
instpaths=
Nothing special

Re: Communication via http

Posted: Mon Nov 02, 2020 9:28 pm
by danielmaximiliano
mol wrote: Thu Jan 04, 2018 1:09 pm Hi guys!
I've realised my first Arduino project. It will switch on/off lamp indicators depend on barcode scanner read in the warehouse.
Barcode scanner sends barcode to computer, computer sends info to my arduino which lamp should be switched on.

I've created http server which wait for info from computer via http.
I can control lamps via internet browser, calling arduino server: http://192.168.20.190/BRAMKA=3
I want to create simple application, which can call such a page.
Second problem is to act as http server to read info from wifi barcode reader.
Can anybody provide simplest http server application?

regards, Marek
does your scanner have websocket property?