Arduino publish to MQTT server

I have an Arduino mega setup with multiple input and output devices. This has been a growing project for two years and I have learned so much yet I have tons more to learn. I have an automated weather and garden watering system that publishes data from my Arduino (via ESP8266-01) in my garden to a MQTT server that is also running Node-Red running in my house. Everything works great except that when it comes to receiving the data on what solenoids are running I only see “Watering section 1” even though it has already been through all of 10 solenoids. I can see my temp and humidity inside and outside of my bee hive, the soil moisture, battery voltage, and connection status.

void Solenoid()
{ int count = 0;       // Our counter
  char stgFromFloat[10];

  char msgToPublish[60];

  count++;  // add one (1) to our count
  Serial.print(" count = ");
  Serial.println(count);
  dtostrf(count, 4, 2, stgFromFloat);
  sprintf(msgToPublish, "   count : %s", stgFromFloat);
  client.publish("s2", msgToPublish);

  Serial.println(" Watering Section 1 ");
  sprintf(msgToPublish, "  Watering Section 1");
  client.publish("solenoid1", msgToPublish);
  //delay(1000);       //  waits 1000 milliseconds (1 sec).
  digitalWrite(solenoidRelay1, LOW);  //  turns on
  delay(120000);      //  waits 120000 milliseconds (2 min).
  digitalWrite(solenoidRelay1, HIGH); //  turns off
  Serial.println(" Solenoid 1 is OFF ");
  sprintf(msgToPublish, " Solenoid 1 is OFF");
  client.publish("solenoid1", msgToPublish);
  delay(2000);       //  waits 2000 milliseconds (2 sec).

  count++;  // add one (1) to our count
  Serial.print(" count = ");   Serial.println(count);
  dtostrf(count, 4, 2, stgFromFloat);
  sprintf(msgToPublish, "   count : %s", stgFromFloat);
  client.publish("s2", msgToPublish);

  Serial.println(" Watering Section 2 ");
  sprintf(msgToPublish, " Watering Section 2 ");
  client.publish("solenoid2", msgToPublish);
  //delay(1000);       //  waits 1000 milliseconds (1 sec).
  digitalWrite(solenoidRelay2, LOW);  //  turns on
  delay(120000);      //  waits 120000 milliseconds (2 min).
  digitalWrite(solenoidRelay2, HIGH); //  turns off
  Serial.println(" Solenoid 2 is OFF ");
  sprintf(msgToPublish, " Solenoid 2 is OFF");
  client.publish("solenoid2", msgToPublish);
  delay(2000);       //  waits 2000 milliseconds (2 sec).

  count++;  // add one (1) to our count
  Serial.print(" count = ");   Serial.println(count);
  dtostrf(count, 4, 2, stgFromFloat);
  sprintf(msgToPublish, "   count : %s", stgFromFloat);
  client.publish("s2", msgToPublish);

  Serial.println(" Watering Section 3 ");
  sprintf(msgToPublish, "  Watering Section 3");
  client.publish("solenoid3", msgToPublish);
  //delay(1000);       //  waits 1000 milliseconds (1 sec).
  digitalWrite(solenoidRelay3, LOW);  //  turns on
  delay(120000);      //  waits 120000 milliseconds (2 min).
  digitalWrite(solenoidRelay3, HIGH); //  turns off
  Serial.println(" Solenoid 3 is OFF ");
  sprintf(msgToPublish, " Solenoid 3 is OFF");
  client.publish("solenoid3", msgToPublish);
  delay(2000);       //  waits 2000 milliseconds (2 sec).

  count++;  // add one (1) to our count
  Serial.print(" count = ");   Serial.println(count);
  dtostrf(count, 4, 2, stgFromFloat);
  sprintf(msgToPublish, "   count : %s", stgFromFloat);
  client.publish("s2", msgToPublish);

  Serial.println(" Watering Section 4 ");
  sprintf(msgToPublish, "  Watering Section 4");
  client.publish("solenoid4", msgToPublish);
  //delay(1000);       //  waits 1000 milliseconds (1 sec).
  digitalWrite(solenoidRelay4, LOW);  //  turns on
  delay(120000);      //  waits 120000 milliseconds (2 min).
  digitalWrite(solenoidRelay4, HIGH); //  turns off
  Serial.println(" Solenoid 4 is OFF ");
  sprintf(msgToPublish, " Solenoid 4 is OFF");
  client.publish("solenoid4", msgToPublish);
  delay(2000);       //  waits 2000 milliseconds (2 sec).

  count++;  // add one (1) to our count
  Serial.print(" count = ");   Serial.println(count);
  dtostrf(count, 4, 2, stgFromFloat);
  sprintf(msgToPublish, "   count : %s", stgFromFloat);
  client.publish("s2", msgToPublish);

  Serial.println(" Watering Section 5 ");
  sprintf(msgToPublish, "  Watering Section 5");
  client.publish("solenoid5", msgToPublish);
  //delay(1000);       //  waits 1000 milliseconds (1 sec).
  digitalWrite(solenoidRelay5, LOW);  //  turns on
  delay(120000);      //  waits 120000 milliseconds (2 min).
  digitalWrite(solenoidRelay5, HIGH); //  turns off
  Serial.println(" Solenoid 5 is OFF");
  sprintf(msgToPublish, " Solenoid 5 is OFF ");
  client.publish("solenoid5", msgToPublish);
  delay(2000);       //  waits 2000 milliseconds (2 sec).

  count++;  // add one (1) to our count
  Serial.print(" count = ");   Serial.println(count);
  dtostrf(count, 4, 2, stgFromFloat);
  sprintf(msgToPublish, "   count : %s", stgFromFloat);
  client.publish("s2", msgToPublish);

  Serial.println(" Watering Section 6 ");
  sprintf(msgToPublish, "  Watering Section 6");
  client.publish("solenoid6", msgToPublish);
  //delay(1000);       //  waits 1000 milliseconds (1 sec).
  digitalWrite(solenoidRelay6, LOW);  //  turns on
  delay(120000);      //  waits 120000 milliseconds (2 min).
  digitalWrite(solenoidRelay6, HIGH); //  turns off
  Serial.println(" Solenoid 6 is OFF ");
  sprintf(msgToPublish, " Solenoid 6 is OFF");
  client.publish("solenoid6", msgToPublish);
  delay(2000);       //  waits 2000 milliseconds (2 sec).

  count++;  // add one (1) to our count
  Serial.print(" count = ");   Serial.println(count);
  dtostrf(count, 4, 2, stgFromFloat);
  sprintf(msgToPublish, "   count : %s", stgFromFloat);
  client.publish("s2", msgToPublish);

  Serial.println(" Watering Section 7 ");
  sprintf(msgToPublish, "  Watering Section 7");
  client.publish("solenoid7", msgToPublish);
  //delay(1000);       //  waits 1000 milliseconds (1 sec).
  digitalWrite(solenoidRelay7, LOW);  //  turns on
  delay(120000);      //  waits 120000 milliseconds (2 min).
  digitalWrite(solenoidRelay7, HIGH); //  turns off
  Serial.println(" Solenoid 7 is OFF");
  sprintf(msgToPublish, " Solenoid 7 is OFF ");
  client.publish("solenoid7", msgToPublish);
  delay(2000);       //  waits 2000 milliseconds (2 sec).

  count++;  // add one (1) to our count
  Serial.print(" count = ");   Serial.println(count);
  dtostrf(count, 4, 2, stgFromFloat);
  sprintf(msgToPublish, "   count : %s", stgFromFloat);
  client.publish("s2", msgToPublish);

  Serial.println(" Watering Section 8 ");
  sprintf(msgToPublish, "  Watering Section 8");
  client.publish("solenoid8", msgToPublish);
  //delay(1000);       //  waits 1000 milliseconds (1 sec).
  digitalWrite(solenoidRelay8, LOW);  //  turns on
  delay(120000);      //  waits 120000 milliseconds (2 min).
  digitalWrite(solenoidRelay8, HIGH); //  turns off
  Serial.println(" Solenoid 8 is OFF ");
  sprintf(msgToPublish, " Solenoid 8 is OFF");
  client.publish("solenoid8", msgToPublish);
  delay(2000);       //  waits 2000 milliseconds (2 sec).

  count++;  // add one (1) to our count
  Serial.print(" count = ");   Serial.println(count);
  dtostrf(count, 4, 2, stgFromFloat);
  sprintf(msgToPublish, "   count : %s", stgFromFloat);
  client.publish("s2", msgToPublish);

  Serial.println(" Watering Section 9 ");
  sprintf(msgToPublish, "  Watering Section 9");
  client.publish("solenoid9", msgToPublish);
  //delay(1000);       //  waits 1000 milliseconds (1 sec).
  digitalWrite(solenoidRelay9, LOW);  //  turns on
  delay(120000);      //  waits 120000 milliseconds (2 min).
  digitalWrite(solenoidRelay9, HIGH); //  turns off
  Serial.println(" Solenoid 9 is OFF ");
  sprintf(msgToPublish, " Solenoid 9 is OFF");
  client.publish("solenoid9", msgToPublish);
  delay(2000);       //  waits 2000 milliseconds (2 sec).

  count++;  // add one (1) to our count
  Serial.print(" count = ");   Serial.println(count);
  dtostrf(count, 4, 2, stgFromFloat);
  sprintf(msgToPublish, "   count : %s", stgFromFloat);
  client.publish("s2", msgToPublish);

  Serial.println(" Watering Section 10 ");
  sprintf(msgToPublish, "  Watering Section 10");
  client.publish("solenoid10", msgToPublish);
  //delay(1000);       //  waits 1000 milliseconds (1 sec).
  digitalWrite(solenoidRelay10, LOW);  //  turns on
  delay(120000);      //  waits 120000 milliseconds (2 min).
  digitalWrite(solenoidRelay10, HIGH); //  turns off
  Serial.println(" Solenoid 10 is OFF ");
  sprintf(msgToPublish, " Solenoid 10 is OFF");
  client.publish("solenoid10", msgToPublish);
  delay(2000);       //  waits 2000 milliseconds (2 sec).

  Serial.println(" All solenoids  are OFF ");
  sprintf(msgToPublish, " All solenoids  are OFF");
  client.publish("solenoid", msgToPublish);

}

