Skip to content

Instantly share code, notes, and snippets.

@alana314
Created February 25, 2018 02:18
Show Gist options
  • Save alana314/e97129f58f09e1d770d48bc665a85069 to your computer and use it in GitHub Desktop.
Save alana314/e97129f58f09e1d770d48bc665a85069 to your computer and use it in GitHub Desktop.
Arduino script for listening for bridge commands and controlling a robot arm
/*
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.
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/analog/2" -> analogRead(2)
"/arduino/mode/13/input" -> pinMode(13, INPUT)
"/arduino/mode/13/output" -> pinMode(13, OUTPUT)
This example code is part of the public domain
http://www.arduino.cc/en/Tutorial/Bridge
*/
#include <Bridge.h>
#include <BridgeServer.h>
#include <BridgeClient.h>
// Listen to the default port 5555, the Yún webserver
// will forward there all the HTTP requests you send
BridgeServer server;
void setup() {
Serial.begin(9600);
// Bridge startup
pinMode (1, OUTPUT);
pinMode (2, OUTPUT);
pinMode (3, OUTPUT);
pinMode (4, OUTPUT);
pinMode (5, OUTPUT);
pinMode (6, OUTPUT);
pinMode (7, OUTPUT);
pinMode (8, OUTPUT);
pinMode (9, OUTPUT);
pinMode (10, OUTPUT);
pinMode (11, OUTPUT);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
Bridge.begin();
// Listen for incoming connection only from localhost
// (no one from the external network could connect)
server.listenOnLocalhost();
server.begin();
}
void loop() {
// Get clients coming from server
BridgeClient client = server.accept();
// There is a new client?
if (client) {
// Process request
process(client);
// Close connection and free resources.
client.stop();
}
delay(50); // Poll every 50ms
}
void process(BridgeClient client) {
// read the command
String command = client.readStringUntil('/');
//bridge
Serial.println(command);
if (command == "turnonstop") {
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
Serial.println("Turn on stop");
// Send feedback to client
client.print(F("Stop On"));
}
if (command == "turnonopen") {
digitalWrite(2, LOW);
delay(1000);
digitalWrite(2, HIGH);
Serial.println("Turn on open");
// Send feedback to client
client.print(F("Open On"));
}
// if (command == "turnoffopen") {
// digitalWrite(2, HIGH);
// Serial.println("Turn off open");
// client.print(F("Open Off"));
// }
if (command == "turnonclose") {
digitalWrite(3, LOW);
delay(1000);
digitalWrite(3, HIGH);
Serial.println("Turn on open");
// Send feedback to client
client.print(F("Open On"));
}
if (command == "turnonup") {
digitalWrite(11, LOW);
delay(1000);
digitalWrite(11, HIGH);
Serial.println("Turn on up");
// Send feedback to client
client.print(F("Up On"));
}
if (command == "turnondown") {
digitalWrite(10, LOW);
delay(1000);
digitalWrite(10, HIGH);
Serial.println("Turn on down");
// Send feedback to client
client.print(F("Down On"));
}
if (command == "turnonback") {
digitalWrite(4, LOW);
delay(1000);
digitalWrite(4, HIGH);
Serial.println("Turn on back");
// Send feedback to client
client.print(F("Back On"));
}
if (command == "turnonforward") {
digitalWrite(5, LOW);
delay(1000);
digitalWrite(5, HIGH);
Serial.println("Turn on forward");
// Send feedback to client
client.print(F("Forward On"));
}
if (command == "turnontiltforward") {
digitalWrite(6, LOW);
delay(1000);
digitalWrite(6, HIGH);
Serial.println("Turn on tilt forward");
// Send feedback to client
client.print(F("Tilt Forward On"));
}
if (command == "turnontiltback") {
digitalWrite(7, LOW);
delay(1000);
digitalWrite(7, HIGH);
Serial.println("Turn on tilt back");
// Send feedback to client
client.print(F("Tilt Forward On"));
}
if (command == "turnonleft") {
digitalWrite(8, LOW);
delay(1000);
digitalWrite(8, HIGH);
Serial.println("Turn on left");
// Send feedback to client
client.print(F("Left On"));
}
if (command == "turnonright") {
digitalWrite(9, LOW);
delay(1000);
digitalWrite(9, HIGH);
Serial.println("Turn on right");
// Send feedback to client
client.print(F("Right On"));
}
if (command == "turnonplaywithcats") {
for(int i = 0; i < 5; i++)
{
digitalWrite(9, LOW);
delay(500);
digitalWrite(9, HIGH);
delay(500);
digitalWrite(8, LOW);
delay(500);
digitalWrite(8, HIGH);
delay(500);
}
for(int i = 0; i < 5; i++)
{
digitalWrite(7, LOW);
delay(200);
digitalWrite(7, HIGH);
delay(200);
digitalWrite(6, LOW);
delay(200);
digitalWrite(6, HIGH);
delay(200);
}
}
if (command == "turnonholdthis") {
}
if (command == "turnonletgo") {
}
if (command == "turnonchop") {
}
if (command == "turnonthrowthisaway") {
}
// is "digital" command?
if (command == "digital") {
digitalCommand(client);
}
// is "analog" command?
if (command == "analog") {
analogCommand(client);
}
// is "mode" command?
if (command == "mode") {
modeCommand(client);
}
}
void digitalCommand(BridgeClient client) {
int pin, value;
// Read pin number
pin = client.parseInt();
// If the next character is a '/' it means we have an URL
// with a value like: "/digital/13/1"
if (client.read() == '/') {
value = client.parseInt();
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);
// Update datastore key with the current pin value
String key = "D";
key += pin;
Bridge.put(key, String(value));
}
void analogCommand(BridgeClient client) {
int pin, value;
// Read pin number
pin = client.parseInt();
// If the next character is a '/' it means we have an URL
// with a value like: "/analog/5/120"
if (client.read() == '/') {
// Read value and execute command
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);
// Update datastore key with the current pin value
String key = "D";
key += pin;
Bridge.put(key, String(value));
} else {
// Read analog pin
value = analogRead(pin);
// Send feedback to client
client.print(F("Pin A"));
client.print(pin);
client.print(F(" reads analog "));
client.println(value);
// Update datastore key with the current pin value
String key = "A";
key += pin;
Bridge.put(key, String(value));
}
}
void modeCommand(BridgeClient client) {
int pin;
// Read pin number
pin = client.parseInt();
// If the next character is not a '/' we have a malformed URL
if (client.read() != '/') {
client.println(F("error"));
return;
}
String mode = client.readStringUntil('\r');
if (mode == "input") {
pinMode(pin, INPUT);
// Send feedback to client
client.print(F("Pin D"));
client.print(pin);
client.print(F(" configured as INPUT!"));
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!"));
return;
}
client.print(F("error: invalid mode "));
client.print(mode);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment