Real time Python data to Javascript

A forum for general discussion of the Python programming language.

Real time Python data to Javascript

Postby subhendusc » Thu Aug 08, 2013 7:25 pm

I wrote the below code to display value got by python with ser.readline() in a webpage. The data to be updated dynamically.
But on running it from localhost it is giving Internal server error 500.
Code: Select all
import os
import cgi, cgitb;
import sys, serial
sys.stderr = sys.stdout
ser = serial.Serial('COM27', 9600)
def myvalue():
 while 1:
    try:
        myvalue=ser.readline()

print "Content-type:text/html\n\n"
print """
<html>
<head>
<title>
Real Time Temperature
</title>
<script type="text/javascript">
    window.onload = startInterval;
    function startInterval()
    {
        setInterval("startMy();",1000);
    }

    function startMy()
    {
        document.getElementById('time').innerHTML = myvalue();
    }
  </script>
</head>
<body>
<h1>Real Time Temperature:</h1>
<div id="time"></div>
</body></html>
"""
ser.close()
sys.exit(0)


Where am I going wrong?
Can this HTML be a separate file and still can get the ser.readline() data.? If yes how. Please give me a complete example. I am learning python to implement OpenCV and arduino in the web. This can be done only through python
Last edited by Yoriz on Thu Aug 08, 2013 7:55 pm, edited 1 time in total.
Reason: Fixed code tags
subhendusc
 
Posts: 3
Joined: Thu Aug 08, 2013 7:00 pm

Re: Real time Python data to Javascript

Postby Yoriz » Thu Aug 08, 2013 7:56 pm

Hi welcome to the forum,
Please ensure you have read the new user read this link in my signature.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 789
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Real time Python data to Javascript

Postby subhendusc » Fri Aug 09, 2013 12:44 pm

I have modified the code to this
Code: Select all
#!c:\Python27\Python.exe
import cgi, cgitb;
import sys, serial ,time
cgitb.enable()
ser = serial.Serial('COM27', 9600)
numOfLines = 0
print "Content-type:text/html\n\n"
print """
            <html>
            <head>
            <title>
            Real Time Temperature
            </title>
            <body><h1>Real Time Temperature:</h1></body>
            """
while True:
    ser.flushOutput()
    response = ser.readline()
    print """
            <script type="text/javascript">
            var int=self.setInterval(function(){refer()},1000);
            function refer()
            {
            document.getElementById('mine').innerHTML ="""+response+""";
            }
            </script>
            </head>
            <body>
            <div id="mine"></div>
            </body></html>
            """

ser.close()
sys.exit(0)

But I am not satisfied with the coding. I feel it is clumsy. Also two things are happening
1. The reading rate is getting slower with time.
2. The browser is busy all the time. We cannot use the cursor to do any other thing like pressing a button to do some other work.
How these two can be avoided. ?
Also can anyone clean up the code please.
subhendusc
 
Posts: 3
Joined: Thu Aug 08, 2013 7:00 pm


Return to General Discussions

Who is online

Users browsing this forum: micseydel and 2 guests