VernierST / godirect-examples

Official examples for Vernier Go Direct® Sensors in both Python and JavaScript
BSD 3-Clause "New" or "Revised" License
23 stars 26 forks source link

how to collect data infinitely until user input? #43

Closed stewartpeery closed 2 years ago

stewartpeery commented 2 years ago

So I've been able to execute the simple example scripts no problem running the probes connected to a RasPi. But what I really want to do is run probes indefinitely until user input. For example, I'd like to run a CO2 probe for many days sampling a terrarium and recording the measurements in a csv for data analysis. I can get the writing to csv script to run, but I'm not sure how to run it until I determine the experiment is over. Any thoughts? Thanks in advance for the help.

dvernier commented 2 years ago

I am trying to understand the issue. If you take any of our simple program samples that take say 5 samples, and change the number to 5 million, they will run for a long time. Is that not what you want? You can also change the time between samples with the start statement.

stewartpeery commented 2 years ago

Thanks for the idea of just making the samples huge- had not considered that. And I am fine with changing the intervals; that seemed straightforward. Ideally I would be running the RasPi headless and using a button to start and end data collection with a while loop, but my Python is shaky. I suppose the massive sample number is a brute force way to do this and I could SSH in to start/stop the script- is this what you would suggest? And having physical input would be more doable for my colleagues and students (I'm a high school teacher); any thoughts on how to approach coding that would be welcome.

sswartley commented 2 years ago

Hello,

Here is a draft of an exercise for using the Raspberry Pi pins to control data collection (attached pdf). It doesn't do exactly what you are asking, but could be modified. Since this is a draft your feedback would be appreciated.

Raspberry Pi Pins Button.pdf

Sam

stewartpeery commented 2 years ago

Thanks Sam! I'll check it out and get you some feedback. Also, I have had success today setting the data collection period to 100K and collecting CO2 and Temp from a CO2 sensor. But I ran into 2 other issues:

  1. I have the data collection interval set to 5000 ms and every few measurements I get "ERROR: HID read timeout!". It still records the timestamp I coded, but not the CO2 or temp.
  2. When connecting via bluetooth, the example store to csv script crashes when it gets to select sensors. It was pointing to the gdx.py file and giving an error about "not bool." This issue did not occur when connecting via USB- same sensor went through the selection process just fine.
sswartley commented 2 years ago

Great! See what you think of the activity and let me know. You could probably also find good examples showing a keyboard button press to start and stop your loop, if that makes more sense for you.

1: I think that is a known bug. I'm pretty sure in the godirect library there is a hardcoded timeout of 5000 ms in the read. Speed it up just a bit and it should work.

2: Maybe modify this code a bit, and hardcode in the sensor number to see if that helps. It looks like in the example it leaves the argument blank: gdx.select_sensors()

Instead, put in your sensor numbers: gdx.select_sensors([1,2])

NOTE: in the example code above I'm suggesting sensor numbers [1,2]. I don't actually know off the top of my head if those are the right numbers for your device (probably not). If you are not sure what the sensor numbers are you can run the gdx_getting_started_device_info.py example. This example will show you all of the sensor channel numbers on your device.

Let me know if that helps,

Sam

stewartpeery commented 2 years ago

Sam, I can confirm that reducing sample time to 4000ms and hardcoding sensors (with bluetooth proximity pairing) both worked. Getting much closer on this project, and hopefully I can focus on the button later this week. Am I reading the code correctly that button press starts the loop and button press with hold breaks it (if so, nice idea!)? Also, I am looking to time stamp data collection and have utilized time module so far. Any thoughts about how to incorporate that into the draft you sent? The latest program I've run is attached gdx_getting_started_export_to_csv.txt .

stewartpeery commented 2 years ago

Actually, further wrinkle with the hardcoding of sensors on bluetooth. It bypasses the error (line 186 in gdx.py issue- for s in sensors, TypeError: 'bool' object is not iterable) but does not record data. I hardcoded it as ([1,.2]) as suggested above (CO2 and temp, by the way). Just guessing, but I'm wondering if the proximity pairing isn't set up to deal with sensors of various capabilities, i.e., it's difficult for the code to find the nearest device and then correctly generate a list of available sensors. I have not tried hardcoding the device ID. It is the same error without proximity pairing and selecting manually to connect to that probe. Again, this code works fine if USB.

sswartley commented 2 years ago

Have you ever had any luck using Bluetooth with the Go Direct sensors in Python? If you run the examples without hardcoding in a device id or sensors do you get the dialog in the console listing the found devices?

I know that recently others have had issues using Raspberry Pi because of the bleak library. This is a library that we use for BLE communication. Take a look at the bottom of this discussion. Perhaps you could check your version of bleak and then install the latest version - then shut down and restart - and give it a try?

stewartpeery commented 2 years ago

Thanks Sam- updating the bluez seems to have solved it. The 5.50 version is what came with the OS, and I upgraded to 5.64 (most recent version in the repository). Both the "getting started with bluetooth" script and my own csv scripts are working fine now with bluetooth.

sswartley commented 2 years ago

Great! If you run into issues with the examples or library you could open a new issue here. But if you want to keep us posted on how it goes or share your ideas you can just send an email to support@vernier.com. If you address the question to me it should get forwarded to me.