Sketch only works when USB connected to PC

Hi

I am using the sketch shown below. It accepts ThingSpeak Talkback commands to switch LED 13 on and off. When I connect the YUN to PC all is well. If I unplug the YUN from the PC and connect to a 1A USB charger the sketch no longer accepts commands.

As I plug the YUN into a USB charger (have tried several with different cable as well), the power light comes on, the WAN light flashes once briefly an then nothing for about a minute, then the USB light comes on and the sketch dos not respond.

I do have access to the OpenWRT side of things though, management interface i active and I can ssh to Open-WRT.

Whats wrong?

#include "Bridge.h"
#include "HttpClient.h"


String thingSpeakAPI = "api.thingspeak.com";

//TalkBack Settings
String talkBackAPIKey = "xxxxxxxxxxxx";
String talkBackID = "xxx";
const int checkTalkBackInterval = 15 * 1000;    


void setup()
{
  // Initialize Bridge
  Bridge.begin();
  // Initialize Serial
  Serial.begin(9600);
  while(!Serial);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  delay(1000);
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  
   
}


void loop()
{  
  // Check ThingSpeak for TalkBack Commands
  checkTalkBack();
  delay(checkTalkBackInterval);
}


void checkTalkBack()
{
  HttpClient client;
  
  String talkBackCommand;
  char charIn;
  String talkBackURL =  "http://" + thingSpeakAPI + "/talkbacks/" + talkBackID + "/commands/execute?api_key=" + 

talkBackAPIKey;
  
  // Make a HTTP GET request to the TalkBack API:
  client.get(talkBackURL);
    
  while (client.available()) {
    charIn = client.read();
    talkBackCommand += charIn;
  }
  
  // Turn On/Off the On-board LED
  if (talkBackCommand == "TURN_ON")
  {  
    Serial.println(talkBackCommand);
    digitalWrite(13, HIGH);
  }
  else if (talkBackCommand == "TURN_OFF")
  {      
    Serial.println(talkBackCommand);
    digitalWrite(13, LOW);
  }
  
  Serial.flush(); 
  delay(1000);
}

while(!Serial);

this are your problem.

loop until the serial port are not initialize.

make like that:

#define debug

#ifdef debug
  while (!Serial);
#endif

comment #define debug when you unplug arduino from your PC.

your code will be this:

#include "Bridge.h"
#include "HttpClient.h"


String thingSpeakAPI = "api.thingspeak.com";

//TalkBack Settings
String talkBackAPIKey = "xxxxxxxxxxxx";
String talkBackID = "xxx";
const int checkTalkBackInterval = 15 * 1000;    


// debug
 // #define debug


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

#ifdef debug
  
  Serial.begin(9600);
  while(!Serial);

#endif
  
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  delay(1000);
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  
   
}


void loop()
{  
  // Check ThingSpeak for TalkBack Commands
  checkTalkBack();
  delay(checkTalkBackInterval);
}


void checkTalkBack()
{
  HttpClient client;
  
  String talkBackCommand;
  char charIn;
  String talkBackURL =  "http://" + thingSpeakAPI + "/talkbacks/" + talkBackID + "/commands/execute?api_key=" + 

talkBackAPIKey;
  
  // Make a HTTP GET request to the TalkBack API:
  client.get(talkBackURL);
    
  while (client.available()) {
    charIn = client.read();
    talkBackCommand += charIn;
  }
  
  // Turn On/Off the On-board LED
  if (talkBackCommand == "TURN_ON")
  {  
  #ifdef debug
    Serial.println(talkBackCommand);
  #endif
digitalWrite(13, HIGH);
  }
  else if (talkBackCommand == "TURN_OFF")
  {     
 #ifdef debug  
    Serial.println(talkBackCommand);
    #endif
    digitalWrite(13, LOW);
  }
   #ifdef debug  
  Serial.flush(); 
  #endif
  delay(1000);
}

Ah perfect, that did the trick! Thanks, a great help, also impressed my first post got a positive quick response.

So the serial monitor is reserved for connecting to PC? Makes sense now. So if I want to use it I need to #debug everything.

The question now is could I use console as debug output so that I can still see traces while not connected to PC and use the same code whether PC connected or not?

Yes you can,

just replace Serial to Console.

PC or not PC will work

take a look Console example:

/*
  ASCII table

 Prints out byte values in all possible formats:  
 * as raw binary values
 * as ASCII-encoded decimal, hex, octal, and binary values

 For more on ASCII, see http://www.asciitable.com and http://en.wikipedia.org/wiki/ASCII

 The circuit:  No external hardware needed.

 created 2006
 by Nicholas Zambetti 
 modified 9 Apr 2012
 by Tom Igoe
 modified 22 May 2013
 by Cristian Maglie

 This example code is in the public domain.

 <http://www.zambetti.com> 

 */

#include <Console.h>

void setup() { 
 //Initialize Console and wait for port to open:
  Bridge.begin();
  Console.begin(); 

  // Uncomment the following line to enable buffering:
  // - better transmission speed and efficiency
  // - needs to call Console.flush() to ensure that all 
  //   transmitted data is sent

  //Console.buffer(64);

  while (!Console) {
    ; // wait for Console port to connect.
  }

  // prints title with ending line break 
  Console.println("ASCII Table ~ Character Map"); 
} 

// first visible ASCIIcharacter '!' is number 33:
int thisByte = 33; 
// you can also write ASCII characters in single quotes.
// for example. '!' is the same as 33, so you could also use this:
//int thisByte = '!';  

void loop() { 
  // prints value unaltered, i.e. the raw binary version of the 
  // byte. The Console monitor interprets all bytes as 
  // ASCII, so 33, the first number,  will show up as '!' 
  Console.write(thisByte);    

  Console.print(", dec: "); 
  // prints value as string as an ASCII-encoded decimal (base 10).
  // Decimal is the  default format for Console.print() and Console.println(),
  // so no modifier is needed:
  Console.print(thisByte);      
  // But you can declare the modifier for decimal if you want to.
  //this also works if you uncomment it:

  // Console.print(thisByte, DEC);  

  Console.print(", hex: "); 
  // prints value as string in hexadecimal (base 16):
  Console.print(thisByte, HEX);     

  Console.print(", oct: "); 
  // prints value as string in octal (base 8);
  Console.print(thisByte, OCT);     

  Console.print(", bin: "); 
  // prints value as string in binary (base 2) 
  // also prints ending line break:
  Console.println(thisByte, BIN);   

  // if printed last visible character '~' or 126, stop: 
  if(thisByte == 126) {     // you could also use if (thisByte == '~') {
    // ensure the latest bit of data is sent
    Console.flush();

    // This loop loops forever and does nothing
    while(true) { 
      continue; 
    } 
  } 
  // go on to the next character
  thisByte++;  
}