tcp communication
Dependencies: SDFileSystem mbed-dsp mbed
main.cpp@0:307f417b029a, 2015-07-15 (annotated)
- Committer:
- anupanchsheel
- Date:
- Wed Jul 15 07:09:03 2015 +0000
- Revision:
- 0:307f417b029a
tcp send
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
anupanchsheel | 0:307f417b029a | 1 | /* |
anupanchsheel | 0:307f417b029a | 2 | Designed to work from year 2015 to 2020 |
anupanchsheel | 0:307f417b029a | 3 | */ |
anupanchsheel | 0:307f417b029a | 4 | |
anupanchsheel | 0:307f417b029a | 5 | #include "mbed.h" |
anupanchsheel | 0:307f417b029a | 6 | #include "arm_math.h" |
anupanchsheel | 0:307f417b029a | 7 | #include "SDFileSystem.h" |
anupanchsheel | 0:307f417b029a | 8 | #include "arm_common_tables.h" |
anupanchsheel | 0:307f417b029a | 9 | #define freq 1 |
anupanchsheel | 0:307f417b029a | 10 | #define fft_points 16384 |
anupanchsheel | 0:307f417b029a | 11 | #define dur_sec 16.384 |
anupanchsheel | 0:307f417b029a | 12 | #define module_name "BHJ3" //enter module name here |
anupanchsheel | 0:307f417b029a | 13 | |
anupanchsheel | 0:307f417b029a | 14 | int old_hour = 100; |
anupanchsheel | 0:307f417b029a | 15 | Serial pc(USBTX, USBRX); |
anupanchsheel | 0:307f417b029a | 16 | Serial gsm(D1, D0); |
anupanchsheel | 0:307f417b029a | 17 | AnalogIn AC_input(A0); |
anupanchsheel | 0:307f417b029a | 18 | AnalogIn AC2_input(A1); |
anupanchsheel | 0:307f417b029a | 19 | AnalogIn DC_input(A2); |
anupanchsheel | 0:307f417b029a | 20 | AnalogIn DC_Detect(A3); |
anupanchsheel | 0:307f417b029a | 21 | AnalogIn TC1(A4); |
anupanchsheel | 0:307f417b029a | 22 | AnalogIn TC2(A5); |
anupanchsheel | 0:307f417b029a | 23 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); |
anupanchsheel | 0:307f417b029a | 24 | DigitalOut gsm_rst(D7); |
anupanchsheel | 0:307f417b029a | 25 | |
anupanchsheel | 0:307f417b029a | 26 | Timer trmsn ; // trmsn is for the transmission |
anupanchsheel | 0:307f417b029a | 27 | |
anupanchsheel | 0:307f417b029a | 28 | bool conn = 0; // it determines that server is connected or not |
anupanchsheel | 0:307f417b029a | 29 | char sitename[10] =module_name; |
anupanchsheel | 0:307f417b029a | 30 | char filename[100] = ""; |
anupanchsheel | 0:307f417b029a | 31 | char filepath[100] = ""; |
anupanchsheel | 0:307f417b029a | 32 | char timestamp[600]=""; |
anupanchsheel | 0:307f417b029a | 33 | char csq[50]=""; |
anupanchsheel | 0:307f417b029a | 34 | char csq_value[2]=""; |
anupanchsheel | 0:307f417b029a | 35 | char time_stamp[20]=""; |
anupanchsheel | 0:307f417b029a | 36 | char timestampold[100]=""; |
anupanchsheel | 0:307f417b029a | 37 | char main_dir[100] =""; |
anupanchsheel | 0:307f417b029a | 38 | char hour_dir[100] =""; |
anupanchsheel | 0:307f417b029a | 39 | char pathAC[100] =""; |
anupanchsheel | 0:307f417b029a | 40 | char pathDC[100] =""; |
anupanchsheel | 0:307f417b029a | 41 | //char pathDCdetect[100] =""; |
anupanchsheel | 0:307f417b029a | 42 | char pathfft[100]=""; |
anupanchsheel | 0:307f417b029a | 43 | char unsent_mf[5][100]; //mf -> minute files |
anupanchsheel | 0:307f417b029a | 44 | // array are initalized with the null values |
anupanchsheel | 0:307f417b029a | 45 | int readindex_mf = 0; |
anupanchsheel | 0:307f417b029a | 46 | |
anupanchsheel | 0:307f417b029a | 47 | |
anupanchsheel | 0:307f417b029a | 48 | char server_response[1400]; |
anupanchsheel | 0:307f417b029a | 49 | //char putpathname[100]=""; |
anupanchsheel | 0:307f417b029a | 50 | char filenameAC[100] =""; |
anupanchsheel | 0:307f417b029a | 51 | char filenameDC[100] =""; |
anupanchsheel | 0:307f417b029a | 52 | char filenamefft[100] =""; |
anupanchsheel | 0:307f417b029a | 53 | char clk_response[100] =""; |
anupanchsheel | 0:307f417b029a | 54 | //char ftpget[100]=""; // For FTPGET Function |
anupanchsheel | 0:307f417b029a | 55 | //char ftpget1[100]=""; // For FTPGET Function |
anupanchsheel | 0:307f417b029a | 56 | //char ftpget2[100]=""; // For FTPGET Function |
anupanchsheel | 0:307f417b029a | 57 | char mod_reset[50]= "NVIC_SystemReset();"; // For FTPGET Function |
anupanchsheel | 0:307f417b029a | 58 | //char incoming_data[150]=""; // For FTPGET Function |
anupanchsheel | 0:307f417b029a | 59 | //char ftpputpathname[100]=""; |
anupanchsheel | 0:307f417b029a | 60 | //char ftpputpathname_hour[100]=""; // For FTPPUTHPATH NAME |
anupanchsheel | 0:307f417b029a | 61 | char timestampname_old[30]=""; // For sending past hour info file |
anupanchsheel | 0:307f417b029a | 62 | char timestamppath_old[30]=""; |
anupanchsheel | 0:307f417b029a | 63 | //char ftpputpathname_hour_old[30]=""; // For sending past hour info file |
anupanchsheel | 0:307f417b029a | 64 | char timestampname[30]=""; |
anupanchsheel | 0:307f417b029a | 65 | char timestamppath[30]=""; |
anupanchsheel | 0:307f417b029a | 66 | char file_data[1294]; |
anupanchsheel | 0:307f417b029a | 67 | char location_status[400]; |
anupanchsheel | 0:307f417b029a | 68 | int maxfreq = 0; // For Timestamp Function |
anupanchsheel | 0:307f417b029a | 69 | |
anupanchsheel | 0:307f417b029a | 70 | int m=0,mfinal; |
anupanchsheel | 0:307f417b029a | 71 | int date, month, year, hour, minute, second,minuteold, secondold, temp = 0; |
anupanchsheel | 0:307f417b029a | 72 | unsigned long var = 0; |
anupanchsheel | 0:307f417b029a | 73 | unsigned long currentMillis = 0; |
anupanchsheel | 0:307f417b029a | 74 | int gsmerr=0; |
anupanchsheel | 0:307f417b029a | 75 | |
anupanchsheel | 0:307f417b029a | 76 | time_t previous; |
anupanchsheel | 0:307f417b029a | 77 | |
anupanchsheel | 0:307f417b029a | 78 | int fft_array[32768];//={37767, 34339, 31062, 28224, 26011, 24245, 22588, 21757, 22460, 24288, 27136, 30535, 33829, 36639, 38848, 40598, 42239, 43047, 42302, 40468, 37592, 34182, 30924, 28082, 25922, 24153, 22509, 21739, 22534, 24403, 27302, 30689, 33965, 36774, 38946, 40688, 42324, 43064, 42222, 40351, 37424, 34013, 30787, 27940, 25834, 24067, 22432, 21742, 22605, 24522, 27466, 30863, 34102, 36908, 39018, 40773, 42396, 43057, 42166, 40210, 37256, 33851, 30656, 27800, 25746, 23996, 22353, 21752, 22675, 24648, 27636, 31024, 34230, 37052, 39113, 40859, 42468, 43052, 42090, 40095, 37085, 33682, 30524, 27675, 25656, 23904, 22285, 21744, 22739, 24772, 27811, 31199, 34377, 37176, 39206, 40933, 42546, 43037, 42017, 39960, 36916, 33518, 30383, 27553, 25570, 23822, 22218, 21765, 22825, 24910, 27970, 31355, 34516, 37293, 39292, 41021, 42615, 43033, 41936, 39822, 36733, 33359, 30240, 27444, 25487, 23731, 22146, 21769, 22897, 25052, 28145, 31520, 34655, 37403, 39378, 41105, 42682, 43039, 41858, 39687, 36566, 33192, 30094, 27327, 25399, 23655, 22073, 21777, 22989, 25194, 28323, 31692, 34805, 37512, 39456, 41191, 42744, 43019, 41768, 39560, 36389, 33026, 29941, 27223, 25317, 23580, 22016, 21803, 23077, 25320, 28498, 31841, 34950, 37621, 39537, 41268, 42797, 42995, 41686, 39427, 36223, 32858, 29798, 27112, 25246, 23496, 21971, 21828, 23160, 25454, 28663, 32023, 35091, 37721, 39618, 41345, 42840, 42979, 41595, 39295, 36042, 32717, 29648, 27010, 25158, 23417, 21929, 21846, 23241, 25591, 28832, 32141, 35248, 37826, 39716, 41415, 42890, 42949, 41508, 39157, 35874, 32582, 29495, 26901, 25076, 23346, 21884, 21888, 23343, 25723, 28993, 32307, 35390, 37934, 39790, 41488, 42925, 42903, 41406, 39030, 35723, 32404, 29355, 26803, 24986, 23272, 21852, 21935, 23436, 25863, 29158, 32473, 35530, 38037, 39868, 41566, 42958, 42852, 41319, 38884, 35553, 32248, 29201, 26701, 24900, 23188, 21834, 21983, 23515, 26001, 29320, 32636, 35672, 38141, 39962, 41655, 42972, 42810, 41229, 38759, 35378, 32087, 29059, 26601, 24811, 23105, 21809, 22027, 23618, 26136, 29491, 32806, 35820, 38239, 40047, 41727, 42999, 42757, 41130, 38608, 35221, 31912, 28911, 26501, 24729, 23032, 21794, 22089, 23713, 26298, 29641, 32995, 35956, 38344, 40128, 41805, 43016, 42688, 41028, 38463, 35065, 31730, 28779, 26413, 24643, 22931, 21778, 22154, 23809, 26440, 29802, 33164, 36082, 38442, 40227, 41899, 43025, 42627, 40930, 38304, 34903, 31562, 28651, 26320, 24546, 22839, 21770, 22224, 23914, 26596, 29971, 33327, 36205, 38539, 40315, 41985, 43031, 42559, 40833, 38153, 34719, 31410, 28515, 26226, 24450, 22763, 21755, 22291, 24021, 26764, 30153, 33492, 36334, 38624, 40409, 42068, 43033, 42473, 40722, 37981, 34554, 31252, 28403, 26130, 24361, 22680, 21756, 22360, 24149, 26928, 30318, 33645, 36465, 38732, 40493, 42148, 43048, 42416, 40609, 37804, 34390, 31095, 28260, 26034, 24272, 22605, 21748, 22438, 24252, 27087, 30487, 33780, 36596, 38826, 40578, 42212, 43049, 42342, 40491, 37644, 34223, 30958, 28119, 25949, 24193, 22538, 21743, 22516, 24371, 27257, 30645, 33927, 36742, 38915, 40661, 42298, 43058, 42254, 40377, 37472, 34056, 30822, 27973, 25853, 24095, 22461, 21736, 22578, 24485, 27427, 30817, 34064, 36885, 39007, 40754, 42379, 43062, 42186, 40250, 37306, 33892, 30686, 27834, 25766, 24012, 22381, 21740, 22661, 24613, 27601, 30971, 34198, 37003, 39098, 40823, 42451, 43054, 42107, 40122, 37132, 33733, 30548, 27703, 25675, 23923, 22298, 21749, 22723, 24738, 27767, 31154, 34344, 37149, 39177, 40920, 42527, 43053, 42035, 39999, 36967, 33570, 30405, 27579, 25589, 23843, 22226, 21750, 22802, 24872, 27942, 31312, 34487, 37275, 39276, 41002, 42602, 43044, 41964, 39853, 36775, 33388, 30267, 27468, 25508, 23758, 22161, 21760, 22876, 25017, 28132, 31492, 34625, 37377, 39353, 41087, 42673, 43035, 41879, 39724, 36611, 33232, 30123, 27356, 25420, 23671, 22082, 21769, 22960, 25153, 28291, 31655, 34762, 37497, 39436, 41171, 42730, 43030, 41794, 39586, 36427, 33073, 29978, 27247, 25331, 23593, 22025, 21781, 23052, 25286, 28463, 31814, 34918, 37592, 39524, 41247, 42785, 43012, 41708, 39453, 36258, 32912, 29822, 27138, 25249, 23517, 21973, 21799, 23135, 25424, 28629, 31970, 35073, 37709, 39615, 41330, 42836, 42995, 41620, 39315, 36077, 32745, 29669, 27034, 25191, 23437, 21925, 21830, 23222, 25563, 28797, 32137, 35208, 37821, 39688, 41407, 42877, 42958, 41574, 39183, 35908, 32593, 29529, 26921, 25084, 23355, 21888, 21872, 23324, 25701, 28966, 32285, 35359, 37917, 39785, 41486, 42912, 42925, 41440, 39041, 35744, 32444, 29380, 26820, 24997, 23274, 21860, 21910, 23410, 25839, 29133, 32438, 35505, 38025, 39853, 41550, 42945, 42870, 41341, 38904, 35579, 32292, 29225, 26720, 24921, 23208, 21831, 21969, 23510, 25982, 29290, 32604, 35645, 38122, 39944, 41627, 42969, 42826, 41248, 38768, 35416, 32127, 29075, 26612, 24827, 23121, 21808, 22020, 23602, 26115, 29461, 32775, 35796, 38225, 40030, 41719, 42997, 42763, 41142, 38629, 35248, 31935, 28938, 26524, 24743, 23039, 21794, 22084, 23698, 26271, 29616, 32967, 35921, 38322, 40109, 41798, 43001, 42705, 41053, 38484, 35095, 31768, 28794, 26428, 24644, 22954, 21781, 22136, 23800, 26431, 29785, 33142, 36060, 38435, 40209, 41877, 43022, 42645, 40950, 38308, 34923, 31600, 28677, 26330, 24564, 22862, 21770, 22201, 23898, 26577, 29947, 33302, 36191, 38527, 40297, 41974, 43032, 42572, 40839, 38160, 34763, 31432, 28541, 26241, 24472, 22770, 21758, 22280, 24004, 26741, 30117, 33465, 36324, 38619, 40393, 42062, 43043, 42494, 40738, 37996, 34606, 31268, 28406, 26148, 24377, 22685, 21747, 22359, 24116, 26903, 30292, 33626, 36453, 38702, 40486, 42145, 43042, 42427, 40636, 37828, 34424, 31128, 28272, 26056, 24292, 22617, 21749, 22425, 24226, 27060, 30455, 33772, 36579, 38811, 40569, 42216, 43047, 42343, 40526, 37652, 34251, 30980, 28134, 25962, 24203, 22538, 21748, 22497, 24334, 27233, 30636, 33906, 36731, 38904, 40654, 42294, 43050, 42264, 40407, 37493, 34083, 30850, 27979, 25871, 24104, 22462, 21741, 22562, 24463, 27400, 30800, 34050, 36873, 38991, 40745, 42376, 43057, 42203, 40274, 37323, 33913, 30717, 27836, 25778, 24018, 22392, 21740, 22638, 24593, 27572, 30958, 34182, 36999, 39082, 40827, 42444, 43052, 42130, 40147, 37185, 33751, 30579, 27712, 25689, 23936, 22315, 21733, 22711, 24726, 27744, 31128, 34319, 37133, 39174, 40909, 42518, 43057, 42048, 40013, 36980, 33586, 30433, 27596, 25607, 23852, 22242, 21745, 22786, 24858, 27913, 31304, 34444, 37266, 39251, 40995, 42605, 43048, 41967, 39876, 36817, 33397, 30313, 27467, 25520, 23781, 22152, 21755, 22872, 24990, 28082, 31479, 34590, 37369, 39344, 41070, 42676, 43042, 41893, 39750, 36644, 33236, 30159, 27364, 25434, 23698, 22082, 21773, 22947, 25129, 28256, 31645, 34733, 37483, 39438, 41138, 42741, 43021, 41815, 39606, 36470, 33074, 30011, 27247, 25338, 23619, 22018, 21788, 23038, 25281, 28427, 31804, 34883, 37589, 39528, 41222, 42799, 42991, 41711, 39466, 36293, 32914, 29851, 27159, 25256, 23543, 21963, 21813, 23117, 25421, 28599, 31966, 35030, 37699, 39604, 41310, 42845, 42987, 41633, 39326, 36117, 32761, 29696, 27048, 25178, 23459, 21913, 21853, 23210, 25559, 28764, 32121, 35190, 37790, 39668, 41375, 42891, 42946, 41534, 39196, 35949, 32611, 29544, 26944, 25086, 23384, 21880, 21891, 23304, 25696, 28940, 32274, 35340, 37896, 39778, 41450, 42930, 42891, 41455, 39054, 35780, 32455, 29402, 26845, 25003, 23305, 21850, 21941, 23385, 25829, 29108, 32414, 35493, 37998, 39867, 41538, 42960, 42853, 41372, 38918, 35598, 32301, 29237, 26740, 24905, 23236, 21825, 21996, 23478, 25973, 29276, 32578, 35636, 38101, 39955, 41603, 42981, 42798, 41270, 38769, 35438, 32147, 29090, 26644, 24818, 23154, 21803, 22046, 23577, 26105, 29437, 32753, 35780, 38195, 40046, 41686, 43002, 42748, 41181, 38637, 35270, 31972, 28946, 26540, 24729, 23070, 21784, 22090, 23672, 26263, 29600, 32928, 35923, 38303, 40124, 41763, 43022, 42692, 41074, 38488, 35109, 31792, 28800, 26454, 24644, 22979, 21767, 22160, 23766, 26411, 29762, 33111, 36054, 38392, 40214, 41858, 43034, 42628, 40970, 38337, 34937, 31640, 28662, 26374, 24554, 22888, 21760, 22224, 23871, 26565, 29936, 33262, 36188, 38484, 40306, 41949, 43044, 42549, 40874, 38187, 34765, 31476, 28537, 26273, 24458, 22797, 21755, 22293, 23971, 26718, 30114, 33422, 36324, 38589, 40397, 42037, 43050, 42478, 40765, 38012, 34592, 31319, 28415, 26174, 24365, 22716, 21745, 22364, 24083, 26880, 30288, 33579, 36444, 38683, 40478, 42111, 43051, 42411, 40659, 37853, 34422, 31166, 28287, 26072, 24290, 22645, 21746, 22435, 24200, 27051, 30456, 33727, 36587, 38777, 40564, 42188, 43061, 42332, 40544, 37684, 34241, 31023, 28138, 25979, 24202, 22562, 21736, 22510, 24316, 27218, 30627, 33874, 36717, 38881, 40650, 42271, 43065, 42262, 40418, 37516, 34082, 30883, 27986, 25891, 24115, 22486, 21733, 22586, 24440, 27373, 30778, 34011, 36848, 38970, 40727, 42351, 43061, 42184, 40293, 37352, 33921, 30749, 27849, 25801, 24030, 22397, 21741, 22656, 24573, 27550, 30960, 34144, 36988, 39050, 40808, 42427, 43053, 42104, 40173, 37190, 33759, 30618, 27727, 25713, 23956, 22321, 21745, 22723, 24686, 27700, 31121, 34273, 37127, 39155, 40906, 42515, 43054, 42049, 40037, 37003, 33582, 30481, 27603, 25618, 23870, 22244, 21752, 22795, 24834, 27881, 31283, 34419, 37241, 39239, 40965, 42587, 43046, 41970, 39910, 36842, 33427, 30336, 27486, 25523, 23788, 22167, 21763, 22859, 24982, 28056, 31458, 34565, 37359, 39331, 41051, 42660, 43038, 41897, 39754, 36671, 33251, 30189, 27381, 25450, 23713, 22092, 21772, 22943, 25119, 28232, 31625, 34711, 37462, 39413, 41133, 42725, 43020, 41806, 39631, 36497, 33078, 30038, 27270, 25363, 23622, 22030, 21793, 23027, 25251, 28398, 31795, 34858, 37568, 39494, 41218, 42791, 43005, 41728, 39486, 36319, 32923, 29880, 27167, 25279, 23547, 21975, 21823, 23112, 25387, 28584, 31967, 35015, 37671, 39590, 41304, 42840, 42977, 41646, 39350, 36129, 32761, 29723, 27061, 25181, 23463, 21923, 21847, 23198, 25542, 28753, 32110, 35175, 37780, 39687, 41367, 42888, 42965, 41554, 39192, 35956, 32615, 29574, 26952, 25099, 23395, 21886, 21885, 23295, 25686, 28923, 32259, 35320, 37886, 39766, 41449, 42924, 42905, 41452, 39062, 35787, 32454, 29411, 26846, 25005, 23314, 21849, 21935, 23394, 25824, 29094, 32420, 35480, 37982, 39864, 41529, 42962, 42850, 41378, 38922, 35613, 32315, 29255, 26748, 24908, 23231, 21817, 21979, 23463, 25980, 29265, 32573, 35635, 38092, 39954, 41609, 42983, 42806, 41284, 38788, 35444, 32154, 29102, 26647, 24831, 23158, 21803, 22038, 23562, 26104, 29426, 32740, 35778, 38189, 40035, 41672, 43003, 42758, 41188, 38646, 35286, 31986, 28959, 26548, 24738, 23078, 21785, 22092, 23659, 26251, 29596, 32920, 35918, 38289, 40114, 41760, 43019, 42695, 41085, 38501, 35119, 31809, 28814, 26461, 24658, 22986, 21771, 22143, 23759, 26388, 29759, 33093, 36053, 38386, 40210, 41850, 43032, 42629, 40981, 38356, 34947, 31639, 28684, 26371, 24555, 22896, 21758, 22219, 23855, 26546, 29922, 33252, 36181, 38485, 40299, 41940, 43041, 42539, 40899, 38186, 34776, 31492, 28569, 26280, 24470, 22814, 21750, 22279, 23963, 26709, 30091, 33420, 36319, 38589, 40392, 42029, 43051, 42496, 40762, 38023, 34613, 31330, 28419, 26178, 24374, 22723, 21744, 22345, 24068, 26866, 30262, 33563, 36441, 38680, 40488, 42113, 43056, 42422, 40670, 37860, 34441, 31170, 28291, 26083, 24288, 22650, 21739, 22429, 24180, 27038, 30440, 33720, 36585, 38781, 40566, 42190, 43051, 42349, 4055}; |
anupanchsheel | 0:307f417b029a | 79 | unsigned short ACdata[fft_points]; |
anupanchsheel | 0:307f417b029a | 80 | unsigned short DCdata[fft_points]; |
anupanchsheel | 0:307f417b029a | 81 | unsigned short DC_Detect_data[10]; |
anupanchsheel | 0:307f417b029a | 82 | unsigned short Temp_data[10]; |
anupanchsheel | 0:307f417b029a | 83 | uint32_t dataLength = dur_sec*freq*1000; |
anupanchsheel | 0:307f417b029a | 84 | int avg_dc_detect[2]; |
anupanchsheel | 0:307f417b029a | 85 | int avg_temp[2]; |
anupanchsheel | 0:307f417b029a | 86 | bool sd_status_flag=0; |
anupanchsheel | 0:307f417b029a | 87 | |
anupanchsheel | 0:307f417b029a | 88 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 89 | // Module reset // |
anupanchsheel | 0:307f417b029a | 90 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 91 | void reset_mod() |
anupanchsheel | 0:307f417b029a | 92 | { |
anupanchsheel | 0:307f417b029a | 93 | NVIC_SystemReset(); |
anupanchsheel | 0:307f417b029a | 94 | } |
anupanchsheel | 0:307f417b029a | 95 | |
anupanchsheel | 0:307f417b029a | 96 | int sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout, int clk_flag) //clk_flg 0: normal, clk_flg 1: store response in timestamp, clk_flg 2: ensure timeout, clk_flg 3: store response in CSQ |
anupanchsheel | 0:307f417b029a | 97 | { |
anupanchsheel | 0:307f417b029a | 98 | int grs=0; |
anupanchsheel | 0:307f417b029a | 99 | int answer=0; //x=0 and answer=0, if not put, AT+CCLK?\r is not executed |
anupanchsheel | 0:307f417b029a | 100 | char response[1400]; |
anupanchsheel | 0:307f417b029a | 101 | memset(response, '\0', 1400); // Initialize the string |
anupanchsheel | 0:307f417b029a | 102 | wait_ms(100); |
anupanchsheel | 0:307f417b029a | 103 | previous = time(NULL); |
anupanchsheel | 0:307f417b029a | 104 | char dummy; |
anupanchsheel | 0:307f417b029a | 105 | do |
anupanchsheel | 0:307f417b029a | 106 | { |
anupanchsheel | 0:307f417b029a | 107 | |
anupanchsheel | 0:307f417b029a | 108 | if(gsm.readable()!=0) |
anupanchsheel | 0:307f417b029a | 109 | { |
anupanchsheel | 0:307f417b029a | 110 | dummy=gsm.getc(); |
anupanchsheel | 0:307f417b029a | 111 | pc.putc(dummy); |
anupanchsheel | 0:307f417b029a | 112 | } |
anupanchsheel | 0:307f417b029a | 113 | } |
anupanchsheel | 0:307f417b029a | 114 | while((time(NULL) - previous) < 1); // Clean the input buffer |
anupanchsheel | 0:307f417b029a | 115 | pc.printf("\r\n"); |
anupanchsheel | 0:307f417b029a | 116 | gsm.printf("%s \r", ATcommand); // Send the AT command |
anupanchsheel | 0:307f417b029a | 117 | grs = 0; |
anupanchsheel | 0:307f417b029a | 118 | previous = time(NULL); |
anupanchsheel | 0:307f417b029a | 119 | do |
anupanchsheel | 0:307f417b029a | 120 | { // this loop waits for the answer |
anupanchsheel | 0:307f417b029a | 121 | if(gsm.readable() != 0) |
anupanchsheel | 0:307f417b029a | 122 | { |
anupanchsheel | 0:307f417b029a | 123 | response[grs] = gsm.getc(); // if there are data in the UART input buffer, reads it and checks for the asnwer |
anupanchsheel | 0:307f417b029a | 124 | pc.putc(response[grs]); |
anupanchsheel | 0:307f417b029a | 125 | grs++; |
anupanchsheel | 0:307f417b029a | 126 | if (strstr(response, expected_answer) != NULL) |
anupanchsheel | 0:307f417b029a | 127 | { // check if the desired answer is in the response of the module |
anupanchsheel | 0:307f417b029a | 128 | if(clk_flag==2 || clk_flag==4) answer=2; |
anupanchsheel | 0:307f417b029a | 129 | else answer=1; |
anupanchsheel | 0:307f417b029a | 130 | } |
anupanchsheel | 0:307f417b029a | 131 | } |
anupanchsheel | 0:307f417b029a | 132 | } while((answer !=1) && ((time(NULL) - previous) < timeout)); // Waits for the asnwer with time out |
anupanchsheel | 0:307f417b029a | 133 | if(clk_flag == 1) |
anupanchsheel | 0:307f417b029a | 134 | { |
anupanchsheel | 0:307f417b029a | 135 | //pc.printf("\r\nclock updated\r\n"); |
anupanchsheel | 0:307f417b029a | 136 | sprintf(timestamp,"%.49s",response); |
anupanchsheel | 0:307f417b029a | 137 | // .49s is the precision block, for string conversion, 49 is the max limit of the characters stored |
anupanchsheel | 0:307f417b029a | 138 | |
anupanchsheel | 0:307f417b029a | 139 | //pc.printf("%s",timestamp); //copies response to timestamp for further processing |
anupanchsheel | 0:307f417b029a | 140 | } |
anupanchsheel | 0:307f417b029a | 141 | if(clk_flag == 3) |
anupanchsheel | 0:307f417b029a | 142 | { |
anupanchsheel | 0:307f417b029a | 143 | //pc.printf("timestamp stored11: %s", time_stamp); |
anupanchsheel | 0:307f417b029a | 144 | sprintf(csq,"%.49s",response); |
anupanchsheel | 0:307f417b029a | 145 | //pc.printf("timestamp stored12: %s", time_stamp); |
anupanchsheel | 0:307f417b029a | 146 | //pc.printf ("end"); //copies response to csq for further processing |
anupanchsheel | 0:307f417b029a | 147 | } |
anupanchsheel | 0:307f417b029a | 148 | if(clk_flag==4) |
anupanchsheel | 0:307f417b029a | 149 | { |
anupanchsheel | 0:307f417b029a | 150 | sprintf(location_status,"%s",response); |
anupanchsheel | 0:307f417b029a | 151 | } |
anupanchsheel | 0:307f417b029a | 152 | if(clk_flag==5) |
anupanchsheel | 0:307f417b029a | 153 | { |
anupanchsheel | 0:307f417b029a | 154 | sprintf(server_response,"%s",response); |
anupanchsheel | 0:307f417b029a | 155 | } |
anupanchsheel | 0:307f417b029a | 156 | |
anupanchsheel | 0:307f417b029a | 157 | |
anupanchsheel | 0:307f417b029a | 158 | //pc.printf("\r\nanswer %d\r\n",answer); |
anupanchsheel | 0:307f417b029a | 159 | if(strcmp(expected_answer,"ERROR")==0){ /// assuming for that error response case the command gets executed in the timeout |
anupanchsheel | 0:307f417b029a | 160 | printf("\r\nHi\r\n"); |
anupanchsheel | 0:307f417b029a | 161 | if(answer==1) answer = 0; |
anupanchsheel | 0:307f417b029a | 162 | else answer = 1; |
anupanchsheel | 0:307f417b029a | 163 | } |
anupanchsheel | 0:307f417b029a | 164 | return answer; |
anupanchsheel | 0:307f417b029a | 165 | } |
anupanchsheel | 0:307f417b029a | 166 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 167 | // Send AT Command (Compare with 2 possible responses) // |
anupanchsheel | 0:307f417b029a | 168 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 169 | bool sendATcommand2(char* ATcommand, char* expected_answer1, char* expected_answer2, unsigned int timeout) |
anupanchsheel | 0:307f417b029a | 170 | { |
anupanchsheel | 0:307f417b029a | 171 | int grs=0; |
anupanchsheel | 0:307f417b029a | 172 | bool answer=0; //x=0 and answer=0, if not put, AT+CCLK?\r is not executed |
anupanchsheel | 0:307f417b029a | 173 | char response[300]; |
anupanchsheel | 0:307f417b029a | 174 | memset(response, '\0', 100); // Initialize the string |
anupanchsheel | 0:307f417b029a | 175 | wait_ms(100); |
anupanchsheel | 0:307f417b029a | 176 | previous = time(NULL); |
anupanchsheel | 0:307f417b029a | 177 | char dummy; |
anupanchsheel | 0:307f417b029a | 178 | do |
anupanchsheel | 0:307f417b029a | 179 | { |
anupanchsheel | 0:307f417b029a | 180 | if(gsm.readable()!=0) |
anupanchsheel | 0:307f417b029a | 181 | { |
anupanchsheel | 0:307f417b029a | 182 | dummy=gsm.getc(); |
anupanchsheel | 0:307f417b029a | 183 | pc.putc(dummy); |
anupanchsheel | 0:307f417b029a | 184 | } |
anupanchsheel | 0:307f417b029a | 185 | } |
anupanchsheel | 0:307f417b029a | 186 | while((time(NULL) - previous) < 1); // Clean the input buffer |
anupanchsheel | 0:307f417b029a | 187 | pc.printf("\r\n"); |
anupanchsheel | 0:307f417b029a | 188 | gsm.printf("%s \r", ATcommand); // Send the AT command |
anupanchsheel | 0:307f417b029a | 189 | grs = 0; |
anupanchsheel | 0:307f417b029a | 190 | previous = time(NULL); |
anupanchsheel | 0:307f417b029a | 191 | do |
anupanchsheel | 0:307f417b029a | 192 | { // this loop waits for the answer |
anupanchsheel | 0:307f417b029a | 193 | if(gsm.readable() != 0) |
anupanchsheel | 0:307f417b029a | 194 | { |
anupanchsheel | 0:307f417b029a | 195 | response[grs] = gsm.getc(); // if there are data in the UART input buffer, reads it and checks for the asnwer |
anupanchsheel | 0:307f417b029a | 196 | pc.putc(response[grs]); |
anupanchsheel | 0:307f417b029a | 197 | grs++; |
anupanchsheel | 0:307f417b029a | 198 | if (strstr(response, expected_answer1) != NULL) |
anupanchsheel | 0:307f417b029a | 199 | { // check if the desired answer is in the response of the module |
anupanchsheel | 0:307f417b029a | 200 | answer=1; |
anupanchsheel | 0:307f417b029a | 201 | } |
anupanchsheel | 0:307f417b029a | 202 | if (strstr(response, expected_answer2) != NULL) |
anupanchsheel | 0:307f417b029a | 203 | { // check if the desired answer is in the response of the module |
anupanchsheel | 0:307f417b029a | 204 | answer=1; |
anupanchsheel | 0:307f417b029a | 205 | } |
anupanchsheel | 0:307f417b029a | 206 | } |
anupanchsheel | 0:307f417b029a | 207 | } while((answer==0) && ((time(NULL) - previous) < timeout)); // Waits for the asnwer with time out |
anupanchsheel | 0:307f417b029a | 208 | //pc.printf("\r\nanswer %d\r\n",answer); |
anupanchsheel | 0:307f417b029a | 209 | return answer; |
anupanchsheel | 0:307f417b029a | 210 | } |
anupanchsheel | 0:307f417b029a | 211 | |
anupanchsheel | 0:307f417b029a | 212 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 213 | // Send AT Command (Compare with 2 serial responses) // |
anupanchsheel | 0:307f417b029a | 214 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 215 | bool sendATcommand3(char* ATcommand, char* expected_answer1, char* expected_answer2, char* expected_answer3,char* expected_answer4,unsigned int timeout) // modified for detecting error response and save time |
anupanchsheel | 0:307f417b029a | 216 | { |
anupanchsheel | 0:307f417b029a | 217 | int grs=0; |
anupanchsheel | 0:307f417b029a | 218 | bool answer=0; //x=0 and answer=0, if not put, AT+CCLK?\r is not executed |
anupanchsheel | 0:307f417b029a | 219 | char response[300]; |
anupanchsheel | 0:307f417b029a | 220 | memset(response, '\0', 100); // Initialize the string |
anupanchsheel | 0:307f417b029a | 221 | wait_ms(100); |
anupanchsheel | 0:307f417b029a | 222 | previous = time(NULL); |
anupanchsheel | 0:307f417b029a | 223 | char dummy; |
anupanchsheel | 0:307f417b029a | 224 | do |
anupanchsheel | 0:307f417b029a | 225 | { |
anupanchsheel | 0:307f417b029a | 226 | if(gsm.readable()!=0) |
anupanchsheel | 0:307f417b029a | 227 | { |
anupanchsheel | 0:307f417b029a | 228 | dummy=gsm.getc(); |
anupanchsheel | 0:307f417b029a | 229 | pc.putc(dummy); |
anupanchsheel | 0:307f417b029a | 230 | } |
anupanchsheel | 0:307f417b029a | 231 | } |
anupanchsheel | 0:307f417b029a | 232 | while((time(NULL) - previous) < 1); // Clean the input buffer |
anupanchsheel | 0:307f417b029a | 233 | pc.printf("\r\n"); |
anupanchsheel | 0:307f417b029a | 234 | gsm.printf("%s \r", ATcommand); // Send the AT command |
anupanchsheel | 0:307f417b029a | 235 | grs = 0; |
anupanchsheel | 0:307f417b029a | 236 | previous = time(NULL); |
anupanchsheel | 0:307f417b029a | 237 | do |
anupanchsheel | 0:307f417b029a | 238 | { // this loop waits for the answer |
anupanchsheel | 0:307f417b029a | 239 | if(gsm.readable() != 0) |
anupanchsheel | 0:307f417b029a | 240 | { |
anupanchsheel | 0:307f417b029a | 241 | response[grs] = gsm.getc(); // if there are data in the UART input buffer, reads it and checks for the asnwer |
anupanchsheel | 0:307f417b029a | 242 | pc.putc(response[grs]); |
anupanchsheel | 0:307f417b029a | 243 | grs++; |
anupanchsheel | 0:307f417b029a | 244 | if (strstr(response, expected_answer1) != NULL) |
anupanchsheel | 0:307f417b029a | 245 | { // check if the desired answer is in the response of the module |
anupanchsheel | 0:307f417b029a | 246 | if (strstr(response, expected_answer2) != NULL) |
anupanchsheel | 0:307f417b029a | 247 | { // check if the desired answer is in the response of the module |
anupanchsheel | 0:307f417b029a | 248 | answer=1; |
anupanchsheel | 0:307f417b029a | 249 | } |
anupanchsheel | 0:307f417b029a | 250 | else if((strstr(response, expected_answer3)||(strstr(response, expected_answer4)!=NULL))) |
anupanchsheel | 0:307f417b029a | 251 | { |
anupanchsheel | 0:307f417b029a | 252 | pc.printf("\r\n we are in ftpput error\r\n"); |
anupanchsheel | 0:307f417b029a | 253 | answer=1; |
anupanchsheel | 0:307f417b029a | 254 | } |
anupanchsheel | 0:307f417b029a | 255 | } |
anupanchsheel | 0:307f417b029a | 256 | |
anupanchsheel | 0:307f417b029a | 257 | } |
anupanchsheel | 0:307f417b029a | 258 | } while((answer==0) && ((time(NULL) - previous) < timeout)); // Waits for the asnwer with time out |
anupanchsheel | 0:307f417b029a | 259 | //pc.printf("\r\nanswer %d\r\n",answer); |
anupanchsheel | 0:307f417b029a | 260 | return answer; |
anupanchsheel | 0:307f417b029a | 261 | } |
anupanchsheel | 0:307f417b029a | 262 | |
anupanchsheel | 0:307f417b029a | 263 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 264 | // iteration |
anupanchsheel | 0:307f417b029a | 265 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 266 | |
anupanchsheel | 0:307f417b029a | 267 | bool iteration_send_ATcommand(char* ATcommand, char* expected_answer, unsigned int timeout, int clk_flag,int attempts) |
anupanchsheel | 0:307f417b029a | 268 | { |
anupanchsheel | 0:307f417b029a | 269 | for(int i=0; i < attempts; i++) |
anupanchsheel | 0:307f417b029a | 270 | { |
anupanchsheel | 0:307f417b029a | 271 | if(sendATcommand(ATcommand,expected_answer,timeout,clk_flag)) |
anupanchsheel | 0:307f417b029a | 272 | { |
anupanchsheel | 0:307f417b029a | 273 | return 1; |
anupanchsheel | 0:307f417b029a | 274 | } |
anupanchsheel | 0:307f417b029a | 275 | } |
anupanchsheel | 0:307f417b029a | 276 | return 0 ; |
anupanchsheel | 0:307f417b029a | 277 | } |
anupanchsheel | 0:307f417b029a | 278 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 279 | // Getting Timestamp & CSQ Function // |
anupanchsheel | 0:307f417b029a | 280 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 281 | |
anupanchsheel | 0:307f417b029a | 282 | void GetTime_CSQ() |
anupanchsheel | 0:307f417b029a | 283 | { |
anupanchsheel | 0:307f417b029a | 284 | sendATcommand("AT+CCLK?\r", "OK", 10,1); |
anupanchsheel | 0:307f417b029a | 285 | //sprintf(response,timestamp); |
anupanchsheel | 0:307f417b029a | 286 | int x=20; |
anupanchsheel | 0:307f417b029a | 287 | year = 10*(timestamp[x+1]-48) + (timestamp[x+2]-48); |
anupanchsheel | 0:307f417b029a | 288 | month = (timestamp[x+4]-48)*10+(timestamp[x+5]-48); |
anupanchsheel | 0:307f417b029a | 289 | date = (timestamp[x+7]-48)*10+(timestamp[x+8]-48); |
anupanchsheel | 0:307f417b029a | 290 | hour = (timestamp[x+10]-48)*10+(timestamp[x+11]-48); |
anupanchsheel | 0:307f417b029a | 291 | minute = (timestamp[x+13]-48)*10+(timestamp[x+14]-48); |
anupanchsheel | 0:307f417b029a | 292 | second = (timestamp[x+16]-48)*10+(timestamp[x+17]-48); |
anupanchsheel | 0:307f417b029a | 293 | sprintf(time_stamp,"%02d%02d%02d%02d%02d%02d",year,month,date,hour,minute,second); |
anupanchsheel | 0:307f417b029a | 294 | |
anupanchsheel | 0:307f417b029a | 295 | pc.printf("Get time result:%s.....",time_stamp); |
anupanchsheel | 0:307f417b029a | 296 | if(sendATcommand("AT+CSQ","OK",5,3)){ |
anupanchsheel | 0:307f417b029a | 297 | if(csq[18]==44){ |
anupanchsheel | 0:307f417b029a | 298 | int csq_calc=10*(csq[16]-48)+(csq[17]-48); |
anupanchsheel | 0:307f417b029a | 299 | sprintf(csq_value,"%02d",csq_calc); |
anupanchsheel | 0:307f417b029a | 300 | } |
anupanchsheel | 0:307f417b029a | 301 | else if(csq[17]==44){ |
anupanchsheel | 0:307f417b029a | 302 | sprintf(csq_value,"0%c", csq[16]); |
anupanchsheel | 0:307f417b029a | 303 | } |
anupanchsheel | 0:307f417b029a | 304 | else sprintf(csq_value,"NA"); |
anupanchsheel | 0:307f417b029a | 305 | } |
anupanchsheel | 0:307f417b029a | 306 | //pc.printf("CSQ: %c %c %s",csq[16],csq[17], csq_value); |
anupanchsheel | 0:307f417b029a | 307 | |
anupanchsheel | 0:307f417b029a | 308 | } |
anupanchsheel | 0:307f417b029a | 309 | |
anupanchsheel | 0:307f417b029a | 310 | |
anupanchsheel | 0:307f417b029a | 311 | |
anupanchsheel | 0:307f417b029a | 312 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 313 | // Getting File Directories and names // |
anupanchsheel | 0:307f417b029a | 314 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 315 | void GetFileDir() |
anupanchsheel | 0:307f417b029a | 316 | { |
anupanchsheel | 0:307f417b029a | 317 | int attempt_cnt=0; |
anupanchsheel | 0:307f417b029a | 318 | char month_dir[100]=""; |
anupanchsheel | 0:307f417b029a | 319 | char date_dir[100] =""; |
anupanchsheel | 0:307f417b029a | 320 | GetTime_CSQ(); |
anupanchsheel | 0:307f417b029a | 321 | while(year<15||year>20) |
anupanchsheel | 0:307f417b029a | 322 | { |
anupanchsheel | 0:307f417b029a | 323 | pc.printf("%d\r\n",year); |
anupanchsheel | 0:307f417b029a | 324 | GetTime_CSQ(); |
anupanchsheel | 0:307f417b029a | 325 | attempt_cnt++; |
anupanchsheel | 0:307f417b029a | 326 | if(attempt_cnt>4) break; |
anupanchsheel | 0:307f417b029a | 327 | } |
anupanchsheel | 0:307f417b029a | 328 | if(year>14&&year<21&&month<13) |
anupanchsheel | 0:307f417b029a | 329 | { |
anupanchsheel | 0:307f417b029a | 330 | if((minute!=minuteold)||(second!=secondold)) |
anupanchsheel | 0:307f417b029a | 331 | { |
anupanchsheel | 0:307f417b029a | 332 | sprintf(timestampold,timestamp); |
anupanchsheel | 0:307f417b029a | 333 | //pc.printf("stage 1"); |
anupanchsheel | 0:307f417b029a | 334 | minuteold=minute; |
anupanchsheel | 0:307f417b029a | 335 | secondold=second; |
anupanchsheel | 0:307f417b029a | 336 | sprintf(main_dir,"/sd/%s",sitename); |
anupanchsheel | 0:307f417b029a | 337 | sprintf(month_dir,"%s/%s%02d%02d",main_dir,sitename,year,month); |
anupanchsheel | 0:307f417b029a | 338 | sprintf(date_dir,"%s/%s%02d%02d%02d",month_dir,sitename,year,month,date); |
anupanchsheel | 0:307f417b029a | 339 | sprintf(hour_dir,"%s/%s%02d%02d%02d%02d",date_dir,sitename,year,month,date,hour); |
anupanchsheel | 0:307f417b029a | 340 | sprintf(filepath,"%s/%s%02d%02d%02d%02d%02d%02d",hour_dir,sitename,year,month,date,hour,minute,second); |
anupanchsheel | 0:307f417b029a | 341 | sprintf(filename,"%s%02d%02d%02d%02d%02d%02d",sitename,year,month,date,hour,minute,second); |
anupanchsheel | 0:307f417b029a | 342 | //sprintf(ftpputpathname,"%s/%02d/%02d/%02d/%02d",module_name,year,month,date,hour); // For FTPPUTPATHNAME feature |
anupanchsheel | 0:307f417b029a | 343 | //sprintf(ftpputpathname_hour,"%s/%02d/%02d/%02d",module_name,year,month,date); |
anupanchsheel | 0:307f417b029a | 344 | |
anupanchsheel | 0:307f417b029a | 345 | // for the tcp connection, the name and path will be decided by the script on the server |
anupanchsheel | 0:307f417b029a | 346 | //pc.printf("Ftpputpathname is %s\r\n",ftpputpathname); // For FTPPUTPATHNAME feature |
anupanchsheel | 0:307f417b029a | 347 | gsmerr=0; |
anupanchsheel | 0:307f417b029a | 348 | //pc.printf("stage 2"); |
anupanchsheel | 0:307f417b029a | 349 | mkdir(main_dir,0777); |
anupanchsheel | 0:307f417b029a | 350 | mkdir(month_dir,0777); |
anupanchsheel | 0:307f417b029a | 351 | mkdir(date_dir,0777); |
anupanchsheel | 0:307f417b029a | 352 | mkdir(hour_dir,0777); |
anupanchsheel | 0:307f417b029a | 353 | //pc.printf("stage 3"); |
anupanchsheel | 0:307f417b029a | 354 | } |
anupanchsheel | 0:307f417b029a | 355 | else //timestamp same as previous |
anupanchsheel | 0:307f417b029a | 356 | { |
anupanchsheel | 0:307f417b029a | 357 | //pc.printf("\r\n we r in same time stamp"); |
anupanchsheel | 0:307f417b029a | 358 | sprintf(filepath,"%s/%s%02d%02d%02d%02d%02d%02dErr%03d",hour_dir,sitename,year,month,date,hour,minute,second,gsmerr); |
anupanchsheel | 0:307f417b029a | 359 | sprintf(filename,"%s%02d%02d%02d%02d%02d%02dErr%03d",sitename,year,month,date,hour,minute,second,gsmerr); |
anupanchsheel | 0:307f417b029a | 360 | gsmerr++; |
anupanchsheel | 0:307f417b029a | 361 | //pc.printf("\r\n we r leaving same time stamp"); |
anupanchsheel | 0:307f417b029a | 362 | } |
anupanchsheel | 0:307f417b029a | 363 | } |
anupanchsheel | 0:307f417b029a | 364 | else //garbage timestamp |
anupanchsheel | 0:307f417b029a | 365 | { |
anupanchsheel | 0:307f417b029a | 366 | //pc.printf("\r\n we r in out of range TS"); |
anupanchsheel | 0:307f417b029a | 367 | sprintf(timestamp,timestampold); |
anupanchsheel | 0:307f417b029a | 368 | //pc.printf("\r\nTimestamp......%s\r\n",timestamp); |
anupanchsheel | 0:307f417b029a | 369 | |
anupanchsheel | 0:307f417b029a | 370 | sprintf(filepath,"%s/%s%02d%02dErr%03d",hour_dir,sitename,minuteold,secondold,gsmerr); |
anupanchsheel | 0:307f417b029a | 371 | sprintf(filename,"%s%02d%02dErr%03d",sitename,minuteold,secondold,gsmerr); |
anupanchsheel | 0:307f417b029a | 372 | gsmerr++; |
anupanchsheel | 0:307f417b029a | 373 | } |
anupanchsheel | 0:307f417b029a | 374 | } |
anupanchsheel | 0:307f417b029a | 375 | |
anupanchsheel | 0:307f417b029a | 376 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 377 | // DC & Temp Detect // |
anupanchsheel | 0:307f417b029a | 378 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 379 | void DC_TC_Detection(short int k) //k limited by array size of avg_dc_detect & avg_temp |
anupanchsheel | 0:307f417b029a | 380 | { |
anupanchsheel | 0:307f417b029a | 381 | Timer t; |
anupanchsheel | 0:307f417b029a | 382 | int j=0; |
anupanchsheel | 0:307f417b029a | 383 | ADC0_CFG2=ADC0_CFG2||0x10; |
anupanchsheel | 0:307f417b029a | 384 | t.start(); |
anupanchsheel | 0:307f417b029a | 385 | while(j<10) |
anupanchsheel | 0:307f417b029a | 386 | { |
anupanchsheel | 0:307f417b029a | 387 | if((int)(t.read()*50*freq*1000)%100 == 0) |
anupanchsheel | 0:307f417b029a | 388 | { |
anupanchsheel | 0:307f417b029a | 389 | DC_Detect_data[j] = DC_Detect.read_u16(); |
anupanchsheel | 0:307f417b029a | 390 | Temp_data[j] = TC1.read_u16(); |
anupanchsheel | 0:307f417b029a | 391 | ++j; |
anupanchsheel | 0:307f417b029a | 392 | } |
anupanchsheel | 0:307f417b029a | 393 | } |
anupanchsheel | 0:307f417b029a | 394 | t.stop(); |
anupanchsheel | 0:307f417b029a | 395 | //pc.printf("\r\nTemp Time Taken %f \n\r", t.read()); |
anupanchsheel | 0:307f417b029a | 396 | t.reset(); |
anupanchsheel | 0:307f417b029a | 397 | avg_dc_detect[k]=0; |
anupanchsheel | 0:307f417b029a | 398 | avg_temp[k]=0; |
anupanchsheel | 0:307f417b029a | 399 | for(int i=0; i<10; i++) |
anupanchsheel | 0:307f417b029a | 400 | { |
anupanchsheel | 0:307f417b029a | 401 | avg_dc_detect[k]=avg_dc_detect[k]+int(DC_Detect_data[i]); |
anupanchsheel | 0:307f417b029a | 402 | avg_temp[k]=avg_temp[k]+int(Temp_data[i]); |
anupanchsheel | 0:307f417b029a | 403 | } |
anupanchsheel | 0:307f417b029a | 404 | avg_dc_detect[k]=avg_dc_detect[k]/10; |
anupanchsheel | 0:307f417b029a | 405 | avg_temp[k]=avg_temp[k]/10; |
anupanchsheel | 0:307f417b029a | 406 | // pc.printf("Total Samples %d\r\n",j); |
anupanchsheel | 0:307f417b029a | 407 | } |
anupanchsheel | 0:307f417b029a | 408 | |
anupanchsheel | 0:307f417b029a | 409 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 410 | // AC-DC Sampling // |
anupanchsheel | 0:307f417b029a | 411 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 412 | void calculate_ADC() |
anupanchsheel | 0:307f417b029a | 413 | { |
anupanchsheel | 0:307f417b029a | 414 | Timer t; |
anupanchsheel | 0:307f417b029a | 415 | int j=0; |
anupanchsheel | 0:307f417b029a | 416 | pc.printf("\n\rStarting Reading \n\r"); |
anupanchsheel | 0:307f417b029a | 417 | t.start(); |
anupanchsheel | 0:307f417b029a | 418 | while(j<dataLength) |
anupanchsheel | 0:307f417b029a | 419 | { |
anupanchsheel | 0:307f417b029a | 420 | if((int)(t.read()*100*freq*1000)%100 == 0) |
anupanchsheel | 0:307f417b029a | 421 | { |
anupanchsheel | 0:307f417b029a | 422 | ACdata[j] = AC_input.read_u16(); |
anupanchsheel | 0:307f417b029a | 423 | DCdata[j] = DC_input.read_u16(); |
anupanchsheel | 0:307f417b029a | 424 | ++j; |
anupanchsheel | 0:307f417b029a | 425 | } |
anupanchsheel | 0:307f417b029a | 426 | } |
anupanchsheel | 0:307f417b029a | 427 | t.stop(); |
anupanchsheel | 0:307f417b029a | 428 | //pc.printf("Time Taken %f \n\r", t.read()); |
anupanchsheel | 0:307f417b029a | 429 | t.reset(); |
anupanchsheel | 0:307f417b029a | 430 | //pc.printf("Total Samples %d\r\n",j); |
anupanchsheel | 0:307f417b029a | 431 | } |
anupanchsheel | 0:307f417b029a | 432 | |
anupanchsheel | 0:307f417b029a | 433 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 434 | // Sampling Function // |
anupanchsheel | 0:307f417b029a | 435 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 436 | |
anupanchsheel | 0:307f417b029a | 437 | void sampling() |
anupanchsheel | 0:307f417b029a | 438 | { |
anupanchsheel | 0:307f417b029a | 439 | pc.printf("\r\nSampling attempt"); |
anupanchsheel | 0:307f417b029a | 440 | DC_TC_Detection(0); |
anupanchsheel | 0:307f417b029a | 441 | calculate_ADC(); |
anupanchsheel | 0:307f417b029a | 442 | DC_TC_Detection(1); |
anupanchsheel | 0:307f417b029a | 443 | } |
anupanchsheel | 0:307f417b029a | 444 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 445 | // FFT Function // |
anupanchsheel | 0:307f417b029a | 446 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 447 | |
anupanchsheel | 0:307f417b029a | 448 | void fft(short N, short start_index, short step, int loop) |
anupanchsheel | 0:307f417b029a | 449 | { |
anupanchsheel | 0:307f417b029a | 450 | if(N==2048) |
anupanchsheel | 0:307f417b029a | 451 | { |
anupanchsheel | 0:307f417b029a | 452 | float input_2048[2048]; |
anupanchsheel | 0:307f417b029a | 453 | for (short i=0; i<2048; i++) |
anupanchsheel | 0:307f417b029a | 454 | { |
anupanchsheel | 0:307f417b029a | 455 | input_2048[i]=float(fft_array[(start_index+i*step)]); |
anupanchsheel | 0:307f417b029a | 456 | } |
anupanchsheel | 0:307f417b029a | 457 | float output_4096[4096]; |
anupanchsheel | 0:307f417b029a | 458 | arm_rfft_instance_f32 SR; |
anupanchsheel | 0:307f417b029a | 459 | arm_cfft_radix4_instance_f32 SR_CFFT; |
anupanchsheel | 0:307f417b029a | 460 | arm_status status=arm_rfft_init_f32(&SR, &SR_CFFT, 2048, 0, 1); |
anupanchsheel | 0:307f417b029a | 461 | arm_rfft_f32(&SR, input_2048, output_4096); |
anupanchsheel | 0:307f417b029a | 462 | for (short i=0; i<4096; i++) |
anupanchsheel | 0:307f417b029a | 463 | { |
anupanchsheel | 0:307f417b029a | 464 | fft_array[start_index+i*step]=int(output_4096[i]); |
anupanchsheel | 0:307f417b029a | 465 | } |
anupanchsheel | 0:307f417b029a | 466 | return; |
anupanchsheel | 0:307f417b029a | 467 | } |
anupanchsheel | 0:307f417b029a | 468 | else |
anupanchsheel | 0:307f417b029a | 469 | { |
anupanchsheel | 0:307f417b029a | 470 | fft(N/2, start_index, step*2,0); |
anupanchsheel | 0:307f417b029a | 471 | fft(N/2, start_index+step, step*2,0); |
anupanchsheel | 0:307f417b029a | 472 | float output_data1[2]; |
anupanchsheel | 0:307f417b029a | 473 | float output_data2[2]; |
anupanchsheel | 0:307f417b029a | 474 | float output_data3[2]; |
anupanchsheel | 0:307f417b029a | 475 | float output_data4[2]; |
anupanchsheel | 0:307f417b029a | 476 | output_data3[0]=fft_array[start_index]; |
anupanchsheel | 0:307f417b029a | 477 | output_data3[1]=fft_array[start_index+2*step]; |
anupanchsheel | 0:307f417b029a | 478 | output_data4[0]=fft_array[start_index+step]; |
anupanchsheel | 0:307f417b029a | 479 | output_data4[1]=fft_array[start_index+3*step]; |
anupanchsheel | 0:307f417b029a | 480 | fft_array[start_index]=output_data3[0]+output_data4[0]; |
anupanchsheel | 0:307f417b029a | 481 | fft_array[start_index+step]=output_data3[1]+output_data4[1]; |
anupanchsheel | 0:307f417b029a | 482 | for(int i=1; i<N/2; i++) |
anupanchsheel | 0:307f417b029a | 483 | { |
anupanchsheel | 0:307f417b029a | 484 | output_data1[0]=float(fft_array[start_index+4*(i)*step]); |
anupanchsheel | 0:307f417b029a | 485 | output_data1[1]=float(fft_array[start_index+(4*(i)+2)*step]); |
anupanchsheel | 0:307f417b029a | 486 | output_data2[0]=float(fft_array[start_index+(4*(i)+1)*step]); |
anupanchsheel | 0:307f417b029a | 487 | output_data2[1]=float(fft_array[start_index+(4*(i)+3)*step]); |
anupanchsheel | 0:307f417b029a | 488 | fft_array[start_index+2*i*step]=int(output_data1[0]+(output_data2[0]*cos(2*i*3.142/N))-(output_data2[1]*sin(-2*i*3.142/N))); |
anupanchsheel | 0:307f417b029a | 489 | fft_array[start_index+(2*i+1)*step]=int(output_data1[1]+(output_data2[0]*sin(-2*i*3.142/N))+(output_data2[1]*cos(2*i*3.142/N))); |
anupanchsheel | 0:307f417b029a | 490 | } |
anupanchsheel | 0:307f417b029a | 491 | fft_array[start_index+N*step]=output_data3[0]-output_data4[0]; |
anupanchsheel | 0:307f417b029a | 492 | fft_array[start_index+(N+1)*step]=output_data3[1]-output_data4[1]; |
anupanchsheel | 0:307f417b029a | 493 | if(loop==0) |
anupanchsheel | 0:307f417b029a | 494 | { |
anupanchsheel | 0:307f417b029a | 495 | for(int i=N/2+1; i<N; i++) |
anupanchsheel | 0:307f417b029a | 496 | { |
anupanchsheel | 0:307f417b029a | 497 | fft_array[start_index+2*i*step]=fft_array[start_index+2*(N-i)*step]; |
anupanchsheel | 0:307f417b029a | 498 | fft_array[start_index+(2*i+1)*step]=-1*fft_array[start_index+(2*(N-i)+1)*step]; |
anupanchsheel | 0:307f417b029a | 499 | } |
anupanchsheel | 0:307f417b029a | 500 | } |
anupanchsheel | 0:307f417b029a | 501 | if(loop==1) |
anupanchsheel | 0:307f417b029a | 502 | { |
anupanchsheel | 0:307f417b029a | 503 | for(short i=0; i<8193; i++) |
anupanchsheel | 0:307f417b029a | 504 | { |
anupanchsheel | 0:307f417b029a | 505 | fft_array[i]=int(sqrt((double(fft_array[2*i])*double(fft_array[2*i]))+(double(fft_array[2*i+1])*double(fft_array[2*i+1])))); |
anupanchsheel | 0:307f417b029a | 506 | } |
anupanchsheel | 0:307f417b029a | 507 | } |
anupanchsheel | 0:307f417b029a | 508 | return; |
anupanchsheel | 0:307f417b029a | 509 | } |
anupanchsheel | 0:307f417b029a | 510 | } |
anupanchsheel | 0:307f417b029a | 511 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 512 | // FFT Max Function // |
anupanchsheel | 0:307f417b029a | 513 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 514 | |
anupanchsheel | 0:307f417b029a | 515 | void fft_max(int start_index, int number_count, int maximal_count, uint32_t* maximum_index, float* maximum_value) |
anupanchsheel | 0:307f417b029a | 516 | { |
anupanchsheel | 0:307f417b029a | 517 | float maximal_array[number_count]; |
anupanchsheel | 0:307f417b029a | 518 | for (int i=0; i<number_count; i++) |
anupanchsheel | 0:307f417b029a | 519 | { |
anupanchsheel | 0:307f417b029a | 520 | maximal_array[i]=float(fft_array[start_index+i]); |
anupanchsheel | 0:307f417b029a | 521 | } |
anupanchsheel | 0:307f417b029a | 522 | for(int i=0; i<maximal_count; i++) |
anupanchsheel | 0:307f417b029a | 523 | { |
anupanchsheel | 0:307f417b029a | 524 | arm_max_f32(maximal_array,number_count,&maximum_value[i],&maximum_index[i]); |
anupanchsheel | 0:307f417b029a | 525 | maximal_array[maximum_index[i]]=0; |
anupanchsheel | 0:307f417b029a | 526 | maximum_index[i]=start_index+maximum_index[i]; |
anupanchsheel | 0:307f417b029a | 527 | } |
anupanchsheel | 0:307f417b029a | 528 | } |
anupanchsheel | 0:307f417b029a | 529 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 530 | // FFT Array Prepare // |
anupanchsheel | 0:307f417b029a | 531 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 532 | |
anupanchsheel | 0:307f417b029a | 533 | int prepare_fft_array(int* start_index, int* points_count, int* maximas_required, int segments, int fft_type,int count) |
anupanchsheel | 0:307f417b029a | 534 | { |
anupanchsheel | 0:307f417b029a | 535 | //pc.printf("\r\n we r in prepare fft"); |
anupanchsheel | 0:307f417b029a | 536 | int max_points_count=0; |
anupanchsheel | 0:307f417b029a | 537 | for (int i=0; i<segments; i++) |
anupanchsheel | 0:307f417b029a | 538 | { |
anupanchsheel | 0:307f417b029a | 539 | max_points_count=max_points_count+maximas_required[i]; |
anupanchsheel | 0:307f417b029a | 540 | } |
anupanchsheel | 0:307f417b029a | 541 | uint32_t index[max_points_count]; |
anupanchsheel | 0:307f417b029a | 542 | float maxima[max_points_count]; |
anupanchsheel | 0:307f417b029a | 543 | int j=0; |
anupanchsheel | 0:307f417b029a | 544 | for (int i=0; i<segments; i++) |
anupanchsheel | 0:307f417b029a | 545 | { |
anupanchsheel | 0:307f417b029a | 546 | fft_max(start_index[i],points_count[i],maximas_required[i],&index[j],&maxima[j]); |
anupanchsheel | 0:307f417b029a | 547 | j=j+maximas_required[i]; |
anupanchsheel | 0:307f417b029a | 548 | } |
anupanchsheel | 0:307f417b029a | 549 | |
anupanchsheel | 0:307f417b029a | 550 | char index1[4]=""; |
anupanchsheel | 0:307f417b029a | 551 | char maxima1[9]=""; |
anupanchsheel | 0:307f417b029a | 552 | int length; |
anupanchsheel | 0:307f417b029a | 553 | //pc.printf("\r\n status flag is=%d",sd_status_flag); |
anupanchsheel | 0:307f417b029a | 554 | if(sd_status_flag==0) |
anupanchsheel | 0:307f417b029a | 555 | { |
anupanchsheel | 0:307f417b029a | 556 | for(int i=0; i<max_points_count; i++) |
anupanchsheel | 0:307f417b029a | 557 | { |
anupanchsheel | 0:307f417b029a | 558 | length=sprintf(index1,"%d",index[i]); |
anupanchsheel | 0:307f417b029a | 559 | for(j=0;j<length;j++) |
anupanchsheel | 0:307f417b029a | 560 | { |
anupanchsheel | 0:307f417b029a | 561 | file_data[count]=index1[j]; |
anupanchsheel | 0:307f417b029a | 562 | count++; |
anupanchsheel | 0:307f417b029a | 563 | } |
anupanchsheel | 0:307f417b029a | 564 | |
anupanchsheel | 0:307f417b029a | 565 | file_data[count]=32; |
anupanchsheel | 0:307f417b029a | 566 | count++; |
anupanchsheel | 0:307f417b029a | 567 | length=0; |
anupanchsheel | 0:307f417b029a | 568 | length=sprintf(maxima1,"%d",int(maxima[i])); |
anupanchsheel | 0:307f417b029a | 569 | |
anupanchsheel | 0:307f417b029a | 570 | for(j=0;j<length;j++) |
anupanchsheel | 0:307f417b029a | 571 | { |
anupanchsheel | 0:307f417b029a | 572 | file_data[count]=maxima1[j]; |
anupanchsheel | 0:307f417b029a | 573 | count++; |
anupanchsheel | 0:307f417b029a | 574 | } |
anupanchsheel | 0:307f417b029a | 575 | file_data[count]=13; |
anupanchsheel | 0:307f417b029a | 576 | count++; |
anupanchsheel | 0:307f417b029a | 577 | file_data[count]=10; |
anupanchsheel | 0:307f417b029a | 578 | count++; |
anupanchsheel | 0:307f417b029a | 579 | } |
anupanchsheel | 0:307f417b029a | 580 | return count; |
anupanchsheel | 0:307f417b029a | 581 | |
anupanchsheel | 0:307f417b029a | 582 | } |
anupanchsheel | 0:307f417b029a | 583 | else |
anupanchsheel | 0:307f417b029a | 584 | { |
anupanchsheel | 0:307f417b029a | 585 | FILE *fp = fopen(pathfft, "a"); |
anupanchsheel | 0:307f417b029a | 586 | //pc.printf("\r\n opening file"); |
anupanchsheel | 0:307f417b029a | 587 | if(fp == NULL) |
anupanchsheel | 0:307f417b029a | 588 | { |
anupanchsheel | 0:307f417b029a | 589 | //pc.printf("\r\n before return"); |
anupanchsheel | 0:307f417b029a | 590 | return count; |
anupanchsheel | 0:307f417b029a | 591 | } |
anupanchsheel | 0:307f417b029a | 592 | else |
anupanchsheel | 0:307f417b029a | 593 | { |
anupanchsheel | 0:307f417b029a | 594 | //pc.printf("\r\n ******"); |
anupanchsheel | 0:307f417b029a | 595 | for(int i=0; i<max_points_count; i++) |
anupanchsheel | 0:307f417b029a | 596 | { |
anupanchsheel | 0:307f417b029a | 597 | fprintf(fp,"\n%d %d",index[i], int(maxima[i])); |
anupanchsheel | 0:307f417b029a | 598 | length=sprintf(index1,"%d",index[i]); |
anupanchsheel | 0:307f417b029a | 599 | for(j=0;j<length;j++) |
anupanchsheel | 0:307f417b029a | 600 | { |
anupanchsheel | 0:307f417b029a | 601 | file_data[count]=index1[j]; |
anupanchsheel | 0:307f417b029a | 602 | count++; |
anupanchsheel | 0:307f417b029a | 603 | } |
anupanchsheel | 0:307f417b029a | 604 | |
anupanchsheel | 0:307f417b029a | 605 | file_data[count]=32; |
anupanchsheel | 0:307f417b029a | 606 | count++; |
anupanchsheel | 0:307f417b029a | 607 | length=0; |
anupanchsheel | 0:307f417b029a | 608 | length=sprintf(maxima1,"%d",int(maxima[i])); |
anupanchsheel | 0:307f417b029a | 609 | |
anupanchsheel | 0:307f417b029a | 610 | for(j=0;j<length;j++) |
anupanchsheel | 0:307f417b029a | 611 | { |
anupanchsheel | 0:307f417b029a | 612 | file_data[count]=maxima1[j]; |
anupanchsheel | 0:307f417b029a | 613 | count++; |
anupanchsheel | 0:307f417b029a | 614 | } |
anupanchsheel | 0:307f417b029a | 615 | file_data[count]=13; |
anupanchsheel | 0:307f417b029a | 616 | count++; |
anupanchsheel | 0:307f417b029a | 617 | file_data[count]=10; |
anupanchsheel | 0:307f417b029a | 618 | count++; |
anupanchsheel | 0:307f417b029a | 619 | } |
anupanchsheel | 0:307f417b029a | 620 | if(fft_type==0) |
anupanchsheel | 0:307f417b029a | 621 | { |
anupanchsheel | 0:307f417b029a | 622 | fprintf(fp,"\r\n\r\n"); |
anupanchsheel | 0:307f417b029a | 623 | maxfreq= index[1]; |
anupanchsheel | 0:307f417b029a | 624 | } |
anupanchsheel | 0:307f417b029a | 625 | else fprintf(fp,"\r\nEOF"); |
anupanchsheel | 0:307f417b029a | 626 | fclose(fp); |
anupanchsheel | 0:307f417b029a | 627 | return count; |
anupanchsheel | 0:307f417b029a | 628 | } |
anupanchsheel | 0:307f417b029a | 629 | } |
anupanchsheel | 0:307f417b029a | 630 | } |
anupanchsheel | 0:307f417b029a | 631 | |
anupanchsheel | 0:307f417b029a | 632 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 633 | // Storage // |
anupanchsheel | 0:307f417b029a | 634 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 635 | void store() |
anupanchsheel | 0:307f417b029a | 636 | { |
anupanchsheel | 0:307f417b029a | 637 | GetFileDir(); |
anupanchsheel | 0:307f417b029a | 638 | //pc.printf("getfiledir completed"); |
anupanchsheel | 0:307f417b029a | 639 | // AC Sample storage |
anupanchsheel | 0:307f417b029a | 640 | //pc.printf("\r\n we r back in storage"); |
anupanchsheel | 0:307f417b029a | 641 | sprintf(filenameAC,"%sAC.txt",filename); |
anupanchsheel | 0:307f417b029a | 642 | sprintf(pathAC,"%sAC.txt",filepath); |
anupanchsheel | 0:307f417b029a | 643 | //pc.printf("\r\nFilename-AC\t%s", pathAC); |
anupanchsheel | 0:307f417b029a | 644 | FILE *fp = fopen(pathAC, "w"); |
anupanchsheel | 0:307f417b029a | 645 | |
anupanchsheel | 0:307f417b029a | 646 | if(fp == NULL) |
anupanchsheel | 0:307f417b029a | 647 | { |
anupanchsheel | 0:307f417b029a | 648 | pc.printf("Could not open file for write\n\r"); |
anupanchsheel | 0:307f417b029a | 649 | //reset_mod(); |
anupanchsheel | 0:307f417b029a | 650 | } |
anupanchsheel | 0:307f417b029a | 651 | else |
anupanchsheel | 0:307f417b029a | 652 | { |
anupanchsheel | 0:307f417b029a | 653 | //pc.printf("attempting to save file\n\r"); |
anupanchsheel | 0:307f417b029a | 654 | for(int k=0; k<dataLength; k++) |
anupanchsheel | 0:307f417b029a | 655 | { |
anupanchsheel | 0:307f417b029a | 656 | fprintf(fp, "%d.", ACdata[k]); |
anupanchsheel | 0:307f417b029a | 657 | } |
anupanchsheel | 0:307f417b029a | 658 | fprintf(fp, "EOF"); |
anupanchsheel | 0:307f417b029a | 659 | fclose(fp); |
anupanchsheel | 0:307f417b029a | 660 | //pc.printf("\r\n AC write done"); |
anupanchsheel | 0:307f417b029a | 661 | } |
anupanchsheel | 0:307f417b029a | 662 | // DC Sample storage |
anupanchsheel | 0:307f417b029a | 663 | sprintf(filenameDC,"%sDC.txt",filename); |
anupanchsheel | 0:307f417b029a | 664 | sprintf(pathDC,"%sDC.txt",filepath); |
anupanchsheel | 0:307f417b029a | 665 | //pc.printf("\r\nFilename-DC\t%s", pathDC); |
anupanchsheel | 0:307f417b029a | 666 | fp = fopen(pathDC, "w"); |
anupanchsheel | 0:307f417b029a | 667 | if(fp == NULL) |
anupanchsheel | 0:307f417b029a | 668 | { |
anupanchsheel | 0:307f417b029a | 669 | //pc.printf("Could not open file for write\n\r"); |
anupanchsheel | 0:307f417b029a | 670 | //reset_mod(); |
anupanchsheel | 0:307f417b029a | 671 | } |
anupanchsheel | 0:307f417b029a | 672 | else |
anupanchsheel | 0:307f417b029a | 673 | { |
anupanchsheel | 0:307f417b029a | 674 | for(int k=0; k<dataLength; k++) |
anupanchsheel | 0:307f417b029a | 675 | { |
anupanchsheel | 0:307f417b029a | 676 | fprintf(fp, "%d.", DCdata[k]); |
anupanchsheel | 0:307f417b029a | 677 | } |
anupanchsheel | 0:307f417b029a | 678 | fprintf(fp, "EOF"); |
anupanchsheel | 0:307f417b029a | 679 | fclose(fp); |
anupanchsheel | 0:307f417b029a | 680 | //pc.printf("\r\n DC write done"); |
anupanchsheel | 0:307f417b029a | 681 | } |
anupanchsheel | 0:307f417b029a | 682 | |
anupanchsheel | 0:307f417b029a | 683 | //FFT starts |
anupanchsheel | 0:307f417b029a | 684 | //pc.printf("\r\nfftstart"); |
anupanchsheel | 0:307f417b029a | 685 | //pc.printf("\r\nfftstage1"); |
anupanchsheel | 0:307f417b029a | 686 | sprintf(filenamefft,"%sft.txt",filename); |
anupanchsheel | 0:307f417b029a | 687 | //sprintf(ftp_timestamp,"%s",filename); // For FTP Timestamp |
anupanchsheel | 0:307f417b029a | 688 | sprintf(pathfft,"%sft.txt",filepath); |
anupanchsheel | 0:307f417b029a | 689 | //pc.printf("\r\nFilename-FFT\t%s", pathfft); |
anupanchsheel | 0:307f417b029a | 690 | |
anupanchsheel | 0:307f417b029a | 691 | int fft_segments=4; |
anupanchsheel | 0:307f417b029a | 692 | int fft_start_index[4]= {0,760,1520,2280}; |
anupanchsheel | 0:307f417b029a | 693 | int fft_points_count[4]= {4,120,240,360}; |
anupanchsheel | 0:307f417b029a | 694 | int fft_maximas_required[4]= {1,20,8,8}; |
anupanchsheel | 0:307f417b029a | 695 | float AC_rms_value=0; |
anupanchsheel | 0:307f417b029a | 696 | float AC_mean_value=0; |
anupanchsheel | 0:307f417b029a | 697 | float DC_rms_value=0; |
anupanchsheel | 0:307f417b029a | 698 | float DC_mean_value=0; |
anupanchsheel | 0:307f417b029a | 699 | //AC fft |
anupanchsheel | 0:307f417b029a | 700 | for(int i=0; i<fft_points; i++) |
anupanchsheel | 0:307f417b029a | 701 | { |
anupanchsheel | 0:307f417b029a | 702 | fft_array[i]=int(ACdata[i]); |
anupanchsheel | 0:307f417b029a | 703 | AC_mean_value=AC_mean_value+fft_array[i]; |
anupanchsheel | 0:307f417b029a | 704 | //pc.printf("%d %d %d\r\n", i, i+int(fft_points), fft_array[i]); |
anupanchsheel | 0:307f417b029a | 705 | fft_array[(i+int(fft_points))]=0; |
anupanchsheel | 0:307f417b029a | 706 | } |
anupanchsheel | 0:307f417b029a | 707 | //AC RMS |
anupanchsheel | 0:307f417b029a | 708 | AC_mean_value=AC_mean_value/fft_points; |
anupanchsheel | 0:307f417b029a | 709 | for(int i=0; i<fft_points; i++) |
anupanchsheel | 0:307f417b029a | 710 | { |
anupanchsheel | 0:307f417b029a | 711 | AC_rms_value=AC_rms_value+((AC_mean_value-fft_array[i])*(AC_mean_value-fft_array[i])/10000); |
anupanchsheel | 0:307f417b029a | 712 | } |
anupanchsheel | 0:307f417b029a | 713 | //file_data array write |
anupanchsheel | 0:307f417b029a | 714 | int count=0; |
anupanchsheel | 0:307f417b029a | 715 | int length=0; |
anupanchsheel | 0:307f417b029a | 716 | char AC_rms[10]=""; |
anupanchsheel | 0:307f417b029a | 717 | char DC_rms[10]=""; |
anupanchsheel | 0:307f417b029a | 718 | char avg_dc_detect1[10]=""; |
anupanchsheel | 0:307f417b029a | 719 | char avg_dc_detect2[10]=""; |
anupanchsheel | 0:307f417b029a | 720 | char avg_temp1[10]=""; |
anupanchsheel | 0:307f417b029a | 721 | char avg_temp2[10]=""; |
anupanchsheel | 0:307f417b029a | 722 | |
anupanchsheel | 0:307f417b029a | 723 | for (int i=0; i<12; i++) //Timestamp |
anupanchsheel | 0:307f417b029a | 724 | { |
anupanchsheel | 0:307f417b029a | 725 | file_data[i]= time_stamp[i]; |
anupanchsheel | 0:307f417b029a | 726 | count=count+1; |
anupanchsheel | 0:307f417b029a | 727 | } |
anupanchsheel | 0:307f417b029a | 728 | file_data[count]=32; |
anupanchsheel | 0:307f417b029a | 729 | count++; |
anupanchsheel | 0:307f417b029a | 730 | //pc.printf("\r\nfile_data is=%s",file_data); |
anupanchsheel | 0:307f417b029a | 731 | |
anupanchsheel | 0:307f417b029a | 732 | length=0; |
anupanchsheel | 0:307f417b029a | 733 | length=strlen(csq_value); |
anupanchsheel | 0:307f417b029a | 734 | |
anupanchsheel | 0:307f417b029a | 735 | for(int i=0; i<length;i++) |
anupanchsheel | 0:307f417b029a | 736 | { |
anupanchsheel | 0:307f417b029a | 737 | file_data[count]=csq_value[i]; |
anupanchsheel | 0:307f417b029a | 738 | count=count+1; |
anupanchsheel | 0:307f417b029a | 739 | } |
anupanchsheel | 0:307f417b029a | 740 | file_data[count]=32; |
anupanchsheel | 0:307f417b029a | 741 | count++; |
anupanchsheel | 0:307f417b029a | 742 | |
anupanchsheel | 0:307f417b029a | 743 | length=sprintf(avg_dc_detect1,"%d",avg_dc_detect[0]); |
anupanchsheel | 0:307f417b029a | 744 | for(int i=0; i<length;i++) //avg_dc_detect[0] |
anupanchsheel | 0:307f417b029a | 745 | { |
anupanchsheel | 0:307f417b029a | 746 | file_data[count]=avg_dc_detect1[i]; |
anupanchsheel | 0:307f417b029a | 747 | count++; |
anupanchsheel | 0:307f417b029a | 748 | } |
anupanchsheel | 0:307f417b029a | 749 | file_data[count]=32; |
anupanchsheel | 0:307f417b029a | 750 | count++; |
anupanchsheel | 0:307f417b029a | 751 | |
anupanchsheel | 0:307f417b029a | 752 | |
anupanchsheel | 0:307f417b029a | 753 | length=0; |
anupanchsheel | 0:307f417b029a | 754 | length=sprintf(avg_dc_detect2,"%d",avg_dc_detect[1]); |
anupanchsheel | 0:307f417b029a | 755 | for(int i=0; i<length;i++) //avg_dc_detect[1] |
anupanchsheel | 0:307f417b029a | 756 | { |
anupanchsheel | 0:307f417b029a | 757 | file_data[count]=avg_dc_detect1[i]; |
anupanchsheel | 0:307f417b029a | 758 | count++; |
anupanchsheel | 0:307f417b029a | 759 | } |
anupanchsheel | 0:307f417b029a | 760 | file_data[count]=32; |
anupanchsheel | 0:307f417b029a | 761 | count++; |
anupanchsheel | 0:307f417b029a | 762 | |
anupanchsheel | 0:307f417b029a | 763 | length=0; |
anupanchsheel | 0:307f417b029a | 764 | length=sprintf(avg_temp1,"%d",avg_temp[0]); |
anupanchsheel | 0:307f417b029a | 765 | for(int i=0; i<length;i++) //avg_temp[0] |
anupanchsheel | 0:307f417b029a | 766 | { |
anupanchsheel | 0:307f417b029a | 767 | file_data[count]=avg_temp1[i]; |
anupanchsheel | 0:307f417b029a | 768 | count++; |
anupanchsheel | 0:307f417b029a | 769 | } |
anupanchsheel | 0:307f417b029a | 770 | file_data[count]=32; |
anupanchsheel | 0:307f417b029a | 771 | count++; |
anupanchsheel | 0:307f417b029a | 772 | |
anupanchsheel | 0:307f417b029a | 773 | length=0; |
anupanchsheel | 0:307f417b029a | 774 | length=sprintf(avg_temp2,"%d",avg_temp[1]); |
anupanchsheel | 0:307f417b029a | 775 | for(int i=0; i<length;i++) |
anupanchsheel | 0:307f417b029a | 776 | { |
anupanchsheel | 0:307f417b029a | 777 | file_data[count]=avg_temp2[i]; |
anupanchsheel | 0:307f417b029a | 778 | count++; |
anupanchsheel | 0:307f417b029a | 779 | } |
anupanchsheel | 0:307f417b029a | 780 | file_data[count]=32; |
anupanchsheel | 0:307f417b029a | 781 | count++; |
anupanchsheel | 0:307f417b029a | 782 | length=0; |
anupanchsheel | 0:307f417b029a | 783 | length=sprintf(AC_rms,"%.0f",AC_rms_value); |
anupanchsheel | 0:307f417b029a | 784 | for (int i=0; i<length; i++) //AC_rms_value |
anupanchsheel | 0:307f417b029a | 785 | { |
anupanchsheel | 0:307f417b029a | 786 | file_data[count]= AC_rms[i]; |
anupanchsheel | 0:307f417b029a | 787 | count++; |
anupanchsheel | 0:307f417b029a | 788 | } |
anupanchsheel | 0:307f417b029a | 789 | |
anupanchsheel | 0:307f417b029a | 790 | file_data[count]=13; |
anupanchsheel | 0:307f417b029a | 791 | count++; |
anupanchsheel | 0:307f417b029a | 792 | |
anupanchsheel | 0:307f417b029a | 793 | file_data[count]=10; |
anupanchsheel | 0:307f417b029a | 794 | count++; |
anupanchsheel | 0:307f417b029a | 795 | |
anupanchsheel | 0:307f417b029a | 796 | //FFT file write |
anupanchsheel | 0:307f417b029a | 797 | fp = fopen(pathfft, "w"); |
anupanchsheel | 0:307f417b029a | 798 | //pc.printf("\r\nopened path fft\r\n"); |
anupanchsheel | 0:307f417b029a | 799 | if(fp == NULL) |
anupanchsheel | 0:307f417b029a | 800 | { |
anupanchsheel | 0:307f417b029a | 801 | //pc.printf("Could not open file for write pathfft\n\r"); |
anupanchsheel | 0:307f417b029a | 802 | //reset_mod(); |
anupanchsheel | 0:307f417b029a | 803 | } |
anupanchsheel | 0:307f417b029a | 804 | else |
anupanchsheel | 0:307f417b029a | 805 | { fprintf(fp, "%s",time_stamp); |
anupanchsheel | 0:307f417b029a | 806 | fprintf(fp, " CSQ %s",csq_value); |
anupanchsheel | 0:307f417b029a | 807 | fprintf(fp, " AD1 %d",avg_dc_detect[0]); |
anupanchsheel | 0:307f417b029a | 808 | fprintf(fp, " AD2 %d",avg_dc_detect[1]); |
anupanchsheel | 0:307f417b029a | 809 | fprintf(fp, " AT1 %d",avg_temp[0]); |
anupanchsheel | 0:307f417b029a | 810 | fprintf(fp, " AT2 %d",avg_temp[1]); |
anupanchsheel | 0:307f417b029a | 811 | fprintf(fp, " %.0f",AC_rms_value); |
anupanchsheel | 0:307f417b029a | 812 | fclose(fp); |
anupanchsheel | 0:307f417b029a | 813 | //pc.printf("\r\n AC rms done write done"); |
anupanchsheel | 0:307f417b029a | 814 | } |
anupanchsheel | 0:307f417b029a | 815 | fft(fft_points,0,1,1); |
anupanchsheel | 0:307f417b029a | 816 | maxfreq=0; |
anupanchsheel | 0:307f417b029a | 817 | count=prepare_fft_array(fft_start_index,fft_points_count,fft_maximas_required,fft_segments,0,count); |
anupanchsheel | 0:307f417b029a | 818 | |
anupanchsheel | 0:307f417b029a | 819 | //TIME STAMP FILE WRITE FUNCTION |
anupanchsheel | 0:307f417b029a | 820 | if(gsmerr==0) |
anupanchsheel | 0:307f417b029a | 821 | { |
anupanchsheel | 0:307f417b029a | 822 | |
anupanchsheel | 0:307f417b029a | 823 | sprintf(timestampname,"TS%s%02d%02d%02d%02d.txt",sitename,year,month,date,hour); |
anupanchsheel | 0:307f417b029a | 824 | sprintf(timestamppath,"/sd/%s",timestampname); |
anupanchsheel | 0:307f417b029a | 825 | |
anupanchsheel | 0:307f417b029a | 826 | |
anupanchsheel | 0:307f417b029a | 827 | } |
anupanchsheel | 0:307f417b029a | 828 | else |
anupanchsheel | 0:307f417b029a | 829 | { |
anupanchsheel | 0:307f417b029a | 830 | |
anupanchsheel | 0:307f417b029a | 831 | sprintf(timestampname,"TS%sErr.txt",sitename); |
anupanchsheel | 0:307f417b029a | 832 | //pc.printf("\r\n%s",timestampname); |
anupanchsheel | 0:307f417b029a | 833 | sprintf(timestamppath,"/sd/%s",timestampname); |
anupanchsheel | 0:307f417b029a | 834 | |
anupanchsheel | 0:307f417b029a | 835 | } |
anupanchsheel | 0:307f417b029a | 836 | //pc.printf("\r\n Timestampname is %s\r\n",timestampname); |
anupanchsheel | 0:307f417b029a | 837 | FILE *fp13 = fopen(timestamppath,"a"); |
anupanchsheel | 0:307f417b029a | 838 | |
anupanchsheel | 0:307f417b029a | 839 | if(fp == NULL) |
anupanchsheel | 0:307f417b029a | 840 | { |
anupanchsheel | 0:307f417b029a | 841 | //pc.printf("Could not open file for write pathfft\n\r"); |
anupanchsheel | 0:307f417b029a | 842 | //reset_mod(); |
anupanchsheel | 0:307f417b029a | 843 | } |
anupanchsheel | 0:307f417b029a | 844 | else |
anupanchsheel | 0:307f417b029a | 845 | { |
anupanchsheel | 0:307f417b029a | 846 | fprintf(fp13,"\r\n%02d%02d %d %.0f %d",minute,second,avg_dc_detect[1]/100,AC_rms_value/100000,maxfreq); //Rushabh: avg_dc_detect[2] missing, need to accomodate |
anupanchsheel | 0:307f417b029a | 847 | //pc.printf("Value is \r\n%02d%02d %d %.0f %d",minute,second,avg_dc_detect,AC_rms_value,maxfreq); |
anupanchsheel | 0:307f417b029a | 848 | fclose(fp13); |
anupanchsheel | 0:307f417b029a | 849 | } |
anupanchsheel | 0:307f417b029a | 850 | //pc.printf("\r\nTimestamp.txt Storage Done\n\r"); |
anupanchsheel | 0:307f417b029a | 851 | |
anupanchsheel | 0:307f417b029a | 852 | //DC fft |
anupanchsheel | 0:307f417b029a | 853 | for(int i=0; i<fft_points; i++) |
anupanchsheel | 0:307f417b029a | 854 | { |
anupanchsheel | 0:307f417b029a | 855 | fft_array[i]=int(DCdata[i]); |
anupanchsheel | 0:307f417b029a | 856 | DC_mean_value=DC_mean_value+fft_array[i]; |
anupanchsheel | 0:307f417b029a | 857 | //pc.printf("%d %d %d\r\n", i, i+int(fft_points), fft_array[i]); |
anupanchsheel | 0:307f417b029a | 858 | fft_array[(i+int(fft_points))]=0; |
anupanchsheel | 0:307f417b029a | 859 | } |
anupanchsheel | 0:307f417b029a | 860 | //DC RMS |
anupanchsheel | 0:307f417b029a | 861 | DC_mean_value=DC_mean_value/fft_points; |
anupanchsheel | 0:307f417b029a | 862 | for(int i=0; i<fft_points; i++) |
anupanchsheel | 0:307f417b029a | 863 | { |
anupanchsheel | 0:307f417b029a | 864 | DC_rms_value=DC_rms_value+((DC_mean_value-fft_array[i])*(DC_mean_value-fft_array[i])/10000); |
anupanchsheel | 0:307f417b029a | 865 | } |
anupanchsheel | 0:307f417b029a | 866 | fp = fopen(pathfft, "a"); |
anupanchsheel | 0:307f417b029a | 867 | if(fp == NULL) |
anupanchsheel | 0:307f417b029a | 868 | { |
anupanchsheel | 0:307f417b029a | 869 | //pc.printf("Could not open file for write\n\r"); |
anupanchsheel | 0:307f417b029a | 870 | //reset_mod(); |
anupanchsheel | 0:307f417b029a | 871 | } |
anupanchsheel | 0:307f417b029a | 872 | else |
anupanchsheel | 0:307f417b029a | 873 | { |
anupanchsheel | 0:307f417b029a | 874 | fprintf(fp, "%.0f",DC_rms_value); |
anupanchsheel | 0:307f417b029a | 875 | fclose(fp); |
anupanchsheel | 0:307f417b029a | 876 | } |
anupanchsheel | 0:307f417b029a | 877 | length=0; |
anupanchsheel | 0:307f417b029a | 878 | length=sprintf(DC_rms,"%.0f",DC_rms_value); |
anupanchsheel | 0:307f417b029a | 879 | file_data[count]=10; |
anupanchsheel | 0:307f417b029a | 880 | count++; |
anupanchsheel | 0:307f417b029a | 881 | file_data[count]=13; |
anupanchsheel | 0:307f417b029a | 882 | count++; |
anupanchsheel | 0:307f417b029a | 883 | for (int i=0; i<length; i++) //DC_rms_value |
anupanchsheel | 0:307f417b029a | 884 | { |
anupanchsheel | 0:307f417b029a | 885 | file_data[count]= DC_rms[i]; |
anupanchsheel | 0:307f417b029a | 886 | count++; |
anupanchsheel | 0:307f417b029a | 887 | } |
anupanchsheel | 0:307f417b029a | 888 | file_data[count]=13; |
anupanchsheel | 0:307f417b029a | 889 | count++; |
anupanchsheel | 0:307f417b029a | 890 | file_data[count]=10; |
anupanchsheel | 0:307f417b029a | 891 | count++; |
anupanchsheel | 0:307f417b029a | 892 | |
anupanchsheel | 0:307f417b029a | 893 | fft(fft_points,0,1,1); |
anupanchsheel | 0:307f417b029a | 894 | count=prepare_fft_array(fft_start_index,fft_points_count,fft_maximas_required,fft_segments,1,count); |
anupanchsheel | 0:307f417b029a | 895 | file_data[count]=0; |
anupanchsheel | 0:307f417b029a | 896 | //pc.printf("\r\n file_data is=%s\n",file_data); |
anupanchsheel | 0:307f417b029a | 897 | } |
anupanchsheel | 0:307f417b029a | 898 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 899 | // GSM Initialization // |
anupanchsheel | 0:307f417b029a | 900 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 901 | bool gsm_init() |
anupanchsheel | 0:307f417b029a | 902 | { |
anupanchsheel | 0:307f417b029a | 903 | iteration_send_ATcommand("AT","OK",5,0,4) ; |
anupanchsheel | 0:307f417b029a | 904 | iteration_send_ATcommand("AT+CLTS=1","OK",5,0,4) ; |
anupanchsheel | 0:307f417b029a | 905 | iteration_send_ATcommand("AT+CPIN?","READY",5,0,4); |
anupanchsheel | 0:307f417b029a | 906 | for(int cnt=0;cnt<5;cnt++) //ATcommand with dual response comparison |
anupanchsheel | 0:307f417b029a | 907 | { |
anupanchsheel | 0:307f417b029a | 908 | if(sendATcommand2("AT+CREG?", "+CREG: 1,1", "+CREG: 1,5", 10)==0){ |
anupanchsheel | 0:307f417b029a | 909 | if(sendATcommand2("AT+CREG=1", "+CREG:1", "+CREG:5", 5)) |
anupanchsheel | 0:307f417b029a | 910 | { |
anupanchsheel | 0:307f417b029a | 911 | pc.printf("creg success"); |
anupanchsheel | 0:307f417b029a | 912 | iteration_send_ATcommand("AT+CGATT?","OK",5,0,4) ; |
anupanchsheel | 0:307f417b029a | 913 | iteration_send_ATcommand("AT+CGATT","OK",5,0,4) ; |
anupanchsheel | 0:307f417b029a | 914 | return 1; |
anupanchsheel | 0:307f417b029a | 915 | } |
anupanchsheel | 0:307f417b029a | 916 | } |
anupanchsheel | 0:307f417b029a | 917 | else return 1; |
anupanchsheel | 0:307f417b029a | 918 | } |
anupanchsheel | 0:307f417b029a | 919 | pc.printf("GSM registration failed"); |
anupanchsheel | 0:307f417b029a | 920 | return 0; |
anupanchsheel | 0:307f417b029a | 921 | } |
anupanchsheel | 0:307f417b029a | 922 | |
anupanchsheel | 0:307f417b029a | 923 | void reset(){ |
anupanchsheel | 0:307f417b029a | 924 | gsm_rst = 0 ; |
anupanchsheel | 0:307f417b029a | 925 | wait(1); |
anupanchsheel | 0:307f417b029a | 926 | gsm_rst = 1 ; |
anupanchsheel | 0:307f417b029a | 927 | wait(3); |
anupanchsheel | 0:307f417b029a | 928 | } |
anupanchsheel | 0:307f417b029a | 929 | |
anupanchsheel | 0:307f417b029a | 930 | |
anupanchsheel | 0:307f417b029a | 931 | void resetstore(){ |
anupanchsheel | 0:307f417b029a | 932 | if(sd.disk_initialize()==0) |
anupanchsheel | 0:307f417b029a | 933 | { |
anupanchsheel | 0:307f417b029a | 934 | sd_status_flag=1; |
anupanchsheel | 0:307f417b029a | 935 | pc.printf("sdstatus: %d \r\n",sd_status_flag); |
anupanchsheel | 0:307f417b029a | 936 | mkdir("/sd/jas",0777); |
anupanchsheel | 0:307f417b029a | 937 | FILE *fp = fopen("/sd/SiteInfo.txt","a"); |
anupanchsheel | 0:307f417b029a | 938 | if(fp == NULL) |
anupanchsheel | 0:307f417b029a | 939 | { |
anupanchsheel | 0:307f417b029a | 940 | pc.printf("Some issue with SD card"); |
anupanchsheel | 0:307f417b029a | 941 | //FILE *fp = fopen("/sd/SiteInfo.txt","w"); |
anupanchsheel | 0:307f417b029a | 942 | //fprintf(fp, "\r\n%s",time_stamp); |
anupanchsheel | 0:307f417b029a | 943 | |
anupanchsheel | 0:307f417b029a | 944 | } |
anupanchsheel | 0:307f417b029a | 945 | else |
anupanchsheel | 0:307f417b029a | 946 | { |
anupanchsheel | 0:307f417b029a | 947 | fprintf(fp, "\r\n%s",time_stamp); |
anupanchsheel | 0:307f417b029a | 948 | pc.printf("timestamp: \r\n%s",time_stamp); |
anupanchsheel | 0:307f417b029a | 949 | //fclose(fp); |
anupanchsheel | 0:307f417b029a | 950 | } |
anupanchsheel | 0:307f417b029a | 951 | fclose(fp); |
anupanchsheel | 0:307f417b029a | 952 | pc.printf("\r\n Timestamp saved"); |
anupanchsheel | 0:307f417b029a | 953 | } |
anupanchsheel | 0:307f417b029a | 954 | else{ |
anupanchsheel | 0:307f417b029a | 955 | sd_status_flag=0; |
anupanchsheel | 0:307f417b029a | 956 | pc.printf("sdstatus: %d \r\n",sd_status_flag); |
anupanchsheel | 0:307f417b029a | 957 | pc.printf("sdcardfailed"); |
anupanchsheel | 0:307f417b029a | 958 | } |
anupanchsheel | 0:307f417b029a | 959 | } |
anupanchsheel | 0:307f417b029a | 960 | |
anupanchsheel | 0:307f417b029a | 961 | bool tcp_connect(){ |
anupanchsheel | 0:307f417b029a | 962 | iteration_send_ATcommand("AT+CIPSTATUS","OK",5,0,4) ; |
anupanchsheel | 0:307f417b029a | 963 | iteration_send_ATcommand("AT+CIPSHUT","SHUT OK",5,0,4) ; |
anupanchsheel | 0:307f417b029a | 964 | iteration_send_ATcommand("AT+CIPSTATUS","OK",5,0,4) ; |
anupanchsheel | 0:307f417b029a | 965 | if(iteration_send_ATcommand("AT+CIPMUX=0","OK",5,0,4)){ |
anupanchsheel | 0:307f417b029a | 966 | if(iteration_send_ATcommand("AT+CIPSTART = \"TCP\",\"182.57.130.48\",11000","CONNECT OK",5,0,3)){ |
anupanchsheel | 0:307f417b029a | 967 | conn = 1; |
anupanchsheel | 0:307f417b029a | 968 | pc.printf("Conn established \r\n"); |
anupanchsheel | 0:307f417b029a | 969 | return 1 ; |
anupanchsheel | 0:307f417b029a | 970 | } |
anupanchsheel | 0:307f417b029a | 971 | } |
anupanchsheel | 0:307f417b029a | 972 | conn = 0 ; |
anupanchsheel | 0:307f417b029a | 973 | pc.printf("Conn failed \r\n"); |
anupanchsheel | 0:307f417b029a | 974 | return 0 ; |
anupanchsheel | 0:307f417b029a | 975 | } |
anupanchsheel | 0:307f417b029a | 976 | |
anupanchsheel | 0:307f417b029a | 977 | bool tcp_send(){ |
anupanchsheel | 0:307f417b029a | 978 | for(int j = 0 ; j<2;j++){ |
anupanchsheel | 0:307f417b029a | 979 | if(iteration_send_ATcommand("AT+CIPSEND","ERROR",2,0,4)){ |
anupanchsheel | 0:307f417b029a | 980 | for(int i =0;i<1294;i++){ |
anupanchsheel | 0:307f417b029a | 981 | if(file_data[i]==0){ |
anupanchsheel | 0:307f417b029a | 982 | break; |
anupanchsheel | 0:307f417b029a | 983 | } |
anupanchsheel | 0:307f417b029a | 984 | gsm.printf("%c", file_data[i]); //storing array in the file.. |
anupanchsheel | 0:307f417b029a | 985 | } |
anupanchsheel | 0:307f417b029a | 986 | if(iteration_send_ATcommand(" \x1A","SEND OK",7,0,4)){ |
anupanchsheel | 0:307f417b029a | 987 | return 1; |
anupanchsheel | 0:307f417b029a | 988 | } |
anupanchsheel | 0:307f417b029a | 989 | } |
anupanchsheel | 0:307f417b029a | 990 | } |
anupanchsheel | 0:307f417b029a | 991 | return 0 ; |
anupanchsheel | 0:307f417b029a | 992 | |
anupanchsheel | 0:307f417b029a | 993 | |
anupanchsheel | 0:307f417b029a | 994 | } |
anupanchsheel | 0:307f417b029a | 995 | |
anupanchsheel | 0:307f417b029a | 996 | void checkconn(){ |
anupanchsheel | 0:307f417b029a | 997 | for(int i = 0; i < 2;i++){ |
anupanchsheel | 0:307f417b029a | 998 | if(!conn){ |
anupanchsheel | 0:307f417b029a | 999 | tcp_connect(); |
anupanchsheel | 0:307f417b029a | 1000 | } |
anupanchsheel | 0:307f417b029a | 1001 | else{ |
anupanchsheel | 0:307f417b029a | 1002 | break; |
anupanchsheel | 0:307f417b029a | 1003 | } |
anupanchsheel | 0:307f417b029a | 1004 | } |
anupanchsheel | 0:307f417b029a | 1005 | } |
anupanchsheel | 0:307f417b029a | 1006 | |
anupanchsheel | 0:307f417b029a | 1007 | bool hourdetect(){ |
anupanchsheel | 0:307f417b029a | 1008 | if(old_hour!=100 && hour!=old_hour && hour<24){ |
anupanchsheel | 0:307f417b029a | 1009 | pc.printf("\r\nhour changed"); |
anupanchsheel | 0:307f417b029a | 1010 | //sprintf(filenamefft,"%s",timestampname_old); |
anupanchsheel | 0:307f417b029a | 1011 | //sprintf(pathfft,"%s",timestamppath_old); |
anupanchsheel | 0:307f417b029a | 1012 | if(sendATcommand("AT+CNETSCAN","OK",20,4)==2){ |
anupanchsheel | 0:307f417b029a | 1013 | pc.printf("\r\n Location_Sattus is=%s\r\n",location_status); |
anupanchsheel | 0:307f417b029a | 1014 | FILE *fp1 = fopen(timestamppath_old,"a"); |
anupanchsheel | 0:307f417b029a | 1015 | if(fp1 == NULL){ |
anupanchsheel | 0:307f417b029a | 1016 | printf("Issues with sdcard \r\n"); |
anupanchsheel | 0:307f417b029a | 1017 | } |
anupanchsheel | 0:307f417b029a | 1018 | else{ |
anupanchsheel | 0:307f417b029a | 1019 | fprintf(fp1,"\r\n%s",location_status); |
anupanchsheel | 0:307f417b029a | 1020 | |
anupanchsheel | 0:307f417b029a | 1021 | } |
anupanchsheel | 0:307f417b029a | 1022 | fclose(fp1); |
anupanchsheel | 0:307f417b029a | 1023 | } |
anupanchsheel | 0:307f417b029a | 1024 | old_hour=hour; |
anupanchsheel | 0:307f417b029a | 1025 | return 1; |
anupanchsheel | 0:307f417b029a | 1026 | } |
anupanchsheel | 0:307f417b029a | 1027 | return 0; |
anupanchsheel | 0:307f417b029a | 1028 | } |
anupanchsheel | 0:307f417b029a | 1029 | |
anupanchsheel | 0:307f417b029a | 1030 | void tsop_update(){ |
anupanchsheel | 0:307f417b029a | 1031 | sprintf(timestampname_old,"%s",timestampname); |
anupanchsheel | 0:307f417b029a | 1032 | sprintf(timestamppath_old,"%s",timestamppath); |
anupanchsheel | 0:307f417b029a | 1033 | |
anupanchsheel | 0:307f417b029a | 1034 | } |
anupanchsheel | 0:307f417b029a | 1035 | |
anupanchsheel | 0:307f417b029a | 1036 | bool hoursend(){ |
anupanchsheel | 0:307f417b029a | 1037 | FILE *fp8 = fopen(timestamppath_old, "r"); |
anupanchsheel | 0:307f417b029a | 1038 | if(fp8==NULL){ |
anupanchsheel | 0:307f417b029a | 1039 | pc.printf("\n\r SD issue\r\n"); |
anupanchsheel | 0:307f417b029a | 1040 | return 0; |
anupanchsheel | 0:307f417b029a | 1041 | } |
anupanchsheel | 0:307f417b029a | 1042 | else{ |
anupanchsheel | 0:307f417b029a | 1043 | memset(file_data,'\0',1294); |
anupanchsheel | 0:307f417b029a | 1044 | for(int i = 0; i <1294;i++){ |
anupanchsheel | 0:307f417b029a | 1045 | file_data[i] = fgetc(fp8); |
anupanchsheel | 0:307f417b029a | 1046 | if( feof(fp8) ){ |
anupanchsheel | 0:307f417b029a | 1047 | break ; |
anupanchsheel | 0:307f417b029a | 1048 | } |
anupanchsheel | 0:307f417b029a | 1049 | } |
anupanchsheel | 0:307f417b029a | 1050 | return 1 ; |
anupanchsheel | 0:307f417b029a | 1051 | } |
anupanchsheel | 0:307f417b029a | 1052 | } |
anupanchsheel | 0:307f417b029a | 1053 | |
anupanchsheel | 0:307f417b029a | 1054 | void hourstore(){ |
anupanchsheel | 0:307f417b029a | 1055 | FILE *fp9 = fopen("/sd/unsenthf.txt","a"); |
anupanchsheel | 0:307f417b029a | 1056 | fprintf(fp9,"%s\n",timestamppath_old); |
anupanchsheel | 0:307f417b029a | 1057 | fclose(fp9); |
anupanchsheel | 0:307f417b029a | 1058 | } |
anupanchsheel | 0:307f417b029a | 1059 | |
anupanchsheel | 0:307f417b029a | 1060 | void minutestore(){ |
anupanchsheel | 0:307f417b029a | 1061 | for(int i = 0 ; i < 5 ; i++){ |
anupanchsheel | 0:307f417b029a | 1062 | if(strcmp(unsent_mf[i],"\0")==0){ |
anupanchsheel | 0:307f417b029a | 1063 | // will write the filename in that array at ith index |
anupanchsheel | 0:307f417b029a | 1064 | strcpy(unsent_mf[i],pathfft); |
anupanchsheel | 0:307f417b029a | 1065 | return; |
anupanchsheel | 0:307f417b029a | 1066 | } |
anupanchsheel | 0:307f417b029a | 1067 | } |
anupanchsheel | 0:307f417b029a | 1068 | // if the array is full, overwrite the 0th index |
anupanchsheel | 0:307f417b029a | 1069 | strcpy(unsent_mf[0],pathfft); |
anupanchsheel | 0:307f417b029a | 1070 | return ; |
anupanchsheel | 0:307f417b029a | 1071 | } |
anupanchsheel | 0:307f417b029a | 1072 | |
anupanchsheel | 0:307f417b029a | 1073 | bool minutearraycheck(){ // reading the array |
anupanchsheel | 0:307f417b029a | 1074 | if(strcmp(unsent_mf[readindex_mf],"\0")==0){ // checking empty or not at readindex_mf |
anupanchsheel | 0:307f417b029a | 1075 | printf("All done \n\r"); |
anupanchsheel | 0:307f417b029a | 1076 | readindex_mf = 0 ; |
anupanchsheel | 0:307f417b029a | 1077 | return 1 ; |
anupanchsheel | 0:307f417b029a | 1078 | } |
anupanchsheel | 0:307f417b029a | 1079 | return 0 ; |
anupanchsheel | 0:307f417b029a | 1080 | } |
anupanchsheel | 0:307f417b029a | 1081 | |
anupanchsheel | 0:307f417b029a | 1082 | bool send1(char *filepath1){ |
anupanchsheel | 0:307f417b029a | 1083 | //file reading and sending |
anupanchsheel | 0:307f417b029a | 1084 | FILE *fp = fopen(filepath1,"r"); |
anupanchsheel | 0:307f417b029a | 1085 | if(fp == NULL) { |
anupanchsheel | 0:307f417b029a | 1086 | error("Could not open file for read\n"); |
anupanchsheel | 0:307f417b029a | 1087 | return 0; |
anupanchsheel | 0:307f417b029a | 1088 | } |
anupanchsheel | 0:307f417b029a | 1089 | |
anupanchsheel | 0:307f417b029a | 1090 | if(iteration_send_ATcommand("AT+CIPSEND","ERROR",2,0,4)){ |
anupanchsheel | 0:307f417b029a | 1091 | //gsm.printf(tstamp); |
anupanchsheel | 0:307f417b029a | 1092 | //gsm.printf("\n\rerror- %d\n",err); |
anupanchsheel | 0:307f417b029a | 1093 | while(1){ |
anupanchsheel | 0:307f417b029a | 1094 | int c; |
anupanchsheel | 0:307f417b029a | 1095 | c = fgetc(fp); |
anupanchsheel | 0:307f417b029a | 1096 | if( feof(fp) ){ |
anupanchsheel | 0:307f417b029a | 1097 | break; |
anupanchsheel | 0:307f417b029a | 1098 | } |
anupanchsheel | 0:307f417b029a | 1099 | gsm.printf("%c", c); |
anupanchsheel | 0:307f417b029a | 1100 | //printf("%c", c); |
anupanchsheel | 0:307f417b029a | 1101 | } |
anupanchsheel | 0:307f417b029a | 1102 | fclose(fp); |
anupanchsheel | 0:307f417b029a | 1103 | if(iteration_send_ATcommand("\x1A","SEND OK",10,0,2)){ |
anupanchsheel | 0:307f417b029a | 1104 | //iteration(e,"OK",5,2); |
anupanchsheel | 0:307f417b029a | 1105 | return 1; |
anupanchsheel | 0:307f417b029a | 1106 | } |
anupanchsheel | 0:307f417b029a | 1107 | } |
anupanchsheel | 0:307f417b029a | 1108 | return 0 ; |
anupanchsheel | 0:307f417b029a | 1109 | } |
anupanchsheel | 0:307f417b029a | 1110 | |
anupanchsheel | 0:307f417b029a | 1111 | void cyclicindexupdate(){ |
anupanchsheel | 0:307f417b029a | 1112 | if(readindex_mf < 4){ |
anupanchsheel | 0:307f417b029a | 1113 | readindex_mf ++ ; |
anupanchsheel | 0:307f417b029a | 1114 | } |
anupanchsheel | 0:307f417b029a | 1115 | else{ |
anupanchsheel | 0:307f417b029a | 1116 | readindex_mf = 0 ; |
anupanchsheel | 0:307f417b029a | 1117 | } |
anupanchsheel | 0:307f417b029a | 1118 | } |
anupanchsheel | 0:307f417b029a | 1119 | |
anupanchsheel | 0:307f417b029a | 1120 | void serverwrite(){ |
anupanchsheel | 0:307f417b029a | 1121 | char response2[1400]; // can change it later.. do not need that array |
anupanchsheel | 0:307f417b029a | 1122 | // if do not want to use it, then character by caharacter print in the file on sd card |
anupanchsheel | 0:307f417b029a | 1123 | strcpy(response2,&server_response[42]); |
anupanchsheel | 0:307f417b029a | 1124 | FILE *fp = fopen("/sd/anufinal.txt","w"); |
anupanchsheel | 0:307f417b029a | 1125 | fprintf(fp,response2); |
anupanchsheel | 0:307f417b029a | 1126 | fclose(fp); |
anupanchsheel | 0:307f417b029a | 1127 | } |
anupanchsheel | 0:307f417b029a | 1128 | |
anupanchsheel | 0:307f417b029a | 1129 | int getFileSize(FILE *fp) { |
anupanchsheel | 0:307f417b029a | 1130 | fseek(fp, 0, SEEK_END); |
anupanchsheel | 0:307f417b029a | 1131 | int size = ftell(fp); |
anupanchsheel | 0:307f417b029a | 1132 | fseek(fp, 0, SEEK_SET); |
anupanchsheel | 0:307f417b029a | 1133 | |
anupanchsheel | 0:307f417b029a | 1134 | return size; |
anupanchsheel | 0:307f417b029a | 1135 | } |
anupanchsheel | 0:307f417b029a | 1136 | |
anupanchsheel | 0:307f417b029a | 1137 | void counterupdate(){ |
anupanchsheel | 0:307f417b029a | 1138 | int counter; |
anupanchsheel | 0:307f417b029a | 1139 | FILE *fp = fopen("/sd/counter.txt","r+"); |
anupanchsheel | 0:307f417b029a | 1140 | //fseek(fp,0,SEEK_SET); |
anupanchsheel | 0:307f417b029a | 1141 | fscanf(fp,"%d",&counter); |
anupanchsheel | 0:307f417b029a | 1142 | counter++ ; |
anupanchsheel | 0:307f417b029a | 1143 | rewind(fp); |
anupanchsheel | 0:307f417b029a | 1144 | fprintf(fp,"%d",counter); |
anupanchsheel | 0:307f417b029a | 1145 | fclose(fp); |
anupanchsheel | 0:307f417b029a | 1146 | printf("New value of the counter: %d \n\r",counter); |
anupanchsheel | 0:307f417b029a | 1147 | printf("Counter updated !!!!!!\n\r" ); |
anupanchsheel | 0:307f417b029a | 1148 | } |
anupanchsheel | 0:307f417b029a | 1149 | |
anupanchsheel | 0:307f417b029a | 1150 | bool countercheck(){ |
anupanchsheel | 0:307f417b029a | 1151 | int counter; |
anupanchsheel | 0:307f417b029a | 1152 | FILE *fp = fopen("/sd/counter.txt","r+"); |
anupanchsheel | 0:307f417b029a | 1153 | fscanf(fp,"%d",&counter); |
anupanchsheel | 0:307f417b029a | 1154 | if(counter > 8){ |
anupanchsheel | 0:307f417b029a | 1155 | counter = 0 ; |
anupanchsheel | 0:307f417b029a | 1156 | rewind(fp); |
anupanchsheel | 0:307f417b029a | 1157 | printf("hello\n\r"); |
anupanchsheel | 0:307f417b029a | 1158 | printf("%d\n\r",counter); |
anupanchsheel | 0:307f417b029a | 1159 | fprintf(fp,"%d",counter); |
anupanchsheel | 0:307f417b029a | 1160 | fclose(fp); |
anupanchsheel | 0:307f417b029a | 1161 | return 1; |
anupanchsheel | 0:307f417b029a | 1162 | } |
anupanchsheel | 0:307f417b029a | 1163 | fclose(fp); |
anupanchsheel | 0:307f417b029a | 1164 | return 0; |
anupanchsheel | 0:307f417b029a | 1165 | } |
anupanchsheel | 0:307f417b029a | 1166 | |
anupanchsheel | 0:307f417b029a | 1167 | void filerename(char *oldfile,char *newfile){ |
anupanchsheel | 0:307f417b029a | 1168 | char ch; |
anupanchsheel | 0:307f417b029a | 1169 | FILE *fpold = fopen(oldfile, "r"); |
anupanchsheel | 0:307f417b029a | 1170 | FILE *fpnew = fopen(newfile, "w"); |
anupanchsheel | 0:307f417b029a | 1171 | while (1) { |
anupanchsheel | 0:307f417b029a | 1172 | ch = fgetc(fpold); |
anupanchsheel | 0:307f417b029a | 1173 | if (feof(fpold)) break; |
anupanchsheel | 0:307f417b029a | 1174 | fputc(ch, fpnew); |
anupanchsheel | 0:307f417b029a | 1175 | } |
anupanchsheel | 0:307f417b029a | 1176 | fclose(fpnew); |
anupanchsheel | 0:307f417b029a | 1177 | fclose(fpold); |
anupanchsheel | 0:307f417b029a | 1178 | } |
anupanchsheel | 0:307f417b029a | 1179 | |
anupanchsheel | 0:307f417b029a | 1180 | |
anupanchsheel | 0:307f417b029a | 1181 | void unsenthourfileupdate(){ |
anupanchsheel | 0:307f417b029a | 1182 | char c ; |
anupanchsheel | 0:307f417b029a | 1183 | int filepathlength = 22; |
anupanchsheel | 0:307f417b029a | 1184 | FILE *fp = fopen("/sd/unsenthf.txt","r"); |
anupanchsheel | 0:307f417b029a | 1185 | int counter = 9 ; |
anupanchsheel | 0:307f417b029a | 1186 | fseek(fp,counter*filepathlength,SEEK_SET); |
anupanchsheel | 0:307f417b029a | 1187 | FILE *fp1 = fopen("/sd/unsent1hf.txt","w"); |
anupanchsheel | 0:307f417b029a | 1188 | while(1){ |
anupanchsheel | 0:307f417b029a | 1189 | c = fgetc(fp); |
anupanchsheel | 0:307f417b029a | 1190 | if(feof(fp)){ |
anupanchsheel | 0:307f417b029a | 1191 | break; |
anupanchsheel | 0:307f417b029a | 1192 | } |
anupanchsheel | 0:307f417b029a | 1193 | else { |
anupanchsheel | 0:307f417b029a | 1194 | fputc(c, fp1); |
anupanchsheel | 0:307f417b029a | 1195 | } |
anupanchsheel | 0:307f417b029a | 1196 | } |
anupanchsheel | 0:307f417b029a | 1197 | fclose(fp1); |
anupanchsheel | 0:307f417b029a | 1198 | fclose(fp); |
anupanchsheel | 0:307f417b029a | 1199 | int ret = remove("/sd/unsenthf.txt"); |
anupanchsheel | 0:307f417b029a | 1200 | if(ret == 0) { |
anupanchsheel | 0:307f417b029a | 1201 | printf("File deleted successfully"); |
anupanchsheel | 0:307f417b029a | 1202 | } |
anupanchsheel | 0:307f417b029a | 1203 | else{ |
anupanchsheel | 0:307f417b029a | 1204 | printf("Error: unable to delete the file"); |
anupanchsheel | 0:307f417b029a | 1205 | } |
anupanchsheel | 0:307f417b029a | 1206 | filerename("/sd/unsent1hf.txt","/sd/unsenthf.txt"); |
anupanchsheel | 0:307f417b029a | 1207 | printf("File Renamed"); |
anupanchsheel | 0:307f417b029a | 1208 | remove("/sd/unsent1hf.txt"); |
anupanchsheel | 0:307f417b029a | 1209 | FILE *fpu = fopen("/sd/unsenthf.txt","r"); |
anupanchsheel | 0:307f417b029a | 1210 | while(true){ |
anupanchsheel | 0:307f417b029a | 1211 | char c = fgetc(fpu); |
anupanchsheel | 0:307f417b029a | 1212 | if(feof(fpu)){ |
anupanchsheel | 0:307f417b029a | 1213 | break; |
anupanchsheel | 0:307f417b029a | 1214 | } |
anupanchsheel | 0:307f417b029a | 1215 | printf("%c",c); |
anupanchsheel | 0:307f417b029a | 1216 | } |
anupanchsheel | 0:307f417b029a | 1217 | fclose(fpu); |
anupanchsheel | 0:307f417b029a | 1218 | } |
anupanchsheel | 0:307f417b029a | 1219 | |
anupanchsheel | 0:307f417b029a | 1220 | bool readsend_hf(){ |
anupanchsheel | 0:307f417b029a | 1221 | int counter; |
anupanchsheel | 0:307f417b029a | 1222 | FILE *fp = fopen("/sd/counter.txt","r"); |
anupanchsheel | 0:307f417b029a | 1223 | if(fp == NULL){ |
anupanchsheel | 0:307f417b029a | 1224 | FILE *fp1 = fopen("/sd/counter.txt","w"); |
anupanchsheel | 0:307f417b029a | 1225 | fprintf(fp1,"%d",0); |
anupanchsheel | 0:307f417b029a | 1226 | fclose(fp1); |
anupanchsheel | 0:307f417b029a | 1227 | counter = 0; |
anupanchsheel | 0:307f417b029a | 1228 | printf("countervalue: %d\n\r",counter); |
anupanchsheel | 0:307f417b029a | 1229 | } |
anupanchsheel | 0:307f417b029a | 1230 | else{ |
anupanchsheel | 0:307f417b029a | 1231 | fscanf(fp,"%d", &counter); |
anupanchsheel | 0:307f417b029a | 1232 | fclose(fp); |
anupanchsheel | 0:307f417b029a | 1233 | printf("countervalue: %d\n\r",counter); |
anupanchsheel | 0:307f417b029a | 1234 | } |
anupanchsheel | 0:307f417b029a | 1235 | fclose(fp); |
anupanchsheel | 0:307f417b029a | 1236 | int filepathlength = 22; // file path length 23 including the "\n", one more character |
anupanchsheel | 0:307f417b029a | 1237 | char filepath2[24]; |
anupanchsheel | 0:307f417b029a | 1238 | |
anupanchsheel | 0:307f417b029a | 1239 | FILE *fpu = fopen("/sd/unsenthf.txt","r"); |
anupanchsheel | 0:307f417b029a | 1240 | printf("Filesize: %d",getFileSize(fpu)); // if want to use feof(fpu) file should open in read mode only |
anupanchsheel | 0:307f417b029a | 1241 | |
anupanchsheel | 0:307f417b029a | 1242 | if(getFileSize(fpu)==(counter * filepathlength)){ |
anupanchsheel | 0:307f417b029a | 1243 | //memset(filename,'\0',10); |
anupanchsheel | 0:307f417b029a | 1244 | printf("file is empty or readup all the names\n\r"); |
anupanchsheel | 0:307f417b029a | 1245 | fclose(fpu); |
anupanchsheel | 0:307f417b029a | 1246 | return 0 ; |
anupanchsheel | 0:307f417b029a | 1247 | } |
anupanchsheel | 0:307f417b029a | 1248 | else{ |
anupanchsheel | 0:307f417b029a | 1249 | fseek(fpu,counter*filepathlength,SEEK_SET); |
anupanchsheel | 0:307f417b029a | 1250 | fscanf(fpu,"%s",filepath2); |
anupanchsheel | 0:307f417b029a | 1251 | printf("Unsentfilepath %s\n\r",filepath2); |
anupanchsheel | 0:307f417b029a | 1252 | fclose(fpu); |
anupanchsheel | 0:307f417b029a | 1253 | } |
anupanchsheel | 0:307f417b029a | 1254 | if(send1(filepath2)){ |
anupanchsheel | 0:307f417b029a | 1255 | return 1; |
anupanchsheel | 0:307f417b029a | 1256 | } |
anupanchsheel | 0:307f417b029a | 1257 | else { |
anupanchsheel | 0:307f417b029a | 1258 | conn = 0; |
anupanchsheel | 0:307f417b029a | 1259 | } |
anupanchsheel | 0:307f417b029a | 1260 | //fclose(fpu); |
anupanchsheel | 0:307f417b029a | 1261 | return 0 ; |
anupanchsheel | 0:307f417b029a | 1262 | } |
anupanchsheel | 0:307f417b029a | 1263 | |
anupanchsheel | 0:307f417b029a | 1264 | bool rmnngstk(){ |
anupanchsheel | 0:307f417b029a | 1265 | pc.printf("enters rmnngstk function \r\n"); |
anupanchsheel | 0:307f417b029a | 1266 | if(conn){ |
anupanchsheel | 0:307f417b029a | 1267 | //hour function |
anupanchsheel | 0:307f417b029a | 1268 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 1269 | // hour function // |
anupanchsheel | 0:307f417b029a | 1270 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 1271 | if(readsend_hf()){ |
anupanchsheel | 0:307f417b029a | 1272 | counterupdate(); // counter update |
anupanchsheel | 0:307f417b029a | 1273 | if(countercheck()){ //counter check with update to counter = 0 again |
anupanchsheel | 0:307f417b029a | 1274 | unsenthourfileupdate(); //unsent update |
anupanchsheel | 0:307f417b029a | 1275 | } |
anupanchsheel | 0:307f417b029a | 1276 | return 1; |
anupanchsheel | 0:307f417b029a | 1277 | } |
anupanchsheel | 0:307f417b029a | 1278 | if(!conn){ |
anupanchsheel | 0:307f417b029a | 1279 | return 0; |
anupanchsheel | 0:307f417b029a | 1280 | } |
anupanchsheel | 0:307f417b029a | 1281 | |
anupanchsheel | 0:307f417b029a | 1282 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 1283 | // minute function // |
anupanchsheel | 0:307f417b029a | 1284 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 1285 | if(!minutearraycheck()){ // check that no more files to send.. |
anupanchsheel | 0:307f417b029a | 1286 | if(send1(unsent_mf[readindex_mf])){ |
anupanchsheel | 0:307f417b029a | 1287 | // get inside if success in sending |
anupanchsheel | 0:307f417b029a | 1288 | printf("before changing minute file path: %s \r\n",unsent_mf[readindex_mf]); |
anupanchsheel | 0:307f417b029a | 1289 | strcpy(unsent_mf[readindex_mf],'\0'); // removing the entry |
anupanchsheel | 0:307f417b029a | 1290 | printf("after changing minute file path: %s \r\n",unsent_mf[readindex_mf]); |
anupanchsheel | 0:307f417b029a | 1291 | cyclicindexupdate(); // update the arraqy index position |
anupanchsheel | 0:307f417b029a | 1292 | return 1; |
anupanchsheel | 0:307f417b029a | 1293 | } |
anupanchsheel | 0:307f417b029a | 1294 | else{ |
anupanchsheel | 0:307f417b029a | 1295 | printf("Connection error"); |
anupanchsheel | 0:307f417b029a | 1296 | conn = 0; |
anupanchsheel | 0:307f417b029a | 1297 | } |
anupanchsheel | 0:307f417b029a | 1298 | return 0; |
anupanchsheel | 0:307f417b029a | 1299 | } |
anupanchsheel | 0:307f417b029a | 1300 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 1301 | // server request function // |
anupanchsheel | 0:307f417b029a | 1302 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 1303 | if(iteration_send_ATcommand("AT+CIPSEND","ERROR",2,0,4)){ |
anupanchsheel | 0:307f417b029a | 1304 | if(iteration_send_ATcommand("Any_File\x1A","SEND OK",10,0,2)){ |
anupanchsheel | 0:307f417b029a | 1305 | //iteration("AT+CIPRXGET?","OK",3,1); |
anupanchsheel | 0:307f417b029a | 1306 | if(iteration_send_ATcommand("AT+CIPRXGET=2,1400","OK",5,5,2)){ |
anupanchsheel | 0:307f417b029a | 1307 | if(strstr(server_response, "File does not exist") != NULL){ |
anupanchsheel | 0:307f417b029a | 1308 | pc.printf("\r\n No further action.. \r \n"); |
anupanchsheel | 0:307f417b029a | 1309 | } |
anupanchsheel | 0:307f417b029a | 1310 | else{ |
anupanchsheel | 0:307f417b029a | 1311 | serverwrite(); |
anupanchsheel | 0:307f417b029a | 1312 | pc.printf("FILE DONE>> \r\n"); |
anupanchsheel | 0:307f417b029a | 1313 | } |
anupanchsheel | 0:307f417b029a | 1314 | } |
anupanchsheel | 0:307f417b029a | 1315 | } |
anupanchsheel | 0:307f417b029a | 1316 | } |
anupanchsheel | 0:307f417b029a | 1317 | else{ |
anupanchsheel | 0:307f417b029a | 1318 | conn = 0; |
anupanchsheel | 0:307f417b029a | 1319 | } |
anupanchsheel | 0:307f417b029a | 1320 | |
anupanchsheel | 0:307f417b029a | 1321 | // ALL DONE |
anupanchsheel | 0:307f417b029a | 1322 | //--------------------------------------------------------------------------------------------------// |
anupanchsheel | 0:307f417b029a | 1323 | |
anupanchsheel | 0:307f417b029a | 1324 | return 0; |
anupanchsheel | 0:307f417b029a | 1325 | } |
anupanchsheel | 0:307f417b029a | 1326 | return 0 ; |
anupanchsheel | 0:307f417b029a | 1327 | } |
anupanchsheel | 0:307f417b029a | 1328 | |
anupanchsheel | 0:307f417b029a | 1329 | void trmsnt_check(){ |
anupanchsheel | 0:307f417b029a | 1330 | while(1){ |
anupanchsheel | 0:307f417b029a | 1331 | if(trmsn.read()<14){ |
anupanchsheel | 0:307f417b029a | 1332 | if(!rmnngstk()){ // remaining stock |
anupanchsheel | 0:307f417b029a | 1333 | return ; |
anupanchsheel | 0:307f417b029a | 1334 | } |
anupanchsheel | 0:307f417b029a | 1335 | } |
anupanchsheel | 0:307f417b029a | 1336 | else{ |
anupanchsheel | 0:307f417b029a | 1337 | return; |
anupanchsheel | 0:307f417b029a | 1338 | } |
anupanchsheel | 0:307f417b029a | 1339 | } |
anupanchsheel | 0:307f417b029a | 1340 | } |
anupanchsheel | 0:307f417b029a | 1341 | |
anupanchsheel | 0:307f417b029a | 1342 | void server_network_init(){ |
anupanchsheel | 0:307f417b029a | 1343 | iteration_send_ATcommand("AT+CIPRXGET=?","OK",2,0,2); |
anupanchsheel | 0:307f417b029a | 1344 | iteration_send_ATcommand("AT+CIPRXGET?","OK",2,0,2); |
anupanchsheel | 0:307f417b029a | 1345 | iteration_send_ATcommand("AT+CIPRXGET=1","OK",2,0,2); |
anupanchsheel | 0:307f417b029a | 1346 | } |
anupanchsheel | 0:307f417b029a | 1347 | |
anupanchsheel | 0:307f417b029a | 1348 | void autodelete(){ |
anupanchsheel | 0:307f417b029a | 1349 | char month_dir[100]=""; |
anupanchsheel | 0:307f417b029a | 1350 | sprintf(directory,"/sd/ |
anupanchsheel | 0:307f417b029a | 1351 | int a = remove |
anupanchsheel | 0:307f417b029a | 1352 | |
anupanchsheel | 0:307f417b029a | 1353 | } |
anupanchsheel | 0:307f417b029a | 1354 | |
anupanchsheel | 0:307f417b029a | 1355 | |
anupanchsheel | 0:307f417b029a | 1356 | int main(){ |
anupanchsheel | 0:307f417b029a | 1357 | //int del = 0 ; |
anupanchsheel | 0:307f417b029a | 1358 | if(13<hour<15){ |
anupanchsheel | 0:307f417b029a | 1359 | autodelete(); // function will be executed every day min 1 time , max 2 times. |
anupanchsheel | 0:307f417b029a | 1360 | pc.printf("Autodelete function executed.."); |
anupanchsheel | 0:307f417b029a | 1361 | } |
anupanchsheel | 0:307f417b029a | 1362 | |
anupanchsheel | 0:307f417b029a | 1363 | int loopcount = 0; |
anupanchsheel | 0:307f417b029a | 1364 | pc.printf("Hello World \r\n"); |
anupanchsheel | 0:307f417b029a | 1365 | |
anupanchsheel | 0:307f417b029a | 1366 | reset(); |
anupanchsheel | 0:307f417b029a | 1367 | pc.printf("Reset done \r\n"); |
anupanchsheel | 0:307f417b029a | 1368 | |
anupanchsheel | 0:307f417b029a | 1369 | if(gsm_init() == 0){ |
anupanchsheel | 0:307f417b029a | 1370 | reset_mod(); |
anupanchsheel | 0:307f417b029a | 1371 | } |
anupanchsheel | 0:307f417b029a | 1372 | pc.printf("Initalization Done \r\n"); |
anupanchsheel | 0:307f417b029a | 1373 | |
anupanchsheel | 0:307f417b029a | 1374 | server_network_init(); |
anupanchsheel | 0:307f417b029a | 1375 | pc.printf("Server Initalization Done \r\n"); |
anupanchsheel | 0:307f417b029a | 1376 | |
anupanchsheel | 0:307f417b029a | 1377 | GetFileDir(); |
anupanchsheel | 0:307f417b029a | 1378 | pc.printf("getfiledir function called up.. \r\n"); |
anupanchsheel | 0:307f417b029a | 1379 | |
anupanchsheel | 0:307f417b029a | 1380 | resetstore(); |
anupanchsheel | 0:307f417b029a | 1381 | pc.printf("Resetstoretimedone \r\n"); |
anupanchsheel | 0:307f417b029a | 1382 | |
anupanchsheel | 0:307f417b029a | 1383 | tcp_connect(); // two attempts for establsihing the connection... |
anupanchsheel | 0:307f417b029a | 1384 | pc.printf("tcp_connect functiondone \r\n") ; |
anupanchsheel | 0:307f417b029a | 1385 | |
anupanchsheel | 0:307f417b029a | 1386 | while(1){ |
anupanchsheel | 0:307f417b029a | 1387 | file_data[1293]=0; |
anupanchsheel | 0:307f417b029a | 1388 | |
anupanchsheel | 0:307f417b029a | 1389 | sampling(); |
anupanchsheel | 0:307f417b029a | 1390 | pc.printf("Samplingdone \r\n"); |
anupanchsheel | 0:307f417b029a | 1391 | |
anupanchsheel | 0:307f417b029a | 1392 | store(); |
anupanchsheel | 0:307f417b029a | 1393 | pc.printf("storedone \r\n"); |
anupanchsheel | 0:307f417b029a | 1394 | |
anupanchsheel | 0:307f417b029a | 1395 | trmsn.reset(); |
anupanchsheel | 0:307f417b029a | 1396 | trmsn.start(); |
anupanchsheel | 0:307f417b029a | 1397 | pc.printf("timerstarted \r\n"); |
anupanchsheel | 0:307f417b029a | 1398 | |
anupanchsheel | 0:307f417b029a | 1399 | checkconn(); |
anupanchsheel | 0:307f417b029a | 1400 | //if connection not there make two attempts.. |
anupanchsheel | 0:307f417b029a | 1401 | pc.printf("checkconn functiondone \r\n"); |
anupanchsheel | 0:307f417b029a | 1402 | |
anupanchsheel | 0:307f417b029a | 1403 | // this is to send the minute file.. |
anupanchsheel | 0:307f417b029a | 1404 | if(conn){ |
anupanchsheel | 0:307f417b029a | 1405 | if(!tcp_send()){ |
anupanchsheel | 0:307f417b029a | 1406 | conn = 0; |
anupanchsheel | 0:307f417b029a | 1407 | minutestore(); |
anupanchsheel | 0:307f417b029a | 1408 | pc.printf("minutestore function done \r\n"); |
anupanchsheel | 0:307f417b029a | 1409 | } |
anupanchsheel | 0:307f417b029a | 1410 | } |
anupanchsheel | 0:307f417b029a | 1411 | else{ |
anupanchsheel | 0:307f417b029a | 1412 | minutestore(); |
anupanchsheel | 0:307f417b029a | 1413 | pc.printf("minutestore function done \r\n"); |
anupanchsheel | 0:307f417b029a | 1414 | |
anupanchsheel | 0:307f417b029a | 1415 | } |
anupanchsheel | 0:307f417b029a | 1416 | pc.printf("minutefilesend function done \r\n"); |
anupanchsheel | 0:307f417b029a | 1417 | |
anupanchsheel | 0:307f417b029a | 1418 | // have to use after minutefile send because of file_data[] modifiacation here |
anupanchsheel | 0:307f417b029a | 1419 | if(hourdetect()){ |
anupanchsheel | 0:307f417b029a | 1420 | if(conn){ |
anupanchsheel | 0:307f417b029a | 1421 | if(hoursend()){ // function to send the hour file.. |
anupanchsheel | 0:307f417b029a | 1422 | if(!tcp_send()){ // if unsuccess then set of instructions.. |
anupanchsheel | 0:307f417b029a | 1423 | conn = 0 ; |
anupanchsheel | 0:307f417b029a | 1424 | hourstore(); |
anupanchsheel | 0:307f417b029a | 1425 | pc.printf("hourstore function done \r\n"); |
anupanchsheel | 0:307f417b029a | 1426 | } |
anupanchsheel | 0:307f417b029a | 1427 | } |
anupanchsheel | 0:307f417b029a | 1428 | } |
anupanchsheel | 0:307f417b029a | 1429 | else{ |
anupanchsheel | 0:307f417b029a | 1430 | hourstore(); // in case of failure, hour file store.. |
anupanchsheel | 0:307f417b029a | 1431 | pc.printf("hourstore function done \r\n"); |
anupanchsheel | 0:307f417b029a | 1432 | } |
anupanchsheel | 0:307f417b029a | 1433 | tsop_update(); // timestamp old parameters updated |
anupanchsheel | 0:307f417b029a | 1434 | } |
anupanchsheel | 0:307f417b029a | 1435 | pc.printf("\r\n detecthour condition check out \r\n"); |
anupanchsheel | 0:307f417b029a | 1436 | pc.printf("Transmiision time till now: %f ", trmsn.read()); |
anupanchsheel | 0:307f417b029a | 1437 | |
anupanchsheel | 0:307f417b029a | 1438 | trmsnt_check(); // transmission time check |
anupanchsheel | 0:307f417b029a | 1439 | pc.printf("\r\n transmission time check out \r\n"); |
anupanchsheel | 0:307f417b029a | 1440 | |
anupanchsheel | 0:307f417b029a | 1441 | trmsn.stop(); |
anupanchsheel | 0:307f417b029a | 1442 | pc.printf("timerstopped \r\n"); |
anupanchsheel | 0:307f417b029a | 1443 | |
anupanchsheel | 0:307f417b029a | 1444 | |
anupanchsheel | 0:307f417b029a | 1445 | if((hour%2>0 && minute>3 && minute<6) || loopcount>120){ |
anupanchsheel | 0:307f417b029a | 1446 | loopcount=0; |
anupanchsheel | 0:307f417b029a | 1447 | reset_mod(); |
anupanchsheel | 0:307f417b029a | 1448 | } |
anupanchsheel | 0:307f417b029a | 1449 | } |
anupanchsheel | 0:307f417b029a | 1450 | |
anupanchsheel | 0:307f417b029a | 1451 | |
anupanchsheel | 0:307f417b029a | 1452 | } |