Esempio n. 1
0
class UartThreadExample(threading.Thread):
    def __init__(self):
        self.loop = None
        self.uart = None
        threading.Thread.__init__(self)

    def run(self):
        self.loop = asyncio.new_event_loop()
        self.uart = CrownstoneUart()
        # choose either sync, or async operation
        self.loop.run_until_complete(self.runIt())
        # self.runIt_sync()

    async def runIt(self):
        await self.uart.initialize_usb()

    def runIt_sync(self):
        self.uart.initialize_usb_sync()

    def stop(self):
        self.uart.stop()
Esempio n. 2
0
class UartThreadExample(threading.Thread):
    def __init__(self):
        self.loop = None
        self.uart = None
        threading.Thread.__init__(self)

    def run(self):
        self.loop = asyncio.new_event_loop()
        self.uart = CrownstoneUart()
        # choose either sync, or async operation
        self.loop.run_until_complete(self.runIt())
        # self.runIt_sync()

    async def runIt(self):
        await self.uart.initialize_usb()
        self.switch_crownstone()

    def runIt_sync(self):
        self.uart.initialize_usb_sync()
        self.switch_crownstone()

    def switch_crownstone(self):
        turnOn = True
        for i in range(0, 10):
            if not self.uart.running:
                break

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

    def stop(self):
        self.uart.stop()
Esempio n. 3
0
import signal

from crownstone_uart import CrownstoneUart, UartEventBus, UartTopics
from crownstone_uart.topics.DevTopics import DevTopics

from BluenetWebSocket import WebSocketServer
from BluenetWebSocket.lib.connector.BluenetConnector import BluenetConnector

from parser.WSParser import WSParser

# Create new bluenet instance
bluenet = CrownstoneUart()

# Start up the USB bridge
bluenet.initialize_usb_sync("/dev/ttyACM0")

# start the websocket server
server = WebSocketServer(9000)

# connect the websocket server to bluenet lib
server.connectToBluenet(bluenet, UartEventBus, UartTopics)

connector = BluenetConnector()
connector.connect(UartEventBus, DevTopics)

# add our custom parser
customParser = WSParser()
customParser.connectToBluenet(bluenet)
server.loadCustomParser(customParser.receiveWebSocketCommand)
def showNewData(data):
    global targetCrownstoneId
    if data.crownstoneId == targetCrownstoneId:
        print("New data received!")
        if data.type == AdvType.CROWNSTONE_STATE or data.type == AdvType.EXTERNAL_STATE:
            print(
                f"PowerUsage of crownstone {data.crownstoneId} is {data.powerUsageReal}W"
            )
        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
Esempio n. 5
0
# Keep up the operation mode.
stoneHasBeenSetUp = False


def handleHello(data):
    helloResult = data
    logging.log(logging.DEBUG, f"flags={helloResult.status.flags}")
    global stoneHasBeenSetUp
    stoneHasBeenSetUp = helloResult.status.hasBeenSetUp
    logging.log(logging.DEBUG, f"stoneHasBeenSetUp={stoneHasBeenSetUp}")


UartEventBus.subscribe(UartTopics.hello, handleHello)

# Start up the USB bridge.
uart.initialize_usb_sync(writeChunkMaxSize=64)

# Sphere specific settings:
adminKey = "adminKeyForCrown"
memberKey = "memberKeyForHome"
basicKey = "basicKeyForOther"
serviceDataKey = "MyServiceDataKey"
localizationKey = "aLocalizationKey"
meshAppKey = "MyGoodMeshAppKey"
meshNetworkKey = "MyGoodMeshNetKey"
ibeaconUUID = "1843423e-e175-4af0-a2e4-31e32f729a8a"
sphereId = 1

# Stone specific settings:
crownstoneId = 200
meshDeviceKey = "aStoneKeyForMesh"
Esempio n. 6
0
from crownstone_uart import CrownstoneUart

from bluenet_logs import BluenetLogs

# Change this to the path with the extracted log strings on your system.
logStringsFile = "/opt/bluenet-workspace/bluenet/build/default/extracted_logs.json"

# Init bluenet logs, it will listen to events from the Crownstone lib.
bluenetLogs = BluenetLogs()

# Set the dir containing the bluenet source code files.
bluenetLogs.setLogStringsFile(logStringsFile)

# Init the Crownstone UART lib.
uart = CrownstoneUart()
uart.initialize_usb_sync(port="/dev/ttyACM0")

# The try except part is just to catch a control+c to gracefully stop the UART lib.
try:
	# Simply keep the program running.
	print(f"Listening for logs and using \"{logStringsFile}\" to find the log formats.")
	while True:
		time.sleep(1)
except KeyboardInterrupt:
	pass
finally:
	print("\nStopping UART..")
	uart.stop()
	print("Stopped")
Esempio n. 7
0
stoneHasBeenSetUp = False


def handleHello(data):
    helloResult = data
    logging.log(logging.DEBUG, f"flags={helloResult.status.flags}")
    global stoneHasBeenSetUp
    stoneHasBeenSetUp = helloResult.status.hasBeenSetUp
    logging.log(logging.INFO, f"stoneHasBeenSetUp={stoneHasBeenSetUp}")


UartEventBus.subscribe(UartTopics.hello, handleHello)

# Start up the USB bridge.
uart = CrownstoneUart()
uart.initialize_usb_sync(port=args.device, writeChunkMaxSize=64)

# Sphere specific settings:
adminKey = "adminKeyForCrown"
memberKey = "memberKeyForHome"
basicKey = "basicKeyForOther"
serviceDataKey = "MyServiceDataKey"
localizationKey = "aLocalizationKey"
meshAppKey = "MyGoodMeshAppKey"
meshNetworkKey = "MyGoodMeshNetKey"
ibeaconUUID = "1843423e-e175-4af0-a2e4-31e32f729a8a"
sphereId = 1

# Stone specific settings:
crownstoneId = 200
meshDeviceKey = "aStoneKeyForMesh"
Esempio n. 8
0
    type=str,
    default=None,
    help='The UART device to use, for example: /dev/ttyACM0')
args = argParser.parse_args()

sourceFilesDir = args.sourceFilesDir

# Init bluenet logs, it will listen to events from the Crownstone lib.
bluenetLogs = BluenetLogs()

# Set the dir containing the bluenet source code files.
bluenetLogs.setSourceFilesDir(sourceFilesDir)

# Init the Crownstone UART lib.
uart = CrownstoneUart()
uart.initialize_usb_sync(port=args.device)

# The try except part is just to catch a control+c to gracefully stop the UART lib.
try:
    # Simply keep the program running.
    print(
        f"Listening for logs and using files in \"{sourceFilesDir}\" to find the log formats."
    )
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    pass
finally:
    print("\nStopping UART..")
    uart.stop()
    print("Stopped")