Pages: [1]   Go Down
Author Topic: Arduino Yun Webpage Server Issues  (Read 212 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, I've made a sketch on my Yun, which I would like to be able to take information from a webpage (like a button) and have the Arduino Microcontroller perform functions based on what button is being pressed. I have an html file with the webpage of buttons. I also have a sketch that responds to those button presses. When I upload the project via WiFi, everything copies over properly. The html file gets copied to the SD card, the sketch gets loaded onto the Arduino Microntroller, and I can browse to the webpage with my browser. However, whenever I click on the button within the webpage, nothing gets sent back to the Arduino Microcontroller. If actually program the webpage into the actual sketch, everything works fine, but not if the webpage is being loaded onto the SD card. After that, no data gets passed between the Linio and the Atmel Microcontroller.

I would like to be able to have the webpage be hosted by the Linio processor, but pass information about button presses to the actual Atmel Microcontroller.

Does anybody know how to do this properly? I would like to stay away from JScript and AJAX as much as possible. However, I guess I'll use them only if it's absolutely necessary.

Here is my arduino sketch:

Code:
#include <Bridge>
#include <Console.h>
#include <YunServer.h>
#include <YunClient.h>

YunServer server;
boolean receiving = false;

void setup() {
  Bridge.begin();
  Console.begin();
  server.listenOnLocalhost();
  server.begin();
  Console.println("Ready");
}

void loop() {  
  YunClient client = server.accept();
  
  if (client) {
    Console.println("Client Connected");
    String request = client.readString();
    Console.println(request);
    
    boolean currentLineIsBlank = true;
    boolean sentHeader = false;
  
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Console.println(c);
        if (receiving && c == ' ') receiving = false;
        
        if (c == '?') receiving = true;
        
        if (receiving) {
          if (c == 'A') {
            Console.println("Three Beeps");
            threeBeeps();
            break;
          }
          if (c == 'B') {
            Console.println("Fast Beeps");
            fastBeeps();
            break;
          }
          if (c == 'C') {
            Console.println("Fast Scale");
            fastScale();
            break;
          }
          if (c == 'D') {
            Console.println("Alarm");
            playAlarm();
            break;
          }
          if (c == 'E') {
            Console.println("Evil Alarm");
            evilChord();
            break;
          }
        }
        if (!sentHeader) {
//          client.println("HTTP/1.1 200 OK");
//          client.println("Content-Type: text/html\n");
//          
//          client.println("<form action='' method='get'>");
//          client.println("<input type='hidden' name='A' value='1' />");
//          client.println("<input type='submit' value='Three Beeps' />");
//          client.println("</form>");
//          
//          client.println("<form action='' method='get'>");
//          client.println("<input type='hidden' name='B' value='2' />");
//          client.println("<input type='submit' value='Fast Beeps' />");
//          client.println("</form>");
//          
//          client.println("<form action='' method='get'>");
//          client.println("<input type='hidden' name='C' value='3' />");
//          client.println("<input type='submit' value='Fast Scale' />");
//          client.println("</form>");
//          
//          client.println("<form action='' method='get'>");
//          client.println("<input type='hidden' name='D' value='4' />");
//          client.println("<input type='submit' value='Alarm' />");
//          client.println("</form>");
//  
//          client.println("<form action='' method='get'>");
//          client.println("<input type='hidden' name='E' value='5' />");
//          client.println("<input type='submit' value='Evil Siren' />");
//          client.println("</form>");
          
          sentHeader = true;
        }
        if (c == '\n' && currentLineIsBlank) break;
        
        if (c == '\n') {
          currentLineIsBlank = true;
        }
        else if(c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    delay(5);
    client.stop();
  }
}

void threeBeeps() {
  for (int i = 0; i < 3; i++) {
    tone(11,2200);
    delay(100);
    noTone(11);
    delay(100);
  }
  noTone(11);
}

void fastScale() {
  for (int i = 0; i < 1000; i++) {
    tone(11,(i * 2));
    delay(1);
  }
  for (int i = 1000; i > 0; i--) {
    tone(11,(i * 2));
    delay(1);
  }
  noTone(11);
}

void playAlarm() {
  for (int i = 0; i < 5; i++) {
    for (int j = 1000; j < 4000; j++) {
      tone(11,j);  
    }
    for (int j = 4000; j > 1000; j--) {
      tone(11,j);
    }
  }
  noTone(11);
}

void fastBeeps() {
  for (int i = 0; i < 6; i++) {
    tone(11,2000);
    delay(50);
    noTone(11);
    delay(50);
  }
  noTone(11);
}

void evilChord() {
  for (int i = 0; i < 5; i++) {
    tone(11,1110);
    delay(500);
    tone(11,785);  
    delay(500);
  }
  noTone(11);
}
  

Here's my index.html file that gets loaded onto the SD card:

Code:
<!DOCTYPE html>
<head>
</head>
<body>
<form action='' method='get'>
<input type='hidden' name='A' value='1'>
<input type='submit' value='Three Beeps' />
</form>

<form action='' method='get'>
<input type='hidden' name='B' value='2'>
<input type='submit' value='Fast Beeps' />
</form>

<form action='' method='get'>
<input type='hidden' name='C' value='3'>
<input type='submit' value='Fast Scale' />
</form>

<form action='' method='get'>
<input type='hidden' name='D' value='4'>
<input type='submit' value='Alarm' />
</form>

<form action='' method='get'>
<input type='hidden' name='E' value='5'>
<input type='submit' value='Evil Siren' />
</form>
</body>

As you can probably tell, the 5 buttons the webpage generates tells the Arduino to play a sound corresponding to the pressed button.

Like I said, I can program the webpage into the actual sketch (all commented out for the time being), and everything works just dandy. However, if I load the webpage onto the SD card in the Yun, I can still browse to it by browsing to "Arduino.local/sd/<project name>", but the buttons no longer function since the webpage was generated from the Linio and not the Arduino. The reason I want the Linio to handle it is because it's much faster at handling large webpages and is much quicker accessing the SD card than the actual Arduino. So, does anybody know how to take the button presses from the Linio and send them back to the Arduino?

Any help would be greatly appreciated!
« Last Edit: August 26, 2014, 08:54:00 pm by Uniplast21 » Logged

Pages: [1]   Go Up
Jump to: