mirror of
https://github.com/arduino/Arduino.git
synced 2024-11-29 10:24:12 +01:00
BootWatcher removed
BridgeNew is the new Bridge
This commit is contained in:
parent
53aa42351f
commit
8c9a06056e
@ -1,90 +0,0 @@
|
|||||||
|
|
||||||
/*
|
|
||||||
Arduino Yun Boot watcher
|
|
||||||
|
|
||||||
Allows you to use the Yun's 32U4 processor as a
|
|
||||||
serial terminal for the linino processor
|
|
||||||
|
|
||||||
Upload this to an Arduino Yun via serial (not WiFi)
|
|
||||||
then open the serial monitor at 115200 to see the boot process
|
|
||||||
of the linino processor. You can also use the serial monitor
|
|
||||||
as a basic command line interface for the linino processor using
|
|
||||||
this sketch.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
* Arduino Yun
|
|
||||||
|
|
||||||
created March 2013
|
|
||||||
by Massimo Banzi
|
|
||||||
modified 26 May 2013
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
*/
|
|
||||||
|
|
||||||
long baud = 115200;
|
|
||||||
|
|
||||||
// Pin 13 has an LED connected on most Arduino boards.
|
|
||||||
// give it a name:
|
|
||||||
int led = 13;
|
|
||||||
int ledState = HIGH; // whether the LED is high or low
|
|
||||||
|
|
||||||
String bootString = "";
|
|
||||||
int bootLineCount = 0;
|
|
||||||
boolean booting = true;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(baud); // open serial connection to Linino
|
|
||||||
Serial1.begin(baud); // open serial connection via USB-Serial
|
|
||||||
|
|
||||||
// initialize the digital pin as an output.
|
|
||||||
pinMode(led, OUTPUT);
|
|
||||||
digitalWrite(led, ledState); // turn the LED on (HIGH is the voltage level)
|
|
||||||
while(booting) {
|
|
||||||
listenForBoot();
|
|
||||||
}
|
|
||||||
delay(500);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// After booting, become a serial terminal:
|
|
||||||
if (Serial.available()) { // got anything from USB-Serial?
|
|
||||||
char c = (char)Serial.read(); // read from USB-serial
|
|
||||||
Serial1.write(c); // write to Linino
|
|
||||||
ledState=!ledState; // invert LED state
|
|
||||||
digitalWrite(led, ledState); // toggle the LED
|
|
||||||
}
|
|
||||||
if (Serial1.available()) { // got anything from Linino?
|
|
||||||
char c = (char)Serial1.read(); // read from Linino
|
|
||||||
Serial.write(c); // write to USB-serial
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void listenForBoot() {
|
|
||||||
char c;
|
|
||||||
if (Serial1.available()) { // got anything from Linino?
|
|
||||||
c = (char)Serial1.read(); // read from Linino
|
|
||||||
|
|
||||||
if (c == '\n') { // clear the bootString every newline
|
|
||||||
bootLineCount++; // increment the boot line counter
|
|
||||||
Serial.println(bootLineCount); // print the count
|
|
||||||
bootString = ""; // clear the boot string
|
|
||||||
}
|
|
||||||
else { // anything other than newline, add to string
|
|
||||||
bootString += c;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// look for the final boot string message:
|
|
||||||
if (bootString.endsWith("entered forwarding state")) {
|
|
||||||
Serial1.println();
|
|
||||||
}
|
|
||||||
|
|
||||||
// look for the command prompt:
|
|
||||||
if (bootString.endsWith(":/#")) {
|
|
||||||
Serial.println("Ready for action.");
|
|
||||||
booting = false;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,150 +1,174 @@
|
|||||||
|
|
||||||
//#include <Bridge.h>
|
// Possible commands are listed here:
|
||||||
#include <Mailbox.h>
|
//
|
||||||
|
// "digital/13" -> digitalRead(13)
|
||||||
|
// "digital/13/1" -> digitalWrite(13, HIGH)
|
||||||
|
// "analog/2/123" -> analogWrite(2, 123)
|
||||||
|
// "analog/2" -> analogRead(2)
|
||||||
|
// "mode/13/input" -> pinMode(13, INPUT)
|
||||||
|
// "mode/13/output" -> pinMode(13, OUTPUT)
|
||||||
|
|
||||||
|
#include <Bridge.h>
|
||||||
|
#include <YunServer.h>
|
||||||
|
|
||||||
|
// Listen on default port 5555, the webserver on the Yun
|
||||||
|
// will forward there all the HTTP requests for us.
|
||||||
|
YunServer server;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
Serial.begin(9600);
|
||||||
|
|
||||||
|
// Bridge startup
|
||||||
pinMode(13,OUTPUT);
|
pinMode(13,OUTPUT);
|
||||||
digitalWrite(13, LOW);
|
digitalWrite(13, LOW);
|
||||||
Bridge.begin();
|
Bridge.begin();
|
||||||
digitalWrite(13, HIGH);
|
digitalWrite(13, HIGH);
|
||||||
Serial.begin(9600);
|
|
||||||
|
// Listen for incoming connection only from localhost
|
||||||
|
// (no one from the external network could connect)
|
||||||
|
server.listenOnLocalhost();
|
||||||
|
server.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
while (Mailbox.messageAvailable()) {
|
// Get clients coming from server
|
||||||
String msg;
|
YunClient client = server.accept();
|
||||||
Mailbox.readMessage(msg);
|
|
||||||
process(msg);
|
// There is a new client?
|
||||||
|
if (client) {
|
||||||
|
// Process request
|
||||||
|
process(client);
|
||||||
|
|
||||||
|
// Close connection and free resources.
|
||||||
|
client.stop();
|
||||||
}
|
}
|
||||||
delay(100); // Poll every 0.100s
|
|
||||||
|
delay(50); // Poll every 50ms
|
||||||
}
|
}
|
||||||
|
|
||||||
void process(String command) {
|
void process(YunClient client) {
|
||||||
Serial.println(command);
|
// read the command
|
||||||
// "digital/13" -> digitalRead(13)
|
String command = client.readStringUntil('/');
|
||||||
// "digital/13/1" -> digitalWrite(13, HIGH)
|
|
||||||
// "analog/2/123" -> analogWrite(2, 123)
|
|
||||||
// "analog/2" -> analogRead(2)
|
|
||||||
// "mode/13/input" -> pinMode(13, INPUT)
|
|
||||||
// "mode/13/output" -> pinMode(13, OUTPUT)
|
|
||||||
|
|
||||||
// is digital command?
|
// is "digital" command?
|
||||||
if (command.startsWith("digital/")) {
|
if (command == "digital") {
|
||||||
// extract subcommand (after the "/")
|
digitalCommand(client);
|
||||||
command = command.substring(8);
|
}
|
||||||
digitalCommand(command);
|
|
||||||
|
|
||||||
}
|
// is "analog" command?
|
||||||
// is analog command?
|
if (command == "analog") {
|
||||||
else if (command.startsWith("analog/")) {
|
analogCommand(client);
|
||||||
// extract subcommand (after the "/")
|
}
|
||||||
command = command.substring(7);
|
|
||||||
analogCommand(command);
|
|
||||||
|
|
||||||
}
|
// is "mode" command?
|
||||||
// is mode command?
|
if (command == "mode") {
|
||||||
else if (command.startsWith("mode/")) {
|
modeCommand(client);
|
||||||
// extract subcommand (after the "/")
|
|
||||||
command = command.substring(5);
|
|
||||||
modeCommand(command);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void digitalCommand(String command) {
|
void digitalCommand(YunClient client) {
|
||||||
int pin, value;
|
int pin, value;
|
||||||
|
|
||||||
// Find the position of the "/" inside the command
|
// Read pin number
|
||||||
int slashIndex = command.indexOf("/");
|
pin = client.parseInt();
|
||||||
|
|
||||||
// If there are no slashes
|
// If the next character is a '/' it means we have an URL
|
||||||
if (slashIndex == -1) {
|
// with a value like: "/digital/13/1"
|
||||||
// then we are in the following case:
|
if (client.read() == '/') {
|
||||||
// "digital/13" -> digitalRead(13)
|
value = client.parseInt();
|
||||||
|
|
||||||
// so we can extract the pin number from the remainder of the command string
|
|
||||||
pin = command.toInt();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// else, we found a slash, so we are in the following case:
|
|
||||||
// "digital/13/1" -> digitalWrite(13, HIGH)
|
|
||||||
|
|
||||||
// we must estract pin number before the "/"
|
|
||||||
pin = command.substring(0, slashIndex).toInt();
|
|
||||||
// and value after the "/"
|
|
||||||
value = command.substring(slashIndex+1).toInt();
|
|
||||||
digitalWrite(pin, value);
|
digitalWrite(pin, value);
|
||||||
}
|
|
||||||
reportDigitalRead(pin, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
void analogCommand(String command) {
|
|
||||||
int pin, value;
|
|
||||||
if (command.indexOf("/") != -1) {
|
|
||||||
pin = command.substring(0, command.indexOf("/")).toInt();
|
|
||||||
value = command.substring(command.indexOf("/") + 1, command.length()).toInt();
|
|
||||||
analogWrite(pin, value);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
pin = command.toInt();
|
value = digitalRead(pin);
|
||||||
}
|
}
|
||||||
reportAnalogRead(pin, true);
|
|
||||||
|
// 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 modeCommand(String command) {
|
void analogCommand(YunClient client) {
|
||||||
int pin;
|
int pin, value;
|
||||||
String strValue;
|
|
||||||
pin = command.substring(0, command.indexOf("/")).toInt();
|
|
||||||
strValue = command.substring(command.indexOf("/") + 1, command.length());
|
|
||||||
if (strValue == "output") {
|
|
||||||
pinMode(pin, OUTPUT);
|
|
||||||
reportPinMode(pin, strValue);
|
|
||||||
}
|
|
||||||
else if (strValue == "input") {
|
|
||||||
pinMode(pin, INPUT);
|
|
||||||
reportPinMode(pin, strValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void reportPinMode(int pin, String mode) {
|
// Read pin number
|
||||||
String json = "{\"pin\":";
|
pin = client.parseInt();
|
||||||
json += pin;
|
|
||||||
json += ", \"mode\": \"";
|
|
||||||
json += mode;
|
|
||||||
json += "\"}";
|
|
||||||
Mailbox.writeJSON(json);
|
|
||||||
}
|
|
||||||
|
|
||||||
void reportDigitalRead(int pin, boolean dataset) {
|
// If the next character is a '/' it means we have an URL
|
||||||
int value = digitalRead(pin);
|
// with a value like: "/analog/5/120"
|
||||||
|
if (client.read() == '/') {
|
||||||
|
// Read value and execute command
|
||||||
|
value = client.parseInt();
|
||||||
|
analogWrite(pin, value);
|
||||||
|
|
||||||
String json = "{\"pin\":";
|
// Send feedback to client
|
||||||
json += pin;
|
client.print(F("Pin D"));
|
||||||
json += ", \"value\": ";
|
client.print(pin);
|
||||||
json += value;
|
client.print(F(" set to analog "));
|
||||||
json += "}";
|
client.println(value);
|
||||||
Mailbox.writeJSON(json);
|
|
||||||
|
|
||||||
if (dataset) {
|
// Update datastore key with the current pin value
|
||||||
String key = "D";
|
String key = "D";
|
||||||
key += pin;
|
key += pin;
|
||||||
Bridge.put(key.c_str(), String(value).c_str());
|
Bridge.put(key, String(value));
|
||||||
}
|
}
|
||||||
}
|
else {
|
||||||
|
// Read analog pin
|
||||||
|
value = analogRead(pin);
|
||||||
|
|
||||||
void reportAnalogRead(int pin, boolean dataset) {
|
// Send feedback to client
|
||||||
int value = analogRead(pin);
|
client.print(F("Pin A"));
|
||||||
|
client.print(pin);
|
||||||
|
client.print(F(" reads analog "));
|
||||||
|
client.println(value);
|
||||||
|
|
||||||
String json = "{\"pin\":";
|
// Update datastore key with the current pin value
|
||||||
json += pin;
|
|
||||||
json += ", \"value\": ";
|
|
||||||
json += value;
|
|
||||||
json += "}";
|
|
||||||
Mailbox.writeJSON(json);
|
|
||||||
|
|
||||||
if (dataset) {
|
|
||||||
String key = "A";
|
String key = "A";
|
||||||
key += pin;
|
key += pin;
|
||||||
Bridge.put(key.c_str(), String(value).c_str());
|
Bridge.put(key, String(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void modeCommand(YunClient 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,174 +0,0 @@
|
|||||||
|
|
||||||
// Possible commands are listed here:
|
|
||||||
//
|
|
||||||
// "digital/13" -> digitalRead(13)
|
|
||||||
// "digital/13/1" -> digitalWrite(13, HIGH)
|
|
||||||
// "analog/2/123" -> analogWrite(2, 123)
|
|
||||||
// "analog/2" -> analogRead(2)
|
|
||||||
// "mode/13/input" -> pinMode(13, INPUT)
|
|
||||||
// "mode/13/output" -> pinMode(13, OUTPUT)
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
#include <YunServer.h>
|
|
||||||
|
|
||||||
// Listen on default port 5555, the webserver on the Yun
|
|
||||||
// will forward there all the HTTP requests for us.
|
|
||||||
YunServer server;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(9600);
|
|
||||||
|
|
||||||
// Bridge startup
|
|
||||||
pinMode(13,OUTPUT);
|
|
||||||
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
|
|
||||||
YunClient 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(YunClient 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(YunClient 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(YunClient 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(YunClient 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user