while (!client.connected()) // Loop until we're reconnected
{
Serial.print("Attempting MQTT connection...");
// client.publish("garden", "Attempting MQTT connection using ESP_MQTT-PUB-SUB...");
String clientId = "ESP8266Client-"; // Create a random client ID
clientId += String(random(0xffff), HEX);
char c_string[16];
dtostrf(temp2, 4, 2, c_string);
client.publish("temperature", c_string);
dtostrf(hum2, 4, 2, c_string);
client.publish("humidity", c_string);
Lets look at just this snippet. While there is no client connected, publish some data using that non-connected client. Hmmm. I don't think Mr. Spock would approve.
if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) // Attempt to connect, just a name to identify the client
{
Serial.println("connected");
// client.publish("garden", "connected");
// client.publish("garden", "hello world"); // Once connected, publish an announcement...
client.publish("Temp", " Temp = ", temp2, " F");
delay(1000); //Delay 1 sec.
client.publish("Humidity", " Humidity = ", hum2, " %");
delay(1000); //Delay 1 sec.
Then, we try to connect. If successful, we publish again, using a different method that takes 4 arguments.
The method that PubSubClient has for publishing are:
boolean publish(const char* topic, const char* payload);
boolean publish(const char* topic, const char* payload, boolean retained);
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
None of them take the 4 arguments you are supplying. So, you need to explain what you are trying to accomplish with the 4 non-commented out calls to publish().
You also need to explain the sticking your head in the sand calls to delay().