Skip to content

Transforme tablets Android antigos em servidores de telemetria IoT. Monitoramento em tempo real via WebSocket com Spring Boot e serviço Android headless.

License

Notifications You must be signed in to change notification settings

vitinh0z/ServerWithTelemetrics

Repository files navigation

📱 Tablet Monitor System - Old Device Telemetry with Spring Boot

Transform your old Android tablet into a real-time system monitoring server with WebSocket connectivity

Java Spring Boot Android License

📋 Table of Contents

🎯 Overview

This project transforms an old Android tablet (like Samsung Galaxy Tab 2 GT-P3110) into a real-time system monitoring server. The tablet collects telemetry data (CPU usage, RAM, battery level, temperature) and streams it via WebSocket to a Spring Boot server, which then broadcasts the data to web clients in real-time.

Perfect for:

  • 🔄 Repurposing old Android devices
  • 📊 Real-time system monitoring dashboards
  • 🎓 Learning WebSocket communication
  • 🏗️ IoT and telemetry projects

✨ Features

Android Client (Tablet)

  • Headless Service - Runs in background without UI to save RAM
  • Real-time Metrics Collection - CPU, RAM, Battery, Temperature
  • WebSocket Server - Built-in NanoWSD server on port 8081
  • Root Access Support - Enhanced temperature monitoring with root
  • Auto-reconnection - Resilient connection handling
  • Remote Commands - Stress testing capabilities (CPU stress, RAM fill/clear)

Spring Boot Server

  • WebSocket Bridge - Connects tablet to web clients
  • Auto-reconnection - Automatic reconnection to tablet on disconnect
  • Multi-client Support - Broadcast metrics to multiple web clients
  • Real-time Streaming - 5-second update intervals
  • REST API Ready - Easy integration with frontend applications

🏗️ Architecture

        ┌─────────────────┐         WebSocket          ┌──────────────────┐         WebSocket        ┌─────────────────┐
        │  Old Android    │        (Port 8081)         │   Spring Boot    │       (Port 8080)        │   Web Clients   │
        │    Tablet       │ ────────────────────────>  │     Server       │ ─────────────────────>   │   (Frontend)    │
        │  (T110_Server)  │   Telemetry Data JSON      │  (Bridge/Relay)  │   Broadcast Metrics      │   Dashboard     │
        └─────────────────┘                            └──────────────────┘                          └─────────────────┘
                │                                                                                             │
                │ Collects:                                                                                   │ Displays:
                │ - CPU Usage                                                                                 │ - Real-time Charts
                │ - RAM Available                                                                             │ - System Status
                │ - Battery %                                                                                 │ - Alerts
                │ - Temperature                                                                               │ - Historical Data
                └─────────────────────────────────────────────────────────────────────────────────────────────┘

Data Flow

  1. Tablet Monitoring Service collects system metrics every 5 seconds
  2. NanoWSD WebSocket Server (on tablet) sends JSON telemetry to Spring Boot server
  3. Spring Boot WebSocket Client receives data from tablet
  4. Spring Boot WebSocket Server broadcasts data to all connected web clients
  5. Web Frontend displays real-time metrics and allows remote commands

🛠️ Technologies

Backend (Spring Boot Server)

Technology Version Purpose
Spring Boot 3.5.7 Main application framework
Spring WebSocket 3.5.7 WebSocket support for bi-directional communication
Spring Web 3.5.7 REST API endpoints
Lombok Latest Reduce boilerplate code
Java 17 Programming language
Maven 3.x Dependency management

Android Client (Tablet)

Technology Version Purpose
Android SDK API 21+ Android platform
NanoHTTPD/NanoWSD Latest Lightweight WebSocket server
Java 8+ Programming language
Gradle 8.x Build tool

💻 System Requirements

For Spring Boot Server

✓ Java 17 or higher
✓ Maven 3.6+ or Gradle 7+
✓ 512 MB RAM minimum
✓ Network connectivity to tablet

For Android Tablet

✓ Android 5.0 (API 21) or higher
✓ WiFi connectivity
✓ Root access (optional, for enhanced temperature monitoring)
✓ 256 MB RAM minimum
✓ Static IP address recommended

📦 Installation

1. Clone the Repository

git clone https://github.com/vitinh0z/ServerWIthOldTabletAndTelemetricWithSpringBoot.git
cd ServerWIthOldTabletAndTelemetricWithSpringBoot

2. Setup Spring Boot Server

cd TabletServer

# Build with Maven
./mvnw clean install

# Or use Maven Wrapper on Windows
mvnw.cmd clean install

3. Configure Tablet IP Address

