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

How to use the 'node_helper', 'serialport'

Scheduled Pinned Locked Moved Development
23 Posts 2 Posters 5.6k Views 2 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.
  • N Offline
    nhpunch
    last edited by Jun 10, 2019, 6:52 AM

    const NodeHelper = require('node_helper')
    const Serialport = require("serialport");
    
    module.exports = NodeHelper.create({
    
      start: function () {
      },
    
      socketNotificationReceived: function (notification, payload) {
        const self = this
    
        if (notification === 'CONFIG') {
          self.config = payload
          
        var serialport = new Serialport('/dev/ttyACM0', {   
    		baudRate: 9600
    		});
    
        var led = 0;
    
          serialport.open(function () {
            console.log('connect...');
            serialport.on('data', function(data) { // 아두이노로부터 전달된 데이터
              console.log('data received: ' + data);
            });
    
            setInterval( function() { // 2초마다 아두이노에게 문자열을 전송하는 예
              led = !led;
              serialport.write(led==true ? "1" : "0", function(err, result){
                if(err){
                 console.log(err);
                }
              });
            }, 5000);
          });
        }
      }, 
    })
    

    this is node_helper

    Module.register('MMM-Serial-Notifications', {
      defaults: {
      },
    
      getScripts: function() {
    		return ["modules/MMM-Serial-Notifications/js/jquery.js"];
    	},
    	getStyles: function() {
    		return ["mm-MMM-Serial-Notifications-style.css"];
    	},
      start: function () {
        Log.info("Starting module: " + this.name);
        this.sendSocketNotification('CONFIG', this.config)
      },	
      socketNotificationReceived: function (notification, payload) {	
    	this.sendNotification(notification, payload)
    
      },
    
    	getDom: function() {
    		var wrapper = document.createElement("div");
    		var button = document.createElement("div");
    		var text = document.createElement("span");
    		var overlay = document.createElement("div");
    		var hidden = true;
    
    		overlay.className = "paint-it-black";
    		button.className = "hide-toggle";
    		button.appendChild(text);
    		text.innerHTML = "ON";
    
    		var led = 0;
        wrapper.appendChild(button);
    		wrapper.appendChild(overlay);$(button).on("click", function(){
    			if(hidden){
    				$(overlay).fadeIn(1000);
    				$(button).fadeTo(1000, 0.3);
    				$(text).html('OFF');
    				hidden = false;  
    				serialport.write('0');
    			}else{
    				$(overlay).fadeOut(1000);
    				$(button).fadeTo(1000, 1);  
    				$(text).html('ON');
    				hidden = true;
    				serialport.write('1');
    			}
    		});
    		return wrapper;
    	}
    });
    

    this is module

    I want to send ‘1’, ‘0’ to Arduino by pressing the button

    Now the node_helper is communicating in setInterval

    but, can not use serialport.write in a module

    How do I use the serialport in the module?

    Or, how do I create a button event in the node_helper?

    I am a very beginner. Please let me know. :disappointed_but_relieved_face:

    S 1 Reply Last reply Jun 10, 2019, 10:24 AM Reply Quote 0
    • S Away
      sdetweil @nhpunch
      last edited by Jun 10, 2019, 10:24 AM

      @nhpunch u cannot talk to hardware in module, you do that in node_helper.

      if you split the mirror into two parts server and client… node_helper runs on the server,
      module runs in the client browser…(even in the same machine)

      here is a package u can install to enable code to talk to serial port in node helper

      https://www.npmjs.com/package/serialport
      

      button would be in the module. you can use an html button, or more advanced div and span
      then when u get the button action in the module, you sendSocketNotification() a message to the node_helper… then node_helper then talks serial port to your arduino.

      Sam

      How to add modules

      learning how to use browser developers window for css changes

      N 1 Reply Last reply Jun 10, 2019, 11:53 AM Reply Quote 0
      • N Offline
        nhpunch @sdetweil
        last edited by Jun 10, 2019, 11:53 AM

        @sdetweil

        Thank you very much for your reply.

        You said that hardware and modules did not communicate, but communication is possible.

          serialport.open(function () {
                console.log('connect...');
                serialport.on('data', function(data) { // 아두이노로부터 전달된 데이터
                  console.log('data received: ' + data);
                });
        
                setInterval( function() { // 2초마다 아두이노에게 문자열을 전송하는 예
                  led = !led;
                  serialport.write(led==true ? "1" : "0", function(err, result){
                    if(err){
                     console.log(err);
                    }
                  });
                }, 5000);
              });
        
        

        In this code, it is possible to send a string to Arduino

        What I’m wondering is how to do sendSocketNotification ().

        I referenced https://github.com/MichMich/MagicMirror/tree/master/modules but did not understand

        Please let me know how to send it. Then I will be very thankful:folded_hands_light_skin_tone:

        S 1 Reply Last reply Jun 10, 2019, 12:49 PM Reply Quote 0
        • S Away
          sdetweil @nhpunch
          last edited by Jun 10, 2019, 12:49 PM

          @nhpunch there are two parts of a MagicMirror module

          1. node_helper, runs on server, can use require(…some other library…)
          2. module_name.js, runs in browser, manages data presented on screen, cannot use require(…)

          so, the two parts can talk to each other by sending socket messages, using the supplied function
          sendSocketNotification(some_id_string, some_data_packet)

          the receiver of the message gets called at the function socketNotificationReceived(some_id_string, some_data_packet)

          get my sample module that has both parts, and uses timer,

          https://github.com/sdetweil/SampleModule
          

          also see the developers doc here

          https://github.com/MichMich/MagicMirror/tree/master/modules
          

          my sample implements all the functions so you can see them…
          not all are needed… so u can comment out the ones u don’t need to use

          config for my sample, to add to config.js is

            {
              module: "SampleModule",
              position: "center",
              config: {
                message: "this is a test from config entry"
              }
            },
          

          Sam

          How to add modules

          learning how to use browser developers window for css changes

          N 1 Reply Last reply Jun 10, 2019, 1:47 PM Reply Quote 0
          • N Offline
            nhpunch @sdetweil
            last edited by Jun 10, 2019, 1:47 PM

            @sdetweil

            I was really grateful and ran the sample code.

            But I do not know what to send as a module to do what I want.

            Can you help me with my module and node_helper?

            S 1 Reply Last reply Jun 10, 2019, 1:50 PM Reply Quote 0
            • S Away
              sdetweil @nhpunch
              last edited by Jun 10, 2019, 1:50 PM

              @nhpunch sorry, i don’t understand

              Sam

              How to add modules

              learning how to use browser developers window for css changes

              S 1 Reply Last reply Jun 10, 2019, 1:52 PM Reply Quote 0
              • S Away
                sdetweil @sdetweil
                last edited by sdetweil Jun 10, 2019, 1:53 PM Jun 10, 2019, 1:52 PM

                @sdetweil you make up your own messages…

                so, you have a push button on the screen (done by module_name,js)… when push button,
                send button name and 1 to node helper… when it gets 1, it uses serial port to send to arduino.

                when u let up on button , you send 0…

                write out what you WANT the system to do… (design)

                then implement THAT design

                Sam

                How to add modules

                learning how to use browser developers window for css changes

                N 1 Reply Last reply Jun 10, 2019, 2:07 PM Reply Quote 0
                • N Offline
                  nhpunch @sdetweil
                  last edited by Jun 10, 2019, 2:07 PM

                  @sdetweil

                  I’m doing what you said, but I do not know how to send it to the node_helper when the button is pressed.

                  I’m so sorry.

                  S 1 Reply Last reply Jun 10, 2019, 3:55 PM Reply Quote 0
                  • S Away
                    sdetweil @nhpunch
                    last edited by sdetweil Jun 10, 2019, 4:24 PM Jun 10, 2019, 3:55 PM

                    @nhpunch this.sendSocketNotification(…,…)

                    from the doc

                    this.sendSocketNotification(notification, payload)

                    notification String - The notification identifier.
                    payload AnyType - Optional. A notification payload.

                    If you want to send a notification to the node_helper, use the sendSocketNotification(notification, payload). Only the node_helper of this module will receive the socket notification.

                    Example:

                    this.sendSocketNotification('SET_CONFIG', this.config);
                    

                    you’ll see in the sample that this is used twice… once by the module_name.js to send the config over to the node_helper and once by the node_helper to send a new text string to the module_name.js to display

                    its also used in your module, from the first post above

                      start: function () {
                        Log.info("Starting module: " + this.name);
                        this.sendSocketNotification('CONFIG', this.config)
                      },
                    

                    Sam

                    How to add modules

                    learning how to use browser developers window for css changes

                    1 Reply Last reply Reply Quote 0
                    • N Offline
                      nhpunch
                      last edited by Jun 10, 2019, 5:46 PM

                      @sdetweil

                      I did my best, but I did not succeed.

                      I’m a real idiot.

                      Can you take a look at my code?

                      S 1 Reply Last reply Jun 10, 2019, 5:49 PM Reply Quote 0
                      • 1
                      • 2
                      • 3
                      • 1 / 3
                      1 / 3
                      • First post
                        5/23
                        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