mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-30 16:24:09 +03:00
Spacebrew update
This commit is contained in:
@ -40,9 +40,7 @@ void setup() {
|
||||
|
||||
// for debugging, wait until a serial console is connected
|
||||
delay(4000);
|
||||
while (!Serial) {
|
||||
;
|
||||
}
|
||||
while (!Serial) { ; }
|
||||
|
||||
// start-up the bridge
|
||||
Bridge.begin();
|
||||
@ -68,7 +66,8 @@ void setup() {
|
||||
|
||||
// connect to cloud spacebrew server at "sandbox.spacebrew.cc"
|
||||
sb.connect("sandbox.spacebrew.cc");
|
||||
|
||||
// we give some time to arduino to connect to sandbox, otherwise the first sb.monitor(); call will give an error
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
|
||||
@ -94,6 +93,7 @@ void loop() {
|
||||
|
||||
}
|
||||
}
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
// define handler methods, all standard data type handlers take two appropriate arguments
|
||||
|
@ -40,9 +40,7 @@ void setup() {
|
||||
|
||||
// for debugging, wait until a serial console is connected
|
||||
delay(4000);
|
||||
while (!Serial) {
|
||||
;
|
||||
}
|
||||
while (!Serial) { ; }
|
||||
|
||||
// start-up the bridge
|
||||
Bridge.begin();
|
||||
|
@ -41,9 +41,7 @@ void setup() {
|
||||
|
||||
// for debugging, wait until a serial console is connected
|
||||
delay(4000);
|
||||
while (!Serial) {
|
||||
;
|
||||
}
|
||||
while (!Serial) { ; }
|
||||
|
||||
// start-up the bridge
|
||||
Bridge.begin();
|
||||
|
@ -38,9 +38,7 @@ void setup() {
|
||||
|
||||
// for debugging, wait until a serial console is connected
|
||||
delay(4000);
|
||||
while (!Serial) {
|
||||
;
|
||||
}
|
||||
while (!Serial) { ; }
|
||||
|
||||
// start-up the bridge
|
||||
Bridge.begin();
|
||||
|
@ -1,4 +1,4 @@
|
||||
SpacebrewYun KEYWORD3
|
||||
SpacebrewYun KEYWORD1
|
||||
addPublish KEYWORD2
|
||||
addSubscribe KEYWORD2
|
||||
connect KEYWORD2
|
||||
|
@ -1,8 +1,10 @@
|
||||
name=SpacebrewYun
|
||||
version=1.0
|
||||
author=Julio Terra
|
||||
maintainer=Julio Terra <julioterra@gmail.com>
|
||||
email=julioterra@gmail.com
|
||||
sentence=Easily connect the Arduino Yun to Spacebrew
|
||||
paragraph=This library was developed to enable you to easily connect the Arduino Yun to Spacebrew. To learn more about Spacebrew visit Spacebrew.cc
|
||||
url=https://github.com/julioterra/yunSpacebrew
|
||||
architectures=*
|
||||
architectures=avr
|
||||
version=1.0
|
||||
dependencies=Bridge
|
||||
core-dependencies=arduino (>=1.5.0)
|
@ -10,6 +10,7 @@ SpacebrewYun::SpacebrewYun(const String& _name, const String& _description) {
|
||||
server = "sandbox.spacebrew.cc";
|
||||
port = 9000;
|
||||
|
||||
_started = false;
|
||||
_connected = false;
|
||||
_verbose = false;
|
||||
_error_msg = false;
|
||||
@ -21,6 +22,9 @@ SpacebrewYun::SpacebrewYun(const String& _name, const String& _description) {
|
||||
read_name = false;
|
||||
read_msg = false;
|
||||
|
||||
connect_attempt = 0;
|
||||
connect_attempt_inter = 10000;
|
||||
|
||||
for ( int i = 0; i < pidLength; i++ ) {
|
||||
pid [i] = '\0';
|
||||
}
|
||||
@ -45,25 +49,25 @@ SpacebrewYun::OnSBOpen SpacebrewYun::_onOpen = NULL;
|
||||
SpacebrewYun::OnSBClose SpacebrewYun::_onClose = NULL;
|
||||
SpacebrewYun::OnSBError SpacebrewYun::_onError = NULL;
|
||||
|
||||
void SpacebrewYun::onOpen(OnSBOpen function) {
|
||||
void SpacebrewYun::onOpen(OnSBOpen function){
|
||||
_onOpen = function;
|
||||
}
|
||||
void SpacebrewYun::onClose(OnSBClose function) {
|
||||
void SpacebrewYun::onClose(OnSBClose function){
|
||||
_onClose = function;
|
||||
}
|
||||
void SpacebrewYun::onRangeMessage(OnRangeMessage function) {
|
||||
void SpacebrewYun::onRangeMessage(OnRangeMessage function){
|
||||
_onRangeMessage = function;
|
||||
}
|
||||
void SpacebrewYun::onStringMessage(OnStringMessage function) {
|
||||
void SpacebrewYun::onStringMessage(OnStringMessage function){
|
||||
_onStringMessage = function;
|
||||
}
|
||||
void SpacebrewYun::onBooleanMessage(OnBooleanMessage function) {
|
||||
void SpacebrewYun::onBooleanMessage(OnBooleanMessage function){
|
||||
_onBooleanMessage = function;
|
||||
}
|
||||
void SpacebrewYun::onCustomMessage(OnCustomMessage function) {
|
||||
void SpacebrewYun::onCustomMessage(OnCustomMessage function){
|
||||
_onCustomMessage = function;
|
||||
}
|
||||
void SpacebrewYun::onError(OnSBError function) {
|
||||
void SpacebrewYun::onError(OnSBError function){
|
||||
_onError = function;
|
||||
}
|
||||
|
||||
@ -88,13 +92,13 @@ void SpacebrewYun::addPublish(const String& name, const String& type) {
|
||||
name.toCharArray(p->name, name.length() + 1);
|
||||
type.toCharArray(p->type, type.length() + 1);
|
||||
|
||||
if (publishers == NULL) {
|
||||
if (publishers == NULL){
|
||||
publishers = p;
|
||||
}
|
||||
else {
|
||||
struct Publisher *curr = publishers;
|
||||
int counter = 1;
|
||||
while (curr->next != NULL) {
|
||||
while(curr->next != NULL){
|
||||
curr = curr->next;
|
||||
counter++;
|
||||
}
|
||||
@ -110,12 +114,12 @@ void SpacebrewYun::addSubscribe(const String& name, const String& type) {
|
||||
name.toCharArray(s->name, name.length() + 1);
|
||||
type.toCharArray(s->type, type.length() + 1);
|
||||
|
||||
if (subscribers == NULL) {
|
||||
if (subscribers == NULL){
|
||||
subscribers = s;
|
||||
}
|
||||
else {
|
||||
struct Subscriber *curr = subscribers;
|
||||
while (curr->next != NULL) {
|
||||
while(curr->next != NULL){
|
||||
curr = curr->next;
|
||||
}
|
||||
curr->next = s;
|
||||
@ -123,8 +127,11 @@ void SpacebrewYun::addSubscribe(const String& name, const String& type) {
|
||||
}
|
||||
|
||||
void SpacebrewYun::connect(String _server, int _port) {
|
||||
Serial.print(F("NEW LIB"));
|
||||
_started = true;
|
||||
server = _server;
|
||||
port = _port;
|
||||
connect_attempt = millis();
|
||||
|
||||
killPids();
|
||||
|
||||
@ -142,7 +149,7 @@ void SpacebrewYun::connect(String _server, int _port) {
|
||||
|
||||
if (subscribers != NULL) {
|
||||
struct Subscriber *curr = subscribers;
|
||||
while (curr != NULL) {
|
||||
while(curr != NULL){
|
||||
if (_verbose) {
|
||||
Serial.print(F("Creating subscribers: "));
|
||||
Serial.print(curr->name);
|
||||
@ -163,7 +170,7 @@ void SpacebrewYun::connect(String _server, int _port) {
|
||||
}
|
||||
if (publishers != NULL) {
|
||||
struct Publisher *curr = publishers;
|
||||
while (curr != NULL) {
|
||||
while(curr != NULL){
|
||||
if (_verbose) {
|
||||
Serial.print(F("Creating publishers: "));
|
||||
Serial.print(curr->name);
|
||||
@ -189,34 +196,42 @@ void SpacebrewYun::connect(String _server, int _port) {
|
||||
if (_verbose) {
|
||||
Serial.println(F("Brew started "));
|
||||
}
|
||||
while (!Console) {
|
||||
;
|
||||
}
|
||||
while (!Console) { ; }
|
||||
}
|
||||
|
||||
void SpacebrewYun::monitor() {
|
||||
|
||||
// if not connected try to reconnect after appropriate interval
|
||||
if (_started && !_connected) {
|
||||
if ((millis() - connect_attempt) > connect_attempt_inter) {
|
||||
connect(server, port);
|
||||
}
|
||||
}
|
||||
|
||||
// if message received from console, then process it
|
||||
while (Console.available() > 0) {
|
||||
char c = Console.read();
|
||||
|
||||
if (c == char(CONNECTION_START) && !_connected) {
|
||||
if (c == char(CONNECTION_START) && _started && !_connected) {
|
||||
if (_verbose) {
|
||||
Serial.print(F("Connected to spacebrew server at: "));
|
||||
Serial.println(server);
|
||||
Serial.print(F("Application name set to: "));
|
||||
Serial.println(name);
|
||||
}
|
||||
if (_onOpen != NULL) {
|
||||
if (_onOpen != NULL){
|
||||
_onOpen();
|
||||
}
|
||||
_connected = true;
|
||||
}
|
||||
|
||||
else if (c == char(CONNECTION_END) && _connected) {
|
||||
_connected = false;
|
||||
if (_verbose) {
|
||||
Serial.print(F("Disconnected from spacebrew server at: "));
|
||||
Serial.println(server);
|
||||
}
|
||||
if (_onClose != NULL) {
|
||||
if (_onClose != NULL){
|
||||
_onClose();
|
||||
}
|
||||
}
|
||||
@ -269,9 +284,10 @@ void SpacebrewYun::monitor() {
|
||||
}
|
||||
}
|
||||
|
||||
if (publishers != NULL) {
|
||||
// check if received confirmation that linino received messages
|
||||
if (publishers != NULL && _connected) {
|
||||
struct Publisher *curr = publishers;
|
||||
while ((curr != NULL)) {
|
||||
while((curr != NULL)){
|
||||
|
||||
if ( (curr->confirmed == 0) && ((millis() - curr->time) > 50) ) {
|
||||
if (_verbose) {
|
||||
@ -289,13 +305,9 @@ void SpacebrewYun::monitor() {
|
||||
void SpacebrewYun::onConfirm() {
|
||||
if (publishers != NULL) {
|
||||
struct Publisher *curr = publishers;
|
||||
while ((curr != NULL)) {
|
||||
while((curr != NULL)){
|
||||
if (sub_name.equals(curr->name) == true) {
|
||||
curr->confirmed = true;
|
||||
// if (_verbose) {
|
||||
// Serial.print(F("confirmed "));
|
||||
// Serial.println(curr->name);
|
||||
// }
|
||||
break;
|
||||
}
|
||||
curr = curr->next;
|
||||
@ -318,7 +330,7 @@ void SpacebrewYun::verbose(boolean verbose = true) {
|
||||
void SpacebrewYun::onMessage() {
|
||||
if (subscribers != NULL) {
|
||||
struct Subscriber *curr = subscribers;
|
||||
while ((curr != NULL) && (sub_type == "")) {
|
||||
while((curr != NULL) && (sub_type == "")){
|
||||
if (sub_name.equals(curr->name) == true) {
|
||||
sub_type = curr->type;
|
||||
}
|
||||
@ -358,7 +370,7 @@ void SpacebrewYun::onMessage() {
|
||||
}
|
||||
|
||||
|
||||
void SpacebrewYun::send(const String& name, const String& value) {
|
||||
void SpacebrewYun::send(const String& name, const String& value){
|
||||
if (publishers != NULL) {
|
||||
|
||||
Console.print(char(29));
|
||||
@ -369,7 +381,7 @@ void SpacebrewYun::send(const String& name, const String& value) {
|
||||
Console.flush();
|
||||
|
||||
struct Publisher *curr = publishers;
|
||||
while (curr != NULL) {
|
||||
while(curr != NULL){
|
||||
if (name.equals(curr->name) == true) {
|
||||
int msg_len = 0;
|
||||
|
||||
@ -422,7 +434,7 @@ void SpacebrewYun::getPids() {
|
||||
sbPids[sbPidsIndex] = atoi(pid);
|
||||
if ( sbPidsIndex < (sbPidsLen - 1) ) sbPidsIndex = (sbPidsIndex + 1);
|
||||
|
||||
for ( int i = 0; i < pidLength; i++ ) {
|
||||
for( int i = 0; i < pidLength; i++ ){
|
||||
pid[i] = '\0';
|
||||
pidCharIndex = 0;
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ struct Publisher {
|
||||
long time;
|
||||
};
|
||||
|
||||
struct Subscriber {
|
||||
struct Subscriber{
|
||||
char *name;
|
||||
char *type;
|
||||
Subscriber *next;
|
||||
@ -44,12 +44,8 @@ class SpacebrewYun {
|
||||
void addSubscribe(const String&, const String&);
|
||||
|
||||
void connect(String, int);
|
||||
void connect() {
|
||||
connect(server, port);
|
||||
};
|
||||
void connect(String _server) {
|
||||
connect(String(_server), port);
|
||||
};
|
||||
void connect() { connect(server, port); };
|
||||
void connect(String _server) { connect(String(_server), port); };
|
||||
|
||||
void monitor();
|
||||
void onMessage();
|
||||
@ -58,21 +54,11 @@ class SpacebrewYun {
|
||||
boolean connected();
|
||||
|
||||
void send(const String&, const String&);
|
||||
void send(const String& name, char * value) {
|
||||
send(name, String(value));
|
||||
}
|
||||
void send(const String& name, bool value) {
|
||||
send(name, (value ? String("true") : String("false")));
|
||||
};
|
||||
void send(const String& name, int value) {
|
||||
send(name, String(value));
|
||||
};
|
||||
void send(const String& name, long value) {
|
||||
send(name, String(value));
|
||||
};
|
||||
void send(const String& name, float value) {
|
||||
send(name, String(value));
|
||||
};
|
||||
void send(const String& name, char * value) { send(name, String(value)); }
|
||||
void send(const String& name, bool value){ send(name, (value ? String("true") : String("false"))); };
|
||||
void send(const String& name, int value) { send(name, String(value)); };
|
||||
void send(const String& name, long value) { send(name, String(value)); };
|
||||
void send(const String& name, float value) { send(name, String(value)); };
|
||||
|
||||
void verbose(boolean);
|
||||
|
||||
@ -98,6 +84,7 @@ class SpacebrewYun {
|
||||
String name;
|
||||
String server;
|
||||
String description;
|
||||
boolean _started;
|
||||
boolean _connected;
|
||||
boolean _error_msg;
|
||||
boolean _verbose;
|
||||
@ -125,8 +112,9 @@ class SpacebrewYun {
|
||||
static int sub_msg_str_max;
|
||||
static int sub_msg_int_max;
|
||||
static int sub_msg_bool_max;
|
||||
// int sub_name_max;
|
||||
// int sub_msg_str_max;
|
||||
|
||||
long connect_attempt;
|
||||
int connect_attempt_inter;
|
||||
|
||||
Process pids;
|
||||
char pid [6];
|
||||
@ -135,12 +123,12 @@ class SpacebrewYun {
|
||||
void killPids();
|
||||
void getPids();
|
||||
|
||||
static char * createString(int len) {
|
||||
static char * createString(int len){
|
||||
char * out = ( char * ) malloc ( len + 1 );
|
||||
return out;
|
||||
}
|
||||
|
||||
static void emptyString(char * str, int len) {
|
||||
static void emptyString(char * str, int len){
|
||||
for (int i = 0; i < len; i++) {
|
||||
str[i] = '\0';
|
||||
}
|
||||
|
Reference in New Issue
Block a user