MagicMirror Forum

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • Donate
    • Discord
    MagicMirror² v2.20.0 is available! For more information about this release, check out this topic.

    SOLVED Simple Text that reads a .txt file

    Requests
    2
    15
    2154
    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.
    • J
      Jackkt last edited by

      Hey,
      So I have a python script that uses facial recognition and prints out the name of the person. I want this to display on the magic mirror. Guidance on how to do it would be welcome as well.
      Thanks

      S 1 Reply Last reply Reply Quote 0
      • S
        sdetweil @Jackkt last edited by sdetweil

        @Jackkt you need to use the node_helper module to launch your python script, and capture the output of the python script… then send that up to the module.js via sendSocketNotification…

        then the module needs to signal updateDom() to tell MM there is new content, and it will call back at getDom() to get the new content, where u format your html to be shown.

        node_helper

        const { exec } = require('child_process');
        
        exec('python your_script', (error, stdout, stderr) => {
          if (error) {
            console.error(`exec error: ${error}`);
            return;
          }
          console.log(`stdout: ${stdout}`);
           this.sendSocketNotification('some id string', stdout);
          console.log(`stderr: ${stderr}`);
        });
        

        module.js

        socketNotificationReceived: function(notification, payload) {
           if( notification ==='some id string') {
           var save_payload=payload)
           this.updateDom(1000) 
           }
        }
        getDom: function (){
           create the html here using save_payload)
           return created_html
           MM will inject the html in the div for your module
        }
        

        you can use my sample module for starters…
        https://github.com/sdetweil/SampleModule

        check out the difference between exec (one time) and spawn (send multiple responses over time)

        Sam

        Create a working config
        How to add modules

        J 1 Reply Last reply Reply Quote 1
        • J
          Jackkt @sdetweil last edited by

          @sdetweil Thanks for the response. Im having trouble making the module(I have no experience and the "Head first developing MM module for extreme beginner"didnt work). Also The python script can run in the background and can output into a .txt file. Can I just edit helloworld to just print the thing in the text file? Sorry if this is stupid.

          J 1 Reply Last reply Reply Quote 0
          • J
            Jackkt @Jackkt last edited by

            @Jackkt Also the text file updates every 5 seconds by the results of the python script

            S 1 Reply Last reply Reply Quote 0
            • S
              sdetweil @Jackkt last edited by

              @Jackkt if the python script can be changed to just write to the screen, you can use spawn instead. And it can send u info every time it writes

              Sam

              Create a working config
              How to add modules

              J 1 Reply Last reply Reply Quote 1
              • J
                Jackkt @sdetweil last edited by

                @sdetweil Hey, I get a syntax error from “const { exec } = require(‘child_process’); unexpected token {” its the one before exec. Get the same from spawn.
                Thanks

                J 1 Reply Last reply Reply Quote 0
                • J
                  Jackkt @Jackkt last edited by

                  @Jackkt Also without the {} the error is “unexpected identifier” for
                  “spawn”

                  S 1 Reply Last reply Reply Quote 0
                  • S
                    sdetweil @Jackkt last edited by

                    @Jackkt that leading quote shouldn’t be there

                    Sam

                    Create a working config
                    How to add modules

                    J 1 Reply Last reply Reply Quote 1
                    • J
                      Jackkt @sdetweil last edited by

                      @sdetweil I was just quoting sorry it looks like:
                      const { exec } = require(‘child_process’);

                      and

                      const exec = require(‘child_process’);

                      S 1 Reply Last reply Reply Quote 0
                      • S
                        sdetweil @Jackkt last edited by

                        @Jackkt ok. I just cut/pasted it from a working module.

                        When posting code or config in the forum, please use the markup syntax. 3 backticks (key left of number 1) before your content on a separate line
                        And 3 more after on a separate line

                        Sam

                        Create a working config
                        How to add modules

                        S J 2 Replies Last reply Reply Quote 1
                        • 1
                        • 2
                        • 1 / 2
                        • First post
                          Last post
                        Enjoying MagicMirror? Please consider a donation!
                        MagicMirror created by Michael Teeuw.
                        Forum managed by Paul-Vincent Roll and Rodrigo Ramírez Norambuena.
                        This forum is using NodeBB as its core | Contributors
                        Contact | Privacy Policy