StarBoard Orange - Example application No.1 GoogleChartLogger with StarBoard Orange

Dependencies:   EthernetNetIf mbed

Committer:
shintamainjp
Date:
Wed Aug 11 10:24:25 2010 +0000
Revision:
1:123eff9ba7b2
Parent:
0:77d8b45a8f42

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shintamainjp 0:77d8b45a8f42 1 /**
shintamainjp 0:77d8b45a8f42 2 * StarBoard Orange - Example application No.1 (Version 0.0.1)
shintamainjp 0:77d8b45a8f42 3 * GoogleChartLogger with StarBoard Orange
shintamainjp 1:123eff9ba7b2 4 * See also ... http://mbed.org/users/shintamainjp/notebook/starboard_example1_ja/
shintamainjp 1:123eff9ba7b2 5 * See also ... http://mbed.org/users/shintamainjp/notebook/starboard_example1_en/
shintamainjp 0:77d8b45a8f42 6 *
shintamainjp 0:77d8b45a8f42 7 * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
shintamainjp 0:77d8b45a8f42 8 * http://shinta.main.jp/
shintamainjp 0:77d8b45a8f42 9 */
shintamainjp 0:77d8b45a8f42 10 #include "mbed.h"
shintamainjp 0:77d8b45a8f42 11 #include "SDFileSystem.h"
shintamainjp 0:77d8b45a8f42 12 #include "TextLCD.h"
shintamainjp 0:77d8b45a8f42 13 #include "EthernetNetIf.h"
shintamainjp 0:77d8b45a8f42 14 #include "HTTPClient.h"
shintamainjp 0:77d8b45a8f42 15 #include "SensorLM60.h"
shintamainjp 0:77d8b45a8f42 16 #include "SensorLM35.h"
shintamainjp 0:77d8b45a8f42 17 #include "SensorMCP9700.h"
shintamainjp 1:123eff9ba7b2 18 #include "SensorDummy.h"
shintamainjp 0:77d8b45a8f42 19 #include "GoogleChartLineChart.h"
shintamainjp 0:77d8b45a8f42 20
shintamainjp 0:77d8b45a8f42 21 #define SUPERTWEET_ACCOUNT "YourAccount"
shintamainjp 0:77d8b45a8f42 22 #define SUPERTWEET_PASSWORD "YourPassword"
shintamainjp 0:77d8b45a8f42 23
shintamainjp 0:77d8b45a8f42 24 #if 1
shintamainjp 0:77d8b45a8f42 25 /*
shintamainjp 0:77d8b45a8f42 26 * Use "DHCP"
shintamainjp 0:77d8b45a8f42 27 */
shintamainjp 0:77d8b45a8f42 28 EthernetNetIf eth;
shintamainjp 0:77d8b45a8f42 29 #else
shintamainjp 0:77d8b45a8f42 30 /*
shintamainjp 0:77d8b45a8f42 31 * Use "static IP address"
shintamainjp 0:77d8b45a8f42 32 *
shintamainjp 0:77d8b45a8f42 33 * -> IP
shintamainjp 0:77d8b45a8f42 34 * -> Subnet mask
shintamainjp 0:77d8b45a8f42 35 * -> Gateway
shintamainjp 0:77d8b45a8f42 36 * -> DNS
shintamainjp 0:77d8b45a8f42 37 */
shintamainjp 0:77d8b45a8f42 38 EthernetNetIf eth(
shintamainjp 0:77d8b45a8f42 39 IpAddr(xxx,xxx,xxx,xxx),
shintamainjp 0:77d8b45a8f42 40 IpAddr(xxx,xxx,xxx,xxx),
shintamainjp 0:77d8b45a8f42 41 IpAddr(xxx,xxx,xxx,xxx),
shintamainjp 0:77d8b45a8f42 42 IpAddr(xxx,xxx,xxx,xxx));
shintamainjp 0:77d8b45a8f42 43 #endif
shintamainjp 0:77d8b45a8f42 44 TextLCD lcd(p24, p25, p26, p27, p28, p29, p30);
shintamainjp 0:77d8b45a8f42 45 SDFileSystem sd(p5, p6, p7, p8, "sd");
shintamainjp 0:77d8b45a8f42 46 BusOut led(LED4, LED3, LED2, LED1);
shintamainjp 0:77d8b45a8f42 47 SensorMCP9700 sensor1(p20);
shintamainjp 0:77d8b45a8f42 48 SensorMCP9700 sensor2(p19);
shintamainjp 0:77d8b45a8f42 49
shintamainjp 0:77d8b45a8f42 50 void splash(void);
shintamainjp 0:77d8b45a8f42 51 void getDateTime(char *buf, int siz);
shintamainjp 0:77d8b45a8f42 52 int main(void);
shintamainjp 0:77d8b45a8f42 53
shintamainjp 0:77d8b45a8f42 54 /**
shintamainjp 0:77d8b45a8f42 55 * Display a splash screen.
shintamainjp 0:77d8b45a8f42 56 */
shintamainjp 0:77d8b45a8f42 57 void splash(void) {
shintamainjp 0:77d8b45a8f42 58 lcd.cls();
shintamainjp 0:77d8b45a8f42 59 lcd.locate(0, 0);
shintamainjp 0:77d8b45a8f42 60 lcd.printf("StarBoard Orange");
shintamainjp 0:77d8b45a8f42 61 lcd.locate(0, 1);
shintamainjp 0:77d8b45a8f42 62 lcd.printf("mbed NXP LPC1768");
shintamainjp 0:77d8b45a8f42 63 wait(3);
shintamainjp 0:77d8b45a8f42 64
shintamainjp 0:77d8b45a8f42 65 lcd.cls();
shintamainjp 0:77d8b45a8f42 66 lcd.locate(0, 0);
shintamainjp 0:77d8b45a8f42 67 lcd.printf("Example app No.1");
shintamainjp 0:77d8b45a8f42 68 lcd.locate(0, 1);
shintamainjp 0:77d8b45a8f42 69 lcd.printf("with GoogleChart");
shintamainjp 0:77d8b45a8f42 70 wait(3);
shintamainjp 0:77d8b45a8f42 71 }
shintamainjp 0:77d8b45a8f42 72
shintamainjp 0:77d8b45a8f42 73 /**
shintamainjp 0:77d8b45a8f42 74 * Set current date and time.
shintamainjp 0:77d8b45a8f42 75 *
shintamainjp 0:77d8b45a8f42 76 * @param year Year.
shintamainjp 0:77d8b45a8f42 77 * @param month Month.
shintamainjp 0:77d8b45a8f42 78 * @param day Day.
shintamainjp 0:77d8b45a8f42 79 * @param hour Hour.
shintamainjp 0:77d8b45a8f42 80 * @param min Minutes.
shintamainjp 0:77d8b45a8f42 81 * @param sec Seconds.
shintamainjp 0:77d8b45a8f42 82 */
shintamainjp 0:77d8b45a8f42 83 void setDateTime(int year, int month, int day, int hour, int min, int sec) {
shintamainjp 0:77d8b45a8f42 84 struct tm t;
shintamainjp 0:77d8b45a8f42 85 t.tm_sec = sec;
shintamainjp 0:77d8b45a8f42 86 t.tm_min = min;
shintamainjp 0:77d8b45a8f42 87 t.tm_hour = hour;
shintamainjp 0:77d8b45a8f42 88 t.tm_mday = day;
shintamainjp 0:77d8b45a8f42 89 t.tm_mon = month - 1;
shintamainjp 0:77d8b45a8f42 90 t.tm_year = year - 1900;
shintamainjp 0:77d8b45a8f42 91 time_t seconds = mktime(&t);
shintamainjp 0:77d8b45a8f42 92 set_time(seconds);
shintamainjp 0:77d8b45a8f42 93 }
shintamainjp 0:77d8b45a8f42 94
shintamainjp 0:77d8b45a8f42 95 /**
shintamainjp 0:77d8b45a8f42 96 * Get current date and time.
shintamainjp 0:77d8b45a8f42 97 *
shintamainjp 0:77d8b45a8f42 98 * @param buf Pointer to buffer.
shintamainjp 0:77d8b45a8f42 99 * @param siz Size of buffer.
shintamainjp 0:77d8b45a8f42 100 */
shintamainjp 0:77d8b45a8f42 101 void getDateTime(char *buf, int siz) {
shintamainjp 0:77d8b45a8f42 102 time_t seconds = time(NULL);
shintamainjp 0:77d8b45a8f42 103 struct tm *t = localtime(&seconds);
shintamainjp 0:77d8b45a8f42 104 strftime(buf, siz, "%b/%d/%Y %H:%M:%S", t);
shintamainjp 0:77d8b45a8f42 105 }
shintamainjp 0:77d8b45a8f42 106
shintamainjp 0:77d8b45a8f42 107 /**
shintamainjp 0:77d8b45a8f42 108 * Entry point.
shintamainjp 0:77d8b45a8f42 109 */
shintamainjp 0:77d8b45a8f42 110 int main(void) {
shintamainjp 0:77d8b45a8f42 111
shintamainjp 0:77d8b45a8f42 112 /*
shintamainjp 0:77d8b45a8f42 113 * Splash.
shintamainjp 0:77d8b45a8f42 114 */
shintamainjp 0:77d8b45a8f42 115 splash();
shintamainjp 0:77d8b45a8f42 116
shintamainjp 0:77d8b45a8f42 117 /*
shintamainjp 0:77d8b45a8f42 118 * Initialize ethernet interface.
shintamainjp 0:77d8b45a8f42 119 */
shintamainjp 0:77d8b45a8f42 120 lcd.cls();
shintamainjp 0:77d8b45a8f42 121 lcd.locate(0, 0);
shintamainjp 0:77d8b45a8f42 122 lcd.printf("Initializing. ");
shintamainjp 0:77d8b45a8f42 123 lcd.locate(0, 1);
shintamainjp 0:77d8b45a8f42 124 lcd.printf("Ethernet: ");
shintamainjp 0:77d8b45a8f42 125
shintamainjp 0:77d8b45a8f42 126 EthernetErr ethErr = eth.setup();
shintamainjp 0:77d8b45a8f42 127 if (ethErr) {
shintamainjp 0:77d8b45a8f42 128 lcd.locate(0, 1);
shintamainjp 0:77d8b45a8f42 129 lcd.printf("Ethernet:NG ");
shintamainjp 0:77d8b45a8f42 130 } else {
shintamainjp 0:77d8b45a8f42 131 lcd.locate(0, 1);
shintamainjp 0:77d8b45a8f42 132 lcd.printf("Ethernet:OK ");
shintamainjp 0:77d8b45a8f42 133 }
shintamainjp 0:77d8b45a8f42 134 wait(3);
shintamainjp 0:77d8b45a8f42 135
shintamainjp 0:77d8b45a8f42 136 /*
shintamainjp 0:77d8b45a8f42 137 * Check your SD card.
shintamainjp 0:77d8b45a8f42 138 */
shintamainjp 0:77d8b45a8f42 139 lcd.cls();
shintamainjp 0:77d8b45a8f42 140 lcd.locate(0, 0);
shintamainjp 0:77d8b45a8f42 141 lcd.printf("Checking SD card");
shintamainjp 0:77d8b45a8f42 142 lcd.locate(0, 1);
shintamainjp 0:77d8b45a8f42 143 lcd.printf("SD card: ");
shintamainjp 0:77d8b45a8f42 144 wait(1);
shintamainjp 0:77d8b45a8f42 145 FILE *fp = fopen("/sd/log_test.txt", "w");
shintamainjp 0:77d8b45a8f42 146 if (NULL != fp) {
shintamainjp 0:77d8b45a8f42 147 fprintf(fp, "This is a test file for SD card.");
shintamainjp 0:77d8b45a8f42 148 fclose(fp);
shintamainjp 0:77d8b45a8f42 149 lcd.locate(0, 1);
shintamainjp 0:77d8b45a8f42 150 lcd.printf("SD card:OK ");
shintamainjp 0:77d8b45a8f42 151 } else {
shintamainjp 0:77d8b45a8f42 152 lcd.locate(0, 1);
shintamainjp 0:77d8b45a8f42 153 lcd.printf("SD card:NG ");
shintamainjp 0:77d8b45a8f42 154 }
shintamainjp 0:77d8b45a8f42 155 wait(1);
shintamainjp 0:77d8b45a8f42 156
shintamainjp 0:77d8b45a8f42 157 /*
shintamainjp 0:77d8b45a8f42 158 * Setup http client object.
shintamainjp 0:77d8b45a8f42 159 *
shintamainjp 0:77d8b45a8f42 160 * Please replace this information by your account.
shintamainjp 0:77d8b45a8f42 161 */
shintamainjp 0:77d8b45a8f42 162 HTTPClient twitter;
shintamainjp 0:77d8b45a8f42 163 twitter.basicAuth(SUPERTWEET_ACCOUNT, SUPERTWEET_PASSWORD);
shintamainjp 0:77d8b45a8f42 164
shintamainjp 0:77d8b45a8f42 165 /*
shintamainjp 0:77d8b45a8f42 166 * Setup GoogleChart objects.
shintamainjp 0:77d8b45a8f42 167 */
shintamainjp 0:77d8b45a8f42 168 static const int SAMPLES = 60;
shintamainjp 0:77d8b45a8f42 169 static const int INTERVAL = 1;
shintamainjp 0:77d8b45a8f42 170
shintamainjp 0:77d8b45a8f42 171 GoogleChartLineChart chart;
shintamainjp 0:77d8b45a8f42 172 const int axs1 = chart.addNewAxis(Axis::Left);
shintamainjp 0:77d8b45a8f42 173 const int axs2 = chart.addNewAxis(Axis::Bottom);
shintamainjp 0:77d8b45a8f42 174 const int dsn1 = chart.addNewDataSet("Ch.1", SAMPLES);
shintamainjp 0:77d8b45a8f42 175 const int dsn2 = chart.addNewDataSet("Ch.2", SAMPLES);
shintamainjp 0:77d8b45a8f42 176
shintamainjp 0:77d8b45a8f42 177 static const int TEMP_HIGH = 50;
shintamainjp 0:77d8b45a8f42 178 static const int TEMP_LOW = -10;
shintamainjp 0:77d8b45a8f42 179
shintamainjp 0:77d8b45a8f42 180 chart.setAxisRange(axs1, TEMP_LOW, TEMP_HIGH);
shintamainjp 0:77d8b45a8f42 181 chart.setAxisRange(axs2, 0, SAMPLES);
shintamainjp 0:77d8b45a8f42 182 chart.setDataScale(dsn1, TEMP_LOW, TEMP_HIGH);
shintamainjp 0:77d8b45a8f42 183 chart.setDataScale(dsn2, TEMP_LOW, TEMP_HIGH);
shintamainjp 0:77d8b45a8f42 184
shintamainjp 0:77d8b45a8f42 185 /*
shintamainjp 0:77d8b45a8f42 186 * Set date and time. (If you want.)
shintamainjp 0:77d8b45a8f42 187 *
shintamainjp 0:77d8b45a8f42 188 * -> Year.
shintamainjp 0:77d8b45a8f42 189 * -> Month.
shintamainjp 0:77d8b45a8f42 190 * -> Day.
shintamainjp 0:77d8b45a8f42 191 * -> Hour.
shintamainjp 0:77d8b45a8f42 192 * -> Minutes.
shintamainjp 0:77d8b45a8f42 193 * -> Seconds.
shintamainjp 0:77d8b45a8f42 194 */
shintamainjp 0:77d8b45a8f42 195 #if 0
shintamainjp 0:77d8b45a8f42 196 setDateTime(2012, 1, 1, 0, 0, 0);
shintamainjp 0:77d8b45a8f42 197 #endif
shintamainjp 0:77d8b45a8f42 198
shintamainjp 0:77d8b45a8f42 199 /*
shintamainjp 0:77d8b45a8f42 200 * Start logging.
shintamainjp 0:77d8b45a8f42 201 */
shintamainjp 0:77d8b45a8f42 202 uint16_t count = 0;
shintamainjp 0:77d8b45a8f42 203 static const int WAIT_FOR_USER = 1;
shintamainjp 0:77d8b45a8f42 204 while (1) {
shintamainjp 0:77d8b45a8f42 205 /*
shintamainjp 0:77d8b45a8f42 206 * Get date and time
shintamainjp 0:77d8b45a8f42 207 */
shintamainjp 0:77d8b45a8f42 208 if (0 == (count % SAMPLES)) {
shintamainjp 0:77d8b45a8f42 209 char dt[64];
shintamainjp 0:77d8b45a8f42 210 getDateTime(dt, sizeof(dt));
shintamainjp 0:77d8b45a8f42 211 chart.setTitle(std::string(dt));
shintamainjp 0:77d8b45a8f42 212 }
shintamainjp 0:77d8b45a8f42 213 count++;
shintamainjp 0:77d8b45a8f42 214
shintamainjp 0:77d8b45a8f42 215 /*
shintamainjp 0:77d8b45a8f42 216 * Status on LED.
shintamainjp 0:77d8b45a8f42 217 */
shintamainjp 0:77d8b45a8f42 218 led = count & 0x0f;
shintamainjp 0:77d8b45a8f42 219
shintamainjp 0:77d8b45a8f42 220 /*
shintamainjp 0:77d8b45a8f42 221 * Logging current status.
shintamainjp 0:77d8b45a8f42 222 */
shintamainjp 0:77d8b45a8f42 223 double sample1 = sensor1.read();
shintamainjp 0:77d8b45a8f42 224 double sample2 = sensor2.read();
shintamainjp 0:77d8b45a8f42 225 chart.addData(dsn1, sample1);
shintamainjp 0:77d8b45a8f42 226 chart.addData(dsn2, sample2);
shintamainjp 0:77d8b45a8f42 227
shintamainjp 0:77d8b45a8f42 228 /*
shintamainjp 0:77d8b45a8f42 229 * Display the information.
shintamainjp 0:77d8b45a8f42 230 */
shintamainjp 0:77d8b45a8f42 231 lcd.cls();
shintamainjp 0:77d8b45a8f42 232 lcd.locate(0, 0);
shintamainjp 0:77d8b45a8f42 233 lcd.printf("Data=[%4.1f,%4.1f]", sample1, sample2);
shintamainjp 0:77d8b45a8f42 234 lcd.locate(0, 1);
shintamainjp 0:77d8b45a8f42 235 lcd.printf("Stat=[%4d/%4d]", count, SAMPLES);
shintamainjp 0:77d8b45a8f42 236
shintamainjp 0:77d8b45a8f42 237 /*
shintamainjp 0:77d8b45a8f42 238 * Write to storages if the data filled the chart.
shintamainjp 0:77d8b45a8f42 239 */
shintamainjp 0:77d8b45a8f42 240 if (0 == (count % SAMPLES)) {
shintamainjp 0:77d8b45a8f42 241 /*
shintamainjp 0:77d8b45a8f42 242 * Write to a SD card.
shintamainjp 0:77d8b45a8f42 243 */
shintamainjp 0:77d8b45a8f42 244 static int fcnt = 0;
shintamainjp 0:77d8b45a8f42 245 fcnt++;
shintamainjp 0:77d8b45a8f42 246 char fname[64];
shintamainjp 0:77d8b45a8f42 247 snprintf(fname, sizeof(fname), "/sd/log%05d.txt", fcnt);
shintamainjp 0:77d8b45a8f42 248 lcd.locate(0, 1);
shintamainjp 0:77d8b45a8f42 249 lcd.printf("SD card: ");
shintamainjp 0:77d8b45a8f42 250 FILE *fp = fopen(fname, "w");
shintamainjp 0:77d8b45a8f42 251 if (NULL != fp) {
shintamainjp 0:77d8b45a8f42 252 fprintf(fp, "%s\n", chart.toString().c_str());
shintamainjp 0:77d8b45a8f42 253 fclose(fp);
shintamainjp 0:77d8b45a8f42 254 lcd.locate(0, 1);
shintamainjp 0:77d8b45a8f42 255 lcd.printf("SD card:OK ");
shintamainjp 0:77d8b45a8f42 256 } else {
shintamainjp 0:77d8b45a8f42 257 lcd.locate(0, 1);
shintamainjp 0:77d8b45a8f42 258 lcd.printf("SD card:NG ");
shintamainjp 0:77d8b45a8f42 259 }
shintamainjp 0:77d8b45a8f42 260 wait(WAIT_FOR_USER);
shintamainjp 0:77d8b45a8f42 261
shintamainjp 0:77d8b45a8f42 262 /*
shintamainjp 0:77d8b45a8f42 263 * Write to twitter.
shintamainjp 0:77d8b45a8f42 264 */
shintamainjp 0:77d8b45a8f42 265 lcd.locate(0, 1);
shintamainjp 0:77d8b45a8f42 266 lcd.printf("Tweet: ");
shintamainjp 0:77d8b45a8f42 267 HTTPMap msg;
shintamainjp 0:77d8b45a8f42 268 msg["status"] = chart.toString();
shintamainjp 0:77d8b45a8f42 269 HTTPResult r = twitter.post("http://api.supertweet.net/1/statuses/update.xml", msg, NULL);
shintamainjp 0:77d8b45a8f42 270 if (r == HTTP_OK) {
shintamainjp 0:77d8b45a8f42 271 lcd.locate(0, 1);
shintamainjp 0:77d8b45a8f42 272 lcd.printf("Tweet:OK ");
shintamainjp 0:77d8b45a8f42 273 } else {
shintamainjp 0:77d8b45a8f42 274 lcd.locate(0, 1);
shintamainjp 0:77d8b45a8f42 275 lcd.printf("Tweet:NG(%02d) ", r);
shintamainjp 0:77d8b45a8f42 276 }
shintamainjp 0:77d8b45a8f42 277 wait(WAIT_FOR_USER);
shintamainjp 0:77d8b45a8f42 278
shintamainjp 0:77d8b45a8f42 279 /*
shintamainjp 0:77d8b45a8f42 280 * Clear the logged data.
shintamainjp 0:77d8b45a8f42 281 */
shintamainjp 0:77d8b45a8f42 282 chart.clearAllData(dsn1);
shintamainjp 0:77d8b45a8f42 283 chart.clearAllData(dsn2);
shintamainjp 0:77d8b45a8f42 284 count = 0;
shintamainjp 0:77d8b45a8f42 285 }
shintamainjp 0:77d8b45a8f42 286 wait(INTERVAL);
shintamainjp 0:77d8b45a8f42 287 }
shintamainjp 0:77d8b45a8f42 288
shintamainjp 0:77d8b45a8f42 289 return 0;
shintamainjp 0:77d8b45a8f42 290 }