Qualche settimana fa l’amico ed appassionato di arduino e programmazione Roberto Pompermaier mi ha scritto dopo aver realizzato un interessante esperimento con l’Arduino Yun Bridge port
Subito dopo aver letto la sua email, in cui descriveva il suo test e l’esperimento condotto, ho chiesto a Roberto di scrivere qualche riga sulla sua esperienza e su se stesso.
Lascio alle sue parole questo articolo:
About Roberto
Roberto Pompermaier , classe 1985, dopo la laurea in Informatica all’università Trento ha frequentato un master in Domotica e Case Digitali presso la Politecnica di Madrid.
Si occupa principalmente di Domotica, SmartHome e Iot (Internet Of Things). Oltre a divertirsi su qualsiasi tipo di tavola (windsurf, snwoboard, surf, wakeboard…) negli ultimi anni ha sviluppato il progetto John O.S., una piattaforma per creare oggetti e servizi IoT con il minimo sforzo.
Il Tutorial Arduino Yun Bridge port
Hacking YunClient
void process(YunClient client) {
// read the command
String command = client.readStringUntil('/');
// is "digital" command?
if (command == "digital") {
digitalCommand(client);
return;
}
// is "analog" command?
if (command == "analog") {
analogCommand(client);
return;
}
// is "mode" command?
if (command == "mode") {
modeCommand(client);
return;
}
client.print(F("Unknow command: "));
client.print(command);
client.print(client.readString());
}
e quello dedotto dallo sketch.
/*
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 <YunServer.h>
#include <YunClient.h>
// Listen to the default port 5555, the Yún webserver
// will forward there all the HTTP requests you send
YunServer server;
void setup() {
// 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.noListenOnLocalhost();
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('/');
if (command=="GET ")
command = client.readStringUntil('/');
if (command=="POST ")
command = client.readStringUntil('/');
// is "digital" command?
if (command == "digital") {
digitalCommand(client);
return;
}
// is "analog" command?
if (command == "analog") {
analogCommand(client);
return;
}
// is "mode" command?
if (command == "mode") {
modeCommand(client);
return;
}
client.print(F("Unknow command:\n"));
client.print(command);
client.print(client.readString());
}
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.indexOf(' ')!=-1)
mode = mode.substring(0,mode.indexOf(' '));
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);
}
Teoria
digital/13
GET /digital/13 HTTP/1.1 Host: arduino.local:5555 Connection: keep-alive Cache-Control: max-age=0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36 Accept-Encoding: gzip, deflate, sdch Accept-Language: it-IT,it;q=0.8,es;q=0.6,en-US;q=0.4,en;q=0.2,en-GB;q=0.2
void loop() {
// Get clients coming from server
YunClient client = server.accept();
// There is a new client?
if (client) {
// Process request
//process(client);
client.print(client.readString());
// Close connection and free resources.
client.stop();
}
delay(50); // Poll every 50ms
}





Il blog mauroalfieri.it ed i suoi contenuti sono distribuiti con Licenza
2 commenti
Ciao,
invece di leggere solo un pin di arduino yun, sarebbe possibile leggere tramite uno script python un valore calcolato all’interno di uno sketch eseguito su yun?
Vorrei inviare la distanza calcolata (quindi non un singolo valore di un pin) con un sensore ad ultrasuoni collegato allo Yun, ad un server python in ascolto su un Raspberry
Autore
Ciao Carlo,
a memoria ricordo che la libreria Bridge della Yun possa interfacciarsi con molte informazioni presenti nello sketch.
Dovresti poter scambiare dati tra lo sketch e la parte openWRT leggendo nella documentazione ufficiale della libreria.