diff --git a/docs/LearningProcessBram/documentatie/FirstDocumentation.md b/docs/LearningProcessBram/documentatie/FirstDocumentation.md index b963112..733278e 100644 --- a/docs/LearningProcessBram/documentatie/FirstDocumentation.md +++ b/docs/LearningProcessBram/documentatie/FirstDocumentation.md @@ -157,7 +157,10 @@ And here it looks in action: Later on, I could expand this code and the physical product to include the rest of the sensors. - +The wiring is shown here: +![the wiring]() +The red cables are 3v, the black cables are the ground and the green cable is the echo for data. +This version of a DHT11 has a built-in resistor otherwise this would have to be included. ### Buzzers .pt 2 I found out how to make multiple buzzers go off with the press of one button and increase as Mutch as there are pins. diff --git a/docs/LearningProcessBram/documentatie/assets/DHT11 wires.png b/docs/LearningProcessBram/documentatie/assets/DHT11 wires.png new file mode 100644 index 0000000..c8cb600 Binary files /dev/null and b/docs/LearningProcessBram/documentatie/assets/DHT11 wires.png differ diff --git a/docs/rpi-documentation/Put-Request.md b/docs/rpi-documentation/Put-Request.md new file mode 100644 index 0000000..6e80883 --- /dev/null +++ b/docs/rpi-documentation/Put-Request.md @@ -0,0 +1,71 @@ +# To edit data in the database we wanna use PUT request. + +### What is a put request? +To edit data in the database we wanna use PUT request. A PUT request is used to update an existing resource. If the resource does not exist, it will create a new one. The PUT request requires the client to send the entire updated resource, not just the changes. This means that the client must send all the data, even if only one field has changed. + +A put request is a json its for example looks like this: +```json +{ + "NodeID": 1, + "Location": "testlocation", + "Name": "testname", +} +``` + +### How to use a put request +We have been trying to use a PUT request with flask. But that didn't work because the reverse proxy would reform the request to a GET request. So we tried to let apache 2 run it with wsgi. + +### What is wsgi? +WSGI stands for Web Server Gateway Interface. It is a specification that describes how a web server communicates with web applications, and how web applications can be chained together to process one request. WSGI is a Python standard, and it is implemented by most Python web frameworks. + +We couldnt get it to work even though we worked trough all the errors apache 2 gave us. + +We first had some paths misconfigured. Then we had Forbidden error because we didn't give permission for apache2 to use that path. + +And now we ended on not found and we verified that all configuration is in the correct syntrax with ```apachectl configtest``` + + +### How to use wsgi +To use wsgi you need to install mod_wsgi with the following command: +```bash +sudo apt-get install libapache2-mod-wsgi +``` + +Then you need to enable the module with the following command: +```bash +sudo a2enmod wsgi +``` + +Then you need to create a wsgi file in the same directory as your python file. The wsgi file should look like this: +```python +import sys +sys.path.insert(0, '/home/pi/webapp') +from mainflask import app as application +``` + +Then you need to configure your apache2 configuration file to use the wsgi file. The configuration file should look like this: +```apache + + DocumentRoot /home/pi/www/html/ + + WSGIDaemonProcess webapp python-path=/home/pi/webapp + WSGIScriptAlias /flask /home/pi/webapp/main.wsgi + + + WSGIProcessGroup webapp + WSGIApplicationGroup %{GLOBAL} + Require all granted + + + # Logging + ErrorLog ${APACHE_LOG_DIR}/error.log + CustomLog ${APACHE_LOG_DIR}/access.log combined + +``` + +Then you need to restart apache2 with the following command: +```bash +sudo systemctl restart apache2 +``` + + diff --git a/server/Flask/main.py b/server/Flask/main.py index c434884..76362b7 100644 --- a/server/Flask/main.py +++ b/server/Flask/main.py @@ -10,42 +10,20 @@ def index(): MAC = request.args.get('MAC', default = None) return getData(node, dataType, MAC) -@app.route('/putData', methods=['PUT']) -def putData(): - node_id = request.json.get('node_id', None) - new_name = request.json.get('new_name', None) - new_location = request.json.get('new_location', None) +@app.route('/updateData') +def updateDataIndex(): + node_id = request.args.get('node', None) + new_name = request.args.get('name', None) + new_location = request.args.get('location', None) + return updateData(node_id, new_name, new_location) + - if node_id is None: - return jsonify({"error": "node_id is required"}), 400 - - mydb = mysql.connector.connect( - host="localhost", - user="root", - password="Dingleberries69!", - database="NodeData" - ) - - cursor = mydb.cursor() - - if new_name is not None: - cursor.execute("UPDATE Node SET Name = %s WHERE NodeID = %s", (new_name, node_id)) - mydb.commit() - - if new_location is not None: - cursor.execute("UPDATE Node SET Location = %s WHERE NodeID = %s", (new_location, node_id)) - mydb.commit() - - cursor.close() - mydb.close() - - return jsonify({"message": "Node updated successfully"}), 200 - -def putData(node, name, location, MAC): +def updateData(node, name, location): mydb = loginDB() - query = get_query(node, name, location, MAC) + query = update_query(node, name, location) cursor = mydb.cursor(dictionary=True) # Enable dictionary output cursor.execute(query) + mydb.commit() result = cursor.fetchall() # Fetch the results cursor.close() mydb.close() diff --git a/server/Flask/queries.py b/server/Flask/queries.py index c9aae44..e2da308 100644 --- a/server/Flask/queries.py +++ b/server/Flask/queries.py @@ -10,3 +10,14 @@ def get_query(node, dataType, MAC): else: query = "SELECT * FROM `Measurement`" return query + + + +def update_query(node, name, location): + if node and name and location: + query = f""" + UPDATE Node + SET Name = '{name}', Location = '{location}' + WHERE NodeID = {node}; + """ + return query \ No newline at end of file diff --git a/server/reverseproxy b/server/reverseproxy new file mode 100644 index 0000000..567d696 --- /dev/null +++ b/server/reverseproxy @@ -0,0 +1,20 @@ + + ProxyPreserveHost On + DocumentRoot /home/pi/www/html/ + + + # Enable proxying WebSockets + ProxyPass /ws ws://localhost:8001/ + ProxyPassReverse /ws ws://localhost:8001/ + # Enable proxying HTTP + ProxyPass /http http://localhost:8080/ + + ProxyPass /putData http://localhost:5000/putData + ProxyPassReverse /putData http://localhost:5000/putData + ProxyPass /flask http://localhost:5000/ + ProxyPassReverse /flask http://localhost:5000/ + ProxyPreserveHost On + # Logging + ErrorLog ${APACHE_LOG_DIR}/error.log + CustomLog ${APACHE_LOG_DIR}/access.log combined + \ No newline at end of file