[136] | 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://arduino.cc/en/Tutorial/Bridge
|
---|
| 22 |
|
---|
| 23 | */
|
---|
| 24 |
|
---|
| 25 | #include <Bridge.h>
|
---|
| 26 | #include <YunServer.h>
|
---|
| 27 | #include <YunClient.h>
|
---|
| 28 |
|
---|
| 29 | // Listen to the default port 5555, the Y炭n webserver
|
---|
| 30 | // will forward there all the HTTP requests you send
|
---|
| 31 | YunServer server;
|
---|
| 32 |
|
---|
| 33 | void 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 |
|
---|
| 46 | void loop() {
|
---|
| 47 | // Get clients coming from server
|
---|
| 48 | YunClient 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 |
|
---|
| 62 | void process(YunClient 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 |
|
---|
| 82 | void digitalCommand(YunClient 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 | }
|
---|
| 94 | else {
|
---|
| 95 | value = digitalRead(pin);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | // Send feedback to client
|
---|
| 99 | client.print(F("Pin D"));
|
---|
| 100 | client.print(pin);
|
---|
| 101 | client.print(F(" set to "));
|
---|
| 102 | client.println(value);
|
---|
| 103 |
|
---|
| 104 | // Update datastore key with the current pin value
|
---|
| 105 | String key = "D";
|
---|
| 106 | key += pin;
|
---|
| 107 | Bridge.put(key, String(value));
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | void analogCommand(YunClient client) {
|
---|
| 111 | int pin, value;
|
---|
| 112 |
|
---|
| 113 | // Read pin number
|
---|
| 114 | pin = client.parseInt();
|
---|
| 115 |
|
---|
| 116 | // If the next character is a '/' it means we have an URL
|
---|
| 117 | // with a value like: "/analog/5/120"
|
---|
| 118 | if (client.read() == '/') {
|
---|
| 119 | // Read value and execute command
|
---|
| 120 | value = client.parseInt();
|
---|
| 121 | analogWrite(pin, value);
|
---|
| 122 |
|
---|
| 123 | // Send feedback to client
|
---|
| 124 | client.print(F("Pin D"));
|
---|
| 125 | client.print(pin);
|
---|
| 126 | client.print(F(" set to analog "));
|
---|
| 127 | client.println(value);
|
---|
| 128 |
|
---|
| 129 | // Update datastore key with the current pin value
|
---|
| 130 | String key = "D";
|
---|
| 131 | key += pin;
|
---|
| 132 | Bridge.put(key, String(value));
|
---|
| 133 | }
|
---|
| 134 | else {
|
---|
| 135 | // Read analog pin
|
---|
| 136 | value = analogRead(pin);
|
---|
| 137 |
|
---|
| 138 | // Send feedback to client
|
---|
| 139 | client.print(F("Pin A"));
|
---|
| 140 | client.print(pin);
|
---|
| 141 | client.print(F(" reads analog "));
|
---|
| 142 | client.println(value);
|
---|
| 143 |
|
---|
| 144 | // Update datastore key with the current pin value
|
---|
| 145 | String key = "A";
|
---|
| 146 | key += pin;
|
---|
| 147 | Bridge.put(key, String(value));
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | void modeCommand(YunClient client) {
|
---|
| 152 | int pin;
|
---|
| 153 |
|
---|
| 154 | // Read pin number
|
---|
| 155 | pin = client.parseInt();
|
---|
| 156 |
|
---|
| 157 | // If the next character is not a '/' we have a malformed URL
|
---|
| 158 | if (client.read() != '/') {
|
---|
| 159 | client.println(F("error"));
|
---|
| 160 | return;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | String mode = client.readStringUntil('\r');
|
---|
| 164 |
|
---|
| 165 | if (mode == "input") {
|
---|
| 166 | pinMode(pin, INPUT);
|
---|
| 167 | // Send feedback to client
|
---|
| 168 | client.print(F("Pin D"));
|
---|
| 169 | client.print(pin);
|
---|
| 170 | client.print(F(" configured as INPUT!"));
|
---|
| 171 | return;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | if (mode == "output") {
|
---|
| 175 | pinMode(pin, OUTPUT);
|
---|
| 176 | // Send feedback to client
|
---|
| 177 | client.print(F("Pin D"));
|
---|
| 178 | client.print(pin);
|
---|
| 179 | client.print(F(" configured as OUTPUT!"));
|
---|
| 180 | return;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | client.print(F("error: invalid mode "));
|
---|
| 184 | client.print(mode);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 |
|
---|