Go Down

Topic: Return Global Variables over HTML (Read 719 times) previous topic - next topic

NiMCK

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: [Select]
<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

zoomkat

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.
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

NiMCK

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?

zoomkat

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.
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

NiMCK

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?

PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

zoomkat


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.
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

NiMCK

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: [Select]

#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="";
 
}}}}}

Go Up