MQTT के माध्यम से रास्पबेरी पाई के माध्यम से ESP8266 को नियंत्रित करना


9

मैं एक होम ऑटोमेशन प्रोजेक्ट पर काम कर रहा हूं। मेरी परियोजना का मूल उद्देश्य विभिन्न स्थानों पर स्थित रिले और अन्य सेंसर को नियंत्रित करना है। मैंने एक MQTT ब्रोकर के रूप में अपना रास्पबेरी पाई स्थापित किया है। मच्छर ठीक चल रहा है। अभी के लिए, मैं जो करने की कोशिश कर रहा हूं वह एस्प 8266 (जीपीआईओ 2) के साथ एक रिले को ट्रिगर करना है। यहाँ मेरा पायथन वेब सर्वर कोड है:

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");
}

परिणाम: सब कुछ ठीक काम कर रहा है, लेकिन फिर भी जब मैं वेब सर्वर पर बटन दबाता हूं तो रिले ट्रिगर नहीं होता है। मेरा मानना ​​है कि ESP को ठीक से सब्सक्राइब नहीं किया गया है। जब मैं टर्मिनल पर पायथन स्क्रिप्ट चलाता हूं, तो पहली बार मुझे टर्मिनल पर HTTP / 1.1 "404 प्राप्त होता है, और हर दूसरे क्लिक पर मुझे HTTP / 1.1" 200 प्राप्त होता है

My Pi अभी गतिशील आईपी पर काम कर रहा है। लेकिन मुझे यकीन है कि ESP8266 को वर्तमान Pi IP पते से कॉन्फ़िगर किया गया है।


1
यह टिप्पणी श्रृंखला अब काफी लंबी हो रही है; इस वार्तालाप को बातचीत में स्थानांतरित कर दिया गया है । आप अपनी चर्चा को ऐसे वातावरण में जारी रख सकते हैं जो विस्तारित बातचीत के लिए अधिक अनुकूल है। अपने प्रश्नों को अपडेट के साथ संपादित करने या नए प्रश्न पूछने पर विचार करें यदि आप उन समस्याओं का सामना करते हैं जिन्हें आप हल करने में सक्षम नहीं हैं।
अरोरा ००००

1
यदि मेरे पास कई ग्राहक हैं तो मुझे अपने कोड में क्या जोड़ना चाहिए?
रोहित माथुर

जवाबों:


3

मैं आपको सुझाव देता हूं कि आप समस्या का समाधान करें।

सीधे MQTT ब्रोकर को संदेश प्रकाशित करके रिले का परीक्षण करने का प्रयास करें (यानी मच्छर_पूब ग्राहक का उपयोग करके)।

यह जांचने का प्रयास करें कि क्या वेब ऐप ब्रोकर को सही विषय और संदेश प्रकाशित कर रहा है (यानी मच्छर_ ग्राहक का उपयोग करके)।

आप SYS विषय (यानी जुड़े क्लाइंट या सदस्यता की कुल संख्या) की सदस्यता लेकर अपने उपकरणों के व्यवहार की निगरानी कर सकते हैं ।


1
और ESP8266 से एक पिंग (प्रकाशित) जोड़ें ताकि आप पुष्टि कर सकें कि यह काम कर रहा है और एमक्यूटीटी सर्वर तक पहुंच सकता है।
MatsK
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.