mbed based IoT Gateway More details http://blog.thiseldo.co.uk/wp-filez/IoTGateway.pdf

Dependencies:   NetServices FatFileSystem csv_parser mbed MQTTClient RF12B DNSResolver SDFileSystem

Committer:
SomeRandomBloke
Date:
Wed May 09 20:29:30 2012 +0000
Revision:
5:0dbc27a7af55
Parent:
4:d460406ac780
Reduced debug output

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 4:d460406ac780 42 /** Alternative Constructor
SomeRandomBloke 4:d460406ac780 43 */
SomeRandomBloke 4:d460406ac780 44 OutputPachube::OutputPachube( char *internalBufferStart, char *url, char *key ) {
SomeRandomBloke 4:d460406ac780 45 sendCount = 0;
SomeRandomBloke 4:d460406ac780 46 dataBuffer = internalBufferStart;
SomeRandomBloke 4:d460406ac780 47 apiUrl = url;
SomeRandomBloke 0:a29a0225f203 48 apiKey = key;
SomeRandomBloke 4:d460406ac780 49 dbufPtr = dataBuffer;
SomeRandomBloke 4:d460406ac780 50 *dbufPtr = '\0';
SomeRandomBloke 0:a29a0225f203 51 }
SomeRandomBloke 4:d460406ac780 52
SomeRandomBloke 0:a29a0225f203 53
SomeRandomBloke 0:a29a0225f203 54 void OutputPachube::init( ) {
SomeRandomBloke 0:a29a0225f203 55 dbufPtr = dataBuffer;
SomeRandomBloke 0:a29a0225f203 56 *dbufPtr = '\0';
SomeRandomBloke 0:a29a0225f203 57 }
SomeRandomBloke 0:a29a0225f203 58
SomeRandomBloke 0:a29a0225f203 59 void OutputPachube::addReading(char *dataFeed, char *dataStream, char *reading ) {
SomeRandomBloke 0:a29a0225f203 60 char tmpBuf[20];
SomeRandomBloke 0:a29a0225f203 61 int thisFeed = atoi( dataFeed );
SomeRandomBloke 0:a29a0225f203 62
SomeRandomBloke 0:a29a0225f203 63 if ( thisFeed != currentFeed )
SomeRandomBloke 0:a29a0225f203 64 send();
SomeRandomBloke 0:a29a0225f203 65
SomeRandomBloke 0:a29a0225f203 66 currentFeed = thisFeed;
SomeRandomBloke 0:a29a0225f203 67 snprintf(tmpBuf, DATABUF_SIZE, "%s,%s\n", dataStream, reading);
SomeRandomBloke 0:a29a0225f203 68 strcat( dataBuffer, tmpBuf );
SomeRandomBloke 0:a29a0225f203 69 }
SomeRandomBloke 0:a29a0225f203 70
SomeRandomBloke 0:a29a0225f203 71
SomeRandomBloke 0:a29a0225f203 72 int OutputPachube::send( void ) {
SomeRandomBloke 5:0dbc27a7af55 73 char urlBuf[API_URL_LENGTH];
SomeRandomBloke 4:d460406ac780 74 HTTPClient http;
SomeRandomBloke 0:a29a0225f203 75
SomeRandomBloke 0:a29a0225f203 76 if ( strlen( dataBuffer ) > 1 ) {
SomeRandomBloke 4:d460406ac780 77 http.setRequestHeader("X-PachubeApiKey", apiKey);
SomeRandomBloke 0:a29a0225f203 78
SomeRandomBloke 0:a29a0225f203 79 HTTPText csvContent("text/csv");
SomeRandomBloke 0:a29a0225f203 80 // Get the string for Pachube
SomeRandomBloke 0:a29a0225f203 81 csvContent.set(std::string(dataBuffer));
SomeRandomBloke 0:a29a0225f203 82
SomeRandomBloke 0:a29a0225f203 83 // uri for post includes feed ID and datastream ID
SomeRandomBloke 5:0dbc27a7af55 84 snprintf(urlBuf, API_URL_LENGTH, apiUrl, currentFeed );
SomeRandomBloke 5:0dbc27a7af55 85 // printf("URL: %s\n",urlBuf);
SomeRandomBloke 0:a29a0225f203 86
SomeRandomBloke 0:a29a0225f203 87
SomeRandomBloke 0:a29a0225f203 88 activityLED = 1;
SomeRandomBloke 0:a29a0225f203 89 // result should be 0 and response should be 200 for successful post
SomeRandomBloke 5:0dbc27a7af55 90 sendCount++;
SomeRandomBloke 5:0dbc27a7af55 91 // printf("Pachube Send count %d\n", sendCount);
SomeRandomBloke 0:a29a0225f203 92
SomeRandomBloke 5:0dbc27a7af55 93 /* printf("\nHEAP STATS\n");
SomeRandomBloke 0:a29a0225f203 94 __heapstats((__heapprt)fprintf,stderr);
SomeRandomBloke 0:a29a0225f203 95 printf("\nHEAP CHECK\n");
SomeRandomBloke 0:a29a0225f203 96 __heapvalid((__heapprt)fprintf,stderr, 0);
SomeRandomBloke 0:a29a0225f203 97 printf("\nStackP: %ld\n",__current_sp());
SomeRandomBloke 0:a29a0225f203 98 printf("---------------\n");
SomeRandomBloke 5:0dbc27a7af55 99 */
SomeRandomBloke 4:d460406ac780 100 HTTPResult result = http.post(urlBuf, csvContent, NULL);
SomeRandomBloke 4:d460406ac780 101 int response = http.getHTTPResponseCode();
SomeRandomBloke 5:0dbc27a7af55 102 // printf("updateDataStream(%d)\n", response );
SomeRandomBloke 0:a29a0225f203 103 activityLED = 0;
SomeRandomBloke 0:a29a0225f203 104 }
SomeRandomBloke 0:a29a0225f203 105
SomeRandomBloke 4:d460406ac780 106 dbufPtr = dataBuffer;
SomeRandomBloke 4:d460406ac780 107 *dbufPtr = '\0';
SomeRandomBloke 4:d460406ac780 108
SomeRandomBloke 0:a29a0225f203 109 return 0;
SomeRandomBloke 0:a29a0225f203 110 }
SomeRandomBloke 0:a29a0225f203 111
SomeRandomBloke 0:a29a0225f203 112
SomeRandomBloke 5:0dbc27a7af55 113 int OutputPachube::getSendCount( void ) {
SomeRandomBloke 5:0dbc27a7af55 114 return sendCount;
SomeRandomBloke 5:0dbc27a7af55 115 }