简体   繁体   English

[Python][Heroku]不能经常向树莓派稳定发送信号

[英][Python][Heroku]Can't send signal often to Raspberry Pi stably

My name is Hekatonkeiru and I am writing you from Japan.我的名字是Hekatonkeiru,我从日本给你写信。

I deployed an app on Heroku that controls the Raspberry Pi GPIO via CloudMQTT.我在 Heroku 上部署了一个应用程序,该应用程序通过 CloudMQTT 控制 Raspberry Pi GPIO。
This is my app configuration这是我的应用配置

Enviroment:Heroku
Front-end: HTML, JavaScript(jQuery)
Server-side: Python(Flask)

But In the following cases, it can't send signal often to Raspberry Pi stably following this case.但是在以下情况下,它不能经常稳定地向树莓派发送信号。

  • When a signal is sent continuously.连续发送信号时。
  • When the signal is sent without any time delay.当信号发送时没有任何延迟。

There is no problem in these following cases.在以下这些情况下没有问题。

  • Run app on the local environment.在本地环境中运行应用程序。
  • When remove the login function from app.从应用程序中删除登录 function 时。

I think this problem is happening between the login function and the function to send MQTT, what do you think?我认为这个问题发生在登录 function 和 function 发送 MQTT 之间,你怎么看?
Or is there some other cause?还是有其他原因?
Anyway, can I ask how to solve this case?无论如何,我能问一下如何解决这个案子吗?

Let me know if you have any further questions.如果您还有其他问题,请告诉我。

Regards,问候,
Hekatonkeiru赫卡通凯鲁



Here is my app directory.这是我的应用程序目录。

RPi_app
│  
├─app.py
├─variable.py
│
├─sendMQTT
│      ca-certificates.crt
│      GPIO_Control_SendMQTT_One.py
│      GPIO_Control_SendMQTT_Three.py
│      GPIO_Control_SendMQTT_Two.py
│
├─static
│      SendToRPi.js
│
└─templates
        index.html
        login.html



app.py应用程序.py

# -*- coding: utf-8 -*-

import os
from flask_sqlalchemy import SQLAlchemy
from flask import Flask, render_template, request
from flask_login import UserMixin, LoginManager, login_user, login_required
from werkzeug.security import check_password_hash
import flask_wtf
import wtforms
from wtforms import validators
from itsdangerous.url_safe import URLSafeTimedSerializer
from sendMQTT import GPIO_Control_SendMQTT_One
from sendMQTT import GPIO_Control_SendMQTT_Two
from sendMQTT import GPIO_Control_SendMQTT_Three

import variable

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = variable.SQLALCHEMY_DATABASE_URI

app.config['SECRET_KEY'] = os.urandom(24)

db = SQLAlchemy(app)
app.secret_key = os.urandom(24)
SALT = os.urandom(24)
currentDirctory = os.getcwd()
login_manager = LoginManager()
login_manager.init_app(app)

class NewPwdForm(flask_wtf.FlaskForm):
    token = wtforms.HiddenField('token', [
        validators.InputRequired()])
    new_pwd1 = wtforms.PasswordField('PW', [
        validators.InputRequired(),
        validators.EqualTo('new_pwd2')])
    new_pwd2 = wtforms.PasswordField('PW', [
        validators.InputRequired()])

class AddressForm(flask_wtf.FlaskForm):
    mail = wtforms.StringField('mail', [
        validators.Email(message='WRONG FORMATT'),
        validators.InputRequired(message='PUT IN ADDEESS')])

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True) 
    username = db.Column(db.String(), nullable=False, unique=True)  
    password = db.Column(db.String()) 

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))


@app.route('/', methods=['GET', 'POST'])
def login():
    if request.method == "POST":
        username = request.form.get('username')  
        password = request.form.get('password')  
        user = User.query.filter_by(username=username).first()

        if user is None:
            return render_template('login.html', login_Message="※NO USER※")

        if check_password_hash(user.password, password):
            login_user(user)
            return render_template('index.html', username=str(user.username))
        else:
            return render_template('login.html', login_Message="※WRONG PW※")
    else:
        return render_template('login.html')


