Pages: [1]   Go Down
Author Topic: Return Global Variables over HTML  (Read 472 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I have currently set up Arduino Mega with an ethernet shield, with control of an LED over the web.

The website is hosted externally (not on the arduino), I have called a hyperlink to the arduino for the on and off switch.

Code:
<a href="http://192.168.1.102:84/?-L1=1" target="inlineframe">On</a>

The arduino then reads the "L1=1" and knows to switch on LED 1, and vice versa for the off switch.

So, is there a way to return a value back that can be displayed on the website so that I can record its current state.

I want to be able to show like a green image if it is currently on, and a red image if its currently off.(just red and green .png)

I was thinking it would be along the lines of the arduino returning a html page that the website can parse for a current value. Im not so strong with HTML so if anyone can point me in the right direction that would be greatly appreciated.

Thanks
Logged

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8894
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why are you using an iframe if you want something returned to the client? The usual thing to do is return a page with the desired content.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

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

The iFrame inst necessary, it was from a previous version I had working.

I want to be able to return a value instead of a page, so that the original page can just be updated with the current state of the LED.

Is there a way of doing this?
Logged

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8894
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In the part of the code that is sent back to the client use if statements to send back the desired pix based on the last state of the LED.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

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

I will use if statements for checking what state the LED is in to know what to send back.

But I'm not so good with html, would you be able to give me a simple example of the code to send back?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11138
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I want to be able to return a value instead of a page, so that the original page can just be updated with the current state of the LED.

Is there a way of doing this?

You need to return a document, but it doesn't need to be an HTML page. Google AJAX for millions of examples of this in action.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8894
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I will use if statements for checking what state the LED is in to know what to send back.
But I'm not so good with html, would you be able to give me a simple example of the code to send back?

Perhaps it is time for you to post your code. No future in guessing what you may need.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

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

So here is the code, if there is anyone else that is trying something similar feel free to give it a go.

There are comments towards the end that explain what I'm trying to achieve.

Ill take a look at AJAX.

Thanks for all the help, much appreciated.

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //mac
byte ip[] = { 192, 168, 1, 108 }; // lan
byte gateway[] = { 192, 168, 1, 1 }; // router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString;
 
Servo myservo1; 
int pos = 0;

int ledPin = 13;

void setup(){

//start Ethernet

Ethernet.begin(mac, ip, gateway, subnet);
server.begin();

//enable serial data print
Serial.begin(9600);

myservo1.attach(9);

pinMode(ledPin, OUTPUT);

Serial.println("setup complete"); //testing purposes
}

void loop(){
// Client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();

//read char by char HTTP request
if (readString.length() < 100) {

//store characters to string
readString += c;
}

//if HTTP request has ended
if (c == '\n') {

Serial.println(readString);

      if (readString.length() >0) {
      Serial.println(readString); //testing purposes
           
      String control = readString.substring(7,11); //This will read the part of the String that contains the command (e.g. L1=1)
     
      Serial.println(control); //testing purposes
           
      if(control == "L1=1")
      {
        digitalWrite(ledPin, HIGH);
      }
      else if(control == "L1=0")
      {
        digitalWrite(ledPin, LOW);
      }
      else if(control == "S1=1")
      {
        for(pos = 0; pos < 180; pos += 1)
        {
          myservo1.write(pos);
          delay(15);
        } 
      }
      else if(control == "S1=0")
      {
        for(pos = 180; pos > 1; pos -= 1)
        {
          myservo1.write(pos);
          delay(15);
        }
      }
     
      //Ignore the servo stuff for now.
      //After this I want to be able to return something after one of the actions has been completed.
      //Could use a global variable for each output if that would make it any easier.
      //Otherwise I just want to return a value to confirm what state an output(LED) is currently in.
      //So if Someone else was to access the web page it would give them all the current values of the outputs(LEDS).
     
      readString=""; //resets readString
      }
 
      //HTML header
      client.println("HTTP/1.1 200 NM");
      client.println();
      delay(1);
      //stop client
      client.stop();

      readString="";
 
}}}}}
Logged

Pages: [1]   Go Up
Jump to: