Hi,
what commands and syntax must be used for simple TCP send and receive data between
PC and ESP?
How to do simple TCP communication betwenn PC and ESP?
-
- Posts: 2
- Joined: Mon Aug 09, 2021 11:49 pm
- cicciocb
- Site Admin
- Posts: 1966
- Joined: Mon Feb 03, 2020 1:15 pm
- Location: Toulouse
- Has thanked: 422 times
- Been thanked: 1310 times
- Contact:
Re: How to do simple TCP communication betwenn PC and ESP?
Hi DK3GR,
this is a minimalist example based on the use of PowerBasic and Annex.
PowerBasic has the capability to establish TCP connections so it is quite easy to use it with Annex.
The exchange is done using the event [Local Link Removed for Guests] and the variables are exchanged using the format var1=10&var2=20&var3=30&....
This example simply send 3 variables and returns the sum of their value.
Hoping this is clear for you.
Annex Code
PowerBasic Code
this is a minimalist example based on the use of PowerBasic and Annex.
PowerBasic has the capability to establish TCP connections so it is quite easy to use it with Annex.
The exchange is done using the event [Local Link Removed for Guests] and the variables are exchanged using the format var1=10&var2=20&var3=30&....
This example simply send 3 variables and returns the sum of their value.
Hoping this is clear for you.
Annex Code
Code: [Local Link Removed for Guests]
' Annex controlled by powerbasic http demo
OnUrlMessage received
wait
received:
wlog UrlMsgGet$()
a = val( UrlMsgGet$("a"))
b = val( UrlMsgGet$("b"))
c = val( UrlMsgGet$("c"))
UrlMsgReturn "The sum is " + str$(a+b+c)
return
Code: [Local Link Removed for Guests]
' PowerBasic example with Annex
' send variables and receive the content
#COMPILE EXE
FUNCTION PBMAIN() AS LONG
LOCAL Buffer$, Site$, Arg$
Site$ = "192.168.1.12" ' IP address of the module
Arg$ = "/msg?a=123&b=200&c=300" ' variables sent to Annex
' Connecting...
TCP OPEN "http" AT Site$ AS #1 TIMEOUT 1000 ' maybe increase a little bit
' Could we connect to site?
IF ERR THEN
BEEP
EXIT FUNCTION
END IF
' Send the GET request...
TCP PRINT #1, "GET " & Arg$ & " HTTP/1.1"
TCP PRINT #1, "Host: " + Site$
TCP PRINT #1, "User-Agent: Annex"
TCP PRINT #1, "Connection: close"
TCP PRINT #1, ""
' Send and retreive the message
TCP RECV #1, 4096, Buffer$
#DEBUG PRINT Buffer$
' extract the result after a double CR LF
ret_msg$ = MID$(Buffer$, INSTR(Buffer$, $CRLF + $CRLF) + 4)
MSGBOX ret_msg$
' Close the TCP/IP port...
TCP CLOSE #1
END FUNCTION
-
- Posts: 2
- Joined: Mon Aug 09, 2021 11:49 pm
Re: How to do simple TCP communication betwenn PC and ESP?
Hi ciciocb,
now i am happy and here is the result of my work:
There are two minor problems:
1. To stop the programm i have to press the STOP butten in the IDE followed by
a reconnect. Then i must press again the STOP butten and the monitor will
answer "program finished". Why not on first STOP? Is this normal?
2. The responce time fpr this bidirctional HTTP communication is not very fast.
The TCP communication on my other modules with TCP commands is faster.
But at the moment i think it is fast enough.
For the next steps i will add:
1. commands for READ, WRITE and CLEAR EEprom.
2. Add data for mini windows on the captured picture (pos x,y, size x,y)
with 6? items.
3. Compare mini window pixels with the next captured pic and detect movement
of a modell locomotive. The result will be continous polled by the Host PC.
For example: On movement in square window 50,100,30,30 lok must stop
If this job is done, one camera for just 10 dollar can control a complete
central station. The Metallus Radio Train System (MRTS) is much cheaper
and has more possibiltys than the slow uncertain communnication over the tracks.
Thank you very much for your qualified and fast help!
now i am happy and here is the result of my work:
Code: [Local Link Removed for Guests]
LEDpin=33
pin.mode LEDpin, output
FLASHpin=4
pin.mode FLASHpin, output
pin(FLASHpin)=0
print
print
toggle 3
MIN_SPACE = 100000 'take pictures only if some space is left on flash or SD-card
wifi.connect "xx","xxxxxxxxxxx" ' ypur wifi access
print "connecting ";
While WIFI.STATUS <> 3
Print ".";
pause 500
wend
print
print "connected, ";
wmode=wifi.mode
if wmode=0 then print "wifi sleep moce"
if wmode=1 then print "wifi station mode"
if wmode=2 then print "wifi AP mode"
pause 2000
print
print ip$
print
rf=ramfree
ff=flashfree
print "RAM free:",rf
print "Flash free:",ff
print
ret=bas.device
print "Basic device:",ret
'if bas.device=202 then print "ESP32CAM detected"
OnUrlMessage received
'wait
z=0
do
z=z+1
option.wdtreset ' reset watchdog timer
loop
end
received:
wlog UrlMsgGet$()
a$=urlmsgget$("a")
print a$
r$=a$
kw$=ucase$(mid$(a$,1,3))
arg1$=mid$(a$,5,4)
arg2$=mid$(a$,10,4)
arg3$=mid$(a$,15,4)
arg1=val("&H"+arg1$)
arg2=val("&H"+arg2$)
arg3=val("&H"+arg3$)
if kw$="VER" then cmdVER ' VERsion
if kw$="RST" then cmdRST ' ReSeT
if kw$="CSU" then cmdCSU ' Camera SetUp
if kw$="FLH" then cmdFLH ' FLasH on/off
UrlMsgReturn r$
return
sub cmdVER
res1=1:res2=2:res3=3:n$="0000":n=0
hx res1
r1$=n$
hx res2
r2$=n$
hx res3
r3$=n$
r$="VER"+" "+r1$+" "+r2$+" "+r3$
end sub
sub cmdRST
res1=1:res2=2:res3=3:n$="0000":n=0
hx res1
r1$=n$
hx res2
r2$=n$
hx res3
r3$=n$
r$="RST"+" "+r1$+" "+r2$+" "+r3$
reboot
end sub
sub hx(n)
n$=hex$(n)
n$="0000"+n$
n$=right$(n$,4)
end sub
sub LEDon(LEDpin)
pin(LEDpin)=0
end sub
sub LEDoff(LEDpin)
pin(LEDpin)=1
end sub
sub toggle(n)
local i
print "Toggle ";n
LEDoff LEDpin
pause 1000
for i=1 to n
pause 300
LEDon LEDpin
pause 300
LEDoff LEDpin
next i
end sub
sub cmdFLH ' cmd flash
res1=1:res2=2:res3=3:n$="0000":n=0 ' setup default result
hx res1
r1$=n$ ' result 1
hx res2
r2$=n$ ' result 2
hx res3
r3$=n$ ' result 3
if arg1=0 then ' check arguments
pin(FLASHpin)=0 ' do the job
else
pin(FLASHpin)=1
end if
r$="FLH"+" "+r1$+" "+r2$+" "+r3$ ' setup return string
end sub
sub cmdCSU ' cmd camera setup
res1=1:res2=2:res3=3:n$="0000":n=0 ' setup default result
hx res1
r1$=n$ ' result 1
hx res2
r2$=n$ ' result 2
hx res3
r3$=n$ ' result 3
option.wdt 10000 'watchdog timer 10seconds
wifi.power 20 'maximize wifi-power
' variables with CAM parameters
quality = 10 : brightness = 0
contrast = 0 : saturation = 0
awb = 1 : awb_gain = 1
wb_mode = 0 : aec = 0
aec2 = 1 : ae_level = 0
agc = 0 : agc_gain = 0
gainceiling = 0
ret = camera.params("quality", quality)
ret = camera.params("brightness", brightness)
ret = camera.params("contrast", contrast)
ret = camera.params("saturation", saturation)
ret = camera.params("awb", awb)
ret = camera.params("awb_gain", awb_gain)
ret = camera.params("wb_mode", wb_mode)
ret = camera.params("aec", aec)
ret = camera.params("aec2", aec2)
ret = camera.params("ae_level", ae_level)
ret = camera.params("agc", agc)
ret = camera.params("agc_gain", agc_gain)
ret = camera.params("gainceiling", gainceiling)
option.wdtreset ' reset watchdog timer
if flashfree < MIN_SPACE then ' check memory
print "NO SPACE FOR FOTO!"
else
print "make Foto"
endif
ret=CAMERA.PICTURE("/PIC.jpg") ' set framesize
print
hx ret
r1$=n$
r$="FLH"+" "+r1$+" "+r2$+" "+r3$ ' setup return string
end sub
1. To stop the programm i have to press the STOP butten in the IDE followed by
a reconnect. Then i must press again the STOP butten and the monitor will
answer "program finished". Why not on first STOP? Is this normal?
2. The responce time fpr this bidirctional HTTP communication is not very fast.
The TCP communication on my other modules with TCP commands is faster.
But at the moment i think it is fast enough.
For the next steps i will add:
1. commands for READ, WRITE and CLEAR EEprom.
2. Add data for mini windows on the captured picture (pos x,y, size x,y)
with 6? items.
3. Compare mini window pixels with the next captured pic and detect movement
of a modell locomotive. The result will be continous polled by the Host PC.
For example: On movement in square window 50,100,30,30 lok must stop
If this job is done, one camera for just 10 dollar can control a complete
central station. The Metallus Radio Train System (MRTS) is much cheaper
and has more possibiltys than the slow uncertain communnication over the tracks.
Thank you very much for your qualified and fast help!
- cicciocb
- Site Admin
- Posts: 1966
- Joined: Mon Feb 03, 2020 1:15 pm
- Location: Toulouse
- Has thanked: 422 times
- Been thanked: 1310 times
- Contact:
Re: How to do simple TCP communication betwenn PC and ESP?
Hi,
Happy to hear that it worked for you.
About the "stop", I don't know.
Happy to hear that it worked for you.
About the "stop", I don't know.