def create_token(user_id, secret_key, salt):
    serializer = URLSafeTimedSerializer(secret_key)
    return serializer.dumps(user_id, salt=salt)


def load_token(token, secret_key, salt, max_age=600):
    serializer = URLSafeTimedSerializer(secret_key)
    return serializer.loads(token, salt=salt, max_age=3600)


@app.route('/sendMovement', methods=['GET', 'POST'])
@login_required
def sendMovement():
    if request.method == "POST":
        PostMonement = str(request.form['Monement'])  

        if PostMonement == "move_one":
            GPIO_Control_SendMQTT_One.send()

        if PostMonement == "move_two":
            GPIO_Control_SendMQTT_Two.send()

        if PostMonement == "move_three":
            GPIO_Control_SendMQTT_Three.send()


    return render_template('index.html')

if __name__ == "__main__":
    app.run(host='0.0.0.0', threaded=True, debug=True)



SendToRPi.js SendToRPi.js

function one_click() {
    Movement("move_one");
    return;
}

function two_click() {
    Movement("move_two");
    return;
}


function three_click() {
    Movement("move_three");
    return;
}


function Movement(strMovement) {
    var fData = new FormData();
    fData.append('Monement', strMovement);
    ajaxSend(fData, '/sendMovement')
    return;
}

function ajaxSend(fData, urlFlask) {
    //ajax
    $.ajax({
        url: urlFlask,
        type: 'POST',
        async: false,
        data: fData,
        contentType: false,
        processData: false,
        success: function(data, dataType) {
            console.log('Success', data);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            console.log('Error : ' + errorThrown);
        }
    });
}



index.html索引.html

<!DOCTYPE html>
<html>

<body>
    <button type="button" onclick="one_click();">ONE</button>
    <button type="button" onclick="two_click();">TWO</button>
    <button type="button" onclick="three_click();">THREE</button>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/qs/6.7.0/qs.min.js"></script>
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <script src="/static/SendToRPi.js?p=(new Date()).getTime() "></script>
</body>

</html>

Check MQTT qos and retain configurations.检查 MQTT qos 并保留配置。 There is a chance that one message overwrite the other if qos = 0 (Means that the message will be sent at most once) Check this: [https://www.hivemq.com/blog/mqtt-essentials-part-6-mqtt-quality-of-service-levels/][1]如果 qos = 0,则一条消息有可能覆盖另一条消息(意味着消息将最多发送一次)检查:[https://www.hivemq.com/blog/mqtt-essentials-part-6- mqtt-服务质量级别/][1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以将 python 用于基于 Raspberry Pi 的应用程序吗? - Can I use python for a Raspberry Pi based App? 无法通过WebSocket连接Raspberry Pi服务器 - Can't connect Raspberry Pi server through WebSocket 如何将数据从客户端发送到树莓派? - How to send data from client to raspberry pi? 在树莓派中向 RabbitMq 的网页之间发送消息 - send a message between a webpage to RabbitMq in raspberry pi 我可以使用 chrome web 蓝牙低能量 android 将来自树莓派的图像的 stream 发送到 android 吗? - Can I send a stream of images from a raspberry pi to android using chrome web bluetooth low energy API? 在树莓派上的HTML和python之间发送消息 - Sending a message between HTML and python on the raspberry pi 无法使用Lighttpd在Raspberry Pi上访问socket.io.js [Node.JS&Socket.IO] - Can't access to socket.io.js on Raspberry Pi with Lighttpd [Node.JS & Socket.IO] 我无法在 Raspberry Pi Desktop 上将 nodejs 版本从 10.24.0 升级到 14.16.1(LTS) - I can't upgrade nodejs version from 10.24.0 to 14.16.1(LTS) on Raspberry Pi Desktop 无法管理更新树莓派,gpio-header html文件 - Can't manage to update raspberry pi, gpio-header html file 安全地将数据从 Raspberry Pi 发送到 Apache 服务器 - Securely send data from Raspberry Pi to Apache Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM