Reconnecting to MMC
I am having problems finding a way to reconnect and retrieve the data from MMC. I am trying to log accelerometer data and analog input data. With a python code, I am able to download this data if MMC always stays close to the computer. However, I need to store data for let's say 1 hour without being in BT range and then bringing the device close to the PC and make the data transfer. For the accelerometer, the mobile app can easily download the data even if gets out of range during the process. As far as I know, there is no option to log 'analog input' for the mobile app though.
I have read most of the tutorials and checked examples provided to write a code for some testing purposes with a help of my computer engineer friend. With the code, I can specify the total duration to log data and download it to my PC(windows). However, if they get out of range and come back to the range, it cannot complete the process.
Ideally, I would like to have two different .py code.
1: Connect to the board, subcribe the sensors and start logging.
Test the device out of the BT range.
2: Download the stored data
I am open to any tips. Links providing examples would help as well.
def process_accelerometer(self, s):
#global states
self.processor_created_fn= FnVoid_VoidP_VoidP(s.processor_created)
self.logger_created_fn= FnVoid_VoidP_VoidP(s.logger_ready)
self.event_created_fn = FnVoid_VoidP_VoidP_Int(s.event_ready)
print("Configuring self.device")
libmetawear.mbl_mw_settings_set_connection_parameters(s.device.board, 7.5, 7.5, 0, 6000)
sleep(1.5)
# setup accelerometer
libmetawear.mbl_mw_acc_set_range(s.device.board, 16.0)
libmetawear.mbl_mw_acc_set_odr(s.device.board, 25)
libmetawear.mbl_mw_acc_write_acceleration_config(s.device.board)
print("setup accelerometer sensor")
# get acc signal
acc_signal = libmetawear.mbl_mw_acc_get_acceleration_data_signal(s.device.board)
print("get accelerometer signal")
# setup passthrough on acc data for x samples and log it
s.events["processor"].clear()
print("setup passthrough")
passthrough = libmetawear.mbl_mw_dataprocessor_passthrough_create(acc_signal, PassthroughMode.COUNT, 0, None, self.processor_created_fn)
s.events["processor"].wait()
# setup logger
s.events["log"].clear()
libmetawear.mbl_mw_datasignal_log(acc_signal, None, self.logger_created_fn)
s.events["log"].wait()
def process_motion(self,s):
#global states
# setup any motion
libmetawear.mbl_mw_acc_bosch_set_any_motion_count(s.device.board, 2)
libmetawear.mbl_mw_acc_bosch_set_any_motion_threshold(s.device.board, 0.1)
libmetawear.mbl_mw_acc_bosch_write_motion_config(s.device.board)
print("setup bmi160 motion recognition")
# get motion signal
motion_signal = libmetawear.mbl_mw_acc_bosch_get_motion_data_signal(s.device.board)
print("get motion signal")
# create event that changes count based on motion signal
s.events["event"].clear()
libmetawear.mbl_mw_event_record_commands(motion_signal)
libmetawear.mbl_mw_dataprocessor_passthrough_set_count(s.passthrough_signal, 10)
print("create event that changes counter based on motion")
libmetawear.mbl_mw_event_end_record(motion_signal, None, self.event_created_fn)
s.events["event"].wait()
# start
print("Start")
libmetawear.mbl_mw_logging_start(s.device.board, 0)
libmetawear.mbl_mw_acc_enable_acceleration_sampling(s.device.board)
libmetawear.mbl_mw_acc_start(s.device.board)
def process_pressure(self,s):
e = Event()
self.file2 = open("Pressure.txt", "r+")
adc_signal = libmetawear.mbl_mw_gpio_get_analog_input_data_signal(s.device.board,0,GpioAnalogReadMode.ADC)
# Subscribe
libmetawear.mbl_mw_datasignal_subscribe(adc_signal, None, self.pressure_callback)
# Create Timer
timer = create_voidp(lambda fn: libmetawear.mbl_mw_timer_create_indefinite(s.device.board, 50, 0, None, fn), resource = "timer", event = e)
# Start recording commands:
libmetawear.mbl_mw_event_record_commands(timer)
# The first command is to read the signal - temp data - based on the timer - every 1s
libmetawear.mbl_mw_datasignal_read(adc_signal)
# Stop recording commands
create_voidp_int(lambda fn: libmetawear.mbl_mw_event_end_record(timer, None, fn), event = e)
# Start the timer
libmetawear.mbl_mw_timer_start(timer)
sleep(1800)
libmetawear.mbl_mw_datasignal_unsubscribe(adc_signal)
self.file2.close()
def write_to_file(self, p):
self.file1.writelines("{epoch: %d, value: %s} \n" % (p.contents.epoch, parse_value(p)))
def process_sensors(self):
for s in self.states:
sleep(10.0)
self.process_accelerometer(s)
sleep(10.0)
self.process_motion(s)
sleep(10.0)
self.process_pressure(s)
def download_sensor_data(self):
print("0 min")
#sleep(self.reading_time)
sleep(5)
self.file1 = open("Accelerometer.txt", "w")
for s in self.states:
print("Stop")
print("s")
libmetawear.mbl_mw_acc_stop(s.device.board)
libmetawear.mbl_mw_acc_disable_acceleration_sampling(s.device.board)
libmetawear.mbl_mw_logging_stop(s.device.board)
print("Downloading data")
sleep(1.0)
s.events["download"].clear()
def progress_update_handler(context, entries_left, total_entries):
if (entries_left == 0):
s.events["download"].set()
fn_wrapper = FnVoid_VoidP_UInt_UInt(progress_update_handler)
download_handler = LogDownloadHandler(context = None, \
received_progress_update = fn_wrapper, \
received_unknown_entry = cast(None, FnVoid_VoidP_UByte_Long_UByteP_UByte), \
received_unhandled_entry = cast(None, FnVoid_VoidP_DataP))
log_callback = FnVoid_VoidP_DataP(lambda ctx, p: self.write_to_file(p))
libmetawear.mbl_mw_logger_subscribe(s.logger, None, log_callback)
libmetawear.mbl_mw_logging_download(s.device.board, 0, byref(download_handler))
s.events["download"].wait()
self.file1.close()
libmetawear.mbl_mw_debug_disconnect(self.device.board)
self.device.disconnect()
print("Disconnected")
solution = Solution(10)
solution.search_for_devices()
solution.connect_to_device()
solution.create_state_of_device()
solution.process_sensors()
solution.download_sensor_data()