Go Down

Topic: Led doesn't stay on (Read 466 times) previous topic - next topic

damvanjohan

Hi,

Im struggling with the following problem. I'm trying to connect to a webpage that tells the arduino how many new emails there are. If the email count is bigger than 0 an LED needs to been turned on, and needs to stay on until the arduino reads that the aren't any unread emails anymore. The connection part is all going well is just i can't seem to get the LED to stay on. My code:


Code: [Select]
/*
  Repeating Web client

This sketch connects to a a web server and makes a request
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
the Adafruit Ethernet shield, either one will work, as long as it's got
a Wiznet Ethernet module on board.

This example uses DNS, by assigning the Ethernet client with a MAC address,
IP address, and DNS address.

Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13

created 19 Apr 2012
by Tom Igoe

http://arduino.cc/en/Tutorial/WebClientRepeating
This code is in the public domain.

*/

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

// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

// initialize the library instance:
EthernetClient client;
char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?
boolean lightstatus = false; // is reading?

char server[] = "www.freelanceasp.net";

// Led
int led = 13;


unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
boolean lastConnected = false;                 // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000;  // delay between updates, in milliseconds
void setup() {
  pinMode(led, OUTPUT);
  // start serial port:
  Serial.begin(9600);
  // give the ethernet module time to boot up:
  delay(1000);
  // start the Ethernet connection using a fixed IP address and DNS server:
  Ethernet.begin(mac);
  // print the Ethernet board/shield's IP address:
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available()) {
    if(readPage()=="0"){
      lightstatus = false;
    }
    else{
      lightstatus = true;
    }
  }

  if(lightstatus==true){
    digitalWrite(led, HIGH);
  }
  if(lightstatus==false){
    digitalWrite(led, LOW);
  }
  // if there's no net connection, but there was one last time
  // through the loop, then stop the client:
  if (!client.connected() && lastConnected) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }

  // if you're not connected, and ten seconds have passed since
  // your last connection, then connect again and send data:
  if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
    httpRequest();
  }
  // store the state of the connection for next time through
  // the loop:
  lastConnected = client.connected();
}

// this method makes a HTTP connection to the server:
void httpRequest() {
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    client.println("GET / HTTP/1.1");
    client.println("Host: www.freelanceasp.net");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();

    // note the time that the connection was made:
    lastConnectionTime = millis();
  }
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
    Serial.println("disconnecting.");
    client.stop();
  }
}

String readPage(){
  //read the page, and capture & return everything between '<' and '>'

  stringPos = 0;
  memset( &inString, 0, 32 ); //clear inString memory

  while(true){

    if (client.available()) {
      char c = client.read();

      if (c == '<' ) { //'<' is our begining character
        startRead = true; //Ready to start reading the part
      }
      else if(startRead){

        if(c != '>'){ //'>' is our ending character
          inString[stringPos] = c;
          stringPos ++;
        }
        else{
          //got what we need here! We can disconnect now
          startRead = false;
          return inString;
        }
      }
    }
  }
}

MarkT

Code: [Select]
    if(readPage()=="0"){


That is not how to compare strings, it compares the addresses the strings are stored at, which will always be different.

You need to compare the contents of the strings:

Code: [Select]
    if (strcmp (readPage(), "0") == 0) {
[ I won't respond to messages, use the forum please ]

PaulS

Code: [Select]
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available()) {
    if(readPage()=="0"){
      lightstatus = false;
    }
    else{
      lightstatus = true;
    }
  }

So, you have a problem that you are trying to debug. Why are you NOT printing what you get from the client?

Code: [Select]
String readPage(){
  //read the page, and capture & return everything between '<' and '>'

You return a char array that is a global from this function as the worst possible type. Stop that. Make this function type void, and remove the return statements.

Use inString where you are currently using the value returned by this function.

MarkT

Ah, hang on, you're using the String class....

That's going to run you out of RAM - replace it with char* style strings throughout the code is my recommendation.
[ I won't respond to messages, use the forum please ]

damvanjohan


Code: [Select]
String readPage(){
  //read the page, and capture & return everything between '<' and '>'

You return a char array that is a global from this function as the worst possible type. Stop that. Make this function type void, and remove the return statements.

Use inString where you are currently using the value returned by this function.


Do you mean like this?

Code: [Select]
void readPage(){
  //read the page, and capture & return everything between '<' and '>'

  stringPos = 0;
  memset( &inString, 0, 32 ); //clear inString memory

  while(true){

    if (client.available()) {
      char c = client.read();

      if (c == '<' ) { //'<' is our begining character
        startRead = true; //Ready to start reading the part
      }
      else if(startRead){

        if(c != '>'){ //'>' is our ending character
          inString[stringPos] = c;
          stringPos ++;
        }
        else{
          //got what we need here! We can disconnect now
          startRead = false;
        }
      }
    }
  }
}

PaulS

Quote
Do you mean like this?

For the function, yes.

damvanjohan

#6
Nov 23, 2012, 12:41 am Last Edit: Nov 23, 2012, 12:43 am by damvanjohan Reason: 1
Ok i cleaned up my code and used the textfinder library instead for my count retrieval.

The led turns on when email > 0 but goes off when the client is disconnected. How is this possible?

(Yes, i had the led on in an if statement when email > 0. Code below is just a try to find the problem ;))

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>

// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

// initialize the library instance:
EthernetClient client;
TextFinder finder( client);
boolean lightstatus = false;
int led = 13;
char server[] = "www.freelanceasp.net";

unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
boolean lastConnected = false;                 // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000;  // delay between updates, in milliseconds
void setup() {
 // start serial port:
 pinMode(led, OUTPUT);  
 Serial.begin(9600);
 // give the ethernet module time to boot up:
 delay(1000);
 // start the Ethernet connection using a fixed IP address and DNS server:
 Ethernet.begin(mac);
 // print the Ethernet board/shield's IP address:
 Serial.print("My IP address: ");
 Serial.println(Ethernet.localIP());

}

void loop() {
 digitalWrite(led, HIGH);

 fillMailCount();
}

void fillMailCount(){
 // if there's incoming data from the net connection.
 // send it out the serial port.  This is for debugging
 // purposes only:
 if (client.available()) {
   if(finder.find("<mail count=") )
   {      
     int mails = finder.getValue();
     Serial.print("Mail count ");  // and echo it to the serial port.
     Serial.println(mails);
     if(mails>0){
       lightstatus = true;
     }
     else{
       lightstatus = false;
     }
   }
 }

 // if there's no net connection, but there was one last time
 // through the loop, then stop the client:
 if (!client.connected() && lastConnected) {
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
 }

 // if you're not connected, and ten seconds have passed since
 // your last connection, then connect again and send data:
 if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
   httpRequest();
 }
 // store the state of the connection for next time through
 // the loop:
 lastConnected = client.connected();
}


// this method makes a HTTP connection to the server:
void httpRequest() {
 // if there's a successful connection:
 if (client.connect(server, 80)) {
   Serial.println("connecting...");
   // send the HTTP PUT request:
   client.println("GET / HTTP/1.1");
   client.println("Host: www.freelanceasp.net");
   client.println("User-Agent: arduino-ethernet");
   client.println("Connection: close");
   client.println();

   // note the time that the connection was made:
   lastConnectionTime = millis();
 }
 else {
   // if you couldn't make a connection:
   Serial.println("connection failed");
   Serial.println("disconnecting.");
   client.stop();
 }
}

wildbill

Rather hard to say - that last post doesn't appear to exhibit the issue - can you post the code that does?

Go Up