Package pymata_aio :: Module pymata_iot :: Class PymataIOT
[hide private]
[frames] | no frames]

Class PymataIOT

source code

autobahn.asyncio.websocket.WebSocketServerProtocol --+
                                                     |
                                                    PymataIOT


This class implements the PyMata Websocket interface. JSON command messages are received via Websocket connection,
decoded and mapped to an associated PyMata method to be executed
operation.

The methods below are not intended to be called directly. The JSON message format is documented as part of the
method description. All JSON reply messages (to be handled by the client)  will also be documented for
within the description.

usage: pymata_iot.py [-h] [-host HOSTNAME] [-port PORT] [-wait WAIT]
                 [-comport COM] [-sleep SLEEP]

    optional arguments:
      -h, --help      show this help message and exit
      -host HOSTNAME  Server name or IP address
      -port PORT      Server port number
      -wait WAIT      Arduino wait time
      -comport COM    Arduino COM port
      -sleep SLEEP    sleep tune in ms.

Instance Methods [hide private]
 
__init__(self)
This is the "constructor" for PymataIOT.
source code
 
analog_read(self, command)
This method reads and returns the last reported value for an analog pin.
source code
 
analog_write(self, command)
This method writes a value to an analog pin.
source code
 
digital_read(self, command)
This method reads and returns the last reported value for a digital pin.
source code
 
digital_write(self, command)
This method writes a zero or one to a digital pin.
source code
 
disable_analog_reporting(self, command)
Disable Firmata reporting for an analog pin.
source code
 
disable_digital_reporting(self, command)
Disable Firmata reporting for a digital pin.
source code
 
enable_analog_reporting(self, command)
Enable Firmata reporting for an analog pin.
source code
 
enable_digital_reporting(self, command)
Enable Firmata reporting for a digital pin.
source code
 
encoder_config(self, command)
Configure 2 pins for FirmataPlus encoder operation.
source code
 
encoder_read(self, command)
This is a polling method to read the last cached FirmataPlus encoder value.
source code
 
get_analog_latch_data(self, command)
This method retrieves a latch table entry for an analog pin.
source code
 
get_analog_map(self)
This method retrieves the Firmata analog map.
source code
 
get_capability_report(self)
This method retrieves the Firmata capability report.
source code
 
get_digital_latch_data(self, command)
This method retrieves a latch table entry for a digital pin.
source code
 
get_firmware_version(self)
This method retrieves the Firmata firmware version.
source code
 
get_pinstate_report(self, command)
This method retrieves a Firmata pin_state report for a pin..
source code
 
get_protocol_version(self)
This method retrieves the Firmata protocol version.
source code
 
get_pymata_version(self)
This method retrieves the PyMata release version number.
source code
 
i2c_config(self, command)
This method initializes the I2c and sets the optional read delay (in microseconds).
source code
 
i2c_read_data(self, command)
This method retrieves the last value read for an i2c device identified by address.
source code
 
i2c_read_request(self, command)
This method sends an I2C read request to Firmata.
source code
 
i2c_write_request(self, command)
This method performs an I2C write at a given I2C address,
source code
 
play_tone(self, command)
This method controls a piezo device to play a tone.
source code
 
set_analog_latch(self, command)
This method sets the an analog latch for a given analog pin, providing the threshold type, and latching threshold.
source code
 
set_digital_latch(self, command)
This method sets the a digital latch for a given digital pin, the threshold type, and latching threshold.
source code
 
set_pin_mode(self, command)
This method sets the pin mode for the selected pin.
source code
 
set_sampling_interval(self, command)
This method sets the Firmata sampling interval in ms.
source code
 
sonar_config(self, command)
This method configures 2 pins to support HC-SR04 Ping devices.
source code
 
sonar_read(self, command)
This method retrieves the last sonar data value that was cached.
source code
 
servo_config(self, command)
This method configures a pin for servo operation.
source code
 
stepper_config(self, command)
This method configures 4 pins for stepper motor operation.
source code
 
stepper_step(self, command)
This method activates a stepper motor motion.
source code
 
onClose(self, was_clean, code, reason)
Websocket management message.
source code
 
