-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeedControl.py
More file actions
52 lines (43 loc) · 1.33 KB
/
speedControl.py
File metadata and controls
52 lines (43 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#Import essential modules
import paho.mqtt.client as mqtt
import RPi.GPIO as gpio
#Set up RPI GPIO pins
def gpioSetup():
#Set pin numbering to Broadcom scheme
gpio.setmode(gpio.BCM)
#Set PWM GPIOS for the motor controller
gpio.setup(6, gpio.OUT)
gpio.setup(12, gpio.OUT)
global mOne
mOne = gpio.PWM(6,1000)
global mTwo
mTwo = gpio.PWM(12,1000)
mOne.start(0)
mTwo.start(0)
#Execute when a connection has been established ot the MQTT server
def connectionStatus(client, userdata, flags, rc):
#Subscribe client to a topic
mqttClient.subscribe("rpi/speed")
#Execute when a message has been received from the MQTT server
def messageDecoder(client, userdata, msg):
#Decode message
message = msg.payload.decode(encoding='UTF-8')
msg = int(message)
print(msg)
mOne.ChangeDutyCycle(msg)
mTwo.ChangeDutyCycle(msg)
#Set up RPI GPIO pins
gpioSetup()
#set client name
clientName = "RPISpeed"
#Set MQTT server address
serverAddress = "169.254.213.44"
#Instantiate Eclipse Paho as mqttClient
mqttClient = mqtt.Client(clientName)
#Set calling functions to mqttCLient
mqttClient.on_connect = connectionStatus
mqttClient.on_message = messageDecoder
#Connect client to Server
mqttClient.connect(serverAddress)
#Monitor client activity forever
mqttClient.loop_forever()