Skip to main content

crownstone-lib-python-uart

Official Python lib for Crownstone: "Crownstone Unified System Bridge", or Crownstone USB implementation.

This works on all platforms and requires a Crownstone USB to work.

Install guide

This module is written in Python 3 and needs Python 3.7 or higher. The reason for this is that most of the asynchronous processes use the embedded asyncio core library.

If you want to use python virtual environments, take a look at the README_VENV

You can install the package by pip:

pip3 install crownstone-uart

If you prefer the cutting edge (which may not always work!) or want to work on the library itself, use the setuptools.

python3 setup.py install

Requirements for the Crownstone USB

OS X

OS X requires installation of the SiliconLabs driver: https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers

Ubuntu

In order to use serial without root access, you should be in the dialout group.

You can check if you're in the group:

$ groups

To add yourself:

$ sudo adduser $USER dialout

Raspbian

Similar to Ubuntu.

Arch Linux

To use serial in Arch Linux, add yourself to the uucp group.

To add yourself to the group:

$ sudo gpasswd -a $USER uucp

Make sure to logout and login again to register the group change.

Example

An example is provided in the root of this repository.

Prerequisites

  • First use the phone app to setup your Crownstones and the Crownstone USB.
  • Make sure you update the Crownstones' firmware to at least 5.4.0.
  • Find out what port to use (e.g. COM1, /dev/ttyUSB0, or /dev/tty.SLAB_USBtoUART) and fill this in at discoveryExample.py and example.py at the initializeUSB method.
  • install the CrownstoneUartLib library using the setup.py:
python3 setup.py install

# or:

python setup.py install

Find the IDs of your Crownstones

Firstly run the example script that simply lists the IDs of the Crownstones. They are located in /examples/examplesUsb:

$ python3 ./examples/discoveryExample.py

Once some IDs are printed, use one of them for the next example. This can take a while because Crownstones, if not switched, only broadcast their state every 60 seconds.

Switch a Crownstone, and show power usage.

After filling in the port to use, and the Crownstone ID to switch, run the example with python 3:

$ python3 ./examples/example.py

The code

The example is shown below to get an idea of how everything works:

#!/usr/bin/env python3

"""An example that switches a Crownstone, and prints the power usage of the selected Crownstone."""

import time
from crownstone_uart import CrownstoneUart, UartEventBus, UartTopics


# This is the id of the Crownstone we will be switching
# change it to match the Crownstone Id you want to switch!
targetCrownstoneId = 3

def showNewData(data):
	global targetCrownstoneId
	if data["id"] == targetCrownstoneId:
		print("New data received!")
		print("PowerUsage of crownstone", data["id"], data["powerUsageReal"])
		print("-------------------")


uart = CrownstoneUart()

# Start up the USB bridge.
uart.initialize_usb_sync()
# you can alternatively do this async by
# await uart.initialize_usb()

# Set up event listeners
UartEventBus.subscribe(UartTopics.newDataAvailable, showNewData)

# Switch this Crownstone on and off.
turnOn = True

# The try except part is just to catch a control+c, time.sleep does not appreciate being killed.
try:
	for i in range(0, 10):
		if not uart.running:
			break

		if turnOn:
			print("Switching Crownstone on  (iteration: ", i,")")
		else:
			print("Switching Crownstone off (iteration: ", i,")")
		uart.switch_crownstone(targetCrownstoneId, on = turnOn)

		turnOn = not turnOn
		time.sleep(2)
except KeyboardInterrupt:
	print("\nClosing example.... Thanks for your time!")

uart.stop()

Documentation

The docs can be found here.

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

crownstone-uart-2.0.0.tar.gz (25.2 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

crownstone_uart-2.0.0-py3.7.egg (96.7 kB view details)

Uploaded Egg

crownstone_uart-2.0.0-py3.6.egg (96.5 kB view details)

Uploaded Egg

crownstone_uart-2.0.0-py3-none-any.whl (36.7 kB view details)

Uploaded Python 3

File details

Details for the file crownstone-uart-2.0.0.tar.gz.

File metadata

  • Download URL: crownstone-uart-2.0.0.tar.gz
  • Upload date:
  • Size: 25.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for crownstone-uart-2.0.0.tar.gz
Algorithm Hash digest
SHA256 eb3872c62ed4053d10d2042e108d25269ff610b58bfa355c0c82b9a3cf63bfbf
MD5 8d3257f70b2c588805edcd60450bb0f7
BLAKE2b-256 ecc3ff2424da75dfe9656d04b4201c18cc7c9d9ee75e0c222b523709a815add5

See more details on using hashes here.

File details

Details for the file crownstone_uart-2.0.0-py3.7.egg.

File metadata

  • Download URL: crownstone_uart-2.0.0-py3.7.egg
  • Upload date:
  • Size: 96.7 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.6.9

File hashes

Hashes for crownstone_uart-2.0.0-py3.7.egg
Algorithm Hash digest
SHA256 8847a2030402fe7b7401105e5ec6eb916923fc66cc2fdb60062bd6f4700713cb
MD5 9eeac1aedb9a4528767aeae8da9c3a19
BLAKE2b-256 6e6a44be34fd542b6c489b6b868133d2315f02b2ccb1a8f94d5a7c5972ff4843

See more details on using hashes here.

File details

Details for the file crownstone_uart-2.0.0-py3.6.egg.

File metadata

  • Download URL: crownstone_uart-2.0.0-py3.6.egg
  • Upload date:
  • Size: 96.5 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.6.9

File hashes

Hashes for crownstone_uart-2.0.0-py3.6.egg
Algorithm Hash digest
SHA256 f087ae569fff3e313234e8b71c6c4508ad5c6f0ff60d3367a5d8c9eeadc549c4
MD5 c8b8d0039364fc85c117bbd824c6dae0
BLAKE2b-256 735d017ba3d25e50f62b6f588ec449cafdeb4cec5140458b0198f6c04df14ff1

See more details on using hashes here.

File details

Details for the file crownstone_uart-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: crownstone_uart-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 36.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for crownstone_uart-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f97023bba2cc433e6ff00f5871b4c6f88bc0dbba28298fb248e35ccbffc7d2a8
MD5 51542baf6d5ff6c03b100e83033c8c89
BLAKE2b-256 40474a78ba24ac6665737293b6d0ee275fb5d39704776a0c2ba8d3420b48656d

See more details on using hashes here.

Release history Release notifications | RSS feed

2.7.0

2 files

2.6.0

2 files

2.5.0

2 files

2.4.1

2 files

2.4.0

2 files

2.3.0

2 files

2.2.0

2 files

2.1.0

2 files

This release

2.0.0 This release

4 files

1.0.1

2 files

1.0.0

4 files

0.8.0

2 files

0.7.8

2 files

0.7.7

4 files

0.7.6

2 files

0.7.5

2 files

0.7.4

4 files

0.7.3

2 files

0.7.2

2 files

0.6.2

2 files

0.6.1

2 files

0.6.0

2 files

0.5.3

2 files

0.5.2

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page