Process MQTT Message after Deep SLeep

Hello,

Apologies if this is a bit of a stupid question, however I have been struggling with it for a while now.

I have an ESP8266 connected to a BME280 that sends weather readings to my HomeAssistant via MQTT. Once readings have been sent, the ESP8266 goes into Deep Sleep for 10mins.

My question is whilst the ESP8266 is awake, is it possible to receive an MQTT Message from another topic? I have tried the code below and it doesn’t ever seem to receive any messages on the “topic/message” topic. Any support appreciated.

#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

//#define BME_SCK 13
//#define BME_MISO 12
//#define BME_MOSI 11
//#define BME_CS 10

#define MQTT_VERSION MQTT_VERSION_3_1_1

#define SEALEVELPRESSURE_HPA (1013.25)
#define INT_STR_SIZE 16

Adafruit_BME280 bme; // I2C

unsigned long delayTime;
unsigned int raw = 0;
float volt = 0.0;

// Define SDA and SCL Pins
//#define PIN_WIRE_SDA (4)
//#define PIN_WIRE_SCL (5)

// Time to sleep (in mins):
const int sleepTimeM = 1;

// Wifi Details
const char* ssid = "******";
const char* password = "********";

// MQTT: ID, server IP, port, username and password
const PROGMEM char* MQTT_CLIENT_ID = "bme280_indoor";
const PROGMEM char* MQTT_SERVER_IP = "********";
const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
const PROGMEM char* MQTT_USER = "**********";
const PROGMEM char* MQTT_PASSWORD = "*******";

//Sensors
const PROGMEM char* MQTT_INDOOR_TEMP_TOPIC = "sensor/bme280/indoor/temp";
const PROGMEM char* MQTT_INDOOR_HUMIDITY_TOPIC = "sensor/bme280/indoor/humidity";
const PROGMEM char* MQTT_INDOOR_PRESSURE_TOPIC = "sensor/bme280/indoor/pressure";
const PROGMEM char* MQTT_BATTERY_VOLTAGE_TOPIC = "sensor/batteryvoltage";

// buffer used to send/receive data with MQTT
const uint8_t MSG_BUFFER_SIZE = 20;
char m_msg_buffer[MSG_BUFFER_SIZE];

String payload;


WiFiClient wifiClient;
PubSubClient client(wifiClient);

void setup (void)
{

  Serial.begin(9600);
  delay(100);

  pinMode(D0, WAKEUP_PULLUP);

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

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

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

  // init the MQTT connection
  client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
  client.setCallback(callback);

  if (!client.connected()) {
    Serial.println("Reconnecting!");
    reconnect();
  }

  client.subscribe("topic/message");

  publishMeasurements();

  publishFakeMeasurements();

  //Delay before sleeping
  delay (10000);
  client.disconnect();

  // Going into a Deeeeep Sleeeeeep
  WiFi.disconnect();
  Serial.println(" Going into a deep sleeeeeeeeeep");
  ESP.deepSleep(60000000 * sleepTimeM);
  delay (500);
}

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


void reconnect() {
  // Loop until we're reconnected
  while (!client.connected())
  {
    Serial.println("INFO: Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD))
    {
      Serial.println("INFO: MQTT Connected");
      Serial.println("Subscribe to Wind Speed Topic");
      //Subscribe to windspeed topic
      client.subscribe("topic/message");
    }
    else
    {
      Serial.print("ERROR: failed, rc=");
      Serial.print(client.state());
      Serial.println("DEBUG: try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}


// function called to publish the variables
void publishMeasurements() {

  bool status;

  // default settings
  status = bme.begin(0x76);
  if (!status) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
  }

  char buffer[INT_STR_SIZE];


  //  ****************  TEMPERATURE (C) ************************
  dtostrf(bme.readTemperature(), 3, 0, buffer);
  Serial.print("Temperature = ");
  Serial.print(buffer);
  Serial.println(" *C");
  client.publish(MQTT_INDOOR_TEMP_TOPIC, buffer);


  //  ****************  HUMIDITY  *********************
  dtostrf(bme.readHumidity(), 3, 0, buffer);
  Serial.print("Humidity = ");
  Serial.print(buffer);
  Serial.println(" %");
  client.publish(MQTT_INDOOR_HUMIDITY_TOPIC, buffer);


  //  ****************  PRESSURE  *********************
  dtostrf(bme.readPressure() / 100.0F, 3, 2, buffer);
  Serial.print("Pressure = ");
  Serial.print(buffer);
  Serial.println(" hPa");
  client.publish(MQTT_INDOOR_PRESSURE_TOPIC, buffer);


  //  ****************  ALTITUDE  *********************
  //dtostrf(bme.readAltitude(SEALEVELPRESSURE_HPA), 3, 0, buffer);//   +4 to correct altitude
  //Serial.print("Approx. Altitude = ");
  //Serial.print(buffer);
  //Serial.println(" m");
  //client.publish(MQTT_INDOOR_ALTITUDE_TOPIC, buffer);


  //  ****************  VOLTAGE  *********************
  //  Code to calculate Battery Voltage
  pinMode(A0, INPUT);
  raw = analogRead(A0);
  volt = raw / 1023.0;
  volt = volt * 4.2;
  dtostrf(volt, 3, 2, buffer);

  Serial.print("Battery Voltage = ");
  Serial.print(volt);
  Serial.println(" v");
  client.publish(MQTT_BATTERY_VOLTAGE_TOPIC, buffer);
  Serial.println();
}

void publishFakeMeasurements()
{

  //  ****************  APPARENT TEMP  *********************
  //  Code to calcukate apparent temp
  char buffer[INT_STR_SIZE];
  //dtostrf("3", 3, 2, buffer);

  Serial.print("Wind Speed = ");
  Serial.print(payload);
  Serial.println(" m/s");
  client.publish(MQTT_BATTERY_VOLTAGE_TOPIC, buffer);
  Serial.println();
}


void callback(char* p_topic, byte* p_payload, unsigned int p_length)
{
  Serial.print("Message arrived in topic: ");
  Serial.println(p_topic);

  // concat the payload into a string
  for (uint8_t i = 0; i < p_length; i++)
  {
    payload.concat((char)p_payload[i]);
  }
  Serial.print("Message Received: ");
  Serial.println(payload);
}

My question is whilst the ESP8266 is awake, is it possible to receive an MQTT Message from another topic?

Of course.

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

After your 10 minute nap is over, you completely ignore the MQTT client you are running. It's no woonder that you never receive any messages you have subscribed to.

PaulS: Of course.

Care to elaborate?

PaulS: After your 10 minute nap is over, you completely ignore the MQTT client you are running. It's no woonder that you never receive any messages you have subscribed to.

I have commented it out as the Wemos needs to go into Deep Sleep i.e. just run void setup()

Care to elaborate?

I shouldn't need to. You asked a question that required a Yes or No answer. I think that it is obvious that my answer falls into the Yes category.

I have commented it out as the Wemos needs to go into Deep Sleep i.e. just run void setup()

So, it never needs to wake up and do anything else? Well, then, what is the problem?

PaulS: I shouldn't need to. You asked a question that required a Yes or No answer. I think that it is obvious that my answer falls into the Yes category. So, it never needs to wake up and do anything else? Well, then, what is the problem?

Well, to be honest, he also asked for "any help" and asked a follow up question, but you are right, you do not "need to", but you could have. Many "yes/no" questions are in fact "how do I do this?" questions.

Anyway your 5 star membership with matching elitist attitude put that newbie in his place. He never returned a reply. How dare he ask stupid questions to begin with.