Forum

ESP32 & MQTT - list...
 
Notifications
Clear all

ESP32 & MQTT - listen for messages from domoticz

1 Posty
1 Users
0 Reactions
697 Widok
0
Topic starter

how to recieve gpio singnal from domoticz and recieve it on remote esp32 using wifi?

1 Answer
0
Topic starter

Example ESP32 MQTT client that listened to motion sensors connected to domoticz using gpio and blink leds.

#include "EspMQTTClient.h"

DynamicJsonDocument doc(512);
EspMQTTClient client(
  "MY_WIFI_NAME",
  "MY_WIFI_PASS",
  "IP",  // MQTT Broker server ip/domoticz
  "HOSTNAME_OF_THIS_DEVICE"      // Client name that uniquely identify your device
);

byte idxTOespPort [255];  //IDX array 0-255


void setup() {
  Serial.begin(115200);

  idxTOespPort[1] = 18;  // IDX (sensor num) to LED PORT BLINK (esp32 port out)
  idxTOespPort[2] = 23;
  idxTOespPort[3] = 16;

  for (byte i = 0; i < sizeof(idxTOespPort) - 1; i++) {
    if(idxTOespPort[i]>0){
      pinMode(idxTOespPort[i], OUTPUT);
      Serial.print(idxTOespPort[i]);
    }
  }

  //client.enableDebuggingMessages();
  client.enableMQTTPersistence(); //required if is non-battery powered
  client.setMaxPacketSize(512); // required! default is to low to recieve messages!
}

void onConnectionEstablished() {
  Serial.println("Connected with wifi.");

  client.subscribe("domoticz/out", [] (const String & payload)  {
    Serial.println(payload);
    deserializeJson(doc, payload);

    const char* sensor = doc["sensor"];
    byte idx                = doc["idx"];
    byte status         = doc["nvalue"];

    if (idx == 1 || idx == 2 || idx == 3) {
      Serial.print("Processing IDX: ");
      Serial.println(idx);
      processLed(idx, status);
    }
  });
}

void loop() {
  client.loop();
}

void processLed(byte idx, byte status) {

  if (status == 1) {
    Serial.print("IDX led enable: ");
    Serial.println(idx);
    enableLed(idxTOespPort[idx]);
  } else {
    Serial.print("IDX led disable: ");
    Serial.println(idx);
    disableLed(idxTOespPort[idx]);
  }
}

void enableLed(byte port) {
  Serial.print("enabling led port num: ");
  Serial.println(port);
  digitalWrite(port, HIGH);
}

void disableLed(byte port) {
  Serial.print("disabling led port num: ");
  Serial.println(port);
  digitalWrite(port, LOW);
}

Odpowiedź

Author Name

Author Email

Your question *

 
Preview 0 Revisions Saved
Share: