async def getPowerMeasurement(self, expectedSwitchState): result = [True, None, None] def handleMessage(container, data): # check to ensure its this crownstone, not a mesh stone if there are multiple proggers if data["id"] == TESTING_CROWNSTONE_ID: # container[1] = (data["powerUsageReal"],data["powerUsageApparent"],data["powerFactor"]) container[1] = data["powerUsageReal"] container[2] = data["switchState"] if data["switchState"] == expectedSwitchState: # this will stop the measurement container[0] = False subscriptionId = BluenetEventBus.subscribe( DevTopics.newServiceData, lambda data: handleMessage(result, data)) counter = 0 while result[0] and counter < 100: counter += 1 await asyncio.sleep(0.05) if counter >= 100: print(gt(), "----- Get Power Measurement: Timeout Expired") BluenetEventBus.unsubscribe(subscriptionId) print(gt(), "----- Result in power measurement", result[1], "with switchState", result[2]) return {"powerMeasurement": result[1], "switchState": result[2]}
def uartecho(HelloWorld): def showUartMessage(data): print("Received payload", data) echo = BluenetEventBus.subscribe(UsbTopics.uartMessage, showUartMessage) bluenet.uartEcho(HelloWorld) time.sleep(0.2) bluenet.uartEcho(HelloWorld) time.sleep(0.2) BluenetEventBus.unsubscribe(echo)
def getMacAddress(): result = [True, None] def handleMessage(container, data): container[0] = False container[1] = data subscriptionId = BluenetEventBus.subscribe(DevTopics.ownMacAddress, lambda data: handleMessage(result, data)) bluenet._usbDev.requestMacAddress() time.sleep(0.5) BluenetEventBus.unsubscribe(subscriptionId) return result[1]
async def _getMacAddress(self): result = [True, None] def handleMessage(container, data): container[0] = False container[1] = data subscriptionId = BluenetEventBus.subscribe( DevTopics.ownMacAddress, lambda data: handleMessage(result, data)) self.bluenet._usbDev.requestMacAddress() counter = 0 while result[0] and counter < 50: counter += 1 await asyncio.sleep(0.05) BluenetEventBus.unsubscribe(subscriptionId) return result[1]
def showNewData(data): print("New data received!") print(json.dumps(data, indent=2)) print("-------------------") # Create new instance of Bluenet bluenet = Bluenet() # Start up the USB bridge. # Fill in the correct device, see the readme. # For firmware versions below 2.1, add the parameter baudrate=38400 bluenet.initializeUSB("/dev/ttyUSB0") # Set up event listeners BluenetEventBus.subscribe(UsbTopics.newDataAvailable, showNewData) # This is the id of the Crownstone we will be switching targetCrownstoneId = 10 # Switch this Crownstone on and off. switchState = True for i in range(0,100): if not bluenet.running: break if switchState: print("Switching Crownstone on (iteration: ", i,")") else: print("Switching Crownstone off (iteration: ", i,")") bluenet.switchCrownstone(targetCrownstoneId, on = switchState)
import time from BluenetLib import Bluenet, BluenetEventBus, Topics bluenet = Bluenet(catchSIGINT=True) cloud = bluenet.getCloud() cloud.loadUserConfigFromFile('user.json') sphereHandler = cloud.getSphereHandler('58de6bda62a2241400f10c67') def printEvent(topic, data): print("Got Event:", topic, data) BluenetEventBus.subscribe(Topics.personEnteredLocation, lambda x: printEvent(Topics.personEnteredLocation, x)) BluenetEventBus.subscribe(Topics.personLeftLocation, lambda x: printEvent(Topics.personLeftLocation, x)) print("Start Polling Presence") sphereHandler.startPollingPresence()
# Function that's called when the power usage is updated. def showPowerUsage(data): print("PowerUsage for Crownstone ID", data["id"], "is", data["powerUsage"], "W") # Create new instance of Bluenet bluenet = Bluenet() # Start up the USB bridge bluenet.initializeUSB("/dev/tty.SLAB_USBtoUART") # Set up event listeners BluenetEventBus.subscribe(Topics.powerUsageUpdate, showPowerUsage) # This is the id of the Crownstone we will be switching targetCrownstoneId = 10 # Switch this Crownstone on and off. switchState = True for i in range(0, 100): if not bluenet.running: break if switchState: print("Switching Crownstone on (iteration: ", i, ")") else: print("Switching Crownstone off (iteration: ", i, ")") bluenet.switchCrownstone(targetCrownstoneId, on=switchState)
handle = open(filename, "a") handle.write(payload + "\n") handle.close() print(payload) def showNewData(data): print("New data received!") print(json.dumps(data, indent=2)) print("-------------------") log("PING!") bluenet.uartEcho("PONG!") def showUartMessage(data): log("Received Uart Message " + data["string"]) # Set up event listeners BluenetEventBus.subscribe(UsbTopics.newDataAvailable, showNewData) BluenetEventBus.subscribe(UsbTopics.uartMessage, showUartMessage) # List the ids that have been seen print("Listening for Crownstones on the mesh, this might take a while.") while bluenet.running: time.sleep(1) # ids = bluenet.getCrownstoneIds() # print("Crownstone IDs seen so far:", ids)
# Create new instance of Bluenet bluenet = Bluenet() # Function that's called when the power usage is updated. def showUartMessage(data): print("Received payload", data) # Start up the USB bridge. # Fill in the correct device, see the readme. # For firmware versions below 2.1, add the parameter baudrate=38400 bluenet.initializeUSB("/dev/ttyUSB0") # Set up event listeners BluenetEventBus.subscribe(UsbTopics.uartMessage, showUartMessage) bluenet.uartEcho("HelloWorld") time.sleep(0.2) bluenet.uartEcho("HelloWorld") time.sleep(0.2) bluenet.uartEcho("HelloWorld") time.sleep(0.2) bluenet.uartEcho("HelloWorld") time.sleep(0.2) bluenet.uartEcho("HelloWorld") time.sleep(0.2) bluenet.uartEcho("HelloWorld") time.sleep(0.2) bluenet.uartEcho("HelloWorld") time.sleep(0.2)
adminKey="adminKeyForCrown", memberKey="memberKeyForHome", basicKey="guestKeyForOther", serviceDataKey="guestKeyForOther", localizationKey="localizationKeyX", meshApplicationKey="meshKeyForStones", meshNetworkKey="meshAppForStones", ) print('tt') bluenet.stop() ''' result = [True, None] def handleMessage(container, data): container[0] = False container[1] = data ''' subscriptionId = BluenetEventBus.subscribe(DevTopics.ownMacAddress, lambda data: handleMessage(result, data)) bluenet._usbDev.requestMacAddress() BluenetEventBus.unsubscribe(subscriptionId) counter = 0 while result[0] and counter < 50: counter += 1 time.sleep(0.5) print(result[1]) bluenet.stop() '''