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)

Dependents:   AmbientExampleSITB AmbientHeartRateMonitor AmbientHeartBeat AmbientExampleSITB_ws ... more

Files at this revision

API Documentation at this revision

Comitter:
AmbientData
Date:
Mon Jun 13 12:03:41 2016 +0000
Parent:
2:a319af936fd5
Child:
4:fcbd652bbb7a
Commit message:
bulk_send() and delete_data() added

Changed in this revision

Ambient.cpp Show annotated file Show diff for this revision Revisions of this file
Ambient.h Show annotated file Show diff for this revision Revisions of this file
--- a/Ambient.cpp	Tue Jun 07 07:47:53 2016 +0000
+++ b/Ambient.cpp	Mon Jun 13 12:03:41 2016 +0000
@@ -20,8 +20,8 @@
 
 const char* AMBIENT_HOST = "54.65.206.59";
 const int AMBIENT_PORT = 80;
-const char* AMBIENT_HOST_DEV = "54.65.206.59";
-const int AMBIENT_PORT_DEV = 80;
+const char* AMBIENT_HOST_DEV = "192.168.0.6";
+const int AMBIENT_PORT_DEV = 4567;
 
 const char * ambient_keys[] = {"\"d1\":\"", "\"d2\":\"", "\"d3\":\"", "\"d4\":\"", "\"d5\":\"", "\"d6\":\"", "\"d7\":\"", "\"d8\":\"", "\"lat\":\"", "\"lng\":\"", "\"created\":\""};
 
@@ -99,13 +99,10 @@
         return false;
     }
 
-    char str[360] = {0};
-    char header[54] = {0};
-    char host[32] = {0};
-    char contentLen[28] = {0};
-    const char *contentType = "Content-Type: application/json\r\n\r\n";
-    char body[192] = {0};
+    char str[180];
+    char body[192];
 
+    memset(body, 0, sizeof(body));
     strcat(body, "{\"writeKey\":\"");
     strcat(body, this->writeKey);
     strcat(body, "\",");
@@ -118,17 +115,17 @@
         }
     }
     body[strlen(body) - 1] = '\0';
-
     strcat(body, "}\r\n");
 
-    sprintf(header, "POST /api/v2/channels/%d/data HTTP/1.1\r\n", this->channelId);
+    memset(str, 0, sizeof(str));
+    sprintf(str, "POST /api/v2/channels/%d/data HTTP/1.1\r\n", this->channelId);
     if (this->port == 80) {
-        sprintf(host, "Host: %s\r\n", this->host);
+        sprintf(&str[strlen(str)], "Host: %s\r\n", this->host);
     } else {
-        sprintf(host, "Host: %s:%d\r\n", this->host, this->port);
+        sprintf(&str[strlen(str)], "Host: %s:%d\r\n", this->host, this->port);
     }
-    sprintf(contentLen, "Content-Length: %d\r\n", strlen(body));
-    sprintf(str, "%s%s%s%s%s", header, host, contentLen, contentType, body);
+    sprintf(&str[strlen(str)], "Content-Length: %d\r\n", strlen(body));
+    sprintf(&str[strlen(str)], "Content-Type: application/json\r\n\r\n");
 
     DBG("sending: %d bytes\r\n%s", strlen(str), str);
 
@@ -140,6 +137,126 @@
         ERR("send failed\r\n");
         return false;
     }
