Go Down

Topic: Problem with LEDON (Read 862 times) previous topic - next topic

Fernando_Estevam

Good afternoon guys!

Sorry.. I don't know to speak and write English very well..

I have Arduino duemilanove atmega 328 + ethernet shield.
I'm doing a code that turn ON and turn off a LED.

My problem is: I turn ON the ledPin 4 and I close my browser. When I open the browser with the ethernet IP, automatically the ledpin turn OFF.

I want that the ledPin 4 keeps the previous status.

#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 };                       // IP
byte gateway[] = {
  192, 168, 1, 10 };                    // roteador
byte subnet[] = {
  255, 255, 255, 0 };                   //Subnet
Server server(80);                                      //Porta
byte sampledata=50;//some sample data - outputs 2 (ascii = 50 DEC)

int ledPin = 4;//Pino do Led
int fanPin = 5;// Pino do Fan
int analogPin = 0; //Pino Termistor
int tempc= 0, tempf=0,tempk=0;
int samples[8];
int i;
String readString = String(30);
boolean LEDON = false;
boolean FANON = false;//LED status flag

void setup(){
  Ethernet.begin(mac, ip, gateway, subnet);
  pinMode(ledPin, OUTPUT);
  pinMode(fanPin, OUTPUT);
  Serial.begin(9600); //enable serial datada print
}
void loop(){

  Client client = server.available();// Cria uma conexao cliente
  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;
        }

        for(i = 0; i<=7 ;i++){
          samples = ( 5.0 * analogRead(analogPin) * 100.0) / 1024.0;
          tempc = tempc + samples; // somando os 8 valores lidos
        }

        tempc = tempc/8.0; // tirando a media dos 8 valores
        tempf = (tempc * 9)/ 5 + 32; // converte para fahrenheit
        tempk= tempc+273; // converte para Kelvin
        delay(100);
       
       if (tempc >= 33){
         digitalWrite(fanPin, LOW);
         FANON = true;
       }
         else{
           digitalWrite(fanPin, HIGH);
           FANON = false;
         }

        //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.print("<meta http-equiv='refresh' content='10' ; url='192.168.1.243'>");
          client.println("<center>");
          client.println("<font color='blue'><h1>Casa Inteligente</font></h1>");
       
          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
          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><br><br>");
         
          client.println("Temperatura C:"); //Mostra as temperaturas em C F e K
          client.println(tempc);
          client.println("\t");
          client.println("Temperatura F:");
          client.println(tempf);
          client.println("\t");
          client.println("Temperatura K:");
          client.println(tempk);
          client.println("\n");
          client.println("</center>");
         
          client.println("</body></html>");
          readString=""; //clearing string for next read
          client.stop(); //stopping client
        }
      }
    }
  }
}

Thanks. :)

Go Up