I would appreciate if anyone could give me some directions here. I have an MPU6050 accelerometer connected to RP Zero. I made it portable so that I could collect data outside. On start up, with crontab -e command, the script runs successfully and I am able to log data to csv file from the accelererometer with no problem. So everything seems to work fine when I am at home and connected to wifi. As soon as I go with raspberrypi outside, there is no logging happens at all. Once I am back home or at proximity of home wifi and start the device again, it starts to collect data. I want to be able to use the device outside and to collect data where there is no internet but can't solve the problem so far. Please let me know if you have any advice, thanks The code that I use is below:
import os
import argparse
import datetime
from csv import DictWriter
# SMBus module of I2C
from time import sleep, time
import smbus
# MPU6050 Registers and their Address
DEVICE_ADDRESS = 0x68
# 0 for older version boards
BUS_NO = 1
PWR_MGMT_1 = 0x6B
SMPLRT_DIV = 0x19
CONFIG = 0x1A
GYRO_CONFIG = 0x1B
INT_ENABLE = 0x38
ADDRESS_MAP = {'gx': 0x43, 'gy': 0x45, 'gz': 0x47, 'ax': 0x3B, 'ay': 0x3D, 'az': 0x3F}
VALUE_NORMS = {'gx': 131., 'gy': 131., 'gz': 131., 'ax': 16384., 'ay': 16384., 'az': 16384.}
OUT_DIR = 'output'
def mpu_init():
# write to sample rate register
bus.write_byte_data(DEVICE_ADDRESS, SMPLRT_DIV, 7)
# write to power management register
bus.write_byte_data(DEVICE_ADDRESS, PWR_MGMT_1, 1)
# write to Configuration register
bus.write_byte_data(DEVICE_ADDRESS, CONFIG, 0)
# write to Gyro configuration register
bus.write_byte_data(DEVICE_ADDRESS, GYRO_CONFIG, 24)
# write to interrupt enable register
bus.write_byte_data(DEVICE_ADDRESS, INT_ENABLE, 1)
def read_raw_data(addr):
# accelerometer and gyro value are 16-bit
high = bus.read_byte_data(DEVICE_ADDRESS, addr)
low = bus.read_byte_data(DEVICE_ADDRESS, addr+1)
# concatenate higher and lower value
value = ((high << 8) | low)
# to get signed value from mpu6050
if value > 32768:
value = value - 65536
return value
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--filename', type=str, help='Name of the file to write data to')
parser.add_argument(
'--log', default=list(ADDRESS_MAP.keys()), nargs='+', choices=list(ADDRESS_MAP.keys()),
help='What parameters you would like to log from the MPU'
)
parser.add_argument('--hertz', type=int, default=100, help='Sampling rate from the MPU')
args = vars(parser.parse_args())
bus = smbus.SMBus(BUS_NO)
mpu_init()
if not os.path.isdir(OUT_DIR):
os.makedirs(OUT_DIR)
sleep_time = 1 / args['hertz']
if args['filename'] is not None:
output_path = os.path.join(OUT_DIR, f'{args["filename"]}.csv')
else:
output_path = os.path.join(OUT_DIR, f'{datetime.datetime.now().strftime("%Y%m%d_%H%M%S")}.csv')
log_keys = sorted(args['log'])
print(f'Outputting data to {output_path:s}')
print(f'Logging at a sampling rate of {args["hertz"]:d} Hz')
print(f'Logging: {log_keys}')
start = time()
with open(output_path, 'w', newline='') as fp:
csv_writer = DictWriter(fp, delimiter=',', fieldnames=log_keys + ['time(ms)'])
csv_writer.writeheader()
while True:
csv_writer.writerow({
**{key: f'{read_raw_data(ADDRESS_MAP[key]) / VALUE_NORMS[key]:.3f}' for key in log_keys},
**{'time(ms)': f'{(time() - start) * 1000:.1f}'}
})
sleep(sleep_time)