Inital Commit
This commit is contained in:
commit
c0d3bd7f60
5 changed files with 4473 additions and 0 deletions
3
README.md
Normal file
3
README.md
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# Aerospace Jam Template
|
||||
|
||||
Write your README here!
|
||||
46
main.py
Normal file
46
main.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
# These two modules allow us to run a web server.
|
||||
from flask import Flask, render_template
|
||||
from flask_socketio import SocketIO
|
||||
# This module lets us pick random numbers, you can remove it later.
|
||||
import random
|
||||
|
||||
# Here, we create the neccesary base app. You don't need to worry about this.
|
||||
app = Flask(__name__)
|
||||
socketio = SocketIO(app)
|
||||
|
||||
# When someone requests the root page from our web server, we return 'index.html'.
|
||||
@app.route('/')
|
||||
def index():
|
||||
return render_template('index.html')
|
||||
|
||||
# This function runs in the background to transmit data to connected clients.
|
||||
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)
|
||||
# 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.
|
||||
socketio.emit(
|
||||
'update_data',
|
||||
{
|
||||
'randomNumber': random.randint(1, 100),
|
||||
# you can add more here! for instance, something along the lines of:
|
||||
# 'mySensor': mysensor.get_sensor_data(),
|
||||
}
|
||||
)
|
||||
# To add a your first new sensor, try giving https://docs.aerospacejam.org/getting-started/first-sensor a read!
|
||||
|
||||
# This function runs when someone connects to the server - and all we do is start the background thread to update the data.
|
||||
@socketio.on('connect')
|
||||
def handle_connect():
|
||||
print('Client connected')
|
||||
socketio.start_background_task(target=background_thread)
|
||||
|
||||
# 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)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
7
pyproject.toml
Normal file
7
pyproject.toml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
[project]
|
||||
name = "teamcode"
|
||||
version = "0.1.0"
|
||||
description = "Aerospace Jam example project"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.11"
|
||||
dependencies = []
|
||||
4385
static/js/socket.io.js
Normal file
4385
static/js/socket.io.js
Normal file
File diff suppressed because it is too large
Load diff
32
templates/index.html
Normal file
32
templates/index.html
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<!-- This sets your page title. You can change this to whatever you want! -->
|
||||
<title>Aerospace Jam Example</title>
|
||||
<!-- The below script tag is used to load socket.io from the server. -->
|
||||
<script src="{{ url_for('static', filename='js/socket.io.js') }}"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Aerospace Jam Example</h1>
|
||||
<!-- Notice how the span that contains the placeholder value has an ID. We reference this ID later in the script, and it lets us pick this element in specific. -->
|
||||
<p><b>Random number chosen by the Pi: </b> <span id="randomNumber">Loading...</span></p>
|
||||
<script>
|
||||
// This function wrapper (addEventListener) makes the inner contents run only when the DOM (Document Object Model) content is loaded.
|
||||
// In simpler terms, this runs once the page has finished loading.
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
// Then, we connect to our webserver using two special variables - you don't need to worry about this either.
|
||||
var socket = io.connect('http://' + document.domain + ':' + location.port);
|
||||
// Now, we subscribe to the server's "update_data" events - this is sent every second by the background thread, but you can change both the events' names and the speed of these events being sent.
|
||||
socket.on('update_data', function(msg) {
|
||||
// Now, we get the span that we defined before by its ID...
|
||||
var randomNumberSpan = document.getElementById('randomNumber');
|
||||
// And we set its content to the random number we just got from the server.
|
||||
randomNumberSpan.textContent = msg.randomNumber;
|
||||
// For new sensors, we can really easily repeat this! For instance, if you added a piece to the data on the server side called "mySensor", as in the other example, you could do:
|
||||
// var mySensorSpan = document.getElementById('mySensor');
|
||||
// mySensorSpan.textContent = msg.mySensor;
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Add table
Add a link
Reference in a new issue