World Wide Data Logger using mbed.
Dependencies: mbed ThermistorPack Pachube ConfigFile EthernetNetIf TextLCD SDFileSystem
Revision 0:798c62ea70a3, committed 2011-04-04
- Comitter:
- shintamainjp
- Date:
- Mon Apr 04 21:33:25 2011 +0000
- Commit message:
- Initial version.
Changed in this revision
diff -r 000000000000 -r 798c62ea70a3 extlib/EthernetNetIf.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extlib/EthernetNetIf.lib Mon Apr 04 21:33:25 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/EthernetNetIf/#bc7df6da7589
diff -r 000000000000 -r 798c62ea70a3 extlib/FATFileSystem.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extlib/FATFileSystem.lib Mon Apr 04 21:33:25 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_unsupported/code/fatfilesystem/ \ No newline at end of file
diff -r 000000000000 -r 798c62ea70a3 extlib/HTTPClient.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extlib/HTTPClient.lib Mon Apr 04 21:33:25 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/HTTPClient/#d0be6af2d1db
diff -r 000000000000 -r 798c62ea70a3 extlib/SDFileSystem.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extlib/SDFileSystem.lib Mon Apr 04 21:33:25 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/SDFileSystem/#b1ddfc9a9b25
diff -r 000000000000 -r 798c62ea70a3 extlib/TextLCD.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extlib/TextLCD.lib Mon Apr 04 21:33:25 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/TextLCD/#44f34c09bd37
diff -r 000000000000 -r 798c62ea70a3 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Apr 04 21:33:25 2011 +0000 @@ -0,0 +1,315 @@ +/** + * ============================================================================= + * TORAGI 2011/04 : WEB Data Logger + * ============================================================================= + * Copyright (c) 2010-2011 Shinichiro Nakamura + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ============================================================================= + */ + +#include "mbed.h" +#include "PachubeV2CSV.h" +#include "EthernetNetIf.h" +#include "HTTPClient.h" +#include "ThermistorMCP9701.h" +#include "TextLCD.h" +#include "appconf.h" +#include "SDFileSystem.h" + +/* + * Definitions for a configuration file. + */ +#define CONFIG_FILENAME "/local/PACHUBE.CFG" +#define DATACSV_FILENAME "/sd/DATA.CSV" +const int PACHUBE_CODE_OK = 200; + +LocalFileSystem localfs("local"); +SDFileSystem sdfs(p5, p6, p7, p8, "sd"); +TextLCD lcd(p24, p26, p27, p28, p29, p30); +EthernetNetIf netif; +BusOut led(LED1, LED2, LED3, LED4); +ThermistorMCP9701 thermistor1(p16); +ThermistorMCP9701 thermistor2(p17); +ThermistorMCP9701 thermistor3(p18); +ThermistorMCP9701 thermistor4(p19); +ThermistorMCP9701 thermistor5(p20); +static appconf_t appconf; + +/** + * Display a splash screen. + */ +void splash(void) { + lcd.cls(); + lcd.locate(0, 0); + lcd.printf(" TORAGI 2011/04 "); + lcd.locate(0, 1); + lcd.printf("WEB Data Logger "); + wait(2); + + lcd.cls(); + lcd.locate(0, 0); + lcd.printf(" Starting up... "); + lcd.locate(0, 1); + lcd.printf(" Wait a moment. "); + wait(2); +} + +/** + * Convert double to char. + * + * @param val Value. + * @param buf A pointer to a buffer. + * @param bufsiz The buffer size. + */ +void convertDoubleToChar(double val, char *buf, size_t bufsiz) { + snprintf(buf, bufsiz, "%f", val); +} + +/** + * Post to the feed on Pachube. + * + * @param web Pointer to a Pachube object. + * @param feed_id Feed ID. + * @param stream_no Stream number. + * @param value value. + * + * @return Pachube code. + */ +int web_post(PachubeV2CSV *web, int feed_id, int stream_no, double value) { + char value_text[16]; + convertDoubleToChar(value, value_text, sizeof(value_text)); + char stream_no_text[8]; + stream_no_text[0] = "0123456789"[stream_no]; + stream_no_text[1] = '\0'; + return web->updateDataStream(feed_id, stream_no_text, std::string(value_text)); +} + +/** + * Write to the file. + * + * @param v1 value no. 1. + * @param v2 value no. 2. + * @param v3 value no. 3. + * @param v4 value no. 4. + * @param v5 value no. 5. + * + * @return Return 0 if it succeed. + */ +int file_write(int v1, int v2, int v3, int v4, int v5) { + FILE *fp = fopen(DATACSV_FILENAME, "a"); + if (fp == NULL) { + return -1; + } + fprintf(fp, "%d, %d, %d, %d, %d\n", v1, v2, v3, v4, v5); + fclose(fp); + return 0; +} + +/** + * Entry point. + */ +int main() { + + /* + * Splash. + */ + splash(); + wait(5); + + /* + * Initialize ethernet interface. + */ + lcd.cls(); + lcd.locate(0, 0); + lcd.printf("Initializing..."); + lcd.locate(0, 1); + lcd.printf("Ethernet: "); + EthernetErr ethErr = netif.setup(); + if (ethErr) { + lcd.printf("[NG]"); + error("Ethernet setup failed. Done with code %d.\n", ethErr); + } + lcd.printf("[OK]"); + wait(3); + + /* + * Read configuration variables from a file. + */ + lcd.cls(); + lcd.locate(0, 0); + lcd.printf("Reading..."); + lcd.locate(0, 1); + lcd.printf("Setup: "); + appconf_init(&appconf); + if (appconf_read(CONFIG_FILENAME, &appconf) != 0) { + lcd.printf("[NG]"); + error("Failure to read a configuration file.\n"); + } + lcd.printf("[OK]"); + wait(3); + + /* + * Initialize objects. + */ + PachubeV2CSV web(appconf.apikey); + const int feed_id = atoi(appconf.feedid); + + /* + * Check the pachube feautures. + */ + { + lcd.cls(); + lcd.locate(0, 0); + lcd.printf("Checking Pachube"); + lcd.locate(0, 1); + lcd.printf("Status:"); + int errcnt = 0; + if (web_post(&web, feed_id, 0, thermistor1.read()) == PACHUBE_CODE_OK) { + lcd.printf("o"); + } else { + lcd.printf("x"); + errcnt++; + } + if (web_post(&web, feed_id, 1, thermistor2.read()) == PACHUBE_CODE_OK) { + lcd.printf("o"); + } else { + lcd.printf("x"); + errcnt++; + } + if (web_post(&web, feed_id, 2, thermistor3.read()) == PACHUBE_CODE_OK) { + lcd.printf("o"); + } else { + lcd.printf("x"); + errcnt++; + } + if (web_post(&web, feed_id, 3, thermistor4.read()) == PACHUBE_CODE_OK) { + lcd.printf("o"); + } else { + lcd.printf("x"); + errcnt++; + } + if (web_post(&web, feed_id, 4, thermistor5.read()) == PACHUBE_CODE_OK) { + lcd.printf("o"); + } else { + lcd.printf("x"); + errcnt++; + } + if (errcnt == 5) { + lcd.printf("[NG]"); + error("Inavlid pachube configuration.\n"); + } + lcd.printf("[OK]"); + wait(3); + } + + int cnt = 0; + do { + /* + * Sense. + */ + lcd.cls(); + double v1 = 0, v2 = 0, v3 = 0, v4 = 0, v5 = 0; + for (int i = 0; i < appconf.interval; i++) { + led = 1 << (i % 4); + // printf("%d/%d\n", i + 1, appconf.interval); + v1 += thermistor1.read(); + v2 += thermistor2.read(); + v3 += thermistor3.read(); + v4 += thermistor4.read(); + v5 += thermistor5.read(); + lcd.locate(0, 0); + lcd.printf("| 0| 1| 2| 3| 4|"); + lcd.locate(0, 1); + lcd.printf("|%-2.0f|%-2.0f|%-2.0f|%-2.0f|%-2.0f|", + v1 / (i + 1), + v2 / (i + 1), + v3 / (i + 1), + v4 / (i + 1), + v5 / (i + 1)); + wait(1); + } + v1 /= appconf.interval; + v2 /= appconf.interval; + v3 /= appconf.interval; + v4 /= appconf.interval; + v5 /= appconf.interval; + cnt++; + + /* + * Post. + */ + lcd.cls(); + lcd.locate(0, 0); + lcd.printf("Posting No.%d", cnt); + lcd.locate(0, 1); + lcd.printf("Status:"); + int errcnt = 0; + if (web_post(&web, feed_id, 0, v1) == PACHUBE_CODE_OK) { + lcd.printf("o"); + } else { + lcd.printf("x"); + errcnt++; + } + if (web_post(&web, feed_id, 1, v2) == PACHUBE_CODE_OK) { + lcd.printf("o"); + } else { + lcd.printf("x"); + errcnt++; + } + if (web_post(&web, feed_id, 2, v3) == PACHUBE_CODE_OK) { + lcd.printf("o"); + } else { + lcd.printf("x"); + errcnt++; + } + if (web_post(&web, feed_id, 3, v4) == PACHUBE_CODE_OK) { + lcd.printf("o"); + } else { + lcd.printf("x"); + errcnt++; + } + if (web_post(&web, feed_id, 4, v5) == PACHUBE_CODE_OK) { + lcd.printf("o"); + } else { + lcd.printf("x"); + errcnt++; + } + if (errcnt == 5) { + lcd.printf("[NG]"); + } + lcd.printf("[OK]"); + wait(1); + + /* + * Write. + */ + lcd.cls(); + lcd.locate(0, 0); + lcd.printf("Writing No.%d", cnt); + lcd.locate(0, 1); + lcd.printf("Status: "); + if (file_write(v1, v2, v3, v4, v5) == 0) { + lcd.printf("[OK]"); + } else { + lcd.printf("[NG]"); + } + wait(1); + } while (1); +}
diff -r 000000000000 -r 798c62ea70a3 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Apr 04 21:33:25 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/9a9732ce53a1
diff -r 000000000000 -r 798c62ea70a3 mylib/ConfigFile.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mylib/ConfigFile.lib Mon Apr 04 21:33:25 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/shintamainjp/code/ConfigFile/#f6ceafabe9f8
diff -r 000000000000 -r 798c62ea70a3 mylib/Pachube.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mylib/Pachube.lib Mon Apr 04 21:33:25 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/shintamainjp/code/Pachube/#19accbe9a05e
diff -r 000000000000 -r 798c62ea70a3 mylib/ThermistorPack.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mylib/ThermistorPack.lib Mon Apr 04 21:33:25 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/shintamainjp/code/ThermistorPack/#4f84f03b1703
diff -r 000000000000 -r 798c62ea70a3 mylib/appconf/appconf.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mylib/appconf/appconf.cpp Mon Apr 04 21:33:25 2011 +0000 @@ -0,0 +1,99 @@ +/** + * ============================================================================= + * Application configuration for 'Expansion Board One' example no.2 + * http://mbed.org/users/shintamainjp/notebook/starboard_expbrd-one_ex2_en/ + * ============================================================================= + * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ============================================================================= + */ + +#include "appconf.h" +#include "ConfigFile.h" + +#define KEY_PACHUBE_APIKEY "PACHUBE_APIKEY" +#define KEY_PACHUBE_FEEDID "PACHUBE_FEEDID" +#define KEY_PACHUBE_INTERVAL "PACHUBE_INTERVAL" + +/** + * Initialize configuration. + * + * @param p A pointer to a application config. + */ +void appconf_init(appconf_t *p) { + memset(p->apikey, 0, sizeof(p->apikey)); + memset(p->feedid, 0, sizeof(p->feedid)); + p->interval = 0; +} + +/** + * Read configuration. + * + * @param filename Filename. + * @param p A pointer to a application config. + * @return Return zero if it succeed. + */ +int appconf_read(char *filename, appconf_t *p) { + ConfigFile cf; + if (!cf.read(filename)) { + return -1; + } + if (!cf.getValue(KEY_PACHUBE_APIKEY, p->apikey, sizeof(p->apikey))) { + return -2; + } + if (!cf.getValue(KEY_PACHUBE_FEEDID, p->feedid, sizeof(p->feedid))) { + return -3; + } + char buf[64]; + if (!cf.getValue(KEY_PACHUBE_INTERVAL, buf, sizeof(buf))) { + return -4; + } else { + if (sscanf(buf, "%d", &(p->interval)) != 1) { + return -5; + } + } + return 0; +} + +/** + * Write configuration. + * + * @param filename Filename. + * @param p A pointer to a application config. + * @return Return zero if it succeed. + */ +int appconf_write(char *filename, appconf_t *p) { + ConfigFile cf; + if (!cf.setValue(KEY_PACHUBE_APIKEY, p->apikey)) { + return -1; + } + if (!cf.setValue(KEY_PACHUBE_FEEDID, p->feedid)) { + return -2; + } + char buf[64]; + snprintf(buf, sizeof(buf) - 1, "%d", p->interval); + if (!cf.setValue(KEY_PACHUBE_INTERVAL, buf)) { + return -3; + } + if (!cf.write(filename)) { + return -4; + } + return 0; +}
diff -r 000000000000 -r 798c62ea70a3 mylib/appconf/appconf.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mylib/appconf/appconf.h Mon Apr 04 21:33:25 2011 +0000 @@ -0,0 +1,57 @@ +/** + * ============================================================================= + * Application configuration for 'Expansion Board One' example no.2 + * http://mbed.org/users/shintamainjp/notebook/starboard_expbrd-one_ex2_en/ + * ============================================================================= + * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ============================================================================= + */ + +typedef struct { + char apikey[256]; + char feedid[128]; + int interval; +} appconf_t; + +/** + * Initialize configuration. + * + * @param p A pointer to a application config. + */ +void appconf_init(appconf_t *p); + +/** + * Read configuration. + * + * @param filename Filename. + * @param p A pointer to a application config. + * @return Return zero if it succeed. + */ +int appconf_read(char *filename, appconf_t *p); + +/** + * Write configuration. + * + * @param filename Filename. + * @param p A pointer to a application config. + * @return Return zero if it succeed. + */ +int appconf_write(char *filename, appconf_t *p);