Esp8266 + mqtt + dht11 + multitopic

Hi Guys! Im very new to this fascinating world. But i want to learn more and more every day. Now i have a question

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

#define MQTT_SERVER ""
const char* ssid = "";
const char* password = "";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

const int relay1Pin = D0;
const int btnPin =D6;
int buttonState = 0;
char* ledTopic = "test/led";
char* relayTopic = "test/relay";
const char* user = "";
const char* pass = "";
int relayState= 0;
int lastBtnState;
const int ledPin = D2;

 Basic ESP8266 MQTT example

 This sketch demonstrates the capabilities of the pubsub library in combination
 with the ESP8266 board/library.

 It connects to an MQTT server then:
  - publishes "hello world" to the topic "outTopic" every two seconds
  - subscribes to the topic "inTopic", printing out any messages
    it receives. NB - it assumes the received payloads are strings not binary
  - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
    else switch it off

 It will reconnect to the server if the connection is lost using a blocking
 reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
 achieve the same result without blocking the main loop.

 To install the ESP8266 board, (using Arduino 1.6.4+):
  - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
  - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
  - Select your ESP8266 in "Tools -> Board"

void setup() {
  pinMode(relay1Pin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(btnPin, INPUT);
  client.setServer(MQTT_SERVER, 10960);

void setup_wifi() {

  // We start by connecting to a WiFi network
  Serial.print("Connecting to ");

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

  Serial.println("WiFi connected");
  Serial.println("IP address: ");

void callback(String topic, byte* message, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print("] ");
  String messageLed;
  for (int i = 0; i < length; i++) {
    messageLed += (char)message[i];
       if(messageLed == "1"){
        digitalWrite(ledPin, HIGH);
        Serial.print("On Led Topic");
      else if(messageLed == "0"){
        digitalWrite(ledPin, LOW);
        Serial.print("Off Led Topic");

  relayState = digitalRead(relay1Pin);
  if(topic=="test/relay") {
      if(messageLed == "1"){
        Serial.println ("turning Relay On");
         digitalWrite(relay1Pin, HIGH);
      }else if(messageLed == "0"){
        digitalWrite(relay1Pin, LOW);
         Serial.println ("turning Relay OFF");


void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client", user,pass)) {
      // Once connected, publish an announcement...
      client.publish(relayTopic, "hello world");
      // ... and resubscribe
    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
void loop() {
 buttonState = digitalRead(btnPin);
// read the state of the relay PIN:
relayState = digitalRead(relay1Pin);

if (buttonState != lastBtnState) {

  if (relayState == HIGH) {
    client.publish(relayTopic, "Apagado");
 if (relayState == LOW) {
    client.publish(relayTopic, "Encendido");
lastBtnState= buttonState;
  if (!client.connected()) {


HAve that code to subscribe to more than one topic, but the thing is that i cant read a dht11 sensor and include in the IF section where it waits for the topic, something related to the type of data (string vs float) . I ve been trying to mix this code with the one here for dht22(changing to dht11) But still gettting errors… any help?

Thanks in advance

void callback(String topic, byte* message, unsigned int length) {

You cannot just go changing the expected return datatype without modifying the underlying library code as well.
Better to read the data in the expected *char format and then convert it to a String or forget about String and just compare the chars to your topic.

the thing is that i cant read a dht11 sensor and include in the IF section where it waits for the topic

I have no idea what "IF section where it waits for the topic" even means. The client.loop() function is what checks for subscribers having made a request. That is the function that calls the callback that you registered.

In loop(), you should certainly be able to read a dht11 sensor. In callback() (a really meaningless name for a function), you would simply provide the last sensor reading.

Hi ! Realized that i was doing a mess.. so i started again and completly understood that the message has to be sent and not received. Thanks!