ネットワークアップデート機能とか、Pachubeへの情報登録とかの処理を追加しています

Dependencies:   Terminal EthernetNetIf Pachube TextLCD mbed ConfigFile FirmwareUpdater

Committer:
abe00makoto
Date:
Tue May 17 13:49:41 2011 +0000
Revision:
0:a62f36392b9b
Child:
1:1eb67d074bed
sbm-20 gm-tube support
pachube support
network update suport

Who changed what in which revision?

UserRevisionLine numberNew contents of line
abe00makoto 0:a62f36392b9b 1 #include "mbed.h"
abe00makoto 0:a62f36392b9b 2 #include "geigercounter_sbm_20.h"
abe00makoto 0:a62f36392b9b 3 #include "trans.h"
abe00makoto 0:a62f36392b9b 4 #include "TextLCD.h"
abe00makoto 0:a62f36392b9b 5
abe00makoto 0:a62f36392b9b 6
abe00makoto 0:a62f36392b9b 7 #include "mbed.h"
abe00makoto 0:a62f36392b9b 8 #include "PachubeV2CSV.h"
abe00makoto 0:a62f36392b9b 9 #include "EthernetNetIf.h"
abe00makoto 0:a62f36392b9b 10 #include "HTTPClient.h"
abe00makoto 0:a62f36392b9b 11 #include "appconf.h"
abe00makoto 0:a62f36392b9b 12 #include "FirmwareUpdater.h"
abe00makoto 0:a62f36392b9b 13
abe00makoto 0:a62f36392b9b 14
abe00makoto 0:a62f36392b9b 15
abe00makoto 0:a62f36392b9b 16 extern "C" void mbed_reset();
abe00makoto 0:a62f36392b9b 17
abe00makoto 0:a62f36392b9b 18 /*
abe00makoto 0:a62f36392b9b 19 * Definitions for a configuration file.
abe00makoto 0:a62f36392b9b 20 */
abe00makoto 0:a62f36392b9b 21 #define CONFIG_FILENAME "/local/PACHUBE.CFG"
abe00makoto 0:a62f36392b9b 22 const int PACHUBE_CODE_OK = 200;
abe00makoto 0:a62f36392b9b 23
abe00makoto 0:a62f36392b9b 24 LocalFileSystem localfs("local");
abe00makoto 0:a62f36392b9b 25
abe00makoto 0:a62f36392b9b 26 EthernetNetIf netif;
abe00makoto 0:a62f36392b9b 27
abe00makoto 0:a62f36392b9b 28 FirmwareUpdater fwup("http://mbed.org/media/uploads/abe00makoto", "geiger", true);
abe00makoto 0:a62f36392b9b 29
abe00makoto 0:a62f36392b9b 30 TextLCD output( p24, p26, p27, p28, p29, p30 ); // rs, e, d0-d3
abe00makoto 0:a62f36392b9b 31 Geigercounter_SBM_20 geiger(p18,p22);//Geiger pin,Spekaer pin
abe00makoto 0:a62f36392b9b 32 Trans trans(p21,0.16/1000.0,0.76); //Trans pin,period,duty
abe00makoto 0:a62f36392b9b 33
abe00makoto 0:a62f36392b9b 34 static appconf_t appconf;
abe00makoto 0:a62f36392b9b 35
abe00makoto 0:a62f36392b9b 36
abe00makoto 0:a62f36392b9b 37 /**
abe00makoto 0:a62f36392b9b 38 * Convert double to char.
abe00makoto 0:a62f36392b9b 39 *
abe00makoto 0:a62f36392b9b 40 * @param val Value.
abe00makoto 0:a62f36392b9b 41 * @param buf A pointer to a buffer.
abe00makoto 0:a62f36392b9b 42 * @param bufsiz The buffer size.
abe00makoto 0:a62f36392b9b 43 */
abe00makoto 0:a62f36392b9b 44 void convertDoubleToChar(double val, char *buf, size_t bufsiz) {
abe00makoto 0:a62f36392b9b 45 snprintf(buf, bufsiz, "%f", val);
abe00makoto 0:a62f36392b9b 46 }
abe00makoto 0:a62f36392b9b 47
abe00makoto 0:a62f36392b9b 48 /**
abe00makoto 0:a62f36392b9b 49 * Post to the feed on Pachube.
abe00makoto 0:a62f36392b9b 50 *
abe00makoto 0:a62f36392b9b 51 * @param web Pointer to a Pachube object.
abe00makoto 0:a62f36392b9b 52 * @param feed_id Feed ID.
abe00makoto 0:a62f36392b9b 53 * @param stream_no Stream number.
abe00makoto 0:a62f36392b9b 54 * @param value value.
abe00makoto 0:a62f36392b9b 55 *
abe00makoto 0:a62f36392b9b 56 * @return Pachube code.
abe00makoto 0:a62f36392b9b 57 */
abe00makoto 0:a62f36392b9b 58 int web_post(PachubeV2CSV *web, int feed_id, int stream_no, double value) {
abe00makoto 0:a62f36392b9b 59 char value_text[16];
abe00makoto 0:a62f36392b9b 60 convertDoubleToChar(value, value_text, sizeof(value_text));
abe00makoto 0:a62f36392b9b 61 char stream_no_text[8];
abe00makoto 0:a62f36392b9b 62 stream_no_text[0] = "0123456789"[stream_no];
abe00makoto 0:a62f36392b9b 63 stream_no_text[1] = '\0';
abe00makoto 0:a62f36392b9b 64 return web->updateDataStream(feed_id, stream_no_text, std::string(value_text));
abe00makoto 0:a62f36392b9b 65 }
abe00makoto 0:a62f36392b9b 66
abe00makoto 0:a62f36392b9b 67
abe00makoto 0:a62f36392b9b 68
abe00makoto 0:a62f36392b9b 69 //firm ware updater
abe00makoto 0:a62f36392b9b 70 void check_newfirm() {
abe00makoto 0:a62f36392b9b 71 if (fwup.exist() == 0) {
abe00makoto 0:a62f36392b9b 72 output.printf("Found a new firmware.\n");
abe00makoto 0:a62f36392b9b 73 if (fwup.execute() == 0) {
abe00makoto 0:a62f36392b9b 74 output.printf("Update succeed.\n");
abe00makoto 0:a62f36392b9b 75 wait(10);
abe00makoto 0:a62f36392b9b 76 output.printf("Resetting this system.\n");
abe00makoto 0:a62f36392b9b 77 wait(10);
abe00makoto 0:a62f36392b9b 78 fwup.reset();
abe00makoto 0:a62f36392b9b 79 } else {
abe00makoto 0:a62f36392b9b 80 output.printf("Update failed!\n");
abe00makoto 0:a62f36392b9b 81
abe00makoto 0:a62f36392b9b 82 }
abe00makoto 0:a62f36392b9b 83 }else{
abe00makoto 0:a62f36392b9b 84 output.printf("not found update.\n");
abe00makoto 0:a62f36392b9b 85 }
abe00makoto 0:a62f36392b9b 86 wait(5);
abe00makoto 0:a62f36392b9b 87 }
abe00makoto 0:a62f36392b9b 88
abe00makoto 0:a62f36392b9b 89 void print_geigerdata()
abe00makoto 0:a62f36392b9b 90 {
abe00makoto 0:a62f36392b9b 91 output.cls();
abe00makoto 0:a62f36392b9b 92 output.printf("%.3fuSv/h\n",geiger.getusv());
abe00makoto 0:a62f36392b9b 93 output.printf("%.3fCPM",geiger.getcpm());
abe00makoto 0:a62f36392b9b 94 }
abe00makoto 0:a62f36392b9b 95
abe00makoto 0:a62f36392b9b 96
abe00makoto 0:a62f36392b9b 97
abe00makoto 0:a62f36392b9b 98
abe00makoto 0:a62f36392b9b 99 int main() {
abe00makoto 0:a62f36392b9b 100
abe00makoto 0:a62f36392b9b 101 bool etherconnect;
abe00makoto 0:a62f36392b9b 102 output.cls();
abe00makoto 0:a62f36392b9b 103 output.printf("Hello.");
abe00makoto 0:a62f36392b9b 104 wait(1);
abe00makoto 0:a62f36392b9b 105
abe00makoto 0:a62f36392b9b 106
abe00makoto 0:a62f36392b9b 107 /*
abe00makoto 0:a62f36392b9b 108 * Initialize ethernet interface.
abe00makoto 0:a62f36392b9b 109 */
abe00makoto 0:a62f36392b9b 110 output.cls();
abe00makoto 0:a62f36392b9b 111 output.locate(0, 0);
abe00makoto 0:a62f36392b9b 112 output.printf("Initializing...");
abe00makoto 0:a62f36392b9b 113 output.locate(0, 1);
abe00makoto 0:a62f36392b9b 114 output.printf("Ethernet: ");
abe00makoto 0:a62f36392b9b 115
abe00makoto 0:a62f36392b9b 116 EthernetErr ethErr = netif.setup();
abe00makoto 0:a62f36392b9b 117 if (ethErr) {
abe00makoto 0:a62f36392b9b 118 output.printf("[NG]");
abe00makoto 0:a62f36392b9b 119 //error("Ethernet setup failed. Done with code %d.\n", ethErr);
abe00makoto 0:a62f36392b9b 120 etherconnect=false;
abe00makoto 0:a62f36392b9b 121 }
abe00makoto 0:a62f36392b9b 122 else{
abe00makoto 0:a62f36392b9b 123 etherconnect=true;
abe00makoto 0:a62f36392b9b 124 output.printf("[OK]");
abe00makoto 0:a62f36392b9b 125 }
abe00makoto 0:a62f36392b9b 126
abe00makoto 0:a62f36392b9b 127 wait(2);
abe00makoto 0:a62f36392b9b 128 if(etherconnect){
abe00makoto 0:a62f36392b9b 129 //firmware update
abe00makoto 0:a62f36392b9b 130 output.cls();
abe00makoto 0:a62f36392b9b 131 check_newfirm();
abe00makoto 0:a62f36392b9b 132 }
abe00makoto 0:a62f36392b9b 133
abe00makoto 0:a62f36392b9b 134 /*
abe00makoto 0:a62f36392b9b 135 * Read configuration variables from a file.
abe00makoto 0:a62f36392b9b 136 */
abe00makoto 0:a62f36392b9b 137 output.cls();
abe00makoto 0:a62f36392b9b 138 output.locate(0, 0);
abe00makoto 0:a62f36392b9b 139 output.printf("cfg file Reading...");
abe00makoto 0:a62f36392b9b 140 output.locate(0, 1);
abe00makoto 0:a62f36392b9b 141 output.printf("Setup: ");
abe00makoto 0:a62f36392b9b 142 appconf_init(&appconf);
abe00makoto 0:a62f36392b9b 143 if (appconf_read(CONFIG_FILENAME, &appconf) != 0) {
abe00makoto 0:a62f36392b9b 144 output.printf("[NG]");
abe00makoto 0:a62f36392b9b 145 error("Failure to read a configuration file.\n");
abe00makoto 0:a62f36392b9b 146 }
abe00makoto 0:a62f36392b9b 147 else{
abe00makoto 0:a62f36392b9b 148 output.printf("[OK]");
abe00makoto 0:a62f36392b9b 149 wait(3);
abe00makoto 0:a62f36392b9b 150 }
abe00makoto 0:a62f36392b9b 151
abe00makoto 0:a62f36392b9b 152 /*
abe00makoto 0:a62f36392b9b 153 * Initialize objects.
abe00makoto 0:a62f36392b9b 154 */
abe00makoto 0:a62f36392b9b 155 PachubeV2CSV web(appconf.apikey);
abe00makoto 0:a62f36392b9b 156 const int feed_id = atoi(appconf.feedid);
abe00makoto 0:a62f36392b9b 157
abe00makoto 0:a62f36392b9b 158
abe00makoto 0:a62f36392b9b 159
abe00makoto 0:a62f36392b9b 160 //20 minitues loop
abe00makoto 0:a62f36392b9b 161 for(int min=0;min<(20/5);min++){
abe00makoto 0:a62f36392b9b 162 trans.on();
abe00makoto 0:a62f36392b9b 163 wait(0.5);
abe00makoto 0:a62f36392b9b 164 geiger.start();
abe00makoto 0:a62f36392b9b 165 //5 minitues geiger run
abe00makoto 0:a62f36392b9b 166 for(int i=0;i<5*60;i++){
abe00makoto 0:a62f36392b9b 167 print_geigerdata();
abe00makoto 0:a62f36392b9b 168 wait(1);
abe00makoto 0:a62f36392b9b 169 }
abe00makoto 0:a62f36392b9b 170 geiger.stop();
abe00makoto 0:a62f36392b9b 171 trans.off();
abe00makoto 0:a62f36392b9b 172 output.cls();
abe00makoto 0:a62f36392b9b 173 print_geigerdata();
abe00makoto 0:a62f36392b9b 174 wait(1);
abe00makoto 0:a62f36392b9b 175 /*
abe00makoto 0:a62f36392b9b 176 * Check the pachube feautures.
abe00makoto 0:a62f36392b9b 177 */
abe00makoto 0:a62f36392b9b 178 if(etherconnect){
abe00makoto 0:a62f36392b9b 179
abe00makoto 0:a62f36392b9b 180 if (web_post(&web, feed_id, 0, geiger.getusv()) != PACHUBE_CODE_OK) {
abe00makoto 0:a62f36392b9b 181 output.printf("Checking Pachube status:x");
abe00makoto 0:a62f36392b9b 182 wait(10);
abe00makoto 0:a62f36392b9b 183 }
abe00makoto 0:a62f36392b9b 184
abe00makoto 0:a62f36392b9b 185 if (web_post(&web, feed_id, 1, geiger.getcpm()) != PACHUBE_CODE_OK){
abe00makoto 0:a62f36392b9b 186 output.printf("Checking Pachube status:x");
abe00makoto 0:a62f36392b9b 187 wait(10);
abe00makoto 0:a62f36392b9b 188 }
abe00makoto 0:a62f36392b9b 189 }
abe00makoto 0:a62f36392b9b 190 }
abe00makoto 0:a62f36392b9b 191
abe00makoto 0:a62f36392b9b 192 if(!etherconnect){
abe00makoto 0:a62f36392b9b 193 output.cls();
abe00makoto 0:a62f36392b9b 194 output.printf("please!!\n net connect\n");
abe00makoto 0:a62f36392b9b 195 wait(10);
abe00makoto 0:a62f36392b9b 196 output.printf("mbed reset!!\n");
abe00makoto 0:a62f36392b9b 197 wait(10);
abe00makoto 0:a62f36392b9b 198 mbed_reset();
abe00makoto 0:a62f36392b9b 199 }
abe00makoto 0:a62f36392b9b 200
abe00makoto 0:a62f36392b9b 201
abe00makoto 0:a62f36392b9b 202 //
abe00makoto 0:a62f36392b9b 203 // night running mode.
abe00makoto 0:a62f36392b9b 204 //
abe00makoto 0:a62f36392b9b 205 output.cls();
abe00makoto 0:a62f36392b9b 206 output.printf("sound off mode..");
abe00makoto 0:a62f36392b9b 207 geiger.soundoff();
abe00makoto 0:a62f36392b9b 208
abe00makoto 0:a62f36392b9b 209 int errcnt=0;
abe00makoto 0:a62f36392b9b 210 while(1){
abe00makoto 0:a62f36392b9b 211
abe00makoto 0:a62f36392b9b 212 for(int min=0;min<(20/5);min++){
abe00makoto 0:a62f36392b9b 213 trans.on();
abe00makoto 0:a62f36392b9b 214 wait(1);
abe00makoto 0:a62f36392b9b 215 geiger.start();
abe00makoto 0:a62f36392b9b 216 for(int i=0;i<5*60;i++){
abe00makoto 0:a62f36392b9b 217 print_geigerdata();
abe00makoto 0:a62f36392b9b 218 wait(1);
abe00makoto 0:a62f36392b9b 219 }
abe00makoto 0:a62f36392b9b 220
abe00makoto 0:a62f36392b9b 221 geiger.stop();
abe00makoto 0:a62f36392b9b 222 trans.off();
abe00makoto 0:a62f36392b9b 223
abe00makoto 0:a62f36392b9b 224 /*
abe00makoto 0:a62f36392b9b 225 * Post.
abe00makoto 0:a62f36392b9b 226 */
abe00makoto 0:a62f36392b9b 227 if(etherconnect){
abe00makoto 0:a62f36392b9b 228 if (web_post(&web, feed_id, 0, geiger.getusv()) != PACHUBE_CODE_OK) {
abe00makoto 0:a62f36392b9b 229 output.printf("Checking Pachube status:x");
abe00makoto 0:a62f36392b9b 230 errcnt++;
abe00makoto 0:a62f36392b9b 231 wait(10);
abe00makoto 0:a62f36392b9b 232 }
abe00makoto 0:a62f36392b9b 233
abe00makoto 0:a62f36392b9b 234 if (web_post(&web, feed_id, 1, geiger.getcpm()) != PACHUBE_CODE_OK){
abe00makoto 0:a62f36392b9b 235 output.printf("Checking Pachube status:x");
abe00makoto 0:a62f36392b9b 236 errcnt++;
abe00makoto 0:a62f36392b9b 237 wait(10);
abe00makoto 0:a62f36392b9b 238 }
abe00makoto 0:a62f36392b9b 239 }//if(etherconncet)
abe00makoto 0:a62f36392b9b 240 }//for
abe00makoto 0:a62f36392b9b 241
abe00makoto 0:a62f36392b9b 242 //if firmware exist.reset.
abe00makoto 0:a62f36392b9b 243 if(etherconnect){
abe00makoto 0:a62f36392b9b 244 if(fwup.exist()==0)mbed_reset();
abe00makoto 0:a62f36392b9b 245 }
abe00makoto 0:a62f36392b9b 246
abe00makoto 0:a62f36392b9b 247 //many error!! reset
abe00makoto 0:a62f36392b9b 248 if(errcnt>20)mbed_reset();
abe00makoto 0:a62f36392b9b 249 }//while
abe00makoto 0:a62f36392b9b 250 }