diff --git a/main.py b/main.py index 065d5e8..082a245 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,8 @@ # These two modules allow us to run a web server. from flask import Flask, render_template from flask_socketio import SocketIO +from tfluna import TFLuna + # This module lets us pick random numbers, you can remove it later. import random @@ -8,6 +10,14 @@ import random app = Flask(__name__) socketio = SocketIO(app) +tfluna = TFLuna() +tfluna.open() +tfluna.set_samp_rate(10) +def printLidar(): + distance, strength, temperature = tfluna.read() + lidarValues = {distance, strength, temperature} + return str(lidarValues) + # When someone requests the root page from our web server, we return 'index.html'. @app.route('/') def index(): @@ -17,7 +27,7 @@ def index(): def background_thread(): while True: # We sleep here for a single second, but this can be increased or decreased depending on how quickly you want data to be pushed to clients. - socketio.sleep(1) + socketio.sleep(0.1) # Then, we emit an event called "update_data" - but this can actually be whatever we want - with the data being a dictionary # where 'randomNumber' is set to a random number we choose here. You should replace the data being sent back with your sensor data # that you fetch from things connected to your Pi. @@ -27,6 +37,7 @@ def background_thread(): 'randomNumber': random.randint(1, 100), # you can add more here! for instance, something along the lines of: # 'mySensor': mysensor.get_sensor_data(), + 'lidar': printLidar() } ) # To add a your first new sensor, try giving https://docs.aerospacejam.org/getting-started/first-sensor a read! @@ -40,7 +51,7 @@ def handle_connect(): # This function is called def main(): # These specific arguments are required to make sure the webserver is hosted in a consistent spot, so don't change them unless you know what you're doing. - socketio.run(app, host='0.0.0.0', port=80, allow_unsafe_werkzeug=True) + socketio.run(app, host='0.0.0.0', port=8085, allow_unsafe_werkzeug=True) if __name__ == '__main__': main() diff --git a/templates/index.html b/templates/index.html index e472d4a..0ab2cc1 100644 --- a/templates/index.html +++ b/templates/index.html @@ -10,6 +10,7 @@
Random number chosen by the Pi: Loading...
+Lidar Values: Loading...