discof746 ethernet+lcd+ts2
Dependents: grove_stream_jpa_sd2 grove_stream_jpa_sd2 grove_stream_jpa_sd2-2 grove_stream_jpa_sd2-3
GroveStreams.cpp@2:b77ba7f2611b, 2020-09-15 (annotated)
- Committer:
- 38domo
- Date:
- Tue Sep 15 19:05:59 2020 +0000
- Revision:
- 2:b77ba7f2611b
- Parent:
- 1:e69854e65c88
- Child:
- 3:494da6893902
version GC avec read datas from sd
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 | 2:b77ba7f2611b | 82 | // ------------------------------------------------ |
mmills | 0:8b675b2726b7 | 83 | unsigned long GroveStreams::send(const char* componentId, const char* samples, char* resultBuffer, size_t resultBufferSize) |
38domo | 2:b77ba7f2611b | 84 | { return send(componentId, samples, NULL, NULL, resultBuffer, resultBufferSize); |
mmills | 0:8b675b2726b7 | 85 | } |
mmills | 0:8b675b2726b7 | 86 | |
mmills | 0:8b675b2726b7 | 87 | unsigned long GroveStreams::send(const char* componentId, const char* samples, const char* componentName, |
mmills | 0:8b675b2726b7 | 88 | const char* compTmplId, char* resultBuffer, size_t resultBufferSize) |
mmills | 0:8b675b2726b7 | 89 | { |
mmills | 0:8b675b2726b7 | 90 | int failedCounter = 0; |
mmills | 0:8b675b2726b7 | 91 | int result = 1; |
mmills | 0:8b675b2726b7 | 92 | |
mmills | 0:8b675b2726b7 | 93 | while (failedCounter < 3 && result != 0) { |
mmills | 0:8b675b2726b7 | 94 | |
mmills | 0:8b675b2726b7 | 95 | result = sendNoRetry(componentId, samples, componentName, compTmplId, resultBuffer, resultBufferSize); |
mmills | 0:8b675b2726b7 | 96 | |
mmills | 0:8b675b2726b7 | 97 | this->printf("send result code: %d", result); |
mmills | 0:8b675b2726b7 | 98 | |
mmills | 0:8b675b2726b7 | 99 | if (result != 0) { |
mmills | 0:8b675b2726b7 | 100 | //Wait and try again |
mmills | 0:8b675b2726b7 | 101 | failedCounter++; |
mmills | 0:8b675b2726b7 | 102 | this->printf("send failed %d times", failedCounter); |
mmills | 0:8b675b2726b7 | 103 | wait(2); |
mmills | 0:8b675b2726b7 | 104 | } |
mmills | 0:8b675b2726b7 | 105 | |
mmills | 0:8b675b2726b7 | 106 | if (failedCounter > 3 ) { |
mmills | 0:8b675b2726b7 | 107 | this->printf("Too many failures. Restarting Ethernet."); |
mmills | 0:8b675b2726b7 | 108 | startEthernet(); |
mmills | 0:8b675b2726b7 | 109 | } |
mmills | 0:8b675b2726b7 | 110 | } |
mmills | 0:8b675b2726b7 | 111 | |
mmills | 0:8b675b2726b7 | 112 | return result; |
mmills | 0:8b675b2726b7 | 113 | } |
mmills | 0:8b675b2726b7 | 114 | |
38domo | 1:e69854e65c88 | 115 | unsigned long GroveStreams::sendJPA(const char* componentId, const char* samples, const char* componentName, |
38domo | 1:e69854e65c88 | 116 | const char* compTmplId, char* resultBuffer, size_t resultBufferSize) |
38domo | 1:e69854e65c88 | 117 | { |
38domo | 1:e69854e65c88 | 118 | int failedCounter = 0; |
38domo | 1:e69854e65c88 | 119 | int result = 1; |
38domo | 1:e69854e65c88 | 120 | |
38domo | 1:e69854e65c88 | 121 | while (failedCounter < 3 && result != 0) { |
38domo | 1:e69854e65c88 | 122 | |
38domo | 1:e69854e65c88 | 123 | result = sendNoRetryJPA(componentId, samples, componentName, compTmplId, resultBuffer, resultBufferSize); |
38domo | 1:e69854e65c88 | 124 | |
38domo | 1:e69854e65c88 | 125 | this->printf("send result code: %d", result); |
38domo | 1:e69854e65c88 | 126 | |
38domo | 1:e69854e65c88 | 127 | if (result != 0) { |
38domo | 1:e69854e65c88 | 128 | //Wait and try again |
38domo | 1:e69854e65c88 | 129 | failedCounter++; |
38domo | 1:e69854e65c88 | 130 | this->printf("send failed %d times", failedCounter); |
38domo | 1:e69854e65c88 | 131 | wait(2); |
38domo | 1:e69854e65c88 | 132 | } |
38domo | 1:e69854e65c88 | 133 | |
38domo | 1:e69854e65c88 | 134 | if (failedCounter > 3 ) { |
38domo | 1:e69854e65c88 | 135 | this->printf("Too many failures. Restarting Ethernet."); |
38domo | 1:e69854e65c88 | 136 | startEthernet(); |
38domo | 1:e69854e65c88 | 137 | } |
38domo | 1:e69854e65c88 | 138 | } |
38domo | 1:e69854e65c88 | 139 | |
38domo | 1:e69854e65c88 | 140 | return result; |
38domo | 1:e69854e65c88 | 141 | } |
38domo | 1:e69854e65c88 | 142 | |
38domo | 1:e69854e65c88 | 143 | |
38domo | 1:e69854e65c88 | 144 | unsigned long GroveStreams::sendInitDatasJPA(const char* componentId, const char* samples, const char* componentName, |
38domo | 1:e69854e65c88 | 145 | const char* compTmplId, char* resultBuffer, size_t resultBufferSize) |
38domo | 1:e69854e65c88 | 146 | { |
38domo | 1:e69854e65c88 | 147 | int failedCounter = 0; |
38domo | 1:e69854e65c88 | 148 | int result = 1; |
38domo | 1:e69854e65c88 | 149 | |
38domo | 1:e69854e65c88 | 150 | while (failedCounter < 3 && result != 0) { |
38domo | 1:e69854e65c88 | 151 | |
38domo | 1:e69854e65c88 | 152 | result = sendNoRetryInitDatasJPA(componentId, samples, componentName, compTmplId, resultBuffer, resultBufferSize); |
38domo | 1:e69854e65c88 | 153 | |
38domo | 1:e69854e65c88 | 154 | this->printf("send result code: %d", result); |
38domo | 1:e69854e65c88 | 155 | |
38domo | 1:e69854e65c88 | 156 | if (result != 0) { |
38domo | 1:e69854e65c88 | 157 | //Wait and try again |
38domo | 1:e69854e65c88 | 158 | failedCounter++; |
38domo | 1:e69854e65c88 | 159 | this->printf("send failed %d times", failedCounter); |
38domo | 1:e69854e65c88 | 160 | wait(2); |
38domo | 1:e69854e65c88 | 161 | } |
38domo | 1:e69854e65c88 | 162 | |
38domo | 1:e69854e65c88 | 163 | if (failedCounter > 3 ) { |
38domo | 1:e69854e65c88 | 164 | this->printf("Too many failures. Restarting Ethernet."); |
38domo | 1:e69854e65c88 | 165 | startEthernet(); |
38domo | 1:e69854e65c88 | 166 | } |
38domo | 1:e69854e65c88 | 167 | } |
38domo | 1:e69854e65c88 | 168 | |
38domo | 1:e69854e65c88 | 169 | return result; |
38domo | 1:e69854e65c88 | 170 | } |
38domo | 1:e69854e65c88 | 171 | |
mmills | 0:8b675b2726b7 | 172 | unsigned long GroveStreams::sendNoRetry(const char* componentId, const char* samples, const char* componentName, |
mmills | 0:8b675b2726b7 | 173 | const char* compTmplId, char* resultBuffer, size_t resultBufferSize) |
mmills | 0:8b675b2726b7 | 174 | { |
mmills | 0:8b675b2726b7 | 175 | //Assemble the url that is used to pass the temperature readings to GroveStreams and call it |
mmills | 0:8b675b2726b7 | 176 | |
mmills | 0:8b675b2726b7 | 177 | network::tcp::Socket socket; |
mmills | 0:8b675b2726b7 | 178 | |
mmills | 0:8b675b2726b7 | 179 | int openResult = socket.open(); |
mmills | 0:8b675b2726b7 | 180 | if (openResult == 0) { |
mmills | 0:8b675b2726b7 | 181 | |
mmills | 0:8b675b2726b7 | 182 | int connResult = socket.connect(_domain, _port); |
mmills | 0:8b675b2726b7 | 183 | if(connResult == 0) { |
mmills | 0:8b675b2726b7 | 184 | |
mmills | 0:8b675b2726b7 | 185 | char compNamePart[100] = {0}; |
mmills | 0:8b675b2726b7 | 186 | if (strlen(componentName) > 0) { |
mmills | 0:8b675b2726b7 | 187 | sprintf(compNamePart, "&compName=%s",componentName); |
mmills | 0:8b675b2726b7 | 188 | } |
mmills | 0:8b675b2726b7 | 189 | |
mmills | 0:8b675b2726b7 | 190 | char compTmplIdPart[100] = {0}; |
mmills | 0:8b675b2726b7 | 191 | if (strlen(compTmplId) > 0) { |
mmills | 0:8b675b2726b7 | 192 | sprintf(compTmplIdPart, "&compTmplId=%s",compTmplId); |
mmills | 0:8b675b2726b7 | 193 | } |
mmills | 0:8b675b2726b7 | 194 | |
mmills | 0:8b675b2726b7 | 195 | //You may need to increase the size of sbuffer if any other char array sizes have increased |
mmills | 0:8b675b2726b7 | 196 | char sbuffer[512]= {0}; |
mmills | 0:8b675b2726b7 | 197 | |
38domo | 1:e69854e65c88 | 198 | 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 | 199 | componentId, compTmplIdPart, compNamePart, _apiKey, samples, _myIPAddress); |
38domo | 1:e69854e65c88 | 200 | |
38domo | 1:e69854e65c88 | 201 | this->printf("send sbuffer size: %d", strlen(sbuffer)); |
38domo | 1:e69854e65c88 | 202 | |
38domo | 1:e69854e65c88 | 203 | int scount = socket.write(sbuffer, strlen(sbuffer)); |
38domo | 1:e69854e65c88 | 204 | //this->printf("sent %d bytes: [%.*s]", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
38domo | 1:e69854e65c88 | 205 | |
38domo | 1:e69854e65c88 | 206 | //Read status line |
38domo | 1:e69854e65c88 | 207 | |
38domo | 1:e69854e65c88 | 208 | int rcount = socket.read(resultBuffer, resultBufferSize); |
38domo | 1:e69854e65c88 | 209 | |
38domo | 1:e69854e65c88 | 210 | this->printf("HTTP Response size: %d bytes", rcount); |
38domo | 1:e69854e65c88 | 211 | this->printf("HTTP Response Status:[%.*s]", strstr(resultBuffer, "\r\n")-resultBuffer, resultBuffer); |
38domo | 1:e69854e65c88 | 212 | |
38domo | 1:e69854e65c88 | 213 | //Only return the body |
38domo | 1:e69854e65c88 | 214 | strcpy(resultBuffer, strstr(resultBuffer, "\r\n\r\n") + strlen("\r\n\r\n")); |
38domo | 1:e69854e65c88 | 215 | strcat(resultBuffer, "\0"); |
38domo | 1:e69854e65c88 | 216 | //this->printf("HTTP Body: [%s]", resultBuffer); |
38domo | 1:e69854e65c88 | 217 | |
38domo | 1:e69854e65c88 | 218 | socket.close(); |
38domo | 1:e69854e65c88 | 219 | } else { |
38domo | 1:e69854e65c88 | 220 | |
38domo | 1:e69854e65c88 | 221 | this->printf("Socket Connect Failed: %d", connResult); |
38domo | 1:e69854e65c88 | 222 | } |
38domo | 1:e69854e65c88 | 223 | |
38domo | 1:e69854e65c88 | 224 | } else { |
38domo | 1:e69854e65c88 | 225 | this->printf("Socket Open Failed: %d", openResult); |
38domo | 1:e69854e65c88 | 226 | socket.close(); |
38domo | 1:e69854e65c88 | 227 | return 1; |
38domo | 1:e69854e65c88 | 228 | } |
38domo | 1:e69854e65c88 | 229 | |
38domo | 1:e69854e65c88 | 230 | return 0; |
38domo | 1:e69854e65c88 | 231 | } |
38domo | 1:e69854e65c88 | 232 | |
38domo | 1:e69854e65c88 | 233 | unsigned long GroveStreams::sendNoRetryJPA(const char* componentId, const char* samples, const char* componentName, |
38domo | 1:e69854e65c88 | 234 | const char* compTmplId, char* resultBuffer, size_t resultBufferSize) |
38domo | 1:e69854e65c88 | 235 | { |
38domo | 1:e69854e65c88 | 236 | //Assemble the url that is used to pass the temperature readings to GroveStreams and call it |
38domo | 1:e69854e65c88 | 237 | |
38domo | 1:e69854e65c88 | 238 | network::tcp::Socket socket; |
38domo | 1:e69854e65c88 | 239 | |
38domo | 1:e69854e65c88 | 240 | int openResult = socket.open(); |
38domo | 1:e69854e65c88 | 241 | if (openResult == 0) { |
38domo | 1:e69854e65c88 | 242 | |
38domo | 1:e69854e65c88 | 243 | int connResult = socket.connect(_domain, _port); |
38domo | 1:e69854e65c88 | 244 | if(connResult == 0) { |
38domo | 1:e69854e65c88 | 245 | |
38domo | 1:e69854e65c88 | 246 | char compNamePart[100] = {0}; |
38domo | 1:e69854e65c88 | 247 | if (strlen(componentName) > 0) { |
38domo | 1:e69854e65c88 | 248 | sprintf(compNamePart, "&compName=%s",componentName); |
38domo | 1:e69854e65c88 | 249 | } |
38domo | 1:e69854e65c88 | 250 | |
38domo | 1:e69854e65c88 | 251 | char compTmplIdPart[100] = {0}; |
38domo | 1:e69854e65c88 | 252 | if (strlen(compTmplId) > 0) { |
38domo | 1:e69854e65c88 | 253 | sprintf(compTmplIdPart, "&compTmplId=%s",compTmplId); |
38domo | 1:e69854e65c88 | 254 | } |
38domo | 1:e69854e65c88 | 255 | |
38domo | 1:e69854e65c88 | 256 | //You may need to increase the size of sbuffer if any other char array sizes have increased |
38domo | 1:e69854e65c88 | 257 | char sbuffer[512]= {0}; |
38domo | 1:e69854e65c88 | 258 | |
38domo | 1:e69854e65c88 | 259 | 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 | 260 | componentId, compTmplIdPart, compNamePart, _apiKey, samples, _myIPAddress); |
38domo | 1:e69854e65c88 | 261 | |
38domo | 1:e69854e65c88 | 262 | this->printf("send sbuffer size: %d", strlen(sbuffer)); |
38domo | 1:e69854e65c88 | 263 | |
38domo | 1:e69854e65c88 | 264 | int scount = socket.write(sbuffer, strlen(sbuffer)); |
38domo | 1:e69854e65c88 | 265 | //this->printf("sent %d bytes: [%.*s]", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
38domo | 1:e69854e65c88 | 266 | |
38domo | 1:e69854e65c88 | 267 | //Read status line |
38domo | 1:e69854e65c88 | 268 | |
38domo | 1:e69854e65c88 | 269 | int rcount = socket.read(resultBuffer, resultBufferSize); |
38domo | 1:e69854e65c88 | 270 | |
38domo | 1:e69854e65c88 | 271 | this->printf("HTTP Response size: %d bytes", rcount); |
38domo | 1:e69854e65c88 | 272 | this->printf("HTTP Response Status:[%.*s]", strstr(resultBuffer, "\r\n")-resultBuffer, resultBuffer); |
38domo | 1:e69854e65c88 | 273 | |
38domo | 1:e69854e65c88 | 274 | //Only return the body |
38domo | 1:e69854e65c88 | 275 | strcpy(resultBuffer, strstr(resultBuffer, "\r\n\r\n") + strlen("\r\n\r\n")); |
38domo | 1:e69854e65c88 | 276 | strcat(resultBuffer, "\0"); |
38domo | 1:e69854e65c88 | 277 | //this->printf("HTTP Body: [%s]", resultBuffer); |
38domo | 1:e69854e65c88 | 278 | |
38domo | 1:e69854e65c88 | 279 | socket.close(); |
38domo | 1:e69854e65c88 | 280 | } else { |
38domo | 1:e69854e65c88 | 281 | |
38domo | 1:e69854e65c88 | 282 | this->printf("Socket Connect Failed: %d", connResult); |
38domo | 1:e69854e65c88 | 283 | } |
38domo | 1:e69854e65c88 | 284 | |
38domo | 1:e69854e65c88 | 285 | } else { |
38domo | 1:e69854e65c88 | 286 | this->printf("Socket Open Failed: %d", openResult); |
38domo | 1:e69854e65c88 | 287 | socket.close(); |
38domo | 1:e69854e65c88 | 288 | return 1; |
38domo | 1:e69854e65c88 | 289 | } |
38domo | 1:e69854e65c88 | 290 | |
38domo | 1:e69854e65c88 | 291 | return 0; |
38domo | 1:e69854e65c88 | 292 | } |
38domo | 1:e69854e65c88 | 293 | |
38domo | 1:e69854e65c88 | 294 | unsigned long GroveStreams::sendNoRetryInitDatasJPA(const char* componentId, const char* samples, const char* componentName, |
38domo | 1:e69854e65c88 | 295 | const char* compTmplId, char* resultBuffer, size_t resultBufferSize) |
38domo | 1:e69854e65c88 | 296 | { |
38domo | 1:e69854e65c88 | 297 | //Assemble the url that is used to pass the temperature readings to GroveStreams and call it |
38domo | 1:e69854e65c88 | 298 | |
38domo | 1:e69854e65c88 | 299 | network::tcp::Socket socket; |
38domo | 1:e69854e65c88 | 300 | |
38domo | 1:e69854e65c88 | 301 | int openResult = socket.open(); |
38domo | 1:e69854e65c88 | 302 | if (openResult == 0) { |
38domo | 1:e69854e65c88 | 303 | //printf("\nConnect to socket : %s\n",_domain); |
38domo | 1:e69854e65c88 | 304 | int connResult = socket.connect(_domain, _port); |
38domo | 1:e69854e65c88 | 305 | if(connResult == 0) { |
38domo | 1:e69854e65c88 | 306 | |
38domo | 1:e69854e65c88 | 307 | char compNamePart[100] = {0}; |
38domo | 1:e69854e65c88 | 308 | if (strlen(componentName) > 0) { |
38domo | 1:e69854e65c88 | 309 | sprintf(compNamePart, "&compName=%s",componentName); |
38domo | 1:e69854e65c88 | 310 | } |
38domo | 1:e69854e65c88 | 311 | |
38domo | 1:e69854e65c88 | 312 | char compTmplIdPart[100] = {0}; |
38domo | 1:e69854e65c88 | 313 | if (strlen(compTmplId) > 0) { |
38domo | 1:e69854e65c88 | 314 | sprintf(compTmplIdPart, "&compTmplId=%s",compTmplId); |
38domo | 1:e69854e65c88 | 315 | } |
38domo | 1:e69854e65c88 | 316 | |
38domo | 1:e69854e65c88 | 317 | //You may need to increase the size of sbuffer if any other char array sizes have increased |
38domo | 1:e69854e65c88 | 318 | char sbuffer[512]= {0}; |
38domo | 1:e69854e65c88 | 319 | |
38domo | 1:e69854e65c88 | 320 | 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 | 321 | componentId, compTmplIdPart, compNamePart, _apiKey, samples, _myIPAddress); |
mmills | 0:8b675b2726b7 | 322 | |
mmills | 0:8b675b2726b7 | 323 | this->printf("send sbuffer size: %d", strlen(sbuffer)); |
mmills | 0:8b675b2726b7 | 324 | |
mmills | 0:8b675b2726b7 | 325 | int scount = socket.write(sbuffer, strlen(sbuffer)); |
mmills | 0:8b675b2726b7 | 326 | this->printf("sent %d bytes: [%.*s]", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
mmills | 0:8b675b2726b7 | 327 | |
mmills | 0:8b675b2726b7 | 328 | //Read status line |
mmills | 0:8b675b2726b7 | 329 | |
mmills | 0:8b675b2726b7 | 330 | int rcount = socket.read(resultBuffer, resultBufferSize); |
mmills | 0:8b675b2726b7 | 331 | |
mmills | 0:8b675b2726b7 | 332 | this->printf("HTTP Response size: %d bytes", rcount); |
mmills | 0:8b675b2726b7 | 333 | this->printf("HTTP Response Status:[%.*s]", strstr(resultBuffer, "\r\n")-resultBuffer, resultBuffer); |
mmills | 0:8b675b2726b7 | 334 | |
mmills | 0:8b675b2726b7 | 335 | //Only return the body |
mmills | 0:8b675b2726b7 | 336 | strcpy(resultBuffer, strstr(resultBuffer, "\r\n\r\n") + strlen("\r\n\r\n")); |
mmills | 0:8b675b2726b7 | 337 | strcat(resultBuffer, "\0"); |
mmills | 0:8b675b2726b7 | 338 | this->printf("HTTP Body: [%s]", resultBuffer); |
mmills | 0:8b675b2726b7 | 339 | |
mmills | 0:8b675b2726b7 | 340 | socket.close(); |
mmills | 0:8b675b2726b7 | 341 | } else { |
mmills | 0:8b675b2726b7 | 342 | |
mmills | 0:8b675b2726b7 | 343 | this->printf("Socket Connect Failed: %d", connResult); |
mmills | 0:8b675b2726b7 | 344 | } |
mmills | 0:8b675b2726b7 | 345 | |
mmills | 0:8b675b2726b7 | 346 | } else { |
mmills | 0:8b675b2726b7 | 347 | this->printf("Socket Open Failed: %d", openResult); |
mmills | 0:8b675b2726b7 | 348 | socket.close(); |
mmills | 0:8b675b2726b7 | 349 | return 1; |
mmills | 0:8b675b2726b7 | 350 | } |
mmills | 0:8b675b2726b7 | 351 | |
mmills | 0:8b675b2726b7 | 352 | return 0; |
mmills | 0:8b675b2726b7 | 353 | } |
mmills | 0:8b675b2726b7 | 354 | int GroveStreams::startEthernet() |
mmills | 0:8b675b2726b7 | 355 | { |
mmills | 0:8b675b2726b7 | 356 | //Start or restart the Ethernet connection. |
mmills | 0:8b675b2726b7 | 357 | int result = _eth.disconnect(); |
mmills | 0:8b675b2726b7 | 358 | if (result != 0) { |
mmills | 0:8b675b2726b7 | 359 | this->printf("Ethernet diconnect Failed: %d", result); |
mmills | 0:8b675b2726b7 | 360 | return result; |
mmills | 0:8b675b2726b7 | 361 | } |
mmills | 0:8b675b2726b7 | 362 | |
mmills | 0:8b675b2726b7 | 363 | this->printf("Connecting STM32 to network..."); |
mmills | 0:8b675b2726b7 | 364 | |
mmills | 0:8b675b2726b7 | 365 | //Connect to the network and obtain an IP address using DHCP |
mmills | 0:8b675b2726b7 | 366 | result = _eth.init(); |
mmills | 0:8b675b2726b7 | 367 | if (result != 0) { |
mmills | 0:8b675b2726b7 | 368 | this->printf("Ethernet init Failed: %d", result); |
mmills | 0:8b675b2726b7 | 369 | return result; |
mmills | 0:8b675b2726b7 | 370 | } |
mmills | 0:8b675b2726b7 | 371 | |
mmills | 0:8b675b2726b7 | 372 | result = _eth.connect(); |
mmills | 0:8b675b2726b7 | 373 | if (result != 0) { |
mmills | 0:8b675b2726b7 | 374 | this->printf("Ethernet connect Failed: %d", result); |
mmills | 0:8b675b2726b7 | 375 | return result; |
mmills | 0:8b675b2726b7 | 376 | } else { |
mmills | 0:8b675b2726b7 | 377 | this->printf("STM32 connected to network"); |
mmills | 0:8b675b2726b7 | 378 | |
mmills | 0:8b675b2726b7 | 379 | //Set the mac and ip variables so that they can be used during sensor uploads later |
mmills | 0:8b675b2726b7 | 380 | _myIPAddress = _eth.getIPAddress(); |
mmills | 0:8b675b2726b7 | 381 | |
mmills | 0:8b675b2726b7 | 382 | this->printf("IP address: %s", _myIPAddress ? _myIPAddress : "No IP"); |
mmills | 0:8b675b2726b7 | 383 | } |
mmills | 0:8b675b2726b7 | 384 | |
mmills | 0:8b675b2726b7 | 385 | return 0; |
mmills | 0:8b675b2726b7 | 386 | } |
mmills | 0:8b675b2726b7 | 387 | |
mmills | 0:8b675b2726b7 | 388 | const char* GroveStreams::getMACAddress() |
mmills | 0:8b675b2726b7 | 389 | { |
mmills | 0:8b675b2726b7 | 390 | return _eth.getMACAddress(); |
mmills | 0:8b675b2726b7 | 391 | } |
38domo | 1:e69854e65c88 | 392 |