Troubleshooting help? WiFi only works when connected to PC

Hi - I’m new to Arduino and this forum, and need some troubleshooting guidance. For my first Arduino project, I built a thing called a weather clock from a project posted by GE. Here’s the page: http://www.gegarages.com/projects/build-a-weather-clock/

It consists of an Arduino Leonardo with a WiFi shield, driving a servo motor which spins a disc to indicate the current weather forecast. As long as I have it plugged into the USB port of my PC and have Arduino running (tried both 1.05 and 1.03), it will connect to my WiFi router and retrieve the necessary data - it works fine. If I unplug it from the PC and plug it into a 9-volt battery or external 9-volt power supply, it will not connect to WiFi. I get the On light on the Leonardo, followed by the Link light, then the L9 comes on in the WiFi shield. The L light then goes off on the Leonardo, and then nothing else happens.

Code is below (router SSID and Password obscured). Any tips on how to troubleshoot this will be appreciated.

[Update: If after it establishes a WiFi connection while plugged into the PC I plug it into external power then unplug the device from my PC, the link light on the WiFi shield stays lit for a few minutes, but then goes out (possibly when it goes to update).

#include <Servo.h>
#include <SPI.h>
#include <WiFi.h>

const boolean DEBUG = true;

enum conditionType {
GE = 0,
CLEAR,
CLOUDY,
RAIN,
THUNDERSTORM,
SNOW
};

Servo dial;

int conditionsIndex[6] = {
1, 1, 9, 12, 11, 20};

const char *conditions[20] = {
// GE
{
“THINKING” }
,
// CLEAR
{
“Clear” }
,
// CLOUDY
{
“Overcast”,
“Scattered Clouds”,
“Partly Cloudy”,
“Shallow Fog”,
“Partial Fog”,
“Mostly Cloudy”,
“Fog”,
“Fog Patches”,
“Smoke” }
,
// RAIN
{
“Drizzle”,
“Rain”,
“Hail”,
“Mist”,
“Freezing Drizzle”,
“Patches of Fog”,
“Rain Mist”,
“Rain Showers”,
“Unknown Precipitation”,
“Unknown”,
“Low Drifting Widespread Dust”,
“Low Drifting Sand” }
,
// THUNDERSTORM
{
“Thunderstorm”,
“Thunderstorms and Rain”,
“Thunderstorms and Snow”,
“Thunderstorms and Ice Pellets”,
“Thunderstorms with Hail”,
“Thunderstorms with Small Hail”,
“Blowing Widespread Dust”,
“Blowing Sand”,
“Small Hail”,
“Squalls”,
“Funnel Cloud” }
,
// SNOW
{
“Volcanic Ash”,
“Widespread Dust”,
“Sand”,
“Haze”,
“Spray”,
“Dust Whirls”,
“Sandstorm”,
“Freezing Rain”,
“Freezing Fog”,
“Blowing Snow”,
“Snow Showers”,
“Snow Blowing Snow Mist”,
“Ice Pellet Showers”,
“Hail Showers”,
“Small Hail Showers”,
“Snow”,
“Snow Grains”,
“Low Drifting Snow”,
“Ice Crystals”,
“Ice Pellets” }
};

/*

  • FILL IN THESE VARIABLES
    */
    char ssid = “xxxxx”; // your network SSID (name)
    char pass = “xxxxx”; // your network password
    String apiState = “dc”; // the state where you want to montitor weather (in a two letter format)
    String apiCity = “washington”; // the state where you want to monitor weather (each word should be capitalized, replace space with an underscore i.e. New_York).
    String apiKey = “4ad69218f0e5e1a7”; // the wunderground api token

int status = WL_IDLE_STATUS; // the Wifi radio’s status
WiFiClient client;

const unsigned long requestInterval = 600000; // Update every 60 minutes

char serverName = “api.wunderground.com”;
String GETReqeuest = “GET /api/” + apiKey + “/conditions/q/”+ apiState + “/” + apiCity +".xml HTTP/1.1";
boolean requested;
unsigned long lastAttemptTime = 0;

String currentLine = “”;
String weatherCondition = “”;
String forecast;

boolean readingWeather = false;
boolean processingData = true;
boolean clockOriented = true;
int currentCondition;

void setup() {

dial.attach(9);
dial.write(30); // sets servo to thinking orientation
delay(4000);
currentLine.reserve(256);
weatherCondition.reserve(150);

if(DEBUG) {
Serial.begin(9600);

while (!Serial) {
;
}
}

status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) {
if(DEBUG) Serial.println(“Couldn’t get a wifi connection”);
// don’t do anything else:
while(true);
}
else {
if (DEBUG) Serial.println(“Connected to wifi”);
if (DEBUG) Serial.println(“Starting connection…”);
printWifiData();
delay(10000);

connectToServer();
}
}

void loop()
{
checkRequest();
processWeather();
}

