title: Pubnub Micro servo Control with Raspberry Pi
body: <p>I have been following this tutorial <a href="https://www.pubnub.com/blog/2015-03-17-building-a-model-smart-home-with-raspberry-pi/" rel="nofollow">Building a Model Smart Home with Raspberry PI</a> and I have noticed that the Micro Servo part is not there. Could someone help me with it? I have written some code so far, but it is disastrous.</p>
  
  <p>I only need to control one Micro servo, which I have connected to Power(3.3v), Ground, and (in BCM Mode) pin 17.</p>
  
  <p>PS: Most of the code I've used is in this <a href="https://github.com/pubnub/pi-house/" rel="nofollow">Gitub Repository</a>.</p>
  
  <p>I was thinking of doing it with Pigpio for more precise commands, but that would over-complicate it.</p>
  
  <p>This is the code I have so far:</p>
  
  <pre><code># Using PWM with RPi.GPIO
  
  import RPi.GPIO as GPIO
  import sys
  from pubnub import Pubnub
  
  GPIO.setmode(GPIO.BCM)
  DOOR = 17
  
  GPIO.setup(DOOR,GPIO.OUT)
  
  door = GPIO.PWM(DOOR, 50)
  door.start(0)
  
  
  
  # PubNub
  
  pubnub = Pubnub(publish_key='demo', subscribe_key='demo')
  
  channel = 'pi-house'
  
  def _callback(m, channel):
      print(m)
  
      dc = m['open']
  
      if m['item'] == 'true':
          door.ChangeDutyCycle(5)
  
      elif m['item'] == 'false':
          door.ChangeDutyCycle(7.5)
  
  def _error(m):
    print(m)
  
  pubnub.subscribe(channels='pi-house', callback=_callback, error=_error)
  </code></pre>