Communication via http

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

Moderator: Rathinagiri

User avatar
serge_girard
Posts: 1959
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Has thanked: 350 times
Been thanked: 78 times
Contact:

Re: Communication via http

Post by serge_girard » Sat Jan 06, 2018 3:22 pm

Great Work Marek!

Also wiating to see it!

Serge

User avatar
tonton2
Posts: 247
Joined: Sat Jun 29, 2013 1:26 pm
Location: Algerie
Has thanked: 4 times
Been thanked: 7 times
Contact:

Post by tonton2 » Sat Jan 06, 2018 7:47 pm

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: 17
Joined: Sat Jul 16, 2016 3:16 am
DBs Used: DBF,SQLite
Location: Ivanjica, Serbia
Has thanked: 1 time

Post by nekbmm » Sun Jan 07, 2018 12:16 pm

+1

User avatar
mol
Posts: 2910
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Has thanked: 149 times
Been thanked: 82 times
Contact:

Post by mol » 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

edk
Posts: 232
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland
Has thanked: 67 times
Been thanked: 168 times

Post by edk » Sun Jan 07, 2018 3:19 pm

THX!

User avatar
serge_girard
Posts: 1959
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Has thanked: 350 times
Been thanked: 78 times
Contact:

Post by serge_girard » Sun Jan 07, 2018 7:00 pm

Thanks Marek for sharing!

Serge

User avatar
mol
Posts: 2910
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Has thanked: 149 times
Been thanked: 82 times
Contact:

Post by mol » Sun Jan 07, 2018 8:51 pm

Tomorrow I' ll start production tests.
It's beautiful to join something material like lamps, readers with software.

Post Reply