Brandon Fictorie / Mbed 2 deprecated BF_Websocket

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Websocket.h"
00003 #include "dnsresolve.h"
00004 #include "EthernetNetIf.h"
00005 #include "EE4040.h"
00006 #include "NTPClient.h"
00007 #include "Time.h"
00008 
00009 //------------------Initialization----------------------//
00010 
00011 Serial pc(USBTX, USBRX);                                // Initialize serial communication over USB
00012 Timer tmr;                                              // Initialize a new Timer called tmr
00013 LocalFileSystem local("local");                             // This is the directory where Halifax.csv vill be stored
00014 
00015 // a method to update the time from a timeserver
00016 void updateTime() {                                         // a function to update the time
00017     time_t ctTime;                                          // ??
00018     time(&ctTime);                                          // ??
00019     // printf("Current time is (UTC): %s\r\n", ctime(&ctTime));
00020 
00021     NTPClient ntp;
00022     Host server(IpAddr(), 123, "0.de.pool.ntp.org");            // check that this Server is OK?
00023     ntp.setTime(server);
00024 
00025     // printf("set time ok\r\n");
00026     time(&ctTime);
00027     // printf("Current time is (UTC): %s\r\n", ctime(&ctTime));
00028 
00029 }
00030 //------------ Connect to Server -----------------------//
00031 Websocket ws("ws://131.202.94.104:4040");               // establish websocket connection to this server
00032 
00033 int main() {
00034     void* p1=malloc(16000); // to ensure we have enough heap after NTP setup
00035     while (1) {                                         // Infinite loop
00036 
00037         if (!ws.connect()) {                            // loop here while the connection is not established
00038             pc.printf("Connection Failed - restart mbed\r\n");
00039         }
00040         // pc.printf("Successfully Connected!\r\n");    // Confirm connection
00041         tmr.start();                                    // start the Timer tmr
00042 
00043 //----------------- DATA COLLECTION --------------------//
00044 // Data collected from the arduino/DSP
00045 // this is just dummy data, eventualy there will be function calls here that
00046 // will fetch the data from the DSP/arduino
00047         char b[8] = {'V','1','2','0','.','0','0',';'};
00048         char r[8] = {'I','0','2','0','.','0','0',';'};
00049         char a[8] = {'P','2','4','0','0','.','0',';'};
00050         char n[8] = {'p','0','0','0','0','0','0',';'};
00051         char d[8] = {'v','0','0','0','4','8','6',';'};
00052         char o[8] = {'I','1','2','0','.','0','0',';'};
00053         int w[1] = {'w'};
00054 
00055         // getDataV();                                  // This will be the method call that updates the data from the DSP
00056 
00057 // Pointers to measurements
00058         char V[9] = {*b,*(b+1),*(b+2),*(b+3),*(b+4),*(b+5),*(b+6),*(b+7),'\0'};
00059         char * ptr_V;
00060         ptr_V = V;
00061         char I[9] = {*r,*(r+1),*(r+2),*(r+3),*(r+4),*(r+5),*(r+6),*(r+7),'\0'};
00062         char * ptr_I;
00063         ptr_I = I;
00064         char P[9] = {*a,*(a+1),*(a+2),*(a+3),*(a+4),*(a+5),*(a+6),*(a+7),'\0'};
00065         char * ptr_P;
00066         ptr_P = P;
00067         char p[9] = {*n,*(n+1),*(n+2),*(n+3),*(n+4),*(n+5),*(n+6),*(n+7),'\0'};
00068         char * ptr_p;
00069         ptr_p = p;
00070         char v[9] = {*d,*(d+1),*(d+2),*(d+3),*(d+4),*(d+5),*(d+6),*(d+7),'\0'};
00071         char * ptr_v;
00072         ptr_v = v;
00073         char i[9] = {*o,*(o+1),*(o+2),*(o+3),*(o+4),*(o+5),*(o+6),*(o+7),'\0'};
00074         char * ptr_i;
00075         ptr_i = i;
00076         // char W[2] = {*w,'\0'};
00077         // char * ptr_W;
00078         // ptr_W = W;       // Test value in the format of scotts data
00079 
00080 // Pointer to all the measurements
00081         char data[49] = {*b,*(b+1),*(b+2),*(b+3),*(b+4),*(b+5),*(b+6),*(b+7),
00082                          *r,*(r+1),*(r+2),*(r+3),*(r+4),*(r+5),*(r+6),*(r+7),
00083                          *a,*(a+1),*(a+2),*(a+3),*(a+4),*(a+5),*(a+6),*(a+7),
00084                          *n,*(n+1),*(n+2),*(n+3),*(n+4),*(n+5),*(n+6),*(n+7),
00085                          *d,*(d+1),*(d+2),*(d+3),*(d+4),*(d+5),*(d+6),*(d+7),
00086                          *o,*(o+1),*(o+2),*(o+3),*(o+4),*(o+5),*(o+6),*(o+7),'\0'
00087                         };
00088         char * ptr_data;                                // Declare a new Pointer
00089         ptr_data = data;                                // make the new pointer point to the data
00090 
00091 
00092 //------------------ DATA PROCESSING -------------------//
00093 // Data appended with a timestamp, formatted, and sent to the server
00094 
00095         // ws.send("\r\n");
00096         while (1) {
00097 
00098             if (tmr.read() > 3) {                       // Wait 3 seconds -> Send Data Every 3 seconds
00099 
00100                 free(p1); // reclaim back memory
00101 
00102                 char *buf=new char[32];
00103 
00104                 Time *time=new Time();
00105                 TimeStamp *timestamp=time->getTime();
00106 
00107                 snprintf(buf,32,"%.2d;%.2d;%.2d;%.2d;%.2d;%.2d",
00108                          timestamp->getYear (),timestamp->getMonth ()+1, timestamp->getDay (),
00109                          timestamp->getHour ()-3, timestamp->getMinute (), timestamp->getSecond ());
00110                 // printf("time=%s\r\n",buf);
00111 
00112 
00113                 // pc.printf(ptr_data);
00114                 // pc.printf("\r\n");
00115                 ws.send("\r\n");
00116                 ws.send(ptr_data);
00117                 ws.send(buf);
00118                 // getTime();
00119                 tmr.start();                            // restart the timer
00120             } // end if
00121             Net::poll();
00122         } // end while
00123 
00124 
00125     } // end while
00126 } // end main