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:
Wed Jan 25 12:28:14 2017 +0000
Revision:
4:fcbd652bbb7a
Parent:
3:a724fe60de46
send()?????

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 4:fcbd652bbb7a 4 #define SimpleIoTBoard 0
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 4:fcbd652bbb7a 76 Ambient::set(int field, int data) {
AmbientData 4:fcbd652bbb7a 77 char buf[12];
AmbientData 4:fcbd652bbb7a 78 snprintf(buf, sizeof(buf), "%d", data);
AmbientData 4:fcbd652bbb7a 79 return (set(field, buf));
AmbientData 4:fcbd652bbb7a 80 }
AmbientData 4:fcbd652bbb7a 81
AmbientData 4:fcbd652bbb7a 82 bool
AmbientData 4:fcbd652bbb7a 83 Ambient::set(int field, double data) {
AmbientData 4:fcbd652bbb7a 84 char buf[12];
AmbientData 4:fcbd652bbb7a 85 snprintf(buf, sizeof(buf), "%f", data);
AmbientData 4:fcbd652bbb7a 86 return (set(field, buf));
AmbientData 4:fcbd652bbb7a 87 }
AmbientData 4:fcbd652bbb7a 88
AmbientData 4:fcbd652bbb7a 89 bool
AmbientData 1:dcc2714b5bcb 90 Ambient::clear(int field) {
AmbientData 0:7dca16f75bae 91 --field;
AmbientData 0:7dca16f75bae 92 if (field < 0 || field >= AMBIENT_NUM_PARAMS) {
AmbientData 0:7dca16f75bae 93 return false;
AmbientData 0:7dca16f75bae 94 }
AmbientData 0:7dca16f75bae 95 this->data[field].set = false;
AmbientData 0:7dca16f75bae 96
AmbientData 0:7dca16f75bae 97 return true;
AmbientData 0:7dca16f75bae 98 }
AmbientData 0:7dca16f75bae 99
AmbientData 0:7dca16f75bae 100 bool
AmbientData 1:dcc2714b5bcb 101 Ambient::send() {
AmbientData 0:7dca16f75bae 102
AmbientData 0:7dca16f75bae 103 int retry;
AmbientData 0:7dca16f75bae 104 for (retry = 0; retry < AMBIENT_MAX_RETRY; retry++) {
AmbientData 0:7dca16f75bae 105 int ret;
AmbientData 0:7dca16f75bae 106 ret = this->s->connect(this->host, this->port);
AmbientData 0:7dca16f75bae 107 if (ret == 0) {
AmbientData 0:7dca16f75bae 108 break ;
AmbientData 0:7dca16f75bae 109 }
AmbientData 0:7dca16f75bae 110 }
AmbientData 0:7dca16f75bae 111 if(retry == AMBIENT_MAX_RETRY) {
AmbientData 0:7dca16f75bae 112 ERR("Could not connect socket to host\r\n");
AmbientData 0:7dca16f75bae 113 return false;
AmbientData 0:7dca16f75bae 114 }
AmbientData 0:7dca16f75bae 115
AmbientData 3:a724fe60de46 116 char str[180];
AmbientData 3:a724fe60de46 117 char body[192];
AmbientData 0:7dca16f75bae 118
AmbientData 3:a724fe60de46 119 memset(body, 0, sizeof(body));
AmbientData 0:7dca16f75bae 120 strcat(body, "{\"writeKey\":\"");
AmbientData 0:7dca16f75bae 121 strcat(body, this->writeKey);
AmbientData 0:7dca16f75bae 122 strcat(body, "\",");
AmbientData 0:7dca16f75bae 123
AmbientData 0:7dca16f75bae 124 for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) {
AmbientData 0:7dca16f75bae 125 if (this->data[i].set) {
AmbientData 0:7dca16f75bae 126 strcat(body, ambient_keys[i]);
AmbientData 0:7dca16f75bae 127 strcat(body, this->data[i].item);
AmbientData 0:7dca16f75bae 128 strcat(body, "\",");
AmbientData 0:7dca16f75bae 129 }
AmbientData 0:7dca16f75bae 130 }
AmbientData 0:7dca16f75bae 131 body[strlen(body) - 1] = '\0';
AmbientData 0:7dca16f75bae 132 strcat(body, "}\r\n");
AmbientData 0:7dca16f75bae 133
AmbientData 3:a724fe60de46 134 memset(str, 0, sizeof(str));
AmbientData 3:a724fe60de46 135 sprintf(str, "POST /api/v2/channels/%d/data HTTP/1.1\r\n", this->channelId);
AmbientData 0:7dca16f75bae 136 if (this->port == 80) {
AmbientData 3:a724fe60de46 137 sprintf(&str[strlen(str)], "Host: %s\r\n", this->host);
AmbientData 0:7dca16f75bae 138 } else {
AmbientData 3:a724fe60de46 139 sprintf(&str[strlen(str)], "Host: %s:%d\r\n", this->host, this->port);
AmbientData 0:7dca16f75bae 140 }
AmbientData 3:a724fe60de46 141 sprintf(&str[strlen(str)], "Content-Length: %d\r\n", strlen(body));
AmbientData 3:a724fe60de46 142 sprintf(&str[strlen(str)], "Content-Type: application/json\r\n\r\n");
AmbientData 0:7dca16f75bae 143
AmbientData 0:7dca16f75bae 144 DBG("sending: %d bytes\r\n%s", strlen(str), str);
AmbientData 0:7dca16f75bae 145
AmbientData 0:7dca16f75bae 146 int ret;
AmbientData 0:7dca16f75bae 147 ret = this->s->send_all(str, strlen(str));
AmbientData 0:7dca16f75bae 148 wait_ms(30);
AmbientData 0:7dca16f75bae 149 DBG("%d bytes sent\r\n", ret);
AmbientData 0:7dca16f75bae 150 if (ret < 0) {
AmbientData 0:7dca16f75bae 151 ERR("send failed\r\n");
AmbientData 0:7dca16f75bae 152 return false;
AmbientData 0:7dca16f75bae 153 }
AmbientData 3:a724fe60de46 154 ret = this->s->send_all(body, strlen(body));
AmbientData 3:a724fe60de46 155 wait_ms(30);
AmbientData 3:a724fe60de46 156 DBG("%d bytes sent\r\n", ret);
AmbientData 3:a724fe60de46 157 if (ret < 0) {
AmbientData 3:a724fe60de46 158 ERR("send failed\r\n");
AmbientData 3:a724fe60de46 159 return false;
AmbientData 3:a724fe60de46 160 }
AmbientData 3:a724fe60de46 161
AmbientData 3:a724fe60de46 162 ret = this->s->receive(str,sizeof(str));
AmbientData 3:a724fe60de46 163 wait_ms(30);
AmbientData 3:a724fe60de46 164 str[ret]=0;
AmbientData 3:a724fe60de46 165 DBG("Received String : (%d)\r\n%s\r\n",ret, str);
AmbientData 3:a724fe60de46 166
AmbientData 3:a724fe60de46 167 this->s->close();
AmbientData 3:a724fe60de46 168
AmbientData 3:a724fe60de46 169 for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) {
AmbientData 3:a724fe60de46 170 this->data[i].set = false;
AmbientData 3:a724fe60de46 171 }
AmbientData 3:a724fe60de46 172
AmbientData 3:a724fe60de46 173 return true;
AmbientData 3:a724fe60de46 174 }
AmbientData 3:a724fe60de46 175
AmbientData 3:a724fe60de46 176 int
AmbientData 3:a724fe60de46 177 Ambient::bulk_send(char *buf) {
AmbientData 3:a724fe60de46 178
AmbientData 3:a724fe60de46 179 int retry;
AmbientData 3:a724fe60de46 180 for (retry = 0; retry < AMBIENT_MAX_RETRY; retry++) {
AmbientData 3:a724fe60de46 181 int ret;
AmbientData 3:a724fe60de46 182 ret = this->s->connect(this->host, this->port);
AmbientData 3:a724fe60de46 183 if (ret == 0) {
AmbientData 3:a724fe60de46 184 break ;
AmbientData 3:a724fe60de46 185 }
AmbientData 3:a724fe60de46 186 }
AmbientData 3:a724fe60de46 187 if(retry == AMBIENT_MAX_RETRY) {
AmbientData 3:a724fe60de46 188 ERR("Could not connect socket to host\r\n");
AmbientData 3:a724fe60de46 189 return -1;
AmbientData 3:a724fe60de46 190 }
AmbientData 3:a724fe60de46 191
AmbientData 3:a724fe60de46 192 char str[180];
AmbientData 3:a724fe60de46 193
AmbientData 3:a724fe60de46 194 memset(str, 0, sizeof(str));
AmbientData 3:a724fe60de46 195 sprintf(str, "POST /api/v2/channels/%d/dataarray HTTP/1.1\r\n", this->channelId);
AmbientData 3:a724fe60de46 196 if (this->port == 80) {
AmbientData 3:a724fe60de46 197 sprintf(&str[strlen(str)], "Host: %s\r\n", this->host);
AmbientData 3:a724fe60de46 198 } else {
AmbientData 3:a724fe60de46 199 sprintf(&str[strlen(str)], "Host: %s:%d\r\n", this->host, this->port);
AmbientData 3:a724fe60de46 200 }
AmbientData 3:a724fe60de46 201 sprintf(&str[strlen(str)], "Host: %s:%d\r\n", this->host, this->port);
AmbientData 3:a724fe60de46 202 sprintf(&str[strlen(str)], "Content-Length: %d\r\n", strlen(buf));
AmbientData 3:a724fe60de46 203 sprintf(&str[strlen(str)], "Content-Type: application/json\r\n\r\n");
AmbientData 3:a724fe60de46 204
AmbientData 3:a724fe60de46 205 DBG("sending: %d bytes\r\n%s", strlen(str), str);
AmbientData 3:a724fe60de46 206
AmbientData 3:a724fe60de46 207 int ret;
AmbientData 3:a724fe60de46 208 ret = this->s->send_all(str, strlen(str)); // send header
AmbientData 3:a724fe60de46 209 wait_ms(30);
AmbientData 3:a724fe60de46 210 DBG("%d bytes sent\r\n", ret);
AmbientData 3:a724fe60de46 211 if (ret < 0) {
AmbientData 3:a724fe60de46 212 ERR("send failed\r\n");
AmbientData 3:a724fe60de46 213 return -1;
AmbientData 3:a724fe60de46 214 }
AmbientData 3:a724fe60de46 215 int sent;
AmbientData 3:a724fe60de46 216 sent = this->s->send_all(buf, strlen(buf));
AmbientData 3:a724fe60de46 217 wait_ms(30);
AmbientData 3:a724fe60de46 218 DBG("%d bytes sent\r\n", sent);
AmbientData 3:a724fe60de46 219 if (sent < 0) {
AmbientData 3:a724fe60de46 220 ERR("send failed\r\n");
AmbientData 3:a724fe60de46 221 return -1;
AmbientData 3:a724fe60de46 222 }
AmbientData 3:a724fe60de46 223
AmbientData 3:a724fe60de46 224 ret = this->s->receive(str,sizeof(str));
AmbientData 3:a724fe60de46 225 wait_ms(30);
AmbientData 3:a724fe60de46 226 str[ret]=0;
AmbientData 3:a724fe60de46 227 DBG("Received String : (%d)\r\n%s\r\n",ret, str);
AmbientData 3:a724fe60de46 228
AmbientData 3:a724fe60de46 229 this->s->close();
AmbientData 3:a724fe60de46 230
AmbientData 3:a724fe60de46 231 for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) {
AmbientData 3:a724fe60de46 232 this->data[i].set = false;
AmbientData 3:a724fe60de46 233 }
AmbientData 3:a724fe60de46 234
AmbientData 3:a724fe60de46 235 return sent;
AmbientData 3:a724fe60de46 236 }
AmbientData 3:a724fe60de46 237
AmbientData 3:a724fe60de46 238 bool
AmbientData 3:a724fe60de46 239 Ambient::delete_data(const char * userKey) {
AmbientData 3:a724fe60de46 240 int retry;
AmbientData 3:a724fe60de46 241 for (retry = 0; retry < AMBIENT_MAX_RETRY; retry++) {
AmbientData 3:a724fe60de46 242 int ret;
AmbientData 3:a724fe60de46 243 ret = this->s->connect(this->host, this->port);
AmbientData 3:a724fe60de46 244 if (ret == 0) {
AmbientData 3:a724fe60de46 245 break ;
AmbientData 3:a724fe60de46 246 }
AmbientData 3:a724fe60de46 247 }
AmbientData 3:a724fe60de46 248 if(retry == AMBIENT_MAX_RETRY) {
AmbientData 3:a724fe60de46 249 ERR("Could not connect socket to host\r\n");
AmbientData 3:a724fe60de46 250 return false;
AmbientData 3:a724fe60de46 251 }
AmbientData 3:a724fe60de46 252
AmbientData 3:a724fe60de46 253 char str[180];
AmbientData 3:a724fe60de46 254
AmbientData 3:a724fe60de46 255 memset(str, 0, sizeof(str));
AmbientData 3:a724fe60de46 256 sprintf(str, "DELETE /api/v2/channels/%d/data?userKey=%s HTTP/1.1\r\n", this->channelId, userKey);
AmbientData 3:a724fe60de46 257 if (this->port == 80) {
AmbientData 3:a724fe60de46 258 sprintf(&str[strlen(str)], "Host: %s\r\n", this->host);
AmbientData 3:a724fe60de46 259 } else {
AmbientData 3:a724fe60de46 260 sprintf(&str[strlen(str)], "Host: %s:%d\r\n", this->host, this->port);
AmbientData 3:a724fe60de46 261 }
AmbientData 3:a724fe60de46 262 sprintf(&str[strlen(str)], "Content-Length: 0\r\n");
AmbientData 3:a724fe60de46 263 sprintf(&str[strlen(str)], "Content-Type: application/json\r\n\r\n");
AmbientData 3:a724fe60de46 264 DBG("%s", str);
AmbientData 3:a724fe60de46 265
AmbientData 3:a724fe60de46 266 int ret;
AmbientData 3:a724fe60de46 267 ret = this->s->send_all(str, strlen(str));
AmbientData 3:a724fe60de46 268 wait_ms(30);
AmbientData 3:a724fe60de46 269 DBG("%d bytes sent\r\n", ret);
AmbientData 3:a724fe60de46 270 if (ret < 0) {
AmbientData 3:a724fe60de46 271 ERR("send failed\r\n");
AmbientData 3:a724fe60de46 272 return false;
AmbientData 3:a724fe60de46 273 }
AmbientData 0:7dca16f75bae 274
AmbientData 0:7dca16f75bae 275 ret = this->s->receive(str,sizeof(str));
AmbientData 0:7dca16f75bae 276 str[ret]=0;
AmbientData 0:7dca16f75bae 277 DBG("Received String : (%d)\r\n%s\r\n",ret, str);
AmbientData 0:7dca16f75bae 278
AmbientData 0:7dca16f75bae 279 this->s->close();
AmbientData 0:7dca16f75bae 280
AmbientData 0:7dca16f75bae 281 for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) {
AmbientData 0:7dca16f75bae 282 this->data[i].set = false;
AmbientData 0:7dca16f75bae 283 }
AmbientData 0:7dca16f75bae 284
AmbientData 0:7dca16f75bae 285 return true;
AmbientData 0:7dca16f75bae 286 }