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