Andrew Lindsay / Mbed 2 deprecated IoTGateway_Basic

Dependencies:   NetServices FatFileSystem csv_parser mbed MQTTClient RF12B DNSResolver SDFileSystem

Committer:
SomeRandomBloke
Date:
Fri Apr 20 19:32:34 2012 +0000
Revision:
3:f19f9c62c00b
Parent:
0:a29a0225f203
Child:
4:d460406ac780

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 0:a29a0225f203 1 /** IoT Gateway Output definition for Pachube
SomeRandomBloke 0:a29a0225f203 2 *
SomeRandomBloke 0:a29a0225f203 3 * @author Andrew Lindsay
SomeRandomBloke 0:a29a0225f203 4 *
SomeRandomBloke 0:a29a0225f203 5 * @section LICENSE
SomeRandomBloke 0:a29a0225f203 6 *
SomeRandomBloke 0:a29a0225f203 7 * Copyright (c) 2012 Andrew Lindsay (andrew [at] thiseldo [dot] co [dot] uk)
SomeRandomBloke 0:a29a0225f203 8 *
SomeRandomBloke 0:a29a0225f203 9 * Permission is hereby granted, free of charge, to any person obtaining a copy
SomeRandomBloke 0:a29a0225f203 10 * of this software and associated documentation files (the "Software"), to deal
SomeRandomBloke 0:a29a0225f203 11 * in the Software without restriction, including without limitation the rights
SomeRandomBloke 0:a29a0225f203 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
SomeRandomBloke 0:a29a0225f203 13 * copies of the Software, and to permit persons to whom the Software is
SomeRandomBloke 0:a29a0225f203 14 * furnished to do so, subject to the following conditions:
SomeRandomBloke 0:a29a0225f203 15
SomeRandomBloke 0:a29a0225f203 16 * The above copyright notice and this permission notice shall be included in
SomeRandomBloke 0:a29a0225f203 17 * all copies or substantial portions of the Software.
SomeRandomBloke 0:a29a0225f203 18 *
SomeRandomBloke 0:a29a0225f203 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
SomeRandomBloke 0:a29a0225f203 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
SomeRandomBloke 0:a29a0225f203 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
SomeRandomBloke 0:a29a0225f203 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
SomeRandomBloke 0:a29a0225f203 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
SomeRandomBloke 0:a29a0225f203 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
SomeRandomBloke 0:a29a0225f203 25 * THE SOFTWARE.
SomeRandomBloke 0:a29a0225f203 26 *
SomeRandomBloke 0:a29a0225f203 27 * @section DESCRIPTION
SomeRandomBloke 0:a29a0225f203 28 *
SomeRandomBloke 0:a29a0225f203 29 *
SomeRandomBloke 0:a29a0225f203 30 */
SomeRandomBloke 0:a29a0225f203 31
SomeRandomBloke 0:a29a0225f203 32 #include "mbed.h"
SomeRandomBloke 0:a29a0225f203 33 #include "OutputPachube.h"
SomeRandomBloke 0:a29a0225f203 34
SomeRandomBloke 0:a29a0225f203 35 DigitalOut activityLED(p29, "activityLED");
SomeRandomBloke 0:a29a0225f203 36
SomeRandomBloke 0:a29a0225f203 37 // Constructor
SomeRandomBloke 0:a29a0225f203 38 OutputPachube::OutputPachube() {
SomeRandomBloke 0:a29a0225f203 39 sendCount = 0;
SomeRandomBloke 0:a29a0225f203 40 }
SomeRandomBloke 0:a29a0225f203 41
SomeRandomBloke 0:a29a0225f203 42 void OutputPachube::setApiKey( char *key ) {
SomeRandomBloke 0:a29a0225f203 43 apiKey = key;
SomeRandomBloke 0:a29a0225f203 44 init();
SomeRandomBloke 0:a29a0225f203 45 }
SomeRandomBloke 0:a29a0225f203 46
SomeRandomBloke 0:a29a0225f203 47 void OutputPachube::init( ) {
SomeRandomBloke 0:a29a0225f203 48 dbufPtr = dataBuffer;
SomeRandomBloke 0:a29a0225f203 49 *dbufPtr = '\0';
SomeRandomBloke 0:a29a0225f203 50 }
SomeRandomBloke 0:a29a0225f203 51
SomeRandomBloke 0:a29a0225f203 52 void OutputPachube::addReading(char *dataFeed, char *dataStream, char *reading ) {
SomeRandomBloke 0:a29a0225f203 53 char tmpBuf[20];
SomeRandomBloke 0:a29a0225f203 54 int thisFeed = atoi( dataFeed );
SomeRandomBloke 0:a29a0225f203 55
SomeRandomBloke 0:a29a0225f203 56 if ( thisFeed != currentFeed )
SomeRandomBloke 0:a29a0225f203 57 send();
SomeRandomBloke 0:a29a0225f203 58
SomeRandomBloke 0:a29a0225f203 59 currentFeed = thisFeed;
SomeRandomBloke 0:a29a0225f203 60 snprintf(tmpBuf, DATABUF_SIZE, "%s,%s\n", dataStream, reading);
SomeRandomBloke 0:a29a0225f203 61 strcat( dataBuffer, tmpBuf );
SomeRandomBloke 0:a29a0225f203 62 }
SomeRandomBloke 0:a29a0225f203 63
SomeRandomBloke 0:a29a0225f203 64
SomeRandomBloke 0:a29a0225f203 65 int OutputPachube::send( void ) {
SomeRandomBloke 0:a29a0225f203 66 char urlBuf[64];
SomeRandomBloke 0:a29a0225f203 67 HTTPClient pachubeHttp;
SomeRandomBloke 0:a29a0225f203 68
SomeRandomBloke 0:a29a0225f203 69 if ( strlen( dataBuffer ) > 1 ) {
SomeRandomBloke 0:a29a0225f203 70 pachubeHttp.setRequestHeader("X-PachubeApiKey", apiKey);
SomeRandomBloke 0:a29a0225f203 71
SomeRandomBloke 0:a29a0225f203 72 HTTPText csvContent("text/csv");
SomeRandomBloke 0:a29a0225f203 73 // Get the string for Pachube
SomeRandomBloke 0:a29a0225f203 74 csvContent.set(std::string(dataBuffer));
SomeRandomBloke 0:a29a0225f203 75
SomeRandomBloke 0:a29a0225f203 76 // uri for post includes feed ID and datastream ID
SomeRandomBloke 0:a29a0225f203 77 snprintf(urlBuf, 64, "http://api.pachube.com/v2/feeds/%d.csv?_method=PUT", currentFeed );
SomeRandomBloke 0:a29a0225f203 78 printf("URL: %s\n",urlBuf);
SomeRandomBloke 0:a29a0225f203 79
SomeRandomBloke 0:a29a0225f203 80
SomeRandomBloke 0:a29a0225f203 81 activityLED = 1;
SomeRandomBloke 0:a29a0225f203 82 // result should be 0 and response should be 200 for successful post
SomeRandomBloke 0:a29a0225f203 83 printf("Send count %d\n", ++sendCount);
SomeRandomBloke 0:a29a0225f203 84
SomeRandomBloke 0:a29a0225f203 85 printf("\nHEAP STATS\n");
SomeRandomBloke 0:a29a0225f203 86 __heapstats((__heapprt)fprintf,stderr);
SomeRandomBloke 0:a29a0225f203 87 printf("\nHEAP CHECK\n");
SomeRandomBloke 0:a29a0225f203 88 __heapvalid((__heapprt)fprintf,stderr, 0);
SomeRandomBloke 0:a29a0225f203 89 printf("\nStackP: %ld\n",__current_sp());
SomeRandomBloke 0:a29a0225f203 90 printf("---------------\n");
SomeRandomBloke 0:a29a0225f203 91
SomeRandomBloke 0:a29a0225f203 92 HTTPResult result = pachubeHttp.post(urlBuf, csvContent, NULL);
SomeRandomBloke 0:a29a0225f203 93 int response = pachubeHttp.getHTTPResponseCode();
SomeRandomBloke 0:a29a0225f203 94 printf("updateDataStream(%d)\n", response );
SomeRandomBloke 0:a29a0225f203 95 activityLED = 0;
SomeRandomBloke 0:a29a0225f203 96 }
SomeRandomBloke 0:a29a0225f203 97
SomeRandomBloke 0:a29a0225f203 98 init();
SomeRandomBloke 0:a29a0225f203 99 return 0;
SomeRandomBloke 0:a29a0225f203 100 }
SomeRandomBloke 0:a29a0225f203 101
SomeRandomBloke 0:a29a0225f203 102