Andrew Lindsay / Mbed 2 deprecated IoTGateway_Basic

Dependencies:   NetServices FatFileSystem csv_parser mbed MQTTClient RF12B DNSResolver SDFileSystem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers OutputEmonCms.cpp Source File

OutputEmonCms.cpp

00001 /** IoT Gateway Output definition for Open Energy Monitor emonCms 
00002  *
00003  * @author Andrew Lindsay
00004  *
00005  * @section LICENSE
00006  *
00007  * Copyright (c) 2012 Andrew Lindsay (andrew [at] thiseldo [dot] co [dot] uk)
00008  *
00009  * Permission is hereby granted, free of charge, to any person obtaining a copy
00010  * of this software and associated documentation files (the "Software"), to deal
00011  * in the Software without restriction, including without limitation the rights
00012  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013  * copies of the Software, and to permit persons to whom the Software is
00014  * furnished to do so, subject to the following conditions:
00015 
00016  * The above copyright notice and this permission notice shall be included in
00017  * all copies or substantial portions of the Software.
00018  * 
00019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025  * THE SOFTWARE.
00026  * 
00027  * @section DESCRIPTION
00028  * 
00029  * API URL: http://host/emoncms/api/post?apikey=key&json={power:252.4}
00030  ,temperature:15.4}
00031  * 
00032  */
00033 
00034 #include "mbed.h"
00035 #include "OutputEmonCms.h"
00036 
00037 DigitalOut emonActivityLED(p29, "activityLED");
00038 
00039 // Constructor
00040 OutputEmonCms::OutputEmonCms() {
00041     sendCount = 0;
00042 }
00043 
00044 /** Alternative Constructor
00045   */
00046 OutputEmonCms::OutputEmonCms( char *internalBufferStart, char *url, char *key ) {
00047     sendCount = 0;
00048     dataBuffer = internalBufferStart;
00049     apiUrl = url;
00050     apiKey = key;
00051     // Setup start of URL buffer
00052     init();
00053 }
00054 
00055 
00056 void OutputEmonCms::init( ) {
00057     dbufPtr = dataBuffer;
00058     sprintf(dataBuffer, apiUrl, apiKey );
00059     dbufPtr = dataBuffer + strlen(dataBuffer);
00060     *dbufPtr = '\0';
00061     hasData = false;
00062 }
00063 
00064 // Datafeed us the name of the value
00065 void OutputEmonCms::addReading(char *dataFeed, char *dataStream, char *reading ) {
00066 
00067     snprintf(dbufPtr, DATABUF_SIZE, "{%s:%s}\0", dataFeed, reading);
00068     hasData = true;
00069     send();
00070 }
00071 
00072 
00073 int OutputEmonCms::send( void ) {
00074 //    char urlBuf[64];
00075     HTTPClient http;
00076 
00077     if ( hasData && strlen( dataBuffer ) > 1 ) {
00078 
00079         HTTPText csvContent("application/json");
00080         csvContent.set(std::string(dataBuffer));
00081 
00082         // uri for post includes feed ID and datastream ID
00083 //        printf("URL: %s\n",dataBuffer);
00084 
00085 
00086         emonActivityLED = 1;
00087         sendCount++;
00088         // result should be 0 and response should be 200 for successful post
00089 //        printf("EmonCms send count %d\n", sendCount);
00090 /*
00091         printf("\nHEAP STATS\n");
00092         __heapstats((__heapprt)fprintf,stderr);
00093         printf("\nHEAP CHECK\n");
00094         __heapvalid((__heapprt)fprintf,stderr, 0);
00095         printf("\nStackP: %ld\n",__current_sp());
00096         printf("---------------\n");
00097 */
00098         HTTPResult result = http.get(dataBuffer, NULL);
00099         int response = http.getHTTPResponseCode();
00100         printf("updateDataStream(%d)\n", response );
00101         emonActivityLED = 0;
00102     }
00103 
00104     init();
00105     return 0;
00106 }
00107 
00108 int OutputEmonCms::getSendCount( void ) {
00109     return sendCount;
00110 }
00111