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");
}