Arduino Yun Webserver

Arduino

Scetch: Arduino Yun Status

Webserver bearbeitet Anfragen des NetIO clients
und gibt den Status der Board LED zurück.
Steuerung der Board LED 'L13'

Siehe zugehöriges NetIO iOS App Projekt

Sketch -  Code des Webservers : 


/*
 Project: Arduino Yun Status
 Author: Michael Gries
 Creation: 2013-11-22
 Modified: 2013-12-01
 
 derived from:
 Arduino Yun Bridge example
 
 This example for the Arduino Yun shows how to use the 
 Bridge library to access the digital and analog pins 
 on the board through REST calls. It demonstrates how 
 you can create your own API when using REST style 
 calls through the browser.
 
 Possible commands created in this shetch:

 * "/arduino/digital/13"     -> digitalRead(13)
 * "/arduino/digital/13/1"   -> digitalWrite(13, HIGH)
 * "/arduino/analog/2/123"   -> analogWrite(2, 123)
 * "/arduino/analog/2"       -> analogRead(2)
 * "/arduino/mode/13/input"  -> pinMode(13, INPUT)
 * "/arduino/mode/13/output" -> pinMode(13, OUTPUT)
 
 for further information see: http://arduino.cc/en/Tutorial/Bridge

 */

#include 
#include 
#include 

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

void setup() {
  // 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();

  // Bridge interface (Console) copied from ConsoleRead example
  Console.begin(); 
  // Wait for Console port to connect
  //while (!Console); 
  Console.println(">>>: Console active ...");
}

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('/');
  sConsoleRead = command;

  // is "alive" command?
  if (command == "alive") {
    aliveCommand(client);
  }

  // is "toggle" command?
  if (command == "toggle") {
    toggleCommand(client);
  }

  // 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 aliveCommand(YunClient client) {
  // Send feedback to client
  sConsoleWrite = "lamp_on";
  client.print(sConsoleWrite);
  processConsoleTx(sConsoleWrite);  
}

void toggleCommand(YunClient client) {
  int pin, value, toggleValue;

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

  // get pin value
  value = digitalRead(pin);
  if (value == 0) {
    toggleValue = 1;
    digitalWrite(pin, toggleValue);
  } 
  else {
    toggleValue = 0;
    digitalWrite(pin, toggleValue);
  }

  // Send feedback to client
  client.print(F("Pin D"));
  client.print(pin);
  client.print(F(" set to analog "));
  client.println(toggleValue);
  sConsoleWrite = "Digital Port Pin ";
  sConsoleWrite += pin;
  sConsoleWrite += " set to ";
  sConsoleWrite += toggleValue;

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

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

  // Read pin number
  pin = client.parseInt();
  sConsoleRead += "/";
  sConsoleRead += pin;


  // 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);
    sConsoleRead += "/";
    sConsoleRead += value;
  } 
  else {
    value = digitalRead(pin);
  }

  // Send feedback to client
  if (value == 0) {
      sConsoleWrite = "lamp_off";
      //client.print(F("lamp_off"));
  }
  else {
      sConsoleWrite = "lamp_on";
      //client.print(F("lamp_on"));
  }

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

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 A"));
    client.print(pin);
    client.print(F(" set to analog "));
    client.println(value);

    // Update datastore key with the current pin value
    String key = "A";
    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);
}

void processConsoleRx(String ascii) {
  if (Console.available() > 0) {
      Console.print("<<<: ");
      Console.println(ascii);
  }
}

void processConsoleTx(String ascii) {
  if (Console.available() > 0) {
      processConsoleRx(sConsoleRead);
      Console.print(">>>: ");
      Console.println(sConsoleWrite);
  }
}