Loop only looping once with counter (void loop())

I’m new to Arduino and the c++ language, and with the need to extend an IOT project of mine I decided to buy some node MCU boards (esp8266). I’m using one to measure temp and humidity, but having problems getting my head around a loop within void loop(). The problem being it only runs that loop once even when in my head the conditions are met (obviously, not though :frowning: ). Pulling the last of my hair out with this so any help would be great.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
#define DHTPIN 0
#define DHTTYPE DHT11


const char *ssid =  "******";   // cannot be longer than 32 characters!
const char *pass =  "******";   //

// Update these with values suitable for your network.
IPAddress server(*.*.*.*);

void callback(const MQTT::Publish& pub) {
  // handle message arrived

WiFiClient wclient;
PubSubClient client(wclient, server);

// GPIO set up

int ledPin = 16; // GPIO16

// Temporary variables
static char celsiusTemp[7];
static char fahrenheitTemp[7];
static char humidityTemp[7];

void setup() {
  // Setup console

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.print("Connecting to ");
    WiFi.begin(ssid, pass);

    if (WiFi.waitForConnectResult() != WL_CONNECTED)
    Serial.println("WiFi connected");

  if (WiFi.status() == WL_CONNECTED) {
    if (!client.connected()) {
      Serial.println("Connecting to MQTT server");
      if (client.connect(MQTT::Connect("arduinoClient")
       .set_auth("user", "pass"))) {
        Serial.println("Connected to MQTT server");
  client.publish("presense","Outside node is back online");
 loop(); {
    static int counter = 0;

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  char msgBufferTemp[20];           // make sure this is big enough to hold your string
  char msgBufferHumidity[20];
  if (client.connected()){
    Serial.print("Sending tester: ");
    if (client.publish("home/outside/temp", dtostrf(hic, 6, 2, msgBufferTemp))) {
      Serial.println("Publish ok");
    else {
      Serial.println("Publish failed");
     if (client.publish("home/outside/humidity", dtostrf(h, 6, 2, msgBufferHumidity))) {
      Serial.println("Publish ok");
    else {
      Serial.println("Publish failed");
      } else {
        Serial.println("Could not connect to MQTT server");   

    if (client.connected())

loop(); {Recursion. Not useful, not a Good Thing

thanks for the feedback. :slight_smile: What would be a more suitable option, I need the loop to run only if if (WiFi.status() == WL_CONNECTED) conditions are met but return if they don't. I'm assuming it can't be running out of memory due to recursion, after one loop pass?

Put into a function the code you only want to run when a condition is met. Then call that function when appropriate.

void loop() {
   // other stuff
   if (WiFi.status() == WL_CONNECTED) {
   // more stuff

void myFunction() {
   Serial.println("Hello World");

If appropriate you could also put code in the function that checks if it has already been called so it only produces its output once.

Planning and Implementing a Program

Thank you for getting back, I'm just trying to get my head around what's going on. am I right in assuming that

 if (client.connected())

Is calling void loop()?

No, you are not right in assuming that.
You’re calling the client object’s loop method