1

i want to compare string published over pubnub and the other string so that i can accordingly put gpio pins high or low in raspberry pi3. if this "result.message" is not string then how do i convert this to string type. the message received by the python program over pubnub is stored in "message" which is object type. considering that the message published over pubnub is string type. the message printed on the terminal is perfectly fine but the comparing code is not working( if result.message is "stop":) here is python program

    #!/usr/bin/python
    from pubnub.pnconfiguration import PNConfiguration
    from pubnub.pubnub import PubNub
    from pubnub.callbacks import SubscribeCallback
    from pubnub.enums import PNOperationType, PNStatusCategory
    from pubnub.pubnub import PubNub, SubscribeListener
    import time
    import RPi.GPIO as GPIO
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(12,GPIO.OUT)
    GPIO.setup(16,GPIO.OUT)
    GPIO.setup(20,GPIO.OUT)
    GPIO.setup(21,GPIO.OUT)


    pnconfig = PNConfiguration()
    pnconfig.subscribe_key = "sub-c-fcf7d96e-501b-11e7-abef-0619f8945a4f"
    pnconfig.publish_key = "pub-c-7bf2b0e2-d63a-48eb-8d10-55afcb0ef5c9"
    pnconfig.ssl = True

    pubnub = PubNub(pnconfig)
    my_listener = SubscribeListener()
    pubnub.add_listener(my_listener)

   class MySubscribeCallback(SubscribeCallback):
        def status(self, pubnub, status):
            pass
           if status.operation == PNOperationType.PNSubscribeOperation \
            or status.operation == PNOperationType.PNUnsubscribeOperation:
               if status.category == PNStatusCategory.PNConnectedCategory:
               pass

              elif status.category ==PNStatusCategory.PNReconnectedCategory:
              pass
              elif status.category==PNStatusCategory.PNDisconnectedCategory:
              pass
              elif status.categoryPNStatusCategory.PNUnexpectedDisconnectCategory:
              pass

              elif status.category == PNStatusCategory.PNAccessDeniedCategory:
            pass

              else:
              pass

              elif status.operation == PNOperationType.PNSubscribeOperation:
                   if status.is_error():
                   pass

                   else:
                   pass

              else:
               pass


def presence(self, pubnub, presence):
    pass  # handle incoming presence data

def message(self, pubnub, message):
    result = my_listener.wait_for_message_on('awesomeChannel')
    name=result.message
    if result.message is "stop":
       GPIO.output(12,GPIO.LOW)
       GPIO.output(16,GPIO.LOW)
       GPIO.output(20,GPIO.LOW)
       GPIO.output(21,GPIO.LOW)
    elif result.message is "right":
       GPIO.output(12,GPIO.LOW)
       GPIO.output(16,GPIO.LOW)
       GPIO.output(20,GPIO.HIGH)
       GPIO.output(21,GPIO.LOW)
       TIME.sleep(4)
       GPIO.output(12,GPIO.HIGH)
       GPIO.output(16,GPIO.LOW)
       GPIO.output(20,GPIO.HIGH)
       GPIO.output(21,GPIO.LOW)
    elif result.message is "left":
       GPIO.output(12,GPIO.HIGH)
       GPIO.output(16,GPIO.LOW)
       GPIO.output(20,GPIO.LOW)
       GPIO.output(21,GPIO.LOW)
       TIME.sleep(4)
       GPIO.output(12,GPIO.HIGH)
       GPIO.output(16,GPIO.LOW)
       GPIO.output(20,GPIO.HIGH)
       GPIO.output(21,GPIO.LOW)
    elif result.message is "forward":
       GPIO.output(12,GPIO.HIGH)
       GPIO.output(16,GPIO.LOW)
       GPIO.output(20,GPIO.HIGH)
       GPIO.output(21,GPIO.LOW)
    elif result.message is "backward":
       GPIO.output(12,GPIO.LOW)
       GPIO.output(16,GPIO.HIGH)
       GPIO.output(20,GPIO.LOW)
       GPIO.output(21,GPIO.HIGH)
    print(result.message)   

    pubnub.add_listener(MySubscribeCallback())
    pubnub.subscribe().channels('awesomeChannel').execute()

0 Answers0