Skocz do zawartości

Pierwsze kroki w MQTT


SOYER

Pomocna odpowiedź

Cześć, po nauczeniu się z Waszą pomocą podstaw POST, GET i PATCH, postanowiłem rzucić to w ...krzaki i w moim projekcie użyć MQTT.

Broker mqtt to supla, połączyłem się z nią przy pomocy przykładu z libsa PubSubClient:

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include "HWCDC.h"
HWCDC USBSerial;
// Update these with values suitable for your network.

const char* ssid = "xx";
const char* password = "xx";
const char* mqtt_server = "mqttxx.supla.org";
const char* mqtt_user = "xx";
const char* mqtt_pass = "xx";

WiFiClientSecure client1;
PubSubClient client(client1);

unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE  (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  USBSerial.println();
  USBSerial.print("Connecting to ");
  USBSerial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin("xx", "xx");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    USBSerial.print(".");
  }

  randomSeed(micros());
}

void callback(char* topic, byte* payload, unsigned int length) {
  USBSerial.print("Message arrived [");
  USBSerial.print(topic);
  USBSerial.print("] ");
  for (int i = 0; i < length; i++) {
    USBSerial.print((char)payload[i]);
  }
  USBSerial.println();

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is active low on the ESP-01)
  } else {
    digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
  }

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {

    USBSerial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str(), mqtt_user, mqtt_pass)) {
      USBSerial.println("connected");
      client.subscribe("inTopic");
    } else {
      USBSerial.print("failed, rc=");
      USBSerial.print(client.state());
      USBSerial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
USBSerial.begin(9600);
  setup_wifi();
  client1.setInsecure();
  client.setServer(mqtt_server, 8883);
  client.setCallback(callback);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

}

Odpaliłem też mqtt explorera, screen poniżej, na którym widać wszystkie zdarzenia na moich urządzeniach.

Jak dobrze rozumiem kluczowa jest(na początek odbieranie) ta linijka:

client.subscribe("inTopic");

jednak co wstawić w argument by dostać konkretną informację z serwera?

Nie kapuję tego...jeszcze;)

?

 

Zrzut ekranu 2024-12-26 195337.png

Link do komentarza
Share on other sites

Bądź aktywny - zaloguj się lub utwórz konto!

Tylko zarejestrowani użytkownicy mogą komentować zawartość tej strony

Utwórz konto w ~20 sekund!

Zarejestruj nowe konto, to proste!

Zarejestruj się »

Zaloguj się

Posiadasz własne konto? Użyj go!

Zaloguj się »
×
×
  • Utwórz nowe...

Ważne informacje

Ta strona używa ciasteczek (cookies), dzięki którym może działać lepiej. Więcej na ten temat znajdziesz w Polityce Prywatności.