title: Control Relay by MQTT Publish Subcribe
body: <p>sorry for my bad english, but i'll try my best</p>
  <p>i have a code like this</p>
  <pre><code>void mqttCallback(char *topic, byte *payload, unsigned int length)
  {
    Serial.print(&quot;Message arrived [&quot;);
    Serial.print(topic);
    Serial.print(&quot;] &quot;);
    for (int i = 0; i &lt; length; i++)
    {
      Serial.print((char)payload[i]);
    }
    Serial.println();
    if (strcmp(topic, DeviceConfig.MqttSub) == 0)
    {
      Serial.print(&quot;Recvd relay command parse code: &quot;);
      StaticJsonDocument&lt;100&gt; doc;
      DeserializationError error = deserializeJson(doc, (char *)payload);
      Serial.println(error.code());
      if (error == DeserializationError::Ok)
      {
        if (doc.containsKey(&quot;state&quot;) &amp;&amp; doc[&quot;state&quot;].is&lt;int&gt;())
        {
          DeviceConfig.RelayOn = (doc[&quot;state&quot;].as&lt;int&gt;() == 1);
          Serial.print(&quot;Changing state: &quot;);
          Serial.print(DeviceConfig.RelayOn );
          Serial.println();
        }
      }
    }
    
  }
  </code></pre>
  <p>it means if the broker send data like {state : 1}, it automactically changing state turn on, or {state : 0} to turn off, it works if i see on serial monitor, the state status is changed, but the relay is not affected at all</p>
  <p>this is the loop function</p>
  <pre><code>void loop()
  {
  
    mqttReconnect();
    if (client.connected() &amp;&amp; millis() - lastSendMs &gt;= (DeviceConfig.MqttPubSec * 1000))
    {
      Serial.print(&quot;Publishing:&quot;);
      String json = composeSensorJson();
      Serial.println(json);
      Serial.println(client.publish(DeviceConfig.MqttPub, json.c_str()) ? &quot;OK&quot; : &quot;FAIL&quot;);
      lastSendMs = millis();
    }
    if (DeviceConfig.Device == Relay) {
      digitalWrite(AIO_SENSOR_PIN_3, DeviceConfig.RelayOn ? LOW : HIGH);
    }
    client.loop();
  }
  </code></pre>
  <p>why the relay is not working</p>
  <hr />
  <p>this is the serial monitor if the state is 1
  <img src="https://i.stack.imgur.com/JSUip.png" alt="state :1" /></p>
  <p>and this is the monitor if state is 0
  <img src="https://i.stack.imgur.com/IsD24.png" alt="state:0" /></p>