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

Committer:
AmbientData
Date:
Mon Jun 13 12:03:41 2016 +0000
Revision:
3:a724fe60de46
Parent:
2:a319af936fd5
Child:
4:fcbd652bbb7a
bulk_send() and delete_data() added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AmbientData 0:7dca16f75bae 1 #include "Ambient.h"
AmbientData 0:7dca16f75bae 2
AmbientData 2:a319af936fd5 3 #define AMBIENT_DEBUG 0
AmbientData 2:a319af936fd5 4 #define SimpleIoTBoard 1
AmbientData 0:7dca16f75bae 5
AmbientData 2:a319af936fd5 6 #if AMBIENT_DEBUG
AmbientData 2:a319af936fd5 7 #if SimpleIoTBoard
AmbientData 2:a319af936fd5 8 #include "SoftSerialSendOnry.h"
AmbientData 2:a319af936fd5 9 extern SoftSerialSendOnry pc;
AmbientData 2:a319af936fd5 10 #define DBG(...) { pc.printf(__VA_ARGS__); }
AmbientData 2:a319af936fd5 11 #define ERR(...) { pc.printf(__VA_ARGS__); }
AmbientData 2:a319af936fd5 12 #else
AmbientData 0:7dca16f75bae 13 #define DBG(...) { printf(__VA_ARGS__); }
AmbientData 0:7dca16f75bae 14 #define ERR(...) { printf(__VA_ARGS__); }
AmbientData 2:a319af936fd5 15 #endif /* SOFTSERIAL_SEND_ONRY_H */
AmbientData 0:7dca16f75bae 16 #else
AmbientData 0:7dca16f75bae 17 #define DBG(...)
AmbientData 0:7dca16f75bae 18 #define ERR(...)
AmbientData 2:a319af936fd5 19 #endif /* AMBIENT_DEBUG */
AmbientData 0:7dca16f75bae 20
AmbientData 0:7dca16f75bae 21 const char* AMBIENT_HOST = "54.65.206.59";
AmbientData 0:7dca16f75bae 22 const int AMBIENT_PORT = 80;
AmbientData 3:a724fe60de46 23 const char* AMBIENT_HOST_DEV = "192.168.0.6";
AmbientData 3:a724fe60de46 24 const int AMBIENT_PORT_DEV = 4567;
AmbientData 0:7dca16f75bae 25
AmbientData 0:7dca16f75bae 26 const char * ambient_keys[] = {"\"d1\":\"", "\"d2\":\"", "\"d3\":\"", "\"d4\":\"", "\"d5\":\"", "\"d6\":\"", "\"d7\":\"", "\"d8\":\"", "\"lat\":\"", "\"lng\":\"", "\"created\":\""};
AmbientData 0:7dca16f75bae 27
AmbientData 1:dcc2714b5bcb 28 Ambient::Ambient() {
AmbientData 0:7dca16f75bae 29 }
AmbientData 0:7dca16f75bae 30
AmbientData 0:7dca16f75bae 31 bool
AmbientData 1:dcc2714b5bcb 32 Ambient::init(unsigned int channelId, const char * writeKey, TCPSocketConnection * s, int dev) {
AmbientData 0:7dca16f75bae 33 this->channelId = channelId;
AmbientData 0:7dca16f75bae 34
AmbientData 0:7dca16f75bae 35 if (sizeof(writeKey) > AMBIENT_WRITEKEY_SIZE) {
AmbientData 0:7dca16f75bae 36 ERR("writeKey length > AMBIENT_WRITEKEY_SIZE");
AmbientData 0:7dca16f75bae 37 return false;
AmbientData 0:7dca16f75bae 38 }
AmbientData 0:7dca16f75bae 39 strcpy(this->writeKey, writeKey);
AmbientData 0:7dca16f75bae 40
AmbientData 0:7dca16f75bae 41 if(NULL == s) {
AmbientData 0:7dca16f75bae 42 ERR("Socket Pointer is NULL, open a socket.");
AmbientData 0:7dca16f75bae 43 return false;
AmbientData 0:7dca16f75bae 44 }
AmbientData 0:7dca16f75bae 45 this->s = s;
AmbientData 0:7dca16f75bae 46 this->dev = dev;
AmbientData 0:7dca16f75bae 47 if (dev) {
AmbientData 0:7dca16f75bae 48 strcpy(this->host, AMBIENT_HOST_DEV);
AmbientData 0:7dca16f75bae 49 this->port = AMBIENT_PORT_DEV;
AmbientData 0:7dca16f75bae 50 } else {
AmbientData 0:7dca16f75bae 51 strcpy(this->host, AMBIENT_HOST);
AmbientData 0:7dca16f75bae 52 this->port = AMBIENT_PORT;
AmbientData 0:7dca16f75bae 53 }
AmbientData 0:7dca16f75bae 54 for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) {
AmbientData 0:7dca16f75bae 55 this->data[i].set = false;
AmbientData 0:7dca16f75bae 56 }
AmbientData 0:7dca16f75bae 57 return true;
AmbientData 0:7dca16f75bae 58 }
AmbientData 0:7dca16f75bae 59
AmbientData 0:7dca16f75bae 60 bool
AmbientData 1:dcc2714b5bcb 61 Ambient::set(int field, char * data) {
AmbientData 0:7dca16f75bae 62 --field;
AmbientData 0:7dca16f75bae 63 if (field < 0 || field >= AMBIENT_NUM_PARAMS) {
AmbientData 0:7dca16f75bae 64 return false;
AmbientData 0:7dca16f75bae 65 }
AmbientData 0:7dca16f75bae 66 if (strlen(data) > AMBIENT_DATA_SIZE) {
AmbientData 0:7dca16f75bae 67 return false;
AmbientData 0:7dca16f75bae 68 }
AmbientData 0:7dca16f75bae 69 this->data[field].set = true;
AmbientData 0:7dca16f75bae 70 strcpy(this->data[field].item, data);
AmbientData 0:7dca16f75bae 71
AmbientData 0:7dca16f75bae 72 return true;
AmbientData 0:7dca16f75bae 73 }
AmbientData 0:7dca16f75bae 74
AmbientData 0:7dca16f75bae 75 bool
AmbientData 1:dcc2714b5bcb 76 Ambient::clear(int field) {
AmbientData 0:7dca16f75bae 77 --field;
AmbientData 0:7dca16f75bae 78 if (field < 0 || field >= AMBIENT_NUM_PARAMS) {
AmbientData 0:7dca16f75bae 79 return false;
AmbientData 0:7dca16f75bae 80 }
AmbientData 0:7dca16f75bae 81 this->data[field].set = false;
AmbientData 0:7dca16f75bae 82
AmbientData 0:7dca16f75bae 83 return true;
AmbientData 0:7dca16f75bae 84 }
AmbientData 0:7dca16f75bae 85
AmbientData 0:7dca16f75bae 86 bool
AmbientData 1:dcc2714b5bcb 87 Ambient::send() {
AmbientData 0:7dca16f75bae 88
AmbientData 0:7dca16f75bae 89 int retry;
AmbientData 0:7dca16f75bae 90 for (retry = 0; retry < AMBIENT_MAX_RETRY; retry++) {
AmbientData 0:7dca16f75bae 91 int ret;
AmbientData 0:7dca16f75bae 92 ret = this->s->connect(this->host, this->port);
AmbientData 0:7dca16f75bae 93 if (ret == 0) {
AmbientData 0:7dca16f75bae 94 break ;
AmbientData 0:7dca16f75bae 95 }
AmbientData 0:7dca16f75bae 96 }
AmbientData 0:7dca16f75bae 97 if(retry == AMBIENT_MAX_RETRY) {
AmbientData 0:7dca16f75bae 98 ERR("Could not connect socket to host\r\n");
AmbientData 0:7dca16f75bae 99 return false;
AmbientData 0:7dca16f75bae 100 }
AmbientData 0:7dca16f75bae 101
AmbientData 3:a724fe60de46 102 char str[180];
AmbientData 3:a724fe60de46 103 char body[192];
AmbientData 0:7dca16f75bae 104
AmbientData 3:a724fe60de46 105 memset(body, 0, sizeof(body));
AmbientData 0:7dca16f75bae 106 strcat(body, "{\"writeKey\":\"");
AmbientData 0:7dca16f75bae 107 strcat(body, this->writeKey);
AmbientData 0:7dca16f75bae 108 strcat(body, "\",");
AmbientData 0:7dca16f75bae 109
AmbientData 0:7dca16f75bae 110 for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) {
AmbientData 0:7dca16f75bae 111 if (this->data[i].set) {
AmbientData 0:7dca16f75bae 112 strcat(body, ambient_keys[i]);
AmbientData 0:7dca16f75bae 113 strcat(body, this->data[i].item);
AmbientData 0:7dca16f75bae 114 strcat(body, "\",");
AmbientData 0:7dca16f75bae 115 }
AmbientData 0:7dca16f75bae 116 }
AmbientData 0:7dca16f75bae 117 body[strlen(body) - 1] = '\0';
AmbientData 0:7dca16f75bae 118 strcat(body, "}\r\n");
AmbientData 0:7dca16f75bae 119
AmbientData 3:a724fe60de46 120 memset(str, 0, sizeof(str));
AmbientData 3:a724fe60de46 121 sprintf(str, "POST /api/v2/channels/%d/data HTTP/1.1\r\n", this->channelId);
AmbientData 0:7dca16f75bae 122 if (this->port == 80) {
AmbientData 3:a724fe60de46 123 sprintf(&str[strlen(str)], "Host: %s\r\n", this->host);
AmbientData 0:7dca16f75bae 124 } else {
AmbientData 3:a724fe60de46 125 sprintf(&str[strlen(str)], "Host: %s:%d\r\n", this->host, this->port);
AmbientData 0:7dca16f75bae 126 }
AmbientData 3:a724fe60de46 127 sprintf(&str[strlen(str)], "Content-Length: %d\r\n", strlen(body));
AmbientData 3:a724fe60de46 128 sprintf(&str[strlen(str)], "Content-Type: application/json\r\n\r\n");
AmbientData 0:7dca16f75bae 129
AmbientData 0:7dca16f75bae 130 DBG("sending: %d bytes\r\n%s", strlen(str), str);
AmbientData 0:7dca16f75bae 131
AmbientData 0:7dca16f75bae 132 int ret;
AmbientData 0:7dca16f75bae 133 ret = this->s->send_all(str, strlen(str));
AmbientData 0:7dca16f75bae 134 wait_ms(30);
AmbientData 0:7dca16f75bae 135 DBG("%d bytes sent\r\n", ret);
AmbientData 0:7dca16f75bae 136 if (ret < 0) {
AmbientData 0:7dca16f75bae 137 ERR("send failed\r\n");
AmbientData 0:7dca16f75bae 138 return false;
AmbientData 0:7dca16f75bae 139 }
AmbientData 3:a724fe60de46 140 ret = this->s->send_all(body, strlen(body));
AmbientData 3:a724fe60de46 141 wait_ms(30);
AmbientData 3:a724fe60de46 142 DBG("%d bytes sent\r\n", ret);
AmbientData 3:a724fe60de46 143 if (ret < 0) {
AmbientData 3:a724fe60de46 144 ERR("send failed\r\n");
AmbientData 3:a724fe60de46 145 return false;
AmbientData 3:a724fe60de46 146 }
AmbientData 3:a724fe60de46 147
AmbientData 3:a724fe60de46 148 ret = this->s->receive(str,sizeof(str));
AmbientData 3:a724fe60de46 149 wait_ms(30);
AmbientData 3:a724fe60de46 150 str[ret]=0;
AmbientData 3:a724fe60de46 151 DBG("Received String : (%d)\r\n%s\r\n",ret, str);
AmbientData 3:a724fe60de46 152
AmbientData 3:a724fe60de46 153 this->s->close();
AmbientData 3:a724fe60de46 154
AmbientData 3:a724fe60de46 155 for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) {
AmbientData 3:a724fe60de46 156 this->data[i].set = false;
AmbientData 3:a724fe60de46 157 }
AmbientData 3:a724fe60de46 158
AmbientData 3:a724fe60de46 159 return true;
AmbientData 3:a724fe60de46 160 }
AmbientData 3:a724fe60de46 161
AmbientData 3:a724fe60de46 162 int
AmbientData 3:a724fe60de46 163 Ambient::bulk_send(char *buf) {
AmbientData 3:a724fe60de46 164
AmbientData 3:a724fe60de46 165 int retry;
AmbientData 3:a724fe60de46 166 for (retry = 0; retry < AMBIENT_MAX_RETRY; retry++) {
AmbientData 3:a724fe60de46 167 int ret;
AmbientData 3:a724fe60de46 168 ret = this->s->connect(this->host, this->port);
AmbientData 3:a724fe60de46 169 if (ret == 0) {
AmbientData 3:a724fe60de46 170 break ;
AmbientData 3:a724fe60de46 171 }
AmbientData 3:a724fe60de46 172 }
AmbientData 3:a724fe60de46 173 if(retry == AMBIENT_MAX_RETRY) {
AmbientData 3:a724fe60de46 174 ERR("Could not connect socket to host\r\n");
AmbientData 3:a724fe60de46 175 return -1;
AmbientData 3:a724fe60de46 176 }
AmbientData 3:a724fe60de46 177
AmbientData 3:a724fe60de46 178 char str[180];
AmbientData 3:a724fe60de46 179
AmbientData 3:a724fe60de46 180 memset(str, 0, sizeof(str));
AmbientData 3:a724fe60de46 181 sprintf(str, "POST /api/v2/channels/%d/dataarray HTTP/1.1\r\n", this->channelId);
AmbientData 3:a724fe60de46 182 if (this->port == 80) {
AmbientData 3:a724fe60de46 183 sprintf(&str[strlen(str)], "Host: %s\r\n", this->host);
AmbientData 3:a724fe60de46 184 } else {
AmbientData 3:a724fe60de46 185 sprintf(&str[strlen(str)], "Host: %s:%d\r\n", this->host, this->port);
AmbientData 3:a724fe60de46 186 }
AmbientData 3:a724fe60de46 187 sprintf(&str[strlen(str)], "Host: %s:%d\r\n", this->host, this->port);
AmbientData 3:a724fe60de46 188 sprintf(&str[strlen(str)], "Content-Length: %d\r\n", strlen(buf));
AmbientData 3:a724fe60de46 189 sprintf(&str[strlen(str)], "Content-Type: application/json\r\n\r\n");
AmbientData 3:a724fe60de46 190
AmbientData 3:a724fe60de46 191 DBG("sending: %d bytes\r\n%s", strlen(str), str);
AmbientData 3:a724fe60de46 192
AmbientData 3:a724fe60de46 193 int ret;
AmbientData 3:a724fe60de46 194 ret = this->s->send_all(str, strlen(str)); // send header
AmbientData 3:a724fe60de46 195 wait_ms(30);
AmbientData 3:a724fe60de46 196 DBG("%d bytes sent\r\n", ret);
AmbientData 3:a724fe60de46 197 if (ret < 0) {
AmbientData 3:a724fe60de46 198 ERR("send failed\r\n");
AmbientData 3:a724fe60de46 199 return -1;
AmbientData 3:a724fe60de46 200 }
AmbientData 3:a724fe60de46 201 int sent;
AmbientData 3:a724fe60de46 202 sent = this->s->send_all(buf, strlen(buf));
AmbientData 3:a724fe60de46 203 wait_ms(30);
AmbientData 3:a724fe60de46 204 DBG("%d bytes sent\r\n", sent);
AmbientData 3:a724fe60de46 205 if (sent < 0) {
AmbientData 3:a724fe60de46 206 ERR("send failed\r\n");
AmbientData 3:a724fe60de46 207 return -1;
AmbientData 3:a724fe60de46 208 }
AmbientData 3:a724fe60de46 209
AmbientData 3:a724fe60de46 210 ret = this->s->receive(str,sizeof(str));
AmbientData 3:a724fe60de46 211 wait_ms(30);
AmbientData 3:a724fe60de46 212 str[ret]=0;
AmbientData 3:a724fe60de46 213 DBG("Received String : (%d)\r\n%s\r\n",ret, str);
AmbientData 3:a724fe60de46 214
AmbientData 3:a724fe60de46 215 this->s->close();
AmbientData 3:a724fe60de46 216
AmbientData 3:a724fe60de46 217 for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) {
AmbientData 3:a724fe60de46 218 this->data[i].set = false;
AmbientData 3:a724fe60de46 219 }
AmbientData 3:a724fe60de46 220
AmbientData 3:a724fe60de46 221 return sent;
AmbientData 3:a724fe60de46 222 }
AmbientData 3:a724fe60de46 223
AmbientData 3:a724fe60de46 224 bool
AmbientData 3:a724fe60de46 225 Ambient::delete_data(const char * userKey) {
AmbientData 3:a724fe60de46 226 int retry;
AmbientData 3:a724fe60de46 227 for (retry = 0; retry < AMBIENT_MAX_RETRY; retry++) {
AmbientData 3:a724fe60de46 228 int ret;
AmbientData 3:a724fe60de46 229 ret = this->s->connect(this->host, this->port);
AmbientData 3:a724fe60de46 230 if (ret == 0) {
AmbientData 3:a724fe60de46 231 break ;
AmbientData 3:a724fe60de46 232 }
AmbientData 3:a724fe60de46 233 }
AmbientData 3:a724fe60de46 234 if(retry == AMBIENT_MAX_RETRY) {
AmbientData 3:a724fe60de46 235 ERR("Could not connect socket to host\r\n");
AmbientData 3:a724fe60de46 236 return false;
AmbientData 3:a724fe60de46 237 }
AmbientData 3:a724fe60de46 238
AmbientData 3:a724fe60de46 239 char str[180];
AmbientData 3:a724fe60de46 240
AmbientData 3:a724fe60de46 241 memset(str, 0, sizeof(str));
AmbientData 3:a724fe60de46 242 sprintf(str, "DELETE /api/v2/channels/%d/data?userKey=%s HTTP/1.1\r\n", this->channelId, userKey);
AmbientData 3:a724fe60de46 243 if (this->port == 80) {
AmbientData 3:a724fe60de46 244 sprintf(&str[strlen(str)], "Host: %s\r\n", this->host);
AmbientData 3:a724fe60de46 245 } else {
AmbientData 3:a724fe60de46 246 sprintf(&str[strlen(str)], "Host: %s:%d\r\n", this->host, this->port);
AmbientData 3:a724fe60de46 247 }
AmbientData 3:a724fe60de46 248 sprintf(&str[strlen(str)], "Content-Length: 0\r\n");
AmbientData 3:a724fe60de46 249 sprintf(&str[strlen(str)], "Content-Type: application/json\r\n\r\n");
AmbientData 3:a724fe60de46 250 DBG("%s", str);
AmbientData 3:a724fe60de46 251
AmbientData 3:a724fe60de46 252 int ret;
AmbientData 3:a724fe60de46 253 ret = this->s->send_all(str, strlen(str));
AmbientData 3:a724fe60de46 254 wait_ms(30);
AmbientData 3:a724fe60de46 255 DBG("%d bytes sent\r\n", ret);
AmbientData 3:a724fe60de46 256 if (ret < 0) {
AmbientData 3:a724fe60de46 257 ERR("send failed\r\n");
AmbientData 3:a724fe60de46 258 return false;
AmbientData 3:a724fe60de46 259 }
AmbientData 0:7dca16f75bae 260
AmbientData 0:7dca16f75bae 261 ret = this->s->receive(str,sizeof(str));
AmbientData 0:7dca16f75bae 262 str[ret]=0;
AmbientData 0:7dca16f75bae 263 DBG("Received String : (%d)\r\n%s\r\n",ret, str);
AmbientData 0:7dca16f75bae 264
AmbientData 0:7dca16f75bae 265 this->s->close();
AmbientData 0:7dca16f75bae 266
AmbientData 0:7dca16f75bae 267 for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) {
AmbientData 0:7dca16f75bae 268 this->data[i].set = false;
AmbientData 0:7dca16f75bae 269 }
AmbientData 0:7dca16f75bae 270
AmbientData 0:7dca16f75bae 271 return true;
AmbientData 0:7dca16f75bae 272 }