Read the statement by Michael Teeuw here.
Python in to the magic mirror.
-
@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.jsis 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.jsMMM-P2000.jsandnode_helper.jscan communicate with each other by socketNotification.you need to make proper
node_helper.jswhich 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.jsis 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.
-
@Sean Thank you very much im going to check the code and see if I can implement it in the magic mirror.
Thank you I will let you know -
@sean
Hello Sean, Again thank you very much for you’re valued help!.I spend yesterday after midday and evening with a friend figure out how you’re code is working. and we did some tests to send also somthing back to the Node helper and alote came clear by you’re example.
So realy thank u very much.To day I spend my morning figure out one last problem the text on the screen is [ Object ArrayBuffer] and not the string I put true the stdout.write.
The text that is displayed on the screen is :
I did some debugging of my own. and wenn I override the data in the node helper just after it come from the spawn function this is displayed on the mirror. so the code is working until the spawn function.
/// 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)=>{ data= "test hello World" console.log(data) this.sendSocketNotification("HERE_IS_DATA", data) }) } })
So i tested also the python test project. and wenn I run it in the terminal it give no error. so the python project is fine aswell I think.
import sys sys.stdout.write("Hallo Ik ben Paul")
So I think the spawn function is not correct at this time?
Can you help me out one more time please? -
@paulvanderheijden90 said in Python in to the magic mirror.:
Object ArrayBuffer
is one of javascript Object type. not text itself. (Sorry for my prior example. it needs to be converted from Object to HTMLText.)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
process.stdout.on("data", (data)=>{ console.log(data) // You can see the structure of this `data` object(ArrayBuffer) on the shell terminal. var result = String.fromCharCode.apply(null, new Uint16Array((data)) // You need to convert or filter or manipulate `data` object whatever for your real usage. this.sendSocketNotification("HERE_IS_DATA", result) }) -
@Sean Hello Sean,
I was thinking somting was wrong because I see the buffer printing in the terminal showing:Buffer 48 61 6c 6c 6f 20 49 6b 20 62 65 6e 20 50 61 75 6c>
i was trying to figure out the text line that it was representing but it was not showing something.
So im happy you came up with the solution for the buffer.
Unfortunly wenn at the var result code the magic mirror is crashing again and im not able to debug.
Can you please heave a look at it/// 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) var result = String.fromCharCode.apply(null, new Uint16Array((data)) this.sendSocketNotification("HERE_IS_DATA", result) }) } -
@paulvanderheijden90
Crashing? show me the error log. -
@sean said in Python in to the magic mirror.:
Crashing? show me th
App threw an error during load /home/pi/MagicMirror/modules/MMM-P2000/node_helper.js:76 }.call(this, exports, require, module, __filename, __dirname); }); ^ SyntaxError: missing ) after argument list at createScript (vm.js:80:10) at Object.runInThisContext (vm.js:139:10) at Module._compile (module.js:606:28) at Object.Module._extensions..js (module.js:653:10) at Module.load (module.js:561:32) at tryModuleLoad (module.js:504:12) at Function.Module._load (module.js:496:3) at Module.require (module.js:586:17) at require (internal/module.js:11:18) at loadModule (/home/pi/MagicMirror/js/app.js:127:17) Whoops! There was an uncaught exception... /home/pi/MagicMirror/modules/MMM-P2000/node_helper.js:76 }.call(this, exports, require, module, __filename, __dirname); }); ^ SyntaxError: missing ) after argument list at createScript (vm.js:80:10) at Object.runInThisContext (vm.js:139:10) at Module._compile (module.js:606:28) at Object.Module._extensions..js (module.js:653:10) at Module.load (module.js:561:32) at tryModuleLoad (module.js:504:12) at Function.Module._load (module.js:496:3) at Module.require (module.js:586:17) at require (internal/module.js:11:18) at loadModule (/home/pi/MagicMirror/js/app.js:127:17) MagicMirror will not quit, but it might be a good idea to check why this happened. Maybe no internet connection? If you think this really is an issue, please open an issue on GitHub: https://github.com/MichMich/MagicMirror/issues Launching application. -
@paulvanderheijden90 said in Python in to the magic mirror.:
Your log is telling you what is wrong.
See the 76 line ofnode_helper.js. There could be missing)after argument list. -
@sean So sorry you’re totally right… I was little bit confused by all the “at” conclusens.
Im sorry I could had fixed that. And I did !Now it is working !!!
So very nice ! im going to put this in my P2000 Raspberry python Programma. and will heave a lot of play with it.I will post/ show you the and result !
-
Hello @Sean,
Again thank you for the help. As I say after your help it was possible for me to “print” a string variable on the magic mirror module through python.
But is was not able to put my P2000 alarm from the fire department on my screen. I noticed that it was a problem in the python script of the alarm.
I used the stdout.write function again. like you told me. but I needed to add the flush function as wel.
so now it issys.stdout.write("hallo ik ben Paul") sys.stdout.flush()And then it was working !
so thank you and here the result: -
@sean
Hello Sean,
like I posted yesterday the app is running now. And it is showing wat I need to display. So im very happy.But…
After a time ( more after a couple of runs ) the magic mirror app is crashed and freezed. So noting is updating any more.My feeling is telling that the buffer were I put in the string value is filling up.
wenn i run the python script without the mirror it is running fine.
Please can you give me a help?
-
@paulvanderheijden90
I have no idea about Python. I think you’d better reboot once or twice per daily to maintain RPI to be stable. You can do it withcronautomatically.
The better approach would be implementing your job as native nodeJS MagicMirror module instead external python script. -
@paulvanderheijden90 can u put your project on github and tell me where it is… I will look at it later today (us time)
-
@sdetweil
Hallo Thank you for you’re ReplayI managed to upload the project on git hub ( new experience for me )
I uploaded the magic mirror Module ( P2000) I made and the node helper.And I added the Python project.
The python project works fine on it self. But wenn I call ( spawn ) the python. The magic mirror application stops and freezes.
I think the flush/write fills the buffer and wenn it is it full the application is stoped.
https://github.com/paulvanderheijden90/MMM-P2000-python-connect
Here is the link
-
sorry, the is no PUBLIC repo there
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login
