diff --git a/examples/Bridge/Bridge.ino b/examples/Bridge/Bridge.ino index 35c3aac..a65971d 100644 --- a/examples/Bridge/Bridge.ino +++ b/examples/Bridge/Bridge.ino @@ -1,17 +1,18 @@ /* Arduino Yún Bridge example - This example for the YunShield/Yún 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. + This example for the Arduino Yún 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/digital/13/1" -> pinMode(13, OUTPUT), digitalWrite(13, HIGH) + "/arduino/toggle/13" -> pinMode(13, OUTPUT), digitalWrite(13, !last_state) + "/arduino/analog/2/123" -> pinMode(2, OUTPUT), analogWrite(2, 123) "/arduino/analog/2" -> analogRead(2) "/arduino/mode/13/input" -> pinMode(13, INPUT) "/arduino/mode/13/output" -> pinMode(13, OUTPUT) @@ -25,18 +26,30 @@ #include #include #include +#include + +// Define constants EEPROM +#define PIN_MODE 0 +#define PIN_VALUE 1 +#define MEMORY_SLOT_SIZE 2 +#define TOTAL_PINS 20 // Listen to the default port 5555, the Yún webserver // will forward there all the HTTP requests you send BridgeServer server; +String response; void setup() { + // Recover pin states from memory + recoverPinStates(); + // 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(); @@ -67,6 +80,11 @@ void process(BridgeClient client) { if (command == "digital") { digitalCommand(client); } + + // is "toggle" command? + if (command == "toggle") { + toggleCommand(client); + } // is "analog" command? if (command == "analog") { @@ -89,17 +107,42 @@ void digitalCommand(BridgeClient client) { // with a value like: "/digital/13/1" if (client.read() == '/') { value = client.parseInt(); + pinMode(pin, OUTPUT); 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); + response = String(digitalRead(pin)); + client.print(String("{\"status\":"+response+"}")); + // Save Pin State + savePinState(pin,false,value); + + // Update datastore key with the current pin value + String key = "D"; + key += pin; + Bridge.put(key, String(value)); +} + +void toggleCommand(BridgeClient client) { + int pin, value; + + // Read pin number + pin = client.parseInt(); + + value = !digitalRead(pin); + pinMode(pin, OUTPUT); + digitalWrite(pin, value); + + // Send feedback to client + response = String(digitalRead(pin)); + client.print(String("{\"status\":"+response+"}")); + + // Save Pin State + savePinState(pin,false,value); + // Update datastore key with the current pin value String key = "D"; key += pin; @@ -116,14 +159,16 @@ void analogCommand(BridgeClient client) { // with a value like: "/analog/5/120" if (client.read() == '/') { // Read value and execute command + pinMode(pin, OUTPUT); 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); + response = String(value); + client.print(String("{\"analog\":"+response+"}")); + + // Save Pin State + savePinState(pin,true,value); // Update datastore key with the current pin value String key = "D"; @@ -134,10 +179,8 @@ void analogCommand(BridgeClient client) { value = analogRead(pin); // Send feedback to client - client.print(F("Pin A")); - client.print(pin); - client.print(F(" reads analog ")); - client.println(value); + response = String(value); + client.print(String("{\"analog\":"+response+"}")); // Update datastore key with the current pin value String key = "A"; @@ -154,7 +197,7 @@ void modeCommand(BridgeClient client) { // If the next character is not a '/' we have a malformed URL if (client.read() != '/') { - client.println(F("error")); + client.print(String("{\"error\":\"Malformed URL\"}")); return; } @@ -163,21 +206,52 @@ void modeCommand(BridgeClient client) { if (mode == "input") { pinMode(pin, INPUT); // Send feedback to client - client.print(F("Pin D")); - client.print(pin); - client.print(F(" configured as INPUT!")); + response = String(INPUT); + client.print(String("{\"mode\":"+response+"}")); + + // Clear pin state + clearPinState(pin); + 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!")); + response = String(OUTPUT); + client.print(String("{\"mode\":"+response+"}")); + return; } - client.print(F("error: invalid mode ")); - client.print(mode); + client.print(String("{\"error\":\"Invalid mode "+mode+"\"}")); } + +void recoverPinStates(){ + int i,offset; + for(i=0;i