jung hur
jung hur

Reputation: 91

Lua script - Coding a scenario

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()

Upvotes: 0

Views: 180

Answers (2)

Ray Osgod
Ray Osgod

Reputation: 64

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.

Upvotes: 1

moteus
moteus

Reputation: 2235

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.

Upvotes: 1

Related Questions