Advertisement
Hobe

WuDevice

Jan 18th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.06 KB | None | 0 0
  1. from twisted.internet import reactor
  2. from udpwkpf import WuClass, Device
  3. import sys
  4. from udpwkpf_io_interface import *
  5.  
  6. Button_Pin = 2
  7. Light_Actuator_Pin = 4
  8. Smoke_Sensor_Pin = 7
  9. R_LED_Pin = 3
  10. G_LED_Pin = 5
  11. B_LED_Pin = 6
  12. Magnetic_Pin = 8
  13. Slider_Pin = 14
  14. Light_Sensor_Pin = 15
  15.  
  16. class Button(WuClass):
  17.     def __init__(self):
  18.         WuClass.__init__(self)
  19.         self.loadClass('Button')
  20.         self.IO = pin_mode(Button_Pin, PIN_TYPE_DIGITAL, PIN_MODE_INPUT)
  21.         self.prev_value = -1
  22.  
  23.     def update(self,obj,pID=None,val=None):
  24.         try:
  25.             current_value = digital_read(self.IO)
  26.             if current_value != self.prev_value:
  27.                 self.prev_value = current_value
  28.                 print "Button value: %d" % current_value
  29.                 obj.setProperty(0, current_value)
  30.         except IOError:
  31.             print "Error"
  32.  
  33. class Light_Actuator(WuClass):
  34.     def __init__(self):
  35.         WuClass.__init__(self)
  36.         self.loadClass('Light_Actuator')
  37.         self.light_actuator_gpio = pin_mode(Light_Actuator_Pin, PIN_TYPE_DIGITAL, PIN_MODE_OUTPUT)
  38.  
  39.     def update(self,obj,pID=None,val=None):
  40.         try:
  41.             if pID == 0:
  42.                 if val == True:
  43.                     digital_write(self.light_actuator_gpio, 1)
  44.                     print "Light Actuator On"
  45.                 else:
  46.                     digital_write(self.light_actuator_gpio, 0)
  47.                     print "Light Actuator Off"
  48.         except IOError:
  49.             print ("Error")
  50.  
  51. class Smoke_Sensor(WuClass):
  52.     def __init__(self):
  53.         WuClass.__init__(self)
  54.         self.loadClass('Smoke_Sensor')
  55.         self.IO = pin_mode(Smoke_Sensor_Pin, PIN_TYPE_DIGITAL, PIN_MODE_INPUT)
  56.         self.prev_value = -1
  57.  
  58.     def update(self,obj,pID=None,val=None):
  59.         try:
  60.             current_value = digital_read(self.IO)
  61.             if current_value != self.prev_value:
  62.                 self.prev_value = current_value
  63.                 print "Smoke sensor value: %d" % current_value
  64.                 obj.setProperty(0, current_value)
  65.         except IOError:
  66.             print "Error"
  67.  
  68. class RGB_LED(WuClass):
  69.     def __init__(self):
  70.         WuClass.__init__(self)
  71.         self.loadClass('RGB_LED')
  72.         self.r_led_gpio = pin_mode(R_LED_Pin, PIN_TYPE_DIGITAL, PIN_MODE_OUTPUT)
  73.         self.g_led_gpio = pin_mode(G_LED_Pin, PIN_TYPE_DIGITAL, PIN_MODE_OUTPUT)
  74.         self.b_led_gpio = pin_mode(B_LED_Pin, PIN_TYPE_DIGITAL, PIN_MODE_OUTPUT)
  75.  
  76.     def update(self,obj,pID=None,val=None):
  77.         try:
  78.             if pID == 0:
  79.                 analog_write(self.r_led_gpio, val)
  80.                 print "set R:", val
  81.             elif pID == 1:
  82.                 analog_write(self.g_led_gpio, val)
  83.                 print "set G:", val
  84.             elif pID == 2:
  85.                 analog_write(self.b_led_gpio, val)
  86.                 print "set B:", val
  87.         except IOError:
  88.             print ("Error")
  89.  
  90. class Magnetic_Sensor(WuClass):
  91.     def __init__(self):
  92.         WuClass.__init__(self)
  93.         self.loadClass('Magnetic_Sensor')
  94.         self.IO = pin_mode(Magnetic_Pin, PIN_TYPE_DIGITAL, PIN_MODE_INPUT)
  95.  
  96.     def update(self,obj,pID=None,val=None):
  97.         try:
  98.             current_value = digital_read(self.IO)
  99.             print "Magnetic value: %d" % current_value
  100.             obj.setProperty(0, current_value)
  101.         except IOError:
  102.             print "Error"
  103.  
  104. class Slider(WuClass):
  105.     def __init__(self):
  106.         WuClass.__init__(self)
  107.         self.loadClass('Slider')
  108.         self.slider_aio = pin_mode(Slider_Pin, PIN_TYPE_ANALOG)
  109.         print "Slider init success"
  110.         self.prev_value = -1
  111.  
  112.     def update(self,obj,pID=None,val=None):
  113.         try:
  114.             current_value = analog_read(self.slider_aio)/4 #4 is divisor value which depends on the slider.
  115.             if current_value != self.prev_value:
  116.                 self.prev_value = current_value
  117.                 obj.setProperty(0, current_value)
  118.                 print "Slider analog pin: ", Slider_Pin, ", value: ", current_value
  119.         except IOError:
  120.             print ("Error")
  121.  
  122. class Light_Sensor(WuClass):
  123.     def __init__(self):
  124.         WuClass.__init__(self)
  125.         self.loadClass('Light_Sensor')
  126.         self.light_sensor_aio = pin_mode(Light_Sensor_Pin, PIN_TYPE_ANALOG)
  127.         self.prev_value = -1
  128.  
  129.     def update(self,obj,pID=None,val=None):
  130.         try:
  131.             current_value = analog_read(self.light_sensor_aio)/4 # 4 is divisor value which depends on the light sensor
  132.             if current_value != self.prev_value:
  133.                 self.prev_value = current_value
  134.                 obj.setProperty(0, current_value)
  135.                 print "Light sensor analog pin: ", Light_Sensor_Pin, ", value: ", current_value
  136.         except IOError:
  137.             print ("Error")
  138.  
  139. if __name__ == "__main__":
  140.     class MyDevice(Device):
  141.         def __init__(self,addr,localaddr):
  142.             Device.__init__(self,addr,localaddr)
  143.  
  144.         def init(self):
  145.             m = Button()
  146.             self.addClass(m,0)
  147.             self.addObject(m.ID)
  148.  
  149.             m2 = Light_Actuator()
  150.             self.addClass(m2,0)
  151.             self.addObject(m2.ID)
  152.  
  153.             m3 = Smoke_Sensor()
  154.             self.addClass(m3,0)
  155.             self.addObject(m3.ID)
  156.  
  157.             m4 = RGB_LED()
  158.             self.addClass(m4,0)
  159.             self.addObject(m4.ID)
  160.  
  161.             m5 = Magnetic_Sensor()
  162.             self.addClass(m5,0)
  163.             self.addObject(m5.ID)
  164.  
  165.             m6 = Slider()
  166.             self.addClass(m6,0)
  167.             self.addObject(m6.ID)
  168.  
  169.             m7 = Light_Sensor()
  170.             self.addClass(m7,0)
  171.             self.addObject(m7.ID)
  172.  
  173.     if len(sys.argv) <= 2:
  174.         print 'python %s <gip> <dip>:<port>' % sys.argv[0]
  175.         print '      <gip>: IP addrees of gateway'
  176.         print '      <dip>: IP address of Python device'
  177.         print '      <port>: An unique port number'
  178.         print ' ex. python %s 192.168.4.7 127.0.0.1:3000' % sys.argv[0]
  179.         sys.exit(-1)
  180.  
  181.     d = MyDevice(sys.argv[1],sys.argv[2])
  182.     reactor.run()
  183.     device_cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement