from Adafruit_IO import Client, Feed, Data, RequestError ADAFRUIT_IO_USERNAME = 'gloowb' ADAFRUIT_IO_KEY = '' #Differs for different accounts aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) from time import sleep import RPi.GPIO as GPIO import wiringpi DIR = 11 # Direction GPIO Pin STEP = 13 # Step GPIO Pin CW = 1 # Clockwise Rotation CCW = 0 # Counterclockwise Rotation ENABLE = 15 # use 'GPIO naming' wiringpi.wiringPiSetupGpio() # set #12 to be a PWM output wiringpi.pinMode(12, wiringpi.GPIO.PWM_OUTPUT) # set the PWM mode to milliseconds stype wiringpi.pwmSetMode(wiringpi.GPIO.PWM_MODE_MS) # divide down clock wiringpi.pwmSetClock(192) wiringpi.pwmSetRange(2000) delay_period = 0.01 # for the stepper motor STEPS_PER_CM = 20000 delay = 0.000001 button = (1.6, 3.8, 4.44, 5.96, 0 ) # Distance to buttons in cm (0 means the button doesn't exist) fan_speed = 0 # Monitors the current fan speed GPIO.setmode(GPIO.BOARD) GPIO.setup(DIR, GPIO.OUT) GPIO.setup(STEP, GPIO.OUT) GPIO.setup(ENABLE, GPIO.OUT) GPIO.output(ENABLE, GPIO.HIGH) #disable stepper motor driver try: digital = aio.feeds('fan') print("Connected") except RequestError: feed = Feed(name="Fan") fan = aio.create_feed(feed) def press(dist): step_count = int(dist*STEPS_PER_CM) GPIO.output(ENABLE, GPIO.LOW) sleep(1) GPIO.output(DIR, CCW) print("Moving CW") for x in range(step_count): GPIO.output(STEP, GPIO.HIGH) sleep(delay) GPIO.output(STEP, GPIO.LOW) sleep(delay) GPIO.output(ENABLE, GPIO.HIGH) sleep(1) print("Pushing button") for pulse in range(150, 0, -1): wiringpi.pwmWrite(12, pulse) sleep(delay_period) sleep(2) for pulse in range(0, 150, 1): wiringpi.pwmWrite(12, pulse) sleep(delay_period) sleep(2) GPIO.output(ENABLE, GPIO.LOW) sleep(0.2) GPIO.output(DIR, CW) print("Moving CCW") for x in range(step_count): GPIO.output(STEP, GPIO.HIGH) sleep(delay) GPIO.output(STEP, GPIO.LOW) sleep(delay) GPIO.output(ENABLE, GPIO.HIGH) sleep(0.2) # GPIO.cleanup() while True: data = aio.receive(digital.key) speed = int(data.value) print(speed) if speed == fan_speed: print("The fan is already at that speed.") elif speed >= 0 and speed <= 4 and button[speed] != 0: fan_speed = speed print(button[speed]) print("Type is", type(button[speed])) press(button[speed]) else: print("Sorry, that fan speed doesn't exist. Please try again.")
PS9888 | Voice-controlled Fan Attachment
Proudly brought to you by The Fan Club