ESP32与Laravel项目通信遇404错误,请求技术协助
问题分析与解决方案
核心问题
Laravel项目本身运行正常,但ESP32请求Laravel的指定URL时返回404,原因是路由不匹配+通信逻辑颠倒:ESP32作为客户端主动拉取命令,但Laravel未提供对应路由;同时原Laravel代码错误地尝试向ESP32发送POST请求(ESP32未做服务器监听)。
1. 修复Laravel路由与命令存储逻辑
新增路由
在routes/web.php中添加ESP32拉取命令的GET路由:
// 前端提交命令的路由(保留原路由) Route::post('/irrigation_control/esp32/action', [IrrigationControlController::class, 'controlSystem'])->name('irrigation.esp32'); // ESP32拉取命令的路由 Route::get('/irrigation_control/esp32/command', [IrrigationControlController::class, 'getCommand']);
修改控制器代码
更新controlSystem方法,将命令存入缓存(替代原错误的POST请求逻辑):
use Illuminate\Support\Facades\Cache; public function controlSystem(Request $request) { $action = $request->input('command'); // 将命令存入缓存,有效期1分钟 Cache::put('irrigation_command', $action, 60); $message = 'System ' . ($action === 'activate' ? 'activated!' : 'deactivated!'); return redirect()->route('irrigation.index')->with('message', $message); } // 新增方法:返回ESP32需要的命令 public function getCommand() { $command = Cache::get('irrigation_command', ''); // 返回后清空缓存,避免重复执行 Cache::forget('irrigation_command'); return response()->json(['command' => $command]); }
2. 调整ESP32请求与解析逻辑
ESP32需解析Laravel返回的JSON数据,同时修正请求注释(原代码误写为POST):
#include <WiFi.h> #include <HTTPClient.h> #include <ArduinoJson.h> // 需要安装ArduinoJson库 const char* ssid = "<wifiname>"; const char* password = "<wifipass>"; const char* serverName = "http://<ip-address>/irrigation_control/esp32/command"; const int PIN_RELAY = 4; const int PIN_LED = 32; const int PIN_SENSOR = 35; const int dryThreshold = 3000; void setup() { Serial.begin(115200); pinMode(PIN_RELAY, OUTPUT); pinMode(PIN_LED, OUTPUT); digitalWrite(PIN_RELAY, LOW); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { digitalWrite(PIN_LED, LOW); Serial.print("Connecting to Wifi..."); delay(1000); } digitalWrite(PIN_LED, HIGH); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void loop(){ if (WiFi.status() == WL_CONNECTED){ HTTPClient http; http.begin(serverName); http.addHeader("Content-Type", "application/json"); int httpResponseCode = http.GET(); if (httpResponseCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println("Received payload: " + payload); // 解析JSON数据 DynamicJsonDocument doc(1024); deserializeJson(doc, payload); String command = doc["command"].as<String>(); Serial.println("Command received: " + command); if (command == "activate") { int moistureLevel = analogRead(PIN_SENSOR); Serial.print("Soil Moisture Level: "); Serial.println(moistureLevel); if (moistureLevel > dryThreshold) { digitalWrite(PIN_RELAY, HIGH); Serial.println("Soil is dry. Activating relay."); } else { digitalWrite(PIN_RELAY, LOW); Serial.println("Soil is moist. Relay is off."); } } } else { Serial.print("Error on GET request: "); Serial.println(httpResponseCode); } http.end(); } else { digitalWrite(PIN_LED, LOW); delay(500); digitalWrite(PIN_LED, HIGH); delay(500); Serial.println("Wifi Disconnected"); } delay(10000); }
3. 确认XAMPP配置正确性
因为Laravel在htdocs外,需确保Apache配置正确:
- 启用
mod_rewrite(修改httpd.conf,去掉#LoadModule rewrite_module modules/mod_rewrite.so前的注释) - 配置虚拟主机,指向Laravel的
public目录:
<VirtualHost *:80> ServerName irrigation.local DocumentRoot "C:/path/to/your/laravel/public" <Directory "C:/path/to/your/laravel/public"> AllowOverride All Require all granted </Directory> </VirtualHost>
- 修改系统
hosts文件,添加127.0.0.1 irrigation.local映射
内容的提问来源于stack exchange,提问作者mona molina
相关产品推荐
相关产品推荐

