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
Outputs/OutputPachube.cpp@4:d460406ac780, 2012-05-01 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Tue May 01 21:43:40 2012 +0000
- Revision:
- 4:d460406ac780
- Parent:
- 0:a29a0225f203
- Child:
- 5:0dbc27a7af55
Added output module for Sen.Se and emonCMS
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:a29a0225f203 | 73 | char urlBuf[64]; |
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 | 4:d460406ac780 | 84 | snprintf(urlBuf, 64, PACHUBE_URL, currentFeed ); |
SomeRandomBloke | 0:a29a0225f203 | 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 | 4:d460406ac780 | 90 | printf("Pachube Send count %d\n", ++sendCount); |
SomeRandomBloke | 0:a29a0225f203 | 91 | |
SomeRandomBloke | 0:a29a0225f203 | 92 | printf("\nHEAP STATS\n"); |
SomeRandomBloke | 0:a29a0225f203 | 93 | __heapstats((__heapprt)fprintf,stderr); |
SomeRandomBloke | 0:a29a0225f203 | 94 | printf("\nHEAP CHECK\n"); |
SomeRandomBloke | 0:a29a0225f203 | 95 | __heapvalid((__heapprt)fprintf,stderr, 0); |
SomeRandomBloke | 0:a29a0225f203 | 96 | printf("\nStackP: %ld\n",__current_sp()); |
SomeRandomBloke | 0:a29a0225f203 | 97 | printf("---------------\n"); |
SomeRandomBloke | 0:a29a0225f203 | 98 | |
SomeRandomBloke | 4:d460406ac780 | 99 | HTTPResult result = http.post(urlBuf, csvContent, NULL); |
SomeRandomBloke | 4:d460406ac780 | 100 | int response = http.getHTTPResponseCode(); |
SomeRandomBloke | 0:a29a0225f203 | 101 | printf("updateDataStream(%d)\n", response ); |
SomeRandomBloke | 0:a29a0225f203 | 102 | activityLED = 0; |
SomeRandomBloke | 0:a29a0225f203 | 103 | } |
SomeRandomBloke | 0:a29a0225f203 | 104 | |
SomeRandomBloke | 4:d460406ac780 | 105 | dbufPtr = dataBuffer; |
SomeRandomBloke | 4:d460406ac780 | 106 | *dbufPtr = '\0'; |
SomeRandomBloke | 4:d460406ac780 | 107 | |
SomeRandomBloke | 0:a29a0225f203 | 108 | return 0; |
SomeRandomBloke | 0:a29a0225f203 | 109 | } |
SomeRandomBloke | 0:a29a0225f203 | 110 | |
SomeRandomBloke | 0:a29a0225f203 | 111 |