Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 09:00 05 May 2024 Privacy Policy
Jump to

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 : Question re: HTML served by Micromite

Author Message
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3018
Posted: 11:30am 27 Apr 2017
Copy link to clipboard 
Print this post

This is really an HTML question, not a Micromite question, but I think it's related to my HTML being served by a Micromite Explore-28 instead of a normal server, so I'll ask here.

Here is the page I'm serving:



It displays well, but the connection doesn't close, which causes problems with Chrome on a PC (I have to press "Reload") but not on Firefox and other browsers I've tested. The problem also occurs on Opera and Chrome on Android.

I start the transmission after the Micromite decodes a request with:
HTTP/1.1 200 OK
Content-type: text/html
Connection: close

Then the body:
<html> <title>Solar Greenhouse System</title> <head></head> <BODY bgcolor='#d0d0d0'>

[etc.]
and close with " </form> </BODY> </html>"

I have "Y"ed off the serial output from the Micromite to a serial port for monitoring, so I can see that this is exactly what is being sent.

The requests I'm getting from the different browsers include "Connection: keep-alive", which I suspect is causing my problem. With the Firefox browser, the page loads but the little wheel on the browser tab goes round and round (as can be seen in the image)--but with Chrome, I get "Page can't load", but [Reload] works (and the wheel goes around).

Is there something else I can do to make sure the connection closes (assuming that that is my problem)?

I've attached the entire HTML output. The part beginning <html> can be saved to a file and it loads perfectly well into a browser.

2017-04-27_212427_gh_html_output.zip

(I have addressed the "favicon" issue with an embedded data icon, if anyone thinks that might be the problem--I see now no favicon requests.)Edited by lizby 2017-04-28
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 09:37pm 27 Apr 2017
Copy link to clipboard 
Print this post

The problem is not with the HTML.
It has more to do with the HTTP protocol and the underlying TCP protocol.

HTTP uses a header "content-length" to tell the receiving end how many bytes of data to expect. After this the connection can be closed.

For dynamically created content you will not know at the time the headers are send how long the data stream will be.
You just stream the data and at the end you close the connection on the server side.
Best to wait a little while as it can be possible that the closing command will terminate the last data block. This is a brute force method not the recommended way.

A better solutions (if possible) is to do a 'half close' on the tcp connection. You close the output and then wait until the receiver also closes it output. Then you close it fully. This can only work when you have access to the tcp connection. Even then you should have a timeout so that if the receiver does not close its side, you still brute force close your side fully.

For all the detail about http see: http://www.w3.org/Protocols/rfc2616/rfc2616.txt


It is also good practice to include a doctype as it forces the best rendering mode on browsers in this example it is HTML5:
[code]
<!DOCTYPE html>
<html>
<head>
..
</head>
<body>
...
</body>

</html>
[/code]
Edited by MicroBlocks 2017-04-29
Microblocks. Build with logic.
 
panky

Guru

Joined: 02/10/2012
Location: Australia
Posts: 1097
Posted: 02:26am 28 Apr 2017
Copy link to clipboard 
Print this post

@lizby

Out of curiosity, how was the html created? It appears to be extraordinarily complex code with some mismatched elements (not being critical, just curious);-)

Doug.

... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it!
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3018
Posted: 02:49am 28 Apr 2017
Copy link to clipboard 
Print this post

Microblocks--since this is served on a Micromite Explore-28 through a little openWrt router running ser2net (which converts serial input to TCP), I'm not sure how I either close or half-close the TCP connection. Do you see a way?

It may be possible for me to build the entire string on the micromite using the long string function, then I would know the content-length and could use that before squirting out the html.

I'll add the "<!DOCTYPE html>".[DONE]

PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3018
Posted: 03:15am 28 Apr 2017
Copy link to clipboard 
Print this post

