Advertisement
Guest User

For Josh II

a guest
Feb 23rd, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. # MQTT Publish Demo
  2. # Send joystick instructions to MQTT topic
  3.  
  4. #import necessary libraries
  5. import paho.mqtt.publish as publish
  6. from sense_hat import SenseHat, ACTION_PRESSED, ACTION_HELD, ACTION_RELEASED
  7. from time import sleep
  8.  
  9. #define instance of Sense Hat
  10. sense = SenseHat()
  11.  
  12.  
  13. #define what to do when joystick pressed in different directions
  14. #each function sends the name of the direction to the test channel on the hivemq broker
  15.  
  16. def pushed_up(event):
  17. if event.action != ACTION_RELEASED:
  18. publish.single("INTD320/test", "up", hostname="broker.hivemq.com")
  19.  
  20. def pushed_down(event):
  21. if event.action != ACTION_RELEASED:
  22. publish.single("INTD320/test", "down", hostname="broker.hivemq.com")
  23.  
  24. def pushed_left(event):
  25. if event.action != ACTION_RELEASED:
  26. publish.single("INTD320/test", "left", hostname="broker.hivemq.com")
  27.  
  28. def pushed_right(event):
  29. if event.action != ACTION_RELEASED:
  30. publish.single("INTD320/test", "right", hostname="broker.hivemq.com")
  31.  
  32.  
  33. #call the direction functions when the joystick is pressed
  34. sense.stick.direction_up = pushed_up
  35. sense.stick.direction_down = pushed_down
  36. sense.stick.direction_left = pushed_left
  37. sense.stick.direction_right = pushed_right
  38. pause()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement