Skip to content

Instantly share code, notes, and snippets.

@darkwave
Last active February 12, 2016 13:46
Show Gist options
  • Save darkwave/e0345650bd5e361c8434 to your computer and use it in GitHub Desktop.
Save darkwave/e0345650bd5e361c8434 to your computer and use it in GitHub Desktop.
Project RobOld
#include <Bridge.h>
#include <HttpClient.h>
int lightValue;
int photoResistorPin = A0; //analog input 0
void setup() {
// Bridge takes about two seconds to start up
// it can be helpful to use the on-board LED
// as an indicator for when it has initialized
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
Bridge.begin();
digitalWrite(13, HIGH);
Serial.begin(9600);
//while (!Serial); // wait for a serial connection
}
void loop() {
lightValue = analogRead(photoResistorPin);
lightValue = map(lightValue, 0, 1023, 0, 100);
Serial.print(lightValue);
Serial.println("%");
if (lightValue > 50) {
HttpClient chrome;
chrome.get("http://192.168.240.1/arduino/digital/5/1");
} else {
HttpClient chrome;
chrome.get("http://192.168.240.1/arduino/digital/5/0");
}
delay(500);
}
/*
Arduino Yún Bridge example
This example for the Arduino 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() {
// Bridge startup
pinMode(13, OUTPUT);
pinMode(8, OUTPUT); //this is for the relay
digitalWrite(13, LOW);
Bridge.begin();
digitalWrite(13, HIGH);
// 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('/');
// 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);
}
int MOTOR_A = 0;
int MOTOR_B = 1;
int MOTOR_A_AND_B = 2;
#include <Bridge.h>
#include <BridgeServer.h>
#include <BridgeClient.h>
#include <HttpClient.h>
// Listen to the default port 5555, the Yún webserver
// will forward there all the HTTP requests you send
BridgeServer server;
void setup() {
// Bridge startup
Bridge.begin();
//Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
//Setup Channel B
pinMode(13, OUTPUT); //Initiates Motor Channel A pin
pinMode(8, OUTPUT); //Initiates Brake Channel A pin
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
// Listen for incoming connection only from localhost
// (no one from the external network could connect)
server.listenOnLocalhost();
server.begin();
}
void soundOnFire() {
int fireValue = analogRead(A2);
int fireRange = map(fireValue, 0, 1024, 0, 3);
switch(fireRange) {
case 0:
tone(10,1000, 20);
tone(10,4500, 20);
break;
case 1:
tone(10,3000, 20);
tone(10,2500, 20);
break;
case 2:
break;
}
}
void loop() {
soundOnFire();
// 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('/');
// is "digital" command?
if (command == "digital") {
digitalCommand(client);
}
// is "analog" command?
if (command == "analog") {
analogCommand(client);
}
// is "mode" command?
if (command == "mode") {
modeCommand(client);
}
if (command == "left") {
turnLeft();
}
if (command == "right") {
turnRight();
}
if (command == "go") {
goForward(255);
}
if (command == "stop") {
breakMotor(MOTOR_A_AND_B);
}
if (command == "lamp_on") {
HttpClient lampClient;
// Make a HTTP request:
lampClient.get("http://192.168.240.186/arduino/digital/8/1");
}
if (command == "lamp_off") {
HttpClient lampClient;
// Make a HTTP request:
lampClient.get("http://192.168.240.186/arduino/digital/8/0");
}
if (command == "fire") {
int fireValue = analogRead(A2);
int fireRange = map(fireValue, 0, 1024, 0, 3);
client.print(fireValue);
client.print(" ");
switch(fireRange) {
case 0:
client.println("Closed fire");
break;
case 1:
client.println("Distant fire");
break;
case 2:
client.println("No fire");
break;
}
}
}
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);
}
int MOTOR_FORWARD = HIGH;
int MOTOR_BACKWARD = LOW;
void go(int motorNumber, int motorSpeed, int motorDirection) {
if (motorNumber == MOTOR_A) {
//Motor A forward @ motorSpeed
digitalWrite(12, motorDirection); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, motorSpeed); //Spins the motor on Channel A at motorSpeed
} else if (motorNumber == MOTOR_B) {
//Motor B forward @ motorSpeed
digitalWrite(13, motorDirection); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, motorSpeed); //Spins the motor on Channel A at motorSpeed
} else if (motorNumber == MOTOR_A_AND_B) {
digitalWrite(12, motorDirection); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, motorSpeed); //Spins the motor on Channel A at motorSpeed
//Motor B forward @ motorSpeed
digitalWrite(13, motorDirection); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, motorSpeed); //Spins the motor on Channel A at motorSpeed
}
}
void goForward(int motorSpeed) {
go(MOTOR_B, 255, MOTOR_FORWARD);
go(MOTOR_A, 255, MOTOR_BACKWARD);
}
void goBackward(int motorSpeed) {
go(MOTOR_B, 255, MOTOR_BACKWARD);
go(MOTOR_A, 255, MOTOR_FORWARD);
}
void turnLeft() {
go(MOTOR_B, 255, MOTOR_BACKWARD);
go(MOTOR_A, 255, MOTOR_BACKWARD);
}
void turnRight() {
go(MOTOR_B, 255, MOTOR_FORWARD);
go(MOTOR_A, 255, MOTOR_FORWARD);
}
void breakMotor(int motorNumber) {
if (motorNumber == MOTOR_A) {
digitalWrite(9, HIGH); //Engage the Brake for Channel A
} else if (motorNumber == MOTOR_B) {
digitalWrite(8, HIGH); //Engage the Brake for Channel B
} else if (motorNumber == MOTOR_A_AND_B) {
digitalWrite(9, HIGH); //Engage the Brake for Channel A
digitalWrite(8, HIGH); //Engage the Brake for Channel B
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment