Untitled
unknown
plain_text
5 months ago
1.8 kB
1
Indexable
import RPi.GPIO as GPIO import time from pubnub.callbacks import SubscribeCallback from pubnub import Pubnub # Pin configurations LED_PIN = 7 SERVO_PIN = 8 # Setup GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(LED_PIN, GPIO.OUT) # Initialize PWM for Servo GPIO.setup(SERVO_PIN, GPIO.OUT) servo = GPIO.PWM(SERVO_PIN, 50) # Set frequency to 50 Hz servo.start(0) # Initialize servo position # PubNub configuration pubnub = Pubnub(publish_key='your_publish_key', subscribe_key='your_subscribe_key') class MySubscribeCallback(SubscribeCallback): def message(self, pubnub, message): command = message.message print(f"Received command: {command}") if command == "lightOn": GPIO.output(LED_PIN, GPIO.HIGH) # Turn LED on print("LED is ON") elif command == "lightOff": GPIO.output(LED_PIN, GPIO.LOW) # Turn LED off print("LED is OFF") elif command == "servoOpen": servo.ChangeDutyCycle(2) # Adjust for your servo time.sleep(1) # Allow time for movement servo.ChangeDutyCycle(0) # Stop sending signal print("Servo is OPEN") elif command == "servoClose": servo.ChangeDutyCycle(12) # Adjust for your servo time.sleep(1) # Allow time for movement servo.ChangeDutyCycle(0) # Stop sending signal print("Servo is CLOSED") else: print("Invalid command") pubnub.subscribe().channels('your_channel_name').execute() try: pubnub.add_listener(MySubscribeCallback()) while True: time.sleep(1) # Keep the main thread alive except KeyboardInterrupt: pass finally: GPIO.cleanup() # Clean up GPIO on exit servo.stop() # Stop the servo PWM
Editor is loading...
Leave a Comment