from flask import Flask, jsonify from flask_socketio import SocketIO, send, emit app = Flask(__name__) socketio = SocketIO(app) # Store the latest sensor data sensor_data = {} @app.route('/') def index(): return "WebSocket Server is running." @socketio.on('connect') def handle_connect(): print('Client connected') @socketio.on('disconnect') def handle_disconnect(): print('Client disconnected') @socketio.on('message') def handle_message(data): global sensor_data print('Received message:', data) sensor_data = data emit('response', {'message': 'Data received'}) @app.route('/data') def get_data(): global sensor_data return jsonify(sensor_data) if __name__ == '__main__': app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))