[SOLVED] Email Orb not working

Hello everyone! I had recently purchased HanRun's ethernet shield off of eBay. Just today I was trying my first project with it. RadioShack's great create Email Orb!!! I have the php file up and running and checking my email, but the ethernet sheild won't connect to the internet. Can you give me some advice? Here's the code:

/*
  The "Email Orb": This is an internet-connected device
  that shows you how many new emails are waiting for 
  you in your gmail account's inbox by pulsing an attached
  LED: one pulse per message.  If there are more than 10 
  messages, the LED will remain on. 
  
  This project requires an Ethernet Shield and an LED (with
  an appropriate resistor) connected to pin 9. It also requires
  a "proxy" php script to handle the https request to your
  gmail account and xml parsing.
  
  IMPORTANT: Read the comments below, as you will need
  to edit this file before compiling and uploading!
 */

#include <SPI.h>
#include <Ethernet.h>

// set TEST_MODE = true to pulse LED 3x without 
// making network connection to remote server.
// Handy for making sure your electronics work!
boolean TEST_MODE = false;

// BEFORE UPLOADING:
// You will need to set some of the configuration options below:

// Set your ethernet sheild's hardware address:
// Uncomment the line below and replace each "??" with the
// values from the sticker on your Ethernet Shield box.

byte mac[] = {  0x55, 0xCB, 0xFE, 0x41, 0xED, 0x3F, };

// Uncomment the line below and replace the current
// value with IP address of the server you uploaded
// your PHP script to.  Note that the octets
// are sperated by commas, not periods.

IPAddress server(31,170,160,112);

// Uncomment the line below and replace with the
// host name of your php server:

char server_name[] = "phpmaster.comule.com";

// Uncomment the line below and replace with the
// path to your php script on the server:

char script_path[] = "/checkGmail.php";

// Some other config options (these defaults should be OK):
// interval between checking email (in millis):
unsigned long GET_INTERVAL = 1000 * 30; 
// Affects rate of pulse:
int RAMP_DELAY = 3; 
// LED output pin:
int LED_PIN = 9;
// end configuration options

// global variables:
long lastGet = 0 - GET_INTERVAL;
int inboxCount = 0;

EthernetClient client;

void setup() {
  Serial.begin(9600);
  pinMode(LED_PIN, OUTPUT);
  if (!TEST_MODE) {
    /*
      Leave the Ethernet.begin(mac) statement below as-is if your network
      uses DHCP to assign IP addresses.  To assign a static IP address,
      use one of the following forms:
      
        Ethernet.begin(mac, ip); 
        Ethernet.begin(mac, ip, dns); 
        Ethernet.begin(mac, ip, dns, gateway); 
        Ethernet.begin(mac, ip, dns, gateway, subnet); 
        
     For more info:
       http://arduino.cc/en/Reference/EthernetBegin
    */
    if (Ethernet.begin(mac) == 0) {
      Serial.println("Failed to initialize ethernet!");
      // Failed, so do nothing, forever:
      for(;;)
        ;
    }
    // Allow Shield time to initialize:
    delay(1000);
  }
}

void loop()
{
  // Main loop: check for messages every GET_INTERVAL millis:
  unsigned long now = millis();
  if ( ( now - lastGet ) > GET_INTERVAL ) {
    lastGet = now;
    int result = getInboxCount();    
    if ( result != -1 ) {
      inboxCount = result;
      inboxCount = inboxCount - 69;
    } 
    Serial.print("inboxCount:");
    Serial.println(inboxCount);     
  } 
  else {
    pulseInboxCount();
  }
}

int getInboxCount() {
  if (TEST_MODE) {
    Serial.println("TEST MODE: returning dummy inbox count");
    return 3;
  } else {
    Serial.println("connecting...");
    // if you get a connection, report back via serial:
    if (client.connect(server, 80)) {
      Serial.println("connected");
      // Make an HTTP request:
      client.print("GET ");
      client.println(" HTTP/2.0");
      client.print("Host: ");
      client.println();
      
      boolean weAreReadingTheNumber = false;
      // let's arbitrarily cap the number at 2 digits,
      // and initialize it with spaces:
      char inboxCountStr[] = {"  "};
      byte strPos = 0;
      int inboxCountInt;
      while(client.connected()) {
        if (client.available()) {
          char c = client.read();
          //Serial.print(c);    
          if (weAreReadingTheNumber) {
            if ( c == ']') {
              inboxCountInt = atoi(inboxCountStr);
              client.stop();
              return(inboxCountInt);
            } 
            else if (strPos > 1) {
              Serial.println("WARNING: Exceeding allowed number of digits");
              client.stop();
              return 99; // use the max of 99 if more than 2 digits
            } 
            else {
              inboxCountStr[strPos++] = c;
            }
          } 
          else {
            if ( c == '[' ) {
              weAreReadingTheNumber = true;
            }     
          }
        }
      }
      Serial.println("lost connection before data was read");
      client.stop();
      return -1;
    } 
    else {
      // if you didn't get a connection to the server:
      Serial.println("connection failed");
      return -1;
    }
  }
}

void pulseInboxCount() {
  //Serial.print("pulse count:");
  //Serial.println(inboxCount); 
  if (inboxCount < 10) {
    analogWrite(LED_PIN,0);
    for ( int i = 0 ; i < inboxCount ; i ++ ) {
      singlePulse();
    }
    // ensure pin is off between sets of pulses:
    analogWrite(LED_PIN,0);
  } 
  else { // if 10 ore greater, steady on:
    analogWrite(LED_PIN,255);
  }
  delay(1000); // delay between sets of pulses
}

void singlePulse() {
  int v;
  // After some experimentaiton, I adjusted starting/ending 
  // values of loops to create smoother sets of pulses.
  // Ramp brightness up:
  for ( v = 50 ; v < 256 ; v++ ) {  
    analogWrite(LED_PIN,brightness(v));
    delay(RAMP_DELAY);
  }
  // Ramp brightness down:
  for (v = 255 ; v > 50 ; v-- ) {
    analogWrite(LED_PIN,(int)brightness(v));
    delay(RAMP_DELAY);
  }
}

/* 
 Brightness function: this is used to translate a linear range of values
 to something resembling an exponential curve.  This is useful because
 humans perceive phenomena such as brightness logarthmically, not linearly.
 The result is an LED that appears to pulse much more evenly. 
 */
int brightness(int input) {
  //return (int)(pow(input,4)*255.0);
  return (int)(pow((float)input / 255.0, 4) * 255.0);
}

but the ethernet sheild won't connect to the internet.

All that you have asserted is that the ethernet shield won't connect to one server. That's a long way from not being able to connect to the internet.

Have you tried connecting to other servers?

What is a "HanRun's ethernet shield" anyway?

this is a hanrun: http://www.ebay.com/itm/New-High-Grade-Ethernet-Shield-W5100-For-Arduino-2009-UNO-Mega-1280-2560-EP98-/130970922911?pt=LH_DefaultDomain_0&hash=item1e7e79af9f

anyway, no. But I want to connect to this server. Is there any troubleshooting I could do???

Are you connecting to the server? Even if you connect to the server at that ip (which should give you an error 404), this is a malformed request.

      client.print("GET ");
// missing the page you want here
// and stick to HTTP/1.0 below for now
      client.println(" HTTP/2.0");
      client.print("Host: ");
// missing your domain (server_name) here
      client.println();

The error that's coming through in serial monitor is "Unable to initialize ethernet"

Is there any troubleshooting I could do???

Yes... Try connecting to a different server... Maybe you need more punctuation in your code... You certainly have more than enough in your posts!!!

What are you connecting the other end of the ethernet cable to? A router?

Edit: Where is your pinMode(10, OUTPUT); statement?