onConnect(self, request)
WebSocket management method.
source code
 
onMessage(self, payload, is_binary)
Websocket management method.
source code
 
onOpen(self)
WebSocket management method.
source code
 
analog_callback(self, data)
This method handles the analog message received from pymata_core
source code
 
analog_latch_callback(self, data)
This method handles analog_latch data received from pymata_core
source code
 
digital_callback(self, data)
This method handles the digital message received from pymata_core
source code
 
digital_latch_callback(self, data)
This method handles the digital latch data message received from pymata_core
source code
 
encoder_callback(self, data)
This method handles the encoder data message received from pymata_core
source code
 
i2c_read_request_callback(self, data)
This method handles the i2c read data message received from pymata_core.
source code
 
i2c_read_data_callback(self, data)
This method handles the i2c cached read data received from pymata_core.
source code
 
sonar_callback(self, data)
This method handles sonar data received from pymata_core.
source code
Class Variables [hide private]
  parser = argparse.ArgumentParser()
  args = parser.parse_args()
  ip_addr = args.hostname
  ip_port = args.port
  comport = args.com
  core = PymataCore(int(args.wait), float(args.sleep), comport)
Method Details [hide private]

__init__(self)
(Constructor)

source code 

This is the "constructor" for PymataIOT. It sets up a translation dictionary using incoming JSON commands and maps them to command methods.

Returns:
No Return.

analog_read(self, command)

source code 

This method reads and returns the last reported value for an analog pin. Normally not used since analog pin updates will be provided automatically as they occur with the analog_message_reply being sent to the client after set_pin_mode is called. (see enable_analog_reporting for message format).

Parameters:
  • command - {"method": "analog_read", "params": [ANALOG_PIN]}
Returns:
{"method": "analog_read_reply", "params": [PIN, ANALOG_DATA_VALUE]}
Decorators:
  • @asyncio.coroutine

analog_write(self, command)

source code 

This method writes a value to an analog pin.

It is used to set the output of a PWM pin or the angle of a Servo.

Parameters:
  • command - {"method": "analog_write", "params": [PIN, WRITE_VALUE]}
Returns:
No return message.
Decorators:
  • @asyncio.coroutine

digital_read(self, command)

source code 

This method reads and returns the last reported value for a digital pin. Normally not used since digital pin updates will be provided automatically as they occur with the digital_message_reply being sent to the client after set_pin_mode is called.. (see enable_digital_reporting for message format)

Parameters:
  • command - {"method": "digital_read", "params": [PIN]}
Returns:
{"method": "digital_read_reply", "params": [PIN, DIGITAL_DATA_VALUE]}
Decorators:
  • @asyncio.coroutine

digital_write(self, command)

source code 

This method writes a zero or one to a digital pin.

Parameters:
  • command - {"method": "digital_write", "params": [PIN, DIGITAL_DATA_VALUE]}
Returns:
No return message..
Decorators:
  • @asyncio.coroutine

disable_analog_reporting(self, command)

source code 

Disable Firmata reporting for an analog pin.

Parameters:
  • command - {"method": "disable_analog_reporting", "params": [PIN]}
Returns:
No return message..
Decorators:
  • @asyncio.coroutine

disable_digital_reporting(self, command)

source code 

Disable Firmata reporting for a digital pin.

Parameters:
  • command - {"method": "disable_digital_reporting", "params": [PIN]}
Returns:
No return message.
Decorators:
  • @asyncio.coroutine

enable_analog_reporting(self, command)

source code 

Enable Firmata reporting for an analog pin.

Parameters:
  • command - {"method": "enable_analog_reporting", "params": [PIN]}
Returns:
{"method": "analog_message_reply", "params": [PIN, ANALOG_DATA_VALUE]}
Decorators:
  • @asyncio.coroutine

enable_digital_reporting(self, command)

source code 

Enable Firmata reporting for a digital pin.

Parameters:
  • command - {"method": "enable_digital_reporting", "params": [PIN]}
Returns:
{"method": "digital_message_reply", "params": [PIN, DIGITAL_DATA_VALUE]}
Decorators:
  • @asyncio.coroutine

encoder_config(self, command)

source code 

Configure 2 pins for FirmataPlus encoder operation.

Parameters:
  • command - {"method": "encoder_config", "params": [PIN_A, PIN_B]}
Returns:
{"method": "encoder_data_reply", "params": [ENCODER_DATA]}
Decorators:
  • @asyncio.coroutine

encoder_read(self, command)

source code 

This is a polling method to read the last cached FirmataPlus encoder value. Normally not used. See encoder config for the asynchronous report message format.

Parameters:
  • command - {"method": "encoder_read", "params": [PIN_A]}
Returns:
{"method": "encoder_read_reply", "params": [PIN_A, ENCODER_VALUE]}
Decorators:
  • @asyncio.coroutine

get_analog_latch_data(self, command)

source code 

This method retrieves a latch table entry for an analog pin.

See constants.py for definition of reply message parameters.

Parameters:
  • command - {"method": "get_analog_latch_data", "params": [ANALOG_PIN]}
Returns:
{"method": "get_analog_latch_data_reply", "params": [ANALOG_PIN, LATCHED_STATE, THRESHOLD_TYPE, THRESHOLD_TARGET, DATA_VALUE, TIME_STAMP ]}
Decorators:
  • @asyncio.coroutine

get_analog_map(self)

source code 

This method retrieves the Firmata analog map.

Refer to: http://firmata.org/wiki/Protocol#Analog_Mapping_Query to interpret the reply

The command JSON format is: {"method":"get_analog_map","params":["null"]}

Returns:
{"method": "analog_map_reply", "params": [ANALOG_MAP]}
Decorators:
  • @asyncio.coroutine

get_capability_report(self)

source code 

This method retrieves the Firmata capability report.

Refer to http://firmata.org/wiki/Protocol#Capability_Query

The command format is: {"method":"get_capability_report","params":["null"]}

Returns:
{"method": "capability_report_reply", "params": [RAW_CAPABILITY_REPORT]}
Decorators:
  • @asyncio.coroutine

get_digital_latch_data(self, command)

source code 

This method retrieves a latch table entry for a digital pin.

See constants.py for definition of reply message parameters.

Parameters:
  • command - {"method": "get_digital_latch_data", "params": [ANALOG_PIN]}
Returns:
{"method": "get_digital_latch_data_reply", "params": [DIGITAL_PIN, LATCHED_STATE, THRESHOLD_TYPE, THRESHOLD_TARGET, DATA_VALUE, TIME_STAMP ]}
Decorators:
  • @asyncio.coroutine

get_firmware_version(self)

source code 

This method retrieves the Firmata firmware version.

See: http://firmata.org/wiki/Protocol#Query_Firmware_Name_and_Version

JSON command: {"method": "get_firmware_version", "params": ["null"]}

Returns:
{"method": "firmware_version_reply", "params": [FIRMWARE_VERSION]}
Decorators:
  • @asyncio.coroutine

get_pinstate_report(self, command)

source code 

This method retrieves a Firmata pin_state report for a pin..

See: http://firmata.org/wiki/Protocol#Pin_State_Query

Parameters:
  • command - {"method": "get_pin_state", "params": [PIN]}
Returns:
{"method": "get_pin_state_reply", "params": [PIN_NUMBER, PIN_MODE, PIN_STATE]}
Decorators:
  • @asyncio.coroutine

get_protocol_version(self)

source code 

This method retrieves the Firmata protocol version.

JSON command: {"method": "get_protocol_version", "params": ["null"]}

Returns:
{"method": "protocol_version_reply", "params": [PROTOCOL_VERSION]}
Decorators:
  • @asyncio.coroutine

get_pymata_version(self)

source code 

This method retrieves the PyMata release version number.

JSON command: {"method": "get_pymata_version", "params": ["null"]}

Returns:
{"method": "pymata_version_reply", "params":[PYMATA_VERSION]}
Decorators:
  • @asyncio.coroutine

i2c_config(self, command)

source code 

This method initializes the I2c and sets the optional read delay (in microseconds).

It must be called before doing any other i2c operations for a given device.

Parameters:
  • command - {"method": "i2c_config", "params": [DELAY]}
Returns:
No Return message.
Decorators:
  • @asyncio.coroutine

i2c_read_data(self, command)

source code 

This method retrieves the last value read for an i2c device identified by address. This is a polling implementation and i2c_read_request and i2c_read_request_reply may be a better alternative.

Parameters:
  • command - {"method": "i2c_read_data", "params": [I2C_ADDRESS ]}
Returns:
{"method": "i2c_read_data_reply", "params": i2c_data}
Decorators:
  • @asyncio.coroutine

i2c_read_request(self, command)

source code 

This method sends an I2C read request to Firmata. It is qualified by a single shot, continuous read, or stop reading command. Special Note: for the read type supply one of the following strings:

"I2C_READ"

"I2C_READ | I2C_END_TX_MASK"

"I2C_READ_CONTINUOUSLY"

"I2C_READ_CONTINUOUSLY | I2C_END_TX_MASK"

Parameters:
  • command - {"method": "i2c_read_request", "params": [I2C_ADDRESS, I2C_REGISTER, NUMBER_OF_BYTES, I2C_READ_TYPE ]}
Returns:
{"method": "i2c_read_request_reply", "params": [DATA]}
Decorators:
  • @asyncio.coroutine

i2c_write_request(self, command)

source code 

This method performs an I2C write at a given I2C address,

Parameters:
  • command - {"method": "i2c_write_request", "params": [I2C_DEVICE_ADDRESS, [DATA_TO_WRITE]]}
Returns:
No return message.
Decorators:
  • @asyncio.coroutine

play_tone(self, command)

source code 

This method controls a piezo device to play a tone. It is a FirmataPlus feature. Tone command is TONE_TONE to play, TONE_NO_TONE to stop playing.

Parameters:
  • command - {"method": "play_tone", "params": [PIN, TONE_COMMAND, FREQUENCY(Hz), DURATION(MS)]}
Returns:
No return message.
Decorators:
  • @asyncio.coroutine

set_analog_latch(self, command)

source code 

This method sets the an analog latch for a given analog pin, providing the threshold type, and latching threshold.

Parameters:
  • command - {"method": "set_analog_latch", "params": [PIN, THRESHOLD_TYPE, THRESHOLD_VALUE]}
Returns:
{"method": "analog_latch_data_reply", "params": [PIN, DATA_VALUE_LATCHED, TIMESTAMP_STRING]}
Decorators:
  • @asyncio.coroutine

set_digital_latch(self, command)

source code 

This method sets the a digital latch for a given digital pin, the threshold type, and latching threshold.

Parameters:
  • command - {"method": "set_digital_latch", "params": [PIN, THRESHOLD (0 or 1)]}
