Menu
JAQForum Ver 19.10.27

Forum Index : Microcontroller and PC projects : PicoMiteWeb firmware for the Pico W - now making real progress

   Page 8 of 8    
Posted: 06:37am
14 Feb 2023
Copy link to clipboard
TassyJim
Guru


Starting with Peter's example server code, I have expanded it to make a more generic web server.
I haven't tested all file types yet but so far, all is good.

You will need to modify it for any form data that needs acting on.

 OPTION EXPLICIT
 OPTION DEFAULT INTEGER
 
 DIM buff%(512)
 CONST maxargs=20
 CONST html_def$ = "index.html" ' default landing page
 DIM hit_count
 
 DIM mystring$="PicoMite Web Test"
 DIM FLOAT mytemp, myhumid, oldtemp, oldhumid
 DIM mytime$
 DIM INTEGER allrdg, badrdg
 
 TIMER = 3000
 'date$ = "01-04-2000"
 'on error skip
 WEB ntp 11
 'if date$ = "01-04-2000" then print "OOPS!"
 PRINT "Ready   ";DATE$
 WEB TCP INTERRUPT gotrequest
 
 DO
   IF TIMER > 3000 THEN doread ' background processing to gather data
 LOOP
 '
SUB gotrequest
 LOCAL p%, t%, a%, s$, i, nparams, page$, page_ext$, cons$
 LOCAL arg$(1,maxargs-1)
 FOR a%=1 TO MM.INFO(MAX connections)
   IF NOT MM.INFO(TCP request a%) THEN cons$= cons$ + "-" :CONTINUE FOR
   LONGSTRING CLEAR buff%()
   WEB TCP READ a%,buff%()
   INC hit_count
   IF LLEN(buff%()) THEN
     cons$=cons$+"*"
     page$=parsehtmldata$(nparams,buff%(), arg$())
     IF page$ = "" THEN page$ = "index.html"
     PRINT STR$(hit_count,7,0)+"   "+TIME$+"   "+page$
     IF INSTR(page$,".") THEN
       page_ext$ = LCASE$(MID$(page$,INSTR(page$,".")+1))
     ELSE
       page_ext$ = ""
     ENDIF
     ' process arguments before returning page with updated results
     FOR i=0 TO nparams%
       ' Print arg$(0,i),"=",arg$(1,i)
       IF arg$(0,i)="RB" THEN ' setting for radio button
         '
       ENDIF
       IF arg$(0,i)="RESET" AND arg$(1,i)="YES" THEN 'Reset maxmin
         '
       ENDIF
     NEXT i
     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"
           WEB TRANSMIT file a%,page$,"text/plain"
         CASE "cgi"
           WEB TRANSMIT file a%,page$,"application/x-httpd-cgi"
         CASE ELSE
           s$=LGETSTR$(buff%(),1,MIN(255,LLEN(buff%())))
           PRINT s$
           WEB TRANSMIT PAGE a%,"notfound.html"
           WEB TRANSMIT code a%, 404
       END SELECT
     ELSE
       WEB TRANSMIT PAGE a%,"notfound.html"
       PRINT "404 "+page$
       WEB TRANSMIT code a%, 404
     ENDIF
   ELSE
     cons$ = cons$+"0"
   ENDIF
 NEXT a%
 PRINT "status = "+cons$
 ' - = no conection, * = conection with data, 0 = connection but no data
END SUB
 
FUNCTION parsehtmldata$(paramcount AS INTEGER, inbuf() AS INTEGER, arg$())
 CONST starttext$="HTTP"
 LOCAL a$,b$
 LOCAL INTEGER buf(BOUND(inbuf()))
 LOCAL INTEGER inpos,startparam,processargs
 paramcount=0
 inpos=LINSTR(inbuf(),"GET /",1)
 IF inpos=0 THEN
   parsehtmldata$=""
 ELSE
   LONGSTRING MID buf(),inbuf(),inpos+5
   inpos=LINSTR(buf(),starttext$,1)
   IF inpos>2 THEN 'page request found
     inpos=inpos-2
     a$=LGETSTR$(buf(),1,inPos)
     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$ '"index.html"
   ENDIF
 ENDIF
END FUNCTION
 
SUB doread
 BITBANG HUMID GP17, mytemp, myhumid
 IF mytemp > 999 THEN
   mytemp = oldtemp
   myhumid = oldhumid
   ' print "!";
   badrdg = badrdg + 1
 ELSE
   oldtemp = mytemp
   oldhumid = myhumid
   ' print "*";
 ENDIF
 allrdg = allrdg + 1
 mytime$ = TIME$+"  "+DATE$+"   "+STR$(badrdg)+"/"+STR$(allrdg)
 TIMER = 0
END SUB


The code to read my DHT22 is included and runs every 3 seconds but that doesn't stop the normal actions of the server.
It can be bypassed if not required.

Jim
 
Posted: 10:20am
14 Feb 2023
Copy link to clipboard
JohnS
Guru

  TassyJim said  My attempt at hosting the MMEdit help files on a pico was a failure.
It worked well for local network use but the connections from the real world did not go well.
My Help system uses frames and the first connection requires 3 "pages" to get loaded. External users couldn't get that far.

