Jump to content

Arduino LED-URL Confirmation

So first of all what I am asking is if I can have help with this. My Arduino code:

#include <SPI.h>
#include <Ethernet2.h>

int led = 13;
String POST = "";
String SET = "";
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0x8B, 0xBF };
IPAddress ip(192,168,1,179);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
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() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        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.println("");
          client.println("Jack's Arudino");
          client.println("");
          client.println("");
          client.println("");
          client.println("<h1>Control Jack's LED</h1>"); 
          client.print("<form method=get>");
          client.print("<input type='radio' value='0'> Off<br>");
          client.print("<input type='radio' value='1'> On<br>");
          client.print("<input type=submit value=submit></form>");
          client.print("</form>"); 
          client.println("");
          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;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
  }
  if(SET == "on"){
    digitalWrite(led, HIGH);
  }
  else{
    digitalWrite(led, LOW);
  }
}

So when I run this code, I can connect to the web page 192.168.1.179 and it all works, but when I click, lets say the off box the LED switches off for a second and then comes back on. Like what is this magic?

 

So I am asking for help to sort this out so the LED will actually turn off, and also my next step after this will be to make it so when I type say '192.168.1.179/?0' it turns the LED off and when I type '192.168.1.179/?1' it turns the LED on. This will then lead me to making my own Android app.

 

Thanks

Link to comment
Share on other sites

Link to post
Share on other sites

I don't understand how your code works at all. When the user submits the form, how are you reading the response? How do you know which radio button was selected? Where are you assigning the "SET" variable?

Link to comment
Share on other sites

Link to post
Share on other sites

20 hours ago, PlutoNZL said:

I don't understand how your code works at all. When the user submits the form, how are you reading the response? How do you know which radio button was selected? Where are you assigning the "SET" variable?

So I have updated my code (it should be much cleaner now):

 

#include <SPI.h>
#include <Ethernet2.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0x8B, 0xBF };
IPAddress ip(192, 168, 1, 179);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


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


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        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("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.print("HELLO");
          client.println("<a href=\"/?0\"\">Turn On LED</a>");
           client.println("<a href=\"/?1\"\">Turn Off LED</a><br />");   
          client.println("</html>");
          break;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    if (readString.indexOf("?0") >0){
               digitalWrite(led, HIGH);
    }
    if (readString.indexOf("?1") >0){
               digitalWrite(led, LOW);
    }
  }
}

But I have another error:

exit status 1
'readString' was not declared in this scope

Do you know what this error is? If so how do I fix it?

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, DerpDiamonds said:

So I have updated my code (it should be much cleaner now):


snip...

But I have another error:


snip...

Do you know what this error is? If so how do I fix it?

You never declare the 'readString' variable. It looks like you expect it to the URL of the request and you are trying to check the URL parameter. You need to assign 'readString' to the URL of the GET request.

 

Are you following a tutorial? If you give me the link I'll be able to help you a lot more.

 

EDIT: Take a look at the tutorial here: http://www.instructables.com/id/Arduino-Webserver-Control-Lights-Relays-Servos-etc/?ALLSTEPS 

their code looks very similar to yours.

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, PlutoNZL said:

You never declare the 'readString' variable. It looks like you expect it to the URL of the request and you are trying to check the URL parameter. You need to assign 'readString' to the URL of the GET request.

 

Are you following a tutorial? If you give me the link I'll be able to help you a lot more.

 

EDIT: Take a look at the tutorial here: http://www.instructables.com/id/Arduino-Webserver-Control-Lights-Relays-Servos-etc/?ALLSTEPS 

their code looks very similar to yours.

So how would you then declare the readStrimg variable?

Specs:

Motherboard: Gigabyte Z97X Gaming 3

CPU: Intel Core I7 4790K

GPU: Gigabyte G1 Gaming GTX 970

RAM: HyperX Fury 16GB

HDD: Seagate ST3000DM001 3TB

SSD: KINGSTON SV300S37A480G 450GB

Cooler: Corsair H100i GTX

Case: NZXT H440 Red

Monitor: DELL U2412M

Keyboard: Gigabyte Force K7

Mouse: Corsair Sabre RGB

 

Link to comment
Share on other sites

Link to post
Share on other sites

12 hours ago, DerpDiamonds1 said:

So how would you then declare the readStrimg variable?

Look at the code in the tutorial I linked.

readString is declared at the top of the file:

String readString;

The first 100 characters of the request string is then added to readString:

//read char by char HTTP request
if (readString.length() < 100) {
    //store characters to string
    readString += c;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 18/01/2017 at 8:25 PM, PlutoNZL said:

Look at the code in the tutorial I linked.

readString is declared at the top of the file:


String readString;

The first 100 characters of the request string is then added to readString:


//read char by char HTTP request
if (readString.length() < 100) {
    //store characters to string
    readString += c;
}

 

Sorry I haven't been responding, I have been doing other things. But now I have added that code. When I go to access the website it just downloads a file called download. Here is a picture:

 

f.png

 

 

 

Edited by DerpDiamonds
Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, DerpDiamonds said:

Sorry I haven't been responding, I have been doing other things. But now I have added that code. When I go to access the website it just downloads a file called download. Here is a picture: ...

I don't know what would cause that. Can you upload your entire code?

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, PlutoNZL said:

I don't know what would cause that. Can you upload your entire code?

Here:

 

#include <SPI.h>
#include <Ethernet2.h>

int led = "13";

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0x8B, 0xBF };
IPAddress ip(192, 168, 1, 179);
String readString;

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // 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);              //initalize the led as an output
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);

        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
         }           

        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("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.print("HELLO");
          client.println("<a href=\"/?0\"\">Turn On LED</a>");
           client.println("<a href=\"/?1\"\">Turn Off LED</a><br />");   
          client.println("</html>");
          break;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    if (readString.indexOf("?0") >0){
               digitalWrite(led, HIGH);
    }
    if (readString.indexOf("?1") >0){
               digitalWrite(led, LOW);
    }
  }
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

12 hours ago, DerpDiamonds said:

Here:

 


snip...

 

Unfortunately I don't have an Arduino so I can't run your code. I think the issue may be that your response is malformed. Update your response to be just this:

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();
          client.println("<html>");
          client.println("<body>");
          client.println("<a href=\"/?0\"\">Turn On LED</a>");
          client.println("<a href=\"/?1\"\">Turn Off LED</a><br />");
          client.println("</body>");
          client.println("</html>");
          break;
}

If that works, that shows that the issue you're getting is because of your response format.

Link to comment
Share on other sites

Link to post
Share on other sites

On 22/01/2017 at 1:20 AM, PlutoNZL said:

I don't know what would cause that. Can you upload your entire code?

Here:

 

#include <SPI.h>
#include <Ethernet2.h>

int led = "13";

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0x8B, 0xBF };
IPAddress ip(192, 168, 1, 179);
String readString;

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // 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);              //initalize the led as an output
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);

        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
         }           

        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("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.print("HELLO");
          client.println("<a href=\"/?0\"\">Turn On LED</a>");
           client.println("<a href=\"/?1\"\">Turn Off LED</a><br />");   
          client.println("</html>");
          break;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    if (readString.indexOf("?0") >0){
               digitalWrite(led, HIGH);
    }
    if (readString.indexOf("?1") >0){
               digitalWrite(led, LOW);
    }
  }
}

It now seem sot not be responding

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×