Go Down

Topic: web server 1st time Yun (Read 1 time) previous topic - next topic

great2b1

HI,

I have a Yun,  and I just can't get my web browser to blink some lights.  Can someone give me a hint ?  I am pointing my browser at 192.168.2.14/arduino/xxx  I can upload over wifi and ping the Yun.

I just want to blink the light to get started,  but no luck.  thanks,  bill


#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

YunServer server;


void setup() {
  pinMode(13, OUTPUT);
  Bridge.begin();

  server.begin();

}

void loop() {
 
  YunClient client = server.accept();

 
  if (client) {
    String command = client.readString();


    digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);              // wait for a second
    digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);
    digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);              // wait for a second
    digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);
    digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);              // wait for a second
    digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);
 client.stop();

  }
 
 
 
   delay(50); // Poll every 50ms

}

AJITnayak

have you tested example code
Code: [Select]


// Possible commands are listed here:
//
// "digital/13"     -> digitalRead(13)
// "digital/13/1"   -> digitalWrite(13, HIGH)
// "analog/2/123"   -> analogWrite(2, 123)
// "analog/2"       -> analogRead(2)
// "mode/13/input"  -> pinMode(13, INPUT)
// "mode/13/output" -> pinMode(13, OUTPUT)

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

// Listen on default port 5555, the webserver on the Yun
// will forward there all the HTTP requests for us.
YunServer server;

void setup() {
  Serial.begin(9600);

  // Bridge startup
  pinMode(13,OUTPUT);
  digitalWrite(13, LOW);
  Bridge.begin();
  digitalWrite(13, HIGH);

  // Listen for incoming connection only from localhost
  // (no one from the external network could connect)
  server.listenOnLocalhost();
  server.begin();
}

void loop() {
  // Get clients coming from server
  YunClient client = server.accept();

  // There is a new client?
  if (client) {
    // Process request
    process(client);

    // Close connection and free resources.
    client.stop();
  }

  delay(50); // Poll every 50ms
}

void process(YunClient client) {
  // read the command
  String command = client.readStringUntil('/');

  // is "digital" command?
  if (command == "digital") {
    digitalCommand(client);
  }

  // is "analog" command?
  if (command == "analog") {
    analogCommand(client);
  }

  // is "mode" command?
  if (command == "mode") {
    modeCommand(client);
  }
}

void digitalCommand(YunClient client) {
  int pin, value;

  // Read pin number
  pin = client.parseInt();

  // If the next character is a '/' it means we have an URL
  // with a value like: "/digital/13/1"
  if (client.read() == '/') {
    value = client.parseInt();
    digitalWrite(pin, value);
  }
  else {
    value = digitalRead(pin);
  }

  // Send feedback to client
  client.print(F("Pin D"));
  client.print(pin);
  client.print(F(" set to "));
  client.println(value);

  // Update datastore key with the current pin value
  String key = "D";
  key += pin;
  Bridge.put(key, String(value));
}

void analogCommand(YunClient client) {
  int pin, value;

  // Read pin number
  pin = client.parseInt();

  // If the next character is a '/' it means we have an URL
  // with a value like: "/analog/5/120"
  if (client.read() == '/') {
    // Read value and execute command
    value = client.parseInt();
    analogWrite(pin, value);

    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" set to analog "));
    client.println(value);

    // Update datastore key with the current pin value
    String key = "D";
    key += pin;
    Bridge.put(key, String(value));
  }
  else {
    // Read analog pin
    value = analogRead(pin);

    // Send feedback to client
    client.print(F("Pin A"));
    client.print(pin);
    client.print(F(" reads analog "));
    client.println(value);

    // Update datastore key with the current pin value
    String key = "A";
    key += pin;
    Bridge.put(key, String(value));
  }
}

void modeCommand(YunClient client) {
  int pin;

  // Read pin number
  pin = client.parseInt();

  // If the next character is not a '/' we have a malformed URL
  if (client.read() != '/') {
    client.println(F("error"));
    return;
  }

  String mode = client.readStringUntil('\r');

  if (mode == "input") {
    pinMode(pin, INPUT);
    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" configured as INPUT!"));
    return;
  }

  if (mode == "output") {
    pinMode(pin, OUTPUT);
    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" configured as OUTPUT!"));
    return;
  }

  client.print(F("error: invalid mode "));
  client.print(mode);
}
AMPS

jumpjack

I can't find in the sample page a descritpion on how the web page must be set up to send proper commands to the sketch, any help?

I'm looking at this tutorial:
http://arduino.cc/en/Tutorial/Bridge

Additionally, I don't understand when the page talks about "REST" (I know POST and GET) and the required password... Isn't there an index of yun-related tutorials, or a single step-by-step Yun guide?

Till now what I understood and what I miss is:

- Create a suitable webpage (how??)
- Put the web page in \arduino\www folder in SD card
- Create a suitable Sketch to decode commands received from webpage (ok, see tutorial)
- Setup a [yunname].local/ address (how?? is it done by Arduino IDE installer or what? Can I setup such address by installing just the ZIP version of the IDE?)
- Connect to Yun using [yunname].local/sd/mypage.html
- Can I connect by WiFi to a Yun not connected to internet?

jumpjack

#3
Apr 13, 2015, 10:29 am Last Edit: Apr 13, 2015, 10:31 am by jumpjack
I created this page, would it work? (I did not yet purchase a Yun
But how do I pass a parameter from sketch to web page?

Code: [Select]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
  <script type="text/javascript">

var StatoLed = -1;

function Accendi() {
//window.open("http://myArduinoYun.local/arduino/digital/13/1","CommandWindow"); // Accendi LED
StatoLed = 1; // In realtà deve impostarla la funzione Leggi()
Leggi();
}

function Spegni() {
//window.open("http://myArduinoYun.local/arduino/digital/13/0","CommandWindow"); // Spegni LED
StatoLed = 0;   // In realtà deve impostarla la funzione Leggi()
Leggi();
}

function Leggi() {
window.open("http://myArduinoYun.local/arduino/digital/13","CommandWindow"); // Leggi LED
// E il valore letto dove viene messo??
// StatoLed = ??? ; // DEBUG
if (StatoLed == 0) {
         document.getElementById("Led13_on").checked = false;
         document.getElementById("Led13_off").checked = true;
alert("Spento?");
}
if (StatoLed == 1) {
         document.getElementById("Led13_on").checked = true;
         document.getElementById("Led13_off").checked = false;
alert("Acceso?");
}

}
  </script>
 

<input type="submit" value="ACCENDI" id="btn_on" onclick="Accendi();"><br>
<input type="submit" value="Spegni" id="btn_off" onclick="Spegni();"><br>
<input type="radio" name="Led13" value="on" id="Led13_on" checked disabled>Acceso<br>
<input type="radio" name="Led13" value="off" id="Led13_off" disabled>Spento<br>


  </body>
</html>

ShapeShifter

jumpjack: You are asking some very broad and general questions, which is not in the same scope as the original question. It's best to start a new topic for questions that are so significantly different.

Since any answers to your questions would probably be interesting to a wide audience, I've started a new thread to discuss your questions HERE. Please respond in that thread, do not continue to post to this one.

jumpjack

Thanks for th very broad reply, I'll reply in the other thread.

Go Up