Ambient Library

Fork of AmbientLib by Takehiko Shimojima

Ambient library. It provides "set" function to set data to a packet and "send" function to send the packet to the Ambient server. It also provides "bulk_send" function to send multiple data. (Japanese: IoT用のクラウドサービス「Ambient」のデーター送信ライブラリーです。Ambientはマイコンから送られたセンサーデーターを受信し、蓄積し、可視化(グラフ化)します。http://ambidata.io)

Committer:
TakehikoShimojima
Date:
Sat May 07 01:11:27 2016 +0000
Revision:
0:d2972e5edb22
initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TakehikoShimojima 0:d2972e5edb22 1 #ifndef AMBIENT_H
TakehikoShimojima 0:d2972e5edb22 2 #define AMBIENT_H
TakehikoShimojima 0:d2972e5edb22 3
TakehikoShimojima 0:d2972e5edb22 4 #include "mbed.h"
TakehikoShimojima 0:d2972e5edb22 5 #include "TCPSocketConnection.h"
TakehikoShimojima 0:d2972e5edb22 6
TakehikoShimojima 0:d2972e5edb22 7 #define AMBIENT_WRITEKEY_SIZE 18
TakehikoShimojima 0:d2972e5edb22 8 #define AMBIENT_MAX_RETRY 5
TakehikoShimojima 0:d2972e5edb22 9 #define AMBIENT_DATA_SIZE 24
TakehikoShimojima 0:d2972e5edb22 10 #define AMBIENT_NUM_PARAMS 11
TakehikoShimojima 0:d2972e5edb22 11
TakehikoShimojima 0:d2972e5edb22 12 /** AMBIENT class
TakehikoShimojima 0:d2972e5edb22 13 * to send data to Ambient service.
TakehikoShimojima 0:d2972e5edb22 14 *
TakehikoShimojima 0:d2972e5edb22 15 * Exsample:
TakehikoShimojima 0:d2972e5edb22 16 * @code
TakehikoShimojima 0:d2972e5edb22 17 * #include "mbed.h"
TakehikoShimojima 0:d2972e5edb22 18 * #include "EthernetInterface.h"
TakehikoShimojima 0:d2972e5edb22 19 * #include "Ambient.h"
TakehikoShimojima 0:d2972e5edb22 20 * #include "HDC1000.h"
TakehikoShimojima 0:d2972e5edb22 21 *
TakehikoShimojima 0:d2972e5edb22 22 * unsigned int channelId = 100;
TakehikoShimojima 0:d2972e5edb22 23 * const char* writeKey = "ライトキー";
TakehikoShimojima 0:d2972e5edb22 24 * AMBIENT ambient;
TakehikoShimojima 0:d2972e5edb22 25 *
TakehikoShimojima 0:d2972e5edb22 26 * HDC1000 hdc1000(p9,p10);
TakehikoShimojima 0:d2972e5edb22 27 *
TakehikoShimojima 0:d2972e5edb22 28 * int main() {
TakehikoShimojima 0:d2972e5edb22 29 * printf("start\r\n");
TakehikoShimojima 0:d2972e5edb22 30 *
TakehikoShimojima 0:d2972e5edb22 31 * EthernetInterface eth;
TakehikoShimojima 0:d2972e5edb22 32 * eth.init(); //Use DHCP
TakehikoShimojima 0:d2972e5edb22 33 * eth.connect();
TakehikoShimojima 0:d2972e5edb22 34 *
TakehikoShimojima 0:d2972e5edb22 35 * TCPSocketConnection socket;
TakehikoShimojima 0:d2972e5edb22 36 * ambient.init(channelId, writeKey, &socket);
TakehikoShimojima 0:d2972e5edb22 37 *
TakehikoShimojima 0:d2972e5edb22 38 * printf("Ambient send to ch: %d\r\n", channelId);
TakehikoShimojima 0:d2972e5edb22 39 *
TakehikoShimojima 0:d2972e5edb22 40 * while (true) {
TakehikoShimojima 0:d2972e5edb22 41 * float temp, humid;
TakehikoShimojima 0:d2972e5edb22 42 * char tempbuf[12], humidbuf[12];
TakehikoShimojima 0:d2972e5edb22 43 *
TakehikoShimojima 0:d2972e5edb22 44 * hdc1000.get();
TakehikoShimojima 0:d2972e5edb22 45 * temp = hdc1000.temperature();
TakehikoShimojima 0:d2972e5edb22 46 * humid = hdc1000.humidity();
TakehikoShimojima 0:d2972e5edb22 47 *
TakehikoShimojima 0:d2972e5edb22 48 * sprintf(tempbuf, "%2.1f", temp);
TakehikoShimojima 0:d2972e5edb22 49 * ambient.set(1, tempbuf);
TakehikoShimojima 0:d2972e5edb22 50 * sprintf(humidbuf, "%2.0f", humid);
TakehikoShimojima 0:d2972e5edb22 51 * ambient.set(2, humidbuf);
TakehikoShimojima 0:d2972e5edb22 52 * printf("Temp: %s C, Humid: %s %%\r\n", tempbuf, humidbuf);
TakehikoShimojima 0:d2972e5edb22 53 *
TakehikoShimojima 0:d2972e5edb22 54 * ambient.send();
TakehikoShimojima 0:d2972e5edb22 55 *
TakehikoShimojima 0:d2972e5edb22 56 * wait(30.0);
TakehikoShimojima 0:d2972e5edb22 57 * }
TakehikoShimojima 0:d2972e5edb22 58 * }
TakehikoShimojima 0:d2972e5edb22 59 * @endcode
TakehikoShimojima 0:d2972e5edb22 60 */
TakehikoShimojima 0:d2972e5edb22 61 class AMBIENT
TakehikoShimojima 0:d2972e5edb22 62 {
TakehikoShimojima 0:d2972e5edb22 63 public:
TakehikoShimojima 0:d2972e5edb22 64 /** Create AMBIENT instance
TakehikoShimojima 0:d2972e5edb22 65 */
TakehikoShimojima 0:d2972e5edb22 66 AMBIENT(void);
TakehikoShimojima 0:d2972e5edb22 67
TakehikoShimojima 0:d2972e5edb22 68 /** Initialize the instance
TakehikoShimojima 0:d2972e5edb22 69 * @param channelId Initialize the Ambient instance with channelId.
TakehikoShimojima 0:d2972e5edb22 70 * @param writeKey and writeKey
TakehikoShimojima 0:d2972e5edb22 71 * @param s and pointer to socket
TakehikoShimojima 0:d2972e5edb22 72 * @returns
TakehikoShimojima 0:d2972e5edb22 73 * true on success,
TakehikoShimojima 0:d2972e5edb22 74 * false on error
TakehikoShimojima 0:d2972e5edb22 75 */
TakehikoShimojima 0:d2972e5edb22 76 bool init(unsigned int channelId, const char * writeKey, TCPSocketConnection * s, int dev = 0);
TakehikoShimojima 0:d2972e5edb22 77 /** Set data on field-th field of payload.
TakehikoShimojima 0:d2972e5edb22 78 * @param field index of payload (1 to 8)
TakehikoShimojima 0:d2972e5edb22 79 * @param data data
TakehikoShimojima 0:d2972e5edb22 80 * @returns
TakehikoShimojima 0:d2972e5edb22 81 * true on success,
TakehikoShimojima 0:d2972e5edb22 82 * false on error
TakehikoShimojima 0:d2972e5edb22 83 */
TakehikoShimojima 0:d2972e5edb22 84 bool set(int field, char * data);
TakehikoShimojima 0:d2972e5edb22 85 /** Clear data on field-th field of payload.
TakehikoShimojima 0:d2972e5edb22 86 * @param field index of payload (1 to 8)
TakehikoShimojima 0:d2972e5edb22 87 * @returns
TakehikoShimojima 0:d2972e5edb22 88 * true on success,
TakehikoShimojima 0:d2972e5edb22 89 * false on error
TakehikoShimojima 0:d2972e5edb22 90 */
TakehikoShimojima 0:d2972e5edb22 91 bool clear(int field);
TakehikoShimojima 0:d2972e5edb22 92
TakehikoShimojima 0:d2972e5edb22 93 /** Send data to Ambient
TakehikoShimojima 0:d2972e5edb22 94 */
TakehikoShimojima 0:d2972e5edb22 95 bool send(void);
TakehikoShimojima 0:d2972e5edb22 96
TakehikoShimojima 0:d2972e5edb22 97 private:
TakehikoShimojima 0:d2972e5edb22 98
TakehikoShimojima 0:d2972e5edb22 99 TCPSocketConnection * s;
TakehikoShimojima 0:d2972e5edb22 100 unsigned int channelId;
TakehikoShimojima 0:d2972e5edb22 101 char writeKey[AMBIENT_WRITEKEY_SIZE];
TakehikoShimojima 0:d2972e5edb22 102 int dev;
TakehikoShimojima 0:d2972e5edb22 103 char host[18];
TakehikoShimojima 0:d2972e5edb22 104 int port;
TakehikoShimojima 0:d2972e5edb22 105
TakehikoShimojima 0:d2972e5edb22 106 struct {
TakehikoShimojima 0:d2972e5edb22 107 int set;
TakehikoShimojima 0:d2972e5edb22 108 char item[AMBIENT_DATA_SIZE];
TakehikoShimojima 0:d2972e5edb22 109 } data[AMBIENT_NUM_PARAMS];
TakehikoShimojima 0:d2972e5edb22 110 };
TakehikoShimojima 0:d2972e5edb22 111
TakehikoShimojima 0:d2972e5edb22 112 #endif // AMBIENT_H