From 96c2a02a2f489896f76186886c753f0d85c00714 Mon Sep 17 00:00:00 2001 From: Alfredo Rius Date: Mon, 31 Oct 2016 23:00:47 -0600 Subject: [PATCH 1/4] Now will remember states after reset --- examples/Bridge/Bridge.ino | 45 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/examples/Bridge/Bridge.ino b/examples/Bridge/Bridge.ino index 35c3aac..23fce3a 100644 --- a/examples/Bridge/Bridge.ino +++ b/examples/Bridge/Bridge.ino @@ -26,11 +26,20 @@ #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; void setup() { + // Recover pin states from memory + recoverPinStates(); + // Bridge startup pinMode(13, OUTPUT); digitalWrite(13, LOW); @@ -94,14 +103,15 @@ void digitalCommand(BridgeClient client) { value = digitalRead(pin); } + // Save Pin State + savePinState(pin,false,value); + // 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)); } @@ -119,6 +129,9 @@ void analogCommand(BridgeClient client) { value = client.parseInt(); analogWrite(pin, value); + // Save Pin State + savePinState(pin,true,value); + // Send feedback to client client.print(F("Pin D")); client.print(pin); @@ -181,3 +194,31 @@ void modeCommand(BridgeClient client) { client.print(F("error: invalid mode ")); client.print(mode); } + +void recoverPinStates(){ + int i,offset; + for(i=0;i Date: Mon, 31 Oct 2016 23:06:09 -0600 Subject: [PATCH 2/4] If output is defined set as output --- examples/Bridge/Bridge.ino | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/Bridge/Bridge.ino b/examples/Bridge/Bridge.ino index 23fce3a..c02d89d 100644 --- a/examples/Bridge/Bridge.ino +++ b/examples/Bridge/Bridge.ino @@ -10,8 +10,8 @@ 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/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) @@ -98,6 +98,7 @@ 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); @@ -127,6 +128,7 @@ void analogCommand(BridgeClient client) { if (client.read() == '/') { // Read value and execute command value = client.parseInt(); + pinMode(pin, OUTPUT); analogWrite(pin, value); // Save Pin State From 3fbd174ac88db9b7b6a8689caf8a02893aa02215 Mon Sep 17 00:00:00 2001 From: Alfredo Rius Date: Mon, 31 Oct 2016 23:25:28 -0600 Subject: [PATCH 3/4] Added JSON responses --- examples/Bridge/Bridge.ino | 67 +++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/examples/Bridge/Bridge.ino b/examples/Bridge/Bridge.ino index c02d89d..a50683a 100644 --- a/examples/Bridge/Bridge.ino +++ b/examples/Bridge/Bridge.ino @@ -1,11 +1,11 @@ /* 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: @@ -25,6 +25,7 @@ #include #include #include +#include // Define constants EEPROM #define PIN_MODE 0 @@ -35,17 +36,19 @@ // 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(); @@ -104,15 +107,15 @@ void digitalCommand(BridgeClient client) { value = digitalRead(pin); } - // Save Pin State - savePinState(pin,false,value); - // 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)); } @@ -127,19 +130,17 @@ void analogCommand(BridgeClient client) { // with a value like: "/analog/5/120" if (client.read() == '/') { // Read value and execute command - value = client.parseInt(); pinMode(pin, OUTPUT); + value = client.parseInt(); analogWrite(pin, value); + // Send feedback to client + response = String(value); + client.print(String("{\"analog\":"+response+"}")); + // Save Pin State savePinState(pin,true,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; @@ -149,10 +150,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"; @@ -169,7 +168,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; } @@ -178,23 +177,25 @@ 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(){ From ecdb854f2e72a273e63f5745fdea9734ff3f6221 Mon Sep 17 00:00:00 2001 From: Alfredo Rius Date: Tue, 1 Nov 2016 02:09:30 -0600 Subject: [PATCH 4/4] Added toggle command --- examples/Bridge/Bridge.ino | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/examples/Bridge/Bridge.ino b/examples/Bridge/Bridge.ino index a50683a..a65971d 100644 --- a/examples/Bridge/Bridge.ino +++ b/examples/Bridge/Bridge.ino @@ -11,6 +11,7 @@ "/arduino/digital/13" -> digitalRead(13) "/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) @@ -79,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") { @@ -120,6 +126,29 @@ void digitalCommand(BridgeClient client) { 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; + Bridge.put(key, String(value)); +} + void analogCommand(BridgeClient client) { int pin, value; @@ -225,3 +254,4 @@ void clearPinState(int pin){ EEPROM.write(offset+PIN_MODE,0); } +