Skip to content

Instantly share code, notes, and snippets.

@tsu-nera
Last active July 25, 2017 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsu-nera/86761c607c33ec1fb2cedc074fbf286d to your computer and use it in GitHub Desktop.
Save tsu-nera/86761c607c33ec1fb2cedc074fbf286d to your computer and use it in GitHub Desktop.
mindstorms ev3 remote control with keyboard by using mqtt, watch this! https://youtu.be/kl_07BWGNWo
#!/usr/bin/env python3
import paho.mqtt.client as mqtt
from pynput.keyboard import Key, Listener
client = mqtt.Client()
client.connect("localhost",1883,60)
def on_press(key):
print("{} is pressed".format(key))
if key == Key.up:
client.publish("topic/motor/dt", str(-50) + "," + str(-50))
elif key == Key.down:
client.publish("topic/motor/dt", str(50) + "," + str(50))
elif key == Key.right:
client.publish("topic/motor/dt", str(-50) + "," + str(0))
elif key == Key.left:
client.publish("topic/motor/dt", str(0) + "," + str(-50))
def on_release(key):
print('{0} released'.format(key))
client.publish("topic/motor/dt", str(0) + "," + str(0))
if key == Key.esc:
client.disconnect();
return False
with Listener(
on_press = on_press,
on_release= on_release
) as listener:
listener.join()
# !/usr/bin/env python3
import paho.mqtt.client as mqtt
from ev3dev.auto import *
ma = Motor(OUTPUT_A)
md = Motor(OUTPUT_D)
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client.subscribe("topic/motor/dt")
def on_message(client, userdata, msg):
msg_str = msg.payload.decode("utf-8")
msg_array = msg_str.split(",")
ma.duty_cycle_sp = msg_array[0]
md.duty_cycle_sp = msg_array[1]
client = mqtt.Client()
client.connect("192.168.3.4" ,1883 ,60)
client.on_connect = on_connect
client.on_message = on_message
ma.run_direct()
md.run_direct()
ma.duty_cycle_sp = 0
md.duty_cycle_sp = 0
client.loop_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment