2

I am currently building a weather logger with a DHT22 that logs the data to a CSV file. It will be used in a place without internet and also without a screen. The script i am using logs the data as long it's running and then writes the CSVon exit.

My idea for the ideal setup: another script that exits the logging script as soon as you plug in a USB Drive, copies the CSV to the drive and triggers the logging script again as soon as you eject the USB. Is this possible? Thanks! :)

1
  • I would have a look at the udev rules (axllent.org/docs/view/auto-mounting-usb-storage) to mount the USB. Look at sync to force writes to the USB. You will need to know when its dismounted and safe to remove (not the same time as sync if the USB has cache) - maybe a flashing light? Look at ps and kill to end the program and rsync to copy the data :-). The RPF have a book on command line scripting store.rpipress.cc/collections/getting-started/products/… that would help (though the free PDF seems to have gone from the new site).
    – user115418
    Commented Apr 20, 2020 at 13:31

1 Answer 1

2

One thing to consider with your approach is writing your data to a log file (append) with each measurement rather than writing data from memory all at once. This would reduce data loss and would allow you to asynchronously download your data file whenever you like.

You can use the GPIO to provide a limited interface for you to trigger the data copy to a USB drive. You will need to mount, copy the file, and unmount your USB to assure reliable data transfer. You could implement this using interrupts to initiate a script and use a light emitting diode (LED) to indicate that the script executed and that it is safe to remove the USB drive. You can use different color LEDs to indicate script execution status.

Here is an article on interrupts that will get you started. A broader search on interrupts on the Raspberry Pi will find other references.

https://www.raspberrypi.org/forums/viewtopic.php?t=107771

Here's a use case for how your system could work.

  • Weather logger records data and writes to a log file. This happens asynchronously with respect to data transfer.

To copy log file data:

  • Plug USB drive into Raspberry Pi.
  • Press a button (debounced switch).
  • LED (yellow) illuminates to indicate script execution start.
  • Script mounts USB drive.
  • Script copies log file to USB drive.
  • Script unmounts USB drive.
  • LED (green) illuminates to indicate success. (Or, LED (red) illuminates on error.)
  • Remove USB drive.

Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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