GMCStation

Dependencies:   EthernetNetIf TextLCD mbed Station

Committer:
yamaguch
Date:
Fri Jan 06 06:50:24 2012 +0000
Revision:
2:2870a9512d73
Parent:
0:3951ca5a2511

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yamaguch 0:3951ca5a2511 1 /*
yamaguch 0:3951ca5a2511 2 Copyright (c) 2011, Senio Networks, Inc.
yamaguch 0:3951ca5a2511 3
yamaguch 0:3951ca5a2511 4 Permission is hereby granted, free of charge, to any person obtaining a copy
yamaguch 0:3951ca5a2511 5 of this software and associated documentation files (the "Software"), to deal
yamaguch 0:3951ca5a2511 6 in the Software without restriction, including without limitation the rights
yamaguch 0:3951ca5a2511 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
yamaguch 0:3951ca5a2511 8 copies of the Software, and to permit persons to whom the Software is
yamaguch 0:3951ca5a2511 9 furnished to do so, subject to the following conditions:
yamaguch 0:3951ca5a2511 10
yamaguch 0:3951ca5a2511 11 The above copyright notice and this permission notice shall be included in
yamaguch 0:3951ca5a2511 12 all copies or substantial portions of the Software.
yamaguch 0:3951ca5a2511 13
yamaguch 0:3951ca5a2511 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
yamaguch 0:3951ca5a2511 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
yamaguch 0:3951ca5a2511 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
yamaguch 0:3951ca5a2511 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
yamaguch 0:3951ca5a2511 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
yamaguch 0:3951ca5a2511 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
yamaguch 0:3951ca5a2511 20 THE SOFTWARE.
yamaguch 0:3951ca5a2511 21 */
yamaguch 0:3951ca5a2511 22
yamaguch 0:3951ca5a2511 23 #include "GMCStation.h"
yamaguch 0:3951ca5a2511 24 #include "PachubeClient.h"
yamaguch 0:3951ca5a2511 25 #include "SuperTweetClient.h"
yamaguch 0:3951ca5a2511 26
yamaguch 0:3951ca5a2511 27 int main() {
yamaguch 0:3951ca5a2511 28 Ether ether = Ether::create("config.txt");
yamaguch 0:3951ca5a2511 29 ether.setup();
yamaguch 0:3951ca5a2511 30 GMCounter gmc = GMCounter::create(p17, p22, p23, "config.txt"); // gmc, buzzer, led
yamaguch 0:3951ca5a2511 31 Location location = Location::create("config.txt");
yamaguch 0:3951ca5a2511 32 NTPClient ntpClient = NTPClient::create("config.txt");
yamaguch 0:3951ca5a2511 33 TextLCD lcd(p24, p26, p27, p28, p29, p30); // rs, e, d4-d7
yamaguch 0:3951ca5a2511 34 PachubeClient datastream = PachubeClient::create("config.txt");
yamaguch 0:3951ca5a2511 35 SuperTweetClient supertweet = SuperTweetClient::create("config.txt");
yamaguch 0:3951ca5a2511 36 GMCStation station(1234, gmc, location, ntpClient, lcd);
yamaguch 0:3951ca5a2511 37 Timer timer;
yamaguch 0:3951ca5a2511 38
yamaguch 0:3951ca5a2511 39 station.display(" GMCStation", " ver 1.0");
yamaguch 0:3951ca5a2511 40 wait(5);
yamaguch 0:3951ca5a2511 41
yamaguch 0:3951ca5a2511 42 if (ether.isActive()) {
yamaguch 0:3951ca5a2511 43 char ipAddress[17];
yamaguch 0:3951ca5a2511 44 char *ip = ether.getIpAddress();
yamaguch 0:3951ca5a2511 45 sprintf(ipAddress, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
yamaguch 0:3951ca5a2511 46 station.display(ipAddress, "Port = 1234");
yamaguch 0:3951ca5a2511 47 } else {
yamaguch 0:3951ca5a2511 48 station.display("Network error");
yamaguch 0:3951ca5a2511 49 error("network error\n");
yamaguch 0:3951ca5a2511 50 }
yamaguch 0:3951ca5a2511 51
yamaguch 0:3951ca5a2511 52 wait(5);
yamaguch 0:3951ca5a2511 53
yamaguch 0:3951ca5a2511 54 if (!ntpClient.setTime()) {
yamaguch 0:3951ca5a2511 55 station.display("No RTC server", "available");
yamaguch 0:3951ca5a2511 56 wait(5);
yamaguch 0:3951ca5a2511 57 }
yamaguch 0:3951ca5a2511 58
yamaguch 0:3951ca5a2511 59 char message[17] = {};
yamaguch 0:3951ca5a2511 60 while (true) {
yamaguch 0:3951ca5a2511 61 timer.reset();
yamaguch 0:3951ca5a2511 62 timer.start();
yamaguch 0:3951ca5a2511 63 station.handleClient();
yamaguch 0:3951ca5a2511 64
yamaguch 0:3951ca5a2511 65 time_t seconds = time(NULL) + 9 * 3600;
yamaguch 0:3951ca5a2511 66 char timestamp[16];
yamaguch 0:3951ca5a2511 67 strftime(timestamp, sizeof(timestamp), "%m/%d %X", localtime(&seconds));
yamaguch 0:3951ca5a2511 68 float cpm = gmc.getAverageCPM();
yamaguch 0:3951ca5a2511 69 float uSv = gmc.getAverageRadiation();
yamaguch 0:3951ca5a2511 70
yamaguch 0:3951ca5a2511 71 // update pachube every minute
yamaguch 0:3951ca5a2511 72 if (seconds % 60 == 0) {
yamaguch 0:3951ca5a2511 73 datastream.add(0, uSv);
yamaguch 0:3951ca5a2511 74 datastream.add(1, cpm);
yamaguch 0:3951ca5a2511 75 bool result = datastream.update();
yamaguch 0:3951ca5a2511 76 printf("Pachube datastream update %s\n", result ? "succeeded." : "failed.");
yamaguch 0:3951ca5a2511 77 sprintf(message, "Pachube %s", result ? "updated" : "failed");
yamaguch 0:3951ca5a2511 78 }
yamaguch 0:3951ca5a2511 79
yamaguch 0:3951ca5a2511 80 // tweet every hour
yamaguch 0:3951ca5a2511 81 if (seconds % 3600 == 2) {
yamaguch 0:3951ca5a2511 82 bool result = supertweet.tweet("", cpm, uSv);
yamaguch 0:3951ca5a2511 83 printf("Tweet CPM & uSv %s\n", result ? "succeeded." : "failed.");
yamaguch 0:3951ca5a2511 84 sprintf(message, "Tweet %s", result ? "succeeded" : "failed");
yamaguch 0:3951ca5a2511 85 }
yamaguch 0:3951ca5a2511 86
yamaguch 0:3951ca5a2511 87 // adjust RTC every 0.5 hours (for my injured mbed)
yamaguch 0:3951ca5a2511 88 if (seconds % 1800 == 4) {
yamaguch 0:3951ca5a2511 89 bool result = ntpClient.setTime();
yamaguch 0:3951ca5a2511 90 printf("RTC %s\n", result ? "adjusted." : "not adjusted.");
yamaguch 0:3951ca5a2511 91 sprintf(message, "RTC %s", result ? "adjusted" : "not adjusted");
yamaguch 0:3951ca5a2511 92 }
yamaguch 0:3951ca5a2511 93
yamaguch 0:3951ca5a2511 94 if (!message[0]) // if no message written
yamaguch 0:3951ca5a2511 95 sprintf(message, "%3.1f, %4.3fuSv", cpm, uSv);
yamaguch 0:3951ca5a2511 96
yamaguch 0:3951ca5a2511 97 station.display(timestamp, message);
yamaguch 0:3951ca5a2511 98 printf("%s: %3.1f, %4.3fuSv\n", timestamp, cpm, uSv);
yamaguch 0:3951ca5a2511 99
yamaguch 0:3951ca5a2511 100 float duration = 1 - timer.read();
yamaguch 0:3951ca5a2511 101 if (duration > 0) wait(duration);
yamaguch 0:3951ca5a2511 102
yamaguch 0:3951ca5a2511 103 if (seconds % 2 == 1) // clear message every other second
yamaguch 0:3951ca5a2511 104 message[0] = 0;
yamaguch 0:3951ca5a2511 105 }
yamaguch 0:3951ca5a2511 106 }