Skip to content

Commit 03da319

Browse files
committed
Initial commit with FONA support.
1 parent c673fb1 commit 03da319

File tree

5 files changed

+601
-5
lines changed

5 files changed

+601
-5
lines changed

Adafruit_IO_Arduino.cpp

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2015 Adafruit Industries
4+
// Author: Tony DiCola
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
#include "Adafruit_IO_Arduino.h"
24+
25+
FeedData::FeedData() {
26+
// Feed data is invalid without a value so write all zeros.
27+
memset(_value, 0, sizeof(_value));
28+
}
29+
30+
FeedData::FeedData(const FeedData& copy) {
31+
// Copy the value from the provided FeedData object.
32+
memset(_value, 0, sizeof(_value));
33+
strncpy(_value, copy._value, sizeof(_value)-1);
34+
}
35+
36+
FeedData::FeedData(const char* value) {
37+
// Copy the provided string value as the feed data value. If the string
38+
// can't fit into the feed data's memory then keep the value in an invalid
39+
// state (all zeros).
40+
memset(_value, 0, sizeof(_value));
41+
if (strlen(value) <= FEEDDATA_LENGTH-1) {
42+
strncpy(_value, value, sizeof(_value)-1);
43+
}
44+
}
45+
46+
FeedData::FeedData(Stream& stream, uint16_t length, uint32_t timeoutMS) {
47+
// Load the data value from the provided stream. Will read a value up to
48+
// length characters in size. If the data can't be read for some reason,
49+
// like the timeout exceeding, then the FeedData will be left in an invalid
50+
// state.
51+
memset(_value, 0, sizeof(_value));
52+
if (length > FEEDDATA_LENGTH-1) {
53+
// Not enough space to store the value.
54+
return;
55+
}
56+
stream.setTimeout(timeoutMS);
57+
if (stream.readBytes(_value, length) != length) {
58+
// Didn't find enough data, set the value as invalid.
59+
memset(_value, 0, sizeof(_value));
60+
}
61+
}
62+
63+
bool FeedData::intValue(int* value) {
64+
// Attempt to convert the value to an integer. Returns true if it succeeds,
65+
// and false if it fails for some reason.
66+
char* endptr;
67+
*value = (int)strtol(_value, &endptr, 10);
68+
return (*_value != 0 && *endptr == 0);
69+
}
70+
71+
bool FeedData::uintValue(unsigned int* value) {
72+
// Attempt to convert the value to an unsigned integer. Returns true if it
73+
// succeeds, and false if it fails for some reason.
74+
char* endptr;
75+
*value = (unsigned int)strtoul(_value, &endptr, 10);
76+
return (*_value != 0 && *endptr == 0);
77+
}
78+
79+
bool FeedData::longValue(long* value) {
80+
// Attempt to convert the value to a long. Returns true if it
81+
// succeeds, and false if it fails for some reason.
82+
char* endptr;
83+
*value = strtol(_value, &endptr, 10);
84+
return (*_value != 0 && *endptr == 0);
85+
}
86+
87+
bool FeedData::ulongValue(unsigned long* value) {
88+
// Attempt to convert the value to an unsigned long. Returns true if it
89+
// succeeds, and false if it fails for some reason.
90+
char* endptr;
91+
*value = strtoul(_value, &endptr, 10);
92+
return (*_value != 0 && *endptr == 0);
93+
}
94+
95+
bool FeedData::floatValue(float* value) {
96+
// Attempt to convert the value to a float. Returns true if it succeeds,
97+
// and false if it fails for some reason.
98+
char* endptr;
99+
*value = (float)strtod(_value, &endptr);
100+
return (*_value != 0 && *endptr == 0);
101+
}
102+
103+
bool FeedData::doubleValue(double* value) {
104+
// Attempt to convert the value to a double. Returns true if it succeeds,
105+
// and false if it fails for some reason.
106+
char* endptr;
107+
*value = strtod(_value, &endptr);
108+
return (*_value != 0 && *endptr == 0);
109+
}
110+
111+
// Buffer to store values converted from numbers to strings before sending to IO.
112+
static char _converted[FEEDDATA_LENGTH];
113+
114+
bool Adafruit_IO_Feed::send(int value) {
115+
// Convert int to string, then send the value (being careful not to quote it).
116+
memset(_converted, 0, sizeof(_converted));
117+
itoa(value, _converted, 10);
118+
return _adapter->send(_name, _converted, _key, false);
119+
}
120+
121+
bool Adafruit_IO_Feed::send(unsigned int value) {
122+
// Convert uint to string, then send the value (being careful not to quote it).
123+
memset(_converted, 0, sizeof(_converted));
124+
utoa(value, _converted, 10);
125+
return _adapter->send(_name, _converted, _key, false);
126+
}
127+
128+
bool Adafruit_IO_Feed::send(long value) {
129+
// Convert long to string, then send the value (being careful not to quote it).
130+
memset(_converted, 0, sizeof(_converted));
131+
ltoa(value, _converted, 10);
132+
return _adapter->send(_name, _converted, _key, false);
133+
}
134+
135+
bool Adafruit_IO_Feed::send(unsigned long value) {
136+
// Convert ulong to string, then send the value (being careful not to quote it).
137+
memset(_converted, 0, sizeof(_converted));
138+
ultoa(value, _converted, 10);
139+
return _adapter->send(_name, _converted, _key, false);
140+
}
141+
142+
bool Adafruit_IO_Feed::send(float value) {
143+
// Convert float to string using scientific notation, then send the value
144+
// (being careful not to quote it).
145+
memset(_converted, 0, sizeof(_converted));
146+
dtostre(value, _converted, 10, 0);
147+
return _adapter->send(_name, _converted, _key, false);
148+
}
149+
150+
bool Adafruit_IO_Feed::send(double value) {
151+
// Convert double to string using scientific notation, then send the value
152+
// (being careful not to quote it).
153+
memset(_converted, 0, sizeof(_converted));
154+
dtostre(value, _converted, 10, 0);
155+
return _adapter->send(_name, _converted, _key, false);
156+
}

Adafruit_IO_Arduino.h

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2015 Adafruit Industries
4+
// Author: Tony DiCola
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
#ifndef ADAFRUIT_IO_ARDUINO_H
24+
#define ADAFRUIT_IO_ARDUINO_H
25+
26+
#include "Arduino.h"
27+
28+
// Control how big the buffer is for storing a data instance's value.
29+
// Keep at least a value of ~33 to store large numeric and float values.
30+
#define FEEDDATA_LENGTH 33
31+
32+
// Uncomment/comment to enable & disable debug output.
33+
//#define ADAFRUIT_IO_DEBUG
34+
35+
// Macros for debug output (only enabled when debug mode is enabled.)
36+
#ifdef ADAFRUIT_IO_DEBUG
37+
#define DEBUG_PRINT(t) { Serial.print(t); }
38+
#define DEBUG_PRINTLN(t) { Serial.println(t); }
39+
#else
40+
#define DEBUG_PRINT(t) {}
41+
#define DEBUG_PRINTLN(t) {}
42+
#endif
43+
44+
45+
// Adafruit IO feed data instance. Exposes functions to read the value of the
46+
// feed as different types.
47+
class FeedData {
48+
public:
49+
FeedData();
50+
FeedData(const FeedData& copy);
51+
FeedData(const char* value);
52+
FeedData(Stream& stream, uint16_t length, uint32_t timeoutMS = 5000);
53+
54+
// Functions to support checking if this data instance was able to be read
55+
// from the AIO service.
56+
bool isValid() { return _value != NULL; }
57+
58+
// Allow implicit conversion to a C string.
59+
operator char*() { return _value; }
60+
61+
// Explicit conversion functions that convert to the specified value. Each
62+
// will return true if the conversion succeeded and false if it could not
63+
// be converted.
64+
bool intValue(int* value);
65+
bool uintValue(unsigned int* value);
66+
bool longValue(long* value);
67+
bool ulongValue(unsigned long* value);
68+
bool floatValue(float* value);
69+
bool doubleValue(double* value);
70+
71+
private:
72+
char _value[FEEDDATA_LENGTH];
73+
};
74+
75+
76+
// Interface for an AIO service that defines functions to send and receive data
77+
// to the AIO REST API. Concrete implementations for FONA, CC3000, and ESP8266
78+
// are what users will actually use.
79+
class AIOService {
80+
public:
81+
virtual ~AIOService() {}
82+
virtual bool send(const char* feed, const char* value, const char* key,
83+
bool quoted) = 0;
84+
virtual FeedData receive(const char* feed, const char* key) = 0;
85+
};
86+
87+
88+
// Main IO feed class that uses an AIO serviec reference to send and receive
89+
// data with IO.
90+
class Adafruit_IO_Feed {
91+
public:
92+
Adafruit_IO_Feed(const char* name, const char* key, AIOService* adapter):
93+
_name(name), _key(key), _adapter(adapter)
94+
{}
95+
96+
bool send(const char* value) {
97+
return _adapter->send(_name, value, _key, true);
98+
}
99+
bool send(int value);
100+
bool send(unsigned int value);
101+
bool send(long value);
102+
bool send(unsigned long value);
103+
bool send(float value);
104+
bool send(double value);
105+
106+
FeedData receive() {
107+
return _adapter->receive(_name, _key);
108+
}
109+
110+
private:
111+
const char* _name;
112+
const char* _key;
113+
AIOService* _adapter;
114+
};
115+
116+
117+
#endif

0 commit comments

Comments
 (0)