Edit TabletServer/src/main/java/io/github/vitinh0z/TabletServer/TabletClientConnector.java:

private static final String TABLET_SERVER_URI = "ws://192.168.15.4:8081";
//                                                    ^^^^^^^^^^^^^^
//                                                    Update with your tablet's IP

4. Setup Android Client

cd MonitorServiceClient

# Build APK
./gradlew assembleDebug

# Or on Windows
gradlew.bat assembleDebug

Install the APK on your tablet:

adb install app/build/outputs/apk/debug/app-debug.apk

🚀 Usage

Starting the System

Step 1: Start the Android Tablet Service

  1. Install and launch the app on your tablet
  2. The service starts automatically and runs in the background
  3. Note the tablet's IP address (Settings → WiFi → Your Network)
  4. Verify WebSocket server is running on port 8081

Step 2: Start the Spring Boot Server

cd TabletServer

# Run with Maven
./mvnw spring-boot:run

# Or run the JAR
java -jar target/TabletServer-0.0.1-SNAPSHOT.jar

Server will start on http://localhost:8080

Step 3: Connect Web Client

Connect to the WebSocket endpoint:

ws://localhost:8080/tablet-metrics

Verifying Connection

Check the logs:

Spring Boot Console:

CONECTADO AO SERVIDOR DO TABLET!
Métrica recebida do Tablet: {"deviceId":"T110_Servidor","cpuUsage":12,"ramAvailableMb":234,...}

Android Logcat:

adb logcat | grep TabletService

Expected output:

TabletService: Cliente Conectado (SpringBoot)
TabletService: Enviando JSON {"deviceId":"T110_Servidor",...}

📡 API Documentation

WebSocket Endpoints

Client → Spring Boot Server

Endpoint: ws://localhost:8080/tablet-metrics

Description: Web clients connect here to receive real-time metrics

Message Format (Received):

{
  "deviceId": "T110_Servidor",
  "cpuUsage": 15,
  "ramAvailableMb": 234,
  "batteryPct": 87,
  "temperatureC": 35.2,
  "timestamp": 1700234567890
}

Remote Commands (Sent):

"CMD_STRESS_CPU"    // Stress test CPU for 10 seconds
"CMD_FILL_RAM"      // Allocate 50 MB of RAM
"CMD_CLEAR_RAM"     // Release allocated RAM

Spring Boot → Tablet

Endpoint: ws://[TABLET_IP]:8081

Description: Spring Boot server connects to tablet's WebSocket server

Auto-reconnection: Yes (10-second interval on disconnect)

📁 Project Structure

ServerWIthOldTabletAndTelemetricWithSpringBoot/
│
├── TabletServer/                          # Spring Boot Server Application
│   ├── src/
│   │   ├── main/
│   │   │   ├── java/.../TabletServer/
│   │   │   │   ├── TabletServerApplication.java      # Main Spring Boot application
│   │   │   │   ├── TabletClientConnector.java        # WebSocket client to tablet
│   │   │   │   ├── config/
│   │   │   │   │   └── WebSocketConfig.java          # WebSocket configuration
│   │   │   │   ├── model/
│   │   │   │   │   └── TabletMetricsModel.java       # Metrics data model
│   │   │   │   └── websocket/
│   │   │   │       └── TabletHandlerWebSocket.java   # WebSocket handler for clients
│   │   │   └── resources/
│   │   │       └── application.properties             # Server configuration
│   │   └── test/
│   ├── pom.xml                                        # Maven dependencies
│   └── mvnw                                           # Maven wrapper
│
└── MonitorServiceClient/                  # Android Tablet Application
    ├── app/
    │   ├── src/
    │   │   ├── main/
    │   │   │   ├── java/io/github/vitinh0z/
    │   │   │   │   ├── MainActivity.java              # Launcher activity
    │   │   │   │   └── MonitorService.java            # Background monitoring service
    │   │   │   ├── AndroidManifest.xml                # App manifest
    │   │   │   └── res/                               # Android resources
    │   │   └── build.gradle.kts                       # Gradle build configuration
    │   └── proguard-rules.pro
    ├── build.gradle.kts
    └── settings.gradle.kts

📊 Monitoring Metrics

Collected Metrics

Metric Type Description Update Interval
CPU Usage Integer (%) Current CPU usage percentage (0-100) 5 seconds
RAM Available Integer (MB) Available memory in megabytes 5 seconds
Battery Level Integer (%) Battery charge percentage (0-100) 5 seconds
Temperature Float (°C) Device temperature in Celsius 5 seconds
Device ID String Unique device identifier Static
Timestamp Long Unix timestamp in milliseconds Per message

JSON Payload Example