If there is no dhcp server on that localnet, this will fail.

    if (Ethernet.begin(mac) == 0) {
      Serial.println("Failed to initialize ethernet!");

Until this works, you will connect to NOTHING!

My output is in setup:

pinMode(LED_PIN, OUTPUT);

I am connected to a DHCP enabled router. I've also tried connecting to the ethernet jack on my computer but that didn't work either.

SurferTim:
Are you connecting to the server? Even if you connect to the server at that ip (which should give you an error 404), this is a malformed request.

      client.print("GET ");

// missing the page you want here
// and stick to HTTP/1.0 below for now
     client.println(" HTTP/2.0");
     client.print("Host: ");
// missing your domain (server_name) here
     client.println();

Tried all this and still not working...

http://flic.kr/p/fKqquB

Does anyone else have any other ideas?

The Ethernet client part looks fairly conventional and implies that your hardware is compatible with the standard Ethernet library. Have you tried running the standard client example sketches?

PeterH:
The Ethernet client part looks fairly conventional and implies that your hardware is compatible with the standard Ethernet library. Have you tried running the standard client example sketches?

Yep! The example code works like a charm. Are you saying that my shield isn't compatible with this code?

BTW Here's the link to the project page: http://www.radioshack.com/graphics/uc/rsk/Support/ProductManuals/RSDIY_Email_OrbInstruct_Rev20120926.pdf

sigilwig444:
Yep! The example code works like a charm.

Good, that shows that your hardware is compatible with the standard library and everything is basically working.

If the sketch you found doesn't work, you need to compare the networking related code in that against the working examples and see what it's doing differently. Your problems seem to occur at a very early stage so presumably it's just some difference in the way you're initialising the network.

When you run your Orb code do you still get this message?

Failed to initialize ethernet!

And you don't get that message with any other ethernet dhcp example code like DhcpAddressPrinter?

SurferTim:
When you run your Orb code do you still get this message?

Failed to initialize ethernet!

And you don't get that message with any other ethernet dhcp example code like DhcpAddressPrinter?

When I run my orb code I now get this message:

"connecting...
connected
lost connection before data was read
inboxCount:0"

And no, DHCPAdressPrinter works fine. I now know that my IP Address is 192.168.1.1 :slight_smile:

Show me what you replaced this malformed request with.

     client.print("GET ");
// missing the page you want here
// and stick to HTTP/1.0 below for now
      client.println(" HTTP/2.0");
      client.print("Host: ");
// missing your domain (server_name) here
      client.println();

Here is an example from my client code.
http://playground.arduino.cc/Code/WebClient

  if(client.connect(ipBuf,thisPort))
  {
    Serial.println(F("connected"));

    sprintf(outBuf,"GET %s HTTP/1.1",page);
    client.println(outBuf);
    sprintf(outBuf,"Host: %s",serverName);
    client.println(outBuf);
    client.println(F("Connection: close\r\n"));
  } 
  else
  {
    Serial.println(F("failed"));
    return 0;
  }

My output is in setup:

pinMode(LED_PIN, OUTPUT);

Does LED_PIN have a value of 10? No, it does not.

Regardless of whether you think you need to, or not, you MUST set pin 10 to OUTPUT. The default is INPUT for all pins. The example does set pin 10 to OUTPUT, and it works. Your code does not, and it doesn't work. See a pattern?

PaulS:
My output is in setup:

pinMode(LED_PIN, OUTPUT);

Does LED_PIN have a value of 10? No, it does not.

Regardless of whether you think you need to, or not, you MUST set pin 10 to OUTPUT. The default is INPUT for all pins. The example does set pin 10 to OUTPUT, and it works. Your code does not, and it doesn't work. See a pattern?

Do you want me to set all of my pins to output or just 10 then???

PaulS:
My output is in setup:

pinMode(LED_PIN, OUTPUT);

Does LED_PIN have a value of 10? No, it does not.

Regardless of whether you think you need to, or not, you MUST set pin 10 to OUTPUT. The default is INPUT for all pins. The example does set pin 10 to OUTPUT, and it works. Your code does not, and it doesn't work. See a pattern?

I tried setting pin 10 to output and it still says:
"connecting...
connected
lost connection before data was read
inboxCount:0"
I'M STUCK UGHHH!!!!!