Skip to content

Commit dbd001a

Browse files
committed
Added example BridgeNew
1 parent 6fb6e63 commit dbd001a

File tree

1 file changed

+177
-0
lines changed
  • hardware/arduino/avr/libraries/Bridge/examples/BridgeNew

1 file changed

+177
-0
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
2+
// Possible commands are listed here:
3+
//
4+
// "digital/13" -> digitalRead(13)
5+
// "digital/13/1" -> digitalWrite(13, HIGH)
6+
// "analog/2/123" -> analogWrite(2, 123)
7+
// "analog/2" -> analogRead(2)
8+
// "mode/13/input" -> pinMode(13, INPUT)
9+
// "mode/13/output" -> pinMode(13, OUTPUT)
10+
11+
#include <Bridge.h>
12+
#include <YunServer.h>
13+
14+
// Listen on port 5555, the webserver on the Yun
15+
// will forward there all the HTTP requests for us.
16+
YunServer server(5555);
17+
18+
void setup() {
19+
Serial.begin(9600);
20+
21+
// Bridge startup
22+
pinMode(13,OUTPUT);
23+
digitalWrite(13, LOW);
24+
Bridge.begin();
25+
digitalWrite(13, HIGH);
26+
27+
// Listen for incoming connection only from localhost
28+
// (no one from the external network could connect)
29+
server.listenOnLocalhost();
30+
server.begin();
31+
}
32+
33+
void loop() {
34+
// Get clients coming from server
35+
YunClient client = server.accept();
36+
37+
// There is a new client?
38+
if (client) {
39+
// Process request
40+
process(client);
41+
42+
// Close connection and free resources.
43+
client.stop();
44+
}
45+
46+
delay(50); // Poll every 50ms
47+
}
48+
49+
void process(YunClient client) {
50+
// discard the first slash '/' character from the URL
51+
client.read();
52+
53+
// read the command
54+
String command = client.readStringUntil('/');
55+
56+
// is "digital" command?
57+
if (command == "digital") {
58+
digitalCommand(client);
59+
}
60+
61+
// is "analog" command?
62+
if (command == "analog") {
63+
analogCommand(client);
64+
}
65+
66+
// is "mode" command?
67+
if (command == "mode") {
68+
modeCommand(client);
69+
}
70+
}
71+
72+
void digitalCommand(YunClient client) {
73+
int pin, value;
74+
75+
// Read pin number
76+
pin = client.parseInt();
77+
78+
// If the next character is a '/' it means we have an URL
79+
// with a value like: "/digital/13/1"
80+
if (client.read() == '/') {
81+
value = client.parseInt();
82+
digitalWrite(pin, value);
83+
}
84+
else {
85+
value = digitalRead(pin);
86+
}
87+
88+
// Send feedback to client
89+
client.print(F("Pin D"));
90+
client.print(pin);
91+
client.print(F(" set to "));
92+
client.println(value);
93+
94+
// Update datastore key with the current pin value
95+
String key = "D";
96+
key += pin;
97+
Bridge.put(key, String(value));
98+
}
99+
100+
void analogCommand(YunClient client) {
101+
int pin, value;
102+
103+
// Read pin number
104+
pin = client.parseInt();
105+
106+
// If the next character is a '/' it means we have an URL
107+
// with a value like: "/analog/5/120"
108+
if (client.read() == '/') {
109+
// Read value and execute command
110+
value = client.parseInt();
111+
analogWrite(pin, value);
112+
113+
// Send feedback to client
114+
client.print(F("Pin D"));
115+
client.print(pin);
116+
client.print(F(" set to analog "));
117+
client.println(value);
118+
119+
// Update datastore key with the current pin value
120+
String key = "D";
121+
key += pin;
122+
Bridge.put(key, String(value));
123+
}
124+
else {
125+
// Read analog pin
126+
value = analogRead(pin);
127+
128+
// Send feedback to client
129+
client.print(F("Pin A"));
130+
client.print(pin);
131+
client.print(F(" reads analog "));
132+
client.println(value);
133+
134+
// Update datastore key with the current pin value
135+
String key = "A";
136+
key += pin;
137+
Bridge.put(key, String(value));
138+
}
139+
}
140+
141+
void modeCommand(YunClient client) {
142+
int pin;
143+
144+
// Read pin number
145+
pin = client.parseInt();
146+
147+
// If the next character is not a '/' we have a malformed URL
148+
if (client.read() != '/') {
149+
client.println(F("error"));
150+
return;
151+
}
152+
153+
String mode = client.readStringUntil('\r');
154+
155+
if (mode == "input") {
156+
pinMode(pin, INPUT);
157+
// Send feedback to client
158+
client.print(F("Pin D"));
159+
client.print(pin);
160+
client.print(F(" configured as INPUT!"));
161+
return;
162+
}
163+
164+
if (mode == "output") {
165+
pinMode(pin, OUTPUT);
166+
// Send feedback to client
167+
client.print(F("Pin D"));
168+
client.print(pin);
169+
client.print(F(" configured as OUTPUT!"));
170+
return;
171+
}
172+
173+
client.print(F("error: invalid mode "));
174+
client.print(mode);
175+
}
176+
177+

0 commit comments

Comments
 (0)