Realtime sync from Firebase -> Laravel (recommended)

Overview
If you store sensor data in Firebase Realtime Database (or Firestore), the recommended real-time approach is to deploy a Firebase Cloud Function that triggers on new data and POSTs the data to your Laravel API (`POST /api/sensor`). This offloads the realtime logic to Firebase and keeps your Laravel app as the canonical MySQL writer.

Secure the endpoint
- Set a shared secret in your Laravel `.env` as `FIREBASE_SYNC_SECRET=your-secret-here`.
- The Cloud Function will include that secret in the header `X-SYNC-SECRET` when POSTing to `/api/sensor`.

Example: Realtime Database (Node.js Cloud Function)

1) In your Firebase functions folder install node-fetch (or use built-in fetch on newer runtimes):

```bash
cd functions
npm install node-fetch@2
```

2) Example `index.js` function:

```js
const functions = require('firebase-functions');
const fetch = require('node-fetch');

exports.syncSensorToLaravel = functions.database
  .ref('/sensors/{pushId}')
  .onCreate(async (snapshot, context) => {
    const data = snapshot.val();
    try {
      const res = await fetch('https://YOUR_LARAVEL_HOST/api/sensor', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'X-SYNC-SECRET': 'your-secret-here'
        },
        body: JSON.stringify({
          suhu: data.suhu,
          kelembapan: data.kelembapan,
          soil: data.soil,
          image_base64: data.image_base64 // if you store base64 in Firebase
        })
      });
      const text = await res.text();
      console.log('Sync result', res.status, text);
    } catch (err) {
      console.error('Sync failed', err);
    }
  });
```

Example: Firestore trigger (if you use Firestore)

```js
exports.syncSensorToLaravel = functions.firestore
  .document('sensors/{docId}')
  .onCreate(async (snap, ctx) => {
    const data = snap.data();
    // same fetch call as above
  });
```

Notes & tips
- If images are large, consider storing image URLs (Cloud Storage) instead of base64 in Firebase; then let Laravel download or reference them.
- Ensure your Laravel app is accessible from Cloud Functions (use HTTPS public URL, or a secured endpoint with token as shown).
- You can also verify requests further by signing payloads with HMAC, or restricting Cloud Function IPs (not recommended / unreliable).

Alternative approaches
- Polling: Laravel scheduled job that reads recent entries from Firebase (using Firebase Admin SDK for PHP). Simpler but higher-latency.
- Direct write from device: configure ESP32 to POST directly to Laravel (we already have `/api/sensor`), bypassing Firebase entirely.

Next steps I can do for you
- Add `FIREBASE_SYNC_SECRET` to your `.env` and commit a .env.example entry.
- Update `SensorController` to accept signed HMAC instead of plain secret.
- Provide a ready-to-deploy Cloud Function file and deployment commands.
- Implement Firebase Admin-based polling in Laravel if you prefer no Cloud Functions.

