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:
/*
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;
}
}
}
}
}
// 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?
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.
//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?
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;
}
}
}
}
}
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 ;))
#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();
}
}