Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Led-switching via ethernet on: August 14, 2012, 01:14:39 pm
Does anyone have an idea why the buttons have to be clicked two times?
2  Using Arduino / Programming Questions / Re: Led-switching via ethernet on: August 12, 2012, 02:06:11 pm
Quote
If you put each { on a new line, and use Tools + Auto format, I think that you will see the problem with your code. If not, post it again after making the changes.

I took these remarks into account, the code is posted below:

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,105);
boolean incoming = 0;
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
int led=2;
String ipstring="http://192.169.0.105";
int power=LOW;
int lastState=HIGH;
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  pinMode(led, OUTPUT);
}


void loop()
{
  digitalWrite(led, power);
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client)
  {
    Serial.println("client connected");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply

        //reads URL string from $ to first blank space
        if(incoming && c == ' ')
        {
          incoming = 0;
        }
        if(c == '$')
        {
          incoming = 1;
        }
        //Checks for the URL string $1 or $2
        if(incoming == 1)
        {
          Serial.println(c);

          if(c == '1')
          {
            power=HIGH;
            digitalWrite(led, power);
          }
          else if(c == '2')
          {
            power=LOW;
            digitalWrite(led, power);
          }
        }
        //Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank)
        {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.print("<!DOCTYPE html>");
          client.print("<html>");
          client.print("<head>");
          client.print("<script type=\"text/javascript\">");
          client.print("function btn_power_on()");
          client.print("{");
          //client.print("alert('ON!');");
          client.print("parent.location='$1'");
          client.print("}");
          client.print("function btn_power_off()");
          client.print("{");
          //client.print("alert('OFF!');");
          client.print("parent.location='$2'");
          client.print("}");
          client.print("</script>");
          client.print("</head>");
          client.print("<body>");
          client.print("<!--<FORM METHOD=\"LINK\" ACTION=\"$1\">");
          client.print("<INPUT TYPE=\"submit\" VALUE=\"ON\">");
          client.print("</FORM>");
          client.print("<FORM METHOD=\"LINK\" ACTION=\"$2\">");
          client.print("<INPUT TYPE=\"submit\" VALUE=\"OFF\">");
          client.print("</FORM>");
          client.print("<FORM METHOD=\"LINK\" ACTION=\"location.href='http://www.en.wikipedia.org'\">");
          client.print("<INPUT TYPE=\"submit\" VALUE=\"wiki\">-->");
          client.print("<input type=button style=\"width:350px;height:200px;font-size:30px\" onClick=\"btn_power_on()\" value='power on'>");
          client.print("<br>");
          client.print("<br>");
          client.print("<br>");
          client.print("<br>");
          client.print("<br>");
          client.print("<input type=button style=\"width:350px;height:200px;font-size:30px\"  onClick=\"btn_power_off()\" value='power off'>");
          client.print("</FORM>");
          client.print("</body>");
          client.print("</html>");
          break;
        }
        if (c == '\n')
        {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r')
        {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
      if(power !=lastState)
      {
        lastState=power;
        Serial.println(power);
      }
    }

    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

In line 86 I changed if(c == '2') to else if(c == '2')

I discovered that the buttons' actions are just turned around: When I press the "OFF" button, the led turns on and vice  versa. Just changing the buttons' labels doesn't appeal to me, I'd rather correct the code's logic.

Quote
Look at the circumstances under which you change the pin state.
The state is set depending on the character (1 or 2) in the url, but this is apparently not done correctly by the buttons.
What exactly are you pointing at?

Thanks and greetings
3  Using Arduino / Programming Questions / Re: Led-switching via ethernet on: August 12, 2012, 12:21:06 pm
Quote
Perhaps your browser is set for a double click requirement to launch a link.
I tried it with the browser on my mobile phone, chrome/chromium and firefox.
Actually, there is a reaction to a button click, but it only lasts for a fraction of a second. The second click then changes the state so that it lasts.

It is notable that the url changes correctly, only the led's state is not set properly!
That may indicate that there's an error in the code
4  Using Arduino / Programming Questions / Re: Led-switching via ethernet on: August 11, 2012, 05:08:38 am
Hey,

thanks for your reply. The code looks quite similar to mine apart from having different kinds of buttons and reading a string instead of single characters from the url.

The effect remains the same: When I toggle the buttons, the string in the url changes as intended.
However, the led behaves the same as it did before, it still does not change the state according to the posted string ON or OFF.

I'm a bit clueless about why the action does not "reach" the led...
5  Using Arduino / Programming Questions / Led-switching via ethernet on: August 10, 2012, 07:28:12 am
Hi,

I wrote some code to let the arduino+ethernet shield send a plain website with two buttons to switch on or off an led which is placed behind a resistor on port 2. Generally, the code is based on the WebServer example. The command is posted as an extension of the url: $1 switches on, $2 switches off.
Modifying the url works like a charm to switch the led, but apparently the buttons work a kind of whacky:
To achieve the desired effect, I always have to click a button twice. When it's pressed only once, you can see it go to the associated state for blink of an eye and then immediately go back to what it was.
So, what's the error in my code?
Here it is:
Code:
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,105);
boolean incoming = 0;
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
int led=2;
String ipstring="http://192.169.0.105";
int power;
void setup() {
 // Open serial communications and wait for port to open:
 Serial.begin(9600);

 // start the Ethernet connection and the server:
 Ethernet.begin(mac, ip);
 server.begin();
 Serial.print("server is at ");
 Serial.println(Ethernet.localIP());
 pinMode(led, OUTPUT);
 pinMode(7, OUTPUT);
}


void loop() {
 // listen for incoming clients
 EthernetClient client = server.available();
 if (client) {
   Serial.println("client connected");
   // an http request ends with a blank line
   boolean currentLineIsBlank = true;
   while (client.connected()) {
     if (client.available()) {
       char c = client.read();
       // if you've gotten to the end of the line (received a newline
       // character) and the line is blank, the http request has ended,
       // so you can send a reply

       //reads URL string from $ to first blank space
       if(incoming && c == ' '){
         incoming = 0;
       }
       if(c == '$'){
         incoming = 1;
       }
       //Checks for the URL string $1 or $2
       if(incoming == 1){
         Serial.println(c);

         if(c == '1'){
           power=HIGH;
           digitalWrite(led, power);
    //      for debugging purposes
           //            Serial.println("ON");
           //            digitalWrite(led, HIGH);
           //            delay(500);
         }
         if(c == '2'){
           power=LOW;
           digitalWrite(led, power);
    //      for debugging purposes
           //            Serial.println("OFF");
           //            digitalWrite(led, LOW);
           //            delay(500);
         }
       }
       digitalWrite(led, power);
       //Serial.write(c);
       // if you've gotten to the end of the line (received a newline
       // character) and the line is blank, the http request has ended,
       // so you can send a reply
       if (c == '\n' && currentLineIsBlank) {
         // send a standard http response header
         client.println("HTTP/1.1 200 OK");
         client.println("Content-Type: text/html");
         client.println("Connnection: close");
         client.println();
         client.print("<!DOCTYPE html>");
         client.print("<html>");
         client.print("<head>");
         client.print("<script type=\"text/javascript\">");
         client.print("function btn_power_on()");
         client.print("{");
         //client.print("alert('ON!');");
         client.print("parent.location='$1'");
         client.print("}");
         client.print("function btn_power_off()");
         client.print("{");
         //client.print("alert('OFF!');");
         client.print("parent.location='$2'");
         client.print("}");
         client.print("</script>");
         client.print("</head>");
         client.print("<body>");
         client.print("<!--<FORM METHOD=\"LINK\" ACTION=\"$1\">");
         client.print("<INPUT TYPE=\"submit\" VALUE=\"ON\">");
         client.print("</FORM>");
         client.print("<FORM METHOD=\"LINK\" ACTION=\"$2\">");
         client.print("<INPUT TYPE=\"submit\" VALUE=\"OFF\">");
         client.print("</FORM>");
         client.print("<FORM METHOD=\"LINK\" ACTION=\"location.href='http://www.en.wikipedia.org'\">");
         client.print("<INPUT TYPE=\"submit\" VALUE=\"wiki\">-->");
         client.print("<input type=button style=\"width:350px;height:200px\" onClick=\"btn_power_on()\" value='power on'>");
         client.print("<br>");
         client.print("<br>");
         client.print("<br>");
         client.print("<br>");
         client.print("<br>");
         client.print("<input type=button style=\"width:350px;height:200px\" onClick=\"btn_power_off()\" value='power off'>");
         client.print("</FORM>");
         client.print("</body>");
         client.print("</html>");
         break;
       }
       if (c == '\n') {
         // you're starting a new line
         currentLineIsBlank = true;
       }
       else if (c != '\r') {
         // you've gotten a character on the current line
         currentLineIsBlank = false;
       }
     }
   }
   digitalWrite(led, power);
   // give the web browser time to receive the data
   delay(1);
   // close the connection:
   client.stop();
   Serial.println("client disonnected");
 }
}


Thanks in advance and greetings


6  Using Arduino / Programming Questions / Re: Missing line in eeprom_clear? on: August 09, 2012, 09:59:54 am
See here: http://arduino.cc/forum/index.php/topic,117872.0.html
7  Using Arduino / Programming Questions / Re: Missing line in eeprom_clear? on: August 09, 2012, 08:04:43 am
done.
8  Community / Website and Forum / Bug in eeprom_clear on: August 09, 2012, 05:56:03 am
Hi,

as discussed earlier today (http://arduino.cc/forum/index.php/topic,117867.0.html) there seems to be a bug in the eeprom_clear example file as it is posted on the website (http://arduino.cc/en/Tutorial/EEPROMClear) and shipped with the IDE.
Should be fixed by inserting
Code:
pinMode(13, OUTPUT);

By the way, is there any bugtracking system to report cases like this one?

Best regards
9  Using Arduino / Programming Questions / Missing line in eeprom_clear? on: August 09, 2012, 04:54:13 am
Hi,

I am fairly new to arduino programming .
After my first small projects I checked some of the example files.
The sample code "eeprom_clear" (see http://arduino.cc/en/Tutorial/EEPROMClear or Examples->EEPROM->eeprom_clear in the IDE) does not work as intended since the (led) port 13 is not declared to be in output mode.
So, the missing line is
pinMode(13, OUTPUT);

Is this a mistake on my part or a case to be reported to the dev team?
Best regards
Pages: [1]