安卓openclaw创建智能花园控制器(基础)
🌱 用一部安卓手机,打造你的智能花园控制器(OpenClaw 实战)
今天分享一个我亲测可用的方案: 👉 用 OpenClaw + Android 节点,搭建一个低成本“智能花园控制器”。
🧠 一、这个系统能做什么?
我把它分成四个核心能力:
📸 1. 每天自动拍照(植物生长记录)
-
定时调用摄像头 -
自动保存 JPG -
形成植物“成长时间轴”
👉 再也不用手动拍照记录了
💨 2. 风力监测(用手机传感器)
-
使用加速度计(accelerometer) -
检测环境抖动 -
模拟风力变化
👉 能判断:
-
是否有大风 -
是否需要加固植物
📍 3. GPS定位(环境标记)
-
自动记录经纬度 -
标记采集位置 -
绑定时间戳
👉 适合:
-
多花园管理 -
户外环境监测
🌦️ 4. 天气 API 融合(关键升级)
通过天气接口获取:
-
🌡️ 温度 -
💨 实际风速 -
🌧️ 天气情况
👉 实现: “本地传感器 + 云端天气”双重感知
⚙️ 二、系统是怎么工作的?
整体结构非常简单:
安卓手机(OpenClaw Node) ↓执行命令(camera / sensor / location) ↓Windows 脚本(PowerShell) ↓保存数据(图片 + CSV) ↓定时任务自动运行
一句话总结:
👉 手机负责感知,电脑负责决策
🛠️ 三、核心实现(关键代码)
📸 拍照
openclaw nodes invoke --node <nodeId> --command "camera.snap"
💨 读取风力(加速度)
openclaw nodes invoke --node <nodeId> --command sensor.read
📍 获取位置
openclaw nodes location get --node <nodeId>
🌦️ 获取天气
$weather = Invoke-RestMethod "https://api.openweathermap.org/data/2.5/weather?lat=$lat&lon=$lon&appid=APIKEY&units=metric"
📊 四、系统每天会生成什么?
每次运行后:
📁 自动生成:
-
📸 植物照片(JPG) -
📄 数据记录(CSV)
📋 示例日报:
🌱 智能花园监控 v3.0 - 2026-04-10 13:24:28📸 拍照保存...💨 风力监测...📍 获取位置...📋 === 花园日报 ===📸 照片: photo_20260410_132433.jpg💨 风速: 9.04m/s 🌤️ 适宜生长📍 位置: 20.0268°N, 110.3385°E 精度: 40m🌡️ 建议: ✅ 正常维护💾 数据保存: C:\Users\mixintu\GardenMonitor\GardenReport_20260410.csvTimestamp : 2026-04-10 13:24:36PhotoFile : photo_20260410_132433.jpgPhotoPath : C:\Users\mixintu\GardenMonitor\photo_20260410_132433.jpgWindSpeed : 9.04Latitude : 20.026827Longitude : 110.338497Accuracy : 40Advice : 正常维护Status : 完整
📋 示例代码:
# ============================================# 🌱 智能花园终极监控器 v3.0 - 完整生产版# ✅ 拍照保存JPG + CSV数据 + 定时任务 + 路径标准化# ============================================# 1. 标准化部署目录$DataDir = "$env:USERPROFILE\GardenMonitor"if (-not (Test-Path $DataDir)) { New-Item -Path $DataDir -ItemType Directory -Force | Out-Null Write-Host "📁 数据目录: $DataDir" -ForegroundColor Green}# 2. 拍照保存函数(已验证成功)function Snap {$NodeId = "7f33ceeecec2442f54da33db90ed210335ae6d3aa3cd109d2b40d802292fb049"# 使用 --json 参数获取纯JSON(避免装饰文本)$photo = openclaw nodes invoke --node $NodeId --command "camera.snap" --json | ConvertFrom-Jsonif ($photo.error) { Write-Error "拍照失败: $($photo.error)"return $null }# 清理base64前缀并保存$base64String = $photo.payload.base64 -replace '^data:image/\w+;base64,', ''$imageBytes = [System.Convert]::FromBase64String($base64String)$outputPath = "$DataDir\photo_$(Get-Date -Format 'yyyyMMdd_HHmmss').jpg" [System.IO.File]::WriteAllBytes($outputPath, $imageBytes)# Write-Host "📸 保存: $outputPath" -ForegroundColor Greenreturn $outputPath}# 3. 完整花园监控函数function GardenMonitor { param([string]$DataDir = "$env:USERPROFILE\GardenMonitor")$CsvPath = "$DataDir\GardenReport_$(Get-Date -Format 'yyyyMMdd').csv"$nodeId = "7f33ceeecec2442f54da33db90ed210335ae6d3aa3cd109d2b40d802292fb049" Write-Host "`n🌱 智能花园监控 v3.0 - $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" -ForegroundColor Green# 确保目录存在if (-not (Test-Path $DataDir)) { New-Item -Path $DataDir -ItemType Directory -Force | Out-Null }# 提取 JSON 函数(已验证完美)function Get-OpenClawJson { param($Command) try {$output = openclaw nodes invoke --node $nodeId --command $Command$jsonMatch = [regex]::Match($output, '\{.*\}')if ($jsonMatch.Success) {return $jsonMatch.Value | ConvertFrom-Json } } catch { Write-Warning "命令失败: $Command" }return $null }# 1. 拍照并保存JPG Write-Host "📸 拍照保存..." -ForegroundColor Yellow openclaw nodes invoke --node $nodeId --command "flash.on" | Out-Null Start-Sleep 1$photoPath = Snap openclaw nodes invoke --node $nodeId --command "flash.off" | Out-Null# 2. 传感器数据(正则提取,超稳定) Write-Host "💨 风力监测..." -ForegroundColor Yellow$accel = Get-OpenClawJson "sensor.read"$windSpeed = if ($accel -and $accel.payload) { [math]::Round($accel.payload.y, 2) } else { 0 }# --------------------------------------# Write-Host "💨 风力监测..." -ForegroundColor Yellow# $sensorRaw = openclaw nodes invoke --node $nodeId --command "sensor.read"# $windSpeed = if ($sensorRaw -match '"y":\s*([\d.-]+)') { # [math]::Round([double]$matches[1], 2) # } else { 0 }# 3. GPS数据 Write-Host "📍 获取位置..." -ForegroundColor Yellow$gps = Get-OpenClawJson "location.get"$lat = if($gps -and $gps.payload){$gps.payload.lat}else{"N/A"}$lng = if($gps -and $gps.payload){$gps.payload.lng}else{"N/A"}# --------------------------------------# Write-Host "📍 GPS定位..." -ForegroundColor Yellow# $gpsRaw = openclaw nodes invoke --node $nodeId --command "location.get"# $lat = if ($gpsRaw -match '"lat":\s*([\d.-]+)') { [math]::Round([double]$matches[1], 4) } else { "N/A" }# $lng = if ($gpsRaw -match '"lng":\s*([\d.-]+)') { [math]::Round([double]$matches[2], 4) } else { "N/A" }# 4. 美化日报 Write-Host "`n📋 === 花园日报 ===" -ForegroundColor Magenta Write-Host "📸 照片: $(if($photoPath){Split-Path $photoPath -Leaf}else{'失败'})" Write-Host "💨 风速: ${windSpeed}m/s $(if($windSpeed -gt 10){'🌪️ 强风警告'}else{'🌤️ 适宜生长'})" -ForegroundColor $(if($windSpeed -gt 10){"Red"}else{"Green"})# Write-Host "📍 位置: ${lat}°N, ${lng}°E"if ($gps -and $gps.payload) { Write-Host "📍 位置: $($gps.payload.lat.ToString('F4'))°N, $($gps.payload.lng.ToString('F4'))°E" Write-Host " 精度: $($gps.payload.accuracy)m" } else { Write-Host "📍 位置: 获取失败" } Write-Host "🌡️ 建议: $(if($windSpeed -gt 12){'🚨 加固支架'}elseif($windSpeed -lt 5){'💧 浇水施肥'}else{'✅ 正常维护'})"# 5. CSV永久保存$report = [PSCustomObject]@{ Timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss' PhotoFile = if($photoPath){Split-Path $photoPath -Leaf}else{'N/A'} PhotoPath = $photoPath WindSpeed = $windSpeed Latitude = $lat Longitude = $lng Accuracy = if($gps -and $gps.payload){$gps.payload.accuracy}else{"N/A"} Advice = if($windSpeed -gt 12){"强风警告"}elseif($windSpeed -lt 5){"适宜浇水"}else{"正常维护"} Status = if($photoPath -and $lat -ne "N/A"){"完整"}else{"部分失败"} }$csvHeaders = "Timestamp,PhotoFile,PhotoPath,WindSpeed,Latitude,Longitude,Accuracy,Advice,Status"if (-not (Test-Path $CsvPath)) {$csvHeaders | Out-File $CsvPath -Encoding UTF8 }$report | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | Out-File $CsvPath -Append -Encoding UTF8 Write-Host "💾 数据保存: $CsvPath" -ForegroundColor Greenreturn $report}# 测试运行# Write-Host "`n🧪 部署测试运行..." -ForegroundColor Cyan# GardenMonitor# 6. 一键永久部署$DataDir = "$env:USERPROFILE\GardenMonitor"$DeployScript = @"# 需要部署的完整函数function Snap { ${function:Snap} }function GardenMonitor { ${function:GardenMonitor} }GardenMonitor"@# 保存部署包$DeployScript | Out-File "$DataDir\GardenMonitor.ps1" -Encoding UTF8Write-Host "✅ 永久保存: $DataDir\GardenMonitor.ps1" -ForegroundColor Green# 部署定时任务# schtasks /delete /tn "GardenMonitor" /f 2>$null# schtasks /create /tn "GardenMonitor" /tr "powershell -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$DataDir\GardenMonitor.ps1`" " /sc daily /st 08:00 /st 18:00 /ru $env:USERNAME# 正确的两个计划任务(早/晚分别一个)schtasks /delete /tn "GardenMonitor-AM" /f 2>$nullschtasks /delete /tn "GardenMonitor-PM" /f 2>$nullschtasks /create /tn "GardenMonitor-AM" /tr "powershell -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$DataDir\GardenMonitor.ps1`"" /sc daily /st 08:00 /ru $env:USERNAMEschtasks /create /tn "GardenMonitor-PM" /tr "powershell -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$DataDir\GardenMonitor.ps1`"" /sc daily /st 18:00 /ru $env:USERNAMEWrite-Host "✅ 已保存到: $DataDir\GardenMonitor.ps1" -ForegroundColor GreenWrite-Host "🕒 已创建两个计划任务: GardenMonitor-AM / GardenMonitor-PM" -ForegroundColor Green# 创建快捷方式$WshShell = New-Object -comObject WScript.Shell$Shortcut = $WshShell.CreateShortcut("$DataDir\花园监控器.lnk")$Shortcut.TargetPath = "powershell.exe"$Shortcut.Arguments = "-ExecutionPolicy Bypass -File `"$DataDir\GardenMonitor.ps1`""$Shortcut.Save()Write-Host "🔗 快捷方式: $DataDir\花园监控器.lnk" -ForegroundColor Green# 测试运行Write-Host "`n🧪 部署测试运行..." -ForegroundColor CyanGardenMonitorWrite-Host "`n🏆 === 终极部署完成 ===" -ForegroundColor MagentaWrite-Host "📁 数据目录: $DataDir"# Write-Host "📸 照片保存: 桌面JPG文件"# Write-Host "📊 数据记录: CSV永久存储"# Write-Host "🕒 定时任务: 08:00/18:00 自动运行"Write-Host "🎉 智能花园系统已进入全自动生产模式!" -ForegroundColor Green# 打开结果目录explorer $DataDir
🚀 五、为什么这个方案很强?
✅ 成本极低
-
不需要树莓派 -
不需要额外硬件 -
一部旧手机就够
✅ 功能完整
你已经拥有:
-
视觉(camera) -
感知(sensor) -
定位(GPS) -
网络(API)
👉 这其实就是一个完整的 IoT 节点
✅ 可扩展性极强
你可以继续加:
-
🤖 AI识别植物健康 -
💧 自动浇水系统 -
📈 数据可视化面板 -
🔔 异常报警(强风/高温)
🧠 六、一些实战经验(避坑)
⚠️ 1. 不是所有传感器都能用
比如:
-
gyroscope ❌ -
barometer ❌
👉 实测只有:
-
accelerometer ✅ -
magnetometer ✅
⚠️ 2. 风速不是真实风速
👉 本质是“震动程度”
建议:
-
做多次采样平均 -
再和天气 API 对比
⚠️ 3. 权限问题很关键
例如:
-
screen.record ❌(用户拒绝) -
canvas ❌(Android 不支持)
🌍 七、这套系统的本质
这不是一个“脚本”,而是:
🧠 一个边缘计算 + 物联网系统(Edge AI IoT)
它具备:
-
感知(手机) -
计算(脚本) -
存储(CSV) -
调度(定时任务)
🏁 八、结尾
如果你家里有一部闲置安卓手机, 别让它吃灰了。
给它装上 OpenClaw, 它就能变成:
🌱 你的专属“智能花园管家”
如果你想进阶:
👉 下一步可以做:
-
自动浇水系统 -
AI植物识别 -
Web可视化面板
💬 欢迎留言交流,一起把“低成本智能硬件”玩出花来。
夜雨聆风