PDA

View Full Version : 18F87J50 as a webserver



Christopher4187
- 15th October 2013, 22:36
I'd like to use an 18F87J50 + RN-171 as a web server. I'm not looking to do anything complex as this is new territory for me. Something like a few buttons, text boxes, etc to get a better understanding of how it all works. A few questions to start:

1. Is this possible?

2. What kind of programming is involved?

3. How much memory do I need save on the 18F87J50?

4. Do you or can you save files onto a PIC18? (I haven't used the memory to save anything other than digital and analog variables)

This is more of a generic question - I've used a Netburner module in the past and with that I saved an HTML file to the module. Instead of using an HTML file, is it possible to send a device (laptop, tablet, etc) the actual HTML codes? In other words, can you do something like:


HSEROUT <html>
HSEROUT<body>

HSEROUT<h1>TESTING....</h1>

HSEROUT<p>HELLO</p>

HSEROUT</body>
HSEROUT</html>

amgen
- 16th October 2013, 18:24
well, yes.................
I have worked on that very same thing, and found it very interesting.
I used a 18f2550 with 3K ram and 48K flash, and a WIZnet module for USART to network comm.
It turns out that it took several sort-of-independent functions working together.
So as a concept:
1) listen for a request for your web page......(to know when to send and what to send)
2)assemble or pull data to send, I used pages stored in high-up flash memory area, and sent with USART and interrupts
3) manipulate the network module to end TCP type connection so you can wait for another request
more discussion if interested.
don

Christopher4187
- 16th October 2013, 18:51
2)assemble or pull data to send, I used pages stored in high-up flash memory area, and sent with USART and interrupts
This is where I'm stuck. How do you compile data where it will end up being displayed correctly on a browser? How do you store an HTML page (or similar) on the memory of a PIC?

amgen
- 17th October 2013, 01:32
you don't have to compile, just create your web page, as html or css etc, test it in browser, then place the text in flash mem at some indexable location to be sent out as serial data. I found that it was also necessary to add an HTTP header (a few lines of text, not part of the HTML ) that directs the data over TCP connection.

7104

Christopher4187
- 17th October 2013, 02:20
Perfect! That's the information I was looking for. Thanks for the quick reply.

I do have one question though.
place the text in flash mem at some indexable location to be sent out as serial data.How exactly do I place the text into flash memory? I've never done this before or if I have I don't realize I've done it.

So here's some dumb questions:

1. When you say flash memory, are you referring to the 128KB of RAM (code space) that the 18F87J50 has? The main page states that it has 1Mbit of Flash but I'm not sure what the difference is?

2. How can I tell the amount of Flash I have remaining? If this is the code space, disregard the question as I see it in the lower left hand corner after compiling PBP.

3. Finally, how exactly do I get the HTML commands into the Flash? Is it something like I stated at the bottom of my first post?

Thanks again for the help.

amgen
- 17th October 2013, 13:14
after you figure how much code mem your program needs, use the asm and 'org' functions to start putting your html (see code) at locations past that. You can use a blocks scheme like A000, B000 etc which are 4Kbytes blocks for example. The "hlpMPASMAsm.chm" help file in the mpasm folder explains this and other info about MPASM assembler. Place the org stuff at the end of all basic code because basic will continue to compile in mem in linear fashion. The text needs to be in quotes which brings up some other issues when you need carriage return or line feed etc. the 'db' is data byte also explained in help file. And if I misstated about compiling, all this gets Built to program as usual.


AX = AX + 1
endif
@ INT_RETURN

END

''==============TABLES============= ''!!! ADD 0,0,0 TO END
ASM
tbl1 org 0xA400
db "HTTP/1.0 200 OK",13,"Content-Length: 800 ",13,13
db "<html>",13,"<h1> PIC 18F...TT Server v1.0</h1>",13,"<input type=",34,"text",34," id=",34,"txt",34,">"
db "<script type=",34,"text/javascript",34,">" ,13," var c=0;var t;var timer_is_on=0; "
db "function timedCount(){document.getElementById('txt').value= c;c=c+1; "
db "t=setTimeout(",34,"timedCount()",34,",1000);}function doTimer(){if (!timer_is_on){timer_is_on=1;timedCount();}} "
db "</script><form method=",34,"get",34,">Project name: <input type=",34,"text",34," name=",34,"PIC",34," value=",34,"PIC18F2525 ",34," />"
db "<br>select one: <select><option>Amps</option> "
db "<option>Volts</option><option>Frequency</option> </select>"
db "<input type=",34,"submit",34," value=",34,"Submit",34," /></form> "
db "<body onLoad=",34,"doTimer()",34,"><p><b>Note: TEXT HERE </b><br><script type=",34,"text/vbscript",34,">"
db "document.write(NOW & ",34," Hello World!",34,")</script><br></body></p> </html> "
db "<style type=",34,"text/css",34,">body{background-color:#d0e4fe;}h1{color:red;} style> "
db "<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR> ",0,0,0
db "<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR> ",0,0,0


db 32,32,32,69,110,116,101,114,32,111,114,32,112,97,1 15,116,101,32,116,101
db 120,116,32,116,111,32,98,101,32,99,111,110,118,101 ,114,116,101,100,32,104
db 101,114,101,46,46,46,46,46,46,46,46,46,46,46,46,46 ,46,0,0

ENDASM
'''---------------------------------------------------------------

Christopher4187
- 11th February 2014, 15:54
Agmen, can you help me a little more. I've been trying get the web server to work but I'm having no success. I'm confused about a few things so I stripped all of the features out of the code and went with something very simple.

I'm trying to use this code:
IF MESSAGE1[0]="G" AND MESSAGE1[1]="E" AND MESSAGE1[2]="T" THEN
LCDOUT $FE, 192, "SERVER HIT!"
HIGH LED1G
HIGH LED2G
HIGH LED3G
HSEROUT [13,13]
HSEROUT["<HTTP/1.0 200 OK Content-Length: 800>",13]
HSEROUT ["Content-Type: text/html",13]
hserout [13]
hserout ["<html>",13]
hserout ["<head>Test page</head>",13]
HSEROUT ["<body>This is a test page</body>",13]
hserout ["</html>",13]
HSEROUT [13,13]
PAUSE 500
Why doesn't this work? When I pull it up on a webpage, it doesn't work correctly. When I use "0,0,0" at the end, the webpage acts like it wants to download a file. Can you point me in the right direction?

amgen
- 12th February 2014, 13:29
hi,
My best understanding (and not real clear), the HTTP stuff uses the "Content-Length: ???" to give the html stuff the right amount of bytes then close the connection and render the page.
Try to figure what would be the correct 'Content-Length: ' and/or send at least that many serial bytes in 'Content-Length:???'.
and please share technical info of successes and failures.
I have been trying to keep up with web stuff, I think things get more straight forward as they evolve...... like HTML5
Seems like there should have been a command to "render page now" in html, but I could not find one for that in much google search.
don

Christopher4187
- 12th February 2014, 14:17
hi,
Try to figure what would be the correct 'Content-Length: ' and/or send at least that many serial bytes in 'Content-Length:???'.
and please share technical info of successes and failures.
Will do. One additional question. Why don't you have this line of code:
"HTTP/1.0 200 OK",13,"Content-Length: 800 ",13,13
written like this:
"<HTTP/1.0 200 OK",13,"Content-Length: 800>",13,13

amgen
- 12th February 2014, 20:21
there is a blank line required separating the HTTP header and html code.
I don't remember exactly what web, but search for HTTP explained has more info.....tons of info....too much info !!
good luck