This is a test program for my Pachube API driver classes.

Dependencies:   mbed ThermistorPack Pachube EthernetNetIf

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * A test program for Pachube API interface driver. (Version 0.0.1)
00003  *
00004  * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
00005  * http://shinta.main.jp/
00006  */
00007 
00008 #include "mbed.h"
00009 #include "PachubeV2CSV.h"
00010 #include "EthernetNetIf.h"
00011 #include "HTTPClient.h"
00012 #include "ThermistorMCP9701.h"
00013 
00014 #define API_KEY "YourAPIKey"
00015 #define FEED_ID 99999
00016 #define STREAM_ID1 "TempNo.1"
00017 #define STREAM_ID2 "TempNo.2"
00018 #define STREAM_ID3 "TempNo.3"
00019 #define STREAM_ID4 "TempNo.4"
00020 #define STREAM_ID5 "TempNo.5"
00021 
00022 PachubeV2CSV web(API_KEY);
00023 EthernetNetIf eth;
00024 ThermistorMCP9701 thermistor1(p15);
00025 ThermistorMCP9701 thermistor2(p16);
00026 ThermistorMCP9701 thermistor3(p17);
00027 ThermistorMCP9701 thermistor4(p19);
00028 ThermistorMCP9701 thermistor5(p20);
00029 
00030 /**
00031  * Convert double to char.
00032  *
00033  * @param val Value.
00034  * @param buf A pointer to a buffer.
00035  * @param bufsiz The buffer size.
00036  */
00037 void convertDoubleToChar(double val, char *buf, size_t bufsiz) {
00038     snprintf(buf, bufsiz, "%f", val);
00039 }
00040 
00041 /**
00042  * Probe temperatures.
00043  *
00044  * @param interval_min probing interval time.
00045  */
00046 void probe_temperatures(const int interval_min) {
00047     int cnt = 0;
00048     while (1) {
00049         char val1[16];
00050         char val2[16];
00051         char val3[16];
00052         char val4[16];
00053         char val5[16];
00054         convertDoubleToChar(thermistor1.read(), val1, sizeof(val1));
00055         convertDoubleToChar(thermistor2.read(), val2, sizeof(val2));
00056         convertDoubleToChar(thermistor3.read(), val3, sizeof(val3));
00057         convertDoubleToChar(thermistor4.read(), val4, sizeof(val4));
00058         convertDoubleToChar(thermistor5.read(), val5, sizeof(val5));
00059         if (cnt == 0) {
00060             printf("createNewDataStream(%d)\n", web.createNewDataStream(FEED_ID, STREAM_ID1, std::string(val1)));
00061             printf("createNewDataStream(%d)\n", web.createNewDataStream(FEED_ID, STREAM_ID2, std::string(val2)));
00062             printf("createNewDataStream(%d)\n", web.createNewDataStream(FEED_ID, STREAM_ID3, std::string(val3)));
00063             printf("createNewDataStream(%d)\n", web.createNewDataStream(FEED_ID, STREAM_ID4, std::string(val4)));
00064             printf("createNewDataStream(%d)\n", web.createNewDataStream(FEED_ID, STREAM_ID5, std::string(val5)));
00065         } else {
00066             printf("updateDataStream(%d)\n", web.updateDataStream(FEED_ID, STREAM_ID1, std::string(val1)));
00067             printf("updateDataStream(%d)\n", web.updateDataStream(FEED_ID, STREAM_ID2, std::string(val2)));
00068             printf("updateDataStream(%d)\n", web.updateDataStream(FEED_ID, STREAM_ID3, std::string(val3)));
00069             printf("updateDataStream(%d)\n", web.updateDataStream(FEED_ID, STREAM_ID4, std::string(val4)));
00070             printf("updateDataStream(%d)\n", web.updateDataStream(FEED_ID, STREAM_ID5, std::string(val5)));
00071         }
00072         cnt++;
00073 
00074         /*
00075          * This is not exactlly same as interval_min[min.].
00076          * But I think this codes are good starting point for you.
00077          */
00078         for (int i = 0; i < interval_min; i++) {
00079             /*
00080              * Wait 60 seconds.
00081              */
00082             wait(60);
00083             printf("%d[min.]\n", i + 1);
00084         }
00085     }
00086 }
00087 
00088 /**
00089  * Entry point.
00090  */
00091 int main() {
00092     eth.setup();
00093 
00094     const int IntervalMinutes = 5;
00095     probe_temperatures(IntervalMinutes);
00096 }