0

I am working on coding lua script.

What I am coding is..Collecting the data and save it into the certain file.

Situation :
There are two sensors that when they recognize the object in front of it, the value of the sensor will be increased.
I want to save the data of the value of sensor every 100ms with time.
Time format would be "2013-04-25 10:30:004"

What I did is here.

===========================================================

require("TIMER")
require("TIMESTAMP")
require("ANALOG_IN")

function OnExit()
    print("Exit code...do something")
end

function main()

    timer = "TIMER"
    analogsensor_1 = "AIR_1"
    analogsensor_2 = "AIR_2"

    while true do 
        valueOfSensor_1 = ANALOG_IN.readAnalogIn(analogsensor_1);
        valueOfSensor_2 = ANALOG_IN.readAnalogIn(analogsensor_2);

        write(colltection_of_data.txt)
        go(print(valueOfSensor_1), 0.1)     //print value of sensor every 100ms
        print(time)
        go(print(valueOfSensor_2), 0.1)
        print(time)
    end 
    TIMER.sleep(timer,500)

end 

print("start main")
main()

================================================================

I know it's not complete code. How can I save the data into certain file? and how can I show the time format like that?

Thank you in advance!

Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
jung hur
  • 81
  • 1
  • 4
  • 11

2 Answers2

4

To get date and time you call:

local timestr = os.date("%Y-%m-%d %H:%M:%S")

Now to save that to a file you need to open the file

local filehandle = io.open(filename[, mode]) - Manual

To output desired data, you then use

local filehandle = io.open("Log.txt", "w+")
filehandle:write(timestr, " - Sensor1: ", tostring(valueOfSensor1), "\n")

Of course, you open your file only once and then issue write command every x (milli)seconds. After you're done:

filehandle:close()

P.S. Please use locals whenever possible. It's much faster than globals (local analogSensor_1 instead of just analogSensor_1)

W.B.
  • 5,445
  • 19
  • 29
  • @junghur It's general syntax for opening a file. For example `io.open("Test.txt", "w+b")` will open Text.txt file with write-append access in binary mode. See link to manual I provided. – W.B. Apr 25 '13 at 13:40
  • Compact date formatting! Unfortunately, `%F %T` not working. – Egor Skriptunoff Apr 25 '13 at 13:47
  • One more question!! If I say the name of file "collection_of_data.txt" and save in the system(linux), is this code automatically find that file and save it into the file? – jung hur Apr 25 '13 at 13:47
  • @EgorSkriptunoff, yes, `%F` crashed my lua interpreter :) – W.B. Apr 25 '13 at 13:50
  • what's the difference local filehandle = io.open(filename[, mode]) and local filehandle = io.open("Log.txt", "w+")? Do i need to write twice like that? – jung hur Apr 25 '13 at 15:05
  • @junghur no, you don't need to do it twice. As I explained earlier, the first one is general syntax from the manual, the second one is an actual example based on the general syntax. – W.B. Apr 25 '13 at 17:49
1

Sorry, no fractional seconds

-- Open file
local file = assert(io.open('collection_of_data.txt','wb'))

-- Write to file
local dt = os.date'*t'
local time_string = 
   dt.year..'-'..('0'..dt.month):sub(-2)..'-'..('0'..dt.day):sub(-2)..' '..
   ('0'..dt.hour):sub(-2)..':'..('0'..dt.min):sub(-2)..':'..('0'..dt.sec):sub(-2)
file:write(valueOfSensor_1, '\n', time_string, '\n')

-- Close file
file:close()
Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
  • For fractional seconds see http://stackoverflow.com/questions/463101/lua-current-time-in-milliseconds – Supr Apr 25 '13 at 13:36
  • @junghur It means to open the file for writing binary data. See http://www.lua.org/pil/21.2.html – Supr Apr 25 '13 at 13:38
  • @junghur It's write access in binary mode. See link to manual in my answer. Also, formatting can be much more easily done. – W.B. Apr 25 '13 at 13:39
  • can i use " local timestr = os.date("%Y-%m-%d %H:%M:%S") " instead of your time_string? – jung hur Apr 25 '13 at 13:43
  • One more question!! If I say the name of file "collection_of_data.txt" and save in the system(linux), is this code automatically find that file and save it into the file? – jung hur Apr 25 '13 at 13:47
  • @junghur - You should specify full path if your file is located outside current directory. – Egor Skriptunoff Apr 25 '13 at 13:48
  • @junghur - Yes, it is better to take `timestr` from W.B.'s code – Egor Skriptunoff Apr 25 '13 at 13:49