Arduino Mega send data to ESP8266-01 then ESP forward to RPI (MQTT)

The other issues were commented out so they weren't used. I had them in there because I wanted to see how the code would respond. Yes they should not have been there, I should have removed them instead of commenting them out.

I used delay because I thought it would slow down the data coming to the terminal on the Pi and it wouldn't be so clustered, basically display smoother.

The blink without delay example shows how to do that without using delay().

Even better, though, would be to publish() only when the temperature or humidity changes.

PaulS:
Even better, though, would be to publish() only when the temperature or humidity changes.

I would definitely like to do that when I get the data streaming to run better. That would be great when I get it set up in my garden and beehive.

Try something like this:

  if(client.connect("ESP8266Client", mqtt_user, mqtt_password))
   {
      char stgFromFloat[10];
      char msgToPublish[60];

      dtostrf(temp2, 4, 2, stgFromFloat);
      sprintf(msgToPublish, "  Temp = %s F", stgFromFloat);

      client.publish("Temp", msgToPublish);

      dtostrf(hum2, 4, 2, stgFromFloat);
      sprintf(msgToPublish, "  Humidity = %s %%", stgFromFloat);

      client.publish("Humidity", msgToPublish);
   }

Thank you for your help. I will try that and respond later.