0

Refer to

I executed the code in the given link. However i am not able to receive the subscribe message that are send from MQTT test broker(MQTT Lens) on the serial console.

Please help. I am receiving successful call to both publish methods

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "honored";
const char* ss_pswd = "12345678";
const char* mqtt_server = "iot.eclipse.org";


void callback(char* topic, byte* payload, unsigned int length);
WiFiClient EspClient;

PubSubClient client(EspClient);

void wifi_setup() {
  Serial.print("Connecting to: ");
  Serial.println(ssid);
  WiFi.begin(ssid, ss_pswd);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    String ClientId = "ESP8266";
    ClientId += String(random(0xffff), HEX);
    if (client.connect(ClientId.c_str())) {
      Serial.println("connected");
      client.publish("outtopic", "hello world, finally");
      client.subscribe("/room/test");
    } else {
      Serial.print("failed, rc=");
      Serial.println(client.state());
      Serial.println("Try again...");
      delay(5000);
    }
  }
}
void sendMQTTMessage() {
  Serial.println("sending your message");
  if (!client.connected()) {
    reconnect();
  }
  client.publish("/sensor/pir", "on");
}
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived: ");
  Serial.println(topic);
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
}

void setup() {
  Serial.begin(115200);
  wifi_setup();
  client.setServer(mqtt_server, 1883);
  sendMQTTMessage();
  client.setCallback(callback);
  delay(2000);
  Serial.println("going into deep sleep");
  ESP.deepSleep(10 * 1000000);
}

void loop() {

}
  • 2
    Once you enter deepsleep the processor will not be woken up by anything but the reset pulse after the deepsleep time expired. Your callback will never be called. Also once you enter Deepsleep you switch of your WiFi connection, no chance of receiving data. Read diyprojects.io/… – Maximilian Gerhardt May 7 at 16:19
  • Deep sleep proves useless if there are such restrictions in web based application. How do i save battery and do subscribe call? looks like i have to giveup one or another. – Rohit May 8 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.