我正在从事家庭自动化项目。我项目的基本目标是控制位于不同位置的继电器和其他传感器。我已经将Raspberry Pi设置为MQTT代理。 Mosquitto运行良好。现在,我想做的是触发与esp8266(GPIO2)连线的继电器。这是我的Python Web服务器代码:

import paho.mqtt.client as mqtt
from flask import Flask, render_template, request
app = Flask(__name__)

mqttc=mqtt.Client()
mqttc.connect("localhost",1883,60)
mqttc.loop_start()

# Create a dictionary called pins to store the pin number, name, and pin state:
pins = {
   2 : {'name' : 'GPIO 2', 'board' : 'esp8266', 'topic' : 'esp8266/2', 'state' : 'False'}
}

# Put the pin dictionary into the template data dictionary:
templateData = {
'pins' : pins
}

@app.route("/")
def main():
# Pass the template data into the template main.html and return it to the user
return render_template('main.html', **templateData)

# The function below is executed when someone requests a URL with the pin number and action in it:
@app.route("/<board>/<changePin>/<action>")

def action(board, changePin, action):
# Convert the pin from the URL into an integer:
changePin = int(changePin)
# Get the device name for the pin being changed:
devicePin = pins[changePin]['name']
# If the action part of the URL is "on," execute the code indented below:
  if action == "1" and board == 'esp8266':
  mqttc.publish(pins[changePin]['topic'],"1")
  pins[changePin]['state'] = 'True'

if action == "0" and board == 'esp8266':
  mqttc.publish(pins[changePin]['topic'],"0")
  pins[changePin]['state'] = 'False'

# Along with the pin dictionary, put the message into the template data dictionary:
templateData = {
  'pins' : pins
}

return render_template('main.html', **templateData)

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


这是我的HTML代码:

 <!DOCTYPE html>
<head>
   <title>RPi Web Server</title>
   <!-- Latest compiled and minified CSS -->
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
   <!-- Optional theme -->
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
   <!-- Latest compiled and minified JavaScript -->
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
   <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
   <h1>RPi Web Server - ESP8266 MQTT</h1>
   {% for pin in pins %}
   <h2>{{ pins[pin].name }}
   {% if pins[pin].state == 'True' %}
  is currently <strong>on</strong></h2><div class="row"><div class="col-md-2">
  <a href="/esp8266/{{pin}}/0" class="btn btn-block btn-lg btn-default" role="button">Turn off</a></div></div>
   {% else %}
  is currently <strong>off</strong></h2><div class="row"><div class="col-md-2">
  <a href="/esp8266/{{pin}}/1" class="btn btn-block btn-lg btn-primary" role="button">Turn on</a></div></div>
   {% endif %}
   {% endfor %}
</body>
</html>
 


这是我的ESP8266代码:

 #include <ESP8266WiFi.h>
#include <PubSubClient.h

const char* ssid = "Godfather";
const char* password = "idontknow";

const char* mqtt_server = "192.168.137.100";

WiFiClient espClient;
PubSubClient client(espClient);

const int ledGPIO2 = 2;

void setup_wifi() {
  delay(10);

  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("WiFi connected - ESP IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(String topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;

  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();

  if(topic=="esp8266/2"){
      Serial.print("Changing GPIO 2 to ");
      if(messageTemp == "1"){
        digitalWrite(ledGPIO2, HIGH);
        Serial.print("On");
      }
      else if(messageTemp == "0"){
        digitalWrite(ledGPIO4, LOW);
        Serial.print("Off");
      }
  }
  Serial.println();
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");  

      client.subscribe("esp8266/2");

    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
  // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  pinMode(ledGPIO2, OUTPUT);

  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);    
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  if(!client.loop())
    client.connect("ESP8266Client");
}
 


结果:一切似乎都工作正常,但是当我按下Web服务器上的按钮时,仍未触发中继。我相信ESP的订阅不正确。当我在终端上运行Python脚本时,第一次单击,我在终端上收到HTTP / 1.1“ 404,然后每单击一次,我都收到HTTP / 1.1” 200
动态IP。但是我确保ESP8266已配置了当前的Pi IP地址。

评论

这个评论链现在已经很长了。此对话已移至聊天。您可以在对扩展对话更友好的环境中继续进行讨论。如果您遇到无法解决的问题,也可以考虑使用更新来编辑问题或提出新问题。

如果我有多个客户端,我应该在代码中添加什么?

#1 楼

我建议您分解问题。
尝试通过直接将消息发布到MQTT代理(即使用mosquito_pub客户端)来测试中继。
尝试检查Web应用程序是否正在将正确的主题和消息发布到代理(即使用mosquito_sub客户端)。
您还可以通过订阅SYS主题(即已连接的客户端或订阅的总数)来监视设备的行为。

评论


并从ESP8266添加ping(发布),以便您确认它是否正常运行并可以连接到MQTT服务器。

– MatsK
18年1月13日在16:44