Package: pasco
READMEThis PASCO Python library allows users to connect to PASCO Wireless sensors using Python. Create your own data collection application, use sensors to interact with other hardware devices, or come up with your own unique solution!For project examples, view ourpasco_python_examples repository.How do I get started?To install the package into your Python environment, type this into your Terminalpip install pascoIn your project file, import thePASCOBLEDeviceclass and/or theCodeNodeDeviceclass.To connect to a regular wireless sensorfrom pasco import PASCOBLEDeviceTo connect to a //code.Node (Note: The Icons package is optional)from pasco import CodeNodeDevice, IconsCompatible Sensors//code.NodeSmart CartWireless Acceleration AltimterWireless CO2Wireless ConductivityWireless CurrentWireless DiffractionWireless Drop CounterWireless Force AccelerationWireless LightWireless Load CellWireless Magnetic FieldWireless MotionWireless O2Wireless Optical DOWireless pHWireless PressureWireless Rotary MotionWireless Smart CartWireless TemperatureWireless VoltageWireless WeatherTestingWireless Blood PressureWireless Soil MoistureConnecting to a sensorDevice StructureDevice: A physical PASCO wireless sensor is a device
Sensor: A device can have multiple sensors built in
Measurements: A sensor can offer multiple measurementsDevice Structure ExampleA Wireless Weather Sensor would be a "device".
The "device" has 4 sensors['WirelessWeatherSensor', 'WirelessGPSSensor', 'WirelessLightSensor', 'WirelessCompass']Each "sensor" can have multiple measurementsWirelessWeatherSensor:['Temperature', 'RelativeHumidity', 'AbsoluteHumidity', 'BarometricPressure', 'WindSpeed', 'DewPoint', 'WindChill', 'Humidex']WirelessGPSSensor:['SatelliteCount', 'Latitude', 'Longitude', 'Altitude', 'Speed']WirelessLightSensor:['UVIndex', 'Illuminance', 'SolarIrradiance', 'SolarPAR']WirelessCompass:['WindDirection', 'MagneticHeading', 'TrueHeading']Available Commandsdevice = PASCOBLEDevice()Create a Bluetooth device objectdevice.scan(sensor_name_filter: string [optional])Scan for available bluetooth devices. Returns a list of available devicesdevice.connect(ble_device: BLEDevice)Connect to a device using the object returned from the scan command.device.connect_by_id(pasco_device_id: string)Connect to a device using the 6 digit ID printed on the sensor.device.disconnect()Disconnect from a devicedevice.is_connectedReturns true/false to tell device connection statedevice.get_sensor_list()Get a list of sensors that a device hasdevice.get_measurement_list(sensor_name: string [optional])Returns all the measurements that a device hasdevice.read_data(measurement: string)Get a single reading from a single measurementdevice.read_data_list(measurements: List[string])Get a list of readings for multiple measurementsdevice.get_measurement_unit(measurement: string)Get the default units for a single measurementdevice.get_measurement_unit_list(measurements: List[string])Get a list of default units for multiple measurementsPASCO's Bluetooth sensors will turn off after 5 minutes of no activity. To keep the device on, call thedevice.keepalive()method. This will keep the connection active without requesting any new data.Step 1: Create an object for the devicemy_sensor = PASCOBLEDevice()If you know the device's 6-digit serial ID (printed on the device) you can quickly scan and connect using the command:my_sensor.connect_by_id('111-123')Otherwise perform Steps 2 & 3 to scan/connect.Step 2: Scan for available bluetooth (BLE) sensorsmy_sensor.scan()Returns list of BLE devices found in the scan.my_sensor.scan('Temperature')` Returns a list of Temperature sensors foundHow to use:found_devices = my_sensor.scan()Step 3: Connect to a BLE sensor found from the scanThe scan command will return a list of found devices. Iterate through that list to determine which device you want to connect to.One way is to print the list and prompt the user like this:for i, ble_device in enumerate(found_devices):
    print(f'{i}: {ble_device.name}')

selected_device = input('Select a device: ')
my_sensor.connect(found_devices[int(selected_device)])Example of how to scan/connectmy_sensor = PASCOBLEDevice()
found_devices = my_sensor.scan()

print('\nDevices Found')
for i, ble_device in enumerate(found_devices):
    display_name = ble_device.name.split('>')
    print(f'{i}: {display_name[0]}')

# Auto connect if only one sensor found
selected_device = input('Select a device: ') if len(found_devices) > 1 else 0
ble_device = found_devices[int(selected_device)]

my_sensor.connect(ble_device)Step 4: View Device Sensor(s)A device can have one or more on-board sensors. To view the list of sensors use the commandmy_sensor.get_sensor_list(). This returns a list of sensor names that a device has.Step 5: View Device Measurement(s)Each sensor in the device can have one or more measurements. If you want to view all the measurements that a device has, use the commandmy_sensor.get_measurement_list().To view only the measurements that a sensor has, use the sensor name (from the list in Step 4) like thismy_sensor.get_measurement_list('WirelessWeatherSensor').Step 6: Start collecting data!The measurement variable names come from Step 4To read theTemperaturemy_temperature_sensor.read_data('Temperature')To read theRelativeHumiditymy_weather_sensor.read_data('RelativeHumidity')To read a multiple measurements at one timemy_weather_sensor.read_data_list(['Temperature','RelativeHumidity'])To get the units for a single measurementmy_temperature_sensor.get_measurement_unit('Temperature')To get the units for a list of measurementsmy_weather_sensor.get_measurement_unit_list(['Temperature','RelativeHumidity'])//code.Node Specific CommandsIn order to connect to a //code.Node we must import theCodeNodeDeviceobject and (optionally) the character library which allows a user to display icons on the 5x5 LED Array.from pasco import CodeNodeDevice, Iconsmy_code_node = CodeNodeDevice()Create //code.Node Bluetooth device objectmy_code_node.set_led_in_array()Set an individual LED in the 5x5 LED Arraymy_code_node.set_leds_in_array()Set multiple LEDs in the 5x5 LED Arraymy_code_node.set_rgb_led()Set the RGB LEDmy_code_node.set_sound_frequency()Set the speaker frequencymy_code_node.scroll_text_in_arrayScroll text on the 5x5 LED Arraymy_code_node.show_image_in_array()Display an image in the 5x5 LED Arraymy_code_node.reset()Reset all of the //code.Node outputsSet LEDs on the 5x5 Displayx, y coordinates on the //code.Node 5x5 LED display
---------------------------
| 0,0  1,0  2,0  3,0  4,0 |
| 0,1  1,1  2,1  3,1  4,1 |
| 0,2  1,2  2,2  3,2  4,2 |
| 0,3  1,3  2,3  3,3  4,3 |
| 0,4  1,4  2,4  3,4  4,4 |
---------------------------

intensity range is 0-255Set one LEDcode_node_device.set_led_in_array(x, y, intensity)Example: This will turn the top center LED on at max brightnesscode_node_device.set_led_in_array(2, 0, 255)Set multiple LEDs at oncecode_node_device.set_leds_in_array(led_list, intensity)led_list = [[4,4], [0,4], [2,2]]
code_node_device.set_leds_in_array(led_list, 128)Set the RGB LEDcode_node_device.set_rgb_led(r, g, b)r,g,bindicate brightness ranges between 0 and 255.r = 20
g = 100
b = 200
code_node_device.set_rgb_led(r, g, b)Turn the speaker on/offcode_node_device.set_sound_frequency(frequency)Sendfrequency(int) in Hzcode_node_device.set_sound_frequency(440)Turn the speaker offcode_node_device.set_sound_frequency(0)Scroll Text on the 5x5 LED Arraycode_node_device.scroll_text_in_array(text)This will scroll the text on the //code.Node's displaycode_node_device.scroll_text_in_array('HELLO WORLD')The character librarycode_node_device.show_image_in_array(Icons().smile)If we import theIconsclass from thecharacter_libraryto our project we can show unique images on the 5x5 LED Array. Refer to the library file to see available options. Examples:code_node_device.show_image_in_array(Icons().smile)
code_node_device.show_image_in_array(Icons().heart)Reset the code_node outputscode_node_device.reset()Turn the 5x5 LED display, RGB LED and speaker off.Let's put it all togetherExample 1A: One shot readConnect to a Wireless Temperature Sensor and get one reading:from pasco import PASCOBLEDevice


temp_sensor = PASCOBLEDevice()
temp_sensor.connect_by_id('055-808')

temp_value = temp_sensor.read_data('Temperature')
print(temp_value)

temp_sensor.disconnect()Example 1B: One shot read and display unitsfrom pasco import PASCOBLEDevice


temp_sensor = PASCOBLEDevice()
temp_sensor.connect_by_id('055-808')

temp_value = temp_sensor.read_data('Temperature')
temp_units = temp_sensor.get_measurement_unit('Temperature')
print(f'{temp_value} {temp_units}')

temp_sensor.disconnect()Example 2: Scan/select a sensor and read dataScan for a sensor and get the current temperature. In this example we can use a Temperature, Weather or //code.Node to read the temperature measurement. We do not need to specify a device type. We will continuously read and display the result.from pasco import PASCOBLEDevice


my_sensor = PASCOBLEDevice()
found_devices = my_sensor.scan()

print('\nDevices Found')
for i, ble_device in enumerate(found_devices):
    display_name = ble_device.name.split('>')
    print(f'{i}: {display_name[0]}')

# Auto connect if only one sensor found
selected_device = input('Select a device: ') if len(found_devices) > 1 else 0

ble_device = found_devices[int(selected_device)]
my_sensor.connect(ble_device)

# Loop that will read/display the data 100 times
for i in range(100):
    current_temp = my_sensor.read_data('Temperature')
    print(f'The current temp is {current_temp}')

my_sensor.disconnect()Example 3: Working with the //code.NodeBelow is a simple example that shows how to connect to a //code.Node, read a measurement and control an output.from pasco import CodeNodeDevice


code_node = CodeNodeDevice()
code_node.connect_by_id('481-782')

while code_node.read_data('Button1') == 0:
    if code_node.read_data('Brightness') < 2:
        code_node.set_rgb_led(100,100,100)
    else:
        code_node.set_rgb_led(0,0,0)

code_node.scroll_text_in_array('Goodbye')

code_node.reset()Example 4: Connect to multiple sensorsWe can also connect to multiple sensors. Here we are connecting to a //code.Node and Wireless Force Sensor. We are also using //code.Node specific commands and testing the Character Library.from pasco import PASCOBLEDevice, CodeNodeDevice, Icons


code_node_device = CodeNodeDevice()
found_devices = code_node_device.scan('//code.Node')

if found_devices:
    for i, ble_device in enumerate(found_devices):
        print(f'{i}: {ble_device.name}')

    selected_device = input('Select a device: ') if len(found_devices) > 1 else 0
    code_node_device.connect(found_devices[int(selected_device)])
else:
    print("No Devices Found")
    exit(1)

force_accel_device = PASCOBLEDevice()
found_devices = force_accel_device.scan('Force')

if found_devices:
    for i, ble_device in enumerate(found_devices):
        print(f'{i}: {ble_device.name}')

    selected_device = input('Select a device: ') if len(found_devices) > 1 else 0
    force_accel_device.connect(found_devices[int(selected_device)])
else:
    print("No Devices Found")
    exit(1)

code_node_device.reset()
light_on = False

for i in range (1000):
    if force_accel_device.read_data('Force') > 10:
        if light_on == False:
            code_node_device.set_rgb_led(100,100,100)
            code_node_device.set_sound_frequency(200)
            code_node_device.show_image_in_array(Icons().alien)
            light_on = True
        else:
            code_node_device.reset()
            light_on = False
        while force_accel_device.read_data('Force') > 10:
            pass

code_node_device.disconnect()
force_accel_device.disconnect()