Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: AmbientExampleSITB AmbientHeartRateMonitor AmbientHeartBeat AmbientExampleSITB_ws ... more
Ambient.cpp
00001 #include "Ambient.h" 00002 00003 #define AMBIENT_DEBUG 0 00004 #define SimpleIoTBoard 0 00005 00006 #if AMBIENT_DEBUG 00007 #if SimpleIoTBoard 00008 #include "SoftSerialSendOnry.h" 00009 extern SoftSerialSendOnry pc; 00010 #define DBG(...) { pc.printf(__VA_ARGS__); } 00011 #define ERR(...) { pc.printf(__VA_ARGS__); } 00012 #else 00013 #define DBG(...) { printf(__VA_ARGS__); } 00014 #define ERR(...) { printf(__VA_ARGS__); } 00015 #endif /* SOFTSERIAL_SEND_ONRY_H */ 00016 #else 00017 #define DBG(...) 00018 #define ERR(...) 00019 #endif /* AMBIENT_DEBUG */ 00020 00021 const char* AMBIENT_HOST = "54.65.206.59"; 00022 const int AMBIENT_PORT = 80; 00023 const char* AMBIENT_HOST_DEV = "192.168.0.6"; 00024 const int AMBIENT_PORT_DEV = 4567; 00025 00026 const char * ambient_keys[] = {"\"d1\":\"", "\"d2\":\"", "\"d3\":\"", "\"d4\":\"", "\"d5\":\"", "\"d6\":\"", "\"d7\":\"", "\"d8\":\"", "\"lat\":\"", "\"lng\":\"", "\"created\":\""}; 00027 00028 Ambient::Ambient() { 00029 } 00030 00031 bool 00032 Ambient::init(unsigned int channelId, const char * writeKey, TCPSocketConnection * s, int dev) { 00033 this->channelId = channelId; 00034 00035 if (sizeof(writeKey) > AMBIENT_WRITEKEY_SIZE) { 00036 ERR("writeKey length > AMBIENT_WRITEKEY_SIZE"); 00037 return false; 00038 } 00039 strcpy(this->writeKey, writeKey); 00040 00041 if(NULL == s) { 00042 ERR("Socket Pointer is NULL, open a socket."); 00043 return false; 00044 } 00045 this->s = s; 00046 this->dev = dev; 00047 if (dev) { 00048 strcpy(this->host, AMBIENT_HOST_DEV); 00049 this->port = AMBIENT_PORT_DEV; 00050 } else { 00051 strcpy(this->host, AMBIENT_HOST); 00052 this->port = AMBIENT_PORT; 00053 } 00054 for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) { 00055 this->data[i].set = false; 00056 } 00057 return true; 00058 } 00059 00060 bool 00061 Ambient::set(int field, char * data) { 00062 --field; 00063 if (field < 0 || field >= AMBIENT_NUM_PARAMS) { 00064 return false; 00065 } 00066 if (strlen(data) > AMBIENT_DATA_SIZE) { 00067 return false; 00068 } 00069 this->data[field].set = true; 00070 strcpy(this->data[field].item, data); 00071 00072 return true; 00073 } 00074 00075 bool 00076 Ambient::set(int field, int data) { 00077 char buf[12]; 00078 snprintf(buf, sizeof(buf), "%d", data); 00079 return (set(field, buf)); 00080 } 00081 00082 bool 00083 Ambient::set(int field, double data) { 00084 char buf[12]; 00085 snprintf(buf, sizeof(buf), "%f", data); 00086 return (set(field, buf)); 00087 } 00088 00089 bool 00090 Ambient::clear(int field) { 00091 --field; 00092 if (field < 0 || field >= AMBIENT_NUM_PARAMS) { 00093 return false; 00094 } 00095 this->data[field].set = false; 00096 00097 return true; 00098 } 00099 00100 bool 00101 Ambient::send() { 00102 00103 int retry; 00104 for (retry = 0; retry < AMBIENT_MAX_RETRY; retry++) { 00105 int ret; 00106 ret = this->s->connect(this->host, this->port); 00107 if (ret == 0) { 00108 break ; 00109 } 00110 } 00111 if(retry == AMBIENT_MAX_RETRY) { 00112 ERR("Could not connect socket to host\r\n"); 00113 return false; 00114 } 00115 00116 char str[180]; 00117 char body[192]; 00118 00119 memset(body, 0, sizeof(body)); 00120 strcat(body, "{\"writeKey\":\""); 00121 strcat(body, this->writeKey); 00122 strcat(body, "\","); 00123 00124 for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) { 00125 if (this->data[i].set) { 00126 strcat(body, ambient_keys[i]); 00127 strcat(body, this->data[i].item); 00128 strcat(body, "\","); 00129 } 00130 } 00131 body[strlen(body) - 1] = '\0'; 00132 strcat(body, "}\r\n"); 00133 00134 memset(str, 0, sizeof(str)); 00135 sprintf(str, "POST /api/v2/channels/%d/data HTTP/1.1\r\n", this->channelId); 00136 if (this->port == 80) { 00137 sprintf(&str[strlen(str)], "Host: %s\r\n", this->host); 00138 } else { 00139 sprintf(&str[strlen(str)], "Host: %s:%d\r\n", this->host, this->port); 00140 } 00141 sprintf(&str[strlen(str)], "Content-Length: %d\r\n", strlen(body)); 00142 sprintf(&str[strlen(str)], "Content-Type: application/json\r\n\r\n"); 00143 00144 DBG("sending: %d bytes\r\n%s", strlen(str), str); 00145 00146 int ret; 00147 ret = this->s->send_all(str, strlen(str)); 00148 wait_ms(30); 00149 DBG("%d bytes sent\r\n", ret); 00150 if (ret < 0) { 00151 ERR("send failed\r\n"); 00152 return false; 00153 } 00154 ret = this->s->send_all(body, strlen(body)); 00155 wait_ms(30); 00156 DBG("%d bytes sent\r\n", ret); 00157 if (ret < 0) { 00158 ERR("send failed\r\n"); 00159 return false; 00160 } 00161 00162 ret = this->s->receive(str,sizeof(str)); 00163 wait_ms(30); 00164 str[ret]=0; 00165 DBG("Received String : (%d)\r\n%s\r\n",ret, str); 00166 00167 this->s->close(); 00168 00169 for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) { 00170 this->data[i].set = false; 00171 } 00172 00173 return true; 00174 } 00175 00176 int 00177 Ambient::bulk_send(char *buf) { 00178 00179 int retry; 00180 for (retry = 0; retry < AMBIENT_MAX_RETRY; retry++) { 00181 int ret; 00182 ret = this->s->connect(this->host, this->port); 00183 if (ret == 0) { 00184 break ; 00185 } 00186 } 00187 if(retry == AMBIENT_MAX_RETRY) { 00188 ERR("Could not connect socket to host\r\n"); 00189 return -1; 00190 } 00191 00192 char str[180]; 00193 00194 memset(str, 0, sizeof(str)); 00195 sprintf(str, "POST /api/v2/channels/%d/dataarray HTTP/1.1\r\n", this->channelId); 00196 if (this->port == 80) { 00197 sprintf(&str[strlen(str)], "Host: %s\r\n", this->host); 00198 } else { 00199 sprintf(&str[strlen(str)], "Host: %s:%d\r\n", this->host, this->port); 00200 } 00201 sprintf(&str[strlen(str)], "Host: %s:%d\r\n", this->host, this->port); 00202 sprintf(&str[strlen(str)], "Content-Length: %d\r\n", strlen(buf)); 00203 sprintf(&str[strlen(str)], "Content-Type: application/json\r\n\r\n"); 00204 00205 DBG("sending: %d bytes\r\n%s", strlen(str), str); 00206 00207 int ret; 00208 ret = this->s->send_all(str, strlen(str)); // send header 00209 wait_ms(30); 00210 DBG("%d bytes sent\r\n", ret); 00211 if (ret < 0) { 00212 ERR("send failed\r\n"); 00213 return -1; 00214 } 00215 int sent; 00216 sent = this->s->send_all(buf, strlen(buf)); 00217 wait_ms(30); 00218 DBG("%d bytes sent\r\n", sent); 00219 if (sent < 0) { 00220 ERR("send failed\r\n"); 00221 return -1; 00222 } 00223 00224 ret = this->s->receive(str,sizeof(str)); 00225 wait_ms(30); 00226 str[ret]=0; 00227 DBG("Received String : (%d)\r\n%s\r\n",ret, str); 00228 00229 this->s->close(); 00230 00231 for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) { 00232 this->data[i].set = false; 00233 } 00234 00235 return sent; 00236 } 00237 00238 bool 00239 Ambient::delete_data(const char * userKey) { 00240 int retry; 00241 for (retry = 0; retry < AMBIENT_MAX_RETRY; retry++) { 00242 int ret; 00243 ret = this->s->connect(this->host, this->port); 00244 if (ret == 0) { 00245 break ; 00246 } 00247 } 00248 if(retry == AMBIENT_MAX_RETRY) { 00249 ERR("Could not connect socket to host\r\n"); 00250 return false; 00251 } 00252 00253 char str[180]; 00254 00255 memset(str, 0, sizeof(str)); 00256 sprintf(str, "DELETE /api/v2/channels/%d/data?userKey=%s HTTP/1.1\r\n", this->channelId, userKey); 00257 if (this->port == 80) { 00258 sprintf(&str[strlen(str)], "Host: %s\r\n", this->host); 00259 } else { 00260 sprintf(&str[strlen(str)], "Host: %s:%d\r\n", this->host, this->port); 00261 } 00262 sprintf(&str[strlen(str)], "Content-Length: 0\r\n"); 00263 sprintf(&str[strlen(str)], "Content-Type: application/json\r\n\r\n"); 00264 DBG("%s", str); 00265 00266 int ret; 00267 ret = this->s->send_all(str, strlen(str)); 00268 wait_ms(30); 00269 DBG("%d bytes sent\r\n", ret); 00270 if (ret < 0) { 00271 ERR("send failed\r\n"); 00272 return false; 00273 } 00274 00275 ret = this->s->receive(str,sizeof(str)); 00276 str[ret]=0; 00277 DBG("Received String : (%d)\r\n%s\r\n",ret, str); 00278 00279 this->s->close(); 00280 00281 for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) { 00282 this->data[i].set = false; 00283 } 00284 00285 return true; 00286 }
Generated on Sat Jul 16 2022 03:41:26 by
1.7.2
Ambient