source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/libraries/Bridge/examples/Bridge/Bridge.ino @ 46

Last change on this file since 46 was 46, checked in by jrpelegrina, 4 years ago

First release to Xenial

File size: 4.1 KB
Line 
1/*
2  Arduino Yún Bridge example
3
4  This example for the Arduino Yún shows how to use the
5  Bridge library to access the digital and analog pins
6  on the board through REST calls. It demonstrates how
7  you can create your own API when using REST style
8  calls through the browser.
9
10  Possible commands created in this shetch:
11
12  "/arduino/digital/13"     -> digitalRead(13)
13  "/arduino/digital/13/1"   -> digitalWrite(13, HIGH)
14  "/arduino/analog/2/123"   -> analogWrite(2, 123)
15  "/arduino/analog/2"       -> analogRead(2)
16  "/arduino/mode/13/input"  -> pinMode(13, INPUT)
17  "/arduino/mode/13/output" -> pinMode(13, OUTPUT)
18
19  This example code is part of the public domain
20
21  http://www.arduino.cc/en/Tutorial/Bridge
22
23*/
24
25#include <Bridge.h>
26#include <BridgeServer.h>
27#include <BridgeClient.h>
28
29// Listen to the default port 5555, the Yún webserver
30// will forward there all the HTTP requests you send
31BridgeServer server;
32
33void setup() {
34  // Bridge startup
35  pinMode(13, OUTPUT);
36  digitalWrite(13, LOW);
37  Bridge.begin();
38  digitalWrite(13, HIGH);
39
40  // Listen for incoming connection only from localhost
41  // (no one from the external network could connect)
42  server.listenOnLocalhost();
43  server.begin();
44}
45
46void loop() {
47  // Get clients coming from server
48  BridgeClient client = server.accept();
49
50  // There is a new client?
51  if (client) {
52    // Process request
53    process(client);
54
55    // Close connection and free resources.
56    client.stop();
57  }
58
59  delay(50); // Poll every 50ms
60}
61
62void process(BridgeClient client) {
63  // read the command
64  String command = client.readStringUntil('/');
65
66  // is "digital" command?
67  if (command == "digital") {
68    digitalCommand(client);
69  }
70
71  // is "analog" command?
72  if (command == "analog") {
73    analogCommand(client);
74  }
75
76  // is "mode" command?
77  if (command == "mode") {
78    modeCommand(client);
79  }
80}
81
82void digitalCommand(BridgeClient client) {
83  int pin, value;
84
85  // Read pin number
86  pin = client.parseInt();
87
88  // If the next character is a '/' it means we have an URL
89  // with a value like: "/digital/13/1"
90  if (client.read() == '/') {
91    value = client.parseInt();
92    digitalWrite(pin, value);
93  } else {
94    value = digitalRead(pin);
95  }
96
97  // Send feedback to client
98  client.print(F("Pin D"));
99  client.print(pin);
100  client.print(F(" set to "));
101  client.println(value);
102
103  // Update datastore key with the current pin value
104  String key = "D";
105  key += pin;
106  Bridge.put(key, String(value));
107}
108
109void analogCommand(BridgeClient client) {
110  int pin, value;
111
112  // Read pin number
113  pin = client.parseInt();
114
115  // If the next character is a '/' it means we have an URL
116  // with a value like: "/analog/5/120"
117  if (client.read() == '/') {
118    // Read value and execute command
119    value = client.parseInt();
120    analogWrite(pin, value);
121
122    // Send feedback to client
123    client.print(F("Pin D"));
124    client.print(pin);
125    client.print(F(" set to analog "));
126    client.println(value);
127
128    // Update datastore key with the current pin value
129    String key = "D";
130    key += pin;
131    Bridge.put(key, String(value));
132  } else {
133    // Read analog pin
134    value = analogRead(pin);
135
136    // Send feedback to client
137    client.print(F("Pin A"));
138    client.print(pin);
139    client.print(F(" reads analog "));
140    client.println(value);
141
142    // Update datastore key with the current pin value
143    String key = "A";
144    key += pin;
145    Bridge.put(key, String(value));
146  }
147}
148
149void modeCommand(BridgeClient client) {
150  int pin;
151
152  // Read pin number
153  pin = client.parseInt();
154
155  // If the next character is not a '/' we have a malformed URL
156  if (client.read() != '/') {
157    client.println(F("error"));
158    return;
159  }
160
161  String mode = client.readStringUntil('\r');
162
163  if (mode == "input") {
164    pinMode(pin, INPUT);
165    // Send feedback to client
166    client.print(F("Pin D"));
167    client.print(pin);
168    client.print(F(" configured as INPUT!"));
169    return;
170  }
171
172  if (mode == "output") {
173    pinMode(pin, OUTPUT);
174    // Send feedback to client
175    client.print(F("Pin D"));
176    client.print(pin);
177    client.print(F(" configured as OUTPUT!"));
178    return;
179  }
180
181  client.print(F("error: invalid mode "));
182  client.print(mode);
183}
184
185
Note: See TracBrowser for help on using the repository browser.