m sayed
m sayed

Reputation: 3

How to import python's loop data into javascript

I've a python file reads serial data from arduino which is generated in loop.

#!/usr/bin/python
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
while True:
  print(ser.readline())
ser.close()

How to import these data by Javascript?

I used xmlhttprequest to open the python file in a loop but it's re-opening the python file rather than streaming the serial data.. Can I loop the xmlhttprequest response to retrieve the data?

Is there anyway to retrieve the python's data in javascript?

Upvotes: 0

Views: 63

Answers (1)

shaswat.dharaiya
shaswat.dharaiya

Reputation: 451

Try this then.

This will write a file in python

f = open("sensor.txt", "a")
while True:
  f.write(ser.readline())
ser.close()
f.close()

I am guessing you are using some HTML with JS, so try this to read it in JS.

<body>
    <input type="file" name="inputfile"
            id="inputfile">
    
    <script>
        document.getElementById('inputfile')
            .addEventListener('change', function() {
              
            var fr=new FileReader();
            fr.onload=function(){
                document.getElementById('output')
                        .textContent=fr.result;
            }
              
            fr.readAsText(this.files[0]);
        })
    </script>
</body>

Upvotes: 1

Related Questions