Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : PicoMiteWeb firmware for the Pico W - now making real progress
Page 8 of 8 | |||||
Author | Message | ||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6103 |
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 VK7JH MMedit MMBasic Help |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3816 |
I was probably one - it hung with a blank page if that helps. John |
||||
TheMonkeys Regular Member Joined: 15/12/2022 Location: AustraliaPosts: 59 |
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 |
||||
lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3152 |
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. PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3152 |
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 PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
TheMonkeys Regular Member Joined: 15/12/2022 Location: AustraliaPosts: 59 |
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. |
||||
Calli Regular Member Joined: 20/10/2021 Location: GermanyPosts: 74 |
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 |
||||
atmega8 Guru Joined: 19/11/2013 Location: GermanyPosts: 722 |
I'am a wizzard, but have lost my glass sphere Not showing your code is very uncool. |
||||
Calli Regular Member Joined: 20/10/2021 Location: GermanyPosts: 74 |
It was a typo in option tcp Server Port ... Doh. |
||||
atmega8 Guru Joined: 19/11/2013 Location: GermanyPosts: 722 |
That's why showing th code is usefull for everybody.......... |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6103 |
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 VK7JH MMedit MMBasic Help |
||||
Calli Regular Member Joined: 20/10/2021 Location: GermanyPosts: 74 |
Oh! Yes, Fritzbox... Carsten |
||||
Page 8 of 8 |
Print this page |