{
  "deviceId": "T110_Servidor",
  "cpuUsage": 15,
  "ramAvailableMb": 234,
  "batteryPct": 87,
  "temperatureC": 35.2,
  "timestamp": 1700234567890
}

Metric Details

CPU Usage

  • Range: 0-100%
  • Calculation: Simulated (actual CPU monitoring requires more complex implementation)
  • Stress Mode: When stressed, reports 80-95% usage
  • Normal Mode: Reports 5-15% usage

RAM Available

  • Unit: Megabytes (MB)
  • Source: ActivityManager.getMemoryInfo()
  • Note: Shows available RAM, not total or used

Battery Percentage

  • Range: 0-100%
  • Source: Android BatteryManager
  • Accuracy: ±1%

Temperature

  • Unit: Celsius (°C)
  • Source: /sys/class/power_supply/battery/temp (requires root)
  • Precision: 0.1°C
  • Fallback: Returns 0 if unable to read

🔌 WebSocket Protocol

Connection Lifecycle

┌─────────┐                ┌──────────┐                ┌────────┐
│ Tablet  │                │  Spring  │                │ Client │
└────┬────┘                └────┬─────┘                └───┬────┘
     │                          │                          │
     │ 1. Start NanoWSD         │                          │
     │ (Port 8081)              │                          │
     │                          │                          │
     │     2. Connect           │                          │
     │<─────────────────────────│                          │
     │                          │                          │
     │ 3. Send "onOpen"         │                          │
     │─────────────────────────>│                          │
     │                          │                          │
     │ 4. Start metrics         │      5. Client connects  │
     │    (5s interval)         │<─────────────────────────│
     │                          │                          │
     │ 6. JSON metrics          │                          │
     │─────────────────────────>│ 7. Broadcast metrics     │
     │                          │─────────────────────────>│
     │                          │                          │
     │ 8. Receive command       │ 9. Send command          │
     │<─────────────────────────│<─────────────────────────│
     │                          │                          │
     │ 10. Execute & respond    │                          │
     │─────────────────────────>│─────────────────────────>│
     │                          │                          │

Error Handling

  • Connection Lost: 10-second retry interval
  • Invalid JSON: Logged and ignored
  • Command Errors: Logged without crashing service
  • Root Access Failure: Falls back to non-root methods

🐛 Troubleshooting

Common Issues

1. Spring Boot can't connect to tablet

Symptoms:

Falha ao conectar ao Tablet: Connection refused. Tentando reconectar em 10s...

Solutions:

  • ✅ Verify tablet IP address is correct in TabletClientConnector.java
  • ✅ Ensure tablet and server are on the same network
  • ✅ Check firewall settings on both devices
  • ✅ Verify NanoWSD server is running on tablet (check logcat)
  • ✅ Try pinging the tablet: ping 192.168.15.4

2. Android service not starting

Solutions:

  • ✅ Check logcat for errors: adb logcat | grep TabletService
  • ✅ Verify app has necessary permissions (INTERNET, ACCESS_NETWORK_STATE)
  • ✅ Reinstall the APK
  • ✅ Clear app data and restart

3. Temperature shows 0°C

Solutions:

  • ✅ Grant root access to the app
  • ✅ Check if temperature file exists: adb shell su -c "cat /sys/class/power_supply/battery/temp"
  • ✅ Some devices use different paths - may need code modification

4. High CPU/RAM usage on tablet

Solutions:

  • ✅ Increase update interval in MonitorService.java (change UPDATE_INTERVALO)
  • ✅ Disable stress testing features
  • ✅ Close other apps on tablet
  • ✅ Consider using a more powerful device

Debug Commands

# View Android logs
adb logcat | grep TabletService

# Check if service is running
adb shell dumpsys activity services | grep MonitorService

# Test WebSocket connectivity
wscat -c ws://192.168.15.4:8081

# View Spring Boot logs
tail -f TabletServer/logs/spring-boot.log

🎨 Example Implementation

Simple Web Client (HTML + JavaScript)

