def __init__(self, com='/dev/ttyACM0', sunshader_pin=5, servo_range=(0, 180), sprinkler_pin=6): try: self.board = ArduinoDue(com) except Exception as e: self.board = None return # # # Setup the sunshader pin. # self.board.digital[sunshader_pin].mode = pyfirmata.SERVO self.sunshader_pin = sunshader_pin self.servo_range = servo_range self._angle = 0 # # Setup the sprinkler pin. # self.board.digital[sprinkler_pin].mode = pyfirmata.DIGITAL self.sprinkler_pin = sprinkler_pin # # Initialize to center (not to load the servo). # self.setAngle(90)
class Controller: def __init__(self): self.monitor_thread=None self.button = None self.a11 = None self.board = ArduinoDue('/dev/ttyS0') def mqtt_monitor(self): """ monitor sensor data """ global cLient if self.monitor_thread==None: cLient.connect("gpssensor.ddns.net", 1883, 60) self.monitor_thread = MonitorThread() self.monitor_thread.start() else: print("MQTT client already exist! Don't start again") def mqtt_send(self,payload): #client = new mqtt.MqttClient("tcp://gpssensor.ddns.net:1883", "LASS-Wuuong"); #client.connect(); #MqttMessage message = new MqttMessage(); #message.setPayload("A single message".getBytes()); #client.publish("LASS/Test/LinkItSmart7688-Test", message); #client.disconnect(); publish.single("LASS/Test/LinkItSmart7688-Test", payload, hostname="gpssensor.ddns.net") def board_led_blink(self): self.board.digital[13].write(1) sleep(0.5) self.board.digital[13].write(0) sleep(0.5) def board_d_reporting(self): iterator = util.Iterator(self.board) iterator.start() sleep(1) self.button = self.board.get_pin('d:4:i') sleep(1) self.button.enable_reporting() sleep(1) def board_a_reporting(self): it = util.Iterator(self.board) it.start() sleep(1) self.button = self.board.get_pin('a:0:i') self.a11 = self.board.get_pin('a:11:i') sleep(1) self.button.enable_reporting() self.a11.enable_reporting() sleep(1)
def connect(self, port: str, board: str="uno") -> None: """ Initialize a connection with the arduino. Parameters ---------- port : str Computer serial port to which the arduino is connected board : str, optional | The type of the arduino that is being used: | ``uno``: `Arduino Uno <https://store.arduino.cc/products/arduino-uno-rev3>`_ | ``mega``: `Arduino Mega <https://store.arduino.cc/products/arduino-mega-2560-rev3>`_ | ``due``: `Arduino Due <https://store.arduino.cc/products/arduino-due>`_ | ``nano``: `Arduino Nano <https://store.arduino.cc/products/arduino-nano>`_ """ self.port = port if board == "uno": self.board = Arduino(self.port) elif board == "mega": self.board = ArduinoMega(self.port) elif board == "due": self.board = ArduinoDue(self.port) elif board == "nano": self.board = ArduinoNano(self.port) else: raise UnknownBoardException("Unknown board " + board) self.it = util.Iterator(self.board) self.it.start()
from pyfirmata import ArduinoDue from pyfirmata import util board = ArduinoDue('COM3') LEDS = [ board.get_pin('d:3:o'), board.get_pin('d:5:o'), board.get_pin('d:6:o'), board.get_pin('d:9:o') ] SW = board.get_pin('d:10:i') it = util.Iterator(board) it.start() def encode(num): if num < 0: num = 0 elif num >= 16: num = 15 str = '{0:04b}'.format(num) for i, value in enumerate(str): LEDS[i].write(int(value)) return str def read_sw():
class ArduinoAPI(object): """Control the camera sunshader, water sprinkler and more. Args: com (str, optional): Com port of the arduino. sunshader_pin (int, optional): Pin number of the sunshader servo. servo_range (tuple, optional): (min, max) angles of the sunshader servo. sprinkler_ping (int, optional): Pin number of the sprinkler relay. """ def __init__(self, com='/dev/ttyACM0', sunshader_pin=5, servo_range=(0, 180), sprinkler_pin=6): try: self.board = ArduinoDue(com) except Exception as e: self.board = None return # # # Setup the sunshader pin. # self.board.digital[sunshader_pin].mode = pyfirmata.SERVO self.sunshader_pin = sunshader_pin self.servo_range = servo_range self._angle = 0 # # Setup the sprinkler pin. # self.board.digital[sprinkler_pin].mode = pyfirmata.DIGITAL self.sprinkler_pin = sprinkler_pin # # Initialize to center (not to load the servo). # self.setAngle(90) def __del__(self): if self.board is not None: self.board.exit() def setAngle(self, angle): """Set the angle of the sunshader. Args: angle (int): Requested angle of the sunshader. """ if self.board is None: raise Exception('No Arduino board available.') # # Round the angle # angle = int(angle + 0.5) if angle < self.servo_range[0] or angle > self.servo_range[1]: raise Exception('Angle out of range: angle={} range={}.'.format( angle, self.servo_range)) self._angle = angle self.board.digital[self.sunshader_pin].write(angle) def getAngle(self): """Get the last set angle of the sunshader. Returns: The last set angle of the sunshader. """ return self._angle def setSprinkler(self, state): """Set the state of the sprinkler. Args: state (bool): True to turn on the sprinkler, otherwise turn off. """ self.board.digital[self.sprinkler_pin].write(1 if state else 0)
def __init__(self): self.monitor_thread=None self.button = None self.a11 = None self.board = ArduinoDue('/dev/ttyS0')