User avatar
luchiand
Posts: 44
Joined: Sun Feb 24, 2019 8:36 am
Location: Copenhagen
Contact: Skype

I wrote a program in Python but I want to send data to a JavaScript program

Tue May 07, 2019 7:36 pm

I have a program in Python that collects data from different sensors and I want to make some of this data used by a Java script. I can write them in a file on the SD card and read them from that file. But I need to display this values in real time. So I have to write and read too much on the SD card. Is there another solution not to use the SD card and use only the RAM? I try to create a virtual file system in Python with FS but I could not install the library for some reason. I use LAMP as a web server (Apache).
I will be thankful for any advice.

pfletch101
Posts: 624
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: I wrote a program in Python but I want to send data to a JavaScript program

Tue May 07, 2019 11:54 pm

One answer is to use a directory that has been mapped to tmpfs (data kept in RAM rather than on disk). I have found that the published recipes for modifying fstab to add additional tmpfs mounts freeze my system when the revised fstab is loaded, but a number of directories (including /run) are mapped to tmpfs by default, and one of these could be used for your temporary file(s).

Heater
Posts: 16092
Joined: Tue Jul 17, 2012 3:02 pm

Re: I wrote a program in Python but I want to send data to a JavaScript program

Wed May 08, 2019 2:50 am

Can I assume that your Javascript is running under node.js? Seeing as you said you are writing to files from Python and reading from JS.

Or are is that Javascript running in the browser and making requests to your server which then reads the file(s) and returns the data in the response.

Or is your display not even in the browser? If not how?

Also how much data are you talking about? Presumably if it's for display to a user it cannot be such a huge data rate.

ll in all we need more information on what you are actually wanting to do to be able to advise sensibly.

Anyway you don't need actual files. Unless you intend to actually store data long term. Just connect the programs with some interprocess communication mechanism, sockets, named pipes etc. Then all data is exchanged through RAM. Unless you have really huge amounts of it that should be fine.

But really, this simplest thing to do here is to not use Python. Use Javascript running under node.js to read your sensors. That same Javascript/node.js can be your web server with the addition of a few line of code. Then it has all the data it needs internally anyway, no communication between programs with files or anything required.
Memory in C++ is a leaky abstraction .

User avatar
luchiand
Posts: 44
Joined: Sun Feb 24, 2019 8:36 am
Location: Copenhagen
Contact: Skype

Re: I wrote a program in Python but I want to send data to a JavaScript program

Wed May 08, 2019 7:59 am

I have already written in python 4 programs. They interact in a certain maner but I used files to comunicate. The files are writen each hour on the SD card and on an external drive, some pictures are saved when motions are detected. I have to work too much to rewrite the main program in another programming language. I wrote a server and a client on the same machine and they work fine but I used python for both of them. I use also python for client on another device and they comunicate via socket perfectly.

All I need now is the server that will be integrated in my program can send a 4 digit number to java script. So I need a javascript client on the same compter or a virtual file in ram to be read from javascript. I want to avoid writing and reading the SD card where the OS is installed.

I will try what you wrote here. Unfortunately I don’t know to work with node yet but have to learn it. I needed. a simpler solution just to send unidirectional from python to java script without using the SD card.
Thank you for your advice.

User avatar
rin67630
Posts: 1038
Joined: Fri Mar 04, 2016 10:15 am

Re: I wrote a program in Python but I want to send data to a JavaScript program

Wed May 08, 2019 8:09 am

You may use the PTY library to create a pair of pseudo TTY ports.
You write to the master and Java reads from the slave as if it were a serial port. It works also in reverse direction.

Take a look a that example:

Code: Select all

import os, pty
import sys
import threading
import usb.core
import sched, time
from threading import Timer

master,slave = pty.openpty() #open the pseudoterminal pair
s_name = os.ttyname(slave)   #translate the slave fd to a filename
m_name = os.ttyname(master)  #translate the slave fd to a filename

print ("Slave name is: " , s_name, " | Slave FD is: " , slave)
print ("Master name is: ", m_name, " | Master FD is: " , master)
print("To test: minicom -H -w -D %s" % s_name)
print("To run DFLD, edit file Station.ini and replace Device by Device=%s" % s_name)


def update():
  # define here the content AK to be sent 
  os.write( master , AK.to_bytes(1,byteorder='big'))
  root.update_idletasks() 
  t = Timer(1.0, update)
  t.start();

update()

ghp
Posts: 1517
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: I wrote a program in Python but I want to send data to a JavaScript program

Wed May 08, 2019 8:18 pm

Hello, could you give mire details on how this javascript code is started ? Running standalone, or is it in a browser?

PhatFil
Posts: 1685
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: I wrote a program in Python but I want to send data to a JavaScript program

Wed May 08, 2019 8:54 pm

not quite what you asked for but you may want to consider using mqtt and host a broker on your pi. then add the lines to publish your sensor data as mqtt topic/payload messages to your python scripts, and then in your javascript subscribe to the relevant topic/s.

this will also open your data to any mqtt capable system on your pi or net for analysis or control purposes.

python mqtt libs
https://pypi.org/project/paho-mqtt/
javascript mqtt libs
https://github.com/mqttjs/MQTT.js#example

Return to “Python”