Bridge library to the 1.5 format
This commit is contained in:
parent
35f10e412f
commit
a3e2e68e2b
|
@ -1,229 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright (c) 2013 Arduino LLC. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "Bridge.h"
|
|
||||||
#include <util/crc16.h>
|
|
||||||
|
|
||||||
BridgeClass::BridgeClass(Stream &_stream) :
|
|
||||||
index(0), stream(_stream), started(false), max_retries(0) {
|
|
||||||
// Empty
|
|
||||||
}
|
|
||||||
|
|
||||||
void BridgeClass::begin() {
|
|
||||||
if (started)
|
|
||||||
return;
|
|
||||||
started = true;
|
|
||||||
|
|
||||||
// Wait for U-boot to finish startup
|
|
||||||
do {
|
|
||||||
dropAll();
|
|
||||||
delay(1000);
|
|
||||||
} while (stream.available()>0);
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
// Bridge interrupt:
|
|
||||||
// - Ask the bridge to close itself
|
|
||||||
uint8_t quit_cmd[] = {'X','X','X','X','X'};
|
|
||||||
max_retries = 1;
|
|
||||||
transfer(quit_cmd, 5);
|
|
||||||
|
|
||||||
// Bridge startup:
|
|
||||||
// - If the bridge is not running starts it safely
|
|
||||||
stream.print(CTRL_C);
|
|
||||||
delay(250);
|
|
||||||
stream.print(F("\n"));
|
|
||||||
delay(250);
|
|
||||||
stream.print(F("\n"));
|
|
||||||
delay(500);
|
|
||||||
// Wait for OpenWRT message
|
|
||||||
// "Press enter to activate console"
|
|
||||||
stream.print(F("run-bridge\n"));
|
|
||||||
delay(500);
|
|
||||||
dropAll();
|
|
||||||
|
|
||||||
// Reset the brigde to check if it is running
|
|
||||||
uint8_t cmd[] = {'X','X', '1','0','0'};
|
|
||||||
uint8_t res[1];
|
|
||||||
max_retries = 50;
|
|
||||||
uint16_t l = transfer(cmd, 5, res, 1);
|
|
||||||
if (l == TRANSFER_TIMEOUT) {
|
|
||||||
// Bridge didn't start...
|
|
||||||
// Maybe the board is starting-up?
|
|
||||||
|
|
||||||
// Wait and retry
|
|
||||||
delay(1000);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (res[0] != 0)
|
|
||||||
while (true);
|
|
||||||
|
|
||||||
max_retries = 50;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void BridgeClass::put(const char *key, const char *value) {
|
|
||||||
// TODO: do it in a more efficient way
|
|
||||||
String cmd = "D";
|
|
||||||
cmd += key;
|
|
||||||
cmd += "\xFE";
|
|
||||||
cmd += value;
|
|
||||||
transfer((uint8_t*)cmd.c_str(), cmd.length());
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int BridgeClass::get(const char *key, uint8_t *value, unsigned int maxlen) {
|
|
||||||
uint8_t cmd[] = {'d'};
|
|
||||||
unsigned int l = transfer(cmd, 1, (uint8_t *)key, strlen(key), value, maxlen);
|
|
||||||
if (l < maxlen)
|
|
||||||
value[l] = 0; // Zero-terminate string
|
|
||||||
return l;
|
|
||||||
}
|
|
||||||
|
|
||||||
void BridgeClass::crcUpdate(uint8_t c) {
|
|
||||||
|
|
||||||
CRC = _crc_ccitt_update(CRC, c);
|
|
||||||
//CRC = CRC ^ c;
|
|
||||||
//CRC = (CRC >> 8) + (CRC << 8);
|
|
||||||
}
|
|
||||||
|
|
||||||
void BridgeClass::crcReset() {
|
|
||||||
CRC = 0xFFFF;
|
|
||||||
}
|
|
||||||
|
|
||||||
void BridgeClass::crcWrite() {
|
|
||||||
stream.write((char)(CRC >> 8));
|
|
||||||
stream.write((char)(CRC & 0xFF));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool BridgeClass::crcCheck(uint16_t _CRC) {
|
|
||||||
return CRC == _CRC;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t BridgeClass::transfer(const uint8_t *buff1, uint16_t len1,
|
|
||||||
const uint8_t *buff2, uint16_t len2,
|
|
||||||
const uint8_t *buff3, uint16_t len3,
|
|
||||||
uint8_t *rxbuff, uint16_t rxlen)
|
|
||||||
{
|
|
||||||
uint16_t len = len1 + len2 + len3;
|
|
||||||
uint8_t retries = 0;
|
|
||||||
for ( ; retries<max_retries; retries++, delay(100), dropAll() /* Delay for retransmission */) {
|
|
||||||
// Send packet
|
|
||||||
crcReset();
|
|
||||||
stream.write((char)0xFF); // Start of packet (0xFF)
|
|
||||||
crcUpdate(0xFF);
|
|
||||||
stream.write((char)index); // Message index
|
|
||||||
crcUpdate(index);
|
|
||||||
stream.write((char)((len >> 8) & 0xFF)); // Message length (hi)
|
|
||||||
crcUpdate((len >> 8) & 0xFF);
|
|
||||||
stream.write((char)(len & 0xFF)); // Message length (lo)
|
|
||||||
crcUpdate(len & 0xFF);
|
|
||||||
for (uint16_t i=0; i<len1; i++) { // Payload
|
|
||||||
stream.write((char)buff1[i]);
|
|
||||||
crcUpdate(buff1[i]);
|
|
||||||
}
|
|
||||||
for (uint16_t i=0; i<len2; i++) { // Payload
|
|
||||||
stream.write((char)buff2[i]);
|
|
||||||
crcUpdate(buff2[i]);
|
|
||||||
}
|
|
||||||
for (uint16_t i=0; i<len3; i++) { // Payload
|
|
||||||
stream.write((char)buff3[i]);
|
|
||||||
crcUpdate(buff3[i]);
|
|
||||||
}
|
|
||||||
crcWrite(); // CRC
|
|
||||||
|
|
||||||
// Wait for ACK in 100ms
|
|
||||||
if (timedRead(100) != 0xFF)
|
|
||||||
continue;
|
|
||||||
crcReset();
|
|
||||||
crcUpdate(0xFF);
|
|
||||||
|
|
||||||
// Check packet index
|
|
||||||
if (timedRead(5) != index)
|
|
||||||
continue;
|
|
||||||
crcUpdate(index);
|
|
||||||
|
|
||||||
// Recv len
|
|
||||||
int lh = timedRead(5);
|
|
||||||
if (lh < 0)
|
|
||||||
continue;
|
|
||||||
crcUpdate(lh);
|
|
||||||
int ll = timedRead(5);
|
|
||||||
if (ll < 0)
|
|
||||||
continue;
|
|
||||||
crcUpdate(ll);
|
|
||||||
uint16_t l = lh;
|
|
||||||
l <<= 8;
|
|
||||||
l += ll;
|
|
||||||
|
|
||||||
// Recv data
|
|
||||||
for (uint16_t i=0; i<l; i++) {
|
|
||||||
int c = timedRead(5);
|
|
||||||
if (c < 0)
|
|
||||||
continue;
|
|
||||||
// Cut received data if rxbuffer is too small
|
|
||||||
if (i < rxlen)
|
|
||||||
rxbuff[i] = c;
|
|
||||||
crcUpdate(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check CRC
|
|
||||||
int crc_hi = timedRead(5);
|
|
||||||
if (crc_hi < 0)
|
|
||||||
continue;
|
|
||||||
int crc_lo = timedRead(5);
|
|
||||||
if (crc_lo < 0)
|
|
||||||
continue;
|
|
||||||
if (!crcCheck((crc_hi<<8)+crc_lo))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// Increase index
|
|
||||||
index++;
|
|
||||||
|
|
||||||
// Return bytes received
|
|
||||||
if (l > rxlen)
|
|
||||||
return rxlen;
|
|
||||||
return l;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Max retries exceeded
|
|
||||||
return TRANSFER_TIMEOUT;
|
|
||||||
}
|
|
||||||
|
|
||||||
int BridgeClass::timedRead(unsigned int timeout) {
|
|
||||||
int c;
|
|
||||||
unsigned long _startMillis = millis();
|
|
||||||
do {
|
|
||||||
c = stream.read();
|
|
||||||
if (c >= 0) return c;
|
|
||||||
} while(millis() - _startMillis < timeout);
|
|
||||||
return -1; // -1 indicates timeout
|
|
||||||
}
|
|
||||||
|
|
||||||
void BridgeClass::dropAll() {
|
|
||||||
while (stream.available() > 0) {
|
|
||||||
stream.read();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Bridge instance
|
|
||||||
#ifdef __AVR_ATmega32U4__
|
|
||||||
// Leonardo variants (where HardwareSerial is Serial1)
|
|
||||||
SerialBridgeClass Bridge(Serial1);
|
|
||||||
#else
|
|
||||||
SerialBridgeClass Bridge(Serial);
|
|
||||||
#endif
|
|
|
@ -1,97 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright (c) 2013 Arduino LLC. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef BRIDGE_H_
|
|
||||||
#define BRIDGE_H_
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
#include <Stream.h>
|
|
||||||
|
|
||||||
class BridgeClass {
|
|
||||||
public:
|
|
||||||
BridgeClass(Stream &_stream);
|
|
||||||
void begin();
|
|
||||||
|
|
||||||
// Methods to handle key/value datastore
|
|
||||||
void put(const char *key, const char *value);
|
|
||||||
void put(const String &key, const String &value)
|
|
||||||
{ put(key.c_str(), value.c_str()); }
|
|
||||||
unsigned int get(const char *key, uint8_t *buff, unsigned int size);
|
|
||||||
unsigned int get(const char *key, char *value, unsigned int maxlen)
|
|
||||||
{ get(key, reinterpret_cast<uint8_t *>(value), maxlen); }
|
|
||||||
|
|
||||||
// Trasnfer a frame (with error correction and response)
|
|
||||||
uint16_t transfer(const uint8_t *buff1, uint16_t len1,
|
|
||||||
const uint8_t *buff2, uint16_t len2,
|
|
||||||
const uint8_t *buff3, uint16_t len3,
|
|
||||||
uint8_t *rxbuff, uint16_t rxlen);
|
|
||||||
// multiple inline versions of the same function to allow efficient frame concatenation
|
|
||||||
uint16_t transfer(const uint8_t *buff1, uint16_t len1)
|
|
||||||
{ return transfer(buff1, len1, NULL, 0); }
|
|
||||||
uint16_t transfer(const uint8_t *buff1, uint16_t len1,
|
|
||||||
uint8_t *rxbuff, uint16_t rxlen)
|
|
||||||
{ return transfer(buff1, len1, NULL, 0, rxbuff, rxlen); }
|
|
||||||
uint16_t transfer(const uint8_t *buff1, uint16_t len1,
|
|
||||||
const uint8_t *buff2, uint16_t len2,
|
|
||||||
uint8_t *rxbuff, uint16_t rxlen)
|
|
||||||
{ return transfer(buff1, len1, buff2, len2, NULL, 0, rxbuff, rxlen); }
|
|
||||||
|
|
||||||
static const int TRANSFER_TIMEOUT = 0xFFFF;
|
|
||||||
|
|
||||||
private:
|
|
||||||
uint8_t index;
|
|
||||||
int timedRead(unsigned int timeout);
|
|
||||||
void dropAll();
|
|
||||||
|
|
||||||
private:
|
|
||||||
void crcUpdate(uint8_t c);
|
|
||||||
void crcReset();
|
|
||||||
void crcWrite();
|
|
||||||
bool crcCheck(uint16_t _CRC);
|
|
||||||
uint16_t CRC;
|
|
||||||
|
|
||||||
private:
|
|
||||||
static const char CTRL_C = 3;
|
|
||||||
Stream &stream;
|
|
||||||
bool started;
|
|
||||||
uint8_t max_retries;
|
|
||||||
};
|
|
||||||
|
|
||||||
// This subclass uses a serial port Stream
|
|
||||||
class SerialBridgeClass : public BridgeClass {
|
|
||||||
public:
|
|
||||||
SerialBridgeClass(HardwareSerial &_serial)
|
|
||||||
: BridgeClass(_serial), serial(_serial) {
|
|
||||||
// Empty
|
|
||||||
}
|
|
||||||
|
|
||||||
void begin() {
|
|
||||||
serial.begin(250000);
|
|
||||||
BridgeClass::begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
HardwareSerial &serial;
|
|
||||||
};
|
|
||||||
|
|
||||||
extern SerialBridgeClass Bridge;
|
|
||||||
|
|
||||||
#endif /* BRIDGE_H_ */
|
|
||||||
|
|
||||||
#include <Console.h>
|
|
||||||
#include <Process.h>
|
|
|
@ -1,153 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright (c) 2013 Arduino LLC. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Console.h>
|
|
||||||
|
|
||||||
// Default constructor uses global Bridge instance
|
|
||||||
ConsoleClass::ConsoleClass() :
|
|
||||||
bridge(Bridge), inBuffered(0), inReadPos(0), inBuffer(NULL),
|
|
||||||
autoFlush(true)
|
|
||||||
{
|
|
||||||
// Empty
|
|
||||||
}
|
|
||||||
|
|
||||||
// Constructor with a user provided BridgeClass instance
|
|
||||||
ConsoleClass::ConsoleClass(BridgeClass &_b) :
|
|
||||||
bridge(_b), inBuffered(0), inReadPos(0), inBuffer(NULL),
|
|
||||||
autoFlush(true)
|
|
||||||
{
|
|
||||||
// Empty
|
|
||||||
}
|
|
||||||
|
|
||||||
ConsoleClass::~ConsoleClass() {
|
|
||||||
end();
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t ConsoleClass::write(uint8_t c) {
|
|
||||||
if (autoFlush) {
|
|
||||||
uint8_t tmp[] = { 'P', c };
|
|
||||||
bridge.transfer(tmp, 2);
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
outBuffer[outBuffered++] = c;
|
|
||||||
if (outBuffered == outBufferSize)
|
|
||||||
flush();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t ConsoleClass::write(const uint8_t *buff, size_t size) {
|
|
||||||
if (autoFlush) {
|
|
||||||
// TODO: do it in a more efficient way
|
|
||||||
uint8_t *tmp = new uint8_t[size+1];
|
|
||||||
tmp[0] = 'P';
|
|
||||||
memcpy(tmp+1, buff, size);
|
|
||||||
bridge.transfer(tmp, size+1);
|
|
||||||
delete[] tmp;
|
|
||||||
return size;
|
|
||||||
} else {
|
|
||||||
while (size > 0) {
|
|
||||||
outBuffer[outBuffered++] = *buff++;
|
|
||||||
size--;
|
|
||||||
if (outBuffered == outBufferSize)
|
|
||||||
flush();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConsoleClass::flush() {
|
|
||||||
if (autoFlush)
|
|
||||||
return;
|
|
||||||
|
|
||||||
bridge.transfer(outBuffer, outBuffered);
|
|
||||||
outBuffered = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConsoleClass::noBuffer() {
|
|
||||||
if (autoFlush)
|
|
||||||
return;
|
|
||||||
delete[] outBuffer;
|
|
||||||
autoFlush = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConsoleClass::buffer(uint8_t size) {
|
|
||||||
noBuffer();
|
|
||||||
if (size==0)
|
|
||||||
return;
|
|
||||||
outBuffer = new uint8_t[size+1];
|
|
||||||
outBuffer[0] = 'P'; // WRITE tag
|
|
||||||
outBufferSize = size+1;
|
|
||||||
outBuffered = 1;
|
|
||||||
autoFlush = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ConsoleClass::connected() {
|
|
||||||
uint8_t tmp = 'a';
|
|
||||||
bridge.transfer(&tmp, 1, &tmp, 1);
|
|
||||||
return tmp==1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int ConsoleClass::available() {
|
|
||||||
// Look if there is new data available
|
|
||||||
doBuffer();
|
|
||||||
return inBuffered;
|
|
||||||
}
|
|
||||||
|
|
||||||
int ConsoleClass::read() {
|
|
||||||
doBuffer();
|
|
||||||
if (inBuffered == 0)
|
|
||||||
return -1; // no chars available
|
|
||||||
else {
|
|
||||||
inBuffered--;
|
|
||||||
return inBuffer[inReadPos++];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int ConsoleClass::peek() {
|
|
||||||
doBuffer();
|
|
||||||
if (inBuffered == 0)
|
|
||||||
return -1; // no chars available
|
|
||||||
else
|
|
||||||
return inBuffer[inReadPos];
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConsoleClass::doBuffer() {
|
|
||||||
// If there are already char in buffer exit
|
|
||||||
if (inBuffered > 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Try to buffer up to 32 characters
|
|
||||||
inReadPos = 0;
|
|
||||||
uint8_t tmp[] = { 'p', BUFFER_SIZE };
|
|
||||||
inBuffered = bridge.transfer(tmp, 2, inBuffer, BUFFER_SIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConsoleClass::begin() {
|
|
||||||
bridge.begin();
|
|
||||||
end();
|
|
||||||
inBuffer = new uint8_t[BUFFER_SIZE];
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConsoleClass::end() {
|
|
||||||
noBuffer();
|
|
||||||
if (inBuffer) {
|
|
||||||
delete[] inBuffer;
|
|
||||||
inBuffer = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ConsoleClass Console;
|
|
|
@ -1,69 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright (c) 2013 Arduino LLC. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef CONSOLE_H_
|
|
||||||
#define CONSOLE_H_
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
|
|
||||||
class ConsoleClass : public Stream {
|
|
||||||
public:
|
|
||||||
// Default constructor uses global Bridge instance
|
|
||||||
ConsoleClass();
|
|
||||||
// Constructor with a user provided BridgeClass instance
|
|
||||||
ConsoleClass(BridgeClass &_b);
|
|
||||||
~ConsoleClass();
|
|
||||||
|
|
||||||
void begin();
|
|
||||||
void end();
|
|
||||||
|
|
||||||
void buffer(uint8_t size);
|
|
||||||
void noBuffer();
|
|
||||||
|
|
||||||
bool connected();
|
|
||||||
|
|
||||||
// Stream methods
|
|
||||||
// (read from console socket)
|
|
||||||
int available();
|
|
||||||
int read();
|
|
||||||
int peek();
|
|
||||||
// (write to console socket)
|
|
||||||
size_t write(uint8_t);
|
|
||||||
size_t write(const uint8_t *buffer, size_t size);
|
|
||||||
void flush();
|
|
||||||
|
|
||||||
operator bool () { return connected(); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
BridgeClass &bridge;
|
|
||||||
|
|
||||||
void doBuffer();
|
|
||||||
uint8_t inBuffered;
|
|
||||||
uint8_t inReadPos;
|
|
||||||
static const int BUFFER_SIZE = 32;
|
|
||||||
uint8_t *inBuffer;
|
|
||||||
|
|
||||||
bool autoFlush;
|
|
||||||
uint8_t outBuffered;
|
|
||||||
uint8_t outBufferSize;
|
|
||||||
uint8_t *outBuffer;
|
|
||||||
};
|
|
||||||
|
|
||||||
extern ConsoleClass Console;
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,250 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright (c) 2013 Arduino LLC. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <FileIO.h>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
File::File(BridgeClass &b) : mode(255), bridge(b) {
|
|
||||||
// Empty
|
|
||||||
}
|
|
||||||
|
|
||||||
File::File(const char *_filename, uint8_t _mode, BridgeClass &b) : mode(_mode), bridge(b) {
|
|
||||||
filename = _filename;
|
|
||||||
char modes[] = {'r','w','a'};
|
|
||||||
uint8_t cmd[] = {'F', modes[mode]};
|
|
||||||
uint8_t res[2];
|
|
||||||
dirPosition = 1;
|
|
||||||
bridge.transfer(cmd, 2, (uint8_t*)filename.c_str(), filename.length(), res, 2);
|
|
||||||
if (res[0] != 0) { // res[0] contains error code
|
|
||||||
mode = 255; // In case of error keep the file closed
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
handle = res[1];
|
|
||||||
buffered = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
File::operator bool() {
|
|
||||||
return (mode != 255);
|
|
||||||
}
|
|
||||||
|
|
||||||
File::~File() {
|
|
||||||
close();
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t File::write(uint8_t c) {
|
|
||||||
return write(&c, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t File::write(const uint8_t *buf, size_t size) {
|
|
||||||
if (mode == 255)
|
|
||||||
return -1;
|
|
||||||
uint8_t cmd[] = {'g', handle};
|
|
||||||
uint8_t res[1];
|
|
||||||
bridge.transfer(cmd, 2, buf, size, res, 1);
|
|
||||||
if (res[0] != 0) // res[0] contains error code
|
|
||||||
return -res[0];
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
int File::read() {
|
|
||||||
doBuffer();
|
|
||||||
if (buffered == 0)
|
|
||||||
return -1; // no chars available
|
|
||||||
else {
|
|
||||||
buffered--;
|
|
||||||
return buffer[readPos++];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int File::peek() {
|
|
||||||
doBuffer();
|
|
||||||
if (buffered == 0)
|
|
||||||
return -1; // no chars available
|
|
||||||
else
|
|
||||||
return buffer[readPos];
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean File::seek(uint32_t position) {
|
|
||||||
uint8_t cmd[] = {
|
|
||||||
's',
|
|
||||||
handle,
|
|
||||||
(position >> 24) & 0xFF,
|
|
||||||
(position >> 16) & 0xFF,
|
|
||||||
(position >> 8) & 0xFF,
|
|
||||||
position & 0xFF
|
|
||||||
};
|
|
||||||
uint8_t res[1];
|
|
||||||
bridge.transfer(cmd, 6, res, 1);
|
|
||||||
if (res[0]==0) {
|
|
||||||
// If seek succeed then flush buffers
|
|
||||||
buffered = 0;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t File::position() {
|
|
||||||
uint8_t cmd[] = {'S', handle};
|
|
||||||
uint8_t res[5];
|
|
||||||
bridge.transfer(cmd, 2, res, 5);
|
|
||||||
//err = res[0]; // res[0] contains error code
|
|
||||||
uint32_t pos = res[1] << 24;
|
|
||||||
pos += res[2] << 16;
|
|
||||||
pos += res[3] << 8;
|
|
||||||
pos += res[4];
|
|
||||||
return pos - buffered;
|
|
||||||
}
|
|
||||||
|
|
||||||
void File::doBuffer() {
|
|
||||||
// If there are already char in buffer exit
|
|
||||||
if (buffered > 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Try to buffer up to 32 characters
|
|
||||||
readPos = 0;
|
|
||||||
uint8_t cmd[] = {'G', handle, sizeof(buffer)};
|
|
||||||
buffered = bridge.transfer(cmd, 3, buffer, sizeof(buffer)) - 1;
|
|
||||||
//err = buff[0]; // First byte is error code
|
|
||||||
if (buffered>0) {
|
|
||||||
// Shift the reminder of buffer
|
|
||||||
for (uint8_t i=0; i<buffered; i++)
|
|
||||||
buffer[i] = buffer[i+1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int File::available() {
|
|
||||||
// Look if there is new data available
|
|
||||||
doBuffer();
|
|
||||||
return buffered;
|
|
||||||
}
|
|
||||||
|
|
||||||
void File::flush() {
|
|
||||||
}
|
|
||||||
|
|
||||||
//int read(void *buf, uint16_t nbyte)
|
|
||||||
|
|
||||||
//uint32_t size()
|
|
||||||
|
|
||||||
void File::close() {
|
|
||||||
if (mode == 255)
|
|
||||||
return;
|
|
||||||
uint8_t cmd[] = {'f', handle};
|
|
||||||
bridge.transfer(cmd, 2);
|
|
||||||
mode = 255;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *File::name() {
|
|
||||||
return filename.c_str();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
boolean File::isDirectory() {
|
|
||||||
uint8_t res[1];
|
|
||||||
uint8_t lenght;
|
|
||||||
uint8_t cmd[] = {'i'};
|
|
||||||
if (mode != 255)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
bridge.transfer(cmd, 1, (uint8_t *)filename.c_str(), filename.length(), res, 1);
|
|
||||||
return res[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
File File::openNextFile(uint8_t mode){
|
|
||||||
Process awk;
|
|
||||||
char tmp;
|
|
||||||
String command;
|
|
||||||
String filepath;
|
|
||||||
if (dirPosition == 0xFFFF) return File();
|
|
||||||
|
|
||||||
command = "ls ";
|
|
||||||
command += filename;
|
|
||||||
command += " | awk 'NR==";
|
|
||||||
command += dirPosition;
|
|
||||||
command += "'";
|
|
||||||
|
|
||||||
awk.runShellCommand(command);
|
|
||||||
|
|
||||||
while(awk.running());
|
|
||||||
|
|
||||||
command = "";
|
|
||||||
|
|
||||||
while (awk.available()){
|
|
||||||
tmp = awk.read();
|
|
||||||
if (tmp!='\n') command += tmp;
|
|
||||||
}
|
|
||||||
if (command.length() == 0)
|
|
||||||
return File();
|
|
||||||
dirPosition++;
|
|
||||||
filepath = filename + "/" + command;
|
|
||||||
return File(filepath.c_str(),mode);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void File::rewindDirectory(void){
|
|
||||||
dirPosition = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
boolean FileSystemClass::begin() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
File FileSystemClass::open(const char *filename, uint8_t mode) {
|
|
||||||
return File(filename, mode);
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean FileSystemClass::exists(const char *filepath) {
|
|
||||||
Process ls;
|
|
||||||
ls.begin("ls");
|
|
||||||
ls.addParameter(filepath);
|
|
||||||
int res = ls.run();
|
|
||||||
return (res == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean FileSystemClass::mkdir(const char *filepath) {
|
|
||||||
Process mk;
|
|
||||||
mk.begin("mkdir");
|
|
||||||
mk.addParameter("-p");
|
|
||||||
mk.addParameter(filepath);
|
|
||||||
int res = mk.run();
|
|
||||||
return (res == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean FileSystemClass::remove(const char *filepath) {
|
|
||||||
Process rm;
|
|
||||||
rm.begin("rm");
|
|
||||||
rm.addParameter(filepath);
|
|
||||||
int res = rm.run();
|
|
||||||
return (res == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean FileSystemClass::rmdir(const char *filepath) {
|
|
||||||
Process rm;
|
|
||||||
rm.begin("rmdir");
|
|
||||||
rm.addParameter(filepath);
|
|
||||||
int res = rm.run();
|
|
||||||
return (res == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
FileSystemClass FileSystem;
|
|
|
@ -1,103 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright (c) 2013 Arduino LLC. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef __FILEIO_H__
|
|
||||||
#define __FILEIO_H__
|
|
||||||
|
|
||||||
#include <Process.h>
|
|
||||||
|
|
||||||
#define FILE_READ 0
|
|
||||||
#define FILE_WRITE 1
|
|
||||||
#define FILE_APPEND 2
|
|
||||||
|
|
||||||
class File : public Stream {
|
|
||||||
|
|
||||||
public:
|
|
||||||
File(BridgeClass &b = Bridge);
|
|
||||||
File(const char *_filename, uint8_t _mode, BridgeClass &b = Bridge);
|
|
||||||
~File();
|
|
||||||
|
|
||||||
virtual size_t write(uint8_t);
|
|
||||||
virtual size_t write(const uint8_t *buf, size_t size);
|
|
||||||
virtual int read();
|
|
||||||
virtual int peek();
|
|
||||||
virtual int available();
|
|
||||||
virtual void flush();
|
|
||||||
int read(void *buf, uint16_t nbyte);
|
|
||||||
boolean seek(uint32_t pos);
|
|
||||||
uint32_t position();
|
|
||||||
uint32_t size();
|
|
||||||
void close();
|
|
||||||
operator bool();
|
|
||||||
const char * name();
|
|
||||||
boolean isDirectory();
|
|
||||||
File openNextFile(uint8_t mode = FILE_READ);
|
|
||||||
void rewindDirectory(void);
|
|
||||||
|
|
||||||
//using Print::write;
|
|
||||||
|
|
||||||
private:
|
|
||||||
void doBuffer();
|
|
||||||
uint8_t buffered;
|
|
||||||
uint8_t readPos;
|
|
||||||
uint16_t dirPosition;
|
|
||||||
static const int BUFFER_SIZE = 64;
|
|
||||||
uint8_t buffer[BUFFER_SIZE];
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
|
||||||
BridgeClass &bridge;
|
|
||||||
String filename;
|
|
||||||
uint8_t mode;
|
|
||||||
uint8_t handle;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
class FileSystemClass {
|
|
||||||
public:
|
|
||||||
FileSystemClass() : bridge(Bridge) { }
|
|
||||||
FileSystemClass(BridgeClass &_b) : bridge(_b) { }
|
|
||||||
|
|
||||||
boolean begin();
|
|
||||||
|
|
||||||
// Open the specified file/directory with the supplied mode (e.g. read or
|
|
||||||
// write, etc). Returns a File object for interacting with the file.
|
|
||||||
// Note that currently only one file can be open at a time.
|
|
||||||
File open(const char *filename, uint8_t mode = FILE_READ);
|
|
||||||
|
|
||||||
// Methods to determine if the requested file path exists.
|
|
||||||
boolean exists(const char *filepath);
|
|
||||||
|
|
||||||
// Create the requested directory heirarchy--if intermediate directories
|
|
||||||
// do not exist they will be created.
|
|
||||||
boolean mkdir(const char *filepath);
|
|
||||||
|
|
||||||
// Delete the file.
|
|
||||||
boolean remove(const char *filepath);
|
|
||||||
|
|
||||||
boolean rmdir(const char *filepath);
|
|
||||||
|
|
||||||
private:
|
|
||||||
friend class File;
|
|
||||||
|
|
||||||
BridgeClass &bridge;
|
|
||||||
};
|
|
||||||
|
|
||||||
extern FileSystemClass FileSystem;
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,53 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright (c) 2013 Arduino LLC. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "HttpClient.h"
|
|
||||||
|
|
||||||
unsigned int HttpClient::get(String &url) {
|
|
||||||
begin("curl");
|
|
||||||
addParameter(url);
|
|
||||||
return run();
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int HttpClient::get(const char *url) {
|
|
||||||
begin("curl");
|
|
||||||
addParameter(url);
|
|
||||||
return run();
|
|
||||||
}
|
|
||||||
|
|
||||||
void HttpClient::getAsynchronously(String &url) {
|
|
||||||
begin("curl");
|
|
||||||
addParameter(url);
|
|
||||||
runAsynchronously();
|
|
||||||
}
|
|
||||||
|
|
||||||
void HttpClient::getAsynchronously(const char *url) {
|
|
||||||
begin("curl");
|
|
||||||
addParameter(url);
|
|
||||||
runAsynchronously();
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean HttpClient::ready() {
|
|
||||||
return running();
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int HttpClient::getResult() {
|
|
||||||
return exitValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -1,36 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright (c) 2013 Arduino LLC. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef HTTPCLIENT_H_
|
|
||||||
#define HTTPCLIENT_H_
|
|
||||||
|
|
||||||
#include <Process.h>
|
|
||||||
|
|
||||||
class HttpClient : public Process {
|
|
||||||
public:
|
|
||||||
|
|
||||||
unsigned int get(String &url);
|
|
||||||
unsigned int get(const char * url);
|
|
||||||
void getAsynchronously(String &url);
|
|
||||||
void getAsynchronously(const char * url);
|
|
||||||
boolean ready();
|
|
||||||
unsigned int getResult();
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif /* HTTPCLIENT_H_ */
|
|
|
@ -1,56 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright (c) 2013 Arduino LLC. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Mailbox.h>
|
|
||||||
|
|
||||||
unsigned int MailboxClass::readMessage(uint8_t *buff, unsigned int size) {
|
|
||||||
uint8_t tmp[] = { 'm' };
|
|
||||||
return bridge.transfer(tmp, 1, buff, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
void MailboxClass::readMessage(String &str, unsigned int maxLength) {
|
|
||||||
uint8_t tmp[] = { 'm' };
|
|
||||||
// XXX: Is there a better way to create the string?
|
|
||||||
uint8_t buff[maxLength+1];
|
|
||||||
int l = bridge.transfer(tmp, 1, buff, maxLength);
|
|
||||||
buff[l] = 0;
|
|
||||||
str = (const char *)buff;
|
|
||||||
}
|
|
||||||
|
|
||||||
void MailboxClass::writeMessage(const uint8_t *buff, unsigned int size) {
|
|
||||||
uint8_t cmd[] = {'M'};
|
|
||||||
bridge.transfer(cmd, 1, buff, size, NULL, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void MailboxClass::writeMessage(const String& str) {
|
|
||||||
writeMessage((uint8_t*) str.c_str(), str.length());
|
|
||||||
}
|
|
||||||
|
|
||||||
void MailboxClass::writeJSON(const String& str) {
|
|
||||||
uint8_t cmd[] = {'J'};
|
|
||||||
bridge.transfer(cmd, 1, (uint8_t*) str.c_str(), str.length(), NULL, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int MailboxClass::messageAvailable() {
|
|
||||||
uint8_t tmp[] = {'n'};
|
|
||||||
uint8_t res[2];
|
|
||||||
bridge.transfer(tmp, 1, res, 2);
|
|
||||||
return (res[0] << 8) + res[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
MailboxClass Mailbox(Bridge);
|
|
|
@ -1,53 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright (c) 2013 Arduino LLC. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef _MAILBOX_CLASS_H_INCLUDED_
|
|
||||||
#define _MAILBOX_CLASS_H_INCLUDED_
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
|
|
||||||
class MailboxClass {
|
|
||||||
public:
|
|
||||||
MailboxClass(BridgeClass &b = Bridge) : bridge(b) { }
|
|
||||||
|
|
||||||
void begin() { }
|
|
||||||
void end() { }
|
|
||||||
|
|
||||||
// Receive a message and store it inside a buffer
|
|
||||||
unsigned int readMessage(uint8_t *buffer, unsigned int size);
|
|
||||||
// Receive a message and store it inside a String
|
|
||||||
void readMessage(String &str, unsigned int maxLength=128);
|
|
||||||
|
|
||||||
// Send a message
|
|
||||||
void writeMessage(const uint8_t *buffer, unsigned int size);
|
|
||||||
// Send a message
|
|
||||||
void writeMessage(const String& str);
|
|
||||||
// Send a JSON message
|
|
||||||
void writeJSON(const String& str);
|
|
||||||
|
|
||||||
// Return the size of the next available message, 0 if there are
|
|
||||||
// no messages in queue.
|
|
||||||
unsigned int messageAvailable();
|
|
||||||
|
|
||||||
private:
|
|
||||||
BridgeClass &bridge;
|
|
||||||
};
|
|
||||||
|
|
||||||
extern MailboxClass Mailbox;
|
|
||||||
|
|
||||||
#endif // _MAILBOX_CLASS_H_INCLUDED_
|
|
|
@ -1,142 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright (c) 2013 Arduino LLC. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Process.h>
|
|
||||||
|
|
||||||
Process::~Process() {
|
|
||||||
close();
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Process::write(uint8_t c) {
|
|
||||||
uint8_t cmd[] = {'I', handle, c};
|
|
||||||
bridge.transfer(cmd, 3);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Process::flush() {
|
|
||||||
}
|
|
||||||
|
|
||||||
int Process::available() {
|
|
||||||
// Look if there is new data available
|
|
||||||
doBuffer();
|
|
||||||
return buffered;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Process::read() {
|
|
||||||
doBuffer();
|
|
||||||
if (buffered == 0)
|
|
||||||
return -1; // no chars available
|
|
||||||
else {
|
|
||||||
buffered--;
|
|
||||||
return buffer[readPos++];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int Process::peek() {
|
|
||||||
doBuffer();
|
|
||||||
if (buffered == 0)
|
|
||||||
return -1; // no chars available
|
|
||||||
else
|
|
||||||
return buffer[readPos];
|
|
||||||
}
|
|
||||||
|
|
||||||
void Process::doBuffer() {
|
|
||||||
// If there are already char in buffer exit
|
|
||||||
if (buffered > 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Try to buffer up to 32 characters
|
|
||||||
readPos = 0;
|
|
||||||
uint8_t cmd[] = {'O', handle, sizeof(buffer)};
|
|
||||||
buffered = bridge.transfer(cmd, 3, buffer, sizeof(buffer));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Process::begin(const String &command) {
|
|
||||||
close();
|
|
||||||
cmdline = new String(command);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Process::addParameter(const String ¶m) {
|
|
||||||
*cmdline += "\xFE";
|
|
||||||
*cmdline += param;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Process::runAsynchronously() {
|
|
||||||
uint8_t cmd[] = {'R'};
|
|
||||||
uint8_t res[2];
|
|
||||||
bridge.transfer(cmd, 1, (uint8_t*)cmdline->c_str(), cmdline->length(), res, 2);
|
|
||||||
handle = res[1];
|
|
||||||
|
|
||||||
delete cmdline;
|
|
||||||
cmdline = NULL;
|
|
||||||
|
|
||||||
if (res[0]==0) // res[0] contains error code
|
|
||||||
started = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean Process::running() {
|
|
||||||
uint8_t cmd[] = {'r', handle};
|
|
||||||
uint8_t res[1];
|
|
||||||
bridge.transfer(cmd, 2, res, 1);
|
|
||||||
return (res[0] == 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int Process::exitValue() {
|
|
||||||
uint8_t cmd[] = {'W', handle};
|
|
||||||
uint8_t res[2];
|
|
||||||
bridge.transfer(cmd, 2, res, 2);
|
|
||||||
return (res[0] << 8) + res[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int Process::run() {
|
|
||||||
runAsynchronously();
|
|
||||||
while (running())
|
|
||||||
delay(100);
|
|
||||||
return exitValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Process::close() {
|
|
||||||
if (started) {
|
|
||||||
uint8_t cmd[] = {'w', handle};
|
|
||||||
bridge.transfer(cmd, 2);
|
|
||||||
}
|
|
||||||
started = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int Process::runShellCommand(const String &command) {
|
|
||||||
runShellCommandAsynchronously(command);
|
|
||||||
while (running())
|
|
||||||
delay(100);
|
|
||||||
return exitValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Process::runShellCommandAsynchronously(const String &command) {
|
|
||||||
begin("/bin/ash");
|
|
||||||
addParameter("-c");
|
|
||||||
addParameter(command);
|
|
||||||
runAsynchronously();
|
|
||||||
}
|
|
||||||
|
|
||||||
// This method is currently unused
|
|
||||||
//static unsigned int __commandOutputAvailable(uint8_t handle) {
|
|
||||||
// uint8_t cmd[] = {'o', handle};
|
|
||||||
// uint8_t res[1];
|
|
||||||
// Bridge.transfer(cmd, 2, res, 1);
|
|
||||||
// return res[0];
|
|
||||||
//}
|
|
||||||
|
|
|
@ -1,69 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright (c) 2013 Arduino LLC. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef PROCESS_H_
|
|
||||||
#define PROCESS_H_
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
|
|
||||||
class Process : public Stream {
|
|
||||||
public:
|
|
||||||
// Constructor with a user provided BridgeClass instance
|
|
||||||
Process(BridgeClass &_b = Bridge) :
|
|
||||||
bridge(_b), started(false), buffered(0), readPos(0) { }
|
|
||||||
~Process();
|
|
||||||
|
|
||||||
void begin(const String &command);
|
|
||||||
void addParameter(const String ¶m);
|
|
||||||
unsigned int run();
|
|
||||||
void runAsynchronously();
|
|
||||||
boolean running();
|
|
||||||
unsigned int exitValue();
|
|
||||||
void close();
|
|
||||||
|
|
||||||
unsigned int runShellCommand(const String &command);
|
|
||||||
void runShellCommandAsynchronously(const String &command);
|
|
||||||
|
|
||||||
operator bool () { return started; }
|
|
||||||
|
|
||||||
// Stream methods
|
|
||||||
// (read from process stdout)
|
|
||||||
int available();
|
|
||||||
int read();
|
|
||||||
int peek();
|
|
||||||
// (write to process stdin)
|
|
||||||
size_t write(uint8_t);
|
|
||||||
void flush();
|
|
||||||
// TODO: add optimized function for block write
|
|
||||||
|
|
||||||
private:
|
|
||||||
BridgeClass &bridge;
|
|
||||||
unsigned int handle;
|
|
||||||
String *cmdline;
|
|
||||||
boolean started;
|
|
||||||
|
|
||||||
private:
|
|
||||||
void doBuffer();
|
|
||||||
uint8_t buffered;
|
|
||||||
uint8_t readPos;
|
|
||||||
static const int BUFFER_SIZE = 64;
|
|
||||||
uint8_t buffer[BUFFER_SIZE];
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,167 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright (c) 2013 Arduino LLC. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <YunClient.h>
|
|
||||||
|
|
||||||
YunClient::YunClient(int _h, BridgeClass &_b) :
|
|
||||||
bridge(_b), handle(_h), opened(true), buffered(0) {
|
|
||||||
}
|
|
||||||
|
|
||||||
YunClient::YunClient(BridgeClass &_b) :
|
|
||||||
bridge(_b), handle(0), opened(false), buffered(0) {
|
|
||||||
}
|
|
||||||
|
|
||||||
YunClient::~YunClient() {
|
|
||||||
}
|
|
||||||
|
|
||||||
YunClient& YunClient::operator=(const YunClient &_x) {
|
|
||||||
opened = _x.opened;
|
|
||||||
handle = _x.handle;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
void YunClient::stop() {
|
|
||||||
if (opened) {
|
|
||||||
uint8_t cmd[] = {'j', handle};
|
|
||||||
bridge.transfer(cmd, 2);
|
|
||||||
}
|
|
||||||
opened = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void YunClient::doBuffer() {
|
|
||||||
// If there are already char in buffer exit
|
|
||||||
if (buffered > 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Try to buffer up to 32 characters
|
|
||||||
readPos = 0;
|
|
||||||
uint8_t cmd[] = {'K', handle, sizeof(buffer)};
|
|
||||||
buffered = bridge.transfer(cmd, 3, buffer, sizeof(buffer));
|
|
||||||
}
|
|
||||||
|
|
||||||
int YunClient::available() {
|
|
||||||
// Look if there is new data available
|
|
||||||
doBuffer();
|
|
||||||
return buffered;
|
|
||||||
}
|
|
||||||
|
|
||||||
int YunClient::read() {
|
|
||||||
doBuffer();
|
|
||||||
if (buffered == 0)
|
|
||||||
return -1; // no chars available
|
|
||||||
else {
|
|
||||||
buffered--;
|
|
||||||
return buffer[readPos++];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int YunClient::read(uint8_t *buff, size_t size) {
|
|
||||||
int readed = 0;
|
|
||||||
do {
|
|
||||||
if (buffered == 0) {
|
|
||||||
doBuffer();
|
|
||||||
if (buffered == 0)
|
|
||||||
return readed;
|
|
||||||
}
|
|
||||||
buff[readed++] = buffer[readPos++];
|
|
||||||
buffered--;
|
|
||||||
} while (readed < size);
|
|
||||||
return readed;
|
|
||||||
}
|
|
||||||
|
|
||||||
int YunClient::peek() {
|
|
||||||
doBuffer();
|
|
||||||
if (buffered == 0)
|
|
||||||
return -1; // no chars available
|
|
||||||
else
|
|
||||||
return buffer[readPos];
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t YunClient::write(uint8_t c) {
|
|
||||||
if (!opened)
|
|
||||||
return 0;
|
|
||||||
uint8_t cmd[] = {'l', handle, c};
|
|
||||||
bridge.transfer(cmd, 3);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t YunClient::write(const uint8_t *buf, size_t size) {
|
|
||||||
if (!opened)
|
|
||||||
return 0;
|
|
||||||
uint8_t cmd[] = {'l', handle};
|
|
||||||
bridge.transfer(cmd, 2, buf, size, NULL, 0);
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
void YunClient::flush() {
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t YunClient::connected() {
|
|
||||||
if (!opened)
|
|
||||||
return false;
|
|
||||||
uint8_t cmd[] = {'L', handle};
|
|
||||||
uint8_t res[1];
|
|
||||||
bridge.transfer(cmd, 2, res, 1);
|
|
||||||
return (res[0] == 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
int YunClient::connect(IPAddress ip, uint16_t port) {
|
|
||||||
String address;
|
|
||||||
address.reserve(18);
|
|
||||||
address += ip[0];
|
|
||||||
address += '.';
|
|
||||||
address += ip[1];
|
|
||||||
address += '.';
|
|
||||||
address += ip[2];
|
|
||||||
address += '.';
|
|
||||||
address += ip[3];
|
|
||||||
return connect(address.c_str(), port);
|
|
||||||
}
|
|
||||||
|
|
||||||
int YunClient::connect(const char *host, uint16_t port) {
|
|
||||||
uint8_t tmp[] = {
|
|
||||||
'C',
|
|
||||||
(port >> 8) & 0xFF,
|
|
||||||
port & 0xFF
|
|
||||||
};
|
|
||||||
uint8_t res[1];
|
|
||||||
int l = bridge.transfer(tmp, 3, (const uint8_t *)host, strlen(host), res, 1);
|
|
||||||
if (l==0)
|
|
||||||
return 0;
|
|
||||||
handle = res[0];
|
|
||||||
|
|
||||||
// wait for connection
|
|
||||||
uint8_t tmp2[] = { 'c', handle };
|
|
||||||
uint8_t res2[1];
|
|
||||||
while (true) {
|
|
||||||
bridge.transfer(tmp2, 2, res2, 1);
|
|
||||||
if (res2[0] == 0)
|
|
||||||
break;
|
|
||||||
delay(1);
|
|
||||||
}
|
|
||||||
opened = true;
|
|
||||||
|
|
||||||
// check for successful connection
|
|
||||||
if (connected())
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
opened = false;
|
|
||||||
handle = 0;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,68 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright (c) 2013 Arduino LLC. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef _YUN_CLIENT_H_
|
|
||||||
#define _YUN_CLIENT_H_
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
#include <Client.h>
|
|
||||||
|
|
||||||
class YunClient : public Client {
|
|
||||||
public:
|
|
||||||
// Constructor with a user provided BridgeClass instance
|
|
||||||
YunClient(int _h, BridgeClass &_b = Bridge);
|
|
||||||
YunClient(BridgeClass &_b = Bridge);
|
|
||||||
~YunClient();
|
|
||||||
|
|
||||||
// Stream methods
|
|
||||||
// (read message)
|
|
||||||
virtual int available();
|
|
||||||
virtual int read();
|
|
||||||
virtual int read(uint8_t *buf, size_t size);
|
|
||||||
virtual int peek();
|
|
||||||
// (write response)
|
|
||||||
virtual size_t write(uint8_t);
|
|
||||||
virtual size_t write(const uint8_t *buf, size_t size);
|
|
||||||
virtual void flush();
|
|
||||||
// TODO: add optimized function for block write
|
|
||||||
|
|
||||||
virtual operator bool () { return opened; }
|
|
||||||
|
|
||||||
YunClient& operator=(const YunClient &_x);
|
|
||||||
|
|
||||||
virtual void stop();
|
|
||||||
virtual uint8_t connected();
|
|
||||||
|
|
||||||
virtual int connect(IPAddress ip, uint16_t port);
|
|
||||||
virtual int connect(const char *host, uint16_t port);
|
|
||||||
|
|
||||||
private:
|
|
||||||
BridgeClass &bridge;
|
|
||||||
unsigned int handle;
|
|
||||||
boolean opened;
|
|
||||||
|
|
||||||
private:
|
|
||||||
void doBuffer();
|
|
||||||
uint8_t buffered;
|
|
||||||
uint8_t readPos;
|
|
||||||
static const int BUFFER_SIZE = 64;
|
|
||||||
uint8_t buffer[BUFFER_SIZE];
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // _YUN_CLIENT_H_
|
|
|
@ -1,54 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright (c) 2013 Arduino LLC. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <YunServer.h>
|
|
||||||
#include <YunClient.h>
|
|
||||||
|
|
||||||
YunServer::YunServer(uint16_t _p, BridgeClass &_b) :
|
|
||||||
bridge(_b), port(_p), listening(false), useLocalhost(false) {
|
|
||||||
}
|
|
||||||
|
|
||||||
void YunServer::begin() {
|
|
||||||
uint8_t tmp[] = {
|
|
||||||
'N',
|
|
||||||
(port >> 8) & 0xFF,
|
|
||||||
port & 0xFF
|
|
||||||
};
|
|
||||||
uint8_t res[1];
|
|
||||||
String address = F("127.0.0.1");
|
|
||||||
if (!useLocalhost)
|
|
||||||
address = F("0.0.0.0");
|
|
||||||
bridge.transfer(tmp, 3, (const uint8_t *)address.c_str(), address.length(), res, 1);
|
|
||||||
listening = (res[0] == 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
YunClient YunServer::accept() {
|
|
||||||
uint8_t cmd[] = {'k'};
|
|
||||||
uint8_t res[1];
|
|
||||||
unsigned int l = bridge.transfer(cmd, 1, res, 1);
|
|
||||||
if (l==0)
|
|
||||||
return YunClient();
|
|
||||||
return YunClient(res[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t YunServer::write(uint8_t c) {
|
|
||||||
uint8_t cmd[] = { 'b', c };
|
|
||||||
bridge.transfer(cmd, 2);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,47 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright (c) 2013 Arduino LLC. All right reserved.
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License as published by the Free Software Foundation; either
|
|
||||||
version 2.1 of the License, or (at your option) any later version.
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef _YUN_SERVER_H_
|
|
||||||
#define _YUN_SERVER_H_
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
#include <Server.h>
|
|
||||||
|
|
||||||
class YunClient;
|
|
||||||
|
|
||||||
class YunServer : public Server {
|
|
||||||
public:
|
|
||||||
// Constructor with a user provided BridgeClass instance
|
|
||||||
YunServer(uint16_t port = 5555, BridgeClass &_b = Bridge);
|
|
||||||
|
|
||||||
void begin();
|
|
||||||
YunClient accept();
|
|
||||||
|
|
||||||
virtual size_t write(uint8_t c);
|
|
||||||
|
|
||||||
void listenOnLocalhost() { useLocalhost = true; }
|
|
||||||
void noListenOnLocalhost() { useLocalhost = false; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
uint16_t port;
|
|
||||||
bool listening;
|
|
||||||
bool useLocalhost;
|
|
||||||
BridgeClass &bridge;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // _YUN_SERVER_H_
|
|
|
@ -1,175 +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>
|
|
||||||
#include <YunClient.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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -1,94 +0,0 @@
|
||||||
/*
|
|
||||||
ASCII table
|
|
||||||
|
|
||||||
Prints out byte values in all possible formats:
|
|
||||||
* as raw binary values
|
|
||||||
* as ASCII-encoded decimal, hex, octal, and binary values
|
|
||||||
|
|
||||||
For more on ASCII, see http://www.asciitable.com and http://en.wikipedia.org/wiki/ASCII
|
|
||||||
|
|
||||||
The circuit: No external hardware needed.
|
|
||||||
|
|
||||||
created 2006
|
|
||||||
by Nicholas Zambetti
|
|
||||||
modified 9 Apr 2012
|
|
||||||
by Tom Igoe
|
|
||||||
modified 22 May 2013
|
|
||||||
by Cristian Maglie
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
<http://www.zambetti.com>
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Console.h>
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
//Initialize Console and wait for port to open:
|
|
||||||
Bridge.begin();
|
|
||||||
Console.begin();
|
|
||||||
|
|
||||||
// Uncomment the following line to enable buffering:
|
|
||||||
// - better transmission speed and efficiency
|
|
||||||
// - needs to call Console.flush() to ensure that all
|
|
||||||
// transmitted data is sent
|
|
||||||
|
|
||||||
//Console.buffer(64);
|
|
||||||
|
|
||||||
while (!Console) {
|
|
||||||
; // wait for Console port to connect.
|
|
||||||
}
|
|
||||||
|
|
||||||
// prints title with ending line break
|
|
||||||
Console.println("ASCII Table ~ Character Map");
|
|
||||||
}
|
|
||||||
|
|
||||||
// first visible ASCIIcharacter '!' is number 33:
|
|
||||||
int thisByte = 33;
|
|
||||||
// you can also write ASCII characters in single quotes.
|
|
||||||
// for example. '!' is the same as 33, so you could also use this:
|
|
||||||
//int thisByte = '!';
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// prints value unaltered, i.e. the raw binary version of the
|
|
||||||
// byte. The Console monitor interprets all bytes as
|
|
||||||
// ASCII, so 33, the first number, will show up as '!'
|
|
||||||
Console.write(thisByte);
|
|
||||||
|
|
||||||
Console.print(", dec: ");
|
|
||||||
// prints value as string as an ASCII-encoded decimal (base 10).
|
|
||||||
// Decimal is the default format for Console.print() and Console.println(),
|
|
||||||
// so no modifier is needed:
|
|
||||||
Console.print(thisByte);
|
|
||||||
// But you can declare the modifier for decimal if you want to.
|
|
||||||
//this also works if you uncomment it:
|
|
||||||
|
|
||||||
// Console.print(thisByte, DEC);
|
|
||||||
|
|
||||||
Console.print(", hex: ");
|
|
||||||
// prints value as string in hexadecimal (base 16):
|
|
||||||
Console.print(thisByte, HEX);
|
|
||||||
|
|
||||||
Console.print(", oct: ");
|
|
||||||
// prints value as string in octal (base 8);
|
|
||||||
Console.print(thisByte, OCT);
|
|
||||||
|
|
||||||
Console.print(", bin: ");
|
|
||||||
// prints value as string in binary (base 2)
|
|
||||||
// also prints ending line break:
|
|
||||||
Console.println(thisByte, BIN);
|
|
||||||
|
|
||||||
// if printed last visible character '~' or 126, stop:
|
|
||||||
if(thisByte == 126) { // you could also use if (thisByte == '~') {
|
|
||||||
// ensure the latest bit of data is sent
|
|
||||||
Console.flush();
|
|
||||||
|
|
||||||
// This loop loops forever and does nothing
|
|
||||||
while(true) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// go on to the next character
|
|
||||||
thisByte++;
|
|
||||||
}
|
|
|
@ -1,62 +0,0 @@
|
||||||
/*
|
|
||||||
Console Pixel
|
|
||||||
|
|
||||||
An example of using the Arduino board to receive data from the
|
|
||||||
Console on the Arduino Yun. In this case, the Arduino boards turns on an LED when
|
|
||||||
it receives the character 'H', and turns off the LED when it
|
|
||||||
receives the character 'L'.
|
|
||||||
|
|
||||||
To see the Console, pick your Yún's name and IP address in the Port menu
|
|
||||||
then open the Port Monitor. You can also see it by opening a terminal window
|
|
||||||
and typing
|
|
||||||
ssh root@ yourYunsName.local 'telnet localhost 6571'
|
|
||||||
then pressing enter. When prompted for the password, enter it.
|
|
||||||
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
* LED connected from digital pin 13 to ground
|
|
||||||
|
|
||||||
created 2006
|
|
||||||
by David A. Mellis
|
|
||||||
modified 25 Jun 2013
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Console.h>
|
|
||||||
|
|
||||||
const int ledPin = 13; // the pin that the LED is attached to
|
|
||||||
char incomingByte; // a variable to read incoming Console data into
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Bridge.begin(); // Initialize Bridge
|
|
||||||
Console.begin(); // Initialize Console
|
|
||||||
|
|
||||||
// Wait for the Console port to connect
|
|
||||||
while(!Console);
|
|
||||||
|
|
||||||
Console.println("type H or L to turn pin 13 on or off");
|
|
||||||
|
|
||||||
// initialize the LED pin as an output:
|
|
||||||
pinMode(ledPin, OUTPUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// see if there's incoming Console data:
|
|
||||||
if (Console.available() > 0) {
|
|
||||||
// read the oldest byte in the Console buffer:
|
|
||||||
incomingByte = Console.read();
|
|
||||||
Console.println(incomingByte);
|
|
||||||
// if it's a capital H (ASCII 72), turn on the LED:
|
|
||||||
if (incomingByte == 'H') {
|
|
||||||
digitalWrite(ledPin, HIGH);
|
|
||||||
}
|
|
||||||
// if it's an L (ASCII 76) turn off the LED:
|
|
||||||
if (incomingByte == 'L') {
|
|
||||||
digitalWrite(ledPin, LOW);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,56 +0,0 @@
|
||||||
/*
|
|
||||||
Console Read example
|
|
||||||
|
|
||||||
Read data coming from bridge using the Console.read() function
|
|
||||||
and store it in a string.
|
|
||||||
|
|
||||||
To see the Console, pick your Yún's name and IP address in the Port menu
|
|
||||||
then open the Port Monitor. You can also see it by opening a terminal window
|
|
||||||
and typing:
|
|
||||||
ssh root@ yourYunsName.local 'telnet localhost 6571'
|
|
||||||
then pressing enter. When prompted for the password, enter it.
|
|
||||||
|
|
||||||
created 13 Jun 2013
|
|
||||||
by Angelo Scialabba
|
|
||||||
modified 16 June 2013
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Console.h>
|
|
||||||
|
|
||||||
String name;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// Initialize Console and wait for port to open:
|
|
||||||
Bridge.begin();
|
|
||||||
Console.begin();
|
|
||||||
|
|
||||||
// Wait for Console port to connect
|
|
||||||
while (!Console);
|
|
||||||
|
|
||||||
Console.println("Hi, what's your name?");
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
if (Console.available() > 0) {
|
|
||||||
char c = Console.read(); // read the next char received
|
|
||||||
// look for the newline character, this is the last character in the string
|
|
||||||
if (c == '\n') {
|
|
||||||
//print text with the name received
|
|
||||||
Console.print("Hi ");
|
|
||||||
Console.print(name);
|
|
||||||
Console.println("! Nice to meet you!");
|
|
||||||
Console.println();
|
|
||||||
// Ask again for name and clear the old name
|
|
||||||
Console.println("Hi, what's your name?");
|
|
||||||
name = ""; // clear the name string
|
|
||||||
}
|
|
||||||
else { // if the buffer is empty Cosole.read() returns -1
|
|
||||||
name += c; // append the read char from Console to the name string
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -1,99 +0,0 @@
|
||||||
/*
|
|
||||||
SD card datalogger
|
|
||||||
|
|
||||||
This example shows how to log data from three analog sensors
|
|
||||||
to an SD card mounted on the Arduino Yún using the Bridge library.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
* analog sensors on analog pins 0, 1 and 2
|
|
||||||
* SD card attached to SD card slot of the Arduino Yún
|
|
||||||
|
|
||||||
Prepare your SD card creating an empty folder in the SD root
|
|
||||||
named "arduino". This will ensure that the Yún will create a link
|
|
||||||
to the SD to the "/mnt/sd" path.
|
|
||||||
|
|
||||||
You can remove the SD card while the Linux and the
|
|
||||||
sketch are running but be careful not to remove it while
|
|
||||||
the system is writing to it.
|
|
||||||
|
|
||||||
created 24 Nov 2010
|
|
||||||
modified 9 Apr 2012
|
|
||||||
by Tom Igoe
|
|
||||||
adapted to the Yún Bridge library 20 Jun 2013
|
|
||||||
by Federico Vanzati
|
|
||||||
modified 21 Jun 2013
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <FileIO.h>
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// Initialize the Bridge and the Serial
|
|
||||||
Bridge.begin();
|
|
||||||
Serial.begin(9600);
|
|
||||||
FileSystem.begin();
|
|
||||||
|
|
||||||
while(!Serial); // wait for Serial port to connect.
|
|
||||||
Serial.println("Filesystem datalogger\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void loop () {
|
|
||||||
// make a string that start with a timestamp for assembling the data to log:
|
|
||||||
String dataString;
|
|
||||||
dataString += getTimeStamp();
|
|
||||||
dataString += " = ";
|
|
||||||
|
|
||||||
// read three sensors and append to the string:
|
|
||||||
for (int analogPin = 0; analogPin < 3; analogPin++) {
|
|
||||||
int sensor = analogRead(analogPin);
|
|
||||||
dataString += String(sensor);
|
|
||||||
if (analogPin < 2) {
|
|
||||||
dataString += ","; // separate the values with a comma
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// open the file. note that only one file can be open at a time,
|
|
||||||
// so you have to close this one before opening another.
|
|
||||||
// The FileSystem card is mounted at the following "/mnt/FileSystema1"
|
|
||||||
File dataFile = FileSystem.open("/mnt/sd/datalog.txt", FILE_APPEND);
|
|
||||||
|
|
||||||
// if the file is available, write to it:
|
|
||||||
if (dataFile) {
|
|
||||||
dataFile.println(dataString);
|
|
||||||
dataFile.close();
|
|
||||||
// print to the serial port too:
|
|
||||||
Serial.println(dataString);
|
|
||||||
}
|
|
||||||
// if the file isn't open, pop up an error:
|
|
||||||
else {
|
|
||||||
Serial.println("error opening datalog.txt");
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(15000);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// This function return a string with the time stamp
|
|
||||||
String getTimeStamp() {
|
|
||||||
String result;
|
|
||||||
Process time;
|
|
||||||
// date is a command line utility to get the date and the time
|
|
||||||
// in different formats depending on the additional parameter
|
|
||||||
time.begin("date");
|
|
||||||
time.addParameter("+%D-%T"); // parameters: D for the complete date mm/dd/yy
|
|
||||||
// T for the time hh:mm:ss
|
|
||||||
time.run(); // run the command
|
|
||||||
|
|
||||||
// read the output of the command
|
|
||||||
while(time.available()>0) {
|
|
||||||
char c = time.read();
|
|
||||||
if(c != '\n')
|
|
||||||
result += c;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
|
@ -1,82 +0,0 @@
|
||||||
/*
|
|
||||||
Write to file using FileIO classes.
|
|
||||||
|
|
||||||
This sketch demonstrate how to write file into the Yún filesystem.
|
|
||||||
A shell script file is created in /tmp, and it is executed afterwards.
|
|
||||||
|
|
||||||
created 7 June 2010
|
|
||||||
by Cristian Maglie
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <FileIO.h>
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// Setup Bridge (needed every time we communicate with the Arduino Yún)
|
|
||||||
Bridge.begin();
|
|
||||||
// Initialize the Serial
|
|
||||||
Serial.begin(9600);
|
|
||||||
|
|
||||||
while(!Serial); // wait for Serial port to connect.
|
|
||||||
Serial.println("File Write Script example\n\n");
|
|
||||||
|
|
||||||
// Setup File IO
|
|
||||||
FileSystem.begin();
|
|
||||||
|
|
||||||
// Upload script used to gain network statistics
|
|
||||||
uploadScript();
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// Run stats script every 5 secs.
|
|
||||||
runScript();
|
|
||||||
delay(5000);
|
|
||||||
}
|
|
||||||
|
|
||||||
// this function creates a file into the linux processor that contains a shell script
|
|
||||||
// to check the network traffic of the WiFi interface
|
|
||||||
void uploadScript() {
|
|
||||||
// Write our shell script in /tmp
|
|
||||||
// Using /tmp stores the script in RAM this way we can preserve
|
|
||||||
// the limited amount of FLASH erase/write cycles
|
|
||||||
File script = FileSystem.open("/tmp/wlan-stats.sh", FILE_WRITE);
|
|
||||||
// Shell script header
|
|
||||||
script.print("#!/bin/sh\n");
|
|
||||||
// shell commands:
|
|
||||||
// ifconfig: is a command line utility for controlling the network interfaces.
|
|
||||||
// wlan0 is the interface we want to query
|
|
||||||
// grep: search inside the output of the ifconfig command the "RX bytes" keyword
|
|
||||||
// and extract the line that contains it
|
|
||||||
script.print("ifconfig wlan0 | grep 'RX bytes'\n");
|
|
||||||
script.close(); // close the file
|
|
||||||
|
|
||||||
// Make the script executable
|
|
||||||
Process chmod;
|
|
||||||
chmod.begin("chmod"); // chmod: change mode
|
|
||||||
chmod.addParameter("+x"); // x stays for executable
|
|
||||||
chmod.addParameter("/tmp/wlan-stats.sh"); // path to the file to make it executable
|
|
||||||
chmod.run();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// this function run the script and read the output data
|
|
||||||
void runScript() {
|
|
||||||
// Run the script and show results on the Serial
|
|
||||||
Process myscript;
|
|
||||||
myscript.begin("/tmp/wlan-stats.sh");
|
|
||||||
myscript.run();
|
|
||||||
|
|
||||||
String output = "";
|
|
||||||
|
|
||||||
// read the output of the script
|
|
||||||
while (myscript.available()) {
|
|
||||||
output += (char)myscript.read();
|
|
||||||
}
|
|
||||||
// remove the blank spaces at the beginning and the ending of the string
|
|
||||||
output.trim();
|
|
||||||
Serial.println(output);
|
|
||||||
Serial.flush();
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,26 +0,0 @@
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
#include <HttpClient.h>
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
pinMode(13, OUTPUT);
|
|
||||||
digitalWrite(13, LOW);
|
|
||||||
Bridge.begin();
|
|
||||||
Serial.begin(9600);
|
|
||||||
while(!Serial);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
HttpClient client;
|
|
||||||
client.get("http://arduino.cc/asciilogo.txt");
|
|
||||||
|
|
||||||
while (client.available()) {
|
|
||||||
char c = client.read();
|
|
||||||
Serial.print(c);
|
|
||||||
}
|
|
||||||
Serial.flush();
|
|
||||||
|
|
||||||
delay(5000);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -1,70 +0,0 @@
|
||||||
/*
|
|
||||||
Running process using Process class.
|
|
||||||
|
|
||||||
This sketch demonstrate how to run linux processes
|
|
||||||
using an Arduino Yún.
|
|
||||||
|
|
||||||
created 5 Jun 2013
|
|
||||||
by Cristian Maglie
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Process.h>
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// Initialize Bridge
|
|
||||||
Bridge.begin();
|
|
||||||
|
|
||||||
// Initialize Serial
|
|
||||||
Serial.begin(9600);
|
|
||||||
|
|
||||||
// Wait until a Serial Monitor is connected.
|
|
||||||
while (!Serial);
|
|
||||||
|
|
||||||
// run various example processes
|
|
||||||
runCurl();
|
|
||||||
runCpuInfo();
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// Do nothing here.
|
|
||||||
}
|
|
||||||
|
|
||||||
void runCurl() {
|
|
||||||
// Launch "curl" command and get Arduino ascii art logo from the network
|
|
||||||
// curl is command line program for transferring data using different internet protocols
|
|
||||||
Process p; // Create a process and call it "p"
|
|
||||||
p.begin("curl"); // Process that launch the "curl" command
|
|
||||||
p.addParameter("http://arduino.cc/asciilogo.txt"); // Add the URL parameter to "curl"
|
|
||||||
p.run(); // Run the process and wait for its termination
|
|
||||||
|
|
||||||
// Print arduino logo over the Serial
|
|
||||||
// A process output can be read with the stream methods
|
|
||||||
while (p.available()>0) {
|
|
||||||
char c = p.read();
|
|
||||||
Serial.print(c);
|
|
||||||
}
|
|
||||||
// Ensure the last bit of data is sent.
|
|
||||||
Serial.flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
void runCpuInfo() {
|
|
||||||
// Launch "cat /proc/cpuinfo" command (shows info on Atheros CPU)
|
|
||||||
// cat is a command line utility that shows the content of a file
|
|
||||||
Process p; // Create a process and call it "p"
|
|
||||||
p.begin("cat"); // Process that launch the "cat" command
|
|
||||||
p.addParameter("/proc/cpuinfo"); // Add the cpuifo file path as parameter to cut
|
|
||||||
p.run(); // Run the process and wait for its termination
|
|
||||||
|
|
||||||
// Print command output on the Serial.
|
|
||||||
// A process output can be read with the stream methods
|
|
||||||
while (p.available()>0) {
|
|
||||||
char c = p.read();
|
|
||||||
Serial.print(c);
|
|
||||||
}
|
|
||||||
// Ensure the last bit of data is sent.
|
|
||||||
Serial.flush();
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,53 +0,0 @@
|
||||||
/*
|
|
||||||
Running shell commands using Process class.
|
|
||||||
|
|
||||||
This sketch demonstrate how to run linux shell commands
|
|
||||||
using an Arduino Yún. It runs the wifiCheck script on the linino side
|
|
||||||
of the Yun, then uses grep to get just the signal strength line.
|
|
||||||
Then it uses parseInt() to read the wifi signal strength as an integer,
|
|
||||||
and finally uses that number to fade an LED using analogWrite().
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
* Arduino Yun with LED connected to pin 9
|
|
||||||
|
|
||||||
created 12 Jun 2013
|
|
||||||
by Cristian Maglie
|
|
||||||
modified 25 June 2013
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Process.h>
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Bridge.begin(); // Initialize the Bridge
|
|
||||||
Serial.begin(9600); // Initialize the Serial
|
|
||||||
|
|
||||||
// Wait until a Serial Monitor is connected.
|
|
||||||
while(!Serial);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
Process p;
|
|
||||||
// This command line runs the WifiStatus script, (/usr/bin/pretty-wifi-info.lua), then
|
|
||||||
// sends the result to the grep command to look for a line containing the word
|
|
||||||
// "Signal:" the result is passed to this sketch:
|
|
||||||
p.runShellCommand("/usr/bin/pretty-wifi-info.lua | grep Signal");
|
|
||||||
|
|
||||||
// do nothing until the process finishes, so you get the whole output:
|
|
||||||
while(p.running());
|
|
||||||
|
|
||||||
// Read command output. runShellCommand() should have passed "Signal: xx&":
|
|
||||||
while (p.available()) {
|
|
||||||
int result = p.parseInt(); // look for an integer
|
|
||||||
int signal = map(result, 0, 100, 0, 255); // map result from 0-100 range to 0-255
|
|
||||||
analogWrite(9, signal); // set the brightness of LED on pin 9
|
|
||||||
Serial.println(result); // print the number as well
|
|
||||||
}
|
|
||||||
delay(5000); // wait 5 seconds before you do it again
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,130 +0,0 @@
|
||||||
/*
|
|
||||||
Input Output
|
|
||||||
|
|
||||||
Demonstrates how to create a sketch that sends and receives all standard
|
|
||||||
spacebrew data types, and a custom data type. Every time data is
|
|
||||||
received it is output to the Serial monitor.
|
|
||||||
|
|
||||||
Make sure that your Yun is connected to the internet for this example
|
|
||||||
to function properly.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
- No circuit required
|
|
||||||
|
|
||||||
created 2013
|
|
||||||
by Julio Terra
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
More information about Spacebrew is available at:
|
|
||||||
http://spacebrew.cc/
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
#include <SpacebrewYun.h>
|
|
||||||
|
|
||||||
// create a variable of type SpacebrewYun and initialize it with the constructor
|
|
||||||
SpacebrewYun sb = SpacebrewYun("aYun", "Arduino Yun spacebrew test");
|
|
||||||
|
|
||||||
// create variables to manage interval between each time we send a string
|
|
||||||
long last = 0;
|
|
||||||
int interval = 2000;
|
|
||||||
|
|
||||||
int counter = 0;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
|
|
||||||
// start the serial port
|
|
||||||
Serial.begin(57600);
|
|
||||||
|
|
||||||
// for debugging, wait until a serial console is connected
|
|
||||||
delay(4000);
|
|
||||||
while (!Serial) { ; }
|
|
||||||
|
|
||||||
// start-up the bridge
|
|
||||||
Bridge.begin();
|
|
||||||
|
|
||||||
// configure the spacebrew object to print status messages to serial
|
|
||||||
sb.verbose(true);
|
|
||||||
|
|
||||||
// configure the spacebrew publisher and subscriber
|
|
||||||
sb.addPublish("string test", "string");
|
|
||||||
sb.addPublish("range test", "range");
|
|
||||||
sb.addPublish("boolean test", "boolean");
|
|
||||||
sb.addPublish("custom test", "crazy");
|
|
||||||
sb.addSubscribe("string test", "string");
|
|
||||||
sb.addSubscribe("range test", "range");
|
|
||||||
sb.addSubscribe("boolean test", "boolean");
|
|
||||||
sb.addSubscribe("custom test", "crazy");
|
|
||||||
|
|
||||||
// register the string message handler method
|
|
||||||
sb.onRangeMessage(handleRange);
|
|
||||||
sb.onStringMessage(handleString);
|
|
||||||
sb.onBooleanMessage(handleBoolean);
|
|
||||||
sb.onCustomMessage(handleCustom);
|
|
||||||
|
|
||||||
// connect to cloud spacebrew server at "sandbox.spacebrew.cc"
|
|
||||||
sb.connect("sandbox.spacebrew.cc");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// monitor spacebrew connection for new data
|
|
||||||
sb.monitor();
|
|
||||||
|
|
||||||
// connected to spacebrew then send a string every 2 seconds
|
|
||||||
if ( sb.connected() ) {
|
|
||||||
|
|
||||||
// check if it is time to send a new message
|
|
||||||
if ( (millis() - last) > interval ) {
|
|
||||||
String test_str_msg = "testing, testing, ";
|
|
||||||
test_str_msg += counter;
|
|
||||||
counter ++;
|
|
||||||
|
|
||||||
sb.send("string test", test_str_msg);
|
|
||||||
sb.send("range test", 500);
|
|
||||||
sb.send("boolean test", true);
|
|
||||||
sb.send("custom test", "youre loco");
|
|
||||||
|
|
||||||
last = millis();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// define handler methods, all standard data type handlers take two appropriate arguments
|
|
||||||
|
|
||||||
void handleRange (String route, int value) {
|
|
||||||
Serial.print("Range msg ");
|
|
||||||
Serial.print(route);
|
|
||||||
Serial.print(", value ");
|
|
||||||
Serial.println(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
void handleString (String route, String value) {
|
|
||||||
Serial.print("String msg ");
|
|
||||||
Serial.print(route);
|
|
||||||
Serial.print(", value ");
|
|
||||||
Serial.println(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
void handleBoolean (String route, boolean value) {
|
|
||||||
Serial.print("Boolen msg ");
|
|
||||||
Serial.print(route);
|
|
||||||
Serial.print(", value ");
|
|
||||||
Serial.println(value ? "true" : "false");
|
|
||||||
}
|
|
||||||
|
|
||||||
// custom data type handlers takes three String arguments
|
|
||||||
|
|
||||||
void handleCustom (String route, String value, String type) {
|
|
||||||
Serial.print("Custom msg ");
|
|
||||||
Serial.print(route);
|
|
||||||
Serial.print(" of type ");
|
|
||||||
Serial.print(type);
|
|
||||||
Serial.print(", value ");
|
|
||||||
Serial.println(value);
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,89 +0,0 @@
|
||||||
/*
|
|
||||||
Spacebrew Boolean
|
|
||||||
|
|
||||||
Demonstrates how to create a sketch that sends and receives a
|
|
||||||
boolean value to and from Spacebrew. Every time the buttton is
|
|
||||||
pressed (or other digital input component) a spacebrew message
|
|
||||||
is sent. The sketch also accepts analog range messages from
|
|
||||||
other Spacebrew apps.
|
|
||||||
|
|
||||||
Make sure that your Yun is connected to the internet for this example
|
|
||||||
to function properly.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
- Button connected to Yun, using the Arduino's internal pullup resistor.
|
|
||||||
|
|
||||||
created 2013
|
|
||||||
by Julio Terra
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
More information about Spacebrew is available at:
|
|
||||||
http://spacebrew.cc/
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
#include <SpacebrewYun.h>
|
|
||||||
|
|
||||||
// create a variable of type SpacebrewYun and initialize it with the constructor
|
|
||||||
SpacebrewYun sb = SpacebrewYun("spacebrewYun Boolean", "Boolean sender and receiver");
|
|
||||||
|
|
||||||
// variable that holds the last potentiometer value
|
|
||||||
int last_value = 0;
|
|
||||||
|
|
||||||
// create variables to manage interval between each time we send a string
|
|
||||||
void setup() {
|
|
||||||
|
|
||||||
// start the serial port
|
|
||||||
Serial.begin(57600);
|
|
||||||
|
|
||||||
// for debugging, wait until a serial console is connected
|
|
||||||
delay(4000);
|
|
||||||
while (!Serial) { ; }
|
|
||||||
|
|
||||||
// start-up the bridge
|
|
||||||
Bridge.begin();
|
|
||||||
|
|
||||||
// configure the spacebrew object to print status messages to serial
|
|
||||||
sb.verbose(true);
|
|
||||||
|
|
||||||
// configure the spacebrew publisher and subscriber
|
|
||||||
sb.addPublish("physical button", "boolean");
|
|
||||||
sb.addSubscribe("virtual button", "boolean");
|
|
||||||
|
|
||||||
// register the string message handler method
|
|
||||||
sb.onBooleanMessage(handleBoolean);
|
|
||||||
|
|
||||||
// connect to cloud spacebrew server at "sandbox.spacebrew.cc"
|
|
||||||
sb.connect("sandbox.spacebrew.cc");
|
|
||||||
|
|
||||||
pinMode(3, INPUT);
|
|
||||||
digitalWrite(3, HIGH);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// monitor spacebrew connection for new data
|
|
||||||
sb.monitor();
|
|
||||||
|
|
||||||
// connected to spacebrew then send a new value whenever the pot value changes
|
|
||||||
if ( sb.connected() ) {
|
|
||||||
int cur_value = digitalRead(3);
|
|
||||||
if ( last_value != cur_value ) {
|
|
||||||
if (cur_value == HIGH) sb.send("physical button", false);
|
|
||||||
else sb.send("physical button", true);
|
|
||||||
last_value = cur_value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// handler method that is called whenever a new string message is received
|
|
||||||
void handleBoolean (String route, boolean value) {
|
|
||||||
// print the message that was received
|
|
||||||
Serial.print("From ");
|
|
||||||
Serial.print(route);
|
|
||||||
Serial.print(", received msg: ");
|
|
||||||
Serial.println(value ? "true" : "false");
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,86 +0,0 @@
|
||||||
/*
|
|
||||||
Spacebrew Range
|
|
||||||
|
|
||||||
Demonstrates how to create a sketch that sends and receives analog
|
|
||||||
range value to and from Spacebrew. Every time the state of the
|
|
||||||
potentiometer (or other analog input component) change a spacebrew
|
|
||||||
message is sent. The sketch also accepts analog range messages from
|
|
||||||
other Spacebrew apps.
|
|
||||||
|
|
||||||
Make sure that your Yun is connected to the internet for this example
|
|
||||||
to function properly.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
- Potentiometer connected to Yun. Middle pin connected to analog pin A0,
|
|
||||||
other pins connected to 5v and GND pins.
|
|
||||||
|
|
||||||
created 2013
|
|
||||||
by Julio Terra
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
More information about Spacebrew is available at:
|
|
||||||
http://spacebrew.cc/
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
#include <SpacebrewYun.h>
|
|
||||||
|
|
||||||
// create a variable of type SpacebrewYun and initialize it with the constructor
|
|
||||||
SpacebrewYun sb = SpacebrewYun("spacebrewYun Range", "Range sender and receiver");
|
|
||||||
|
|
||||||
// variable that holds the last potentiometer value
|
|
||||||
int last_value = 0;
|
|
||||||
|
|
||||||
// create variables to manage interval between each time we send a string
|
|
||||||
void setup() {
|
|
||||||
|
|
||||||
// start the serial port
|
|
||||||
Serial.begin(57600);
|
|
||||||
|
|
||||||
// for debugging, wait until a serial console is connected
|
|
||||||
delay(4000);
|
|
||||||
while (!Serial) { ; }
|
|
||||||
|
|
||||||
// start-up the bridge
|
|
||||||
Bridge.begin();
|
|
||||||
|
|
||||||
// configure the spacebrew object to print status messages to serial
|
|
||||||
sb.verbose(true);
|
|
||||||
|
|
||||||
// configure the spacebrew publisher and subscriber
|
|
||||||
sb.addPublish("physical pot", "range");
|
|
||||||
sb.addSubscribe("virtual pot", "range");
|
|
||||||
|
|
||||||
// register the string message handler method
|
|
||||||
sb.onRangeMessage(handleRange);
|
|
||||||
|
|
||||||
// connect to cloud spacebrew server at "sandbox.spacebrew.cc"
|
|
||||||
sb.connect("sandbox.spacebrew.cc");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// monitor spacebrew connection for new data
|
|
||||||
sb.monitor();
|
|
||||||
|
|
||||||
// connected to spacebrew then send a new value whenever the pot value changes
|
|
||||||
if ( sb.connected() ) {
|
|
||||||
int cur_value = analogRead(A0);
|
|
||||||
if ( last_value != cur_value ) {
|
|
||||||
sb.send("physical pot", cur_value);
|
|
||||||
last_value = cur_value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// handler method that is called whenever a new string message is received
|
|
||||||
void handleRange (String route, int value) {
|
|
||||||
// print the message that was received
|
|
||||||
Serial.print("From ");
|
|
||||||
Serial.print(route);
|
|
||||||
Serial.print(", received msg: ");
|
|
||||||
Serial.println(value);
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,84 +0,0 @@
|
||||||
/*
|
|
||||||
Spacebrew String
|
|
||||||
|
|
||||||
Demonstrates how to create a sketch that sends and receives strings
|
|
||||||
to and from Spacebrew. Every time string data is received it
|
|
||||||
is output to the Serial monitor.
|
|
||||||
|
|
||||||
Make sure that your Yun is connected to the internet for this example
|
|
||||||
to function properly.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
- No circuit required
|
|
||||||
|
|
||||||
created 2013
|
|
||||||
by Julio Terra
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
More information about Spacebrew is available at:
|
|
||||||
http://spacebrew.cc/
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
#include <SpacebrewYun.h>
|
|
||||||
|
|
||||||
// create a variable of type SpacebrewYun and initialize it with the constructor
|
|
||||||
SpacebrewYun sb = SpacebrewYun("spacebrewYun Strings", "String sender and receiver");
|
|
||||||
|
|
||||||
// create variables to manage interval between each time we send a string
|
|
||||||
long last_time = 0;
|
|
||||||
int interval = 2000;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
|
|
||||||
// start the serial port
|
|
||||||
Serial.begin(57600);
|
|
||||||
|
|
||||||
// for debugging, wait until a serial console is connected
|
|
||||||
delay(4000);
|
|
||||||
while (!Serial) { ; }
|
|
||||||
|
|
||||||
// start-up the bridge
|
|
||||||
Bridge.begin();
|
|
||||||
|
|
||||||
// configure the spacebrew object to print status messages to serial
|
|
||||||
sb.verbose(true);
|
|
||||||
|
|
||||||
// configure the spacebrew publisher and subscriber
|
|
||||||
sb.addPublish("speak", "string");
|
|
||||||
sb.addSubscribe("listen", "string");
|
|
||||||
|
|
||||||
// register the string message handler method
|
|
||||||
sb.onStringMessage(handleString);
|
|
||||||
|
|
||||||
// connect to cloud spacebrew server at "sandbox.spacebrew.cc"
|
|
||||||
sb.connect("sandbox.spacebrew.cc");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// monitor spacebrew connection for new data
|
|
||||||
sb.monitor();
|
|
||||||
|
|
||||||
// connected to spacebrew then send a string every 2 seconds
|
|
||||||
if ( sb.connected() ) {
|
|
||||||
|
|
||||||
// check if it is time to send a new message
|
|
||||||
if ( (millis() - last_time) > interval ) {
|
|
||||||
sb.send("speak", "is anybody out there?");
|
|
||||||
last_time = millis();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// handler method that is called whenever a new string message is received
|
|
||||||
void handleString (String route, String value) {
|
|
||||||
// print the message that was received
|
|
||||||
Serial.print("From ");
|
|
||||||
Serial.print(route);
|
|
||||||
Serial.print(", received msg: ");
|
|
||||||
Serial.println(value);
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,443 +0,0 @@
|
||||||
/*
|
|
||||||
ControlBySMS
|
|
||||||
|
|
||||||
Demonstrates using an SMS message to a Twilio account to turn an LED
|
|
||||||
on the Yun board on and off using the Temboo Arduino Yun SDK.
|
|
||||||
Sending a SMS with the text "LED ON" to your Twilio phone number
|
|
||||||
will turn on the LED on the Yun. Sending "LED OFF" will turn it off.
|
|
||||||
|
|
||||||
Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
|
|
||||||
|
|
||||||
A Temboo account and application key are necessary to run all Temboo examples.
|
|
||||||
If you don't already have one, you can register for a free Temboo account at
|
|
||||||
http://www.temboo.com
|
|
||||||
|
|
||||||
Since this sketch uses Twilio to retrieve the SMS, you'll also need a valid
|
|
||||||
Twilio account. You can create one for free at https://www.twilio.com.
|
|
||||||
|
|
||||||
The sketch needs your Twilio Account SID and Auth Token you get when you
|
|
||||||
register with Twilio. Make sure to use the Account SID and Auth Token from
|
|
||||||
your Twilio Dashboard (not your test credentials from the Dev Tools panel).
|
|
||||||
|
|
||||||
Normally, Twilio expects to contact a web site you provide to get a response
|
|
||||||
when an SMS message is received for your Twilio number. In this case, we
|
|
||||||
don't want to send any response (and we don't want to have to set up a web
|
|
||||||
site just to receive SMS messages.) You can use a URL that Twilio provides
|
|
||||||
for this purpose. When a message is received and sent to the Twilio "twimlets"
|
|
||||||
URL, it returns a code meaning "no response required." To set this up:
|
|
||||||
|
|
||||||
1. Log in to your Twilio account and go to this URL:
|
|
||||||
|
|
||||||
https://www.twilio.com/user/account/phone-numbers/incoming
|
|
||||||
|
|
||||||
2. Select the Twilio number you want to receive SMS messages at.
|
|
||||||
|
|
||||||
3. Put this URL in the "SMS Request URL" field:
|
|
||||||
|
|
||||||
http://twimlets.com/echo?Twiml=%3CResponse%3E%3C%2FResponse%3E
|
|
||||||
|
|
||||||
See this link to Twilio's FAQ for details:
|
|
||||||
|
|
||||||
https://www.twilio.com/help/faq/sms/how-can-i-receive-sms-messages-without-responding
|
|
||||||
|
|
||||||
4. Click the "Save Changes" button at the bottom of the page.
|
|
||||||
|
|
||||||
Your account will now receive SMS messages, but won't send any responses.
|
|
||||||
|
|
||||||
This example assumes basic familiarity with Arduino sketches, and that your Yun is connected
|
|
||||||
to the Internet.
|
|
||||||
|
|
||||||
Looking for another API? We've got over 100 in our Library!
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
#include <Temboo.h>
|
|
||||||
#include "TembooAccount.h" // contains Temboo account information
|
|
||||||
// as described in the footer comment below
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*** SUBSTITUTE YOUR VALUES BELOW: ***/
|
|
||||||
|
|
||||||
// Note that for additional security and reusability, you could
|
|
||||||
// use #define statements to specify these values in a .h file.
|
|
||||||
|
|
||||||
// the Account SID from your Twilio account
|
|
||||||
const String TWILIO_ACCOUNT_SID = "xxxxxxxxxxxxxxxxxx";
|
|
||||||
|
|
||||||
// the Auth Token from your Twilio account
|
|
||||||
const String TWILIO_AUTH_TOKEN = "xxxxxxxxxxxxxxxxx";
|
|
||||||
|
|
||||||
// only act on messages sent from this phone number. (e.g. 15415551212)
|
|
||||||
const String FROM_PHONE_NUMBER = "xxxxxxxxxxx";
|
|
||||||
|
|
||||||
// how often (in milliseconds) to check for new SMS messages.
|
|
||||||
const unsigned long SMS_CHECK_PERIOD = 60000;
|
|
||||||
|
|
||||||
// keep track of when we last checked for new messages
|
|
||||||
// (initialize it to SMS_CHECK_PERIOD seconds ago so
|
|
||||||
// we do the first check as soon as the sketch starts.)
|
|
||||||
unsigned long lastSMSCheckTime = -SMS_CHECK_PERIOD;
|
|
||||||
|
|
||||||
// keep track of the ID of the last SMS message we processed.
|
|
||||||
// (we only need to process newer messages)
|
|
||||||
String lastSid;
|
|
||||||
|
|
||||||
// we'll be turning the LED built in to the Yun on and off
|
|
||||||
// to simulate controlling some device. That LED is on pin 13.
|
|
||||||
int LED_PIN = 13;
|
|
||||||
|
|
||||||
int numRuns = 1; // execution count, so this doesn't run forever
|
|
||||||
int maxRuns = 10; // the max number of times the Twitter HomeTimeline Choreo should run
|
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(9600);
|
|
||||||
|
|
||||||
// for debugging, wait until a serial console is connected
|
|
||||||
delay(4000);
|
|
||||||
while(!Serial);
|
|
||||||
|
|
||||||
// tell the board to treat the LED pin as an output.
|
|
||||||
pinMode(LED_PIN, OUTPUT);
|
|
||||||
|
|
||||||
// start with the LED off
|
|
||||||
digitalWrite(LED_PIN, LOW);
|
|
||||||
|
|
||||||
// initialize the connection to the Linino processor.
|
|
||||||
Bridge.begin();
|
|
||||||
|
|
||||||
// Twilio will report old SMS messages. We want to
|
|
||||||
// ignore any existing control messages when we start.
|
|
||||||
Serial.println("Ignoring any existing control messages...");
|
|
||||||
checkForMessages(true);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
|
|
||||||
// get the number of milliseconds the CPU has been running.
|
|
||||||
unsigned long now = millis();
|
|
||||||
|
|
||||||
// see if it's time to check for new SMS messages.
|
|
||||||
if (now - lastSMSCheckTime >= SMS_CHECK_PERIOD) {
|
|
||||||
|
|
||||||
// it's time to check for new messages
|
|
||||||
// save this time so we know when to check next
|
|
||||||
lastSMSCheckTime = now;
|
|
||||||
|
|
||||||
if (numRuns <= maxRuns) {
|
|
||||||
Serial.println("Checking for new SMS messages - Run #" + String(numRuns++));
|
|
||||||
|
|
||||||
// execute the choreo and don't ignore control messages.
|
|
||||||
checkForMessages(false);
|
|
||||||
} else {
|
|
||||||
Serial.println("Already ran " + String(maxRuns) + " times.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
This function executes the Twilio > SMSMessages > ListMessages choreo
|
|
||||||
and processes the results.
|
|
||||||
|
|
||||||
If ignoreCommands is 'true', this function will read and process messages
|
|
||||||
updating 'lastSid', but will not actually take any action on any commands
|
|
||||||
found. This is so we can ignore any old control messages when we start.
|
|
||||||
|
|
||||||
If ignoreCommands is 'false', control messages WILL be acted on.
|
|
||||||
*/
|
|
||||||
void checkForMessages(bool ignoreCommands) {
|
|
||||||
|
|
||||||
// we need a TembooChoreo object to send a Choreo request to Temboo
|
|
||||||
TembooChoreo ListMessagesChoreo;
|
|
||||||
|
|
||||||
ListMessagesChoreo.begin();
|
|
||||||
|
|
||||||
// set Temboo account credentials
|
|
||||||
ListMessagesChoreo.setAccountName(TEMBOO_ACCOUNT);
|
|
||||||
ListMessagesChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
|
|
||||||
ListMessagesChoreo.setAppKey(TEMBOO_APP_KEY);
|
|
||||||
|
|
||||||
// identify the Temboo Library choreo to run (Twilio > SMSMessages > ListMessages)
|
|
||||||
ListMessagesChoreo.setChoreo("/Library/Twilio/SMSMessages/ListMessages");
|
|
||||||
|
|
||||||
// set the choreo inputs
|
|
||||||
// see https://www.temboo.com/library/Library/Twilio/SMSMessages/ListMessages/
|
|
||||||
// for complete details about the inputs for this Choreo
|
|
||||||
|
|
||||||
// the first input is a your Twilio AccountSID
|
|
||||||
ListMessagesChoreo.addInput("AccountSID", TWILIO_ACCOUNT_SID);
|
|
||||||
|
|
||||||
// next is your Twilio Auth Token
|
|
||||||
ListMessagesChoreo.addInput("AuthToken", TWILIO_AUTH_TOKEN);
|
|
||||||
|
|
||||||
// we only want to know about messages sent from our designated phone number
|
|
||||||
ListMessagesChoreo.addInput("From", FROM_PHONE_NUMBER);
|
|
||||||
|
|
||||||
// Twilio can return information about up to 1000 messages at a time.
|
|
||||||
// we're only interested in the 3 most recent ones. Note that if
|
|
||||||
// this account receives lots of messages in quick succession,
|
|
||||||
// (more than 3 per minute in this case), we might miss some control
|
|
||||||
// messages. But if we request too many messages, we might run out of
|
|
||||||
// memory on the Arduino side of the Yun.
|
|
||||||
ListMessagesChoreo.addInput("PageSize", "3");
|
|
||||||
|
|
||||||
// We want the response in XML format to process with our
|
|
||||||
// XPath output filters.
|
|
||||||
ListMessagesChoreo.addInput("ResponseFormat", "xml");
|
|
||||||
|
|
||||||
// we don't want everything from the output, just the
|
|
||||||
// message IDs (the Sids) and the message texts
|
|
||||||
ListMessagesChoreo.addOutputFilter("sid", "Sid", "Response");
|
|
||||||
ListMessagesChoreo.addOutputFilter("text", "Body", "Response");
|
|
||||||
|
|
||||||
// tell the Choreo to run and wait for the results. The
|
|
||||||
// return code (returnCode) will tell us whether the Temboo client
|
|
||||||
// was able to send our request to the Temboo servers
|
|
||||||
unsigned int returnCode = ListMessagesChoreo.run();
|
|
||||||
|
|
||||||
// a return code of zero (0) means success
|
|
||||||
if (returnCode == 0) {
|
|
||||||
|
|
||||||
// Need a string to hold the list of message IDs.
|
|
||||||
String messageSids;
|
|
||||||
|
|
||||||
// Need a string to hold the texts of the messages.
|
|
||||||
String messageTexts;
|
|
||||||
|
|
||||||
// when the choreo results are available, process them.
|
|
||||||
// the output filters we specified will return comma delimited
|
|
||||||
// lists containing the Sids and texts of the messages
|
|
||||||
// from our designated phone number.
|
|
||||||
|
|
||||||
while(ListMessagesChoreo.available()) {
|
|
||||||
|
|
||||||
// output names are terminated with '\x1F' characters.
|
|
||||||
String name = ListMessagesChoreo.readStringUntil('\x1F');
|
|
||||||
name.trim();
|
|
||||||
//Serial.println(name);
|
|
||||||
|
|
||||||
// output values are terminated with '\x1E' characters.
|
|
||||||
String data = ListMessagesChoreo.readStringUntil('\x1E');
|
|
||||||
data.trim();
|
|
||||||
//Serial.println(data);
|
|
||||||
|
|
||||||
// assign the data to the appropriate string based on the name
|
|
||||||
if (name == "sid") {
|
|
||||||
messageSids = data;
|
|
||||||
} else if (name == "text") {
|
|
||||||
messageTexts = data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// done reading output, close the Choreo to free up resources.
|
|
||||||
ListMessagesChoreo.close();
|
|
||||||
|
|
||||||
// parse the comma delimited lists of messages and Sids
|
|
||||||
processMessages(messageTexts, messageSids, ignoreCommands);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// a non-zero return code means there was an error
|
|
||||||
// read and print the error message
|
|
||||||
while(ListMessagesChoreo.available()) {
|
|
||||||
char c = ListMessagesChoreo.read();
|
|
||||||
Serial.print(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
This function processes the lists of message texts and Sids.
|
|
||||||
If a message contains a comma as part of the
|
|
||||||
message text, that message will be enclosed in double quotes
|
|
||||||
(") in the list. Example:
|
|
||||||
|
|
||||||
A message,"Hey, now",Another message text
|
|
||||||
|
|
||||||
If the message contains double quotes, it will be enclosed in
|
|
||||||
double quotes AND the internal quotes will be doubled.
|
|
||||||
Example:
|
|
||||||
|
|
||||||
"Hi ""Sam"" the man", Led on
|
|
||||||
|
|
||||||
NOTE! We are assuming that Twilio returns more recent messages
|
|
||||||
first. This isn't officially documented by Twilio, but we've
|
|
||||||
not seen any other case.
|
|
||||||
|
|
||||||
'messageTexts' is a String containing a comma separated list of
|
|
||||||
message texts with commas and quotes escaped as described above.
|
|
||||||
|
|
||||||
'messageSids' is a String containing a comma separated list of
|
|
||||||
message Sids. Sids should not contain embedded commas or quotes.
|
|
||||||
|
|
||||||
'ignoreCommands' is a boolean. 'true' means and control messages
|
|
||||||
will not be acted upon. 'false' means control messages will be
|
|
||||||
acted upon in the usual way.
|
|
||||||
*/
|
|
||||||
void processMessages(String messageTexts, String messageSids, bool ignoreCommands) {
|
|
||||||
|
|
||||||
// proceed if we received at least one message
|
|
||||||
if (messageSids.length() > 0) {
|
|
||||||
int i = -1;
|
|
||||||
int sidsStart = 0;
|
|
||||||
int textsStart = 0;
|
|
||||||
String sid;
|
|
||||||
String text;
|
|
||||||
bool ledUpdated = false;
|
|
||||||
|
|
||||||
// go through the list until we run out of items
|
|
||||||
// or otherwise know we can stop
|
|
||||||
do {
|
|
||||||
|
|
||||||
// Output filter list items are separated by commas
|
|
||||||
// find the start of the next item in the list
|
|
||||||
i = messageSids.indexOf(',', sidsStart);
|
|
||||||
if (i >= 0) {
|
|
||||||
|
|
||||||
//extract a single Sid from the list.
|
|
||||||
sid = messageSids.substring(sidsStart, i);
|
|
||||||
sidsStart = i + 1;
|
|
||||||
|
|
||||||
// find the start of the next text in the list.
|
|
||||||
// Note that we have to be prepared to handle embedded
|
|
||||||
// quotes and commans in the message texts.
|
|
||||||
// The standard Arduino String class doesn't handle
|
|
||||||
// this, so we have to write our own function to do it.
|
|
||||||
i = quotedIndexOf(messageTexts, ',', textsStart);
|
|
||||||
if (i >= 0) {
|
|
||||||
|
|
||||||
// extract a single message text from the list.
|
|
||||||
text = messageTexts.substring(textsStart, i);
|
|
||||||
textsStart = i + 1;
|
|
||||||
|
|
||||||
// process the Sid and text to see if it's a
|
|
||||||
// control message.
|
|
||||||
ledUpdated = processMessage(sid, text, ignoreCommands);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
|
|
||||||
// the last item in the lists won't have a comma at the end,
|
|
||||||
// so we have to handle them specially.
|
|
||||||
// Since we know this is the last item, we can just
|
|
||||||
// take the rest of the string for the Sid and text.
|
|
||||||
sid = messageSids.substring(sidsStart);
|
|
||||||
text = messageTexts.substring(textsStart);
|
|
||||||
|
|
||||||
// process the last item.
|
|
||||||
ledUpdated = processMessage(sid, text, ignoreCommands);
|
|
||||||
}
|
|
||||||
|
|
||||||
// keep going until either we run out of list items
|
|
||||||
// or we run into a message we processed on a previous run.
|
|
||||||
} while ((i >=0) && (sid != lastSid));
|
|
||||||
|
|
||||||
// print what we've found to the serial monitor,
|
|
||||||
// just so we can see what's going on.
|
|
||||||
if (sid == lastSid) {
|
|
||||||
if (ledUpdated)
|
|
||||||
Serial.println("Control message processed.");
|
|
||||||
else
|
|
||||||
Serial.println("No new control messages received.");
|
|
||||||
} else {
|
|
||||||
Serial.println("No control messages received.");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Serial.println("No messages found");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
This function knows how to tell if a message is a control message
|
|
||||||
or not. It also knows know to control whatever it is we're controlling
|
|
||||||
(the state of the LED on pin 13 in this case.)
|
|
||||||
|
|
||||||
A message with the text "LED ON" turns the LED on.
|
|
||||||
A message with the text "LED OFF" turns the LED off.
|
|
||||||
(Case is ignored.)
|
|
||||||
|
|
||||||
If 'ignoreCommands' is true, the actions described above will NOT
|
|
||||||
take place.
|
|
||||||
|
|
||||||
It also updates the 'lastSid' global variable when
|
|
||||||
a control message is processed.
|
|
||||||
|
|
||||||
It returns 'true' if the message was a control message, and
|
|
||||||
'false' if it wasn't or if we've already processed this message.
|
|
||||||
*/
|
|
||||||
bool processMessage(String sid, String text, bool ignoreCommands) {
|
|
||||||
|
|
||||||
// a flag to indicate whether this was a control message or not
|
|
||||||
bool ledUpdated = false;
|
|
||||||
|
|
||||||
// if we haven't already processed this message
|
|
||||||
if (sid != lastSid) {
|
|
||||||
|
|
||||||
if (text.equalsIgnoreCase("LED ON")) {
|
|
||||||
|
|
||||||
if (!ignoreCommands) {
|
|
||||||
//turn on the LED
|
|
||||||
digitalWrite(LED_PIN, HIGH);
|
|
||||||
Serial.println("LED ON");
|
|
||||||
}
|
|
||||||
ledUpdated = true;
|
|
||||||
} else if (text.equalsIgnoreCase("LED OFF")) {
|
|
||||||
if (!ignoreCommands) {
|
|
||||||
//turn off the LED
|
|
||||||
digitalWrite(LED_PIN, LOW);
|
|
||||||
Serial.println("LED OFF");
|
|
||||||
}
|
|
||||||
ledUpdated = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the LED state was updated, remember the Sid if this message.
|
|
||||||
if (ledUpdated)
|
|
||||||
lastSid = sid;
|
|
||||||
}
|
|
||||||
return ledUpdated;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
This function finds the index of a delimiter character in a String,
|
|
||||||
ignoring delimiters that appear inside double-quotes.
|
|
||||||
*/
|
|
||||||
int quotedIndexOf(String s, char delim, int start) {
|
|
||||||
bool inQuotes = false;
|
|
||||||
char c;
|
|
||||||
int index = -1;
|
|
||||||
const char QUOTE = '"';
|
|
||||||
do {
|
|
||||||
c = s[start++];
|
|
||||||
if (c == QUOTE)
|
|
||||||
inQuotes = !inQuotes;
|
|
||||||
else if (c == delim && !inQuotes)
|
|
||||||
index = --start;
|
|
||||||
} while ((c != '\0') && (index < 0));
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
IMPORTANT NOTE: TembooAccount.h:
|
|
||||||
|
|
||||||
TembooAccount.h is a file referenced by this sketch that contains your Temboo account information.
|
|
||||||
You'll need to edit the placeholder version of TembooAccount.h included with this example sketch,
|
|
||||||
by inserting your own Temboo account name and app key information. The contents of the file should
|
|
||||||
look like:
|
|
||||||
|
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
||||||
You can find your Temboo App Key information on the Temboo website,
|
|
||||||
under My Account > Application Keys
|
|
||||||
|
|
||||||
The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
|
|
||||||
|
|
||||||
Keeping your account information in a separate file means you can save it once,
|
|
||||||
then just distribute the main .ino file without worrying that you forgot to delete your credentials.
|
|
||||||
*/
|
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
||||||
|
|
|
@ -1,115 +0,0 @@
|
||||||
/*
|
|
||||||
GetYahooWeatherReport
|
|
||||||
|
|
||||||
Demonstrates making a request to the Yahoo! Weather API using Temboo from an Arduino Yun.
|
|
||||||
|
|
||||||
Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
|
|
||||||
|
|
||||||
A Temboo account and application key are necessary to run all Temboo examples.
|
|
||||||
If you don't already have one, you can register for a free Temboo account at
|
|
||||||
http://www.temboo.com
|
|
||||||
|
|
||||||
This example assumes basic familiarity with Arduino sketches, and that your Yun is connected
|
|
||||||
to the Internet.
|
|
||||||
|
|
||||||
Looking for another API to use with your Arduino Yun? We've got over 100 in our Library!
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
#include <Temboo.h>
|
|
||||||
#include "TembooAccount.h" // contains Temboo account information
|
|
||||||
// as described in the footer comment below
|
|
||||||
|
|
||||||
|
|
||||||
// the address for which a weather forecast will be retrieved
|
|
||||||
String ADDRESS_FOR_FORECAST = "104 Franklin St., New York NY 10013";
|
|
||||||
|
|
||||||
int numRuns = 1; // execution count, so that this doesn't run forever
|
|
||||||
int maxRuns = 10; // max number of times the Yahoo WeatherByAddress Choreo should be run
|
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(9600);
|
|
||||||
|
|
||||||
// for debugging, wait until a serial console is connected
|
|
||||||
delay(4000);
|
|
||||||
while(!Serial);
|
|
||||||
Bridge.begin();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
// while we haven't reached the max number of runs...
|
|
||||||
if (numRuns <= maxRuns) {
|
|
||||||
|
|
||||||
// print status
|
|
||||||
Serial.println("Running GetWeatherByAddress - Run #" + String(numRuns++) + "...");
|
|
||||||
|
|
||||||
// create a TembooChoreo object to send a Choreo request to Temboo
|
|
||||||
TembooChoreo GetWeatherByAddressChoreo;
|
|
||||||
|
|
||||||
// invoke the Temboo client
|
|
||||||
GetWeatherByAddressChoreo.begin();
|
|
||||||
|
|
||||||
// add your temboo account info
|
|
||||||
GetWeatherByAddressChoreo.setAccountName(TEMBOO_ACCOUNT);
|
|
||||||
GetWeatherByAddressChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
|
|
||||||
GetWeatherByAddressChoreo.setAppKey(TEMBOO_APP_KEY);
|
|
||||||
|
|
||||||
// set the name of the choreo we want to run
|
|
||||||
GetWeatherByAddressChoreo.setChoreo("/Library/Yahoo/Weather/GetWeatherByAddress");
|
|
||||||
|
|
||||||
// set choreo inputs; in this case, the address for which to retrieve weather data
|
|
||||||
// the Temboo client provides standardized calls to 100+ cloud APIs
|
|
||||||
GetWeatherByAddressChoreo.addInput("Address", ADDRESS_FOR_FORECAST);
|
|
||||||
|
|
||||||
// add an output filter to extract the name of the city.
|
|
||||||
GetWeatherByAddressChoreo.addOutputFilter("city", "/rss/channel/yweather:location/@city", "Response");
|
|
||||||
|
|
||||||
// add an output filter to extract the current temperature
|
|
||||||
GetWeatherByAddressChoreo.addOutputFilter("temperature", "/rss/channel/item/yweather:condition/@temp", "Response");
|
|
||||||
|
|
||||||
// add an output filter to extract the date and time of the last report.
|
|
||||||
GetWeatherByAddressChoreo.addOutputFilter("date", "/rss/channel/item/yweather:condition/@date", "Response");
|
|
||||||
|
|
||||||
// run the choreo
|
|
||||||
GetWeatherByAddressChoreo.run();
|
|
||||||
|
|
||||||
// when the choreo results are available, print them to the serial monitor
|
|
||||||
while(GetWeatherByAddressChoreo.available()) {
|
|
||||||
|
|
||||||
char c = GetWeatherByAddressChoreo.read();
|
|
||||||
Serial.print(c);
|
|
||||||
}
|
|
||||||
GetWeatherByAddressChoreo.close();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.println("Waiting...");
|
|
||||||
Serial.println("");
|
|
||||||
delay(30000); // wait 30 seconds between GetWeatherByAddress calls
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
IMPORTANT NOTE: TembooAccount.h:
|
|
||||||
|
|
||||||
TembooAccount.h is a file referenced by this sketch that contains your Temboo account information.
|
|
||||||
You'll need to edit the placeholder version of TembooAccount.h included with this example sketch,
|
|
||||||
by inserting your own Temboo account name and app key information. The contents of the file should
|
|
||||||
look like:
|
|
||||||
|
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
||||||
You can find your Temboo App Key information on the Temboo website,
|
|
||||||
under My Account > Application Keys
|
|
||||||
|
|
||||||
The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
|
|
||||||
|
|
||||||
Keeping your account information in a separate file means you can share the main .ino file without worrying
|
|
||||||
that you forgot to delete your credentials.
|
|
||||||
*/
|
|
|
@ -1,4 +0,0 @@
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
|
@ -1,173 +0,0 @@
|
||||||
/*
|
|
||||||
ReadATweet
|
|
||||||
|
|
||||||
Demonstrates retrieving the most recent Tweet from a user's home timeline
|
|
||||||
using Temboo from an Arduino Yun.
|
|
||||||
|
|
||||||
Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
|
|
||||||
|
|
||||||
A Temboo account and application key are necessary to run all Temboo examples.
|
|
||||||
If you don't already have one, you can register for a free Temboo account at
|
|
||||||
http://www.temboo.com
|
|
||||||
|
|
||||||
In order to run this sketch, you'll need to register an application using
|
|
||||||
the Twitter dev console at https://dev.twitter.com. After creating the
|
|
||||||
app, you'll find OAuth credentials for that application under the "OAuth Tool" tab.
|
|
||||||
Substitute these values for the placeholders below.
|
|
||||||
|
|
||||||
This example assumes basic familiarity with Arduino sketches, and that your Yun
|
|
||||||
is connected to the Internet.
|
|
||||||
|
|
||||||
Want to use another social API with your Arduino Yun? We've got Facebook,
|
|
||||||
Google+, Instagram, Tumblr and more in our Library!
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
#include <Temboo.h>
|
|
||||||
#include "TembooAccount.h" // contains Temboo account information
|
|
||||||
// as described in the footer comment below
|
|
||||||
|
|
||||||
/*** SUBSTITUTE YOUR VALUES BELOW: ***/
|
|
||||||
|
|
||||||
// Note that for additional security and reusability, you could
|
|
||||||
// use #define statements to specify these values in a .h file.
|
|
||||||
const String TWITTER_ACCESS_TOKEN = "your-twitter-access-token";
|
|
||||||
const String TWITTER_ACCESS_TOKEN_SECRET = "your-twitter-access-token-secret";
|
|
||||||
const String TWITTER_CONSUMER_KEY = "your-twitter-consumer-key";
|
|
||||||
const String TWITTER_CONSUMER_SECRET = "your-twitter-consumer-secret";
|
|
||||||
|
|
||||||
int numRuns = 1; // execution count, so this doesn't run forever
|
|
||||||
int maxRuns = 10; // the max number of times the Twitter HomeTimeline Choreo should run
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(9600);
|
|
||||||
|
|
||||||
// For debugging, wait until a serial console is connected.
|
|
||||||
delay(4000);
|
|
||||||
while(!Serial);
|
|
||||||
Bridge.begin();
|
|
||||||
}
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
// while we haven't reached the max number of runs...
|
|
||||||
if (numRuns <= maxRuns) {
|
|
||||||
Serial.println("Running ReadATweet - Run #" + String(numRuns++));
|
|
||||||
|
|
||||||
TembooChoreo HomeTimelineChoreo;
|
|
||||||
|
|
||||||
// invoke the Temboo client.
|
|
||||||
// NOTE that the client must be reinvoked, and repopulated with
|
|
||||||
// appropriate arguments, each time its run() method is called.
|
|
||||||
HomeTimelineChoreo.begin();
|
|
||||||
|
|
||||||
// set Temboo account credentials
|
|
||||||
HomeTimelineChoreo.setAccountName(TEMBOO_ACCOUNT);
|
|
||||||
HomeTimelineChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
|
|
||||||
HomeTimelineChoreo.setAppKey(TEMBOO_APP_KEY);
|
|
||||||
|
|
||||||
// tell the Temboo client which Choreo to run (Twitter > Timelines > HomeTimeline)
|
|
||||||
HomeTimelineChoreo.setChoreo("/Library/Twitter/Timelines/HomeTimeline");
|
|
||||||
|
|
||||||
|
|
||||||
// set the required choreo inputs
|
|
||||||
// see https://www.temboo.com/library/Library/Twitter/Timelines/HomeTimeline/
|
|
||||||
// for complete details about the inputs for this Choreo
|
|
||||||
|
|
||||||
HomeTimelineChoreo.addInput("Count", "1"); // the max number of Tweets to return from each request
|
|
||||||
HomeTimelineChoreo.addInput("AccessToken", TWITTER_ACCESS_TOKEN);
|
|
||||||
HomeTimelineChoreo.addInput("AccessTokenSecret", TWITTER_ACCESS_TOKEN_SECRET);
|
|
||||||
HomeTimelineChoreo.addInput("ConsumerKey", TWITTER_CONSUMER_KEY);
|
|
||||||
HomeTimelineChoreo.addInput("ConsumerSecret", TWITTER_CONSUMER_SECRET);
|
|
||||||
|
|
||||||
// next, we'll define two output filters that let us specify the
|
|
||||||
// elements of the response from Twitter that we want to receive.
|
|
||||||
// see the examples at http://www.temboo.com/arduino
|
|
||||||
// for more on using output filters
|
|
||||||
|
|
||||||
// we want the text of the tweet
|
|
||||||
HomeTimelineChoreo.addOutputFilter("tweet", "/[1]/text", "Response");
|
|
||||||
|
|
||||||
// and the name of the author
|
|
||||||
HomeTimelineChoreo.addOutputFilter("author", "/[1]/user/screen_name", "Response");
|
|
||||||
|
|
||||||
|
|
||||||
// tell the Process to run and wait for the results. The
|
|
||||||
// return code will tell us whether the Temboo client
|
|
||||||
// was able to send our request to the Temboo servers
|
|
||||||
unsigned int returnCode = HomeTimelineChoreo.run();
|
|
||||||
|
|
||||||
// a response code of 0 means success; print the API response
|
|
||||||
if(returnCode == 0) {
|
|
||||||
|
|
||||||
String author; // a String to hold the tweet author's name
|
|
||||||
String tweet; // a String to hold the text of the tweet
|
|
||||||
|
|
||||||
|
|
||||||
// choreo outputs are returned as key/value pairs, delimited with
|
|
||||||
// newlines and record/field terminator characters, for example:
|
|
||||||
// Name1\n\x1F
|
|
||||||
// Value1\n\x1E
|
|
||||||
// Name2\n\x1F
|
|
||||||
// Value2\n\x1E
|
|
||||||
|
|
||||||
// see the examples at http://www.temboo.com/arduino for more details
|
|
||||||
// we can read this format into separate variables, as follows:
|
|
||||||
|
|
||||||
while(HomeTimelineChoreo.available()) {
|
|
||||||
// read the name of the output item
|
|
||||||
String name = HomeTimelineChoreo.readStringUntil('\x1F');
|
|
||||||
name.trim();
|
|
||||||
|
|
||||||
// read the value of the output item
|
|
||||||
String data = HomeTimelineChoreo.readStringUntil('\x1E');
|
|
||||||
data.trim();
|
|
||||||
|
|
||||||
// assign the value to the appropriate String
|
|
||||||
if (name == "tweet") {
|
|
||||||
tweet = data;
|
|
||||||
} else if (name == "author") {
|
|
||||||
author = data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.println("@" + author + " - " + tweet);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// there was an error
|
|
||||||
// print the raw output from the choreo
|
|
||||||
while(HomeTimelineChoreo.available()) {
|
|
||||||
char c = HomeTimelineChoreo.read();
|
|
||||||
Serial.print(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
HomeTimelineChoreo.close();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.println("Waiting...");
|
|
||||||
delay(90000); // wait 90 seconds between HomeTimeline calls
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
IMPORTANT NOTE: TembooAccount.h:
|
|
||||||
|
|
||||||
TembooAccount.h is a file referenced by this sketch that contains your Temboo account information.
|
|
||||||
You'll need to edit the placeholder version of TembooAccount.h included with this example sketch,
|
|
||||||
by inserting your own Temboo account name and app key information. The contents of the file should
|
|
||||||
look like:
|
|
||||||
|
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
||||||
You can find your Temboo App Key information on the Temboo website,
|
|
||||||
under My Account > Application Keys
|
|
||||||
|
|
||||||
The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
|
|
||||||
|
|
||||||
Keeping your account information in a separate file means you can share the main .ino file without worrying
|
|
||||||
that you forgot to delete your credentials.
|
|
||||||
*/
|
|
|
@ -1,4 +0,0 @@
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
|
@ -1,138 +0,0 @@
|
||||||
/*
|
|
||||||
SendATweet
|
|
||||||
|
|
||||||
Demonstrates sending a tweet via a Twitter account using Temboo from an Arduino Yun.
|
|
||||||
|
|
||||||
Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
|
|
||||||
|
|
||||||
A Temboo account and application key are necessary to run all Temboo examples.
|
|
||||||
If you don't already have one, you can register for a free Temboo account at
|
|
||||||
http://www.temboo.com
|
|
||||||
|
|
||||||
In order to run this sketch, you'll need to register an application using
|
|
||||||
the Twitter dev console at https://dev.twitter.com. Note that since this
|
|
||||||
sketch creates a new tweet, your application will need to be configured with
|
|
||||||
read+write permissions. After creating the app, you'll find OAuth credentials
|
|
||||||
for that application under the "OAuth Tool" tab. Substitute these values for
|
|
||||||
the placeholders below.
|
|
||||||
|
|
||||||
This example assumes basic familiarity with Arduino sketches, and that your Yun is connected
|
|
||||||
to the Internet.
|
|
||||||
|
|
||||||
Want to use another social API with your Arduino Yun? We've got Facebook,
|
|
||||||
Google+, Instagram, Tumblr and more in our Library!
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
#include <Temboo.h>
|
|
||||||
#include "TembooAccount.h" // contains Temboo account information
|
|
||||||
// as described in the footer comment below
|
|
||||||
|
|
||||||
|
|
||||||
/*** SUBSTITUTE YOUR VALUES BELOW: ***/
|
|
||||||
|
|
||||||
// Note that for additional security and reusability, you could
|
|
||||||
// use #define statements to specify these values in a .h file.
|
|
||||||
const String TWITTER_ACCESS_TOKEN = "your-twitter-access-token";
|
|
||||||
const String TWITTER_ACCESS_TOKEN_SECRET = "your-twitter-access-token-secret";
|
|
||||||
const String TWITTER_CONSUMER_KEY = "your-twitter-consumer-key";
|
|
||||||
const String TWITTER_CONSUMER_SECRET = "your-twitter-consumer-secret";
|
|
||||||
|
|
||||||
int numRuns = 1; // execution count, so this sketch doesn't run forever
|
|
||||||
int maxRuns = 3; // the max number of times the Twitter Update Choreo should run
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(9600);
|
|
||||||
|
|
||||||
// for debugging, wait until a serial console is connected
|
|
||||||
delay(4000);
|
|
||||||
while(!Serial);
|
|
||||||
|
|
||||||
Bridge.begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
// only try to send the tweet if we haven't already sent it successfully
|
|
||||||
if (numRuns <= maxRuns) {
|
|
||||||
|
|
||||||
Serial.println("Running SendATweet - Run #" + String(numRuns++) + "...");
|
|
||||||
|
|
||||||
// define the text of the tweet we want to send
|
|
||||||
String tweetText("My Arduino Yun has been running for " + String(millis()) + " milliseconds.");
|
|
||||||
|
|
||||||
|
|
||||||
TembooChoreo StatusesUpdateChoreo;
|
|
||||||
|
|
||||||
// invoke the Temboo client
|
|
||||||
// NOTE that the client must be reinvoked, and repopulated with
|
|
||||||
// appropriate arguments, each time its run() method is called.
|
|
||||||
StatusesUpdateChoreo.begin();
|
|
||||||
|
|
||||||
// set Temboo account credentials
|
|
||||||
StatusesUpdateChoreo.setAccountName(TEMBOO_ACCOUNT);
|
|
||||||
StatusesUpdateChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
|
|
||||||
StatusesUpdateChoreo.setAppKey(TEMBOO_APP_KEY);
|
|
||||||
|
|
||||||
// identify the Temboo Library choreo to run (Twitter > Tweets > StatusesUpdate)
|
|
||||||
StatusesUpdateChoreo.setChoreo("/Library/Twitter/Tweets/StatusesUpdate");
|
|
||||||
|
|
||||||
// set the required choreo inputs
|
|
||||||
// see https://www.temboo.com/library/Library/Twitter/Tweets/StatusesUpdate/
|
|
||||||
// for complete details about the inputs for this Choreo
|
|
||||||
|
|
||||||
// add the Twitter account information
|
|
||||||
StatusesUpdateChoreo.addInput("AccessToken", TWITTER_ACCESS_TOKEN);
|
|
||||||
StatusesUpdateChoreo.addInput("AccessTokenSecret", TWITTER_ACCESS_TOKEN_SECRET);
|
|
||||||
StatusesUpdateChoreo.addInput("ConsumerKey", TWITTER_CONSUMER_KEY);
|
|
||||||
StatusesUpdateChoreo.addInput("ConsumerSecret", TWITTER_CONSUMER_SECRET);
|
|
||||||
|
|
||||||
// and the tweet we want to send
|
|
||||||
StatusesUpdateChoreo.addInput("StatusUpdate", tweetText);
|
|
||||||
|
|
||||||
// tell the Process to run and wait for the results. The
|
|
||||||
// return code (returnCode) will tell us whether the Temboo client
|
|
||||||
// was able to send our request to the Temboo servers
|
|
||||||
unsigned int returnCode = StatusesUpdateChoreo.run();
|
|
||||||
|
|
||||||
// a return code of zero (0) means everything worked
|
|
||||||
if (returnCode == 0) {
|
|
||||||
Serial.println("Success! Tweet sent!");
|
|
||||||
} else {
|
|
||||||
// a non-zero return code means there was an error
|
|
||||||
// read and print the error message
|
|
||||||
while (StatusesUpdateChoreo.available()) {
|
|
||||||
char c = StatusesUpdateChoreo.read();
|
|
||||||
Serial.print(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
StatusesUpdateChoreo.close();
|
|
||||||
|
|
||||||
// do nothing for the next 90 seconds
|
|
||||||
Serial.println("Waiting...");
|
|
||||||
delay(90000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
IMPORTANT NOTE: TembooAccount.h:
|
|
||||||
|
|
||||||
TembooAccount.h is a file referenced by this sketch that contains your Temboo account information.
|
|
||||||
You'll need to edit the placeholder version of TembooAccount.h included with this example sketch,
|
|
||||||
by inserting your own Temboo account name and app key information. The contents of the file should
|
|
||||||
look like:
|
|
||||||
|
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
||||||
You can find your Temboo App Key information on the Temboo website,
|
|
||||||
under My Account > Application Keys
|
|
||||||
|
|
||||||
The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
|
|
||||||
|
|
||||||
Keeping your account information in a separate file means you can share the main .ino file without worrying
|
|
||||||
that you forgot to delete your credentials.
|
|
||||||
*/
|
|
|
@ -1,4 +0,0 @@
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
|
@ -1,137 +0,0 @@
|
||||||
/*
|
|
||||||
SendAnEmail
|
|
||||||
|
|
||||||
Demonstrates sending an email via a Google Gmail account using Temboo from an Arduino Yun.
|
|
||||||
|
|
||||||
Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
|
|
||||||
|
|
||||||
A Temboo account and application key are necessary to run all Temboo examples.
|
|
||||||
If you don't already have one, you can register for a free Temboo account at
|
|
||||||
http://www.temboo.com
|
|
||||||
|
|
||||||
Since this sketch uses Gmail to send the email, you'll also need a valid
|
|
||||||
Google Gmail account. The sketch needs the username and password you use
|
|
||||||
to log into your Gmail account - substitute the placeholders below for these values.
|
|
||||||
|
|
||||||
This example assumes basic familiarity with Arduino sketches, and that your Yun is connected
|
|
||||||
to the Internet.
|
|
||||||
|
|
||||||
Looking for another API to use with your Arduino Yun? We've got over 100 in our Library!
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
#include <Temboo.h>
|
|
||||||
#include "TembooAccount.h" // contains Temboo account information
|
|
||||||
// as described in the footer comment below
|
|
||||||
|
|
||||||
/*** SUBSTITUTE YOUR VALUES BELOW: ***/
|
|
||||||
|
|
||||||
// Note that for additional security and reusability, you could
|
|
||||||
// use #define statements to specify these values in a .h file.
|
|
||||||
|
|
||||||
// your Gmail username, formatted as a complete email address, eg "bob.smith@gmail.com"
|
|
||||||
const String GMAIL_USER_NAME = "xxxxxxxxxx";
|
|
||||||
|
|
||||||
// your Gmail password
|
|
||||||
const String GMAIL_PASSWORD = "xxxxxxxxxx";
|
|
||||||
|
|
||||||
// the email address you want to send the email to, eg "jane.doe@temboo.com"
|
|
||||||
const String TO_EMAIL_ADDRESS = "xxxxxxxxxx";
|
|
||||||
|
|
||||||
// a flag to indicate whether we've tried to send the email yet or not
|
|
||||||
boolean attempted = false;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(9600);
|
|
||||||
|
|
||||||
// for debugging, wait until a serial console is connected
|
|
||||||
delay(4000);
|
|
||||||
while(!Serial);
|
|
||||||
|
|
||||||
Bridge.begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
// only try to send the email if we haven't already tried
|
|
||||||
if (!attempted) {
|
|
||||||
|
|
||||||
Serial.println("Running SendAnEmail...");
|
|
||||||
|
|
||||||
TembooChoreo SendEmailChoreo;
|
|
||||||
|
|
||||||
// invoke the Temboo client
|
|
||||||
// NOTE that the client must be reinvoked, and repopulated with
|
|
||||||
// appropriate arguments, each time its run() method is called.
|
|
||||||
SendEmailChoreo.begin();
|
|
||||||
|
|
||||||
// set Temboo account credentials
|
|
||||||
SendEmailChoreo.setAccountName(TEMBOO_ACCOUNT);
|
|
||||||
SendEmailChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
|
|
||||||
SendEmailChoreo.setAppKey(TEMBOO_APP_KEY);
|
|
||||||
|
|
||||||
// identify the Temboo Library choreo to run (Google > Gmail > SendEmail)
|
|
||||||
SendEmailChoreo.setChoreo("/Library/Google/Gmail/SendEmail");
|
|
||||||
|
|
||||||
|
|
||||||
// set the required choreo inputs
|
|
||||||
// see https://www.temboo.com/library/Library/Google/Gmail/SendEmail/
|
|
||||||
// for complete details about the inputs for this Choreo
|
|
||||||
|
|
||||||
// the first input is your Gmail email address.
|
|
||||||
SendEmailChoreo.addInput("Username", GMAIL_USER_NAME);
|
|
||||||
// next is your Gmail password.
|
|
||||||
SendEmailChoreo.addInput("Password", GMAIL_PASSWORD);
|
|
||||||
// who to send the email to
|
|
||||||
SendEmailChoreo.addInput("ToAddress", TO_EMAIL_ADDRESS);
|
|
||||||
// then a subject line
|
|
||||||
SendEmailChoreo.addInput("Subject", "ALERT: Greenhouse Temperature");
|
|
||||||
|
|
||||||
// next comes the message body, the main content of the email
|
|
||||||
SendEmailChoreo.addInput("MessageBody", "Hey! The greenhouse is too cold!");
|
|
||||||
|
|
||||||
// tell the Choreo to run and wait for the results. The
|
|
||||||
// return code (returnCode) will tell us whether the Temboo client
|
|
||||||
// was able to send our request to the Temboo servers
|
|
||||||
unsigned int returnCode = SendEmailChoreo.run();
|
|
||||||
|
|
||||||
// a return code of zero (0) means everything worked
|
|
||||||
if (returnCode == 0) {
|
|
||||||
Serial.println("Success! Email sent!");
|
|
||||||
} else {
|
|
||||||
// a non-zero return code means there was an error
|
|
||||||
// read and print the error message
|
|
||||||
while (SendEmailChoreo.available()) {
|
|
||||||
char c = SendEmailChoreo.read();
|
|
||||||
Serial.print(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
SendEmailChoreo.close();
|
|
||||||
|
|
||||||
// set the flag showing we've tried
|
|
||||||
attempted = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
IMPORTANT NOTE: TembooAccount.h:
|
|
||||||
|
|
||||||
TembooAccount.h is a file referenced by this sketch that contains your Temboo account information.
|
|
||||||
You'll need to edit the placeholder version of TembooAccount.h included with this example sketch,
|
|
||||||
by inserting your own Temboo account name and app key information. The contents of the file should
|
|
||||||
look like:
|
|
||||||
|
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
||||||
You can find your Temboo App Key information on the Temboo website,
|
|
||||||
under My Account > Application Keys
|
|
||||||
|
|
||||||
The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
|
|
||||||
|
|
||||||
Keeping your account information in a separate file means you can share the main .ino file without worrying
|
|
||||||
that you forgot to delete your credentials.
|
|
||||||
*/
|
|
|
@ -1,4 +0,0 @@
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
|
@ -1,154 +0,0 @@
|
||||||
/*
|
|
||||||
SendAnSMS
|
|
||||||
|
|
||||||
Demonstrates sending an SMS via Twilio using Temboo from an Arduino Yun.
|
|
||||||
|
|
||||||
Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
|
|
||||||
|
|
||||||
A Temboo account and application key are necessary to run all Temboo examples.
|
|
||||||
If you don't already have one, you can register for a free Temboo account at
|
|
||||||
http://www.temboo.com
|
|
||||||
|
|
||||||
Since this sketch uses Twilio to send the SMS, you'll also need a valid
|
|
||||||
Twilio account. You can create one for free at https://www.twilio.com.
|
|
||||||
|
|
||||||
The sketch needs your Twilio phone number, along with
|
|
||||||
the Account SID and Auth Token you get when you register with Twilio.
|
|
||||||
Make sure to use the Account SID and Auth Token from your Twilio Dashboard
|
|
||||||
(not your test credentials from the Dev Tools panel).
|
|
||||||
|
|
||||||
Also note that if you're using a free Twilio account, you'll need to verify
|
|
||||||
the phone number to which messages are being sent by going to twilio.com and following
|
|
||||||
the instructions under the "Numbers > Verified Caller IDs" tab (this restriction
|
|
||||||
doesn't apply if you have a paid Twilio account).
|
|
||||||
|
|
||||||
This example assumes basic familiarity with Arduino sketches, and that your Yun is connected
|
|
||||||
to the Internet.
|
|
||||||
|
|
||||||
Looking for another API to use with your Arduino Yun? We've got over 100 in our Library!
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
#include <Temboo.h>
|
|
||||||
#include "TembooAccount.h" // contains Temboo account information
|
|
||||||
// as described in the footer comment below
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*** SUBSTITUTE YOUR VALUES BELOW: ***/
|
|
||||||
|
|
||||||
// Note that for additional security and reusability, you could
|
|
||||||
// use #define statements to specify these values in a .h file.
|
|
||||||
|
|
||||||
// the Account SID from your Twilio account
|
|
||||||
const String TWILIO_ACCOUNT_SID = "xxxxxxxxxx";
|
|
||||||
|
|
||||||
// the Auth Token from your Twilio account
|
|
||||||
const String TWILIO_AUTH_TOKEN = "xxxxxxxxxx";
|
|
||||||
|
|
||||||
// your Twilio phone number, e.g., "+1 555-222-1212"
|
|
||||||
const String TWILIO_NUMBER = "xxxxxxxxxx";
|
|
||||||
|
|
||||||
// the number to which the SMS should be sent, e.g., "+1 555-222-1212"
|
|
||||||
const String RECIPIENT_NUMBER = "xxxxxxxxxx";
|
|
||||||
|
|
||||||
// a flag to indicate whether we've attempted to send the SMS yet or not
|
|
||||||
boolean attempted = false;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(9600);
|
|
||||||
|
|
||||||
// for debugging, wait until a serial console is connected
|
|
||||||
delay(4000);
|
|
||||||
while(!Serial);
|
|
||||||
|
|
||||||
Bridge.begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
// only try to send the SMS if we haven't already sent it successfully
|
|
||||||
if (!attempted) {
|
|
||||||
|
|
||||||
Serial.println("Running SendAnSMS...");
|
|
||||||
|
|
||||||
// we need a Process object to send a Choreo request to Temboo
|
|
||||||
TembooChoreo SendSMSChoreo;
|
|
||||||
|
|
||||||
// invoke the Temboo client
|
|
||||||
// NOTE that the client must be reinvoked and repopulated with
|
|
||||||
// appropriate arguments each time its run() method is called.
|
|
||||||
SendSMSChoreo.begin();
|
|
||||||
|
|
||||||
// set Temboo account credentials
|
|
||||||
SendSMSChoreo.setAccountName(TEMBOO_ACCOUNT);
|
|
||||||
SendSMSChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
|
|
||||||
SendSMSChoreo.setAppKey(TEMBOO_APP_KEY);
|
|
||||||
|
|
||||||
// identify the Temboo Library choreo to run (Twilio > SMSMessages > SendSMS)
|
|
||||||
SendSMSChoreo.setChoreo("/Library/Twilio/SMSMessages/SendSMS");
|
|
||||||
|
|
||||||
// set the required choreo inputs
|
|
||||||
// see https://www.temboo.com/library/Library/Twilio/SMSMessages/SendSMS/
|
|
||||||
// for complete details about the inputs for this Choreo
|
|
||||||
|
|
||||||
// the first input is a your AccountSID
|
|
||||||
SendSMSChoreo.addInput("AccountSID", TWILIO_ACCOUNT_SID);
|
|
||||||
|
|
||||||
// next is your Auth Token
|
|
||||||
SendSMSChoreo.addInput("AuthToken", TWILIO_AUTH_TOKEN);
|
|
||||||
|
|
||||||
// next is your Twilio phone number
|
|
||||||
SendSMSChoreo.addInput("From", TWILIO_NUMBER);
|
|
||||||
|
|
||||||
// next, what number to send the SMS to
|
|
||||||
SendSMSChoreo.addInput("To", RECIPIENT_NUMBER);
|
|
||||||
|
|
||||||
// finally, the text of the message to send
|
|
||||||
SendSMSChoreo.addInput("Body", "Hey, there! This is a message from your Arduino Yun!");
|
|
||||||
|
|
||||||
// tell the Process to run and wait for the results. The
|
|
||||||
// return code (returnCode) will tell us whether the Temboo client
|
|
||||||
// was able to send our request to the Temboo servers
|
|
||||||
unsigned int returnCode = SendSMSChoreo.run();
|
|
||||||
|
|
||||||
// a return code of zero (0) means everything worked
|
|
||||||
if (returnCode == 0) {
|
|
||||||
Serial.println("Success! SMS sent!");
|
|
||||||
} else {
|
|
||||||
// a non-zero return code means there was an error
|
|
||||||
// read and print the error message
|
|
||||||
while (SendSMSChoreo.available()) {
|
|
||||||
char c = SendSMSChoreo.read();
|
|
||||||
Serial.print(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
SendSMSChoreo.close();
|
|
||||||
|
|
||||||
// set the flag indicatine we've tried once.
|
|
||||||
attempted=true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
IMPORTANT NOTE: TembooAccount.h:
|
|
||||||
|
|
||||||
TembooAccount.h is a file referenced by this sketch that contains your Temboo account information.
|
|
||||||
You'll need to edit the placeholder version of TembooAccount.h included with this example sketch,
|
|
||||||
by inserting your own Temboo account name and app key information. The contents of the file should
|
|
||||||
look like:
|
|
||||||
|
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
||||||
You can find your Temboo App Key information on the Temboo website,
|
|
||||||
under My Account > Application Keys
|
|
||||||
|
|
||||||
The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
|
|
||||||
|
|
||||||
Keeping your account information in a separate file means you can share the main .ino file without worrying
|
|
||||||
that you forgot to delete your credentials.
|
|
||||||
*/
|
|
|
@ -1,5 +0,0 @@
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
||||||
|
|
|
@ -1,178 +0,0 @@
|
||||||
/*
|
|
||||||
SendDataToGoogleSpreadsheet
|
|
||||||
|
|
||||||
Demonstrates appending a row of data to a Google spreadsheet using Temboo from an Arduino Yun.
|
|
||||||
|
|
||||||
Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
|
|
||||||
|
|
||||||
A Temboo account and application key are necessary to run all Temboo examples.
|
|
||||||
If you don't already have one, you can register for a free Temboo account at
|
|
||||||
http://www.temboo.com
|
|
||||||
|
|
||||||
Since this sketch uses a Google spreadsheet, you'll also need a
|
|
||||||
Google account: substitute the placeholders below for your Google account values.
|
|
||||||
|
|
||||||
This example assumes basic familiarity with Arduino sketches, and that your
|
|
||||||
Yun is connected to the Internet.
|
|
||||||
|
|
||||||
The columns in your spreadsheet must have labels for the Choreo to
|
|
||||||
work properly. It doesn't matter what the column labels actually are,
|
|
||||||
but there must be text in the first row of each column. This example
|
|
||||||
assumes there are two columns. The first column is the time (in milliseconds)
|
|
||||||
that the row was appended, and the second column is a sensor value.
|
|
||||||
In other words, your spreadsheet should look like:
|
|
||||||
|
|
||||||
Time | Sensor Value |
|
|
||||||
------+-----------------
|
|
||||||
| |
|
|
||||||
|
|
||||||
NOTE that the first time you run this sketch, you may receive a warning from
|
|
||||||
Google, prompting you to authorize access from a 3rd party system.
|
|
||||||
|
|
||||||
Looking for another API to use with your Arduino Yun? We've got over 100 in our Library!
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
#include <Temboo.h>
|
|
||||||
#include "TembooAccount.h" // contains Temboo account information,
|
|
||||||
// as described in the footer comment below
|
|
||||||
|
|
||||||
|
|
||||||
/*** SUBSTITUTE YOUR VALUES BELOW: ***/
|
|
||||||
|
|
||||||
// Note that for additional security and reusability, you could
|
|
||||||
// use #define statements to specify these values in a .h file.
|
|
||||||
|
|
||||||
const String GOOGLE_USERNAME = "your-google-username";
|
|
||||||
const String GOOGLE_PASSWORD = "your-google-password";
|
|
||||||
|
|
||||||
// the title of the spreadsheet you want to send data to
|
|
||||||
// (Note that this must actually be the title of a Google spreadsheet
|
|
||||||
// that exists in your Google Drive/Docs account, and is configured
|
|
||||||
// as described above.)
|
|
||||||
const String SPREADSHEET_TITLE = "your-spreadsheet-title";
|
|
||||||
|
|
||||||
const unsigned long RUN_INTERVAL_MILLIS = 60000; // how often to run the Choreo (in milliseconds)
|
|
||||||
|
|
||||||
// the last time we ran the Choreo
|
|
||||||
// (initialized to 60 seconds ago so the
|
|
||||||
// Choreo is run immediately when we start up)
|
|
||||||
unsigned long lastRun = (unsigned long)-60000;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
|
|
||||||
// for debugging, wait until a serial console is connected
|
|
||||||
Serial.begin(9600);
|
|
||||||
delay(4000);
|
|
||||||
while(!Serial);
|
|
||||||
|
|
||||||
Serial.print("Initializing the bridge...");
|
|
||||||
Bridge.begin();
|
|
||||||
Serial.println("Done");
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
// get the number of milliseconds this sketch has been running
|
|
||||||
unsigned long now = millis();
|
|
||||||
|
|
||||||
// run again if it's been 60 seconds since we last ran
|
|
||||||
if (now - lastRun >= RUN_INTERVAL_MILLIS) {
|
|
||||||
|
|
||||||
// remember 'now' as the last time we ran the choreo
|
|
||||||
lastRun = now;
|
|
||||||
|
|
||||||
Serial.println("Getting sensor value...");
|
|
||||||
|
|
||||||
// get the value we want to append to our spreadsheet
|
|
||||||
unsigned long sensorValue = getSensorValue();
|
|
||||||
|
|
||||||
Serial.println("Appending value to spreadsheet...");
|
|
||||||
|
|
||||||
// we need a Process object to send a Choreo request to Temboo
|
|
||||||
TembooChoreo AppendRowChoreo;
|
|
||||||
|
|
||||||
// invoke the Temboo client
|
|
||||||
// NOTE that the client must be reinvoked and repopulated with
|
|
||||||
// appropriate arguments each time its run() method is called.
|
|
||||||
AppendRowChoreo.begin();
|
|
||||||
|
|
||||||
// set Temboo account credentials
|
|
||||||
AppendRowChoreo.setAccountName(TEMBOO_ACCOUNT);
|
|
||||||
AppendRowChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
|
|
||||||
AppendRowChoreo.setAppKey(TEMBOO_APP_KEY);
|
|
||||||
|
|
||||||
// identify the Temboo Library choreo to run (Google > Spreadsheets > AppendRow)
|
|
||||||
AppendRowChoreo.setChoreo("/Library/Google/Spreadsheets/AppendRow");
|
|
||||||
|
|
||||||
// set the required Choreo inputs
|
|
||||||
// see https://www.temboo.com/library/Library/Google/Spreadsheets/AppendRow/
|
|
||||||
// for complete details about the inputs for this Choreo
|
|
||||||
|
|
||||||
// your Google username (usually your email address)
|
|
||||||
AppendRowChoreo.addInput("Username", GOOGLE_USERNAME);
|
|
||||||
|
|
||||||
// your Google account password
|
|
||||||
AppendRowChoreo.addInput("Password", GOOGLE_PASSWORD);
|
|
||||||
|
|
||||||
// the title of the spreadsheet you want to append to
|
|
||||||
// NOTE: substitute your own value, retaining the "SpreadsheetTitle:" prefix.
|
|
||||||
AppendRowChoreo.addInput("SpreadsheetTitle", SPREADSHEET_TITLE);
|
|
||||||
|
|
||||||
// convert the time and sensor values to a comma separated string
|
|
||||||
String rowData(now);
|
|
||||||
rowData += ",";
|
|
||||||
rowData += sensorValue;
|
|
||||||
|
|
||||||
// add the RowData input item
|
|
||||||
AppendRowChoreo.addInput("RowData", rowData);
|
|
||||||
|
|
||||||
// run the Choreo and wait for the results
|
|
||||||
// The return code (returnCode) will indicate success or failure
|
|
||||||
unsigned int returnCode = AppendRowChoreo.run();
|
|
||||||
|
|
||||||
// return code of zero (0) means success
|
|
||||||
if (returnCode == 0) {
|
|
||||||
Serial.println("Success! Appended " + rowData);
|
|
||||||
Serial.println("");
|
|
||||||
} else {
|
|
||||||
// return code of anything other than zero means failure
|
|
||||||
// read and display any error messages
|
|
||||||
while (AppendRowChoreo.available()) {
|
|
||||||
char c = AppendRowChoreo.read();
|
|
||||||
Serial.print(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
AppendRowChoreo.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// this function simulates reading the value of a sensor
|
|
||||||
unsigned long getSensorValue() {
|
|
||||||
return analogRead(A0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
IMPORTANT NOTE: TembooAccount.h:
|
|
||||||
|
|
||||||
TembooAccount.h is a file referenced by this sketch that contains your Temboo account information.
|
|
||||||
You'll need to edit the placeholder version of TembooAccount.h included with this example sketch,
|
|
||||||
by inserting your own Temboo account name and app key information. The contents of the file should
|
|
||||||
look like:
|
|
||||||
|
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
||||||
You can find your Temboo App Key information on the Temboo website,
|
|
||||||
under My Account > Application Keys
|
|
||||||
|
|
||||||
The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
|
|
||||||
|
|
||||||
Keeping your account information in a separate file means you can share the main .ino file without worrying
|
|
||||||
that you forgot to delete your credentials.
|
|
||||||
*/
|
|
|
@ -1,5 +0,0 @@
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
||||||
|
|
|
@ -1,171 +0,0 @@
|
||||||
/*
|
|
||||||
ToxicFacilitiesSearch
|
|
||||||
|
|
||||||
Demonstrates making a request to the Envirofacts API using Temboo from an Arduino Yun.
|
|
||||||
This example retrieves the names and addresses of EPA-regulated facilities in the
|
|
||||||
Toxins Release Inventory (TRI) database within a given zip code.
|
|
||||||
|
|
||||||
Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
|
|
||||||
|
|
||||||
A Temboo account and application key are necessary to run all Temboo examples.
|
|
||||||
If you don't already have one, you can register for a free Temboo account at
|
|
||||||
http://www.temboo.com
|
|
||||||
|
|
||||||
This example assumes basic familiarity with Arduino sketches, and that your Yun is connected
|
|
||||||
to the Internet.
|
|
||||||
|
|
||||||
Looking for another API to use with your Arduino Yun? We've got over 100 in our Library!
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
#include <Temboo.h>
|
|
||||||
#include "TembooAccount.h" // contains Temboo account information
|
|
||||||
// as described in the footer comment below
|
|
||||||
|
|
||||||
// the zip code to search for toxin-emitting facilities
|
|
||||||
String US_ZIP_CODE = "11215";
|
|
||||||
|
|
||||||
int numRuns = 1; // execution count, so that this doesn't run forever
|
|
||||||
int maxRuns = 10; // max number of times the Envirofacts FacilitiesSearch Choreo should be run
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(9600);
|
|
||||||
|
|
||||||
// for debugging, wait until a serial console is connected
|
|
||||||
delay(4000);
|
|
||||||
while(!Serial);
|
|
||||||
Bridge.begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
// while we haven't reached the max number of runs...
|
|
||||||
if (numRuns <= maxRuns) {
|
|
||||||
|
|
||||||
// print status
|
|
||||||
Serial.println("Running ToxicFacilitiesSearch - Run #" + String(numRuns++) + "...");
|
|
||||||
|
|
||||||
// we need a Process object to send a Choreo request to Temboo
|
|
||||||
TembooChoreo FacilitiesSearchByZipChoreo;
|
|
||||||
|
|
||||||
// invoke the Temboo client
|
|
||||||
// NOTE that the client must be reinvoked and repopulated with
|
|
||||||
// appropriate arguments each time its run() method is called.
|
|
||||||
FacilitiesSearchByZipChoreo.begin();
|
|
||||||
|
|
||||||
// set Temboo account credentials
|
|
||||||
FacilitiesSearchByZipChoreo.setAccountName(TEMBOO_ACCOUNT);
|
|
||||||
FacilitiesSearchByZipChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
|
|
||||||
FacilitiesSearchByZipChoreo.setAppKey(TEMBOO_APP_KEY);
|
|
||||||
|
|
||||||
// identify the Temboo Library choreo to run (EnviroFacts > Toxins > FacilitiesSearchByZip)
|
|
||||||
FacilitiesSearchByZipChoreo.setChoreo("/Library/EnviroFacts/Toxins/FacilitiesSearchByZip");
|
|
||||||
|
|
||||||
// set choreo inputs; in this case, the US zip code for which to retrieve toxin release data
|
|
||||||
// the Temboo client provides standardized calls to 100+ cloud APIs
|
|
||||||
FacilitiesSearchByZipChoreo.addInput("Zip", US_ZIP_CODE);
|
|
||||||
|
|
||||||
// specify two output filters, to help simplify the Envirofacts API results.
|
|
||||||
// see the tutorials on using Temboo SDK output filters at http://www.temboo.com/arduino
|
|
||||||
FacilitiesSearchByZipChoreo.addOutputFilter("fac", "FACILITY_NAME", "Response");
|
|
||||||
|
|
||||||
FacilitiesSearchByZipChoreo.addOutputFilter("addr", "STREET_ADDRESS", "Response");
|
|
||||||
|
|
||||||
// run the choreo
|
|
||||||
unsigned int returnCode = FacilitiesSearchByZipChoreo.run();
|
|
||||||
if (returnCode == 0) {
|
|
||||||
String facilities;
|
|
||||||
String addresses;
|
|
||||||
|
|
||||||
// when the choreo results are available, process them.
|
|
||||||
// the output filters we specified will return comma delimited
|
|
||||||
// lists containing the name and street address of the facilities
|
|
||||||
// located in the specified zip code.
|
|
||||||
while(FacilitiesSearchByZipChoreo.available()) {
|
|
||||||
String name = FacilitiesSearchByZipChoreo.readStringUntil('\x1F');
|
|
||||||
name.trim();
|
|
||||||
|
|
||||||
String data = FacilitiesSearchByZipChoreo.readStringUntil('\x1E');
|
|
||||||
data.trim();
|
|
||||||
|
|
||||||
if (name == "fac") {
|
|
||||||
facilities = data;
|
|
||||||
} else if (name == "addr") {
|
|
||||||
addresses = data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
FacilitiesSearchByZipChoreo.close();
|
|
||||||
|
|
||||||
// parse the comma delimited lists of facilities to join the
|
|
||||||
// name with the address and print it to the serial monitor
|
|
||||||
if (facilities.length() > 0) {
|
|
||||||
int i = -1;
|
|
||||||
int facilityStart = 0;
|
|
||||||
int addressStart = 0;
|
|
||||||
String facility;
|
|
||||||
String address;
|
|
||||||
do {
|
|
||||||
i = facilities.indexOf(',', facilityStart);
|
|
||||||
if (i >= 0) {
|
|
||||||
facility = facilities.substring(facilityStart, i);
|
|
||||||
facilityStart = i + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
i = addresses.indexOf(',', addressStart);
|
|
||||||
if (i >= 0) {
|
|
||||||
address = addresses.substring(addressStart, i);
|
|
||||||
addressStart = i + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i >= 0) {
|
|
||||||
printResult(facility, address);
|
|
||||||
}
|
|
||||||
|
|
||||||
}while (i >= 0);
|
|
||||||
facility = facilities.substring(facilityStart);
|
|
||||||
address = addresses.substring(addressStart);
|
|
||||||
printResult(facility, address);
|
|
||||||
} else {
|
|
||||||
Serial.println("No facilities found in zip code " + US_ZIP_CODE);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
while(FacilitiesSearchByZipChoreo.available()) {
|
|
||||||
char c = FacilitiesSearchByZipChoreo.read();
|
|
||||||
Serial.print(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Serial.println("Waiting...");
|
|
||||||
Serial.println("");
|
|
||||||
delay(30000); // wait 30 seconds between calls
|
|
||||||
}
|
|
||||||
|
|
||||||
// a simple utility function, to output the facility name and address in the serial monitor.
|
|
||||||
void printResult(String facility, String address) {
|
|
||||||
Serial.print(facility);
|
|
||||||
Serial.print(" - ");
|
|
||||||
Serial.println(address);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
IMPORTANT NOTE: TembooAccount.h:
|
|
||||||
|
|
||||||
TembooAccount.h is a file referenced by this sketch that contains your Temboo account information.
|
|
||||||
You'll need to edit the placeholder version of TembooAccount.h included with this example sketch,
|
|
||||||
by inserting your own Temboo account name and app key information. The contents of the file should
|
|
||||||
look like:
|
|
||||||
|
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
||||||
You can find your Temboo App Key information on the Temboo website,
|
|
||||||
under My Account > Application Keys
|
|
||||||
|
|
||||||
The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
|
|
||||||
|
|
||||||
Keeping your account information in a separate file means you can share the main .ino file without worrying
|
|
||||||
that you forgot to delete your credentials.
|
|
||||||
*/
|
|
|
@ -1,5 +0,0 @@
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
||||||
|
|
|
@ -1,132 +0,0 @@
|
||||||
/*
|
|
||||||
UpdateFacebookStatus
|
|
||||||
|
|
||||||
Demonstrates sending a Facebook status update using Temboo from an Arduino Yun.
|
|
||||||
|
|
||||||
Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
|
|
||||||
|
|
||||||
A Temboo account and application key are necessary to run all Temboo examples.
|
|
||||||
If you don't already have one, you can register for a free Temboo account at
|
|
||||||
http://www.temboo.com
|
|
||||||
|
|
||||||
In order to run this sketch, you'll need to register an application using
|
|
||||||
the Facebook dev console at https://developers.facebook.com/apps -- after creating
|
|
||||||
the app, log in to Temboo and visit https://www.temboo.com/library/Library/Facebook/Publishing/SetStatus/
|
|
||||||
to use our OAuth Wizard (or OAuth Choreos) to obtain a Facebook access token.
|
|
||||||
Substitute your access token for the placeholder value of FACEBOOK_ACCESS_TOKEN below.
|
|
||||||
|
|
||||||
This example assumes basic familiarity with Arduino sketches, and that your Yun
|
|
||||||
is connected to the Internet.
|
|
||||||
|
|
||||||
Want to use another social API with your Arduino Yun? We've got Twitter, Google+,
|
|
||||||
Instagram, Tumblr and more in our Library!
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
#include <Temboo.h>
|
|
||||||
#include "TembooAccount.h" // contains Temboo account information,
|
|
||||||
// as described in the footer comment below
|
|
||||||
|
|
||||||
/*** SUBSTITUTE YOUR VALUES BELOW: ***/
|
|
||||||
|
|
||||||
// Note that for additional security and reusability, you could
|
|
||||||
// use a #define statement to specify this value in a .h file.
|
|
||||||
|
|
||||||
// the Facebook Access Token, which can be obtained using the Temboo OAuth Wizard or Choreos
|
|
||||||
const String FACEBOOK_ACCESS_TOKEN = "xxxxxxxxxx";
|
|
||||||
|
|
||||||
|
|
||||||
int numRuns = 1; // execution count, so this sketch doesn't run forever
|
|
||||||
int maxRuns = 10; // the max number of times the Facebook SetStatus Choreo should run
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(9600);
|
|
||||||
|
|
||||||
// For debugging, wait until a serial console is connected.
|
|
||||||
delay(4000);
|
|
||||||
while(!Serial);
|
|
||||||
Bridge.begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// while we haven't reached the max number of runs...
|
|
||||||
if (numRuns <= maxRuns) {
|
|
||||||
|
|
||||||
// print status
|
|
||||||
Serial.println("Running UpdateFacebookStatus - Run #" + String(numRuns++) + "...");
|
|
||||||
|
|
||||||
// Define the status message we want to post on Facebook; since Facebook
|
|
||||||
// doesn't allow duplicate status messages, we'll include a changing value.
|
|
||||||
String statusMsg = "My Arduino Yun has been running for " + String(millis()) + " milliseconds!";
|
|
||||||
|
|
||||||
// define the Process that will be used to call the "temboo" client
|
|
||||||
TembooChoreo SetStatusChoreo;
|
|
||||||
|
|
||||||
// invoke the Temboo client
|
|
||||||
// NOTE that the client must be reinvoked and repopulated with
|
|
||||||
// appropriate arguments each time its run() method is called.
|
|
||||||
SetStatusChoreo.begin();
|
|
||||||
|
|
||||||
// set Temboo account credentials
|
|
||||||
SetStatusChoreo.setAccountName(TEMBOO_ACCOUNT);
|
|
||||||
SetStatusChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
|
|
||||||
SetStatusChoreo.setAppKey(TEMBOO_APP_KEY);
|
|
||||||
|
|
||||||
// tell the Temboo client which Choreo to run (Facebook > Publishing > SetStatus)
|
|
||||||
SetStatusChoreo.setChoreo("/Library/Facebook/Publishing/SetStatus");
|
|
||||||
|
|
||||||
// set the required choreo inputs
|
|
||||||
// see https://www.temboo.com/library/Library/Facebook/Publishing/SetStatus/
|
|
||||||
// for complete details about the inputs for this Choreo
|
|
||||||
|
|
||||||
SetStatusChoreo.addInput("AccessToken", FACEBOOK_ACCESS_TOKEN);
|
|
||||||
SetStatusChoreo.addInput("Message", statusMsg);
|
|
||||||
|
|
||||||
|
|
||||||
// tell the Process to run and wait for the results. The
|
|
||||||
// return code (returnCode) will tell us whether the Temboo client
|
|
||||||
// was able to send our request to the Temboo servers
|
|
||||||
unsigned int returnCode = SetStatusChoreo.run();
|
|
||||||
|
|
||||||
// print the response code and API response.
|
|
||||||
Serial.println("Response code: " + String(returnCode));
|
|
||||||
|
|
||||||
// note that in this case, we're just printing the raw response from Facebook.
|
|
||||||
// see the examples on using Temboo SDK output filters at http://www.temboo.com/arduino
|
|
||||||
// for information on how to filter this data
|
|
||||||
while(SetStatusChoreo.available()) {
|
|
||||||
char c = SetStatusChoreo.read();
|
|
||||||
Serial.print(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
SetStatusChoreo.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.println("Waiting...");
|
|
||||||
Serial.println("");
|
|
||||||
|
|
||||||
delay(30000); // wait 30 seconds between SetStatus calls
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
IMPORTANT NOTE: TembooAccount.h:
|
|
||||||
|
|
||||||
TembooAccount.h is a file referenced by this sketch that contains your Temboo account information.
|
|
||||||
You'll need to edit the placeholder version of TembooAccount.h included with this example sketch,
|
|
||||||
by inserting your own Temboo account name and app key information. The contents of the file should
|
|
||||||
look like:
|
|
||||||
|
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
||||||
You can find your Temboo App Key information on the Temboo website,
|
|
||||||
under My Account > Application Keys
|
|
||||||
|
|
||||||
The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
|
|
||||||
|
|
||||||
Keeping your account information in a separate file means you can share the main .ino file without worrying
|
|
||||||
that you forgot to delete your credentials.
|
|
||||||
*/
|
|
|
@ -1,5 +0,0 @@
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
||||||
|
|
|
@ -1,208 +0,0 @@
|
||||||
/*
|
|
||||||
UploadToDropbox
|
|
||||||
|
|
||||||
Demonstrates uploading a file to a Dropbox account using Temboo from an Arduino Yun.
|
|
||||||
|
|
||||||
Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
|
|
||||||
|
|
||||||
A Temboo account and application key are necessary to run all Temboo examples.
|
|
||||||
If you don't already have one, you can register for a free Temboo account at
|
|
||||||
http://www.temboo.com
|
|
||||||
|
|
||||||
You'll also need a valid Dropbox app and accompanying OAuth credentials.
|
|
||||||
To create a Dropbox app, visit https://www.dropbox.com/developers/apps and
|
|
||||||
do the following:
|
|
||||||
|
|
||||||
1. Create a "Dropbox API app"
|
|
||||||
2. Select "Files and datastores"
|
|
||||||
3. Select "Yes - my app only needs access to the files it creates."
|
|
||||||
|
|
||||||
Once you've created your app, follow the instructions at
|
|
||||||
https://www.temboo.com/library/Library/Dropbox/OAuth/ to run the Initialize and Finalize
|
|
||||||
OAuth Choreos. These Choreos complete the OAuth handshake and retrieve your Dropbox OAuth access tokens.
|
|
||||||
|
|
||||||
This example assumes basic familiarity with Arduino sketches, and that your Yun is connected
|
|
||||||
to the Internet.
|
|
||||||
|
|
||||||
Looking for another API to use with your Arduino Yun? We've got over 100 in our Library!
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Bridge.h>
|
|
||||||
#include <Temboo.h>
|
|
||||||
#include "TembooAccount.h" // contains Temboo account information
|
|
||||||
// as described in the footer comment below
|
|
||||||
|
|
||||||
|
|
||||||
/*** SUBSTITUTE YOUR VALUES BELOW: ***/
|
|
||||||
|
|
||||||
// Note that for additional security and reusability, you could
|
|
||||||
// use #define statements to specify these values in a .h file.
|
|
||||||
|
|
||||||
// your Dropbox app key, available on the Dropbox developer console after registering an app
|
|
||||||
const String DROPBOX_APP_KEY = "xxxxxxxxxx";
|
|
||||||
|
|
||||||
// your Dropbox app secret, available on the Dropbox developer console after registering an app
|
|
||||||
const String DROPBOX_APP_SECRET = "xxxxxxxxxx";
|
|
||||||
|
|
||||||
// your Dropbox access token, which is returned by the FinalizeOAuth Choreo
|
|
||||||
const String DROPBOX_ACCESS_TOKEN = "xxxxxxxxxx";
|
|
||||||
|
|
||||||
// your Dropbox access token secret, which is returned by the FinalizeOAuth Choreo
|
|
||||||
const String DROPBOX_ACCESS_TOKEN_SECRET = "xxxxxxxxxx";
|
|
||||||
|
|
||||||
|
|
||||||
boolean success = false; // a flag to indicate whether we've uploaded the file yet
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(9600);
|
|
||||||
|
|
||||||
// For debugging, wait until a serial console is connected.
|
|
||||||
delay(4000);
|
|
||||||
while(!Serial);
|
|
||||||
Bridge.begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
// only try to upload the file if we haven't already done so
|
|
||||||
if (!success) {
|
|
||||||
|
|
||||||
Serial.println("Base64 encoding data to upload...");
|
|
||||||
|
|
||||||
// base64 encode the data to upload
|
|
||||||
String base64EncodedData = base64Encode("Hello, Arduino!");
|
|
||||||
|
|
||||||
|
|
||||||
Serial.println("Uploading data to Dropbox...");
|
|
||||||
|
|
||||||
// we need a Process object to send a Choreo request to Temboo
|
|
||||||
TembooChoreo UploadFileChoreo;
|
|
||||||
|
|
||||||
// invoke the Temboo client
|
|
||||||
// NOTE that the client must be reinvoked and repopulated with
|
|
||||||
// appropriate arguments each time its run() method is called.
|
|
||||||
UploadFileChoreo.begin();
|
|
||||||
|
|
||||||
// set Temboo account credentials
|
|
||||||
UploadFileChoreo.setAccountName(TEMBOO_ACCOUNT);
|
|
||||||
UploadFileChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
|
|
||||||
UploadFileChoreo.setAppKey(TEMBOO_APP_KEY);
|
|
||||||
|
|
||||||
// identify the Temboo Library choreo to run (Dropbox > FilesAndMetadata > UploadFile)
|
|
||||||
UploadFileChoreo.setChoreo("/Library/Dropbox/FilesAndMetadata/UploadFile");
|
|
||||||
|
|
||||||
// set the required choreo inputs
|
|
||||||
// see https://www.temboo.com/library/Library/Dropbox/FilesAndMetadata/UploadFile/
|
|
||||||
// for complete details about the inputs for this Choreo
|
|
||||||
|
|
||||||
// first specify the name of the file to create/update on Dropbox
|
|
||||||
UploadFileChoreo.addInput("FileName", "ArduinoTest.txt");
|
|
||||||
|
|
||||||
// next, the root folder on Dropbox relative to which the file path is specified.
|
|
||||||
// to work with the Dropbox app you created earlier, this should be left as "sandbox"
|
|
||||||
// if your Dropbox app has full access to your files, specify "dropbox"
|
|
||||||
UploadFileChoreo.addInput("Root","sandbox");
|
|
||||||
|
|
||||||
// next, the Base64 encoded file data to upload
|
|
||||||
UploadFileChoreo.addInput("FileContents", base64EncodedData);
|
|
||||||
|
|
||||||
// finally, the Dropbox OAuth credentials defined above
|
|
||||||
UploadFileChoreo.addInput("AppSecret", DROPBOX_APP_SECRET);
|
|
||||||
UploadFileChoreo.addInput("AccessToken", DROPBOX_ACCESS_TOKEN);
|
|
||||||
UploadFileChoreo.addInput("AccessTokenSecret", DROPBOX_ACCESS_TOKEN_SECRET);
|
|
||||||
UploadFileChoreo.addInput("AppKey", DROPBOX_APP_KEY);
|
|
||||||
|
|
||||||
// tell the Process to run and wait for the results. The
|
|
||||||
// return code (returnCode) will tell us whether the Temboo client
|
|
||||||
// was able to send our request to the Temboo servers
|
|
||||||
unsigned int returnCode = UploadFileChoreo.run();
|
|
||||||
|
|
||||||
// a return code of zero (0) means everything worked
|
|
||||||
if (returnCode == 0) {
|
|
||||||
Serial.println("Success! File uploaded!");
|
|
||||||
success = true;
|
|
||||||
} else {
|
|
||||||
// a non-zero return code means there was an error
|
|
||||||
Serial.println("Uh-oh! Something went wrong!");
|
|
||||||
}
|
|
||||||
|
|
||||||
// print out the full response to the serial monitor in all
|
|
||||||
// cases, just for debugging
|
|
||||||
while (UploadFileChoreo.available()) {
|
|
||||||
char c = UploadFileChoreo.read();
|
|
||||||
Serial.print(c);
|
|
||||||
}
|
|
||||||
UploadFileChoreo.close();
|
|
||||||
|
|
||||||
Serial.println("Waiting...");
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(30000); // wait 30 seconds between upload attempts
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
A utility function to Base64 encode the specified string
|
|
||||||
by calling a Temboo Utilities Choreo.
|
|
||||||
*/
|
|
||||||
String base64Encode(String toEncode) {
|
|
||||||
|
|
||||||
// we need a Process object to send a Choreo request to Temboo
|
|
||||||
TembooChoreo Base64EncodeChoreo;
|
|
||||||
|
|
||||||
// invoke the Temboo client
|
|
||||||
Base64EncodeChoreo.begin();
|
|
||||||
|
|
||||||
// set Temboo account credentials
|
|
||||||
Base64EncodeChoreo.setAccountName(TEMBOO_ACCOUNT);
|
|
||||||
Base64EncodeChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
|
|
||||||
Base64EncodeChoreo.setAppKey(TEMBOO_APP_KEY);
|
|
||||||
|
|
||||||
// identify the Temboo Library choreo to run (Utilities > Encoding > Base64Encode)
|
|
||||||
Base64EncodeChoreo.setChoreo("/Library/Utilities/Encoding/Base64Encode");
|
|
||||||
|
|
||||||
// set choreo inputs
|
|
||||||
Base64EncodeChoreo.addInput("Text", toEncode);
|
|
||||||
|
|
||||||
// run the choreo
|
|
||||||
Base64EncodeChoreo.run();
|
|
||||||
|
|
||||||
// read in the choreo results, and return the "Base64EncodedText" output value.
|
|
||||||
// see http://www.temboo.com/arduino for more details on using choreo outputs.
|
|
||||||
while(Base64EncodeChoreo.available()) {
|
|
||||||
// read the name of the output item
|
|
||||||
String name = Base64EncodeChoreo.readStringUntil('\x1F');
|
|
||||||
name.trim();
|
|
||||||
|
|
||||||
// read the value of the output item
|
|
||||||
String data = Base64EncodeChoreo.readStringUntil('\x1E');
|
|
||||||
data.trim();
|
|
||||||
|
|
||||||
if(name == "Base64EncodedText") {
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
IMPORTANT NOTE: TembooAccount.h:
|
|
||||||
|
|
||||||
TembooAccount.h is a file referenced by this sketch that contains your Temboo account information.
|
|
||||||
You'll need to edit the placeholder version of TembooAccount.h included with this example sketch,
|
|
||||||
by inserting your own Temboo account name and app key information. The contents of the file should
|
|
||||||
look like:
|
|
||||||
|
|
||||||
#define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
|
|
||||||
#define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
|
|
||||||
#define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
|
|
||||||
|
|
||||||
You can find your Temboo App Key information on the Temboo website,
|
|
||||||
under My Account > Application Keys
|
|
||||||
|
|
||||||
The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
|
|
||||||
|
|
||||||
Keeping your account information in a separate file means you can share the main .ino file without worrying
|
|
||||||
that you forgot to delete your credentials.
|
|
||||||
*/
|
|
|
@ -1,121 +0,0 @@
|
||||||
/*
|
|
||||||
Temperature web interface
|
|
||||||
|
|
||||||
This example shows how to serve data from an analog input
|
|
||||||
via the Arduino Yún's built-in webserver using the Bridge library.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
* TMP36 temperature sensor on analog pin A1
|
|
||||||
* SD card attached to SD card slot of the Arduino Yún
|
|
||||||
|
|
||||||
Prepare your SD card with an empty folder in the SD root
|
|
||||||
named "arduino" and a subfolder of that named "www".
|
|
||||||
This will ensure that the Yún will create a link
|
|
||||||
to the SD to the "/mnt/sd" path.
|
|
||||||
|
|
||||||
In this sketch folder is a basic webpage and a copy of zepto.js, a
|
|
||||||
minimized version of jQuery. When you upload your sketch, these files
|
|
||||||
will be placed in the /arduino/www/TemperatureWebPanel folder on your SD card.
|
|
||||||
|
|
||||||
You can then go to http://arduino.local/sd/TemperatureWebPanel
|
|
||||||
to see the output of this sketch.
|
|
||||||
|
|
||||||
You can remove the SD card while the Linux and the
|
|
||||||
sketch are running but be careful not to remove it while
|
|
||||||
the system is writing to it.
|
|
||||||
|
|
||||||
created 6 July 2013
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
*/
|
|
||||||
#include <Bridge.h>
|
|
||||||
#include <YunServer.h>
|
|
||||||
#include <YunClient.h>
|
|
||||||
|
|
||||||
// Listen on default port 5555, the webserver on the Yun
|
|
||||||
// will forward there all the HTTP requests for us.
|
|
||||||
YunServer server;
|
|
||||||
String startString;
|
|
||||||
long hits = 0;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(9600);
|
|
||||||
|
|
||||||
// Bridge startup
|
|
||||||
pinMode(13,OUTPUT);
|
|
||||||
digitalWrite(13, LOW);
|
|
||||||
Bridge.begin();
|
|
||||||
digitalWrite(13, HIGH);
|
|
||||||
|
|
||||||
// using A0 and A2 as vcc and gnd for the TMP36 sensor:
|
|
||||||
pinMode(A0, OUTPUT);
|
|
||||||
pinMode(A2, OUTPUT);
|
|
||||||
digitalWrite(A0, HIGH);
|
|
||||||
digitalWrite(A2, LOW);
|
|
||||||
|
|
||||||
// Listen for incoming connection only from localhost
|
|
||||||
// (no one from the external network could connect)
|
|
||||||
server.listenOnLocalhost();
|
|
||||||
server.begin();
|
|
||||||
|
|
||||||
// get the time that this sketch started:
|
|
||||||
Process startTime;
|
|
||||||
startTime.runShellCommand("date");
|
|
||||||
while(startTime.available()) {
|
|
||||||
char c = startTime.read();
|
|
||||||
startString += c;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// Get clients coming from server
|
|
||||||
YunClient client = server.accept();
|
|
||||||
|
|
||||||
// There is a new client?
|
|
||||||
if (client) {
|
|
||||||
// read the command
|
|
||||||
String command = client.readString();
|
|
||||||
command.trim(); //kill whitespace
|
|
||||||
Serial.println(command);
|
|
||||||
// is "temperature" command?
|
|
||||||
if (command == "temperature") {
|
|
||||||
|
|
||||||
// get the time from the server:
|
|
||||||
Process time;
|
|
||||||
time.runShellCommand("date");
|
|
||||||
String timeString = "";
|
|
||||||
while(time.available()) {
|
|
||||||
char c = time.read();
|
|
||||||
timeString += c;
|
|
||||||
}
|
|
||||||
Serial.println(timeString);
|
|
||||||
int sensorValue = analogRead(A1);
|
|
||||||
// convert the reading to millivolts:
|
|
||||||
float voltage = sensorValue * (5000/ 1024);
|
|
||||||
// convert the millivolts to temperature celsius:
|
|
||||||
float temperature = (voltage - 500)/10;
|
|
||||||
// print the temperature:
|
|
||||||
client.print("Current time on the Yún: ");
|
|
||||||
client.println(timeString);
|
|
||||||
client.print("<br>Current temperature: ");
|
|
||||||
client.print(temperature);
|
|
||||||
client.print(" degrees C");
|
|
||||||
client.print("<br>This sketch has been running since ");
|
|
||||||
client.print(startString);
|
|
||||||
client.print("<br>Hits so far: ");
|
|
||||||
client.print(hits);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close connection and free resources.
|
|
||||||
client.stop();
|
|
||||||
hits++;
|
|
||||||
}
|
|
||||||
|
|
||||||
delay(50); // Poll every 50ms
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<script type="text/javascript" src="zepto.min.js"></script>
|
|
||||||
<script type="text/javascript">
|
|
||||||
function refresh() {
|
|
||||||
$('#content').load('/arduino/temperature');
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
</head>
|
|
||||||
<body onload="setInterval(refresh, 2000);">
|
|
||||||
<span id="content">0</span>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1,82 +0,0 @@
|
||||||
|
|
||||||
/*
|
|
||||||
Time Check
|
|
||||||
|
|
||||||
Gets the time from the linino processor via Bridge
|
|
||||||
then parses out hours, minutes and seconds for the Arduino
|
|
||||||
using an Arduino Yún.
|
|
||||||
|
|
||||||
created 27 May 2013
|
|
||||||
modified 21 June 2013
|
|
||||||
By Tom Igoe
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
#include <Process.h>
|
|
||||||
|
|
||||||
Process date; // process used to get the date
|
|
||||||
int hours, minutes, seconds; // for the results
|
|
||||||
int lastSecond = -1; // need an impossible value for comparison
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Bridge.begin(); // initialize Bridge
|
|
||||||
Serial.begin(9600); // initialize serial
|
|
||||||
|
|
||||||
while(!Serial); // wait for Serial Monitor to open
|
|
||||||
Serial.println("Time Check"); // Title of sketch
|
|
||||||
|
|
||||||
// run an initial date process. Should return:
|
|
||||||
// hh:mm:ss :
|
|
||||||
if (!date.running()) {
|
|
||||||
date.begin("date");
|
|
||||||
date.addParameter("+%T");
|
|
||||||
date.run();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
|
|
||||||
if(lastSecond != seconds) { // if a second has passed
|
|
||||||
// print the time:
|
|
||||||
if (hours <= 9) Serial.print("0"); // adjust for 0-9
|
|
||||||
Serial.print(hours);
|
|
||||||
Serial.print(":");
|
|
||||||
if (minutes <= 9) Serial.print("0"); // adjust for 0-9
|
|
||||||
Serial.print(minutes);
|
|
||||||
Serial.print(":");
|
|
||||||
if (seconds <= 9) Serial.print("0"); // adjust for 0-9
|
|
||||||
Serial.println(seconds);
|
|
||||||
|
|
||||||
// restart the date process:
|
|
||||||
if (!date.running()) {
|
|
||||||
date.begin("date");
|
|
||||||
date.addParameter("+%T");
|
|
||||||
date.run();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//if there's a result from the date process, parse it:
|
|
||||||
while (date.available()>0) {
|
|
||||||
// get the result of the date process (should be hh:mm:ss):
|
|
||||||
String timeString = date.readString();
|
|
||||||
|
|
||||||
// find the colons:
|
|
||||||
int firstColon = timeString.indexOf(":");
|
|
||||||
int secondColon= timeString.lastIndexOf(":");
|
|
||||||
|
|
||||||
// get the substrings for hour, minute second:
|
|
||||||
String hourString = timeString.substring(0, firstColon);
|
|
||||||
String minString = timeString.substring(firstColon+1, secondColon);
|
|
||||||
String secString = timeString.substring(secondColon+1);
|
|
||||||
|
|
||||||
// convert to ints,saving the previous second:
|
|
||||||
hours = hourString.toInt();
|
|
||||||
minutes = minString.toInt();
|
|
||||||
lastSecond = seconds; // save to do a time comparison
|
|
||||||
seconds = secString.toInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,50 +0,0 @@
|
||||||
|
|
||||||
/*
|
|
||||||
WiFi Status
|
|
||||||
|
|
||||||
This sketch runs a script called "pretty-wifi-info.lua"
|
|
||||||
installed on your Yún in folder /usr/bin.
|
|
||||||
It prints information about the status of your wifi connection.
|
|
||||||
|
|
||||||
It uses Serial to print, so you need to connect your Yún to your
|
|
||||||
computer using a USB cable and select the appropriate port from
|
|
||||||
the Port menu
|
|
||||||
|
|
||||||
created 18 June 2013
|
|
||||||
By Federico Fissore
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Process.h>
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(9600); // initialize serial communication
|
|
||||||
while(!Serial); // do nothing until the serial monitor is opened
|
|
||||||
|
|
||||||
Serial.println("Starting bridge...\n");
|
|
||||||
pinMode(13,OUTPUT);
|
|
||||||
digitalWrite(13, LOW);
|
|
||||||
Bridge.begin(); // make contact with the linux processor
|
|
||||||
digitalWrite(13, HIGH); // Led on pin 13 turns on when the bridge is ready
|
|
||||||
|
|
||||||
delay(2000); // wait 2 seconds
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
Process wifiCheck; // initialize a new process
|
|
||||||
|
|
||||||
wifiCheck.runShellCommand("/usr/bin/pretty-wifi-info.lua"); // command you want to run
|
|
||||||
|
|
||||||
// while there's any characters coming back from the
|
|
||||||
// process, print them to the serial monitor:
|
|
||||||
while (wifiCheck.available() > 0) {
|
|
||||||
char c = wifiCheck.read();
|
|
||||||
Serial.print(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.println();
|
|
||||||
|
|
||||||
delay(5000);
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,110 +0,0 @@
|
||||||
/*
|
|
||||||
Xively sensor client with Strings
|
|
||||||
|
|
||||||
This sketch connects an analog sensor to Xively,
|
|
||||||
using an Arduino Yún.
|
|
||||||
|
|
||||||
created 15 March 2010
|
|
||||||
updated 27 May 2013
|
|
||||||
by Tom Igoe
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
// include all Libraries needed:
|
|
||||||
#include <Process.h>
|
|
||||||
#include "passwords.h" // contains my passwords, see below
|
|
||||||
|
|
||||||
/*
|
|
||||||
NOTE: passwords.h is not included with this repo because it contains my passwords.
|
|
||||||
You need to create it for your own version of this application. To do so, make
|
|
||||||
a new tab in Arduino, call it passwords.h, and include the following variables and constants:
|
|
||||||
|
|
||||||
#define APIKEY "foo" // replace your pachube api key here
|
|
||||||
#define FEEDID 0000 // replace your feed ID
|
|
||||||
#define USERAGENT "my-project" // user agent is the project name
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
// set up net client info:
|
|
||||||
const unsigned long postingInterval = 60000; //delay between updates to xively.com
|
|
||||||
unsigned long lastRequest = 0; // when you last made a request
|
|
||||||
String dataString = "";
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// start serial port:
|
|
||||||
Bridge.begin();
|
|
||||||
Serial.begin(9600);
|
|
||||||
|
|
||||||
while(!Serial); // wait for Network Serial to open
|
|
||||||
Serial.println("Xively client");
|
|
||||||
|
|
||||||
// Do a first update immediately
|
|
||||||
updateData();
|
|
||||||
sendData();
|
|
||||||
lastRequest = millis();
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// get a timestamp so you can calculate reading and sending intervals:
|
|
||||||
long now = millis();
|
|
||||||
|
|
||||||
// if the sending interval has passed since your
|
|
||||||
// last connection, then connect again and send data:
|
|
||||||
if (now - lastRequest >= postingInterval) {
|
|
||||||
updateData();
|
|
||||||
sendData();
|
|
||||||
lastRequest = now;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void updateData() {
|
|
||||||
// convert the readings to a String to send it:
|
|
||||||
dataString = "Temperature,";
|
|
||||||
dataString += random(10) + 20;
|
|
||||||
// add pressure:
|
|
||||||
dataString += "\nPressure,";
|
|
||||||
dataString += random(5) + 100;
|
|
||||||
}
|
|
||||||
|
|
||||||
// this method makes a HTTP connection to the server:
|
|
||||||
void sendData() {
|
|
||||||
// form the string for the API header parameter:
|
|
||||||
String apiString = "X-ApiKey: ";
|
|
||||||
apiString += APIKEY;
|
|
||||||
|
|
||||||
// form the string for the URL parameter:
|
|
||||||
String url = "https://api.xively.com/v2/feeds/";
|
|
||||||
url += FEEDID;
|
|
||||||
url += ".csv";
|
|
||||||
|
|
||||||
// Send the HTTP PUT request
|
|
||||||
|
|
||||||
// Is better to declare the Process here, so when the
|
|
||||||
// sendData function finishes the resources are immediately
|
|
||||||
// released. Declaring it global works too, BTW.
|
|
||||||
Process xively;
|
|
||||||
Serial.print("\n\nSending data... ");
|
|
||||||
xively.begin("curl");
|
|
||||||
xively.addParameter("-k");
|
|
||||||
xively.addParameter("--request");
|
|
||||||
xively.addParameter("PUT");
|
|
||||||
xively.addParameter("--data");
|
|
||||||
xively.addParameter(dataString);
|
|
||||||
xively.addParameter("--header");
|
|
||||||
xively.addParameter(apiString);
|
|
||||||
xively.addParameter(url);
|
|
||||||
xively.run();
|
|
||||||
Serial.println("done!");
|
|
||||||
|
|
||||||
// If there's incoming data from the net connection,
|
|
||||||
// send it out the Serial:
|
|
||||||
while (xively.available()>0) {
|
|
||||||
char c = xively.read();
|
|
||||||
Serial.write(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,78 +0,0 @@
|
||||||
/*
|
|
||||||
Arduino Yun USB-to-Serial
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
From the serial monitor the following commands can be issued:
|
|
||||||
|
|
||||||
'~' followed by '0' -> Set the UART speed to 57600 baud
|
|
||||||
'~' followed by '1' -> Set the UART speed to 115200 baud
|
|
||||||
'~' followed by '2' -> Set the UART speed to 250000 baud
|
|
||||||
'~' followed by '3' -> Set the UART speed to 500000 baud
|
|
||||||
'~' followeb by '~' -> Sends the bridge's shutdown command to
|
|
||||||
obtain the console.
|
|
||||||
|
|
||||||
The circuit:
|
|
||||||
* Arduino Yun
|
|
||||||
|
|
||||||
created March 2013
|
|
||||||
by Massimo Banzi
|
|
||||||
modified by Cristian Maglie
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
*/
|
|
||||||
|
|
||||||
long lininoBaud = 250000;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(115200); // open serial connection via USB-Serial
|
|
||||||
Serial1.begin(lininoBaud); // open serial connection to Linino
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean commandMode = false;
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
// copy from virtual serial line to uart and vice versa
|
|
||||||
if (Serial.available()) { // got anything from USB-Serial?
|
|
||||||
char c = (char)Serial.read(); // read from USB-serial
|
|
||||||
if (commandMode == false) { // if we aren't in command mode...
|
|
||||||
if (c == '~') { // Tilde '~' key pressed?
|
|
||||||
commandMode = true; // enter in command mode
|
|
||||||
} else {
|
|
||||||
Serial1.write(c); // otherwise write char to Linino
|
|
||||||
}
|
|
||||||
} else { // if we are in command mode...
|
|
||||||
if (c == '0') { // '0' key pressed?
|
|
||||||
Serial1.begin(57600); // set speed to 57600
|
|
||||||
Serial.println("Speed set to 57600");
|
|
||||||
} else if (c == '1') { // '1' key pressed?
|
|
||||||
Serial1.begin(115200); // set speed to 115200
|
|
||||||
Serial.println("Speed set to 115200");
|
|
||||||
} else if (c == '2') { // '2' key pressed?
|
|
||||||
Serial1.begin(250000); // set speed to 250000
|
|
||||||
Serial.println("Speed set to 250000");
|
|
||||||
} else if (c == '3') { // '3' key pressed?
|
|
||||||
Serial1.begin(500000); // set speed to 500000
|
|
||||||
Serial.println("Speed set to 500000");
|
|
||||||
} else if (c == '~') {
|
|
||||||
Serial1.write((uint8_t *)"\xff\0\0\x05XXXXX\x0d\xaf", 11);
|
|
||||||
Serial.println("Sending bridge's shutdown command");
|
|
||||||
} else { // any other key pressed?
|
|
||||||
Serial1.write('~'); // write '~' to Linino
|
|
||||||
Serial1.write(c); // write char to Linino
|
|
||||||
}
|
|
||||||
commandMode = false; // in all cases exit from command mode
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (Serial1.available()) { // got anything from Linino?
|
|
||||||
char c = (char)Serial1.read(); // read from Linino
|
|
||||||
Serial.write(c); // write to USB-serial
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,85 +0,0 @@
|
||||||
#######################################
|
|
||||||
# Syntax Coloring Map For Bridge
|
|
||||||
#######################################
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# Class (KEYWORD3)
|
|
||||||
#######################################
|
|
||||||
|
|
||||||
Bridge KEYWORD3
|
|
||||||
FileIO KEYWORD3
|
|
||||||
FileSystem KEYWORD3
|
|
||||||
Console KEYWORD3
|
|
||||||
Process KEYWORD3
|
|
||||||
Mailbox KEYWORD3
|
|
||||||
HttpClient KEYWORD3
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# Methods and Functions (KEYWORD2)
|
|
||||||
#######################################
|
|
||||||
|
|
||||||
# methods names in commond
|
|
||||||
begin KEYWORD2
|
|
||||||
end KEYWORD2
|
|
||||||
available KEYWORD2
|
|
||||||
read KEYWORD2
|
|
||||||
peek KEYWORD2
|
|
||||||
write KEYWORD2
|
|
||||||
flush KEYWORD2
|
|
||||||
bool KEYWORD2
|
|
||||||
|
|
||||||
# Bridge Class
|
|
||||||
transfer KEYWORD2
|
|
||||||
put KEYWORD2
|
|
||||||
get KEYWORD2
|
|
||||||
|
|
||||||
# Console Class
|
|
||||||
buffer KEYWORD2
|
|
||||||
noBuffer KEYWORD2
|
|
||||||
connected KEYWORD2
|
|
||||||
|
|
||||||
# FileIO Class
|
|
||||||
File KEYWORD2
|
|
||||||
seek KEYWORD2
|
|
||||||
position KEYWORD2
|
|
||||||
size KEYWORD2
|
|
||||||
close KEYWORD2
|
|
||||||
name KEYWORD2
|
|
||||||
isDirectory KEYWORD2
|
|
||||||
openNextFile KEYWORD2
|
|
||||||
rewindDirectory KEYWORD2
|
|
||||||
|
|
||||||
# Process Class
|
|
||||||
addParameter KEYWORD2
|
|
||||||
runAsynchronously KEYWORD2
|
|
||||||
run KEYWORD2
|
|
||||||
running KEYWORD2
|
|
||||||
exitValue KEYWORD2
|
|
||||||
runShellCommand KEYWORD2
|
|
||||||
runShellCommandAsynchronously KEYWORD2
|
|
||||||
|
|
||||||
# Mailbox Class
|
|
||||||
readMessage KEYWORD2
|
|
||||||
writeMessage KEYWORD2
|
|
||||||
writeJSON KEYWORD2
|
|
||||||
message Available KEYWORD2
|
|
||||||
|
|
||||||
# HttpClient Class
|
|
||||||
getAsynchronously KEYWORD2
|
|
||||||
ready KEYWORD2
|
|
||||||
getResult KEYWORD2
|
|
||||||
|
|
||||||
# YunServer Class
|
|
||||||
accept KEYWORD2
|
|
||||||
stop KEYWORD2
|
|
||||||
connect KEYWORD2
|
|
||||||
connected KEYWORD2
|
|
||||||
|
|
||||||
|
|
||||||
#######################################
|
|
||||||
# Constants (LITERAL1)
|
|
||||||
#######################################
|
|
||||||
|
|
||||||
FILE_READ LITERAL1
|
|
||||||
FILE_WRITE LITERAL1
|
|
||||||
FILE_APPEND LITERAL1
|
|
Loading…
Reference in New Issue