Go Down

Topic: opening a new file with current date/time as file name (Read 1 time) previous topic - next topic

changks

Hello all,

I bought a Yun, because I can use the SD card to store the sensor data.

In my project, I intend to get sensor dataset every three hours and the save the dataset to a file name with the current date/time.

So, here is my questions:

  • how to active sensor to work every three hours, for example, 1:30 am, 4:30 am, 7:30 am ... etc.?
  • how to save the dataset to a file named with the current date/time?


Thanks for your help.

mart256

Thats very easy to do with python in the linux side. You can activate a task in linux with cron to predefined hours, run a script in python that tells arduino to read sensor, then receives value over bridge and saves the value on a txt or else with the date time name using the datetime module.

ShapeShifter

I agree with mart256, this is crying out for a Linux side Python solution. The issue is Python telling the Arduino sketch when to do things.

One way is to skip using the Bridge and roll your own solution. A Python script is triggered by cron, it opens /dev/ttyATH0 to talk to the sketch, and sends a string to indicate it's time to take a sample. The sketch reads this string from Serial1, collects the data, and sends it back through Serial1.  The Python script was waiting for a response, it gets it, saves the data to a file, and exits. It all starts over again the next time cron fires the script.

Another way uses the Bridge library. Cron fires off a Python script which just uses Bridge.put() to write a values to the bridge signaling that it's time to collect data. In the sketch's loop, it uses Bridge.get() to read the value. When it is set, it uses Bridge.put() to clear the value, collects the data,and uses a Process class object to run another Python script which writes the data to the file.

A third option is to have the sketch constantly collecting data and using Bridge.put() to save the latest sample.  Then, cron triggers a Python script when needed, which uses Bridge.get() to read the latest sample and write it to a file. An advantage of this method is that you can remotely request http://arduino.local/data/get to retrieve the current sensor reading even if it's between the scheduled collection times.

A final way is to have the sketch constantly looping and using a Process object to read the system time. It then decides if it's time to collect a sample, and when it's time it uses FileIO to open an appropriately named file and write the data. This is the way a regular Arduino with an RTC shield and SD card shield would do it, but is very clunky on the Yun. So much of what you need to do (read the time, schedule the event, write a file with a time based name) is done on the Linux side (even if you use Bridge calls to trigger it from the sketch side) that it really makes sense to do as much as possible in Python. Make the sketch do the physical sensor I/O, and let Linux do the rest.

I'm sure you or others can come up with other ways as well.

sonnyyu


sonnyyu

...
  • how to active sensor to work every three hours, for example, 1:30 am, 4:30 am, 7:30 am ... etc.?
  • how to save the dataset to a file named with the current date/time?

...
in computer world every three hours means 0:30 am, 3:30 am, 6:30 am...




ShapeShifter

Use Console Class collect sensor data
That's a slick solution! It's cleaner than any of my ideas.

After the Python script receives the data, it closes the socket. Will the while (!Console); statement once again wait for a new connection? (I don't have time to test this right now, should be doing paying work!)  I would assume so since it's a network socket and the YunClient class does work properly that way. The only reason I ask is that the Console class is normally used as a networked Serial class replacement, and while waiting for the serial port to open the first time does work, closing the serial port is not recognized by the Serial class and subsequent new connections cannot be detected properly.

If that works, and if you want the sketch to do other things, the  while (!Console); statement can be replaced with an  if (Console) statement to only do the console communications when the socket is open.

sonnyyu

...
If that works, and if you want the sketch to do other things, the  while (!Console); statement can be replaced with an  if (Console) statement to only do the console communications when the socket is open.
Thanks a lot, Revision code is posted.




Go Up