Pages: [1]   Go Down
Author Topic: Help with Arduino WebServer  (Read 771 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to create a webserver with my arduino uno to allow remote control of devices, right now I am working with a 4 led prototype. The issue am having is that whenever I turn first led on, the others don't respond. The second one when turned on just flashes and does not actually stay on. Any help will be appreciated. I have posted my code below:
/////////////////////////////////////////////////
#include <SPI.h>
#include <Client.h>
#include <Ethernet.h>
#include <Server.h>
#include <Udp.h>
//Ethernet Shield MAC address
byte mac[]={0x90,0xA2,0xDA,0x0D,0x9D,0xBB};

//Board IP Address
byte ip[]={192,168,1,5};
byte gateway[]={192,168,1,1};
byte subnet[]={255,255,255,0};
//Initialize ethernet server library
//Port 80 is default port for HTTP
EthernetServer server(80);
int Room1=2;
int Room2=3;
int Room3=5;
int Room4=6;
//int TempPin
String readString = String(30);
//led status flags
boolean LED1_ON=false;
boolean LED2_ON=false;
boolean LED3_ON=false;
boolean LED4_ON=false;
void setup(){
 
  //start ethernet connection and server
    Ethernet.begin(mac,ip,gateway,subnet);
    server.begin();
    pinMode(Room1,OUTPUT);
    pinMode(Room2,OUTPUT);
    pinMode(Room3,OUTPUT);
    pinMode(Room4,OUTPUT);
    //Start Serial communication and initiailize baud rate
  Serial.begin(9600);
  }
void loop(){
  //digitalWrite(Room4,HIGH);
  //listen for incoming clients
  EthernetClient client = server.available();
  if(client){
    while(client.connected()){
      if(client.available()){
      char c = client.read();           
      //read each char of HTTP request
      if(readString.length()<30){
     //store characters to string
     readString +=c;//replaces readString.append(c);
      }
      //print char to serial port
      Serial.print(c);
      //if HTTP request has ended
      if(c=='\n'){
        Serial.println("");
        Serial.println(readString);
        Serial.println("");
        int L1=readString.indexOf("L1=");
        int L2=readString.indexOf("L2=");
        int L3=readString.indexOf("L3=");
        int L4=readString.indexOf("L4=");
        Serial.print("L1=position:");
        Serial.println(L1);
        Serial.print("L2=position:");
        Serial.println(L2);
        Serial.print("L3=position:");
        Serial.println(L3);
        Serial.print("L4=position:");
        Serial.println(L4);
       
        if(readString.indexOf("?")<0)
        {
        //skip everything
        }
        else
        //lets check if LED is lit
       
        if(readString.indexOf("L1=1")>0) 
        //if(L1 > 1){
          //if(readString.substring(L1,(L1+3))=="L1=1")
        {
          digitalWrite(Room1,HIGH);
          LED1_ON=true;
        }
        else{ //if(readString.substring(L1,(L1+3))=="L1=0"){
       digitalWrite(Room1,LOW);
       LED1_ON=false;
         
        }
         if(readString.indexOf("L2=1")>0)
       // if(L2 > 0){
          //if(readString.substring(L2,(L2+3))=="L2=1")
        {
          digitalWrite(Room2,HIGH);
          LED2_ON=true;
        }
         else{ //if(readString.substring(L2,(L2+3))=="L2=0"){
       digitalWrite(Room2,LOW);
       LED2_ON=false;
         
        }
        //if(L3 > 0){
          //if(readString.substring(L3,(L3+3))=="L3=1")
       if(readString.indexOf("L3=1")>0)
        {
          digitalWrite(Room3,HIGH);
          LED3_ON=true;
        }
         else {//if(readString.substring(L3,(L3+3))=="L3=0"){
       digitalWrite(Room3,LOW);
       LED3_ON=false;
         
        }
        if(readString.indexOf("L4=1)")>0)
       // if(L4 > 0){
         // if(readString.substring(L4,(L4+3))=="L4=1")
        {
          digitalWrite(Room4,HIGH);
          LED4_ON=true;
        }
        else {//if(readString.substring(L4,(L4+3))=="L4=0"){
       digitalWrite(Room4,LOW);
       LED4_ON=false;
         
        }
     
     
      //now output HTML data starting with standard header
      //standard http request header
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      //client.println("Connection: close");
      client.println();
      // add a meta refresh tag, so the browser pulls again every 5 seconds:
      //client.println("<meta http-equiv=\"refresh\" content=\"5\">");
      //set background to white
      client.print("<body style=background-color:white>");
      //Send first heading
      client.println("<font color='blue'<h1><b>Home Control System.</b></h1>");
      client.println("<hr />");
      client.println("<hr />");
     
      //controlling LED's
      client.println("<h1>LED CONTROL</h1>");
      client.println("<form method=get name=L1>");
      client.println("<input type=radio name=L1 value=1>LED1 ON<br><input type=radio name=L1 value=0>LED1 OFF<br>");
      client.println("<input type=radio name=L2 value=1>LED2 ON<br><input type=radio name=L2 value=0>LED2 OFF<br>");
      client.println("<input type=radio name=L3 value=1>LED3 ON<br><input type=radio name=L3 value=0>LED3 OFF<br>");
      client.println("<input type=radio name=L4 value=1>LED4 ON<br><input type=radio name=L4 value=0>LED4 OFF<br>");
      client.println("<input type=submit value=submit></form>");
     
      client.println("<br />");
      //client.print("<font size='5[ch8242]>LED status: ")
     
      //printing LED status
      client.print("<font size='5[ch8242]>LED1 status: ");
      if (LED1_ON)
         client.println("<font color='green' size='5[ch8242]>ON");
      else
         client.println("<font color='grey' size='5[ch8242]>OFF");

          client.println("<br />");
          client.println("<hr />");
          //client.println("<hr />");
          //client.println("</body></html>");
          //clearing string for next read
       
     
     
      //printing LED status
      client.print("<font size='5[ch8242]>LED2 status: ");
      if (LED2_ON)
        client.println("<font color='green' size='5[ch8242]>ON");
      else
        client.println("<font color='grey' size='5[ch8242]>OFF");
        client.println("<br />");


          client.println("<hr />");
          //client.println("<hr />");
          //client.println("</body></html>");
          //clearing string for next read
     
         
     
      //printing LED status
      client.print("<font size='5[ch8242]>LED3 status: ");
      if (LED3_ON)
      client.println("<font color='green' size='5[ch8242]>ON");
      else
      client.println("<font color='grey' size='5[ch8242]>OFF");
      client.println("<br />");
      client.println("<hr />");
      //client.println("<hr />");
      //client.println("</body></html>");
      //clearing string for next read
   
         
     
      //printing LED status
      client.print("<font size='5[ch8242>LED4 status: ");
      if (LED4_ON)
      client.println("<font color='green' size='5[ch8242]>ON");
      else
      client.println("<font color='grey' size='5[ch8242]>OFF");
      client.println("<br />");
      client.println("<hr />");
      //client.println("<hr />");
      client.println("</body></html>");
      //clearing string for next read
      readString="";
         
      //stopping client
      client.stop();
       }
      } 
    }
  }
 }

Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8856
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have this code for LED 1:
Code:
  if(readString.indexOf("?")<0) {
    //skip everything
  }
  else
    if(readString.indexOf("L1=1")>0) {
      digitalWrite(Room1,HIGH);
      LED1_ON=true;
    }
    else {
      digitalWrite(Room1,LOW);
      LED1_ON=false;
    }

And this code for LED 2:
Code:
  if(readString.indexOf("L2=1")>0)
  {
    digitalWrite(Room2,HIGH);
    LED2_ON=true;
  }
  else{
    digitalWrite(Room2,LOW);
    LED2_ON=false;         
  }

It looks like you might have intended the LED 2 code to ALSO be part of the ELSE clause in that initial IF statement.  If you want more than one statement in an ELSE clause you need to put brackets around the block of statements to make them act as one statement.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank You. That was my issue. I really appreciate the help!!!
Logged

Pages: [1]   Go Up
Jump to: