Skip to content

Commit 8c9a060

Browse files
author
Federico Fissore
committed
BootWatcher removed
BridgeNew is the new Bridge
1 parent 53aa423 commit 8c9a060

File tree

3 files changed

+132
-372
lines changed

3 files changed

+132
-372
lines changed

Diff for: hardware/arduino/avr/libraries/Bridge/examples/BootWatcher001/BootWatcher001.ino

-90
This file was deleted.
+132-108
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,174 @@
11

2-
//#include <Bridge.h>
3-
#include <Mailbox.h>
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 default port 5555, the webserver on the Yun
15+
// will forward there all the HTTP requests for us.
16+
YunServer server;
417

518
void setup() {
19+
Serial.begin(9600);
20+
21+
// Bridge startup
622
pinMode(13,OUTPUT);
723
digitalWrite(13, LOW);
824
Bridge.begin();
925
digitalWrite(13, HIGH);
10-
Serial.begin(9600);
26+
27+
// Listen for incoming connection only from localhost
28+
// (no one from the external network could connect)
29+
server.listenOnLocalhost();
30+
server.begin();
1131
}
1232

1333
void loop() {
14-
while (Mailbox.messageAvailable()) {
15-
String msg;
16-
Mailbox.readMessage(msg);
17-
process(msg);
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();
1844
}
19-
delay(100); // Poll every 0.100s
45+
46+
delay(50); // Poll every 50ms
2047
}
2148

