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.

    skywriter - python to javascript

    Scheduled Pinned Locked Moved Development
    18 Posts 3 Posters 14.7k Views 3 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.
    • K Offline
      kclemen
      last edited by kclemen

      Just bought a skywriter to control my mirror based on swipes and presses. (https://shop.pimoroni.com/products/skywriter-hat)

      It comes with a python library: https://github.com/pimoroni/skywriter-hat

      What I would like to achieve is that a certain div comes to the page when I make a “up” gesture. Or when I swipe left, that the entire page is switched to another profile.

      What is the best starting point to control javascript in a webpage thru python?

      J 1 Reply Last reply Reply Quote 0
      • J Offline
        Jopyth Moderator @kclemen
        last edited by

        @kclemen If you want an example of how to put together python and a module, you can have a look at this. It uses a python backend for face recognition. I think you could use a similar approach to “listen” for flicks.

        Helpful sticky: How to troubleshoot

        1 Reply Last reply Reply Quote 1
        • K Offline
          kclemen
          last edited by

          Thanks for your reply. First glance at the link, seems to be rather difficult to implement what I want.

          Following code can be generated in py to link an action to a certain “gesture”:
          if(start == “north” and finish == “south”):
          print “Volume: down”
          system(“irsend SEND_ONCE TV KEY_VOLUMEDOWN”)
          elif(start == “south” and finish == “north”):
          print “Volume: up”
          system(“irsend SEND_ONCE TV KEY_VOLUMEUP”)

          I would like to slde in a certain dev when I wave from “north” to “south”… Searches on the net results that py and javascript do not work ery well together?

          J 1 Reply Last reply Reply Quote 0
          • J Offline
            Jopyth Moderator @kclemen
            last edited by Jopyth

            @kclemen Well, you can try googling for node.js (which is programmed in javascript) and python, and the answer will be quite different.

            If you want to program this module, you will need (at least) 3 components (I will demonstrate how it works in the facial recognition example):

            • the module in the browser:
              this will listen for events send from node_helper.js, for example see this function. In the example, it broadcasts this event to all modules (here), and then catches that notification again (here) to show all modules with a certain class

            • the node_helper.js:
              this is what will be calling your python code in the backend, for example in this line the python module is started, and then listens for events here

            • your python code:
              not much here you need, most of the code would look like your example, except the communication to node from this line

            Hope this helps to get you started.

            Helpful sticky: How to troubleshoot

            K 1 Reply Last reply Reply Quote 3
            • K Offline
              kclemen @Jopyth
              last edited by

              @Jopyth Thanks for your wonderful reply. Will take a look at it as soon the skywriter is delivered.

              1 Reply Last reply Reply Quote 0
              • K Offline
                kclemen
                last edited by

                ok, first steps are being taken… https://github.com/Kclemen/MMskywriter

                1 Reply Last reply Reply Quote 1
                • K Offline
                  kclemen
                  last edited by kclemen

                  fixed it…

                  K 1 Reply Last reply Reply Quote 0
                  • K Offline
                    kclemen @kclemen
                    last edited by

                    I’ve got following error:

                    SyntaxError: Unexpected token g in JSON at position 0
                    at PythonShell.asJson (/home/pi/MagicMirror/modules/MMM-skywriter/node_modules/python-shell/index.js:142:21)
                    at /home/pi/MagicMirror/modules/MMM-skywriter/node_modules/python-shell/index.js:232:35
                    at Array.forEach (native)
                    at PythonShell.receive (/home/pi/MagicMirror/modules/MMM-skywriter/node_modules/python-shell/index.js:231:11)
                    at emitOne (events.js:96:13)
                    at Socket.emit (events.js:188:7)
                    at readableAddChunk (_stream_readable.js:176:18)
                    at Socket.Readable.push (_stream_readable.js:134:10)
                    at Pipe.onread (net.js:543:20)

                    Here is the script:
                    `#!/usr/bin/env python
                    import sys
                    import json
                    import time
                    import signal
                    import skywriter

                    some_value = 5000

                    last_airwheel = 0
                    delay = 5000

                    def to_node(type, message):
                    # convert to json and print (node helper will read from stdout)
                    try:
                    print(json.dumps({type: message}))
                    except Exception:
                    pass
                    # stdout has to be flushed manually to prevent delays in the node helper communication
                    sys.stdout.flush()

                    to_node(“status”, ‘Skywriter started…’)

                    @skywriter.flick()
                    def flick(start,finish):

                    #slide music player div OFF the screen
                    if(start == "north" and finish == "south"):
                    	print "gesture is north to south"
                    	to_node("gesture", "down")
                    
                    #slide music player div ONTO the screen
                    elif(start == "south" and finish == "north"):
                    	print "gesture is south to north"
                    	to_node("gesture", "up")
                    
                    #Next track
                    elif(start == "west" and finish == "east"):
                    	print "Gesture is west to east"
                    	to_node("gesture", "next")
                    
                    #previous track
                    elif(start == "east" and finish == "west"):
                    	print "Gesture is east to west"
                    	to_node("gesture", "previous")
                    
                    #else:
                    	# print "Invalid"
                    

                    @skywriter.airwheel()
                    def spinny(delta):
                    global some_value
                    global last_airwheel
                    global delay
                    some_value += delta
                    if some_value < 0:
                    some_value = 0
                    if some_value > 10000:
                    some_value = 10000
                    now = int(round(time.time() * 1000))
                    if(now - last_airwheel > delay):
                    print(“TV & Digibox: power”)
                    # system(“irsend SEND_ONCE TV KEY_POWER”)
                    # system(“irsend SEND_ONCE Digibox KEY_POWER”)
                    last_airwheel = now

                    signal.pause()`

                    I do not really understand what is wrong? Anybody an idea?

                    J 1 Reply Last reply Reply Quote 0
                    • J Offline
                      Jopyth Moderator @kclemen
                      last edited by

                      @kclemen Please use three backticks (```) for markdown if you paste your code, instead of one (`). Otherwise it will not recognize it as multi line code.

                      The problem are your regular print statements (e.g. print "Gesture is west to east"), these are also read by node, and they are not in the JSON format. You can comment these out, or maybe write them to a log file instead, if you want them to debug.

                      Helpful sticky: How to troubleshoot

                      1 Reply Last reply Reply Quote 0
                      • K Offline
                        kclemen
                        last edited by

                        Thanks for the input. Indeed this was the mistake.

                        seems I’m missing still something. Now I get following error:

                        TypeError: parser.close is not a function
                        at SAXStream.openf1 (/home/pi/MagicMirror/node_modules/feedme/lib/xmlfeedparser.js:107:14)
                        at emitOne (events.js:96:13)
                        at SAXStream.emit (events.js:188:7)
                        at Object.me._parser.(anonymous function) [as onopentag] (/home/pi/MagicMirror/node_modules/sax/lib/sax.js:258:17)
                        at emit (/home/pi/MagicMirror/node_modules/sax/lib/sax.js:640:35)
                        at emitNode (/home/pi/MagicMirror/node_modules/sax/lib/sax.js:645:5)
                        at openTag (/home/pi/MagicMirror/node_modules/sax/lib/sax.js:841:5)
                        at Object.write (/home/pi/MagicMirror/node_modules/sax/lib/sax.js:1404:13)
                        at SAXStream.write (/home/pi/MagicMirror/node_modules/sax/lib/sax.js:239:18)
                        at yoshi.write (/home/pi/MagicMirror/node_modules/feedme/lib/feedme.js:41:14)

                        1 Reply Last reply Reply Quote 0
                        • K Offline
                          kclemen
                          last edited by

                          Ok, the script is performing without any errors at the moment.

                          Would somebody be so kind to check why console.log(“payload.action is up”); is not executed in MMM-skywriter.js?

                          I only get the message from node-helper.js : console.log(“[” + self.name + "] " + message.gesture);

                          Files can be found here: https://github.com/Kclemen/MMM-skywriter

                          1 Reply Last reply Reply Quote 0
                          • K Offline
                            kclemen
                            last edited by kclemen

                            Node helper:
                            self.sendSocketNotification('gesture', message.gesture);

                            script:

                            Module.register("MMM-skywriter",{
                            	
                            	//	gesture_up: 0,
                            	//	gesture_right: 0,	
                            
                            	// Override socket notification handler.
                            	socketNotificationReceived: function(notification, payload) {
                            			console.log ("socketnotificationreceived.")
                            		if (payload === "up"){
                            			console.log("test - up");
                            		}
                            		else if (payload === "left"){
                            			console.log("test - left");
                            		}
                            		else if (payload === "down") {
                            			console.log("test - down");
                            		}
                            } ```
                            
                            Why is there no ccommunication between the helper and the script?	}
                            J 1 Reply Last reply Reply Quote -1
                            • J Offline
                              Jopyth Moderator @kclemen
                              last edited by Jopyth

                              @kclemen Are you searching for the output in your browser console? The console.log("...") command from node_helper.js is shown on the console, however console.log("...") commands of your module will appear in your developer tools console in the browser (e.g. electron).

                              Helpful sticky: How to troubleshoot

                              K 1 Reply Last reply Reply Quote 1
                              • K Offline
                                kclemen @Jopyth
                                last edited by kclemen

                                @Jopyth said in skywriter - python to javascript:

                                developer tools console in the browser (e.g. electron).

                                Indeed, I want to see the output to verify the communication works.
                                Could you elaborate some more about the developer tools console?

                                ctrl + shift + i does the trick :)

                                @Jopyth you are a HERO. Last 2 days working on it, antd the trick was that simple!

                                J 1 Reply Last reply Reply Quote 0
                                • J Offline
                                  Jopyth Moderator @kclemen
                                  last edited by

                                  @kclemen I was just pointing you in the right direction sometimes. ;) Best of luck with finishing the rest of the module.

                                  Helpful sticky: How to troubleshoot

                                  1 Reply Last reply Reply Quote 0
                                  • K Offline
                                    kclemen
                                    last edited by

                                    ok, for now the last question ;):

                                    Uncaught reference error: gesture_up is not defined. I understand what this means, but I can’t figure out why it says that :s

                                    	
                                    	// Default module config.
                                    	defaults: {
                                    		gesture_up: 0,
                                    		gesture_right: 0,
                                    		
                                    	},
                                    
                                    	// Override socket notification handler.
                                    	socketNotificationReceived: function(notification, payload) {
                                    		if (notification === "gesture_observed"){
                                    			const self = this;
                                    			
                                    			console.log ("socketnotificationreceived.");
                                    		
                                    			//self.sendNotification(notification, payload);
                                    			
                                    			if (payload === "up"){
                                    				
                                    				if (gesture_up == 0) {
                                    					
                                    					if (gesture_up == 0){
                                    						gesture_up = gesture_up+1;
                                    						
                                    						console.log("gesture_up is nu 1");
                                    
                                    1 Reply Last reply Reply Quote 0
                                    • yawnsY Offline
                                      yawns Moderator
                                      last edited by

                                      Try this.config.gesture_up instead

                                      1 Reply Last reply Reply Quote 1
                                      • K Offline
                                        kclemen
                                        last edited by

                                        This helps indeed for the error, thanks. But how to use it like I want? I mean, how can you do calculations within following up socketnotifications? It looks like with every gesture the this.config;gesture_up starts with the value 0.

                                        1 Reply Last reply Reply Quote 0

                                        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
                                        • 1 / 1
                                        • First post
                                          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