[/code]

The data should read… "Watering section 1, Solenoid 1 is off, Watering section 2, Solenoid 2 is "…all the way to 10 then with a final message “All solenoids are off”.
My code is in multiple file and each variable (Hygro, Solenoid, Temp, etc) is called into the main script.
I know that delay is disliked by many including myself but I haven’t learned how to use millis perfectly yet.

This is the output from the serial monitor:

[WiFiEsp] Initilization successful - 2.0.0
Attempting to connect to WPA SSID: AutoGarden
[WiFiEsp] Connected to AutoGarden
You’re connected to the network
IP address is: XXX.XXX.X.XXX
Attempting MQTT connection…[WiFiEsp] Connecting to XXX.XXX.X.XXX
Attempting MQTT connection…[WiFiEsp] Connecting to XXX.XXX.X.XXX
Attempting MQTT connection…[WiFiEsp] Connecting to XXX.XXX.X.XXX
Attempting MQTT connection…[WiFiEsp] Connecting to XXX.XXX.X.XXX
Attempting MQTT connection…[WiFiEsp] Connecting to XXX.XXX.X.XXX
Attempting MQTT connection…[WiFiEsp] Connecting to XXX.XXX.X.XXX
count = 1
Watering Section 1
Solenoid 1 is OFF
count = 2
Watering Section 2
Solenoid 2 is OFF
count = 3
Watering Section 3
Solenoid 3 is OFF
count = 4
Watering Section 4
Solenoid 4 is OFF
count = 5
Watering Section 5
Solenoid 5 is OFF
count = 6
Watering Section 6
Solenoid 6 is OFF
count = 7
Watering Section 7
Solenoid 7 is OFF
count = 8
Watering Section 8
Solenoid 8 is OFF
count = 9
Watering Section 9
Solenoid 9 is OFF
count = 10
Watering Section 10
Solenoid 10 is OFF
All solenoids are OFF
Attempting MQTT connection…[WiFiEsp] Connecting to XXX.XXX.X.XXX

Outside Temp = 81.24 F
Outside Humidity = 99.90 %

Upper Beehive Temp = 88.80 F
Upper Beehive Humidity = 82.00 %

Lower Beehive Temp = 91.86 F
Lower Beehive Humidity = 72.90 %

int count = 0;       // Our counter
  char stgFromFloat[10];

  char msgToPublish[60];

  count++;  // add one (1) to our count
  Serial.print(" count = ");
  Serial.println(count);
  dtostrf(count, 4, 2, stgFromFloat);

It makes little sense to try to convert an int to a string using dtostrf() (double to string, formatted). It makes NO sense to expect the two decimal places in the resulting string to mean jack sh*i.

EVERY time through Solenoid() count will be EXACTLY 1 when you convert it to a string. It makes NO sense to ever expect it to be anything else.

Ok, thank you for that. I'm really not concerned about the count, I just threw that in there to see if the data was being published. My issue is not being able to see what the solenoid is doing.

My issue is not being able to see what the solenoid is doing.

Why can't you SEE that? How can you expect us to SEE that?

I don't understand what you are trying to reference.

Hey thanks for all the help.