From b5b061b679fa3fd239cc85e9824d1e2c443273a2 Mon Sep 17 00:00:00 2001 From: Raspberry Pi Date: Mon, 2 Dec 2019 18:35:39 -0600 Subject: Minor tweaks to PI.py to get working seamlessly with web server - tested and working as expected. Most files touched because permissions were modified to allow PI.py to run on startup. Everything should be ready now. --- .gitignore | 0 PI.py | 64 +++++++++++++++++++++++++++++++++++++++ README.md | 0 System/__init__.py | 0 System/encoder.py | 0 System/homework8.ipynb | 0 System/initialize_system.py | 0 System/motor.py | 0 System/swingUp.py | 0 System/system.py | 0 System/system_swingup_test.py | 0 System/system_swingup_test_2.py | 0 System/test_Encoder.py | 2 +- System/test_Motor.py | 0 System/test_Return_Home.py | 0 System/test_System.py | 0 Web/PI/PI.py | 0 Web/PI/Results/PLACEHOLDER.txt | 0 Web/PI/Uploads/PLACEHOLDER | 0 Web/PI/test2.py | 0 Web/Server/Results/PLACEHOLDER | 0 Web/Server/Results/results.csv | 0 Web/Server/Server.py | 0 Web/Server/static/Server.css | 0 Web/Server/static/Server.js | 0 Web/Server/templates/base.html | 0 Web/Server/templates/index.html | 0 Web/Server/templates/results.html | 0 Web/requirements.txt | 0 Web/test.py | 0 30 files changed, 65 insertions(+), 1 deletion(-) mode change 100644 => 100755 .gitignore create mode 100755 PI.py mode change 100644 => 100755 README.md mode change 100644 => 100755 System/__init__.py mode change 100644 => 100755 System/encoder.py mode change 100644 => 100755 System/homework8.ipynb mode change 100644 => 100755 System/initialize_system.py mode change 100644 => 100755 System/motor.py mode change 100644 => 100755 System/swingUp.py mode change 100644 => 100755 System/system.py mode change 100644 => 100755 System/system_swingup_test.py mode change 100644 => 100755 System/system_swingup_test_2.py mode change 100644 => 100755 System/test_Encoder.py mode change 100644 => 100755 System/test_Motor.py mode change 100644 => 100755 System/test_Return_Home.py mode change 100644 => 100755 System/test_System.py mode change 100644 => 100755 Web/PI/PI.py mode change 100644 => 100755 Web/PI/Results/PLACEHOLDER.txt mode change 100644 => 100755 Web/PI/Uploads/PLACEHOLDER mode change 100644 => 100755 Web/PI/test2.py mode change 100644 => 100755 Web/Server/Results/PLACEHOLDER mode change 100644 => 100755 Web/Server/Results/results.csv mode change 100644 => 100755 Web/Server/Server.py mode change 100644 => 100755 Web/Server/static/Server.css mode change 100644 => 100755 Web/Server/static/Server.js mode change 100644 => 100755 Web/Server/templates/base.html mode change 100644 => 100755 Web/Server/templates/index.html mode change 100644 => 100755 Web/Server/templates/results.html mode change 100644 => 100755 Web/requirements.txt mode change 100644 => 100755 Web/test.py diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/PI.py b/PI.py new file mode 100755 index 0000000..2157cbd --- /dev/null +++ b/PI.py @@ -0,0 +1,64 @@ +#handle a POST request +from flask import Flask, render_template, request, url_for, jsonify +import subprocess +import os +import RPi.GPIO as GPIO +app = Flask(__name__) + +UPLOAD_DESTINATION = "Uploads/" +SYSTEM_DESTINATION = "System/" +RESULTS_DESTINATION = "Results/" + +INITIALIZE_SYSTEM = "initialize_system.py" + +@app.route('/') +def home(): + return "ANDI'S PIE" + +@app.route('/tests/endpoint', methods=['POST']) +def my_test_endpoint(): + # Receive post + input_json = request.get_json(force=True) + + # Put file content into a file caled upload.py + filename=input_json['filename'].encode("ascii") + file_content=input_json['file_content'].encode("ascii") + filename=input_json['filename'] + file_content=input_json['file_content'] + upload = open(UPLOAD_DESTINATION + filename, "w+") + upload.write(file_content) + upload.close() + + # Run python script + process = subprocess.Popen(["python3", UPLOAD_DESTINATION + filename]) + try: + process.wait() + print("Program exited normally!\n") + except: + print("Exception occurred running program!\n") + process.terminate() + finally: + GPIO.cleanup() + + # Get results file + results_filename = filename.split(".")[0] + results_filename = results_filename + "_results.csv" + with open(RESULTS_DESTINATION + results_filename, 'r') as results: + results_content = results.read() + results.close() + + # Remove test file and results file now that were done with them + os.remove(UPLOAD_DESTINATION + filename) + os.remove(RESULTS_DESTINATION + results_filename) + + # Return results file content + dictToReturn = {'results_filename':results_filename, 'results_content':results_content} + return jsonify(dictToReturn) + +# This will run on system bootup. +if __name__ == '__main__': + os.chdir('/home/pi/pendulum') + # Initialize the system before accepting any files. + subprocess.call(["python3", SYSTEM_DESTINATION + INITIALIZE_SYSTEM]) + # Run the web client to start receiving files. + app.run(host="192.168.1.10", port=8000) diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/System/__init__.py b/System/__init__.py old mode 100644 new mode 100755 diff --git a/System/encoder.py b/System/encoder.py old mode 100644 new mode 100755 diff --git a/System/homework8.ipynb b/System/homework8.ipynb old mode 100644 new mode 100755 diff --git a/System/initialize_system.py b/System/initialize_system.py old mode 100644 new mode 100755 diff --git a/System/motor.py b/System/motor.py old mode 100644 new mode 100755 diff --git a/System/swingUp.py b/System/swingUp.py old mode 100644 new mode 100755 diff --git a/System/system.py b/System/system.py old mode 100644 new mode 100755 diff --git a/System/system_swingup_test.py b/System/system_swingup_test.py old mode 100644 new mode 100755 diff --git a/System/system_swingup_test_2.py b/System/system_swingup_test_2.py old mode 100644 new mode 100755 diff --git a/System/test_Encoder.py b/System/test_Encoder.py old mode 100644 new mode 100755 index 9a2119a..b667cc1 --- a/System/test_Encoder.py +++ b/System/test_Encoder.py @@ -3,7 +3,7 @@ import time # Decide which pins to hook up to on the Pi before running clk_pin = 2 -cs_pin = 14 +cs_pin = 4 data_pin = 3 e = Encoder(clk_pin, cs_pin, data_pin) diff --git a/System/test_Motor.py b/System/test_Motor.py old mode 100644 new mode 100755 diff --git a/System/test_Return_Home.py b/System/test_Return_Home.py old mode 100644 new mode 100755 diff --git a/System/test_System.py b/System/test_System.py old mode 100644 new mode 100755 diff --git a/Web/PI/PI.py b/Web/PI/PI.py old mode 100644 new mode 100755 diff --git a/Web/PI/Results/PLACEHOLDER.txt b/Web/PI/Results/PLACEHOLDER.txt old mode 100644 new mode 100755 diff --git a/Web/PI/Uploads/PLACEHOLDER b/Web/PI/Uploads/PLACEHOLDER old mode 100644 new mode 100755 diff --git a/Web/PI/test2.py b/Web/PI/test2.py old mode 100644 new mode 100755 diff --git a/Web/Server/Results/PLACEHOLDER b/Web/Server/Results/PLACEHOLDER old mode 100644 new mode 100755 diff --git a/Web/Server/Results/results.csv b/Web/Server/Results/results.csv old mode 100644 new mode 100755 diff --git a/Web/Server/Server.py b/Web/Server/Server.py old mode 100644 new mode 100755 diff --git a/Web/Server/static/Server.css b/Web/Server/static/Server.css old mode 100644 new mode 100755 diff --git a/Web/Server/static/Server.js b/Web/Server/static/Server.js old mode 100644 new mode 100755 diff --git a/Web/Server/templates/base.html b/Web/Server/templates/base.html old mode 100644 new mode 100755 diff --git a/Web/Server/templates/index.html b/Web/Server/templates/index.html old mode 100644 new mode 100755 diff --git a/Web/Server/templates/results.html b/Web/Server/templates/results.html old mode 100644 new mode 100755 diff --git a/Web/requirements.txt b/Web/requirements.txt old mode 100644 new mode 100755 diff --git a/Web/test.py b/Web/test.py old mode 100644 new mode 100755 -- cgit v1.2.3