Returns:
{"method": digital_latch_data_reply", "params": [PIN, DATA_VALUE_LATCHED, TIMESTAMP_STRING]}
Decorators:
  • @asyncio.coroutine

set_pin_mode(self, command)

source code 

This method sets the pin mode for the selected pin. It handles: Input, Analog(Input) PWM, and OUTPUT. Servo is handled by servo_config().

Parameters:
  • command - {"method": "set_pin_mode", "params": [PIN, MODE]}
Returns:
No return message.
Decorators:
  • @asyncio.coroutine

set_sampling_interval(self, command)

source code 

This method sets the Firmata sampling interval in ms.

Parameters:
  • command - {"method": "set_sampling_interval", "params": [INTERVAL]}
Returns:
No return message.

sonar_config(self, command)

source code 

This method configures 2 pins to support HC-SR04 Ping devices. This is a FirmataPlus feature.

Parameters:
  • command - {"method": "sonar_config", "params": [TRIGGER_PIN, ECHO_PIN, PING_INTERVAL(default=50), MAX_DISTANCE(default= 200 cm]}
Returns:
{"method": "sonar_data_reply", "params": [DISTANCE_IN_CM]}
Decorators:
  • @asyncio.coroutine

sonar_read(self, command)

source code 

This method retrieves the last sonar data value that was cached. This is a polling method. After sonar config, sonar_data_reply messages will be sent automatically.

Parameters:
  • command - {"method": "sonar_read", "params": [TRIGGER_PIN]}
Returns:
{"method": "sonar_read_reply", "params": [TRIGGER_PIN, DATA_VALUE]}
Decorators:
  • @asyncio.coroutine

servo_config(self, command)

source code 

This method configures a pin for servo operation. The servo angle is set by using analog_write().

Parameters:
  • command - {"method": "servo_config", "params": [PIN, MINIMUM_PULSE(ms), MAXIMUM_PULSE(ms)]}
Returns:
No message returned.
Decorators:
  • @asyncio.coroutine

stepper_config(self, command)

source code 

This method configures 4 pins for stepper motor operation. This is a FirmataPlus feature.

Parameters:
  • command - {"method": "stepper_config", "params": [STEPS_PER_REVOLUTION, [PIN1, PIN2, PIN3, PIN4]]}
Returns:
No message returned.
Decorators:
  • @asyncio.coroutine

stepper_step(self, command)

source code 

This method activates a stepper motor motion. This is a FirmataPlus feature.

Parameters:
  • command - {"method": "stepper_step", "params": [SPEED, NUMBER_OF_STEPS]}
Returns:
No message returned.
Decorators:
  • @asyncio.coroutine

onClose(self, was_clean, code, reason)

source code 

Websocket management message. This message is received when the client closes the connection. A console status message is printed and and the interface is shutdown before exiting.

Parameters:
  • was_clean - Autobahn provided flag
  • code - Autobahn provided flag
  • reason - Autobahn provided flag
Returns:
Console message is generated.

onConnect(self, request)

source code 

WebSocket management method. This method issues a console status message for Websocket connection establishment.

Parameters:
  • request - Websocket request
Returns:
No return value.

onMessage(self, payload, is_binary)

source code 

Websocket management method. This method receives JSON messages from the Websocket client. All messages are assumed to be text. If a binary message is sent, a console status message is generated.

The JSON command is interpreted and translated to a method call to handle the command request.

Parameters:
  • payload - JSON command
  • is_binary - True if message is binary. Assumed to always be False
Returns:
No value is returned.

onOpen(self)

source code 

WebSocket management method. This method issues a console status message for the opening of a Websocket connection. It sends a Firmata reset command to the Arduino.

Returns:
No return value.

analog_callback(self, data)

source code 

This method handles the analog message received from pymata_core

Parameters:
  • data - analog callback message
Returns:
{"method": "analog_message_reply", "params": [PIN, DATA_VALUE}

analog_latch_callback(self, data)

source code 

This method handles analog_latch data received from pymata_core

Parameters:
  • data - analog latch callback message
Returns:
{"method": "analog_latch_data_reply", "params": [ANALOG_PIN, VALUE_AT_TRIGGER, TIME_STAMP_STRING]}

digital_callback(self, data)

source code 

This method handles the digital message received from pymata_core

Parameters:
  • data - digital callback message
Returns:
{"method": "digital_message_reply", "params": [PIN, DATA_VALUE]}

digital_latch_callback(self, data)

source code 

This method handles the digital latch data message received from pymata_core

Parameters:
  • data - digital latch callback message
Returns:
s{"method": "digital_latch_data_reply", "params": [PIN, DATA_VALUE_AT_TRIGGER, TIME_STAMP_STRING]}

encoder_callback(self, data)

source code 

This method handles the encoder data message received from pymata_core

Parameters:
  • data - encoder data callback message
Returns:
{"method": "encoder_data_reply", "params": [ENCODER VALUE]}

i2c_read_request_callback(self, data)

source code 

This method handles the i2c read data message received from pymata_core.

Parameters:
  • data - i2c read data callback message
Returns:
{"method": "i2c_read_request_reply", "params": [DATA_VALUE]}

i2c_read_data_callback(self, data)

source code 

This method handles the i2c cached read data received from pymata_core.

Parameters:
  • data - i2c read cached data callback message
Returns:
{"method": "i2c_read_data_reply", "params": [DATA_VALUE]}

sonar_callback(self, data)

source code 

This method handles sonar data received from pymata_core.

Parameters:
  • data - sonar data callback message
Returns:
{"method": "sonar_data_reply", "params": [DATA_VALUE]}