def main(argv): # instantiate the MBP client mbp = MBPclient() # initialize the MBP client mbp.connect() # parse input arguments interval = mbp.get_start_par_value(argv, "interval", DEFAULT_INTERVAL_FOR_SENDING_DATA) adc_channel = mbp.get_start_par_value(argv, "channel", DEFAULT_CHANNEL) try: # Hardware - init analog input reader reader = AnalogInputReader(adc_channel) while True: # retrieve sensor value received_value = reader.read_light() # send data to the MBP mbp.send_data(float(received_value)) # waits a time interval before sending new data time.sleep(int(interval)) except: error = sys.exc_info() print('Error reading sensor value or sending to the MBP:', str(error)) # terminate the MBP client mbp.finalize()
def main(argv): # instantiate the MBP client mbp = MBPclient() # initialize the MBP client mbp.connect() try: # This loop ensures your code runs continuously, # for example, to read sensor values regularly at a given interval. while True: ############################# #### Your code goes here #### value = random.choice([ 20.0, 20.5, 21.0, 22.0, 22.5, 25.5, 30.0, 30.1, 31.5, 29.9, 35.0 ]) ############################# # send data to the MBP, here a object with a single float field (key "value") is sent mbp.send_data("{\"value\":" + str(value) + "}") # waits a time interval before sending new data time.sleep(INTERVAL_BETWEEN_SENDING_DATA) except: error = sys.exc_info() print('Error:', str(error)) # terminate the MBP client mbp.finalize()
def main(argv): # default interval for sending data (seconds) INTERVAL_BETWEEN_SENDING_DATA = 15 PAR_FIELD_NAME = "name" PAR_FIELD_VALUE = "value" PAR_FIELD_NAME_INTERVAL = "interval" ##### Additional values MIFLORA_FIELD_NAME_SENSOR = "sensor" MIFLORA_FIELD_NAME_SENSOR_TYPE_TEMPERATURE = "temperature" MIFLORA_FIELD_NAME_MAC = "mac" targetSensor = MIFLORA_FIELD_NAME_SENSOR_TYPE_TEMPERATURE targetSensorMac = "" # instantiate the MBP client mbp = MBPclient() # initialize the MBP client mbp.connect() #### parse input arguments ###### paramArray = json.loads(argv[0]) #print(paramArray) for param in paramArray: if not (PAR_FIELD_NAME in param and PAR_FIELD_VALUE in param): continue elif param[PAR_FIELD_NAME].lower() == PAR_FIELD_NAME_INTERVAL.lower(): INTERVAL_BETWEEN_SENDING_DATA = int(param[PAR_FIELD_VALUE]) elif param[PAR_FIELD_NAME].lower() == MIFLORA_FIELD_NAME_SENSOR.lower( ): targetSensor = param[PAR_FIELD_VALUE] elif param[PAR_FIELD_NAME].lower() == MIFLORA_FIELD_NAME_MAC.lower(): targetSensorMac = param[PAR_FIELD_VALUE] ################################# try: # instantiate sensor reader of miflora poller = MiFloraPoller(targetSensorMac, BluepyBackend) # This loop ensures your code runs continuously to read values at a given interval while True: # retrieve sensor value received_value = poller.parameter_value(targetSensor) # send data to the MBP mbp.send_data(float(received_value)) # waits a time interval before sending new data time.sleep(INTERVAL_BETWEEN_SENDING_DATA) except: error = sys.exc_info() print('Error reading sensor value or sending to the MBP:', str(error)) # terminate the MBP client mbp.finalize()
def main(argv): mbp = MBPclient() mbp.connect() try: count = 0 while True: count = count + 1 mbp.send_data(count) time.sleep(INTERVAL_BETWEEN_SENDING_DATA) except: error = sys.exc_info() print('Error:', str(error)) mbp.finalize()