I was probably one - it hung with a blank page if that helps.

John
 
Posted: 10:48am
14 Feb 2023
Copy link to clipboard
TheMonkeys
Regular Member


  TassyJim said  My attempt at hosting the MMEdit help files on a pico was a failure.
It worked well for local network use but the connections from the real world did not go well.
My Help system uses frames and the first connection requires 3 "pages" to get loaded. External users couldn't get that far.


My next experiment will be to implement "Asynchronous HTTP Requests". It works a bit like frames, but the overhead is lower.

I can post up some sample code if you are interested.

I like what you've done with the TCPrequest select. I was thinking of something similar, but using

dim typ$(8) length 25
typ$(0)="text/html":typ$(1)="text/html":typ$(2)="image/vnd.microsoft.icon"
typ$(3)="image/jpeg":typ$(4)="text/css":typ$(5)="text/javascript"
typ$(6)="image/png"
Dim ftyp$="piml html ico  jpg  css  js   png"

ft$=field$(page$,2,".")
j% =(Instr(ftyp$,ft$) - 1) / 5
If j% Then
WEB transmit file a%,page$,typ$(j%)
Else
WEB transmit page a%,page$,typ$(j%)
EndIf

"piml" being the extension for "Pico Interpreted Markup Language".
Edited 2023-02-14 21:36 by TheMonkeys
 
Posted: 02:38pm
14 Feb 2023
Copy link to clipboard
lizby
Guru

Thank you Jim and Tony. Now I understand the "{{". I'll try it in about an hour when it will have warmed up enough for me to go out to my desk on the screened porch.
 
Posted: 06:40pm
14 Feb 2023
Copy link to clipboard
lizby
Guru

  morgs67 said  Reformatted post from @TassyJim  Tony

Thank you, Tony (and Jim). Worked perfectly. Now that's a chunk of javascript which I can incorporate into many HTML pages.

~
Edited 2023-02-15 04:41 by lizby
 
Posted: 12:36am
16 Feb 2023
Copy link to clipboard
TheMonkeys
Regular Member


Hi Peter,
Just a thought.

How hard would it be to change the "{" escape character to "{="?

In my experience, I have never encountered "{=" in any (non-malformed) html, javascript, or css code.

As I said, Just a thought.
Cheers,
Chris.
 
Posted: 12:03pm
13 Mar 2023
Copy link to clipboard
Calli
Regular Member

Thats all exciting news!

I can connect my Pico W with my Wifi, however:

It will connect not the first time but always the second time after Reboot?

ntp works and I have send some request to adafruit.io (however only got Error 400 back but thats my fault I guess).

But none of the webserver examples gets a TCP-Request, the interrupt will never be called when I try my ip in a browser. No idea why.

BTW: PicoMiteWebV5.07.07a24.zip

Sure enough I can ping the Pico W

PS C:\Users\Carsten Wartmann> ping 192.168.0.126

Ping wird ausgeführt für 192.168.0.126 mit 32 Bytes Daten:
Antwort von 192.168.0.126: Bytes=32 Zeit=32ms TTL=255
Antwort von 192.168.0.126: Bytes=32 Zeit=39ms TTL=255



Best,
Carsten

PS: TFTP is cool :)
Edited 2023-03-13 22:20 by Calli
 
Posted: 12:42pm
13 Mar 2023
Copy link to clipboard
atmega8
Guru


  Calli said  Thats all exciting news!

I can connect my Pico W with my Wifi, however:

It will connect not the first time but always the second time after Reboot?

ntp works and I have send some request to adafruit.io (however only got Error 400 back but thats my fault I guess).

But none of the webserver examples gets a TCP-Request, the interrupt will never be called when I try my ip in a browser. No idea why.

BTW: PicoMiteWebV5.07.07a24.zip

Sure enough I can ping the Pico W

PS C:\Users\Carsten Wartmann> ping 192.168.0.126

Ping wird ausgeführt für 192.168.0.126 mit 32 Bytes Daten:
Antwort von 192.168.0.126: Bytes=32 Zeit=32ms TTL=255
Antwort von 192.168.0.126: Bytes=32 Zeit=39ms TTL=255



Best,
Carsten

PS: TFTP is cool :)


I'am a wizzard, but have lost my glass sphere  


Not showing your code is very uncool.
 
Posted: 02:12pm
13 Mar 2023
Copy link to clipboard
Calli
Regular Member

It was a typo in option tcp Server Port ...




Doh.
 
Posted: 02:20pm
13 Mar 2023
Copy link to clipboard
atmega8
Guru


  Calli said  It was a typo in option tcp Server Port ...




Doh.


That's why showing th code is usefull for everybody..........
 
Posted: 07:28pm
13 Mar 2023
Copy link to clipboard
TassyJim
Guru


  Calli said  I can connect my Pico W with my Wifi, however:

It will connect not the first time but always the second time after Reboot?

Is your router a FritzBox?
It always takes two attempts to get an IP from my Fritz.

I have set a watchdog timer so the second attempt is automatic.

Jim
 
Posted: 08:35am
15 Mar 2023
Copy link to clipboard
Calli
Regular Member

Oh! Yes, Fritzbox...

Carsten
 
   Page 8 of 8    


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2026