Trouble Shooting Println Functions and Debugging Advice Request

Hello all,

I'm having an issue with my code on the Arduino IDE where I am using println statements to test the output of my variables as my code goes through the loop. My code will print some of the lines and the last variable but not all of them and I am unsure as to why. I have added delays after each test and I've tried adding serial.flush() after each as well to no avail. The code I am posting is for an ESP8266 connecting to a server and exchanging Json Web Tokens for validation. The code executes but I wan't to make sure each variable is what it should be at each stage. I've attached my code and removed some of the private information as well as a printout of the code when it executes. You can see the blank lines where either the Serial.println doesn't function correctly because of an error or because I am transferring blank values which would be bad as well. I have validated my syntax for the coding and decoding of the tokens in a seperate program so I am confident that much is correct. Any help would be much appreciated! Thank you!

//initial secret
String secret="xxxxx";

//hold token strings
String tokenA;
String valtoken;
String tokenk;
String token2;

//payload variables
String holder;
String payloadJ;

//secret holding variables
String servkey;
String servkey_c;

void setup() {
// Initialize Serial port
      Serial.begin(9600);

//while (!Serial) continue;
//creating initial request payload
     StaticJsonBuffer<200> jsonBuffer;
     JsonObject& doc = jsonBuffer.createObject();

     doc["type"] = "xxx";
     doc["serialNumber"] = "xxxxxx";
     doc["uuid"] = "xxxxxxx";
     doc.printTo(Serial);
     doc.printTo(holder);
//end request payload

//create first registration token payload
     StaticJsonBuffer<200> tokenBuffer;
     JsonObject& tokendoc = tokenBuffer.createObject();
     tokendoc["deviceId"] = "xxxxxxx";
     tokendoc.printTo(Serial);
     tokendoc.printTo(tokenA);

//test output
     Serial.println(tokenA);
     Serial.flush();
     delay(200);
//end first registration token

//create device state token payload
      StaticJsonBuffer<200> statepayload;
      JsonObject& doc2 = statepayload.createObject();
     doc2["deviceId"]="xxxxxxxx";
     doc2.printTo(valtoken);
//test output
     Serial.println(valtoken);
     Serial.flush();
     delay(200);
//end second token

//Initial token encoding
     ArduinoJWT abc = ArduinoJWT(secret);//include secret here
     abc.setPSK(secret);
     tokenk = abc.encodeJWT(tokenA);
//end first token with value passed to tokenk

//initialize WiFi
      delay(100);
// We start by connecting to a WiFi network
      Serial.println();
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
}
     Serial.println("");
     Serial.println("WiFi connected");
     Serial.println("IP address: ");
     Serial.println(WiFi.localIP());
     Serial.print("Netmask: ");
     Serial.println(WiFi.subnetMask());
     Serial.print("Gateway: ");
     Serial.println(WiFi.gatewayIP());
}
int value = 0;

void loop() {
     delay(5000);
     ++value;
     if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
     HTTPClient http; //Declare object of class HTTPClient

//Begin http connection to server
     http.begin("xxxxxxxxxxxx"); //Specify request destination
     http.addHeader("Authorization", "Bearer "+tokenk);
     http.addHeader("Content-Type", "xxxxxxxx");
     http.addHeader("X-Device-ID", "xxxxxxxxx");

//send and receive request and response payloads
     payloadJ=holder;
     int httpCode = http.POST(payloadJ); //Send the request
     String payloadS = http.getString(); //Get the response payload
     Serial.println(httpCode); //Print HTTP return code
     Serial.println(payloadS); //Print request response payload
     delay(200);

//parse payload for encrypted token
     StaticJsonBuffer<200> buffer2;
     JsonObject& js = buffer2.parseObject(payloadS);
     servkey= js["token"].as<String>();
//servkey will hold encrypted token string
     Serial.println(servkey);
     Serial.flush();
     delay(200);

//decode token for new key
     ArduinoJWT bcd = ArduinoJWT(secret);//include secret here
     bcd.setPSK(secret);
     bcd.decodeJWT(servkey,servkey_c);

//test values
     Serial.println(servkey);//original token string
     Serial.flush();
     delay(200);
     Serial.println(servkey_c);//decoded key
     Serial.flush();
     delay(200);

     ArduinoJWT jwt2 = ArduinoJWT(servkey_c);
     jwt2.setPSK(servkey_c);
     token2=jwt2.encodeJWT(valtoken);
//test value
     Serial.println(token2);
     Serial.flush();
     delay(200);

     http.end(); //Close connection

     delay(1000);

//Begin HTTP GET
     http.begin("xxxxxxxxxxx"); //Specify request destination
     http.addHeader("Authorization", "Bearer "+token2); 
     http.addHeader("Content-Type", "xxxxxxxxx");
     http.addHeader("X-Device-ID", "xxxxxxxxx");

     httpCode = http.GET(); //Send the request
     if (httpCode > 0) { //Check the returning code
     String payloadG = http.getString(); //Get the request response payload
     Serial.println(payloadG); //Print the response payload
}

http.end(); //Close connection
}

else

{
     Serial.println("Error in WiFi connection");
}

     Serial.println();
     Serial.println("closing connection");
}

1.) Please fix the whitespace throughout your sketch, it is quite difficult to read
2.) Please format your sketch correctly (indentation) - again, it is quite difficult to read

Power_Broker:
1.) Please fix the whitespace throughout your sketch, it is quite difficult to read
2.) Please format your sketch correctly (indentation) - again, it is quite difficult to read

Apologies! It is now fixed.

You have so many delay()s in loop() that I'm surprised you have the patience to wait for a single iteration of loop() to complete.

When I am debugging a program I usually put a single delay(1000) in loop() so that the data does not scroll off my screen too quickly. Then there is only one line to change when the program works.

If I want permanent time intervals as part of my code I don't use delay(). I use millis() for non-blocking timing as illustrated in Several Things at a Time. Have a look at Using millis() for timing. A beginners guide if you need more explanation.

This sort of code may not work as you expect

if (httpCode > 0) { //Check the returning code
     String payloadG = http.getString(); //Get the request response payload
     Serial.println(payloadG); //Print the response payload
}

Just because one character has been received does not guarantee the whole message has been received. And the huge amount of delays() operates against the ability to check regularly. Have a look at how the code works in the second example in Serial Input Basics. Note how it can check the incoming data several times before it has the complete message. This depends on loop() being able to repeat quickly.

It will be less confusing (while debugging) to check parts of the program one at a time. When you know a piece works there is no longer any need to print debug messages for it.

A key feature of debugging is to develop a hypothesis for what the problem may be and then add debug messages to prove (or disprove) the hypothesis. The alternative of sprinkling debug messages all over the program is just confusing.

...R