Go Down

Topic: help with Duemilanove + Ethernet W5100 (Read 960 times) previous topic - next topic

Fernando_Estevam

Hello Guys,

Sorry, my English don't is very well. But I try.
I'm have issues to do torn ON and OFF two LEDs.
When I turn ON ledPin 4 and I want turn ON ledPin 5, the ledPin 4 turn OFF.
I know that my code have some error.

Does they to help me?

The code whith ONE LED(It Works):

#include <String.h>
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = {
  192, 168, 1, 243 };                       // Endereco IP
byte gateway[] = {
  192, 168, 1, 10 };                    // IP roteador
byte subnet[] = {
  255, 255, 255, 0 };                   //Mascara
Server server(80);                                      //Porta
byte sampledata=50;            //some sample data - outputs 2 (ascii = 50 DEC)
int ledPin = 4;  // Pino do Led utilizado
//char link[]="http://192.168.1.243/"; //link data
String readString = String(30); //string for fetching data from address
boolean LEDON = false; //LED status flag
void setup(){
  //iniciar Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  //Set pin 4 to output
  pinMode(ledPin, OUTPUT);
  //enable serial datada print
  Serial.begin(9600);
}
void loop(){
  // Cria uma conexao cliente
  Client client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        //read char by char HTTP request
        if (readString.length() < 30)
        {
          //store characters to string
          readString += c;
        }
        //output chars to serial port
        Serial.print(c);
        //if HTTP request has ended
        if (c == '\n') {
          //lets check if LED should be lighted
          if(readString.indexOf("L=1") >=0)
          {
            //led has to be turned OFF
            digitalWrite(ledPin, LOW);    // set the LED off
            LEDON = true;
          }
          else{
            //led has to be turned ON
            digitalWrite(ledPin, HIGH);    // set the LED on
            LEDON = false;
          }
          // now output HTML data starting with standart header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          //set background to yellow
          client.print("<body style=background-color:white>");
          //send first heading
          client.println("<center>");
          client.println("<font color='blue'><h1>Casa Inteligente</font></h1>");
          //output some sample data to browser
          //client.println("<font color='blue' size='5'>Sample data: ");
          //client.print(sampledata);//lets output some data
          //drawing simple table
          //client.println("<font color='green'>Simple table: ");
          //printing some link
          client.println("<table border='1'><tr>");
          client.println("<td>");
          client.println("<center>");
          client.println("<h1><font color='blue'>Luz do Quintal</h1>");
          //controlling led via checkbox
          //address will look like http://192.168.1.243/?L=1 when submited
          client.println("<form method=get name=LED><input type=checkbox name=L value=1>LUZ<br><input type=submit value=Aplicar></form>");
          client.println("<br><font size='1'>(1=ON / 0=OFF)<br>");
          //printing LED status
          client.print("<font size='5'>Status: ");
          if (LEDON)
            client.println("<font color='green' size='5'>ON");
          else
            client.println("<font color='red' size='5'>OFF");
          client.println("</center>");
          client.println("</td>");
          client.println("</tr></table></center>");
          client.println("</body></html>");
          //clearing string for next read
          readString="";
          //stopping client
          client.stop();
        }
      }
    }
  }
}


And the code whith problem(usin two leds(led and fan in my code):

#include <String.h>
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = {
  192, 168, 1, 243 };                       // Endereco IP
byte gateway[] = {
  192, 168, 1, 10 };                    // IP roteador
byte subnet[] = {
  255, 255, 255, 0 };                   //Mascara
Server server(80);                                      //Porta
byte sampledata=50;            //some sample data - outputs 2 (ascii = 50 DEC)
int ledPin = 4;
int fanPin = 5;      // Pino do Led utilizado
//char link[]="http://192.168.1.243/"; //link data
String readString = String(30); //string for fetching data from address
boolean LEDON = false;//LED status flag
boolean FANON = false;

void setup(){
  //iniciar Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  //Set pin 4 to output
  pinMode(ledPin, OUTPUT);
  pinMode(fanPin, OUTPUT);
  //enable serial datada print
  Serial.begin(9600);
}
void loop(){
  // Cria uma conexao cliente
  Client client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        //read char by char HTTP request
        if (readString.length() < 30)
        {
          //store characters to string
          readString += c;
        }
        //output chars to serial port
        Serial.print(c);
        //if HTTP request has ended
        if (c == '\n') {
          //lets check if LED should be lighted
          if(readString.indexOf("L=1") >=0)
          {
            //led has to be turned OFF
            digitalWrite(ledPin, LOW);    // set the LED off
            LEDON = true;
          }
          else{
            //led has to be turned ON
            digitalWrite(ledPin, HIGH);    // set the LED on
            LEDON = false;
          }
         
          if (c == '\n') {
          //lets check if LED should be lighted
          if(readString.indexOf("F=1") >=0)
          {
            //led has to be turned OFF
            digitalWrite(fanPin, LOW);    // set the LED off
            FANON = true;
          }
          else{
            //led has to be turned ON
            digitalWrite(fanPin, HIGH);    // set the LED on
            FANON = false;
          }
          }
          // now output HTML data starting with standart header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          //set background to yellow
          client.print("<body style=background-color:white>");
          //send first heading
          client.println("<center>");
          client.println("<font color='blue'><h1>Casa Inteligente</font></h1>");
          //output some sample data to browser
          //client.println("<font color='blue' size='5'>Sample data: ");
          //client.print(sampledata);//lets output some data
          //drawing simple table
          //client.println("<font color='green'>Simple table: ");
          //printing some link
          client.println("<table border='1'><tr>");
          client.println("<td>");
          client.println("<center>");
          client.println("<h1><font color='blue'>Luz do Quintal</h1>");
          //controlling led via checkbox
          //address will look like http://192.168.1.243/?L=1 when submited
          client.println("<form method=get name=LED><input type=checkbox name=L value=1>LUZ<br><input type=submit value=Aplicar></form>");
          client.println("<br><font size='1'>(1=ON / 0=OFF)<br>");
          //printing LED status
          client.print("<font size='5'>Status: ");
          if (LEDON)
            client.println("<font color='green' size='5'>ON");
          else
            client.println("<font color='red' size='5'>OFF");
          client.println("</center>");
          client.println("</td>");
          client.println("<td>");
          client.println("<center>");
          client.println("<h1><font color='blue'>Ar do Quarto</h1>");
          client.println("<form method=get name=FAN><input type=checkbox name=F value=1>Ar-Condionado<br><input type=submit value=Aplicar></form>");
          client.println("<br><font size='1'>(1=ON / 0=OFF)<br>");
          client.print("<font size='5'>Status: ");
          if (FANON)
            client.println("<font color='green' size='5'>ON");
          else
            client.println("<font color='red' size='5'>OFF");
          client.println("</td>");
          client.println("</tr></table></center><br>");
          client.println("</body></html>");
          //clearing string for next read
          readString="";
          //stopping client
          client.stop();
        }
      }
    }
  }
}


Please, help me...  :)

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy