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
# 0 for older version boards
BUS_NO = 1
PWR_MGMT_1 = 0x6B
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')
        '--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)

    if not os.path.isdir(OUT_DIR):

    sleep_time = 1 / args['hertz']
    if args['filename'] is not None:
        output_path = os.path.join(OUT_DIR, f'{args["filename"]}.csv')
        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)'])
        while True:
                **{key: f'{read_raw_data(ADDRESS_MAP[key]) / VALUE_NORMS[key]:.3f}' for key in log_keys},
                **{'time(ms)': f'{(time() - start) * 1000:.1f}'}

Check that the system is not set to wait for a network connection.

  1. From the terminal sudo raspi-config
  2. Option 3 - Boot Options
  3. Option B2 - Wait for network connection during boot
  4. Select No

Menu options based on the 20200727 version of raspi-config.

  • It worked, thank you! – Maksym Kepskyy Aug 19 '20 at 10:40
  • @MaksymKepskyy Please mark the answer as the accepted one with a click on the tick on its left side. That prevents your Question from being shown as a unsolved Post to the community and saves them/us a lot of work. – Ingo Aug 20 '20 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.