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