//========================================================= // TCP client Report data with power saving function // for G-4511 // // collect all IO information, and send to the TCP server // 2013/05/16 //========================================================= #include #include #include #include #include #include "../lib/GSM.h" #include "../lib/G4500.h" #include "../lib/OS7_COM.h" #include "../lib/MCU2LIB.h" int X305IO_AnalogIn_2(int iChannel); void printPacket(GPRSData gprsData) { int i; static int no = 0; no++; Print("----new GPRS Data (%d)----\r\n", no); Print("IP:Port = %s:%d, from link = %d\r\n", gprsData.IP, gprsData.port, gprsData.link); Print("Data length = %d\r\n", gprsData.dataLen); Print("Data = \r\n"); for(i=0; i>8, libVer&0xff, libDate); //-- print GSM lib. version GM_SYS_GetLibDate(libDate); libVer = GM_SYS_GetLibVersion(); Print("GSM LIB vesion %X.%02X, Date:%s\r\n", libVer>>8, libVer&0xff, libDate); //-- print MCU2 lib. version MCU2_getLibVer(libDate); Print("MCU2 LIB vesion : %s\r\n", libDate); //-- (0-2)initial MCU2 result = MCU2_init(); if(result!=0) { Print("MCU2 initial fail\r\n"); return 1; }else { MCU2_getFWVer(libDate); Print("MCU2 FW version: %s\r\n", libDate); } //-- Enable Battery Protecting function: // 11.1V->close system power, 12.6V->system power recovered MCU2_BatteryProtect(1); //-- (0-3)init modem strcpy(sysProfile.PINCode, "0000"); //The pin code of SIM card, ex: "0000" sysProfile.modemPort = 4; //modem port number. G-4500 = 4, uP-5000 = 11 sysProfile.hardware = 0; //hardware type. 1: G-4500, 2: uPAC-5000, 3: iP-8000, 0: Other GM_SYS_SetPowerFunction(powerFunction);//set power-control function if( (result = GM_SYS_InitModem(sysProfile)) != GM_NOERROR) { Print("init_modem fail!! return value is %d\r\n", result); return 1; } { Print("-->Registing."); tmNowStatus = GetTimeTicks(); while (1) { //- update time GetTime(&hh, &mm, &ss); GetDate(&YY, &MM, &DD); // press "ESC" to exit if(Kbhit()) { tmp = Getch(); if( tmp == 27 || tmp == 'q') break; } //-- (1)set GPRS configure after register. if(GM_SYS_CheckModemStatus()==GM_NOERROR && isSetLink==0) { Print("\r\nstart to set Network configure\r\n"); // set Network profile strcpy(netProfile.APN, "INTERNET"); //APN for network provided by your cellular provider strcpy(netProfile.pw, ""); //username for network provided by your cellular provider strcpy(netProfile.user, ""); //password for network provided by your cellular provider //The most basic task of DNS is to translate hostnames such as www.icpdas.com to IP address such as 96.9.41.131. strcpy(netProfile.DnsServerIP, ""); //empty string = system default value GM_NET_SetNet(netProfile); //--(1-1) install link[0], GM_NET_InstallLink(0, 0, serverIP, serverPort) for UDP GM_NET_InstallLink(0, 1, "www.google.com", 80); {MCU2_setDo0(1); Print("set Do0 ON\r\n");} Print("-->linking."); isSetLink=1; } //-- diaplay some information per 5 seconds when not collected data if(GetTimeTicks()-tmNowStatus > 5000 && isSend==0) { tmNowStatus = GetTimeTicks(); Print("."); } //-- if Reporting timeout --> go to sleep (timeout is 3 minute) if( (mm%reportMinute)>=3 && isSend==0) { Print("\r\n==== Fail to report or time out ====\r\n\r\n"); sleepTime = (reportMinute-mm%reportMinute)*60 - ss; Print("go to sleep. (%d s)\r\n", sleepTime); displayTime(); if(MCU2_DeepSleep(sleepTime)!=MCU2_NOERROR) Print("-->sleep fail!!\r\n"); } //-- (2) collect data for report. usually add the codes you need here. // GPRS link to Server need 30~120 seconds if(isSend==0 && GM_NET_GetLinkStatus(0)==1) { Print("\r\n-->Collect data for reporting\r\n"); //-- (2-1) print Signal quality, and register status iSignal = GM_SYS_CheckSignal(); iRegister = GM_SYS_CheckReg(); //---- collect MCU2 information //-- (2-2) get DIO status dioStatus = MCU2_getDioStatus(); //-- (2-3) get Voltage VBattery = MCU2_getVBattery(); //-- (2-4) get other IO data iDOValue=X305IO_Read_All_DI(); for(i=0; i<8; i++) fValue[i]=X305IO_AnalogIn_HexToFloat(X305IO_AnalogIn_2(i)); //-- (2-5) local side logic control. // Notice: the DO don't continue to output in Sleeping Mode except Relay if(fValue[0]>5) X305IO_Write_One_DO(0, 1); else X305IO_Write_One_DO(0, 0); //-- (2-6)prepare data for sending via GPRS sprintf(report_data, "[%d/%02d/%02d %02d:%02d] Signal=%d, Register=%d" ", U2_DI=%d, U2_DO=%d, VB=%.2f" ", DI=%X, AI[0]=%.3f, AI[1]=%.3f, AI[2]=%.3f, AI[3]=%.3f" ", AI[4]=%.3f, AI[5]=%.3f, AI[6]=%.3f, AI[7]=%.3f\r\n", YY, MM, DD, hh, mm, iSignal, iRegister, dioStatus&1, (dioStatus>>1)&1, VBattery, iDOValue, fValue[0], fValue[1], fValue[2], fValue[3], fValue[4], fValue[5], fValue[6], fValue[7]); Print("%s -->Data_Len=%d\r\n", report_data, strlen(report_data)); isSend = 1; } //--(3) check that could the modem service, if it can't, skip operating the modem below if(GM_SYS_CheckModemStatus() != GM_NOERROR) continue; if(GM_NET_GetLinkStatus(0)!=1) continue; else GM_NET_GetIP(myIP); if(isSend) { //--(4) send the data to server, and when LinkStatus[1]=1 switch((netSendStatus=GM_SYS_CheckCmdStatus())) { case GM_READY: Print("sending package..., myIP = %s\r\n", myIP); gprsData.link = 0; strcpy(gprsData.data, report_data); gprsData.dataLen = strlen(gprsData.data); if(GM_NET_Send(gprsData.link, gprsData.data, gprsData.dataLen)!=GM_NOERROR) Print("can't send package[%8ld]\r\n"); break; case GM_NOERROR: Print("send success!!\r\n"); //-- (4-1)break link before sleep DelayMs(2000); GM_NET_CloseLink(0);//== Close client link[n], 3G:0~9, 2G:0 DelayMs(2000); {MCU2_setDo0(0); Print("set Do0 OFF\r\n");} //-- (4-2) sleep GetTime(&hh, &mm, &ss); sleepTime = (reportMinute-mm%reportMinute)*60 -ss; Print("go to sleep: %d sec.\r\n", sleepTime); displayTime(); Print("==== Finish report ====\r\n\r\n"); if(MCU2_DeepSleep(sleepTime)!=MCU2_NOERROR) Print("-->sleep fail!!\r\n"); DelayMs(1000); break; case GM_BUSY: //sending, and waiting reply break; default: Print("send error, and re-send again, error code=%d\r\n", netSendStatus); break; } } //--(5) if any new data packet come in, print it // you can write code here to do something // when device receives any command from the server if(GM_NET_GetNewPacket(&gprsData) != NULL) { Print("\n== new data packet come in\r\n"); printPacket(gprsData); } } Print("--Close Link, and Close Net\r\n"); GM_NET_CloseLink(0);//== Close client link[n], 3G:0~9, 2G:0 DelayMs(1000); GM_NET_CloseNet(); DelayMs(1000); } //must close before program ending to release you resource GM_SYS_CloseModem(0); //== Close the modem, 0:not turn off modem, 1:turn off modem Print("Exit Program!!\r\n"); return 0; } int iAINum; int X305IO_AnalogIn_2(int iChannel) { X305IO_AnalogIn_SetChannel(iChannel); if(iAINum!=iChannel) Delay(4); iAINum=iChannel; return X305IO_AnalogIn_Hex(); }