-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
96 lines (79 loc) · 2.83 KB
/
main.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
################################
# CONNECTING TO MQTT #
################################
from mqttclient import MQTTClient
from time import sleep
import random
MAX_SPEED = 100
#IOT BROKER
BROKER = "broker.hivemq.com" #Alternatively, iot.eclipse.org
USER = None
PWD = None
print("Connecting to broker", BROKER, "...")
#CREATING the mqtt object and CONNECTING to the broker
mqtt = MQTTClient(BROKER, user=USER, password=PWD, ssl=False) #False for hivemq
topic = "charles/esp32/boserBeater"
mqtt.publish(topic, "BoserBeater is online :^)") #ESP32 IS PUBLISHING, CP RECEIVING
print("BoserBeater is online :^)")
currentSpeed = 0
def mqtt_callback(topic,msg):
global currentSpeed
message = msg.decode("utf-8") #msg comes in as byte-format, decode turns it directly into a string
currentSpeed = int(round(float(message)))
if currentSpeed>MAX_SPEED: #Capping it
currentSpeed = MAX_SPEED
mqtt.set_callback(mqtt_callback)
mqtt.subscribe("charles/pc/boserBeater") #SUBSCRIBE TO COMPUTER TOPIC
################################
# SETUP #
################################
#Connect DRV V_in from Vusb. This is running on 5V
from machine import PWM, Pin, Timer
from board import A20, A21, A6, A14, A15
from time import sleep, ticks_ms
from max6675 import MAX6675
################################
# THERMOCOUPLE #
################################
so = Pin(A14, mode=Pin.IN)
cs = Pin(A15, mode=Pin.OUT)
sck = Pin(A6, mode=Pin.OUT)
thermo = MAX6675(sck, cs, so)
################################
# DC MOTOR #
################################
p20 = Pin(A20, mode=Pin.OUT)
p21 = Pin(A21, mode=Pin.OUT)
f = 100000
HI = 100
LO = 100 #MOTOR STARTS OFF NOT RUNNING
xIN1 = PWM(p20, freq = f, duty = HI)
xIN2 = PWM(p21, freq = f, duty = LO)
################################
# MAIN PROGRAM #
################################
mqtt.publish(topic, \
"BoserBeater is now running." \
"\r\r[INSTRUCTIONS]" \
"\r -Input speed range: 0 - 100" \
"\r -Starting speed = 0. Input a speed from 0 to 100." \
"\r Speeds 100 or greater will result in max speed." \
"\r\r -Every 20 seconds, speed can be changed. " \
"\r -Time elapsed and current temperature will also be displayed." \
"\r\r -Input of '-1' will terminate the program!")
while True:
mqtt.check_msg()
if currentSpeed<0: #Terminate Program
xIN2.duty(100)
mqtt.publish(topic, "BoserBeater turning off.")
mqtt.disconnect()
break
LO = MAX_SPEED-currentSpeed #msg is speed
xIN2.duty(LO)
temp = thermo.read()
mqtt.publish(topic, \
"[STATUS]" \
"\r -Speed: " + str(currentSpeed) + "\r" \
" -Current temperature: " + str(temp) + " C." \
"\r -Time elapsed: " + str(int(ticks_ms()/1000)) + " s.")
sleep(20)