MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    1. Home
    2. nhpunch
    3. Posts
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.
    N
    Offline
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 12
    • Groups 0

    Posts

    Recent Best Controversial
    • RE: How to use the 'node_helper', 'serialport'

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

      posted in Development
      N
      nhpunch
    • How to use the 'node_helper', 'serialport'
      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:

      posted in Development
      N
      nhpunch
    • 1
    • 2
    • 2 / 2