< >
Home » Farmbot开发入门教程 » farmbot开发入门教程-根据天气情况选择浇水

farmbot开发入门教程-根据天气情况选择浇水

说明:

  • 介绍根据天气和每种植物的需求获取今日预报,以便更有效地浇水

  • 在此示例中,我们将根据您的 FarmBot 的经纬度从外部服务获取今天的天气预报。然后,我们将根据植物的年龄和浇水曲线以及植物根区预计的降雨量来确定是否应该浇水

步骤 1:添加植物变量

  • 向序列添加一个名为“Plant”的位置变量。将变量设置为“外部定义”,默认值设置为测试工厂。测试后,您应该将默认值设置为“None”。

步骤 2:确保植物有浇水曲线

  • 确保所有要用于此序列的植物都已指定浇水曲线。如果未设置植物的浇水曲线water_curve_id,则序列将完全跳过对该植物的浇水。

步骤3:添加Lua代码

  • 添加Lua命令,代码如下:
plant = variable("Plant")
root_area = 3.14 * plant.radius^2

-- Get today's precipitation forecast from Open Meteo
request_url =
    "https://api.open-meteo.com/v1/forecast" ..
    "?latitude=" .. get_device("lat") ..
    "&longitude=" .. get_device("lng") ..
    "&timezone=auto&daily=precipitation_sum"
debug("Getting weather from " .. request_url)
api_response = http({url=request_url})
forecast = json.decode(api_response.body)
rain_mm = forecast.daily.precipitation_sum[1]

-- Calculate the volume of water the plant will get from the rain
rain_ml = math.floor(root_area * rain_mm / 1000)
debug("Forecasting " .. rain_mm .. "mm of rain today; plant's roots can expect " .. rain_ml .. "mL")

-- Get plant's water curve and water needs in mL based on plant's age
local water_curve, water_needs_ml
if plant.water_curve_id then
    water_curve = get_curve(plant.water_curve_id)
    water_needs_ml = water_curve.day(plant.age)
    debug("Plant needs " .. water_needs_ml .. "mL of water today")
else
    toast("Plant has no assigned water curve; skipping", "warn")
    return
end

-- Determine how much FarmBot should water
water_deficit = water_needs_ml - rain_ml

-- Move to and water the plant, or don't
if water_deficit > 0 then
    toast("Watering the deficit amount of " .. water_deficit .. "mL.")
    move{ x = plant.x, y = plant.y, z = safe_z() }
    dispense(water_deficit)
else
    toast("Rain will provide enough water for this plant today; skipping")
end

步骤 4:制作属于你自己的序列

  • 以下是修改序列的一些想法:

    • 添加一个循环来给花园里的所有植物浇水,而只需要获取一次天气预报。
    • 如果植物没有指定浇水曲线,请添加后备浇水量。
    • 探索Open-Meteo API的其他功能,以利用更多天气数据。例如,您可以获取温度预报,并在天气非常炎热时使用它来增加浇水量。

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: none