void checkRequest() {
if (client.available()) {
char inChar = client.read();
currentLine += inChar;
if (inChar == ‘\n’) {
currentLine = “”;
}
if ( currentLine.endsWith("")) {
readingWeather = true;
weatherCondition = “”;
}
if (readingWeather) {
if (inChar != ‘<’) {
weatherCondition += inChar;
}
else {
readingWeather = false;
weatherCondition = weatherCondition.substring(1, weatherCondition.length());
forecast = weatherCondition;
if(DEBUG) Serial.print("SERVER CONDITION: ");
if(DEBUG) Serial.println(weatherCondition);
client.stop();
clockOriented = false;
processingData = false;
}
}
}
else if (millis() - lastAttemptTime > requestInterval) {
connectToServer();
}
}

void connectToServer() {
if(DEBUG) Serial.println(“connecting to server…”);
if (client.connect(serverName, 80)) {
if(DEBUG) Serial.println(“connected to server”);
client.println(GETReqeuest);

client.println(“Host: api.wunderground.com”);
client.println(“Connection: close”);
client.println();
clockOriented = true;
}
lastAttemptTime = millis();
}

void processWeather(){
if (!processingData && clockOriented == false ){
calculateCondition();
clockOriented = true;
}
}

void calculateCondition(){
if (forecast.indexOf("Light ") >= 0 ) {
forecast.replace("Light ", “”);
}
else if (forecast.indexOf("Heavy ") >= 0 ) {
forecast.replace("Heavy ", “”);
}

if(DEBUG) Serial.print(“forecast: [”);
if(DEBUG) Serial.print(forecast);
if(DEBUG) Serial.println("]");

for ( int i = 0; i < ( sizeof(conditionsIndex) / sizeof(int) ); i++ ) {
int idx = conditionsIndex*;*

* currentCondition = i;*
* }*
* }*
* }*
* orientClock();*
}
void orientClock(){
* int pos = 25;*
* int idx = currentCondition;*
* switch(idx){*
* case 0:*
* if(DEBUG) Serial.println(“THINKING”);*
* pos = 30;*
* break;*
* case 1:*
* if(DEBUG) Serial.println(“CLEAR”);*
* pos = 60;*
* break;*
* case 2:*
* if(DEBUG) Serial.println(“CLOUDY”); *
* pos = 90;*
* break;*
* case 3:*
* if(DEBUG) Serial.println(“RAIN”); *
* pos = 120;*
* break;*
* case 4:*
* if(DEBUG) Serial.println(“THUNDERSTORM”); *
* pos = 150;*
* break;*
* case 5:*
* if(DEBUG) Serial.println(“SNOW”); *
* pos = 180;*
* break;*
* default:*
* pos = 0;*
* break;*
* }*
* for(int i = 0; i < pos; i ++) *
* { *
* dial.write(i); *
* delay(15); *
* }*
* delay(5000);*
}
void printWifiData() {
* if(DEBUG) {*
* // print your WiFi shield’s IP address:*
* IPAddress ip = WiFi.localIP();*
* IPAddress gateway;*
* Serial.print("IP Address: ");*
* Serial.println(ip);*
* // print the SSID of the network you’re attached to:*
* Serial.print("SSID: ");*
* Serial.println(WiFi.SSID());*

gateway = WiFi.gatewayIP();
* Serial.print("GATEWAY: ");*
* Serial.println(gateway);*

* // print the received signal strength:*
* long rssi = WiFi.RSSI();*
* Serial.print(“signal strength (RSSI):”);*
* Serial.print(rssi);*
* Serial.println(" dBm");*
* // print your MAC address:*
* byte mac[6]; *
* WiFi.macAddress(mac);*
* Serial.print("MAC address: ");*
* Serial.print(mac[5],HEX);*
* Serial.print(":");*
* Serial.print(mac[4],HEX);*
* Serial.print(":");*
* Serial.print(mac[3],HEX);*
* Serial.print(":");*
* Serial.print(mac[2],HEX);*
* Serial.print(":");*
* Serial.print(mac[1],HEX);*
* Serial.print(":");*
* Serial.println(mac[0],HEX);*
* }*
}

If I unplug it from the PC and plug it into a 9-volt battery or external 9-volt power supply, it will not connect to WiFi.

More than likely those power supplies are inadequate for your setup. Also #7 below might be helpful.

http://forum.arduino.cc/index.php/topic,148850.0.html

Thanks. I thought of that, but wasn't sure about it for two reasons.

  1. If the +5V from USB will power it, why wouldn't a +9V 650mA power supply do it?
  2. How would that explain that it works only when plugged in and when the Arduino software is running, but not when it is plugged in and the software is not running?

Thanks for helping me with this.

Since DEBUG == True by definition, some how I fear that your

while (!Serial) {
      ;
    }

in the Setup() section has something to do with this, because this procedure is going to block as long it get nothing from serial.

Put a

Serial.println("some thing");

right after it to see if it's where it get stuck