Serial monitor prints out only half of what needs to be printed

hello,
I'm fairly new to coding so i really don't know if this is a problem with my IDE or the code. What must happen when i upload the code is it should print some stuff on the serial. eg: complete msgs, WiFi connected, OK and failed messages. but nothing prints. sometimes when i re-upload the code it shows half of the text but not the complete one.

below is my full code and its in parts because it exceeds 9000 characters

PS i tried changing bud rates on both the code and the serial monitor and at 2400 and 4800 i get just a bit of what needs to be printed but higher than that, NOTHING

please help me solve this issue

Thank You

[code]#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "DHT.h"        // including the library of DHT11 temperature and humidity sensor

/************************* WiFi Access Point *********************************/

#define WLAN_SSID       "TP-LINK_B8B51A"
#define WLAN_PASS       "0312221554"

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "black_ace"
#define AIO_KEY         "8c8e70a6202f4bba984e3603e314f4a8"

/************************* Tempreture *********************************/
#define DHTTYPE DHT11   // DHT 11

#define dht_dpin 1
DHT dht(dht_dpin, DHTTYPE);

/************ Global State (you don't need to change this!) ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

/****************************** Feeds ***************************************/

// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish Temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Temperature");

// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe relay1 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Living");

Adafruit_MQTT_Subscribe relay2 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Kitchen");

Adafruit_MQTT_Subscribe relay3 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Room01");

Adafruit_MQTT_Subscribe relay4 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Room02");


/*************************** Sketch Code ************************************/

// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();

/************************PIR***************************************************/
int calibrationTime = 30;

//the time when the sensor outputs a low impulse
long unsigned int lowIn;

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;

boolean lockLow = true;
boolean takeLowTime;

//int pirPin = 2;    //the digital pin connected to the PIR sensor's output
//int relay = 12;   //the pin connected to the fan

part 1 of the code

void setup(void) {
 /***********************Start the Temperature Sensor*******************/
 dht.begin();
 delay(50);

 Serial.begin(2400);
 Serial.println("Working");



 Serial.println(F("Adafruit MQTT Home Automation"));

 // Connect to WiFi access point.
 Serial.println(); Serial.println();
 Serial.print("Connecting to ");
 Serial.println(WLAN_SSID);

 WiFi.begin(WLAN_SSID, WLAN_PASS);
 while (WiFi.status() != WL_CONNECTED) {
   delay(500);
   Serial.print(".");
 }
 Serial.println();

 Serial.println("WiFi connected");
 Serial.println("IP address: "); Serial.println(WiFi.localIP());

 // Setup MQTT subscription for onoff feed.
 mqtt.subscribe(&relay1);
 mqtt.subscribe(&relay2);
 mqtt.subscribe(&relay3);
 mqtt.subscribe(&relay4);


 pinMode(16, OUTPUT);
 pinMode(5, OUTPUT);
 pinMode(4, OUTPUT);
 pinMode(0, OUTPUT);
 pinMode(2, OUTPUT);
 pinMode(14, OUTPUT);
 pinMode(12, OUTPUT);
 pinMode(13, OUTPUT);
 pinMode(15, OUTPUT);
 pinMode(3, INPUT);
 pinMode(2, INPUT);
 pinMode(1,INPUT);
 /***********************PIR Calibrition************************8*******/
 digitalWrite(2, HIGH);

 //give the sensor some time to calibrate
 Serial.print("calibrating sensor ");
 for (int i = 0; i < calibrationTime; i++) {
   Serial.print(".");
 }
 Serial.println(" done");
 Serial.println("SENSOR ACTIVE");
 delay(50);

}

uint32_t x = 0;


void loop() {
 // Ensure the connection to the MQTT server is alive (this will make the first
 // connection and automatically reconnect when disconnected).  See the MQTT_connect
 // function definition further below.
 MQTT_connect();

 // this is our 'wait for incoming subscription packets' busy subloop
 // try to spend your time here

 Adafruit_MQTT_Subscribe *subscription;
 while ((subscription = mqtt.readSubscription(5000))) {
   if (subscription == &relay1) {
     String lastread = (char *)relay1.lastread;

     if (lastread.equals("HIGH")) {
       Serial.println("living is High");
       digitalWrite(16, LOW);
     }
     else {
       Serial.println("Living is low");
       digitalWrite(16, HIGH);
     }
   }
   if (subscription == &relay2) {
     String lastread = (char *)relay2.lastread;

     if (lastread.equals("HIGH")) {
       Serial.println("Kitchen is high");
       digitalWrite(5, LOW);
     }
     else {
       Serial.println("Kitchen is low");
       digitalWrite(5, HIGH);
     }
   }
   if (subscription == &relay3) {
     String lastread = (char *)relay3.lastread;

     if (lastread.equals("ON")) {
       Serial.println("Room 1 is High");
       digitalWrite(4, LOW);
     }
     else {
       Serial.println("Room 1 is Low");
       digitalWrite(4, HIGH);
     }
   }
   if (subscription == &relay4) {
     String lastread = (char *)relay4.lastread;

     if (lastread.equals("ON")) {
       Serial.println("Room 2 is High");
       digitalWrite(0, LOW);
     }
     else {
       Serial.println("Room 2 is Low");
       digitalWrite(0, HIGH);
     }
   }
 }

Part 2

  // Now we can publish stuff!
 int t = dht.readTemperature();
 if (t >= 50) {
   digitalWrite(14, LOW);
 }
 else {
   digitalWrite(14, HIGH);

 }
 if (! Temperature.publish(t)) {
   Serial.println(F("Failed"));
 } else {
   Serial.println(F("OK"));
   delay(5000);
 }


 // ping the server to keep the mqtt connection alive
 // NOT required if you are publishing once every KEEPALIVE seconds

 // if(! mqtt.ping()) {
 //  mqtt.disconnect();
 //  }

 /**************************************PIR***************************************************************/
 if (digitalRead(2) == HIGH) {
   digitalWrite(12, LOW);   //the led visualizes the sensors output pin state
   if (lockLow) {
     //makes sure we wait for a transition to LOW before any further output is made:
     lockLow = false;
     Serial.println("---");
     Serial.print("motion detected at ");
     Serial.print(millis() / 1000);
     Serial.println(" sec");
     delay(50);
   }
   takeLowTime = true;
 }

 if (digitalRead(2) == LOW) {
   digitalWrite(12, HIGH);  //the led visualizes the sensors output pin state

   if (takeLowTime) {
     lowIn = millis();          //save the time of the transition from high to LOW
     takeLowTime = false;       //make sure this is only done at the start of a LOW phase
   }
   //if the sensor is low for more than the given pause,
   //we assume that no more motion is going to happen
   if (!lockLow && millis() - lowIn > pause) {
     //makes sure this block of code is only executed again after
     //a new motion sequence has been detected
     lockLow = true;
     Serial.print("motion ended at ");      //output
     Serial.print((millis() - pause) / 1000);
     Serial.println(" sec");
     delay(50);
   }
 }
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
 int8_t ret;

 // Stop if already connected.
 if (mqtt.connected()) {
   return;
 }

 Serial.print("Connecting to MQTT... ");

 uint8_t retries = 3;
 while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
   Serial.println(mqtt.connectErrorString(ret));
   Serial.println("Retrying MQTT connection in 5 seconds...");
   mqtt.disconnect();
   delay(5000);  // wait 5 seconds
   retries--;
   if (retries == 0) {
     // basically die and wait for WDT to reset me
     while (1);
   }
 }
 Serial.println("MQTT Connected!");
}

Part 3 and Done

What happens of you run a simple "Hello World" program on your ESP8266 ?
Do you see the serial output from setup() and loop() ?

Once the ESP8266 is running what happens if you reset it ?
Do any/all of the messages change ?

UKHeliBob:
What happens of you run a simple "Hello World" program on your ESP8266 ?
Do you see the serial output from setup() and loop() ?

Once the ESP8266 is running what happens if you reset it ?
Do any/all of the messages change ?

yes normal code works.

in relation to this code nothing from setup and loop

As your code is long please just post your .ino file as an attachment. That way we won't make mistakes joining the bits together.

...R

Here is the ino file

final_pro.ino (8.05 KB)

Robin2:
As your code is long please just post your .ino file as an attachment. That way we won't make mistakes joining the bits together.

I've attached the ino file

final_pro.ino (8.05 KB)

UKHeliBob:
Once the ESP8266 is running what happens if you reset it ?
Do any/all of the messages change ?

yes it does change. shows some other characters and then starts printing hello world again

Robin2:
As your code is long please just post your .ino file as an attachment. That way we won't make mistakes joining the bits together.

Where you able to find the problem?

Where you able to find the problem?

You have not provided enough information. Why are you using such a low baud rate?

Why is Serial.begin() not the FIRST thing in setup()? Why is Serial.print() not the SECOND thing in setup()?

Why are you setting the mode of pins 0 and 1? What Arduino are you using? On the UNO, those are the hardware serial pins, and you should NOT be f**king with them.