Wifi Communication between two Webmites


Author Message
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6546
Posted: 05:57am 22 Aug 2023      

Install the main program on the "remote" WEBmite. along with this index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>Remote location</title>
<p align="center">Temperature is {CPUtemp}</p>
</body></html>

You shold be able to connect with any web browser.
Load the same program on the "Main" WEBmite along with this index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>Main server</title>
<meta http-equiv="Refresh" content="15"></head><body>
<p align="center">Temperature is {CPUtemp}</p>
<p align="center">Remote Temperature is {remoteTemp}</p>
</body></html>

Lines 7 and 8 need changing. to the remotes IP and set isMaster =1

I have left AUTORUN disabled for now.
In the bas program these lines fetch the remote data and convert it ready for the main mite to display it.
 IF isMaster THEN
   WEB OPEN TCP CLIENT slaves$, 80
   WEB TCP CLIENT REQUEST "GET /index.html HTTP/1.1", inbuff%()
   WEB CLOSE TCP CLIENT
   'Longstring print inBuff%()
   r = LINSTR(inbuff%(), "Temperature") + 15
   remoteTemp = VAL(LGETSTR$(inbuff%(), r, 4))
   CPUtemp = PIN(temp)
 ELSE



BAS program:
 ' RTV monitor kw\WEBmite
 OPTION EXPLICIT
 OPTION DEFAULT INTEGER
 'option autorun on
 DIM Buff%(512)
 DIM inBuff%(512)
 DIM slaves$ = "10.1.0.230" ' this is the remote IP, needed on the Master
 CONST isMaster = 0          ' set to 1 for Master
 CONST qt$ = CHR$(34)
 CONST Maxargs=20
 CONST html_def$ = "index.html"
 
 DIM tickTime, CPUtemp, Quit
 DIM FLOAT remoteTemp
 DIM k$
 
 SETTICK 10000, ticker         ' reading temperature at 10 second interval
 WEB TCP INTERRUPT gotrequest
 ticker
 DO
   IF tickTime THEN dotick
   k$ = INKEY$
   IF k$ <>"" THEN
     SELECT CASE k$
       CASE "X","x"
         OPTION AUTORUN OFF
         WATCHDOG hw OFF
         Quit = 1
     END SELECT
   ENDIF
   WATCHDOG hw 8000
 LOOP UNTIL Quit
 
SUB ticker
 tickTime = 1
END SUB
 
SUB dotick
 LOCAL logstate, minLog$, hr$, r
 ' do all the readings here
 tickTime = 0
 WATCHDOG hw 8000
 IF isMaster THEN
   WEB OPEN TCP CLIENT slaves$, 80
   WEB TCP CLIENT REQUEST "GET /index.html HTTP/1.1", inbuff%()
   WEB CLOSE TCP CLIENT
   'Longstring print inBuff%()
   r = LINSTR(inbuff%(), "Temperature") + 15
   remoteTemp = VAL(LGETSTR$(inbuff%(), r, 4))
   CPUtemp = PIN(temp)
 ELSE
   CPUtemp = PIN(temp)*RND() ' randomise the slave so you can see changing values
 ENDIF
END SUB
 
SUB gotrequest
 LOCAL p%, t%, a%, s$, i, nparams, page$, page_ext$, cons$, newfolder$
 LOCAL arg$(1,maxargs-1), endtime
 FOR a%=1 TO MM.INFO(MAX CONNECTIONS)
   IF NOT MM.INFO(TCP REQUEST a%) THEN CONTINUE FOR
   LONGSTRING CLEAR Buff%()
   WEB TCP READ a%,Buff%()
   'Longstring print Buff%()
   IF LLEN(Buff%()) THEN
     page$=parsehtmldata$(nparams,Buff%(), arg$())
     ' if page$ = "" then page$ = html_def$
     IF INSTR(page$,".") THEN
       page_ext$ = LCASE$(FIELD$(page$,2,"."))
     ELSE
       page_ext$ = ""
     ENDIF
     IF MM.INFO$(FILESIZE page$) > 0 THEN ' file does exist.
       SELECT CASE page_ext$
         CASE "html", "htm"
           WEB TRANSMIT PAGE a%,page$
         CASE "ico"
           WEB TRANSMIT FILE a%,page$,"image/vnd.microsoft.icon"
         CASE "jpg"
           WEB TRANSMIT FILE a%,page$,"image/jpeg"
         CASE "png"
           WEB TRANSMIT FILE a%,page$,"image/png"
         CASE "gif"
           WEB TRANSMIT FILE a%,page$,"image/gif"
         CASE "bmp"
           WEB TRANSMIT FILE a%,page$,"image/bmp"
         CASE "js"
           WEB TRANSMIT FILE a%,page$,"script/javascript"
         CASE "txt", "csv", "bas", "py"
           WEB TRANSMIT FILE a%,page$,"text/plain"
         CASE "cgi"
           WEB TRANSMIT FILE a%,page$,"application/x-httpd-cgi"
         CASE ""
           WEB TRANSMIT FILE a%,page$,"text/plain"
         CASE ELSE
           WEB TRANSMIT PAGE a%,"notfound.html"
           WEB TRANSMIT CODE a%, 404
       END SELECT
     ELSE
       WEB TRANSMIT PAGE a%,"notfound.html"
       WEB TRANSMIT CODE a%, 404
     ENDIF
   ELSE
     '
   ENDIF
 NEXT a%
END SUB
 
FUNCTION parsehtmldata$(paramcount AS INTEGER, inbuf() AS INTEGER, arg$())
 CONST starttext$="HTTP"
 LOCAL a$,b$, post
 LOCAL INTEGER buf(BOUND(inbuf()))
 LOCAL INTEGER inpos,startparam,processargs
 paramcount=0
 inpos=LINSTR(inbuf(),"GET /",1)
 IF inpos=0 THEN inpos=LINSTR(inbuf(),"post /",1): post = 1
 IF inpos=0 THEN
   parsehtmldata$=""
 ELSE
   LONGSTRING MID buf(),inbuf(),inpos+5+post
   inpos=LINSTR(buf(),starttext$,1)
   IF inpos>2 THEN 'page request found
     inpos=inpos-2
     a$=LGETSTR$(buf(),1,inpos)
     ' print a$
     inpos=INSTR(a$,"?")
     IF inpos<>0 THEN 'parameters found
       processargs=1
       parsehtmldata$=LEFT$(a$,inpos-1)
       a$=MID$(a$,inpos+1)
       DO
         arg$(0,paramcount)=""
         arg$(1,paramcount)=""
         inpos=INSTR(a$,"=")
         startparam=1
         arg$(0,paramcount)=MID$(a$,startparam,inpos-startparam)
         startparam=inpos+1
         inpos=INSTR(a$,"&")
         IF inpos<>0 THEN
           arg$(1,paramcount)=MID$(a$,startparam,inpos-startparam)
           a$=MID$(a$,inpos+1)
           paramcount=paramcount+1
         ELSE
           arg$(1,paramcount)=MID$(a$,startparam)
           paramcount=paramcount+1
           processargs=0
         ENDIF
       LOOP WHILE processargs
     ELSE
       parsehtmldata$=a$
     ENDIF
   ELSE ' no page requested
     parsehtmldata$= html_def$
   ENDIF
 ENDIF
END FUNCTION


point your PC web browser to the main WEBmite and you should see:



The second line is from the remote mite.

This is one of many ways to skin the cat.

Jim