physicien nocturne / gmon

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers gmon.cpp Source File

gmon.cpp

00001 #include "mbed.h"
00002 #include "WiflyInterface.h"
00003 #include "LM75B.h"
00004 #include "MMA7660.h"
00005 #include "C12832.h"
00006 #include "MSCFileSystem.h"
00007 #include "NTPClient.h"
00008 
00009 
00010 /* wifly interface:
00011 *     - p9 and p10 are for the serial communication
00012 *     - p30 is for the reset pin
00013 *     - p29 is for the connection status
00014 *     - "mbed" is the ssid of the network
00015 *     - "password" is the password
00016 *     - WPA is the security
00017 */
00018 WiflyInterface wifly(p9, p10, p30, p29, "mbed", "password", WPA);
00019 NTPClient ntp; // Don't move this line
00020 MSCFileSystem fs("fs");
00021 C12832 lcd(p5, p7, p6, p8, p11);
00022 DigitalIn my_abort(p14);
00023 
00024 // accelerometer
00025 MMA7660 MMA(p28, p27);
00026 
00027 // temperature sensor
00028 LM75B tmp(p28,p27);
00029 
00030 float x,y,z;
00031 int temp;
00032 char filename[50];
00033 
00034 int main()
00035 {
00036     time_t ctTime;
00037 
00038     // Display welcoming message
00039     lcd.cls();
00040     lcd.printf("mbed g monitor\n");
00041 
00042     // Connect to local wifi network
00043     wifly.init(); //Use DHCP
00044     while (!wifly.connect());
00045     lcd.printf("IP Address is %s\n", wifly.getIPAddress());
00046 
00047     // Get current time (UTC) through internet
00048     lcd.printf("Trying to update time...\n");
00049     if (ntp.setTime("0.pool.ntp.org") == 0) {
00050         ctTime = time(NULL);
00051         set_time(ctTime);
00052         
00053         // Display current time (UTC) on the LCD
00054         lcd.cls();
00055         lcd.printf("Time (UTC): \n%s\n", ctime(&ctTime));
00056     } else {
00057         // Problem setting the current time
00058         // FIXME allow user to set time and date manually
00059         lcd.printf("NTP Error, press reset to retry\n");
00060         while(1);
00061     }
00062 
00063     wait(3);
00064     lcd.cls(); // Clear the LCD screen
00065 
00066     // Check for the accelerometer's presence
00067     if (MMA.testConnection()) {
00068         for (int j = 0; j < 36; j++) {
00069             sprintf(filename,"/fs/gmon%d.csv",j);
00070             lcd.printf("Opening file #%d\n",j);
00071             
00072             FILE *fp = fopen(filename,"w");
00073             // Check if file was properly opened
00074             if (fp != NULL) {
00075                 printf("Create filehandle for %s\r\n",filename);
00076                 printf("Writing to file\r\n");
00077         
00078                 // Take 100000 samples per file
00079                 for (int i = 0; i < 100000; i++) {
00080                     // Read the accelerometer
00081                     x = MMA.x();
00082                     y = MMA.y();
00083                     z = MMA.z();
00084                     
00085                     // Read the temperature
00086                     temp = (int)tmp.read();
00087                     
00088                     // Get current time
00089                     ctTime = time(NULL);
00090                     
00091                     // Write the data to file
00092                     fprintf(fp,"%0.5f, %0.5f, %0.5f, %d, %s",x,y,z,temp, ctime(&ctTime));
00093                     
00094                     // If something goes wrong/was not right press on the joystick button
00095                     if (my_abort){
00096                         // Close the currently opened file to prevent corrupt data
00097                         fclose(fp);
00098                         lcd.cls();
00099                         lcd.printf("Aborting, USB Flash can be removed\n");
00100                         
00101                         // infinite loop
00102                         while(1);
00103                     }
00104                     
00105                     // take one sample every 30 seconds                        
00106                     wait(30);
00107                 }
00108                 fclose(fp);
00109                 printf("Close the handle\n");
00110             } else {
00111                 printf("Can't open %s\r\n",filename);
00112             }
00113         }
00114     }
00115     lcd.cls(); // Clear the LCD screen
00116     lcd.printf("End of data acquisition\n");
00117 }