Please help!! Arduino Mega using ESP-01 with cloud MQTT

Hey everyone

As stated in the topic line, I need help with an IoT project that I am building.

I have been learning to code and have finally completed the code for the Arduino Mega to read all the sensors that I am using.
I also need to be able to control relays hence the use of MQTT.

I now want to take my device online and have been experimenting with code to do so.

For simplicity I have leftonly one switch and one sensor in the code just to get a running code before I try handling them all.

the issues I am facing so far are as follows.

  1. Esp Initializes randomly.

  2. Sometimes i get [WiFiesp] TIMEOUT

  3. I've managed to connect once and send and receive about 6 data sets and then it just reset

  4. sometimes the arduino misses the state change altogether as you can see in the serial monitor.
    Im sending state change messages on an MQTT app or MQTTlens

Iv checked that cloud MQTT receives all my messages from my MQTT app and from MQTTLens.

my circuit is as follows.

ESP-01 is connected to the Arduino Mega on serial 1 through a logic converter.
the ESP-01 is powered by a separate power supply but the 0v are common.
The DHT11 is standard wiring using pin 13

help would be greatly appreciated.

Here is the Test code i'm running.

#include <SPI.h>
#include <WiFiEsp.h> //use for ESP8266
#include <PubSubClient.h>

// Our first sensor, a cheap DHT11 temperature and humidty sensor
#include <DHT.h>
#define DHTPIN 13
#define DHTTYPE DHT11 //21 or 22 also an option
DHT dht(DHTPIN, DHTTYPE);
unsigned long readTime;

// Update these with values suitable for your network.
char ssid[] = "*******"; // your network SSID (name)
char pass[] = "*******"; // your network password
char message_buff[100]; // this buffers our incoming messages so we can do something on certain commands
const char* mqttServer = "farmer.cloudmqtt.com";
const int mqttPort = 16939;
const char* mqttUser = "*******";
const char* mqttPassword = "*******";

WiFiEspClient espClient; //Use this for ESP8266
PubSubClient client(espClient);

const int ledPin26 = 26;
const int ledPin27 = 27;

void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
// Feel free to add more if statements to control more Pins with MQTT
// If a message is received on the topic home/livingroom/arduino/ledPin6
// It's a value between 0 and 255 to adjust the LED brightness
//if(String(topic)=="home/livingroom/arduino/ledPin6"){
//Serial.print("Changing Digital Pin 6 Brithness to ");
//Serial.print(messageTemp);
//analogWrite(ledPin26, messageTemp.toInt());
//}

// If a message is received on the topic home/livingroom/arduino/ledPin7,
//you check if the message is either 1 or 0. Turns the Arduino Digital Pin according to the message
if(String(topic)=="led/test"){
Serial.print("Changing Digital Pin 7 to ");
if(messageTemp == "1"){
digitalWrite(ledPin27, HIGH);
Serial.print("On");
}
else if(messageTemp == "0"){
digitalWrite(ledPin27, LOW);
Serial.print("Off");
}
}

Serial.println();
}

void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
 if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
Serial.println("connected");
// Subscribe or resubscribe to a topic
// You can subscribe to more topics (to control more LEDs in this example)
client.subscribe("home/livingroom/arduino/ledPin6");
client.subscribe("led/test");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void setup()
{
pinMode(ledPin26, OUTPUT);
pinMode(ledPin27, OUTPUT);

Serial.begin(9600);
Serial.println("Starting....");
Serial1.begin(9600);
// initialize ESP module
WiFi.init(&Serial1);
Serial.println("Connecting to WiFi");

// Begin WiFi section
int status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while(true);
}
// print out info about the connection:
else {
Serial.println("Connected to network");
IPAddress ip = WiFi.localIP();
Serial.print("My IP address is: ");
Serial.println(ip);
}
// End WiFi section
  client.setServer(mqttServer, 16939);
  client.setCallback(callback);



dht.begin();
// Allow the hardware to sort itself out
delay(1500);

readTime = 0;
}

void loop()
{

if (!client.connected()) {
reconnect();
}
  delay(1000);
client.loop();
  delay (1000);
//check if 5 seconds has elapsed since the last time we read the sensors.
if(millis() > readTime+6000){
sensorRead();
}

}

void sensorRead(){
readTime = millis();
// 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!");
return;
}

// 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 buffer[10];
dtostrf(t,0, 0, buffer);
client.publish("openhab/himitsu/temperature",buffer);
//Serial.println(buffer);
dtostrf(h,0, 0, buffer);
client.publish("openhab/himitsu/humidity",buffer);

//client.publish("inTopic/humidity",sprintf(buf, "%f", h));
/*Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F"); */
}

Please always do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

How much current can your power supply for the ESP8266 provide?

I would recommend changing the type of messageTemp to a null terminated char array (string) and update your code accordingly. You'll need to use strcmp() for the comparisons instead of ==:
http://www.cplusplus.com/reference/cstring/strcmp/