I recently treated myself to a Farmbot. I am currently setting up the sequences. I would first like to determine the measured moisture value for each plant group and then irrigate. I found a tip in the forum that this can be easily implemented using a LUA script. I use the following for this
– Mount Soil Sensor Tool
– soil_sensor_tool = variable(“Soil Sensor Tool”)
– mount_tool(soil_sensor_tool)
– Carrots
– Move to Measure Point
measure_point_carrots = variable(“Measure Point Carrots”)
move{
x = measure_point_carrots.x,
y = measure_point_carrots.y,
z = measure_point_carrots.z,
safe_z = true
}
– Measure Soil Moisture
soil_sensor_pin = 59
soil_moisture_carrots = read_pin(soil_sensor_pin, “analog”)
– Unmount Soil Moisture tool
dismount_tool()
– Check if Moisture is below threshold - Over all
moisture_threshold = 500
if soil_moisture_carrots < moisture_threshold then
– Mount Water Tool
wait(5000)
water_tool = variable(“Water Tool”)
mount_tool(water_tool)
– Iterate over plants in group Carrots
for i,member in ipairs(group(variable(“Group ID Carrots”))) do
– Get plant object
plant = api({
method = “get”,
url = “/api/points/” … member
})
– Move to plant and water
move_absolute(plant.x, plant.y, 0)
water(plant)
end
– Unmount Water Tool
dismount_tool()
end
Unfortunately, the sequence always returns the error that I have not yet planted the plant.
The “plant” feature is described in more detail in this documentation
I have also already executed this shared sequence The FarmBot Web App which should update all plants.
Unfortunately no success. It still returns the error that the plant has not yet been planted.
I have now rebuilt the LUA script and use “dispense” instead of “water(plant)”.
However, my aim is for the Farmbot to create a soil moisture reading for each group of plants and use this as a basis for deciding whether or not to water a group.
I have extended the script from the forum to measure three plant groups. This works well so far but …
It only waters the first plant in the group and then jumps to the next group. It seems as if the loop from the script is not working properly.
if soil_moisture_1 < moisture_threshold_all then
for i, member in ipairs(group(variable(“Group ID #1”))) do
plant = api({
method = “get”,
url = “/api/points/” … member
})
move_absolute(plant.x, plant.y, 0)
dispense1 = variable(“Dispense #1”)
dispense(dispense1)
end
end
@roryaronson You had provided the LUA code in the forum. Perhaps you have an idea what I am doing wrong here?
Looking at the code, the Lua water() function requires that a Plant object has an age property. I’m not sure how this property comes into being. Likely a coding bug / change of idea.
May I suggest you email support@farm.bot or PM Rory if it’s a show-stopper for you.
@Christian thanks for bringing this up and providing so many details. There seems to be an issue where your plants might not have an age (even though the frontend is showing they do) which might be a bug with the migration script from May 9th. I am still looking into it all now. And just for a sanity check, could you try rebooting your FarmBot to rule out any issues where there could have been a synchronization failure?
The water() function requires the plant to have an age so that it can get the watering amount from the water curve assigned to the plant. Here is the code in FarmBot OS where this check is performed, and where it is failing with the “has not been planted yet” message.
@roryaronson@Christian
What I was trying to say in the previous reply was that age is not in the DB schema for a Point (plant), so that Lua code in water() should not expect to look for it directly ! That’s a bug.
edit
My missed steak . . water() is using the resource object returned from the API not the local DB, but if age is a “computed attribute” then the API is not providing it
– Iterate over plants in group
for i,member in ipairs(group(96308)) do
– Get plant object
plant = api({
method = “get”,
url = “/api/points/” … member
})
– Move to plant and water
move_absolute(plant.x, plant.y, 0)
water(plant)
end"
it returns that the plant has not yet been planted.
Thank you @jsimmonds and @Christian for the help in debugging this! Indeed the problem was that if the plant object was fetched from the local DB with variable(“Plant”) then FarmBot OS would compute the age field as needed. If the plant object was fetched from the API with api() then the age field was neither present nor being computed.
In FBOS v15.4.8 which @Gabriel just released, the water() function will now compute the age of the plant based on the planted_at date even if it was fetched from the API.
@Christian there is however one other issue with your Lua code. If you run this:
for i, member in ipairs(group(123)) do
stuff
end
Then the loop will only execute for the first member in the group.
Instead, you should assign the group to a variable first:
local group_members = group(123)
for i, member in ipairs(group_members) do
stuff
end