discof746 ethernet+lcd
GroveStreams.cpp@1:e69854e65c88, 2020-08-31 (annotated)
- Committer:
- 38domo
- Date:
- Mon Aug 31 17:50:33 2020 +0000
- Revision:
- 1:e69854e65c88
- Parent:
- 0:8b675b2726b7
no modif in grove stream
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mmills | 0:8b675b2726b7 | 1 | /* |
mmills | 0:8b675b2726b7 | 2 | GroveStreams is a helper class to assist with pushing data into the |
mmills | 0:8b675b2726b7 | 3 | GroveStreams.com IoT platform. |
mmills | 0:8b675b2726b7 | 4 | |
mmills | 0:8b675b2726b7 | 5 | License: |
mmills | 0:8b675b2726b7 | 6 | Copyright (C) 2017 GroveStreams LLC. |
mmills | 0:8b675b2726b7 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); |
mmills | 0:8b675b2726b7 | 8 | you may not use this file except in compliance with the License. |
mmills | 0:8b675b2726b7 | 9 | You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0 |
mmills | 0:8b675b2726b7 | 10 | |
mmills | 0:8b675b2726b7 | 11 | Unless required by applicable law or agreed to in writing, software |
mmills | 0:8b675b2726b7 | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
mmills | 0:8b675b2726b7 | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mmills | 0:8b675b2726b7 | 14 | See the License for the specific language governing permissions and |
mmills | 0:8b675b2726b7 | 15 | limitations under the License. |
mmills | 0:8b675b2726b7 | 16 | */ |
mmills | 0:8b675b2726b7 | 17 | |
mmills | 0:8b675b2726b7 | 18 | #include "GroveStreams.h" |
mmills | 0:8b675b2726b7 | 19 | |
mmills | 0:8b675b2726b7 | 20 | GroveStreams::GroveStreams(const char* apiKey, LcdDiscoF746NgTracer* pLcd) |
mmills | 0:8b675b2726b7 | 21 | { |
mmills | 0:8b675b2726b7 | 22 | init(apiKey, pLcd); |
mmills | 0:8b675b2726b7 | 23 | } |
mmills | 0:8b675b2726b7 | 24 | |
mmills | 0:8b675b2726b7 | 25 | GroveStreams::GroveStreams(const char* apiKey) |
mmills | 0:8b675b2726b7 | 26 | { |
mmills | 0:8b675b2726b7 | 27 | init(apiKey, NULL); |
mmills | 0:8b675b2726b7 | 28 | } |
mmills | 0:8b675b2726b7 | 29 | |
mmills | 0:8b675b2726b7 | 30 | GroveStreams::~GroveStreams() { |
mmills | 0:8b675b2726b7 | 31 | _eth.disconnect(); |
mmills | 0:8b675b2726b7 | 32 | } |
mmills | 0:8b675b2726b7 | 33 | |
38domo | 1:e69854e65c88 | 34 | extern char gsStreamIPdatas[]; |
38domo | 1:e69854e65c88 | 35 | static char* _domain; |
mmills | 0:8b675b2726b7 | 36 | int GroveStreams::init(const char* apiKey, LcdDiscoF746NgTracer* pLcd) |
mmills | 0:8b675b2726b7 | 37 | { |
38domo | 1:e69854e65c88 | 38 | |
38domo | 1:e69854e65c88 | 39 | _domain = gsStreamIPdatas; |
38domo | 1:e69854e65c88 | 40 | _port = 1880; |
mmills | 0:8b675b2726b7 | 41 | _apiKey = apiKey; |
mmills | 0:8b675b2726b7 | 42 | _myIPAddress = NULL; //Don't Change. Set below from DHCP. Needed by GroveStreams to verify that a device is not uploading more than once every 10s. |
mmills | 0:8b675b2726b7 | 43 | _pLcd = pLcd; |
mmills | 0:8b675b2726b7 | 44 | |
mmills | 0:8b675b2726b7 | 45 | return startEthernet(); |
mmills | 0:8b675b2726b7 | 46 | } |
mmills | 0:8b675b2726b7 | 47 | |
mmills | 0:8b675b2726b7 | 48 | void GroveStreams::printf(const char* format, ...) |
mmills | 0:8b675b2726b7 | 49 | { |
mmills | 0:8b675b2726b7 | 50 | |
mmills | 0:8b675b2726b7 | 51 | va_list args; |
mmills | 0:8b675b2726b7 | 52 | va_start(args, format); |
mmills | 0:8b675b2726b7 | 53 | |
mmills | 0:8b675b2726b7 | 54 | if (_pLcd != NULL) { |
mmills | 0:8b675b2726b7 | 55 | char sbuffer[512]; |
mmills | 0:8b675b2726b7 | 56 | vsprintf(sbuffer, format, args); |
mmills | 0:8b675b2726b7 | 57 | _pLcd->println(sbuffer); |
mmills | 0:8b675b2726b7 | 58 | } else { |
mmills | 0:8b675b2726b7 | 59 | vprintf(format, args); |
mmills | 0:8b675b2726b7 | 60 | } |
mmills | 0:8b675b2726b7 | 61 | |
mmills | 0:8b675b2726b7 | 62 | va_end(args); |
mmills | 0:8b675b2726b7 | 63 | } |
mmills | 0:8b675b2726b7 | 64 | |
mmills | 0:8b675b2726b7 | 65 | unsigned long GroveStreams::send(const char* componentId, const char* samples) |
mmills | 0:8b675b2726b7 | 66 | { |
mmills | 0:8b675b2726b7 | 67 | char resultBuffer[256]= {0}; |
mmills | 0:8b675b2726b7 | 68 | return send(componentId, samples, NULL, NULL, resultBuffer, sizeof resultBuffer); |
mmills | 0:8b675b2726b7 | 69 | } |
mmills | 0:8b675b2726b7 | 70 | |
38domo | 1:e69854e65c88 | 71 | unsigned long GroveStreams::sendJPA(const char* componentId, const char* samples) |
38domo | 1:e69854e65c88 | 72 | { |
38domo | 1:e69854e65c88 | 73 | char resultBuffer[256]= {0}; |
38domo | 1:e69854e65c88 | 74 | return sendJPA(componentId, samples, NULL, NULL, resultBuffer, sizeof resultBuffer); |
38domo | 1:e69854e65c88 | 75 | } |
38domo | 1:e69854e65c88 | 76 | |
38domo | 1:e69854e65c88 | 77 | unsigned long GroveStreams::sendInitDatasJPA(const char* componentId, const char* samples) |
38domo | 1:e69854e65c88 | 78 | { |
38domo | 1:e69854e65c88 | 79 | char resultBuffer[256]= {0}; |
38domo | 1:e69854e65c88 | 80 | return sendInitDatasJPA(componentId, samples, NULL, NULL, resultBuffer, sizeof resultBuffer); |
38domo | 1:e69854e65c88 | 81 | } |
38domo | 1:e69854e65c88 | 82 | |
mmills | 0:8b675b2726b7 | 83 | unsigned long GroveStreams::send(const char* componentId, const char* samples, char* resultBuffer, size_t resultBufferSize) |
mmills | 0:8b675b2726b7 | 84 | { |
mmills | 0:8b675b2726b7 | 85 | return send(componentId, samples, NULL, NULL, resultBuffer, resultBufferSize); |
mmills | 0:8b675b2726b7 | 86 | } |
mmills | 0:8b675b2726b7 | 87 | |
mmills | 0:8b675b2726b7 | 88 | unsigned long GroveStreams::send(const char* componentId, const char* samples, const char* componentName, |
mmills | 0:8b675b2726b7 | 89 | const char* compTmplId, char* resultBuffer, size_t resultBufferSize) |
mmills | 0:8b675b2726b7 | 90 | { |
mmills | 0:8b675b2726b7 | 91 | int failedCounter = 0; |
mmills | 0:8b675b2726b7 | 92 | int result = 1; |
mmills | 0:8b675b2726b7 | 93 | |
mmills | 0:8b675b2726b7 | 94 | while (failedCounter < 3 && result != 0) { |
mmills | 0:8b675b2726b7 | 95 | |
mmills | 0:8b675b2726b7 | 96 | result = sendNoRetry(componentId, samples, componentName, compTmplId, resultBuffer, resultBufferSize); |
mmills | 0:8b675b2726b7 | 97 | |
mmills | 0:8b675b2726b7 | 98 | this->printf("send result code: %d", result); |
mmills | 0:8b675b2726b7 | 99 | |
mmills | 0:8b675b2726b7 | 100 | if (result != 0) { |
mmills | 0:8b675b2726b7 | 101 | //Wait and try again |
mmills | 0:8b675b2726b7 | 102 | failedCounter++; |
mmills | 0:8b675b2726b7 | 103 | this->printf("send failed %d times", failedCounter); |
mmills | 0:8b675b2726b7 | 104 | wait(2); |
mmills | 0:8b675b2726b7 | 105 | } |
mmills | 0:8b675b2726b7 | 106 | |
mmills | 0:8b675b2726b7 | 107 | if (failedCounter > 3 ) { |
mmills | 0:8b675b2726b7 | 108 | this->printf("Too many failures. Restarting Ethernet."); |
mmills | 0:8b675b2726b7 | 109 | startEthernet(); |
mmills | 0:8b675b2726b7 | 110 | } |
mmills | 0:8b675b2726b7 | 111 | } |
mmills | 0:8b675b2726b7 | 112 | |
mmills | 0:8b675b2726b7 | 113 | return result; |
mmills | 0:8b675b2726b7 | 114 | } |
mmills | 0:8b675b2726b7 | 115 | |
38domo | 1:e69854e65c88 | 116 | unsigned long GroveStreams::sendJPA(const char* componentId, const char* samples, const char* componentName, |
38domo | 1:e69854e65c88 | 117 | const char* compTmplId, char* resultBuffer, size_t resultBufferSize) |
38domo | 1:e69854e65c88 | 118 | { |
38domo | 1:e69854e65c88 | 119 | int failedCounter = 0; |
38domo | 1:e69854e65c88 | 120 | int result = 1; |
38domo | 1:e69854e65c88 | 121 | |
38domo | 1:e69854e65c88 | 122 | while (failedCounter < 3 && result != 0) { |
38domo | 1:e69854e65c88 | 123 | |
38domo | 1:e69854e65c88 | 124 | result = sendNoRetryJPA(componentId, samples, componentName, compTmplId, resultBuffer, resultBufferSize); |
38domo | 1:e69854e65c88 | 125 | |
38domo | 1:e69854e65c88 | 126 | this->printf("send result code: %d", result); |
38domo | 1:e69854e65c88 | 127 | |
38domo | 1:e69854e65c88 | 128 | if (result != 0) { |
38domo | 1:e69854e65c88 | 129 | //Wait and try again |
38domo | 1:e69854e65c88 | 130 | failedCounter++; |
38domo | 1:e69854e65c88 | 131 | this->printf("send failed %d times", failedCounter); |
38domo | 1:e69854e65c88 | 132 | wait(2); |
38domo | 1:e69854e65c88 | 133 | } |
38domo | 1:e69854e65c88 | 134 | |
38domo | 1:e69854e65c88 | 135 | if (failedCounter > 3 ) { |
38domo | 1:e69854e65c88 | 136 | this->printf("Too many failures. Restarting Ethernet."); |
38domo | 1:e69854e65c88 | 137 | startEthernet(); |
38domo | 1:e69854e65c88 | 138 | } |
38domo | 1:e69854e65c88 | 139 | } |
38domo | 1:e69854e65c88 | 140 | |
38domo | 1:e69854e65c88 | 141 | return result; |
38domo | 1:e69854e65c88 | 142 | } |
38domo | 1:e69854e65c88 | 143 | |
38domo | 1:e69854e65c88 | 144 | |
38domo | 1:e69854e65c88 | 145 | unsigned long GroveStreams::sendInitDatasJPA(const char* componentId, const char* samples, const char* componentName, |
38domo | 1:e69854e65c88 | 146 | const char* compTmplId, char* resultBuffer, size_t resultBufferSize) |
38domo | 1:e69854e65c88 | 147 | { |
38domo | 1:e69854e65c88 | 148 | int failedCounter = 0; |
38domo | 1:e69854e65c88 | 149 | int result = 1; |
38domo | 1:e69854e65c88 | 150 | |
38domo | 1:e69854e65c88 | 151 | while (failedCounter < 3 && result != 0) { |
38domo | 1:e69854e65c88 | 152 | |
38domo | 1:e69854e65c88 | 153 | result = sendNoRetryInitDatasJPA(componentId, samples, componentName, compTmplId, resultBuffer, resultBufferSize); |
38domo | 1:e69854e65c88 | 154 | |
38domo | 1:e69854e65c88 | 155 | this->printf("send result code: %d", result); |
38domo | 1:e69854e65c88 | 156 | |
38domo | 1:e69854e65c88 | 157 | if (result != 0) { |
38domo | 1:e69854e65c88 | 158 | //Wait and try again |
38domo | 1:e69854e65c88 | 159 | failedCounter++; |
38domo | 1:e69854e65c88 | 160 | this->printf("send failed %d times", failedCounter); |
38domo | 1:e69854e65c88 | 161 | wait(2); |
38domo | 1:e69854e65c88 | 162 | } |
38domo | 1:e69854e65c88 | 163 | |
38domo | 1:e69854e65c88 | 164 | if (failedCounter > 3 ) { |
38domo | 1:e69854e65c88 | 165 | this->printf("Too many failures. Restarting Ethernet."); |
38domo | 1:e69854e65c88 | 166 | startEthernet(); |
38domo | 1:e69854e65c88 | 167 | } |
38domo | 1:e69854e65c88 | 168 | } |
38domo | 1:e69854e65c88 | 169 | |
38domo | 1:e69854e65c88 | 170 | return result; |
38domo | 1:e69854e65c88 | 171 | } |
38domo | 1:e69854e65c88 | 172 | |
mmills | 0:8b675b2726b7 | 173 | unsigned long GroveStreams::sendNoRetry(const char* componentId, const char* samples, const char* componentName, |
mmills | 0:8b675b2726b7 | 174 | const char* compTmplId, char* resultBuffer, size_t resultBufferSize) |
mmills | 0:8b675b2726b7 | 175 | { |
mmills | 0:8b675b2726b7 | 176 | //Assemble the url that is used to pass the temperature readings to GroveStreams and call it |
mmills | 0:8b675b2726b7 | 177 | |
mmills | 0:8b675b2726b7 | 178 | network::tcp::Socket socket; |
mmills | 0:8b675b2726b7 | 179 | |
mmills | 0:8b675b2726b7 | 180 | int openResult = socket.open(); |
mmills | 0:8b675b2726b7 | 181 | if (openResult == 0) { |
mmills | 0:8b675b2726b7 | 182 | |
mmills | 0:8b675b2726b7 | 183 | int connResult = socket.connect(_domain, _port); |
mmills | 0:8b675b2726b7 | 184 | if(connResult == 0) { |
mmills | 0:8b675b2726b7 | 185 | |
mmills | 0:8b675b2726b7 | 186 | char compNamePart[100] = {0}; |
mmills | 0:8b675b2726b7 | 187 | if (strlen(componentName) > 0) { |
mmills | 0:8b675b2726b7 | 188 | sprintf(compNamePart, "&compName=%s",componentName); |
mmills | 0:8b675b2726b7 | 189 | } |
mmills | 0:8b675b2726b7 | 190 | |
mmills | 0:8b675b2726b7 | 191 | char compTmplIdPart[100] = {0}; |
mmills | 0:8b675b2726b7 | 192 | if (strlen(compTmplId) > 0) { |
mmills | 0:8b675b2726b7 | 193 | sprintf(compTmplIdPart, "&compTmplId=%s",compTmplId); |
mmills | 0:8b675b2726b7 | 194 | } |
mmills | 0:8b675b2726b7 | 195 | |
mmills | 0:8b675b2726b7 | 196 | //You may need to increase the size of sbuffer if any other char array sizes have increased |
mmills | 0:8b675b2726b7 | 197 | char sbuffer[512]= {0}; |
mmills | 0:8b675b2726b7 | 198 | |
38domo | 1:e69854e65c88 | 199 | sprintf(sbuffer, "PUT /stm32/datas?compId=%s%s%s&api_key=%s%s HTTP/1.1\r\nHost: \r\nConnection: close\r\nX-Forwarded-For:%s\r\n\r\n", |
38domo | 1:e69854e65c88 | 200 | componentId, compTmplIdPart, compNamePart, _apiKey, samples, _myIPAddress); |
38domo | 1:e69854e65c88 | 201 | |
38domo | 1:e69854e65c88 | 202 | this->printf("send sbuffer size: %d", strlen(sbuffer)); |
38domo | 1:e69854e65c88 | 203 | |
38domo | 1:e69854e65c88 | 204 | int scount = socket.write(sbuffer, strlen(sbuffer)); |
38domo | 1:e69854e65c88 | 205 | //this->printf("sent %d bytes: [%.*s]", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
38domo | 1:e69854e65c88 | 206 | |
38domo | 1:e69854e65c88 | 207 | //Read status line |
38domo | 1:e69854e65c88 | 208 | |
38domo | 1:e69854e65c88 | 209 | int rcount = socket.read(resultBuffer, resultBufferSize); |
38domo | 1:e69854e65c88 | 210 | |
38domo | 1:e69854e65c88 | 211 | this->printf("HTTP Response size: %d bytes", rcount); |
38domo | 1:e69854e65c88 | 212 | this->printf("HTTP Response Status:[%.*s]", strstr(resultBuffer, "\r\n")-resultBuffer, resultBuffer); |
38domo | 1:e69854e65c88 | 213 | |
38domo | 1:e69854e65c88 | 214 | //Only return the body |
38domo | 1:e69854e65c88 | 215 | strcpy(resultBuffer, strstr(resultBuffer, "\r\n\r\n") + strlen("\r\n\r\n")); |
38domo | 1:e69854e65c88 | 216 | strcat(resultBuffer, "\0"); |
38domo | 1:e69854e65c88 | 217 | //this->printf("HTTP Body: [%s]", resultBuffer); |
38domo | 1:e69854e65c88 | 218 | |
38domo | 1:e69854e65c88 | 219 | socket.close(); |
38domo | 1:e69854e65c88 | 220 | } else { |
38domo | 1:e69854e65c88 | 221 | |
38domo | 1:e69854e65c88 | 222 | this->printf("Socket Connect Failed: %d", connResult); |
38domo | 1:e69854e65c88 | 223 | } |
38domo | 1:e69854e65c88 | 224 | |
38domo | 1:e69854e65c88 | 225 | } else { |
38domo | 1:e69854e65c88 | 226 | this->printf("Socket Open Failed: %d", openResult); |
38domo | 1:e69854e65c88 | 227 | socket.close(); |
38domo | 1:e69854e65c88 | 228 | return 1; |
38domo | 1:e69854e65c88 | 229 | } |
38domo | 1:e69854e65c88 | 230 | |
38domo | 1:e69854e65c88 | 231 | return 0; |
38domo | 1:e69854e65c88 | 232 | } |
38domo | 1:e69854e65c88 | 233 | |
38domo | 1:e69854e65c88 | 234 | unsigned long GroveStreams::sendNoRetryJPA(const char* componentId, const char* samples, const char* componentName, |
38domo | 1:e69854e65c88 | 235 | const char* compTmplId, char* resultBuffer, size_t resultBufferSize) |
38domo | 1:e69854e65c88 | 236 | { |
38domo | 1:e69854e65c88 | 237 | //Assemble the url that is used to pass the temperature readings to GroveStreams and call it |
38domo | 1:e69854e65c88 | 238 | |
38domo | 1:e69854e65c88 | 239 | network::tcp::Socket socket; |
38domo | 1:e69854e65c88 | 240 | |
38domo | 1:e69854e65c88 | 241 | int openResult = socket.open(); |
38domo | 1:e69854e65c88 | 242 | if (openResult == 0) { |
38domo | 1:e69854e65c88 | 243 | |
38domo | 1:e69854e65c88 | 244 | int connResult = socket.connect(_domain, _port); |
38domo | 1:e69854e65c88 | 245 | if(connResult == 0) { |
38domo | 1:e69854e65c88 | 246 | |
38domo | 1:e69854e65c88 | 247 | char compNamePart[100] = {0}; |
38domo | 1:e69854e65c88 | 248 | if (strlen(componentName) > 0) { |
38domo | 1:e69854e65c88 | 249 | sprintf(compNamePart, "&compName=%s",componentName); |
38domo | 1:e69854e65c88 | 250 | } |
38domo | 1:e69854e65c88 | 251 | |
38domo | 1:e69854e65c88 | 252 | char compTmplIdPart[100] = {0}; |
38domo | 1:e69854e65c88 | 253 | if (strlen(compTmplId) > 0) { |
38domo | 1:e69854e65c88 | 254 | sprintf(compTmplIdPart, "&compTmplId=%s",compTmplId); |
38domo | 1:e69854e65c88 | 255 | } |
38domo | 1:e69854e65c88 | 256 | |
38domo | 1:e69854e65c88 | 257 | //You may need to increase the size of sbuffer if any other char array sizes have increased |
38domo | 1:e69854e65c88 | 258 | char sbuffer[512]= {0}; |
38domo | 1:e69854e65c88 | 259 | |
38domo | 1:e69854e65c88 | 260 | sprintf(sbuffer, "PUT /stm32/touches?compId=%s%s%s&api_key=%s%s HTTP/1.1\r\nHost: \r\nConnection: close\r\nX-Forwarded-For:%s\r\n\r\n", |
38domo | 1:e69854e65c88 | 261 | componentId, compTmplIdPart, compNamePart, _apiKey, samples, _myIPAddress); |
38domo | 1:e69854e65c88 | 262 | |
38domo | 1:e69854e65c88 | 263 | this->printf("send sbuffer size: %d", strlen(sbuffer)); |
38domo | 1:e69854e65c88 | 264 | |
38domo | 1:e69854e65c88 | 265 | int scount = socket.write(sbuffer, strlen(sbuffer)); |
38domo | 1:e69854e65c88 | 266 | //this->printf("sent %d bytes: [%.*s]", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
38domo | 1:e69854e65c88 | 267 | |
38domo | 1:e69854e65c88 | 268 | //Read status line |
38domo | 1:e69854e65c88 | 269 | |
38domo | 1:e69854e65c88 | 270 | int rcount = socket.read(resultBuffer, resultBufferSize); |
38domo | 1:e69854e65c88 | 271 | |
38domo | 1:e69854e65c88 | 272 | this->printf("HTTP Response size: %d bytes", rcount); |
38domo | 1:e69854e65c88 | 273 | this->printf("HTTP Response Status:[%.*s]", strstr(resultBuffer, "\r\n")-resultBuffer, resultBuffer); |
38domo | 1:e69854e65c88 | 274 | |
38domo | 1:e69854e65c88 | 275 | //Only return the body |
38domo | 1:e69854e65c88 | 276 | strcpy(resultBuffer, strstr(resultBuffer, "\r\n\r\n") + strlen("\r\n\r\n")); |
38domo | 1:e69854e65c88 | 277 | strcat(resultBuffer, "\0"); |
38domo | 1:e69854e65c88 | 278 | //this->printf("HTTP Body: [%s]", resultBuffer); |
38domo | 1:e69854e65c88 | 279 | |
38domo | 1:e69854e65c88 | 280 | socket.close(); |
38domo | 1:e69854e65c88 | 281 | } else { |
38domo | 1:e69854e65c88 | 282 | |
38domo | 1:e69854e65c88 | 283 | this->printf("Socket Connect Failed: %d", connResult); |
38domo | 1:e69854e65c88 | 284 | } |
38domo | 1:e69854e65c88 | 285 | |
38domo | 1:e69854e65c88 | 286 | } else { |
38domo | 1:e69854e65c88 | 287 | this->printf("Socket Open Failed: %d", openResult); |
38domo | 1:e69854e65c88 | 288 | socket.close(); |
38domo | 1:e69854e65c88 | 289 | return 1; |
38domo | 1:e69854e65c88 | 290 | } |
38domo | 1:e69854e65c88 | 291 | |
38domo | 1:e69854e65c88 | 292 | return 0; |
38domo | 1:e69854e65c88 | 293 | } |
38domo | 1:e69854e65c88 | 294 | |
38domo | 1:e69854e65c88 | 295 | unsigned long GroveStreams::sendNoRetryInitDatasJPA(const char* componentId, const char* samples, const char* componentName, |
38domo | 1:e69854e65c88 | 296 | const char* compTmplId, char* resultBuffer, size_t resultBufferSize) |
38domo | 1:e69854e65c88 | 297 | { |
38domo | 1:e69854e65c88 | 298 | //Assemble the url that is used to pass the temperature readings to GroveStreams and call it |
38domo | 1:e69854e65c88 | 299 | |
38domo | 1:e69854e65c88 | 300 | network::tcp::Socket socket; |
38domo | 1:e69854e65c88 | 301 | |
38domo | 1:e69854e65c88 | 302 | int openResult = socket.open(); |
38domo | 1:e69854e65c88 | 303 | if (openResult == 0) { |
38domo | 1:e69854e65c88 | 304 | //printf("\nConnect to socket : %s\n",_domain); |
38domo | 1:e69854e65c88 | 305 | int connResult = socket.connect(_domain, _port); |
38domo | 1:e69854e65c88 | 306 | if(connResult == 0) { |
38domo | 1:e69854e65c88 | 307 | |
38domo | 1:e69854e65c88 | 308 | char compNamePart[100] = {0}; |
38domo | 1:e69854e65c88 | 309 | if (strlen(componentName) > 0) { |
38domo | 1:e69854e65c88 | 310 | sprintf(compNamePart, "&compName=%s",componentName); |
38domo | 1:e69854e65c88 | 311 | } |
38domo | 1:e69854e65c88 | 312 | |
38domo | 1:e69854e65c88 | 313 | char compTmplIdPart[100] = {0}; |
38domo | 1:e69854e65c88 | 314 | if (strlen(compTmplId) > 0) { |
38domo | 1:e69854e65c88 | 315 | sprintf(compTmplIdPart, "&compTmplId=%s",compTmplId); |
38domo | 1:e69854e65c88 | 316 | } |
38domo | 1:e69854e65c88 | 317 | |
38domo | 1:e69854e65c88 | 318 | //You may need to increase the size of sbuffer if any other char array sizes have increased |
38domo | 1:e69854e65c88 | 319 | char sbuffer[512]= {0}; |
38domo | 1:e69854e65c88 | 320 | |
38domo | 1:e69854e65c88 | 321 | sprintf(sbuffer, "PUT /stm32/init?compId=%s%s%s&api_key=%s%s HTTP/1.1\r\nHost: \r\nConnection: close\r\nX-Forwarded-For:%s\r\n\r\n", |
mmills | 0:8b675b2726b7 | 322 | componentId, compTmplIdPart, compNamePart, _apiKey, samples, _myIPAddress); |
mmills | 0:8b675b2726b7 | 323 | |
mmills | 0:8b675b2726b7 | 324 | this->printf("send sbuffer size: %d", strlen(sbuffer)); |
mmills | 0:8b675b2726b7 | 325 | |
mmills | 0:8b675b2726b7 | 326 | int scount = socket.write(sbuffer, strlen(sbuffer)); |
mmills | 0:8b675b2726b7 | 327 | this->printf("sent %d bytes: [%.*s]", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
mmills | 0:8b675b2726b7 | 328 | |
mmills | 0:8b675b2726b7 | 329 | //Read status line |
mmills | 0:8b675b2726b7 | 330 | |
mmills | 0:8b675b2726b7 | 331 | int rcount = socket.read(resultBuffer, resultBufferSize); |
mmills | 0:8b675b2726b7 | 332 | |
mmills | 0:8b675b2726b7 | 333 | this->printf("HTTP Response size: %d bytes", rcount); |
mmills | 0:8b675b2726b7 | 334 | this->printf("HTTP Response Status:[%.*s]", strstr(resultBuffer, "\r\n")-resultBuffer, resultBuffer); |
mmills | 0:8b675b2726b7 | 335 | |
mmills | 0:8b675b2726b7 | 336 | //Only return the body |
mmills | 0:8b675b2726b7 | 337 | strcpy(resultBuffer, strstr(resultBuffer, "\r\n\r\n") + strlen("\r\n\r\n")); |
mmills | 0:8b675b2726b7 | 338 | strcat(resultBuffer, "\0"); |
mmills | 0:8b675b2726b7 | 339 | this->printf("HTTP Body: [%s]", resultBuffer); |
mmills | 0:8b675b2726b7 | 340 | |
mmills | 0:8b675b2726b7 | 341 | socket.close(); |
mmills | 0:8b675b2726b7 | 342 | } else { |
mmills | 0:8b675b2726b7 | 343 | |
mmills | 0:8b675b2726b7 | 344 | this->printf("Socket Connect Failed: %d", connResult); |
mmills | 0:8b675b2726b7 | 345 | } |
mmills | 0:8b675b2726b7 | 346 | |
mmills | 0:8b675b2726b7 | 347 | } else { |
mmills | 0:8b675b2726b7 | 348 | this->printf("Socket Open Failed: %d", openResult); |
mmills | 0:8b675b2726b7 | 349 | socket.close(); |
mmills | 0:8b675b2726b7 | 350 | return 1; |
mmills | 0:8b675b2726b7 | 351 | } |
mmills | 0:8b675b2726b7 | 352 | |
mmills | 0:8b675b2726b7 | 353 | return 0; |
mmills | 0:8b675b2726b7 | 354 | } |
mmills | 0:8b675b2726b7 | 355 | int GroveStreams::startEthernet() |
mmills | 0:8b675b2726b7 | 356 | { |
mmills | 0:8b675b2726b7 | 357 | //Start or restart the Ethernet connection. |
mmills | 0:8b675b2726b7 | 358 | int result = _eth.disconnect(); |
mmills | 0:8b675b2726b7 | 359 | if (result != 0) { |
mmills | 0:8b675b2726b7 | 360 | this->printf("Ethernet diconnect Failed: %d", result); |
mmills | 0:8b675b2726b7 | 361 | return result; |
mmills | 0:8b675b2726b7 | 362 | } |
mmills | 0:8b675b2726b7 | 363 | |
mmills | 0:8b675b2726b7 | 364 | this->printf("Connecting STM32 to network..."); |
mmills | 0:8b675b2726b7 | 365 | |
mmills | 0:8b675b2726b7 | 366 | //Connect to the network and obtain an IP address using DHCP |
mmills | 0:8b675b2726b7 | 367 | result = _eth.init(); |
mmills | 0:8b675b2726b7 | 368 | if (result != 0) { |
mmills | 0:8b675b2726b7 | 369 | this->printf("Ethernet init Failed: %d", result); |
mmills | 0:8b675b2726b7 | 370 | return result; |
mmills | 0:8b675b2726b7 | 371 | } |
mmills | 0:8b675b2726b7 | 372 | |
mmills | 0:8b675b2726b7 | 373 | result = _eth.connect(); |
mmills | 0:8b675b2726b7 | 374 | if (result != 0) { |
mmills | 0:8b675b2726b7 | 375 | this->printf("Ethernet connect Failed: %d", result); |
mmills | 0:8b675b2726b7 | 376 | return result; |
mmills | 0:8b675b2726b7 | 377 | } else { |
mmills | 0:8b675b2726b7 | 378 | this->printf("STM32 connected to network"); |
mmills | 0:8b675b2726b7 | 379 | |
mmills | 0:8b675b2726b7 | 380 | //Set the mac and ip variables so that they can be used during sensor uploads later |
mmills | 0:8b675b2726b7 | 381 | _myIPAddress = _eth.getIPAddress(); |
mmills | 0:8b675b2726b7 | 382 | |
mmills | 0:8b675b2726b7 | 383 | this->printf("IP address: %s", _myIPAddress ? _myIPAddress : "No IP"); |
mmills | 0:8b675b2726b7 | 384 | } |
mmills | 0:8b675b2726b7 | 385 | |
mmills | 0:8b675b2726b7 | 386 | return 0; |
mmills | 0:8b675b2726b7 | 387 | } |
mmills | 0:8b675b2726b7 | 388 | |
mmills | 0:8b675b2726b7 | 389 | const char* GroveStreams::getMACAddress() |
mmills | 0:8b675b2726b7 | 390 | { |
mmills | 0:8b675b2726b7 | 391 | return _eth.getMACAddress(); |
mmills | 0:8b675b2726b7 | 392 | } |
38domo | 1:e69854e65c88 | 393 |