From b6156ae81d7114341f5659c6c6a7408aefa73c37 Mon Sep 17 00:00:00 2001 From: Qangan Date: Fri, 23 Feb 2024 10:00:48 +0700 Subject: [PATCH] added all info here --- README.md | 0 accounts.json | 6 + data.json | 1 + esplistener/__init__.py | 0 .../__pycache__/__init__.cpython-311.pyc | Bin 0 -> 156 bytes .../__pycache__/esplistener.cpython-311.pyc | Bin 0 -> 4037 bytes esplistener/esplistener.py | 109 ++++++++++ mashinka.py | 143 ++++++++++++++ templates/accounts.json | 22 +++ templates/index.html | 99 ++++++++++ templates/login.html | 62 ++++++ templates/mashinka.bk | 58 ++++++ templates/mashinka.html | 187 ++++++++++++++++++ templates/register.html | 57 ++++++ templates/unauthorized.html | 4 + test.py | 81 ++++++++ webs | 0 webs.py | 94 +++++++++ webserv/webserv.ino | 112 +++++++++++ 19 files changed, 1035 insertions(+) create mode 100644 README.md create mode 100644 accounts.json create mode 100644 data.json create mode 100644 esplistener/__init__.py create mode 100644 esplistener/__pycache__/__init__.cpython-311.pyc create mode 100644 esplistener/__pycache__/esplistener.cpython-311.pyc create mode 100644 esplistener/esplistener.py create mode 100644 mashinka.py create mode 100644 templates/accounts.json create mode 100644 templates/index.html create mode 100644 templates/login.html create mode 100644 templates/mashinka.bk create mode 100644 templates/mashinka.html create mode 100644 templates/register.html create mode 100644 templates/unauthorized.html create mode 100644 test.py create mode 100644 webs create mode 100644 webs.py create mode 100644 webserv/webserv.ino diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/accounts.json b/accounts.json new file mode 100644 index 0000000..6b9b537 --- /dev/null +++ b/accounts.json @@ -0,0 +1,6 @@ +[ + { + "username": "1", + "password": "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b" + } +] \ No newline at end of file diff --git a/data.json b/data.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/data.json @@ -0,0 +1 @@ +{} diff --git a/esplistener/__init__.py b/esplistener/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/esplistener/__pycache__/__init__.cpython-311.pyc b/esplistener/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..37e238b40a0e9e9aaaa59827602c74a1a0be2f9e GIT binary patch literal 156 zcmZ3^%ge<81ab?Hq=M+jAOZ#$p^VRLK*n^26oz01O-8?!3`I;p{%4TnFMa)t{M=Oi z!o1{trzWHg=-LpS=;9p`8++~S zE+L`jgrcj?s*swTh{8iEaaEGzl&h47dpNZ(%_$FktBriBwGt9ipYrA?R|(~%{bv0i z$n~Ma`kR?=zWKgyzWL@i!xxQ>E(E3j@;^x4jnKbnr(F0_A!?O^hz<%(z# zGor*I^F)6G^a$`Xz6P?0HiSJ(jd!P)wTIKW?!l}uloN&)LwSz>^vta@nTC!m=g)Ee z4Ckk;bGOdjelyFp$IwG^9Iz8hSo|9RfCb13>fM925 z<4Kv(J(SY~#Gs}nN%zvxhS?y6oa)K`-_~;h2+oLw2ThLCopoUi+-PS7Y!|O#2x`aD>kFZ~JL(aeAAtxj_ z(Ts#XeltldXE+g$E1DXvJ=*k0+Mr{?I`5$rt2v2Waw9&iRt+%wmYtBx9iA!kdauGO0ROcg2 zM!TeVmH$z>gH|g5RbfG+-otH{94SV1%-HFv52PHbv#KqHOMKa+$x|vOJu9WMTC;*O z4eNe% z|4@m|5;%oE=-I6CdYB&^)M-lSH13vgmYDVgTT*YQyoOU(0gZ{xo-cJ&$xKM|&A%c& z*NJo`VM>Z75nWH{!_sq#RB|I+5JXeWL=g&j3I&GcAX+$~5=+oY`1-V-h+D3y+39Jb zj{$aDJiQk{NF}xqpVT9oY2m2FUV)2-HB8+SPu~cWq#2=CkiLhyO&6)ts9OAWx>AeP z$u$cr)P|8vXa;b=GlO0VaKYH62taP?;AH6_>-{kP4*ZO1@P85Io$dm1@ZEV|N8ZB81SHL-*@A%D{V)I!)YJ{o($oz+M-xAg7sNjZzZdQ{FZ)(b-PyOi@1NeD zb#G7BE9bm&rtf4{7|97E8DXTruwqNznXt?kS6s~xMM2VGzE{;Bu#0}l>lTl;gZ{pt7fO?y}NuQzpNn!487p7iD9_x?7# zE}zfH=Qlhpca?kOGqQ5_j<&3=s##BO&eNN*zr43?<@`E0MAZaiB^z+}_L^fwj)HYfrpi znOXOdocl;-#|O*Vf_RAoP`~&I#L9RJ;{CM27wSe&yP42__G$2>Z^VV3xm+Uy?6c<9 zk$(1BKM(D?+fRg9Hq8D78uhnd%`1*xNTp)dKvA>;e>-o=?NFg|_Zjp5*8@-4`|!f5 z*ABO)L%9KG4zg8xNBXV5db#+ACI8rkG5>;#ChOeQNQ_A1`Yai?`;=z{i)Sl9@6Moo zv;v?<4i=ABK<<+Qfu8D1Zg8-tPa1?G%W_4Dso$(>q*@j5!w8m7W}td7t8<8b`s$~J z6Pg*(X36UnM?taeseQf!%H{r#g2gi)sPTuo5SfRD$L|dtuyP8>-zSRasAQx+G2MM42gfigld~i5WF}R2D3{ z_ycREf;(bZ475~1J4rDpzSc*esM;S(#Qc49v`_j0Guut*@U%zI{{v*+slV zDhba_8VN%m1n#flXQY8FqJQs4ZqL&2L*vg!zk2tZj&Dw7dr#zgPh@*Sxt>tw7nd@v z6YzZ(OMI8So=bjQKl(+&!9`=VB7}+*oSc*wP*g + + + + + + Mashinka Control + + + + + + + + + + + + + +
+

