Re: Communication via http
Posted: Sat Jan 06, 2018 3:22 pm
Great Work Marek!
Also wiating to see it!
Serge
Also wiating to see it!
Serge
Exclusive forum for HMG, a Free / Open Source xBase WIN32/64 Bits / GUI Development System
http://hmgforum.com/
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
*------------------------
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 hmgmol 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
this is first preview onlyCode: 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 *------------------------
Ps. My solution is based on eshop.prg from harbour contrib
Code: Select all
incpaths=
libpaths=
libs=hbtip
libs=curl
libs=hbcurls
libpaths=.
libs=curldll
gt=
mt=no
instpaths=
does your scanner have websocket property?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