Go Down

Topic: [SOLVED] Email Orb not working (Read 4444 times) previous topic - next topic

sigilwig444

Sep 05, 2013, 11:38 pm Last Edit: Sep 15, 2013, 09:22 pm by sigilwig444 Reason: 1
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:

Code: [Select]

/*
 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);
}

PaulS

Quote
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?

sigilwig444

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???

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.
Code: [Select]
     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();


sigilwig444

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

PaulS

#5
Sep 06, 2013, 12:10 am Last Edit: Sep 06, 2013, 12:12 am by PaulS Reason: 1
Quote
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?

SurferTim

If there is no dhcp server on that localnet, this will fail.
Code: [Select]
    if (Ethernet.begin(mac) == 0) {
      Serial.println("Failed to initialize ethernet!");

Until this works, you will connect to NOTHING!

sigilwig444

My output is in setup:
Code: [Select]

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.

sigilwig444


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.
Code: [Select]
     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...

sigilwig444


sigilwig444

Does anyone else have any other ideas?

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?
I only provide help via the forum - please do not contact me for private consultancy.

sigilwig444


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

PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

SurferTim

When you run your Orb code do you still get this message?
Quote
Failed to initialize ethernet!

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

Go Up