Doug--could you explain what you mean by mismatched elements? (By the way, I thought overnight that I might not have <table></table>, <tr></tr>, <td></td> properly balanced, so I'm writing a program to outline that structure).

The HTML code is built following a practice I've been using since 2006 with the picaxe and SimpleLan ethernet module (long since discontinued).

I hand-code an html template and designate variables that I want to be able to change with a "~" followed by 2 digits. For example, I customarily use "~01" for date and "~02" for time. I store the template on the microprocessor--either in eeprom for a picaxe, on the SD for a Micromite Explore-64, or after being coded programmatically as basic assignment statements for the Explore-28--and then run through it in code replacing the "~nn" text with current variable values.

In order to align things the way I want, I use a table as master container, then other tables within it to create a column structure.

The template will display in a browser, though not all the "~nn"s will (because of their use as "checked" fields for checkboxes and radio boxes, or as background color--happily browsers generally ignore what they don't understand).

Here is the displayed template for this application--you can see "~30"-"~35" which will be replaced with sensor readings, and also "~40"-"~45", which can be used to simulate those sensor readings for testing the code when the "AUTO" operating mode is selected.




I am serving this remotely (through the openWrt box and ser2net and my gateway router) so that someone who is 100 miles away can test it.

The system also includes a Nextion OLED for local control. The hardware looks like this.




Lance

PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8592
Posted: 04:59am 28 Apr 2017
Copy link to clipboard 
Print this post

As an experiment try specifying HTML V1.0 and remove "Connection Close"

1.0 didn't have the content length facility and the browser should drop the link when it sees /HTMLEdited by matherp 2017-04-29
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 06:32am 28 Apr 2017
Copy link to clipboard 
Print this post

I think that matherp means HTTP v1.0
HTML is just a way of how the content is defined.
You can also send text over http or images etc.

HTML is also not depended on HTTP and HTTP is not depended on TCP although those are 99.99999% of its use.

If the above is not available then you can always force close the connection. Just wait 500-1000ms (or longer if necessary) after the last data is send.


Microblocks. Build with logic.
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3018
Posted: 08:04am 28 Apr 2017
Copy link to clipboard 
Print this post

Thanks for the suggestions. If I haven't confused myself trying the permutations, "HTTP/1.0 200 OK" with no "Connection Close" made no difference--I still got back "HTTP/1.1" and "Connection: keep-alive", and still the browser didn't indicate that the connection was closed. And I got favicon requests again, perhaps because with HTTP 1.0 requested, the data tag was ignored.

"<!DOCTYPE html>" didn't work with HTTP 1.0 requested (no surprise there). With HTTP 1.1 I could see no difference.

I double-checked, with the assistance of a program, and it appears to me that the <table></table>, <tr></tr>, and <td></td> tags are balanced (remembering that I have tables within <td></td> tags).

I think the next thing to try is to get an accurate Connection-Length.

It works pretty well on Firefox and IE--I'd just like it to work without reloading on Chrome on the PC and especially Chrome and Opera on Android.

Microblocks--with this setup, going through ser2net, how would I close the connection?



Edited by lizby 2017-04-29
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 06:53pm 28 Apr 2017
Copy link to clipboard 
Print this post

"Connection:close" header is normally send from the browser.
In modern browsers it is always "Connection:Keep-Alive" and under HTTP 1.1 it is also the default. It is the client that should disconnect.

How a close can be done through ser2net i don't know as i never used it.
It might work by close the serial port on the micromite side.
Maybe the ser2net docs has something about it, but i think the ser2net is also built with the idea that the client controls the opening/closing of the tcp connection.

Just reading about ser2net it seems to just be a proxy between tcp and a serial port. It is transparent to the data that is being send. So all the headers that are send will be ignored by ser2net. In the case a webserver is used, you have more control over the connection, with ser2net i suspect you have none on the 'server' side.

It is open source thought so you might be able to adapt the code to inspect the data coming from the serial port for a special character or escape sequence to close the tcp connection.
Microblocks. Build with logic.
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3018
Posted: 05:15am 29 Apr 2017
Copy link to clipboard 
Print this post

"[ser2net] is open source thought so you might be able to adapt the code to inspect the data coming from the serial port for a special character or escape sequence to close the tcp connection."

That sounds hard. Easier to count the characters. I tried to make it so that there were few if any variations in the length of the served text. This involved changing the full month and day names to 3-character shortenings, replacing the color names "chartreuse" and "red" with their hex codes, "#7fff00" and "#ff0000", and putting "......." in checkboxes and radio boxes not "checked" (which browsers ignored).

Then I ran through my output routine, counting but not outputting the characters. That gave me a "Content-Length" to plug in when I actually output the HTML upon receiving a browser request. I padded the end with some extra spaces.

As a result, the connection successfully closes now on Chrome and IE and Firefox on the PC, and the glitches with Chrome which required "Reload" have ceased. It also works on Android with Chrome. On Android with Opera, the first request works, but subsequent updates result in the HTML code being printed as text, even when I include "<!DOCTYPE html>" at the beginning (that displays too, and the same without it).

So even with a remaining glitch with one browser on Android, I am happy with the solution. Thanks very much for all the suggestions.

Edited by lizby 2017-04-30
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 06:41am 29 Apr 2017
Copy link to clipboard 
Print this post

Great that you can set the content-length. This is the correct way.
However be aware that the connection stays open, it might be impossible to accept a new connection for a certain time. Maybe test with two browsers at the same time. (I think that is a ser2net limitation)
As the connection is not really close by the browser (keep-alive) and also not by the server
The header "Content-type: text/html" should have done the trick with Opera.
Could it be that the content-type header is missed the second time? Presumably the connection was still open and somehow the headers were skipped?
Could also be a bug on the Opera side.

Microblocks. Build with logic.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 5913
Posted: 01:11pm 29 Apr 2017
Copy link to clipboard 
Print this post

ser2net is definitely one connection at a time.
You can set a timeout to clear the connection with inactivity. I am not sure how short that timeout can be and it set in the conf file so can't get changed 'on the fly'

Jim

VK7JH
MMedit   MMBasic Help
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3018
Posted: 03:30pm 29 Apr 2017
Copy link to clipboard 
Print this post

Yes, one connection at a time with ser2net. I have gotten the ser2net message "serial port already open" a number of times in testing the multiple browsers when I had neglected to close one browser window before trying a different browser.

Because of the nature of this application--as the interface for a control system--there is no expectation that in real life there will ever be more than one browser at a time (or more than one user) trying to access it.

Lance

PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Print this page


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

© JAQ Software 2024