def connect(self, serial_port=None, speed=None): # Render the connect page if serial_port is None: error_message = cherrypy.session.get( 'error_message') #@UndefinedVariable cherrypy.session['error_message'] = None #@UndefinedVariable template = self.jinja2_env.get_template( 'web-ui-select-serial-port.html') return template.render(error_message=error_message) # Try to conect error_message = None # holds the error messages, and used as a flag too. serial_port = serial_port.strip() if serial_port: # != None try: logger.info("Trying to connect to %s", serial_port) proxy = ArduinoProxy(tty=serial_port, speed=int(speed)) logger.info("Connected OK") self.proxy = proxy raise cherrypy.HTTPRedirect( "/") # Connect OK -> redirect to '/' except cherrypy.HTTPRedirect, cherrypy_redirect: raise cherrypy_redirect except ArduinoProxy, ap_exception: error_message = str(ap_exception) logger.exception("Couldn't create instance of ArduinoProxy")
def main(): # pylint: disable=R0915 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Test autoconnect() #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ logging.basicConfig(level=logging.DEBUG) print "Test autoconnect()" proxy = ArduinoProxy() proxy.autoconnect() proxy.ping() proxy.close()
def main(): if len(sys.argv) == 1: tty_device = os.environ['TTY_DEVICE'] analog_port = os.environ['ANALOG_PORT'] proxy = ArduinoProxy(tty_device, 9600) sum = 0.0 for i in range(0, COUNT): value = proxy.analogRead(int(analog_port)) value = ((5.0 * value * 100.0) / 1024.0) sum = sum + value value = sum / float(COUNT) print "temp.value %.2f" % value else: if sys.argv[1] == "config": print "graph_title Temperature" print "graph_args --vertical-label Temperature" print "graph_category arduino" print "temp.label Temperature" print "temp.type GAUGE"
def main(): if len(sys.argv) == 1: tty_device = os.environ['TTY_DEVICE'] analog_port = os.environ['ANALOG_PORT'] proxy = ArduinoProxy(tty_device, 9600) sum = 0.0 for i in range(0, COUNT): value = proxy.analogRead(int(analog_port)) value = ((5.0 * value * 100.0)/1024.0) sum = sum + value value = sum/float(COUNT) print "temp.value %.2f" % value else: if sys.argv[1] == "config": print "graph_title Temperature" print "graph_args --vertical-label Temperature" print "graph_category arduino" print "temp.label Temperature" print "temp.type GAUGE"
def default_main(optparse_usage="usage: %prog [options] serial_device", add_options_callback=None, args_validator=default_args_validator): """ Utility method to help creation of programs around ArduinoProxy. This method configures logging and initial wait, and creates the ArduinoProxy instance. Parameters: - add_options_callback: callback method to let the user of 'main_utils()' add options. This method is called, and the 'parser' instance is passed as parameter. - args_validator: method that validates the args. Returns: - options, args, proxy """ parser = optparse.OptionParser(usage=optparse_usage) parser.add_option("--debug", action="store_true", dest="debug", default=False, help="Configure logging to show debug messages.") parser.add_option("--arduino-debug", action="store_true", dest="arduino_debug", default=False, help="Configure the proxy to debug all the comunication with Arduino (implies --info).") parser.add_option("--info", action="store_true", dest="info", default=False, help="Configure logging to show info messages.") parser.add_option("--initial-wait", action="store", dest="initial_wait", default=None, help="How many seconds wait before conect (workaround for auto-reset on connect).") parser.add_option("--dont-call-validate-connection", action="store_true", dest="dont_call_validate_connection", default=False, help="Don't call validateConnection() on startup (the default is " + \ "to call validateConnection() automatically at startup).") if not add_options_callback is None: add_options_callback(parser) (options, args) = parser.parse_args() if len(args) == 0: parser.error("must specify the serial device (like /dev/ttyACM0). " + \ "Serial devices that looks like Arduinos: %s." % ', '.join(glob.glob('/dev/ttyACM*'))) if args_validator: args_validator(parser, options, args) if options.debug: logging.basicConfig(level=logging.DEBUG) elif options.info or options.arduino_debug: logging.basicConfig(level=logging.INFO) else: logging.basicConfig(level=logging.ERROR) if options.initial_wait == 0: proxy = ArduinoProxy(args[0], 9600, wait_after_open=0, call_validate_connection=not(options.dont_call_validate_connection)) else: if options.initial_wait is None: logging.info("Waiting some seconds to let the Arduino reset...") proxy = ArduinoProxy(args[0], 9600, call_validate_connection=not(options.dont_call_validate_connection)) else: initial_wait = int(options.initial_wait) if initial_wait > 0: logging.info("Waiting %d seconds to let the Arduino reset...", initial_wait) proxy = ArduinoProxy(args[0], 9600, wait_after_open=initial_wait, call_validate_connection=not(options.dont_call_validate_connection)) if options.arduino_debug: proxy.enableDebug() return options, args, proxy
def setUp(self): # pylint: disable=C0103 self.proxy = ArduinoProxy.create_emulator()
def setUp(self): # pylint: disable=C0103 self.proxy = ArduinoProxy.create_emulator( initial_input_buffer_contents="** SOME TEXT **\n" * 5)
def default_main(optparse_usage="usage: %prog [options] serial_device", add_options_callback=None, args_validator=default_args_validator, connect_only_if_device_specified=False): """ Utility method to help creation of programs around ArduinoProxy. This method configures logging and initial wait, and creates the ArduinoProxy instance. Parameters: - add_options_callback: callback method to let the user of 'main_utils()' add options. This method is called, and the 'parser' instance is passed as parameter. - args_validator: method that validates the args. Returns: - options, args, proxy """ parser = optparse.OptionParser(usage=optparse_usage) parser.add_option("--debug", action="store_true", dest="debug", default=False, help="Configure logging to show debug messages.") parser.add_option( "--arduino-debug", action="store_true", dest="arduino_debug", default=False, help= "Configure the proxy to debug all the comunication with Arduino (implies --info)." ) parser.add_option("--info", action="store_true", dest="info", default=False, help="Configure logging to show info messages.") parser.add_option( "--initial-wait", action="store", dest="initial_wait", default=None, help= "How many seconds wait before conect (workaround for auto-reset on connect)." ) parser.add_option("--dont-call-validate-connection", action="store_true", dest="dont_call_validate_connection", default=False, help="Don't call validateConnection() on startup (the default is " + \ "to call validateConnection() automatically at startup).") if not add_options_callback is None: add_options_callback(parser) (options, args) = parser.parse_args() if len(args) == 0 and not connect_only_if_device_specified: parser.error("must specify the serial device (like /dev/ttyACM0). " + \ "Serial devices that looks like Arduinos: %s." % ', '.join(glob.glob('/dev/ttyACM*'))) if args_validator: args_validator(parser, options, args) if options.debug: logging.basicConfig(level=logging.DEBUG) elif options.info or options.arduino_debug: logging.basicConfig(level=logging.INFO) else: logging.basicConfig(level=logging.ERROR) if connect_only_if_device_specified and len(args) == 0: return options, args, None else: if options.initial_wait == 0: proxy = ArduinoProxy( args[0], 9600, wait_after_open=0, call_validate_connection=not ( options.dont_call_validate_connection)).connect() else: if options.initial_wait is None: logging.info( "Waiting some seconds to let the Arduino reset...") proxy = ArduinoProxy( args[0], 9600, call_validate_connection=not ( options.dont_call_validate_connection)).connect() else: initial_wait = int(options.initial_wait) if initial_wait > 0: logging.info( "Waiting %d seconds to let the Arduino reset...", initial_wait) proxy = ArduinoProxy( args[0], 9600, wait_after_open=initial_wait, call_validate_connection=not ( options.dont_call_validate_connection)).connect() if options.arduino_debug: proxy.enableDebug() return options, args, proxy
def connect_emulator(self): logger.info("Creating EMULATOR...") self.proxy = ArduinoProxy.create_emulator() raise cherrypy.HTTPRedirect("/") # Connect OK -> redirect to '/'