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:
Tue May 01 21:43:40 2012 +0000
Revision:
4:d460406ac780
Child:
5:0dbc27a7af55
Added output module for Sen.Se and emonCMS

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 4:d460406ac780 83 printf("URL: %s\n",dataBuffer);
SomeRandomBloke 4:d460406ac780 84
SomeRandomBloke 4:d460406ac780 85
SomeRandomBloke 4:d460406ac780 86 emonActivityLED = 1;
SomeRandomBloke 4:d460406ac780 87 // result should be 0 and response should be 200 for successful post
SomeRandomBloke 4:d460406ac780 88 printf("EmonCms send count %d\n", ++sendCount);
SomeRandomBloke 4:d460406ac780 89 /*
SomeRandomBloke 4:d460406ac780 90 printf("\nHEAP STATS\n");
SomeRandomBloke 4:d460406ac780 91 __heapstats((__heapprt)fprintf,stderr);
SomeRandomBloke 4:d460406ac780 92 printf("\nHEAP CHECK\n");
SomeRandomBloke 4:d460406ac780 93 __heapvalid((__heapprt)fprintf,stderr, 0);
SomeRandomBloke 4:d460406ac780 94 printf("\nStackP: %ld\n",__current_sp());
SomeRandomBloke 4:d460406ac780 95 printf("---------------\n");
SomeRandomBloke 4:d460406ac780 96 */
SomeRandomBloke 4:d460406ac780 97 HTTPResult result = http.get(dataBuffer, NULL);
SomeRandomBloke 4:d460406ac780 98 int response = http.getHTTPResponseCode();
SomeRandomBloke 4:d460406ac780 99 printf("updateDataStream(%d)\n", response );
SomeRandomBloke 4:d460406ac780 100 emonActivityLED = 0;
SomeRandomBloke 4:d460406ac780 101 }
SomeRandomBloke 4:d460406ac780 102
SomeRandomBloke 4:d460406ac780 103 init();
SomeRandomBloke 4:d460406ac780 104 return 0;
SomeRandomBloke 4:d460406ac780 105 }
SomeRandomBloke 4:d460406ac780 106