# Robot Connection Guide

## Connecting to Your Robot via WiFi or Bluetooth

The Agriculture Robot Control System supports direct connections to your robot via WiFi and Bluetooth.

## WiFi Connection

### Requirements
- Robot must be on the same network as your device
- Robot must have WiFi capability (ESP32, Raspberry Pi, etc.)
- Know the robot's IP address

### Steps to Connect

1. **Go to Robot Connection Page**
   - Click "Robot Connect" in the sidebar
   - Or navigate to `/robot-connection`

2. **Click "Connect via WiFi"**

3. **Enter Robot Information:**
   - Robot Name: Give it a name (e.g., "Field Robot 1")
   - IP Address: Your robot's IP (e.g., `192.168.1.100`)
   - Port: Robot's communication port (usually `8080`)

4. **Click "Connect"**

5. **Wait for Connection**
   - Status will show "connecting" then "connected"
   - You'll see signal strength and battery level

### Robot WiFi Setup (Example - ESP32)

```cpp
#include <WiFi.h>

const char* ssid = "YourWiFi";
const char* password = "YourPassword";
const int port = 8080;

WiFiServer server(port);

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  
  Serial.print("Robot IP: ");
  Serial.println(WiFi.localIP());
  
  server.begin();
}

void loop() {
  WiFiClient client = server.available();
  if (client) {
    // Handle commands from app
    while (client.connected()) {
      if (client.available()) {
        String command = client.readStringUntil('\n');
        // Process command and send response
        client.println("OK");
      }
    }
    client.stop();
  }
}
```

## Bluetooth Connection

### Requirements
- Robot must have Bluetooth capability
- Device must support Bluetooth
- Know the robot's Bluetooth device ID/MAC address (optional)

### Steps to Connect

1. **Go to Robot Connection Page**

2. **Click "Connect via Bluetooth"**

3. **Enter Robot Information:**
   - Robot Name: Give it a name
   - Device ID: Bluetooth MAC address (optional - can scan)

4. **Click "Connect"**

5. **If Device ID is empty:**
   - Click "Scan Bluetooth" to find available robots
   - Select from the list of found robots

### Robot Bluetooth Setup (Example - ESP32)

```cpp
#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("AgriBot-001"); // Bluetooth device name
  
  Serial.println("Bluetooth started, ready to pair!");
}

void loop() {
  if (SerialBT.available()) {
    String command = SerialBT.readString();
    // Process command
    SerialBT.println("OK");
  }
  
  // Send sensor data
  SerialBT.println("{\"battery\":85,\"temperature\":25.5}");
  delay(2000);
}
```

## Scanning for Robots

### WiFi Scan
- Click "Scan WiFi" button
- System will search for robots on your network
- Found robots will appear in a list
- Click "Connect" on any robot to connect

### Bluetooth Scan
- Click "Scan Bluetooth" button
- System will search for nearby Bluetooth devices
- Found robots will appear in a list
- Click "Connect" on any robot to connect

## Connection Status

Once connected, you'll see:
- **Connection Status**: Connected/Disconnected/Connecting
- **Signal Strength**: WiFi/Bluetooth signal quality (0-100%)
- **Robot Battery**: Battery level of the robot
- **Last Seen**: Last communication timestamp

## Sending Commands

Once connected, you can:
- Control robot movement
- Send operation commands
- Receive real-time sensor data
- View live camera feed

## Troubleshooting

### WiFi Connection Issues
- Verify robot and device are on same network
- Check robot IP address is correct
- Ensure firewall allows connections
- Verify robot's WiFi server is running

### Bluetooth Connection Issues
- Ensure Bluetooth is enabled on your device
- Check robot Bluetooth is powered on
- Try scanning for devices first
- Some browsers don't support Web Bluetooth - use mobile app or desktop app

### Connection Drops
- Check signal strength (should be > 50%)
- Verify robot is still powered on
- Check network connectivity
- Reconnect if needed

## API Endpoints

### Connect via WiFi
```
POST /api/robot-connection/connect/wifi
{
  "name": "My Robot",
  "ipAddress": "192.168.1.100",
  "port": 8080
}
```

### Connect via Bluetooth
```
POST /api/robot-connection/connect/bluetooth
{
  "name": "My Robot",
  "deviceId": "00:11:22:33:44:55"
}
```

### Scan for Robots
```
POST /api/robot-connection/scan
{
  "protocol": "wifi" // or "bluetooth"
}
```

### Send Command
```
POST /api/robot-connection/command/{connectionId}
{
  "command": "move",
  "params": {
    "direction": "forward",
    "speed": 50
  }
}
```

## WebSocket Events

When connected, you'll receive real-time updates:
- `robot_connection_status`: Connection status changes
- `robot_data`: Real-time robot data (battery, sensors, etc.)

## Next Steps

After connecting:
1. Go to "Manual Control" page for game-style control
2. Use the joystick and buttons to move the robot
3. View live camera feed from the robot
4. Monitor robot status in real-time