Вы не авторизированы

+
+ +       + +
+ + + + + + + + + + + + + + + + + diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..b9c385b --- /dev/null +++ b/templates/login.html @@ -0,0 +1,62 @@ + + + + + + + Mashinka Control + + + + + + + + + + + + + + +
+
+
+ + +
+

+
+ + +
+

+ +
+ {% if err != '' %} +

{{err}}

+ {% endif%} +
+ + + + + + + + + + + + diff --git a/templates/mashinka.bk b/templates/mashinka.bk new file mode 100644 index 0000000..28f539e --- /dev/null +++ b/templates/mashinka.bk @@ -0,0 +1,58 @@ + + + + + + + Mashinka controls + + + + + + + + + + + Seal Team + + + + + +
+
+
+ +
+
+ +
+
+
+ +
+
+
+
+ +
+ +
+
+ + + + diff --git a/templates/mashinka.html b/templates/mashinka.html new file mode 100644 index 0000000..ceeb755 --- /dev/null +++ b/templates/mashinka.html @@ -0,0 +1,187 @@ + + + + + + + Mashinka Control + + + + + + + + + + + + + +
+ +
+ +

{{cur_speed}}

+ +
+ + +
+
+
+
+
+ +
+ +
+ +
+
+ + + + + + + + + + + + + + + + + diff --git a/templates/register.html b/templates/register.html new file mode 100644 index 0000000..02f6312 --- /dev/null +++ b/templates/register.html @@ -0,0 +1,57 @@ + + + + + + + Mashinka Control + + + + + + + + + + + + + + +
+
+
+ + +
+

+
+ + +
+

+ +
+ {% if err != '' %} +

{{err}}

+ {% endif%} +
+ + + + + + + diff --git a/templates/unauthorized.html b/templates/unauthorized.html new file mode 100644 index 0000000..2c9950c --- /dev/null +++ b/templates/unauthorized.html @@ -0,0 +1,4 @@ + + + no rights + diff --git a/test.py b/test.py new file mode 100644 index 0000000..118cc13 --- /dev/null +++ b/test.py @@ -0,0 +1,81 @@ +from flask import Flask, request, redirect, url_for, render_template, json, render_template_string, jsonify + +app = Flask(__name__) + + +@app.route('/', methods=['GET', 'POST']) +def control_panel(): + print('request.form:', request.form) + + if request.method == 'POST': + if request.form.get('button') == 'button-play': + print("play button pressed") + + elif request.form.get('button') == 'button-exit': + print("exit button pressed") + + elif request.form.get('slide'): + volume = request.form.get('slide') + print('volume:', volume) + #return jsonify({'volume': volume}) + return json.dumps({'volume': volume}) + + print('render') + return render_template_string(''' + + + + + + + + + + Slider + + +
+
+
+
+ + + + + +
+
+
+
+ + + + + + + +''') + +if __name__ == '__main__': + app.run(debug=True, use_reloader=False) diff --git a/webs b/webs new file mode 100644 index 0000000..e69de29 diff --git a/webs.py b/webs.py new file mode 100644 index 0000000..16c41aa --- /dev/null +++ b/webs.py @@ -0,0 +1,94 @@ +import requests +import json +from flask import Flask, render_template, url_for, redirect, request, json +import hashlib + +app = Flask(__name__) + +app.secret_key = '%TGHUI8uyFGHI(*&^5rUIO9i*&^TRfvgbHNJKLKJnhgfDE3WedFGhjKOlIuyTRe4567ioP:liuytr56&8()IuGTfrEdsXcghuy6' + +print("OPEN YOUR ESP SERIAL PORT READER A AND ENDER IP") +ip = input() + +def hash_data(data): + hashpass = hashlib.sha256() + hashpass.update(bytes(password, encoding='utf8')) + passw = hashpass.hexdigest() + return passw + +def create_account(login, password): + passw = hash_data(password) + data = {'username': login, 'password': passw} + with open('accounts.json', 'rw') as f: + for entry in json.load(f): + if entry['username'] == login: + return 0 + json.dump(data, f) + return 1 +def login_into_account(session, login, password): + passw = hash_data(password) + with open('accounts.json', 'r') as f: + for entry in json.load(f): + if entry['username'] == login and entry["password"] == password: + session['loggedin'], session['username'] = True, login + return 1 + return 0 + + + + + +def get_status(): + return requests.get("http://"+ip+"/status").text + +@app.route('/', methods = ["GET", "POST"]) +def index(): + if session["loggedin"] == True: + stat = get_status().split() + led_status = int(stat[0]) + led_brightness = int(stat[1]) + if request.method == "POST": + if request.form.get("slide"): + brightness = request.form.get("slide") + print(brightness) + led_britghtness = change(brightness) + return render_template("index.html", led_status = led_status, led_brightness = led_brightness) + else: + return render_template("unauthorized.html") + +@app.route('/register', methods = ["POST", "GET"]) +def register(): + if request.method == "GET": + if session['loggedin'] == True: + return redirect('/') + else: + return render_template("register.html") + else: + data = request.args + login = data['login'] + passw = data['password'] + if create_account(login, passw): + return redirect('/') + else: + return render_template("register.html", err='Такой пользователь уже существует') + +@app.route('/login', methods = ["POST", "GET"]) +def login(): + if request.method == 'GET': + if session['loggedin'] == True: + return redirect('/') + else: + return render_template("login.html") + else: + data = request.args + login = data['login'] + passw = data['passw'] + if login_into_account(session, login, passw): + return redirect('/') + else: + return render_template("login.html", err="Неверный логин/пароль") + + + +if __name__ == '__main__': + app.ru() diff --git a/webserv/webserv.ino b/webserv/webserv.ino new file mode 100644 index 0000000..5e84469 --- /dev/null +++ b/webserv/webserv.ino @@ -0,0 +1,112 @@ +#include +#include + + +ESP8266WebServer server(80);//Создадим веб сервер на 80 порту +bool LEDstatus = LOW; +byte led_brightness = 0; + +void setup() { + Serial.begin(9600);// Настроим частоту Serial порта + delay(100); + pinMode(3, OUTPUT);// Настроим ножку светодиода на выход, чтобы управлять им + + Serial.println("Connecting to "); + Serial.println("node"); + //connect to your local wi-fi network + WiFi.mode(WIFI_STA);// Режим подключения к сторонней сети wi-fi + WiFi.begin("adm2", "98765432110"); + // Ждем пока статус соеденения не будет равен WL_CONNECTED , что означает что еsp успешно подключена к сети + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print("."); + } + Serial.println(""); + Serial.println("WiFi connected..!"); + Serial.print("Got IP: "); Serial.println(WiFi.localIP());// Вывод ip esp + //Настройка путей и их обработчиков + server.on("/status", handle_status); + server.on("/ledon", HTTPMethod::HTTP_POST, handle_ledon); + server.on("/ledoff", HTTPMethod::HTTP_POST, handle_ledoff); + //Настройка обработчика ненайденного пути + server.on("/slider", HTTPMethod::HTTP_POST, handle_slider); + server.onNotFound(handle_NotFound); + //Запуск сервера + server.begin(); + Serial.println("HTTP server started"); +} +void loop() { + //Обработка запросов + server.handleClient(); + //Включение/Выключение светодиода. Помните у esp инвертированная логика светодиода на плате, поэтому LOW означает включить светодиод + /*if(LEDstatus) + {analogWrite(6, LOW);} + else + {analogWrite(6, HIGH);} +*/ +} + +void handle_status() { + Serial.println("LED: "); + Serial.println(LEDstatus); + //Отправка + server.send(200, "text/plain", String(not(LEDstatus))+" "+String(led_brightness));} + +void handle_slider(){ + if(!server.hasArg("brightness") or floor(atof(server.arg("brightness").c_str())) <= 0 or floor(atof(server.arg("brightness").c_str())) >= 256 ){ + server.send(400, "texp/plain", "Brightness argument must be 0 0) ? floor(atof(server.arg("brightness").c_str())) : 255; + analogWrite(3, led_brightness); + server.send(200, "text/plain", String(not(LEDstatus))); + + }else{ + + Serial.println("LED: 1"); + Serial.println(server.arg("timeout").c_str()); + LEDstatus = 0; + analogWrite(3, 255); + + delay(atof(server.arg("timeout").c_str())*1000); + //Отправка + LEDstatus = 1; + analogWrite(3, 0); + server.send(200, "text/plain", String(not(LEDstatus))); // +} +} + +void handle_ledoff() { + + + if( ! server.hasArg("timeout")){ + LEDstatus = 1; + led_brightness = 0; + analogWrite(3, led_brightness); + server.send(200, "text/plain", String(not(LEDstatus))); + }else{ + + Serial.println("LED: 0"); + Serial.println(server.arg("timeout").c_str()); + LEDstatus = 1; + analogWrite(3, 0); + delay(atof(server.arg("timeout").c_str())*1000); + //Отправка + LEDstatus = 0; + analogWrite(3, led_brightness); + server.send(200, "text/plain", String(not(LEDstatus))); //Код состояния 200 - OK, тип - text/plain, cами данные - String(LEDstatus) +} +} +//Код состояния 200 - OK, тип - text/plain, cами данные - String(LEDstatus) +void handle_NotFound(){ +//Отправка + server.send(404, "text/plain", "Not found"); //Код состояния 404 - не найдено, тип - text/plain, cами данные - "Not found" +}