0

Situation :

There are two sensors and I want to save the data of values of each sensor in the certain file..But it's not working. I am working on linux system and the file is still empty. What's wrong with my code? any suggestion please?

my code is:

--Header file

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

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

function main()
    timer = "TIMER"
    local analogsensor_1 = "AIR_1"
    local analogsensor_2 = "AIR_2"
    local timestr = os.data("%Y-%m-%d %H:%M:%S")

    -- open the file for writing binary data
    local filehandle = io.open("collection_of_data.txt", "a")

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

        if (valueOfSensor_1 > 0 and valueOfSensor_2 > 0) then
            -- save values of sensors
            filehandle:write(timestr, " -The Value of the Sensors: ", tostring(valueOfSensor_1), tostring(valueOfSensor_2)"\n");

       end

       TIMER.sleep(timer,500)
    end

    -- close the file
    filehandle:close()

end 

print("start main")
main()
jung hur
  • 81
  • 1
  • 4
  • 11

2 Answers2

1

I do not know what this libs realy do. But this code is incorrect; 1) you do not close while statement. if in real code you close it before filehandle:close() then try call filehandle:flush() 2) you forgot comma: filehandle:write(timestr, " -The Value of the Sensors: ", tostring(valueOfSensor_1), tostring(valueOfSensor_2)"\n") (it should seay something like attemt call a number value). 3) try print out valueOfSensor_1 and valueOfSensor_2 values. May be there no data.

moteus
  • 2,187
  • 1
  • 13
  • 16
  • Thanks! I put "end" and comma. after filehandle line. but still doesnt work. :( – jung hur Apr 29 '13 at 11:53
  • and i want to stop this finish until i click "stop" button. but when the scenario is started, after few seconds, it stops automatically.. – jung hur Apr 29 '13 at 11:55
  • still error..it says "attempt to call field 'data' (a nil value) – jung hur Apr 29 '13 at 11:57
  • if you close filehandle in while then you shold reopen it in loop. Or you should flush FS buffers to see changes in file, but i do not know what tipe of concurent access you can get to this file. – moteus Apr 29 '13 at 12:12
  • Until filehandle close all data may store in FS cash. You can call filehandle:flush(). and there stil no comma `tostring(valueOfSensor_2)"\n"`. this is no syntax error' – moteus Apr 29 '13 at 12:41
  • what do you mean? then do i need to put filehandle:flush() after close filehandle? – jung hur Apr 29 '13 at 13:26
  • 1
    fh:write(data) data can store in FS cash. you can call fh:flush() to enforce FS write this data to disk. Or you can set beffering mode (see setvbuf function).But if you get error `attempt to call field 'data'` you need find string where it happen. `tostring(valueOfSensor_2)"\n"` can produce this `attempt to call a number value`. – moteus Apr 29 '13 at 14:49
  • :)))) os.data("%Y-%m-%d %H:%M:%S") -> os.date("%Y-%m-%d %H:%M:%S") – moteus Apr 29 '13 at 15:03
  • That was a sneaky one :-) Nice catch. – Ray Osgod May 03 '13 at 20:58
1

Beside the typos pointed out by @moteus, shouldn't this:

    if (valueOfSensor_1 and valueOfSensor_2 > 0) then

be like this?

    if (valueOfSensor_1 > 0 and valueOfSensor_2 > 0) then

Edit, in response to your comment to another answer:

still error..it says "attempt to call field 'data' (a nil value)

I can't be sure without the stack trace, but, most likely, something bad happens in the ANALOG_IN library code. You may not be using it properly.

try to turn this:

    valueOfSensor_1 = ANALOG_IN.readAnalogIn(analogsensor_1);
    valueOfSensor_2 = ANALOG_IN.readAnalogIn(analogsensor_2);

into this:

    success, valueOfSensor_1 = pcall(ANALOG_IN.readAnalogIn, analogsensor_1);
    if not success then 
        print("Warning: error reading the value of sensor 1:\n"..valueOfSensor_1)
        valueOfSensor_1 = 0
    end

    success, valueOfSensor_2 = pcall(ANALOG_IN.readAnalogIn, analogsensor_2);
    if not success then
        print("Warning: error reading the value of sensor 2:\n"..valueOfSensor_2)
        valueOfSensor_2 = 0
    end

If the failure in ANALOG_IN is not systematic, it will work around it. If the call fails systematically, you'll get a huge warning log, and an empty collection_of_data.txt.

Please note that ANALOG_IN is not a standard Lua library. You should check its documentation , and pay attention to the details of its usage.

Ray Osgod
  • 56
  • 3
  • I just modified like what you recommended, and I execute scenario, but after that, the file was still empty – jung hur Apr 29 '13 at 13:03
  • Do you see warning messages about the sensors? If som you're probably not using `ANALOG_IN` properly, but I don't know that library, and thus I can't help you further. – Ray Osgod Apr 29 '13 at 14:05