commit 0590c61ac854fd1c4f8bfd0a211ad26aec8917d5
parent 04b5110ed27af33fe39097e800a429eb89943a56
Author: xd <[email protected]>
Date: Thu, 12 May 2022 14:06:39 +0200
routes for esp
Diffstat:
7 files changed, 121 insertions(+), 5 deletions(-)
diff --git a/client/boot.py b/client/boot.py
@@ -0,0 +1,36 @@
+import sys
+import config
+import network
+from time import sleep
+from machine import Pin, ADC
+
+led2 = Pin(2, Pin.OUT)
+
+connection = network.WLAN(network.STA_IF)
+
+
+def connect():
+
+ if connection.isconnected():
+ print("Already connected")
+ led2.on()
+ return
+
+ connection.active(True)
+ connection.connect(config.WIFI_SSID, config.WIFI_PASSWORD)
+
+ retry = 0
+ while not connection.isconnected(): # wait until connection is complete
+ if retry == 10: # try 10 times
+ sys.exit("Could not establish connection, check your settings")
+ retry += 1
+
+ sleep(1) # check again in a sec
+
+ # no exit, we have a connection!
+ print("Connection established")
+ led2.on()
+
+
+if __name__ == "__main__":
+ connect()
+\ No newline at end of file
diff --git a/client/config.py b/client/config.py
@@ -0,0 +1,6 @@
+WIFI_SSID = "gercowifi"
+WIFI_PASSWORD = "password"
+SERVER = "192.168.137.1"
+PORT = "5000"
+ENDPOINTSTATUS = "/api/update_status"
+ENDPOINTCONNECT = "/api/search_connect"
+\ No newline at end of file
diff --git a/client/main.py b/client/main.py
@@ -0,0 +1,33 @@
+import esp32
+from machine import Pin, deepsleep
+from time import sleep
+import config
+import urequests as requests
+
+urlstatus = f"http://{config.SERVER}:{config.PORT}{config.ENDPOINTSTATUS}"
+urlconnect = f"http://{config.SERVER}:{config.PORT}{config.ENDPOINTCONNECT}"
+
+led = Pin(23, Pin.OUT)
+trap = Pin(15, Pin.IN, Pin.PULL_DOWN)
+connectbutton = Pin(4, Pin.IN, Pin.PULL_DOWN)
+state = False
+mac = "0000000000000000"
+#esp32.wake_on_ext1(pins = (button, button2), level = esp32.WAKEUP_ANY_HIGH)
+
+while connection.isconnected():
+ print(connectbutton.value())
+ print(trap.value())
+ new_state = trap.value()
+ led.value(new_state)
+ if state != new_state:
+ response = requests.post(urlstatus, json=new_state)
+ answer = response.json()
+ state = new_state
+ print(answer)
+ if connectbutton.value():
+ response = requests.post(urlconnect, json=mac)
+ answer = response.json()
+ print(answer)
+ sleep(1)
+
+#deepsleep()
diff --git a/client/readme b/client/readme
@@ -0,0 +1 @@
+dit zijn de esp32 python bestanden.
+\ No newline at end of file
diff --git a/run-server.py b/run-server.py
@@ -1,4 +1,4 @@
from server.app import app
if __name__ == '__main__':
- app.run(debug=True)
+ app.run(host="0.0.0.0", port=5000, debug=True)
diff --git a/server/routes.py b/server/routes.py
@@ -2,7 +2,7 @@ from operator import or_
import os
import secrets
-from flask import flash, redirect, render_template, request, url_for, abort
+from flask import flash, redirect, render_template, request, url_for, abort, request, jsonify
from flask_login import current_user, login_required, login_user, logout_user
from PIL import Image
from calendar import Calendar as Month
@@ -12,11 +12,37 @@ from .app import app, bcrypt, db
from .forms import LoginForm, RegistrationForm, UpdateAccountForm
from .models import Trap, User, UserType
+status = None
+
[email protected]("/api/update_status", methods=['POST', 'GET'])
+def my_function():
+ data = request.json
+ global status
+ if data is None:
+ status = "Error"
+ elif data:
+ status = "on"
+ else:
+ status = "off"
+ reaction = "congrats"
+ return jsonify(reaction)
+
[email protected]("/api/search_connect", methods=['POST', 'GET'])
+def my_function2():
+ data = request.json # temperature reading
+ if data is None:
+ status = "Error"
+ elif data:
+ status = "on"
+ else:
+ status = "off"
+ reaction = data
+ return jsonify(reaction)
""" index.html (home-page) route """
@app.route("/")
def index():
- return render_template('index.html')
+ return render_template('index.html', status = status)
""" about.html route """
@app.route("/about")
diff --git a/server/templates/index.html b/server/templates/index.html
@@ -2,10 +2,21 @@
{% block content %}
<article class="media content-section">
<div class="media-body">
- <h2>Dit zijn uw muizenvallen</h2>
+ <h2>Dit zijn ons lessen</h2>
<p>
- Klik op de titel van een muizenval om de instellingen in te zien!
+ U kan makkelijk op de titel van een les klikken om in te schrijven!
</p>
</div>
</article>
+<article class="media content-section">
+ <div class="media-body">
+ {% if status == "on" %}
+ <h1> ON!</h1>
+ {% elif status == "off" %}
+ <h1> OFF!</h1>
+ {% else %}
+ <h1> ERROR!</h1>
+ {% endif %}
+ </div>
+</article>
{% endblock content %}
\ No newline at end of file