IEEE1888 GateWay sample that handles multiple sensors.

Dependencies:   EthernetInterface Fiap HTTPClientForFIAP NTPClient mbed-rtos mbed spxml

Fork of IEEE1888_TEMP_NODE by Satoru Yoshida

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "main.h"
00002 #include "mbed.h"
00003 #include "EthernetInterface.h"
00004 #include "NTPClient.h"
00005 #include "fiap.h"
00006 
00007 EthernetInterface eth;
00008 time_t ctTime;
00009 Serial pc(USBTX, USBRX);
00010 FIAP fiap;
00011 
00012 #ifdef P15_POINT_ID
00013 AnalogIn ain_p15(p15);
00014 #endif
00015 #ifdef P16_POINT_ID
00016 AnalogIn ain_p16(p16);
00017 #endif
00018 #ifdef P17_POINT_ID
00019 AnalogIn ain_p17(p17);
00020 #endif
00021 #ifdef P18_POINT_ID
00022 AnalogIn ain_p18(p18);
00023 #endif
00024 #ifdef P19_POINT_ID
00025 AnalogIn ain_p19(p19);
00026 #endif
00027 #ifdef P20_POINT_ID
00028 AnalogIn ain_p20(p20);
00029 #endif
00030 
00031 DigitalOut led1(LED1);//error while connecting to NTP server or FIAPStorage
00032 DigitalOut led3(LED3);//LAN speed (1 = error)
00033 DigitalOut led4(LED4);//LAN link (1 = error)
00034 
00035 DigitalIn lnk(P1_25);//LAN link
00036 DigitalIn spd(P1_26);//LAN speed
00037 
00038 #if USE_RJ45_LAMP
00039 DigitalOut speed(p29);//LAN speed for RJ45 (1 = OK)
00040 DigitalOut link(p30); //LAN link for RJ45 (1 = OK)
00041 #endif
00042 
00043 int exitTimer;
00044 int i;
00045 int this_year, this_mon;
00046 
00047 //I define longer than actual length.
00048 //because last characters are chopped when I define just length, 64.
00049 #define STORAGE_URL_SIZE 100
00050 char storage_url[STORAGE_URL_SIZE];
00051 struct fiap_element element[]= {
00052     {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL},
00053 };
00054 struct tm t;
00055 
00056 #ifdef P15_POINT_ID
00057 char val_p15[8];
00058 float float_p15_wrk;
00059 float float_p15;
00060 #endif
00061 #ifdef P16_POINT_ID
00062 char val_p16[8];
00063 float float_p16_wrk;
00064 float float_p16;
00065 #endif
00066 #ifdef P17_POINT_ID
00067 char val_p17[8];
00068 float float_p17_wrk;
00069 float float_p17;
00070 #endif
00071 #ifdef P18_POINT_ID
00072 char val_p18[8];
00073 float float_p18_wrk;
00074 float float_p18;
00075 #endif
00076 #ifdef P19_POINT_ID
00077 char val_p19[8];
00078 float float_p19_wrk;
00079 float float_p19;
00080 #endif
00081 #ifdef P20_POINT_ID
00082 char val_p20[8];
00083 float float_p20_wrk;
00084 float float_p20;
00085 #endif
00086 
00087 int syncNTP(void)
00088 {
00089     NTPClient ntp;
00090     if (ntp.setTime(NTP_SERVER) != 0) {
00091         return -1;
00092     }
00093     ctTime = time(NULL);
00094     ctTime+= TIME_ZONE_SECONDS;
00095     set_time(ctTime);
00096     return 0;
00097 }
00098 
00099 int setFIAPStorage(void )
00100 {
00101     LocalFileSystem local("local");
00102     FILE *fp;
00103     fp = fopen("/local/storage.txt", "r");
00104     if(fp == NULL){
00105         return -1;
00106     }
00107     fgets(storage_url, STORAGE_URL_SIZE, fp);
00108     fiap.setStorage(storage_url);
00109     fclose(fp);
00110     
00111     fiap.debug_mode=FIAP_DEBUG_MODE;
00112     return 0;
00113 }
00114 
00115 int sendFIAP(void )
00116 {
00117     ctTime = time(NULL);
00118     t = *localtime(&ctTime);
00119 
00120     #ifdef P15_POINT_ID
00121     float_p15_wrk  = ain_p15;//dummy read
00122     float_p15_wrk  = ain_p15;
00123     float_p15_wrk += ain_p15;
00124     float_p15 = float_p15_wrk/2 * P15_COEFFICIENT + P15_CONSTANT;
00125     #endif
00126     #ifdef P16_POINT_ID
00127     float_p16_wrk  = ain_p16;//dummy read
00128     float_p16_wrk  = ain_p16;
00129     float_p16_wrk += ain_p16;
00130     float_p16 = float_p16_wrk/2 * P16_COEFFICIENT + P16_CONSTANT;
00131     #endif
00132     #ifdef P17_POINT_ID
00133     float_p17_wrk  = ain_p17;//dummy read
00134     float_p17_wrk  = ain_p17;
00135     float_p17_wrk += ain_p17;
00136     float_p17 = float_p17_wrk/2 * P17_COEFFICIENT + P17_CONSTANT;
00137     #endif
00138     #ifdef P18_POINT_ID
00139     float_p18_wrk  = ain_p18;//dummy read
00140     float_p18_wrk  = ain_p18;
00141     float_p18_wrk += ain_p18;
00142     float_p18 = float_p18_wrk/2 * P18_COEFFICIENT + P18_CONSTANT;
00143     #endif
00144     #ifdef P19_POINT_ID
00145     float_p19_wrk  = ain_p19;//dummy read
00146     float_p19_wrk  = ain_p19;
00147     float_p19_wrk += ain_p19;
00148     float_p19 = float_p19_wrk/2 * P19_COEFFICIENT + P19_CONSTANT;
00149     #endif
00150     #ifdef P20_POINT_ID
00151     float_p20_wrk  = ain_p20;//dummy read
00152     float_p20_wrk  = ain_p20;
00153     float_p20_wrk += ain_p20;
00154     float_p20 = float_p20_wrk/2 * P20_COEFFICIENT + P20_CONSTANT;
00155     #endif
00156 
00157     #ifdef P15_POINT_ID
00158     sprintf(val_p15, P15_PREC, float_p15);
00159     #endif
00160     #ifdef P16_POINT_ID
00161     sprintf(val_p16, P16_PREC, float_p16);
00162     #endif
00163     #ifdef P17_POINT_ID
00164     sprintf(val_p17, P17_PREC, float_p17);
00165     #endif
00166     #ifdef P18_POINT_ID
00167     sprintf(val_p18, P18_PREC, float_p18);
00168     #endif
00169     #ifdef P19_POINT_ID
00170     sprintf(val_p19, P19_PREC, float_p19);
00171     #endif
00172     #ifdef P20_POINT_ID
00173     sprintf(val_p20, P20_PREC, float_p20);
00174     #endif
00175 
00176     this_year = t.tm_year + 1900;
00177     this_mon  = t.tm_mon  + 1;
00178 
00179     #ifdef P15_POINT_ID
00180     struct fiap_element P15_element = {
00181         P15_POINT_ID, val_p15, this_year, this_mon, t.tm_mday,
00182            t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR};
00183     #endif
00184     #ifdef P16_POINT_ID
00185     struct fiap_element P16_element = {
00186         P16_POINT_ID, val_p16, this_year, this_mon, t.tm_mday,
00187            t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR};
00188     #endif
00189     #ifdef P17_POINT_ID
00190     struct fiap_element P17_element = {
00191         P17_POINT_ID, val_p17, this_year, this_mon, t.tm_mday,
00192            t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR};
00193     #endif
00194     #ifdef P18_POINT_ID
00195     struct fiap_element P18_element = {
00196         P18_POINT_ID, val_p18, this_year, this_mon, t.tm_mday,
00197            t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR};
00198     #endif
00199     #ifdef P19_POINT_ID
00200     struct fiap_element P19_element = {
00201         P19_POINT_ID, val_p19, this_year, this_mon, t.tm_mday,
00202            t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR};
00203     #endif
00204     #ifdef P20_POINT_ID
00205     struct fiap_element P20_element = {
00206         P20_POINT_ID, val_p20, this_year, this_mon, t.tm_mday,
00207            t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR};
00208     #endif
00209 
00210     i = 0;
00211     #ifdef P15_POINT_ID
00212     element[i++] = P15_element;
00213     #endif
00214     #ifdef P16_POINT_ID
00215     element[i++] = P16_element;
00216     #endif
00217     #ifdef P17_POINT_ID
00218     element[i++] = P17_element;
00219     #endif
00220     #ifdef P18_POINT_ID
00221     element[i++] = P18_element;
00222     #endif
00223     #ifdef P19_POINT_ID
00224     element[i++] = P19_element;
00225     #endif
00226     #ifdef P20_POINT_ID
00227     element[i++] = P20_element;
00228     #endif
00229     return fiap.post(element, i);
00230 }
00231 
00232 #if FIAP_DEBUG_MODE
00233 #else
00234 /* stop USB Interface */
00235 #define USR_POWERDOWN (0x104)
00236 int semihost_powerdown() {
00237     uint32_t arg;
00238     return __semihost(USR_POWERDOWN, &arg);
00239 }
00240 #endif
00241 
00242 /** Frequency Control
00243  * @see ClockControl/ClockControl.cpp by Michael Wei
00244  */
00245 void setPLL0Frequency(unsigned char clkSrc, unsigned short M, unsigned char N)
00246 {
00247       LPC_SC->CLKSRCSEL = clkSrc;
00248       LPC_SC->PLL0CFG   = (((unsigned int)N-1) << 16) | M-1;
00249       LPC_SC->PLL0CON   = 0x01;             
00250       LPC_SC->PLL0FEED  = 0xAA;
00251       LPC_SC->PLL0FEED  = 0x55;
00252       while (!(LPC_SC->PLL0STAT & (1<<26)));
00253     
00254       LPC_SC->PLL0CON   = 0x03;
00255       LPC_SC->PLL0FEED  = 0xAA;
00256       LPC_SC->PLL0FEED  = 0x55;
00257 }
00258 
00259 unsigned int setSystemFrequency(unsigned char clkDivider, unsigned char clkSrc, unsigned short M, unsigned char N)
00260 {
00261     setPLL0Frequency(clkSrc, M, N);
00262     LPC_SC->CCLKCFG = clkDivider - 1;
00263     SystemCoreClockUpdate();
00264     return SystemCoreClock;
00265 }
00266 
00267 int main()
00268 {
00269     setSystemFrequency(0x3, 0x1, 6, 1);//clock down from 96MHz to 48MHz
00270     led1 = led3 = led4 = 0;
00271 
00272     #if USE_RJ45_LAMP
00273     speed = link = 0;
00274     #endif
00275 
00276     pc.baud(USB_PAUD_RATE);
00277 
00278     eth.init(); //Use DHCP
00279     eth.connect();
00280     printf("IP: %s\r\n", eth.getIPAddress());
00281 
00282     led1 = 1;
00283     if(syncNTP() != 0) {
00284         printf("NTP Sync Error\r\n");
00285         return -1;
00286     }
00287     ctTime = time(NULL);
00288     printf("Time: %s\r\n", ctime(&ctTime));
00289     
00290     if(setFIAPStorage() != 0) {
00291         printf("Check if storage.txt exists in mbed memory.\r\n");
00292         return -1;
00293     }
00294     led1 = 0;
00295 
00296     #if FIAP_DEBUG_MODE
00297     #else
00298         semihost_powerdown();//Stop also USB Interface
00299     #endif
00300 
00301     exitTimer = RETRY_TIMES_ON_ERROR;
00302     while(exitTimer > 0) {
00303         led3 = spd;
00304         led4 = lnk;
00305 
00306         #if USE_RJ45_LAMP
00307         speed = !spd;
00308         link = !lnk;
00309         #endif
00310         
00311         if (led4 == 1 || led3 == 1) {
00312             exitTimer--;
00313             wait(WAIT_NEXT_SEND);
00314             continue;
00315         }
00316         
00317         if(sendFIAP() != 0){
00318             led1 = 1;
00319             exitTimer--;
00320         } else {
00321             led1 = 0;
00322             exitTimer = RETRY_TIMES_ON_ERROR;//initialize
00323         }
00324         wait(WAIT_NEXT_SEND);
00325     }
00326     eth.disconnect();
00327     #if FIAP_DEBUG_MODE
00328     printf("Disconnected because of error\r\n");
00329     #endif
00330 
00331     return -1;
00332 }