+    ret = this->s->send_all(body, strlen(body));
+    wait_ms(30);
+    DBG("%d bytes sent\r\n", ret);
+    if (ret < 0) {
+        ERR("send failed\r\n");
+        return false;
+    }
+
+    ret = this->s->receive(str,sizeof(str));
+    wait_ms(30);
+    str[ret]=0;
+    DBG("Received String : (%d)\r\n%s\r\n",ret, str);
+
+    this->s->close();
+
+    for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) {
+        this->data[i].set = false;
+    }
+
+    return true;
+}
+
+int
+Ambient::bulk_send(char *buf) {
+
+    int retry;
+    for (retry = 0; retry < AMBIENT_MAX_RETRY; retry++) {
+        int ret;
+        ret = this->s->connect(this->host, this->port);
+        if (ret == 0) {
+            break ;
+        }
+    }
+    if(retry == AMBIENT_MAX_RETRY) {
+        ERR("Could not connect socket to host\r\n");
+        return -1;
+    }
+
+    char str[180];
+
+    memset(str, 0, sizeof(str));
+    sprintf(str, "POST /api/v2/channels/%d/dataarray HTTP/1.1\r\n", this->channelId);
+    if (this->port == 80) {
+        sprintf(&str[strlen(str)], "Host: %s\r\n", this->host);
+    } else {
+        sprintf(&str[strlen(str)], "Host: %s:%d\r\n", this->host, this->port);
+    }
+    sprintf(&str[strlen(str)], "Host: %s:%d\r\n", this->host, this->port);
+    sprintf(&str[strlen(str)], "Content-Length: %d\r\n", strlen(buf));
+    sprintf(&str[strlen(str)], "Content-Type: application/json\r\n\r\n");
+
+    DBG("sending: %d bytes\r\n%s", strlen(str), str);
+
+    int ret;
+    ret = this->s->send_all(str, strlen(str)); // send header
+    wait_ms(30);
+    DBG("%d bytes sent\r\n", ret);
+    if (ret < 0) {
+        ERR("send failed\r\n");
+        return -1;
+    }
+    int sent;
+    sent = this->s->send_all(buf, strlen(buf));
+    wait_ms(30);
+    DBG("%d bytes sent\r\n", sent);
+    if (sent < 0) {
+        ERR("send failed\r\n");
+        return -1;
+    }
+
+    ret = this->s->receive(str,sizeof(str));
+    wait_ms(30);
+    str[ret]=0;
+    DBG("Received String : (%d)\r\n%s\r\n",ret, str);
+
+    this->s->close();
+
+    for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) {
+        this->data[i].set = false;
+    }
+
+    return sent;
+}
+
+bool
+Ambient::delete_data(const char * userKey) {
+    int retry;
+    for (retry = 0; retry < AMBIENT_MAX_RETRY; retry++) {
+        int ret;
+        ret = this->s->connect(this->host, this->port);
+        if (ret == 0) {
+            break ;
+        }
+    }
+    if(retry == AMBIENT_MAX_RETRY) {
+        ERR("Could not connect socket to host\r\n");
+        return false;
+    }
+
+    char str[180];
+
+    memset(str, 0, sizeof(str));
+    sprintf(str, "DELETE /api/v2/channels/%d/data?userKey=%s HTTP/1.1\r\n", this->channelId, userKey);
+    if (this->port == 80) {
+        sprintf(&str[strlen(str)], "Host: %s\r\n", this->host);
+    } else {
+        sprintf(&str[strlen(str)], "Host: %s:%d\r\n", this->host, this->port);
+    }
+    sprintf(&str[strlen(str)], "Content-Length: 0\r\n");
+    sprintf(&str[strlen(str)], "Content-Type: application/json\r\n\r\n");
+    DBG("%s", str);
+
+    int ret;
+    ret = this->s->send_all(str, strlen(str));
+    wait_ms(30);
+    DBG("%d bytes sent\r\n", ret);
+    if (ret < 0) {
+        ERR("send failed\r\n");
+        return false;
+    }
 
     ret = this->s->receive(str,sizeof(str));
     str[ret]=0;
--- a/Ambient.h	Tue Jun 07 07:47:53 2016 +0000
+++ b/Ambient.h	Mon Jun 13 12:03:41 2016 +0000
@@ -8,6 +8,7 @@
 #define AMBIENT_MAX_RETRY 5
 #define AMBIENT_DATA_SIZE 24
 #define AMBIENT_NUM_PARAMS 11
+#define AMBIENT_TIMEOUT 3000 // milliseconds
 
 /** Ambient class
  * to send data to Ambient service.
@@ -71,7 +72,7 @@
      * @param s and pointer to socket
      * @returns
      *    true on success,
-     *    false on error
+     *    false on failure
      */
     bool init(unsigned int channelId, const char * writeKey, TCPSocketConnection * s, int dev = 0);
     /** Set data on field-th field of payload.
@@ -79,21 +80,40 @@
      * @param data data
      * @returns
      *    true on success,
-     *    false on error
+     *    false on failure
      */
     bool set(int field, char * data);
     /** Clear data on field-th field of payload.
      * @param field index of payload (1 to 8)
      * @returns
      *    true on success,
-     *    false on error
+     *    false on failure
      */
     bool clear(int field);
 
     /** Send data to Ambient
+     * @returns
+     *    true on success,
+     *    false on failure
      */
     bool send(void);
 
+    /** Send bulk data to Ambient
+     * @param buf pointer to the data buffer
+     * @returns
+     *    the number of written bytes on success (>=0)
+     *    -1 on failure
+     */
+    int bulk_send(char * buf);
+
+    /* Delete data stored in this channel
+     * @param userKey userKey
+     * @returns
+     *    true on success,
+     *    false on failure
+     */
+    bool delete_data(const char * userKey);
+
 private:
 
     TCPSocketConnection * s;
@@ -109,4 +129,4 @@
     } data[AMBIENT_NUM_PARAMS];
 };
 
-#endif // Ambient_h
\ No newline at end of file
+#endif // Ambient_h