Communication via http

Other General Resources like icon sets, sound files etc.,

Moderator: Rathinagiri

User avatar
serge_girard
Posts: 3158
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Communication via http

Post by serge_girard »

Great Work Marek!

Also wiating to see it!

Serge
There's nothing you can do that can't be done...
User avatar
tonton2
Posts: 444
Joined: Sat Jun 29, 2013 1:26 pm
Location: Algerie
Contact:

Re: Communication via http

Post by tonton2 »

bonsoir tout le monde
bravo et bon courage marek
NB: j'utilise pour Arduino le langage LADDER
L'Algerie vous salut
Y.TABET
User avatar
nekbmm
Posts: 118
Joined: Sat Jul 16, 2016 3:16 am
DBs Used: DBF,SQLite
Location: Ivanjica, Serbia

Re: Communication via http

Post by nekbmm »

+1
User avatar
mol
Posts: 3718
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Communication via http

Post 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
edk
Posts: 909
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: Communication via http

Post by edk »

THX!
User avatar
serge_girard
Posts: 3158
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Communication via http

Post by serge_girard »

Thanks Marek for sharing!

Serge
There's nothing you can do that can't be done...
User avatar
mol
Posts: 3718
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Communication via http

Post by mol »

Tomorrow I' ll start production tests.
It's beautiful to join something material like lamps, readers with software.
User avatar
jairpinho
Posts: 420
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

Re: Communication via http

Post 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
Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br
User avatar
mol
Posts: 3718
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Communication via http

Post 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
User avatar
danielmaximiliano
Posts: 2607
Joined: Fri Apr 09, 2010 4:53 pm
Location: Argentina
Contact:

Re: Communication via http

Post 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?
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`. Harbour/HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

Whatsapp. := +54901169026142
Telegram Name := DaNiElMaXiMiLiAnO
Post Reply