<!DOCTYPE html>
<html>
<head>
    <title>Tablet Monitor Dashboard</title>
    <style>
        .metric { padding: 10px; margin: 10px; border: 1px solid #ccc; }
        .high { background-color: #ffcccc; }
        .normal { background-color: #ccffcc; }
    </style>
</head>
<body>
    <h1>📱 Tablet System Monitor</h1>
    <div id="metrics"></div>
    
    <button onclick="sendCommand('CMD_STRESS_CPU')">🔥 Stress CPU</button>
    <button onclick="sendCommand('CMD_FILL_RAM')">📊 Fill RAM</button>
    <button onclick="sendCommand('CMD_CLEAR_RAM')">🧹 Clear RAM</button>

    <script>
        const ws = new WebSocket('ws://localhost:8080/tablet-metrics');
        
        ws.onmessage = (event) => {
            const data = JSON.parse(event.data);
            document.getElementById('metrics').innerHTML = `
                <div class="metric ${data.cpuUsage > 70 ? 'high' : 'normal'}">
                    CPU: ${data.cpuUsage}%
                </div>
                <div class="metric">RAM: ${data.ramAvailableMb} MB</div>
                <div class="metric">Battery: ${data.batteryPct}%</div>
                <div class="metric ${data.temperatureC > 40 ? 'high' : 'normal'}">
                    Temp: ${data.temperatureC}°C
                </div>
                <div class="metric">Updated: ${new Date(data.timestamp).toLocaleString()}</div>
            `;
        };
        
        function sendCommand(cmd) {
            ws.send(cmd);
        }
    </script>
</body>
</html>

React Component Example

import React, { useEffect, useState } from 'react';

function TabletMonitor() {
  const [metrics, setMetrics] = useState(null);
  const [ws, setWs] = useState(null);

  useEffect(() => {
    const websocket = new WebSocket('ws://localhost:8080/tablet-metrics');
    
    websocket.onmessage = (event) => {
      const data = JSON.parse(event.data);
      setMetrics(data);
    };
    
    setWs(websocket);
    
    return () => websocket.close();
  }, []);

  const sendCommand = (cmd) => {
    if (ws) ws.send(cmd);
  };

  if (!metrics) return <div>Connecting...</div>;

  return (
    <div className="tablet-monitor">
      <h2>📱 {metrics.deviceId}</h2>
      <div className="metrics-grid">
        <MetricCard title="CPU" value={`${metrics.cpuUsage}%`} />
        <MetricCard title="RAM" value={`${metrics.ramAvailableMb} MB`} />
        <MetricCard title="Battery" value={`${metrics.batteryPct}%`} />
        <MetricCard title="Temperature" value={`${metrics.temperatureC}°C`} />
      </div>
      <div className="controls">
        <button onClick={() => sendCommand('CMD_STRESS_CPU')}>Stress CPU</button>
        <button onClick={() => sendCommand('CMD_FILL_RAM')}>Fill RAM</button>
        <button onClick={() => sendCommand('CMD_CLEAR_RAM')}>Clear RAM</button>
      </div>
    </div>
  );
}

🎯 Use Cases

1. Home Lab Monitoring

Monitor old devices in your home lab setup, tracking system health and performance.

2. IoT Learning Project

Perfect for learning WebSocket communication, real-time data streaming, and Android services.

3. Device Repurposing

Give new life to old Android tablets that would otherwise be discarded.

4. Stress Testing

Use remote commands to stress test devices and monitor their behavior under load.

5. Educational Demonstrations

Demonstrate real-time communication, system monitoring, and multi-tier architecture.

🤝 Contributing

Contributions are welcome! Here's how you can help:

Ways to Contribute

  1. 🐛 Report Bugs - Open an issue with detailed reproduction steps
  2. 💡 Suggest Features - Share your ideas for improvements
  3. 📝 Improve Documentation - Fix typos, add examples, clarify instructions
  4. 🔧 Submit Pull Requests - Fix bugs or implement new features

Development Setup

# Fork the repository
git clone https://github.com/YOUR_USERNAME/ServerWIthOldTabletAndTelemetricWithSpringBoot.git

# Create a feature branch
git checkout -b feature/my-new-feature

# Make your changes and commit
git commit -am 'Add some feature'

# Push to your fork
git push origin feature/my-new-feature

# Create a Pull Request

Coding Standards

  • ✅ Follow existing code style and formatting
  • ✅ Add comments for complex logic
  • ✅ Write meaningful commit messages
  • ✅ Test your changes thoroughly
  • ✅ Update documentation as needed

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

👤 Author

vitinh0z

🙏 Acknowledgments

  • NanoHTTPD/NanoWSD - Lightweight Java web server
  • Spring Boot Team - Excellent framework and documentation
  • Android Open Source Project - Android platform and APIs

📞 Support

If you need help or have questions:

  1. 📖 Check the Troubleshooting section
  2. 🐛 Open an Issue
  3. 💬 Start a Discussion

⭐ Star this repository if you find it useful!

Made with ❤️ for repurposing old Android devices

About

Transforme tablets Android antigos em servidores de telemetria IoT. Monitoramento em tempo real via WebSocket com Spring Boot e serviço Android headless.

Topics

Resources

License

Contributing

Stars

Watchers

Forks