22-
void process(String command) {
23-
Serial.println(command);
24-
// "digital/13" -> digitalRead(13)
25-
// "digital/13/1" -> digitalWrite(13, HIGH)
26-
// "analog/2/123" -> analogWrite(2, 123)
27-
// "analog/2" -> analogRead(2)
28-
// "mode/13/input" -> pinMode(13, INPUT)
29-
// "mode/13/output" -> pinMode(13, OUTPUT)
30-
31-
// is digital command?
32-
if (command.startsWith("digital/")) {
33-
// extract subcommand (after the "/")
34-
command = command.substring(8);
35-
digitalCommand(command);
49+
void process(YunClient client) {
50+
// read the command
51+
String command = client.readStringUntil('/');
3652

37-
}
38-
// is analog command?
39-
else if (command.startsWith("analog/")) {
40-
// extract subcommand (after the "/")
41-
command = command.substring(7);
42-
analogCommand(command);
53+
// is "digital" command?
54+
if (command == "digital") {
55+
digitalCommand(client);
56+
}
4357

44-
}
45-
// is mode command?
46-
else if (command.startsWith("mode/")) {
47-
// extract subcommand (after the "/")
48-
command = command.substring(5);
49-
modeCommand(command);
58+
// is "analog" command?
59+
if (command == "analog") {
60+
analogCommand(client);
61+
}
62+
63+
// is "mode" command?
64+
if (command == "mode") {
65+
modeCommand(client);
5066
}
5167
}
5268

53-
void digitalCommand(String command) {
69+
void digitalCommand(YunClient client) {
5470
int pin, value;
5571

56-
// Find the position of the "/" inside the command
57-
int slashIndex = command.indexOf("/");
72+
// Read pin number
73+
pin = client.parseInt();
5874

59-
// If there are no slashes
60-
if (slashIndex == -1) {
61-
// then we are in the following case:
62-
// "digital/13" -> digitalRead(13)
63-
64-
// so we can extract the pin number from the remainder of the command string
65-
pin = command.toInt();
75+
// If the next character is a '/' it means we have an URL
76+
// with a value like: "/digital/13/1"
77+
if (client.read() == '/') {
78+
value = client.parseInt();
79+
digitalWrite(pin, value);
6680
}
6781
else {
68-
// else, we found a slash, so we are in the following case:
69-
// "digital/13/1" -> digitalWrite(13, HIGH)
70-
71-
// we must estract pin number before the "/"
72-
pin = command.substring(0, slashIndex).toInt();
73-
// and value after the "/"
74-
value = command.substring(slashIndex+1).toInt();
75-
digitalWrite(pin, value);
82+
value = digitalRead(pin);
7683
}
77-
reportDigitalRead(pin, true);
84+
85+
// Send feedback to client
86+
client.print(F("Pin D"));
87+
client.print(pin);
88+
client.print(F(" set to "));
89+
client.println(value);
90+
91+
// Update datastore key with the current pin value
92+
String key = "D";
93+
key += pin;
94+
Bridge.put(key, String(value));
7895
}
7996

80-
void analogCommand(String command) {
97+
void analogCommand(YunClient client) {
8198
int pin, value;
82-
if (command.indexOf("/") != -1) {
83-
pin = command.substring(0, command.indexOf("/")).toInt();
84-
value = command.substring(command.indexOf("/") + 1, command.length()).toInt();
99+
100+
// Read pin number
101+
pin = client.parseInt();
102+
103+
// If the next character is a '/' it means we have an URL
104+
// with a value like: "/analog/5/120"
105+
if (client.read() == '/') {
106+
// Read value and execute command
107+
value = client.parseInt();
85108
analogWrite(pin, value);
86-
}
87-
else {
88-
pin = command.toInt();
89-
}
90-
reportAnalogRead(pin, true);
91-
}
92109

93-
void modeCommand(String command) {
94-
int pin;
95-
String strValue;
96-
pin = command.substring(0, command.indexOf("/")).toInt();
97-
strValue = command.substring(command.indexOf("/") + 1, command.length());
98-
if (strValue == "output") {
99-
pinMode(pin, OUTPUT);
100-
reportPinMode(pin, strValue);
101-
}
102-
else if (strValue == "input") {
103-
pinMode(pin, INPUT);
104-
reportPinMode(pin, strValue);
110+
// Send feedback to client
111+
client.print(F("Pin D"));
112+
client.print(pin);
113+
client.print(F(" set to analog "));
114+
client.println(value);
115+
116+
// Update datastore key with the current pin value
117+
String key = "D";
118+
key += pin;
119+
Bridge.put(key, String(value));
105120
}
106-
}
121+
else {
122+
// Read analog pin
123+
value = analogRead(pin);
124+
125+
// Send feedback to client
126+
client.print(F("Pin A"));
127+
client.print(pin);
128+
client.print(F(" reads analog "));
129+
client.println(value);
107130

108-
void reportPinMode(int pin, String mode) {
109-
String json = "{\"pin\":";
110-
json += pin;
111-
json += ", \"mode\": \"";
112-
json += mode;
113-
json += "\"}";
114-
Mailbox.writeJSON(json);
131+
// Update datastore key with the current pin value
132+
String key = "A";
133+
key += pin;
134+
Bridge.put(key, String(value));
135+
}
115136
}
116137

117-
void reportDigitalRead(int pin, boolean dataset) {
118-
int value = digitalRead(pin);
138+
void modeCommand(YunClient client) {
139+
int pin;
119140

120-
String json = "{\"pin\":";
121-
json += pin;
122-
json += ", \"value\": ";
123-
json += value;
124-
json += "}";
125-
Mailbox.writeJSON(json);
141+
// Read pin number
142+
pin = client.parseInt();
126143

127-
if (dataset) {
128-
String key = "D";
129-
key += pin;
130-
Bridge.put(key.c_str(), String(value).c_str());
144+
// If the next character is not a '/' we have a malformed URL
145+
if (client.read() != '/') {
146+
client.println(F("error"));
147+
return;
131148
}
132-
}
133149

134-
void reportAnalogRead(int pin, boolean dataset) {
135-
int value = analogRead(pin);
150+
String mode = client.readStringUntil('\r');
136151

137-
String json = "{\"pin\":";
138-
json += pin;
139-
json += ", \"value\": ";
140-
json += value;
141-
json += "}";
142-
Mailbox.writeJSON(json);
152+
if (mode == "input") {
153+
pinMode(pin, INPUT);
154+
// Send feedback to client
155+
client.print(F("Pin D"));
156+
client.print(pin);
157+
client.print(F(" configured as INPUT!"));
158+
return;
159+
}
143160

144-
if (dataset) {
145-
String key = "A";
146-
key += pin;
147-
Bridge.put(key.c_str(), String(value).c_str());
161+
if (mode == "output") {
162+
pinMode(pin, OUTPUT);
163+
// Send feedback to client
164+
client.print(F("Pin D"));
165+
client.print(pin);
166+
client.print(F(" configured as OUTPUT!"));
167+
return;
148168
}
169+
170+
client.print(F("error: invalid mode "));
171+
client.print(mode);
149172
}
150173

174+

0 commit comments

Comments
 (0)