Skip to content
This repository has been archived by the owner on Sep 30, 2021. It is now read-only.

Now will remember states after reset #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 102 additions & 28 deletions examples/Bridge/Bridge.ino
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -25,18 +26,30 @@
#include <Bridge.h>
#include <BridgeServer.h>
#include <BridgeClient.h>
#include <EEPROM.h>

// 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();
Expand Down Expand Up @@ -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") {
Expand All @@ -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;
Expand All @@ -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";
Expand All @@ -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";
Expand All @@ -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;
}

Expand All @@ -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<TOTAL_PINS;i++){
offset = i*MEMORY_SLOT_SIZE;
if(EEPROM.read(offset)==1){//Digital Output
pinMode(i,OUTPUT);
digitalWrite(i,EEPROM.read(offset+PIN_VALUE));
}else if(EEPROM.read(offset)==2){//Digital Output
pinMode(i,OUTPUT);
analogWrite(i,EEPROM.read(offset+PIN_VALUE));
}
}
}

void savePinState(int pin, uint8_t analog, uint8_t value){
int offset;
offset = pin*MEMORY_SLOT_SIZE;
EEPROM.write(offset+PIN_MODE,analog+1); // 1 if digital, 2 if analog
EEPROM.write(offset+PIN_VALUE,value);
}

void clearPinState(int pin){
int offset;
offset = pin*MEMORY_SLOT_SIZE;
EEPROM.write(offset+PIN_MODE,0);
}