Read the statement by Michael Teeuw here.
Python in to the magic mirror.
-
@sean
1.Try absolute path of your target py script.
( Oke I add the Path )2.When your child_process.spawn is called, python script will be executed.
( oke that’s Clear.)3 Well, node_helper.js is also needed. (* the codeYou send me I add in the node_helper.js.
3-1. Add your module into your config.js either. ( Yes I did. The example you send me was working. )
I can print something on the magic mirror on my own build Module.4 When your python script is executed on the shell, normal stdout will be printed out on the shell.
( Yes this is working wenn I executed on the shell it will print what I give in so that site wil look fine.So thank you for the answers. Im almost sure the Python project is oke. because it wil “print” thrue the sys.stdout.write function.
This is the Node_helper.js . I think this is fine aswell.
This is the MMM_. Module. im not sure I did this one oke.
And ofcourse I add the config. file.
Can you Please have a look what is going wrong because my magic mirror is now black and I can only restart bij ctrl alt del. so please help.
-
@paulvanderheijden90
Yournode_helper.js
is not correct.See other’s module as reference.
Or you can get a hint of executing python script in node_helper.js
https://github.com/eouia/MMM-GroveGestures/blob/master/node_helper.js
(I’m using PythonShell instead child_process in that module.) -
@paulvanderheijden90 start the mirror in developer mode
npm start dev
and look at the console tab for errors (red text)
now in your node_helper spawn data.on() routine, you need to add a sendSocketNotification(:I_DID", some_data) to get the info to the Module.js socketNotificationReceived() method so you can do something to display it (call updateDom(), which will schedule a call to getDom()
-
@sean said in Python in to the magic mirror.:
@paulvanderheijden90
Your node_helper.js is not correct.Hello Sean,
Why is it wrong? I copy it from your post earlier.
I started in Dev mode. I noticed that wenn I add the Node_helper.js module in the MMM-P2000 map the Magic mirror crashes and give only black screen.
I studyed the Node_helper file. I see in the first line you define the function and in the second line you define the settings for that function. So that’s clear to me. In the next lines you define the stdout of the process and copy it in de Console Log. so that’s also clear for me. So I down’t know why is it not working and crashes.I made in the same map ( home/pi/src/ ) a file called test.py Here I defined a string and I write the string to the stdout so as you say before the python project don’t need a separate call. So that should work as well.
Node_helper.js:
Python project:
I also getting in to the module setup so I changed the MMM-P2000.js and I think it is oke like this.
First the start function restart every 1000mS the dom function.
then the getdom Function is now showing the test “Hello World” in the module
so that should be oke aswell. IHow do I need to link the socked notification message to the Module string?
-
@paulvanderheijden90 node helper needs to look like this
see the doc here https://github.com/MichMich/MagicMirror/tree/master/modules#the-node-helper-node_helperjsconst NodeHelper = require('node_helper'); // any other imports (javascript require) module.exports = NodeHelper.create({ // local variables self: 0, pins_loaded:[], pin_index:0, results:{}, using_chartjs: true, suspended: false, timer:null, start: function() { console.log("Starting module: " + this.name); self = this; }, socketNotificationReceived: function(notification, payload) { // usually the module send the config data down to the node_helper if (notification === 'CONFIG') { this.config = payload; } // any other functions, u need, note, after each function is a ',' (comma) } });
-
node_helper.js
is helper script of your module to use various features of NodeJS. your main module script(MMM-P2000.js
) is just front-end javascript on Browser(Electron/Chromium). So when you need more features which are not supported on browser level, usenode_helper.js
MMM-P2000.js
andnode_helper.js
can communicate with each other by socketNotification.you need to make proper
node_helper.js
which could be used in MM framework.Usual format is
var NodeHelper = require("node_helper") module.exports = NodeHelper.create({ start: function() { // ... }, socketNotificationReceived: function(notification, payload) { // ... }, })
There are two entry points in node_helper.js from your main module .
start will be called when MM is executed and your module is loaded. Usually you can prepare things here.
socektNotificationReceived will be called when your main module send socketNotification with sendSocketNotification.
With this, you can transfer config values, specific datas or order to do something.
Your current
MMM-P2000.js
is not so good to use. I recommend you continue to read my https://forum.magicmirror.builders/topic/8534/head-first-developing-mm-module-for-extreme-beginners more. -
I changed the Node_helper.js:
But the minute I run the Start dev I get white screens. so im not able to debug anything.
Can I get a hand ? -
the spawn stuff needs to be INSIDE the node helper…
probably called in Start or in the socketNotificationReceived, when the config is sent down from the module…
-
@sdetweil Thank you for you’re information.
I changed it like you said but unfortunly still get the With screens. -
@paulvanderheijden90
If possible, Instead image, post code itself.
Anyway, You’d better to read docs more carefully.MMM-P2000.js
/// MMM-P2000.js Module.register("MMM-P2000", { getDom: function() { var e = document.createElement("div") e.id = "DISPLAY" return e }, notificationReceived: function(notification, payload, sender) { switch(notification) { case "DOM_OBJECTS_CREATED": var timer = setInterval(()=>{ this.sendSocketNotification("GIVE_ME_DATA") }, 1000) break } }, socketNotificationReceived: function(notification, payload) { switch(notification) { case "HERE_IS_DATA": var e = document.getElementById("DISPLAY") e.innerHTML = payload break } }, })
node_helper.js
/// node_helper.js const spawn = require("child_process").spawn var NodeHelper = require("node_helper") module.exports = NodeHelper.create({ socketNotificationReceived: function(notification, payload) { switch(notification) { case "GIVE_ME_DATA": this.job() break } }, job: function() { var process = spawn("python", ["/home/pi/src/test.py"]) process.stdout.on("data", (data)=>{ console.log(data) this.sendSocketNotification("HERE_IS_DATA", data) }) } })
I didn’t test. I wrote this code with only my brain. so there could be errors, but you can catch the concept.