• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

Python in to the magic mirror.

Scheduled Pinned Locked Moved Solved Troubleshooting
43 Posts 4 Posters 19.0k Views 4 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    paulvanderheijden90
    last edited by Jan 24, 2019, 12:51 PM

    I changed the Node_helper.js:
    0_1548334181951_Schermafbeelding 2019-01-24 om 13.47.25.png

    But the minute I run the Start dev I get white screens. so im not able to debug anything.
    Can I get a hand ?

    0_1548334276267_Schermafbeelding 2019-01-24 om 13.51.05.png

    ? 1 Reply Last reply Jan 24, 2019, 1:07 PM Reply Quote 0
    • S Offline
      sdetweil
      last edited by Jan 24, 2019, 1:00 PM

      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…

      Sam

      How to add modules

      learning how to use browser developers window for css changes

      P 1 Reply Last reply Jan 24, 2019, 1:06 PM Reply Quote 0
      • P Offline
        paulvanderheijden90 @sdetweil
        last edited by Jan 24, 2019, 1:06 PM

        @sdetweil Thank you for you’re information.
        I changed it like you said but unfortunly still get the With screens.

        0_1548335197627_Schermafbeelding 2019-01-24 om 14.05.28.png

        1 Reply Last reply Reply Quote 0
        • ? Offline
          A Former User @paulvanderheijden90
          last edited by A Former User Jan 24, 2019, 1:08 PM Jan 24, 2019, 1:07 PM

          @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.

          P 2 Replies Last reply Jan 24, 2019, 1:24 PM Reply Quote 1
          • P Offline
            paulvanderheijden90 @Guest
            last edited by Jan 24, 2019, 1:24 PM

            @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

            1 Reply Last reply Reply Quote 0
            • P Offline
              paulvanderheijden90 @Guest
              last edited by paulvanderheijden90 Jan 25, 2019, 10:23 AM Jan 25, 2019, 10:22 AM

              @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 : 0_1548411234909_Schermafbeelding 2019-01-25 om 11.09.42.png

              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)
              
                  })
              
                }
              
              })
              

              0_1548411531981_Schermafbeelding 2019-01-25 om 11.18.28.png

              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")
              

              0_1548411681323_Schermafbeelding 2019-01-25 om 11.10.18.png

              So I think the spawn function is not correct at this time?
              Can you help me out one more time please?

              ? 1 Reply Last reply Jan 25, 2019, 11:26 AM Reply Quote 0
              • ? Offline
                A Former User @paulvanderheijden90
                last edited by A Former User Jan 25, 2019, 11:26 AM Jan 25, 2019, 11:26 AM

                @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)
                 })
                
                P 1 Reply Last reply Jan 25, 2019, 1:15 PM Reply Quote 0
                • P Offline
                  paulvanderheijden90 @Guest
                  last edited by Jan 25, 2019, 1:15 PM

                  @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)
                  
                      })
                  
                    }
                  
                  ? 1 Reply Last reply Jan 25, 2019, 1:18 PM Reply Quote 0
                  • ? Offline
                    A Former User @paulvanderheijden90
                    last edited by Jan 25, 2019, 1:18 PM

                    @paulvanderheijden90
                    Crashing? show me the error log.

                    P 1 Reply Last reply Jan 25, 2019, 1:19 PM Reply Quote 0
                    • P Offline
                      paulvanderheijden90 @Guest
                      last edited by Jan 25, 2019, 1:19 PM

                      @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.
                      
                      
                      
                      ? 1 Reply Last reply Jan 25, 2019, 1:22 PM Reply Quote 0
                      • 1
                      • 2
                      • 3
                      • 4
                      • 5
                      • 2 / 5
                      2 / 5
                      • First post
                        17/43
                        Last post
                      Enjoying MagicMirror? Please consider a donation!
                      MagicMirror created by Michael Teeuw.
                      Forum managed by Sam, technical setup by Karsten.
                      This forum is using NodeBB as its core | Contributors
                      Contact | Privacy Policy