discof746 ethernet+lcd+ts2
Dependents: grove_stream_jpa_sd2 grove_stream_jpa_sd2 grove_stream_jpa_sd2-2 grove_stream_jpa_sd2-3
Revision 3:494da6893902, committed 2021-01-13
- Comitter:
- 38domo
- Date:
- Wed Jan 13 19:51:28 2021 +0000
- Parent:
- 2:b77ba7f2611b
- Commit message:
- update grove sreams lib
Changed in this revision
GroveStreams.cpp | Show annotated file Show diff for this revision Revisions of this file |
GroveStreams.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r b77ba7f2611b -r 494da6893902 GroveStreams.cpp --- a/GroveStreams.cpp Tue Sep 15 19:05:59 2020 +0000 +++ b/GroveStreams.cpp Wed Jan 13 19:51:28 2021 +0000 @@ -1,30 +1,14 @@ /* GroveStreams is a helper class to assist with pushing data into the - GroveStreams.com IoT platform. - - License: - Copyright (C) 2017 GroveStreams LLC. - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. + GroveStreams.com IoT platform. */ - #include "GroveStreams.h" - GroveStreams::GroveStreams(const char* apiKey, LcdDiscoF746NgTracer* pLcd) -{ - init(apiKey, pLcd); +{ init(apiKey, pLcd); } GroveStreams::GroveStreams(const char* apiKey) -{ - init(apiKey, NULL); +{ init(apiKey, NULL); } GroveStreams::~GroveStreams() { @@ -34,23 +18,17 @@ extern char gsStreamIPdatas[]; static char* _domain; int GroveStreams::init(const char* apiKey, LcdDiscoF746NgTracer* pLcd) -{ - - _domain = gsStreamIPdatas; +{ _domain = gsStreamIPdatas; _port = 1880; _apiKey = apiKey; _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. _pLcd = pLcd; - return startEthernet(); } void GroveStreams::printf(const char* format, ...) -{ - - va_list args; +{ va_list args; va_start(args, format); - if (_pLcd != NULL) { char sbuffer[512]; vsprintf(sbuffer, format, args); @@ -58,25 +36,21 @@ } else { vprintf(format, args); } - va_end(args); } unsigned long GroveStreams::send(const char* componentId, const char* samples) -{ - char resultBuffer[256]= {0}; +{ char resultBuffer[256]= {0}; return send(componentId, samples, NULL, NULL, resultBuffer, sizeof resultBuffer); } unsigned long GroveStreams::sendJPA(const char* componentId, const char* samples) -{ - char resultBuffer[256]= {0}; +{ char resultBuffer[256]= {0}; return sendJPA(componentId, samples, NULL, NULL, resultBuffer, sizeof resultBuffer); } unsigned long GroveStreams::sendInitDatasJPA(const char* componentId, const char* samples) -{ - char resultBuffer[256]= {0}; +{ char resultBuffer[256]= {0}; return sendInitDatasJPA(componentId, samples, NULL, NULL, resultBuffer, sizeof resultBuffer); } // ------------------------------------------------ @@ -86,307 +60,225 @@ unsigned long GroveStreams::send(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize) -{ - int failedCounter = 0; +{ int failedCounter = 0; int result = 1; - - while (failedCounter < 3 && result != 0) { - - result = sendNoRetry(componentId, samples, componentName, compTmplId, resultBuffer, resultBufferSize); - - this->printf("send result code: %d", result); - + while (failedCounter < 3 && result != 0) { + result = sendNoRetry(componentId, samples, componentName, compTmplId, resultBuffer, resultBufferSize); + this->printf("send result code: %d", result); if (result != 0) { //Wait and try again failedCounter++; this->printf("send failed %d times", failedCounter); wait(2); } - if (failedCounter > 3 ) { this->printf("Too many failures. Restarting Ethernet."); startEthernet(); } } - return result; } unsigned long GroveStreams::sendJPA(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize) -{ - int failedCounter = 0; +{ int failedCounter = 0; int result = 1; - - while (failedCounter < 3 && result != 0) { - - result = sendNoRetryJPA(componentId, samples, componentName, compTmplId, resultBuffer, resultBufferSize); - - this->printf("send result code: %d", result); - + while (failedCounter < 3 && result != 0) { + result = sendNoRetryJPA(componentId, samples, componentName, compTmplId, resultBuffer, resultBufferSize); + this->printf("send result code: %d", result); if (result != 0) { //Wait and try again failedCounter++; this->printf("send failed %d times", failedCounter); wait(2); } - if (failedCounter > 3 ) { this->printf("Too many failures. Restarting Ethernet."); startEthernet(); } } - return result; } - unsigned long GroveStreams::sendInitDatasJPA(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize) -{ - int failedCounter = 0; +{ int failedCounter = 0; int result = 1; - - while (failedCounter < 3 && result != 0) { - - result = sendNoRetryInitDatasJPA(componentId, samples, componentName, compTmplId, resultBuffer, resultBufferSize); - - this->printf("send result code: %d", result); - + while (failedCounter < 3 && result != 0) { + result = sendNoRetryInitDatasJPA(componentId, samples, componentName, compTmplId, resultBuffer, resultBufferSize); + this->printf("send result code: %d", result); if (result != 0) { //Wait and try again failedCounter++; this->printf("send failed %d times", failedCounter); wait(2); } - if (failedCounter > 3 ) { this->printf("Too many failures. Restarting Ethernet."); startEthernet(); } } - return result; } unsigned long GroveStreams::sendNoRetry(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize) -{ - //Assemble the url that is used to pass the temperature readings to GroveStreams and call it - +{ //Assemble the url that is used to pass the temperature readings to GroveStreams and call it network::tcp::Socket socket; - int openResult = socket.open(); if (openResult == 0) { - int connResult = socket.connect(_domain, _port); if(connResult == 0) { - char compNamePart[100] = {0}; if (strlen(componentName) > 0) { sprintf(compNamePart, "&compName=%s",componentName); } - char compTmplIdPart[100] = {0}; if (strlen(compTmplId) > 0) { sprintf(compTmplIdPart, "&compTmplId=%s",compTmplId); } - //You may need to increase the size of sbuffer if any other char array sizes have increased char sbuffer[512]= {0}; - 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", componentId, compTmplIdPart, compNamePart, _apiKey, samples, _myIPAddress); - this->printf("send sbuffer size: %d", strlen(sbuffer)); - int scount = socket.write(sbuffer, strlen(sbuffer)); //this->printf("sent %d bytes: [%.*s]", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); - - //Read status line - + //Read status line int rcount = socket.read(resultBuffer, resultBufferSize); - this->printf("HTTP Response size: %d bytes", rcount); - this->printf("HTTP Response Status:[%.*s]", strstr(resultBuffer, "\r\n")-resultBuffer, resultBuffer); - + this->printf("HTTP Response Status:[%.*s]", strstr(resultBuffer, "\r\n")-resultBuffer, resultBuffer); //Only return the body strcpy(resultBuffer, strstr(resultBuffer, "\r\n\r\n") + strlen("\r\n\r\n")); strcat(resultBuffer, "\0"); - //this->printf("HTTP Body: [%s]", resultBuffer); - + //this->printf("HTTP Body: [%s]", resultBuffer); socket.close(); } else { this->printf("Socket Connect Failed: %d", connResult); } - } else { this->printf("Socket Open Failed: %d", openResult); socket.close(); return 1; } - return 0; } unsigned long GroveStreams::sendNoRetryJPA(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize) -{ - //Assemble the url that is used to pass the temperature readings to GroveStreams and call it - +{ //Assemble the url that is used to pass the temperature readings to GroveStreams and call it network::tcp::Socket socket; - int openResult = socket.open(); if (openResult == 0) { - int connResult = socket.connect(_domain, _port); if(connResult == 0) { - char compNamePart[100] = {0}; if (strlen(componentName) > 0) { sprintf(compNamePart, "&compName=%s",componentName); } - char compTmplIdPart[100] = {0}; if (strlen(compTmplId) > 0) { sprintf(compTmplIdPart, "&compTmplId=%s",compTmplId); } - //You may need to increase the size of sbuffer if any other char array sizes have increased char sbuffer[512]= {0}; - 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", componentId, compTmplIdPart, compNamePart, _apiKey, samples, _myIPAddress); - this->printf("send sbuffer size: %d", strlen(sbuffer)); - int scount = socket.write(sbuffer, strlen(sbuffer)); //this->printf("sent %d bytes: [%.*s]", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); - - //Read status line - + //Read status line int rcount = socket.read(resultBuffer, resultBufferSize); - this->printf("HTTP Response size: %d bytes", rcount); - this->printf("HTTP Response Status:[%.*s]", strstr(resultBuffer, "\r\n")-resultBuffer, resultBuffer); - + this->printf("HTTP Response Status:[%.*s]", strstr(resultBuffer, "\r\n")-resultBuffer, resultBuffer); //Only return the body strcpy(resultBuffer, strstr(resultBuffer, "\r\n\r\n") + strlen("\r\n\r\n")); strcat(resultBuffer, "\0"); - //this->printf("HTTP Body: [%s]", resultBuffer); - + //this->printf("HTTP Body: [%s]", resultBuffer); socket.close(); } else { - this->printf("Socket Connect Failed: %d", connResult); } - } else { this->printf("Socket Open Failed: %d", openResult); socket.close(); return 1; } - return 0; } unsigned long GroveStreams::sendNoRetryInitDatasJPA(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize) -{ - //Assemble the url that is used to pass the temperature readings to GroveStreams and call it - +{ //Assemble the url that is used to pass the temperature readings to GroveStreams and call it network::tcp::Socket socket; - int openResult = socket.open(); if (openResult == 0) { //printf("\nConnect to socket : %s\n",_domain); int connResult = socket.connect(_domain, _port); if(connResult == 0) { - char compNamePart[100] = {0}; if (strlen(componentName) > 0) { sprintf(compNamePart, "&compName=%s",componentName); } - char compTmplIdPart[100] = {0}; if (strlen(compTmplId) > 0) { sprintf(compTmplIdPart, "&compTmplId=%s",compTmplId); } - //You may need to increase the size of sbuffer if any other char array sizes have increased char sbuffer[512]= {0}; 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", componentId, compTmplIdPart, compNamePart, _apiKey, samples, _myIPAddress); - this->printf("send sbuffer size: %d", strlen(sbuffer)); - int scount = socket.write(sbuffer, strlen(sbuffer)); this->printf("sent %d bytes: [%.*s]", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); - - //Read status line - + //Read status line int rcount = socket.read(resultBuffer, resultBufferSize); - this->printf("HTTP Response size: %d bytes", rcount); - this->printf("HTTP Response Status:[%.*s]", strstr(resultBuffer, "\r\n")-resultBuffer, resultBuffer); - + this->printf("HTTP Response Status:[%.*s]", strstr(resultBuffer, "\r\n")-resultBuffer, resultBuffer); //Only return the body strcpy(resultBuffer, strstr(resultBuffer, "\r\n\r\n") + strlen("\r\n\r\n")); strcat(resultBuffer, "\0"); - this->printf("HTTP Body: [%s]", resultBuffer); - + this->printf("HTTP Body: [%s]", resultBuffer); socket.close(); } else { - this->printf("Socket Connect Failed: %d", connResult); } - } else { this->printf("Socket Open Failed: %d", openResult); socket.close(); return 1; } - return 0; } int GroveStreams::startEthernet() -{ - //Start or restart the Ethernet connection. +{ //Start or restart the Ethernet connection. int result = _eth.disconnect(); if (result != 0) { this->printf("Ethernet diconnect Failed: %d", result); return result; } - this->printf("Connecting STM32 to network..."); - //Connect to the network and obtain an IP address using DHCP result = _eth.init(); if (result != 0) { this->printf("Ethernet init Failed: %d", result); return result; } - result = _eth.connect(); if (result != 0) { this->printf("Ethernet connect Failed: %d", result); return result; } else { this->printf("STM32 connected to network"); - //Set the mac and ip variables so that they can be used during sensor uploads later _myIPAddress = _eth.getIPAddress(); - this->printf("IP address: %s", _myIPAddress ? _myIPAddress : "No IP"); } - return 0; } +const char* GroveStreams::getMACAddress() +{ return _eth.getMACAddress(); +} -const char* GroveStreams::getMACAddress() -{ - return _eth.getMACAddress(); -} -
diff -r b77ba7f2611b -r 494da6893902 GroveStreams.h --- a/GroveStreams.h Tue Sep 15 19:05:59 2020 +0000 +++ b/GroveStreams.h Wed Jan 13 19:51:28 2021 +0000 @@ -1,145 +1,76 @@ /* - GroveStreams is a helper class to assist with pushing data into the - GroveStreams.com IoT platform. - - License: - Copyright (C) 2017 GroveStreams LLC. - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. + GroveStreams is a helper class to assist with pushing data into the GroveStreams.com IoT platform. */ - #ifndef GROVESTREAMS_H_ #define GROVESTREAMS_H_ - #include "mbed.h" #include "EthernetInterface.h" #include "NetworkAPI/tcp/socket.hpp" #include "LcdDiscoF746NgTracer.h" - - class GroveStreams { - protected: EthernetInterface _eth; LcdDiscoF746NgTracer* _pLcd; - //const char* _domain; uint16_t _port; const char* _apiKey; const char* _myIPAddress; //Don't Change. Set below from DHCP. Needed by GroveStreams to verify that a device is not uploading more than once every 10s. - - public: - /** Constructor with apiKey and Lcd tracer - * - * \param apiKey the secret api_key for your GroveStreams organization - * \param pLcd optional. Pass NULL or and LCD for tracing + * \param apiKey the secret api_key for your GroveStreams organization \param pLcd optional. Pass NULL or and LCD for tracing */ GroveStreams(const char* apiKey, LcdDiscoF746NgTracer* pLcd); - - /** Constructor with apiKey and Lcd tracer. - * - * \param apiKey the secret api_key for your GroveStreams organization - */ - GroveStreams(const char* apiKey); - - /** Destructor - * - */ + /** Constructor with apiKey and Lcd tracer. * \param apiKey the secret api_key for your GroveStreams organization */ + GroveStreams(const char* apiKey); + /** Destructor * */ ~GroveStreams(); - protected: int init(const char* apiKey, LcdDiscoF746NgTracer* pLcd); void printf(const char* format, ...); - public: - - /** Send sample data to GroveStreams. Attempts to restore dropped ethernet - * connections - * - * \param componentId The ID of the component to put the samples into - * \param samples a string that is URL Encoded and contains all of the + /** Send sample data to GroveStreams. Attempts to restore dropped ethernet connections + * \param componentId The ID of the component to put the samples into \param samples a string that is URL Encoded and contains all of the * samples. Place an ampersand at the beggining of each sample substring. * The substring is &streamId=value. - * Example: &temp=6&eres=78.231&on=true&position=on+ground - * \return 0 on success - */ + * Example: &temp=6&eres=78.231&on=true&position=on+ground \return 0 on success */ unsigned long send(const char* componentId, const char* samples); unsigned long sendJPA(const char* componentId, const char* samples); - unsigned long sendInitDatasJPA(const char* componentId, const char* samples); - - /** Send sample data to GroveStreams. Attempts to restore dropped ethernet - * connections - * + unsigned long sendInitDatasJPA(const char* componentId, const char* samples); + /** Send sample data to GroveStreams. Attempts to restore dropped ethernet connections * * \param componentId The ID of the component to put the samples into - * \param samples a string that is URL Encoded and contains all of the + * \param samples a string that is URL Encoded and contains all of the samples. Place an ampersand at the beggining of each sample substring. + * The substring is &streamId=value. + * Example: &temp=6&eres=78.231&on=true&position=on+ground * \param resultBuffer a string that will contain the http result body + * \param resultBufferSize the size of the result buffer * \return 0 on success */ + unsigned long send(const char* componentId, const char* samples, char* resultBuffer, size_t resultBufferSize); + /** Send sample data to GroveStreams. Attempts to restore dropped ethernet connections * + * \param componentId The ID of the component to put the samples into * \param samples a string that is URL Encoded and contains all of the * samples. Place an ampersand at the beggining of each sample substring. * The substring is &streamId=value. - * Example: &temp=6&eres=78.231&on=true&position=on+ground - * \param resultBuffer a string that will contain the http result body - * \param resultBufferSize the size of the result buffer - * \return 0 on success - */ - unsigned long send(const char* componentId, const char* samples, char* resultBuffer, size_t resultBufferSize); - - /** Send sample data to GroveStreams. Attempts to restore dropped ethernet - * connections - * + * Example: &temp=6&eres=78.231&on=true&position=on+ground * \param componentName optional (NULL). the name assigned to a newly created component. + * \param compTmplId optional (NULL). the component template ID to be used for creating a new component + * \param resultBuffer a string that will contain the http result body* \param resultBufferSize the size of the result buffer + * \return 0 on success */ + unsigned long send(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize); + unsigned long sendJPA(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize); + unsigned long sendInitDatasJPA(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize); + /** Send sample data to GroveStreams. Does not attempt to restore dropped ethernet connections. * * \param componentId The ID of the component to put the samples into * \param samples a string that is URL Encoded and contains all of the * samples. Place an ampersand at the beggining of each sample substring. * The substring is &streamId=value. * Example: &temp=6&eres=78.231&on=true&position=on+ground * \param componentName optional (NULL). the name assigned to a newly created component. - * \param compTmplId optional (NULL). the component template ID to be used for creating a - * new component - * \param resultBuffer a string that will contain the http result body - * \param resultBufferSize the size of the result buffer - * \return 0 on success - */ - unsigned long send(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize); - unsigned long sendJPA(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize); - unsigned long sendInitDatasJPA(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize); - - /** Send sample data to GroveStreams. Does not attempt to restore dropped - * ethernet connections. - * - * \param componentId The ID of the component to put the samples into - * \param samples a string that is URL Encoded and contains all of the - * samples. Place an ampersand at the beggining of each sample substring. - * The substring is &streamId=value. - * Example: &temp=6&eres=78.231&on=true&position=on+ground - * \param componentName optional (NULL). the name assigned to a newly created component. - * \param compTmplId optional (NULL). the component template ID to be used for creating a - * new component - * \param resultBuffer a string that will contain the http result body - * \param resultBufferSize the size of the result buffer - * \return 0 on success - */ + * \param compTmplId optional (NULL). the component template ID to be used for creating a * new component + * \param resultBuffer a string that will contain the http result body * \param resultBufferSize the size of the result buffer + * \return 0 on success */ unsigned long sendNoRetry(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize); unsigned long sendNoRetryJPA(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize); unsigned long sendNoRetryInitDatasJPA(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize); - - /** Starts ethernet. - * Stops ethernet if it is running before starting it. - * \return 0 on success - */ - int startEthernet(); - - /** Get the MAC address of your Ethernet interface - * \return a pointer to a string containing the MAC address - */ + /** Starts ethernet. * Stops ethernet if it is running before starting it. * \return 0 on success */ + int startEthernet(); + /** Get the MAC address of your Ethernet interface -return a pointer to a string containing the MAC address */ const char* getMACAddress(); - }; - #endif /* GROVESTREAMS_H_ */