Saver nodo
Dependencies: ds3231 sd-driver-master
main.cpp@0:6c5b9198f119, 2018-06-15 (annotated)
- Committer:
- SolManB
- Date:
- Fri Jun 15 03:58:21 2018 +0000
- Revision:
- 0:6c5b9198f119
- Child:
- 1:dea5f9c332ba
Saver node
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SolManB | 0:6c5b9198f119 | 1 | #include "mbed.h" |
SolManB | 0:6c5b9198f119 | 2 | #include "ds3231.h" |
SolManB | 0:6c5b9198f119 | 3 | #include "SDBlockDevice.h" |
SolManB | 0:6c5b9198f119 | 4 | #include "FATFileSystem.h" |
SolManB | 0:6c5b9198f119 | 5 | |
SolManB | 0:6c5b9198f119 | 6 | |
SolManB | 0:6c5b9198f119 | 7 | AnalogIn x(p20); |
SolManB | 0:6c5b9198f119 | 8 | AnalogIn y(p19); |
SolManB | 0:6c5b9198f119 | 9 | AnalogIn z(p18); |
SolManB | 0:6c5b9198f119 | 10 | AnalogIn bat1(p17); |
SolManB | 0:6c5b9198f119 | 11 | AnalogIn bat2(p16); |
SolManB | 0:6c5b9198f119 | 12 | Ds3231 rtc(p28,p27); |
SolManB | 0:6c5b9198f119 | 13 | Serial pc(USBTX,USBRX); |
SolManB | 0:6c5b9198f119 | 14 | DigitalOut led2(LED2); |
SolManB | 0:6c5b9198f119 | 15 | SDBlockDevice sd(p5, p6, p7, p8); |
SolManB | 0:6c5b9198f119 | 16 | FATFileSystem fs("fs"); |
SolManB | 0:6c5b9198f119 | 17 | Serial xbee(p13,p14); |
SolManB | 0:6c5b9198f119 | 18 | |
SolManB | 0:6c5b9198f119 | 19 | Thread Lectura; |
SolManB | 0:6c5b9198f119 | 20 | |
SolManB | 0:6c5b9198f119 | 21 | Timer t; |
SolManB | 0:6c5b9198f119 | 22 | time_t epoch_time; |
SolManB | 0:6c5b9198f119 | 23 | char time1[3],fecha[18],nombre[30],dato1[100],dato[13]; |
SolManB | 0:6c5b9198f119 | 24 | int seg1, seg2=-1, milis,cel=25, en=0; |
SolManB | 0:6c5b9198f119 | 25 | unsigned short ax,ay,az; |
SolManB | 0:6c5b9198f119 | 26 | int b1, b2; |
SolManB | 0:6c5b9198f119 | 27 | |
SolManB | 0:6c5b9198f119 | 28 | |
SolManB | 0:6c5b9198f119 | 29 | float temp; |
SolManB | 0:6c5b9198f119 | 30 | FILE *fp; |
SolManB | 0:6c5b9198f119 | 31 | int i=0; |
SolManB | 0:6c5b9198f119 | 32 | |
SolManB | 0:6c5b9198f119 | 33 | |
SolManB | 0:6c5b9198f119 | 34 | void envio() |
SolManB | 0:6c5b9198f119 | 35 | { |
SolManB | 0:6c5b9198f119 | 36 | int r=0;//para el checsum |
SolManB | 0:6c5b9198f119 | 37 | for(int j=0;j<strlen(dato);j++) |
SolManB | 0:6c5b9198f119 | 38 | { |
SolManB | 0:6c5b9198f119 | 39 | r=r+int(dato[j]); |
SolManB | 0:6c5b9198f119 | 40 | |
SolManB | 0:6c5b9198f119 | 41 | } |
SolManB | 0:6c5b9198f119 | 42 | r=r+927;//esto se modifica segun los valores del coordinador |
SolManB | 0:6c5b9198f119 | 43 | if(r>4095) r=r%4096; |
SolManB | 0:6c5b9198f119 | 44 | if(r>255) r=r%256; |
SolManB | 0:6c5b9198f119 | 45 | int t=255-r; |
SolManB | 0:6c5b9198f119 | 46 | |
SolManB | 0:6c5b9198f119 | 47 | //para el tamano |
SolManB | 0:6c5b9198f119 | 48 | int q=14+strlen(dato); |
SolManB | 0:6c5b9198f119 | 49 | int z=int(q/256); |
SolManB | 0:6c5b9198f119 | 50 | int x=int(q%256); |
SolManB | 0:6c5b9198f119 | 51 | |
SolManB | 0:6c5b9198f119 | 52 | xbee.putc(0x7E); |
SolManB | 0:6c5b9198f119 | 53 | xbee.putc(char(z)); |
SolManB | 0:6c5b9198f119 | 54 | xbee.putc(char(x)); |
SolManB | 0:6c5b9198f119 | 55 | xbee.putc(0x10); |
SolManB | 0:6c5b9198f119 | 56 | xbee.putc(0x00); |
SolManB | 0:6c5b9198f119 | 57 | xbee.putc(0x00); |
SolManB | 0:6c5b9198f119 | 58 | xbee.putc(0x13); |
SolManB | 0:6c5b9198f119 | 59 | xbee.putc(0xA2); |
SolManB | 0:6c5b9198f119 | 60 | xbee.putc(0x00); |
SolManB | 0:6c5b9198f119 | 61 | xbee.putc(0x41); |
SolManB | 0:6c5b9198f119 | 62 | xbee.putc(0x02); |
SolManB | 0:6c5b9198f119 | 63 | xbee.putc(0x01); |
SolManB | 0:6c5b9198f119 | 64 | xbee.putc(0x99); |
SolManB | 0:6c5b9198f119 | 65 | xbee.putc(0xFF); |
SolManB | 0:6c5b9198f119 | 66 | xbee.putc(0xFE); |
SolManB | 0:6c5b9198f119 | 67 | xbee.putc(0x00); |
SolManB | 0:6c5b9198f119 | 68 | xbee.putc(0x00); |
SolManB | 0:6c5b9198f119 | 69 | xbee.putc(dato[0]); |
SolManB | 0:6c5b9198f119 | 70 | xbee.putc(dato[1]); |
SolManB | 0:6c5b9198f119 | 71 | xbee.putc(dato[2]); |
SolManB | 0:6c5b9198f119 | 72 | xbee.putc(dato[3]); |
SolManB | 0:6c5b9198f119 | 73 | xbee.putc(dato[4]); |
SolManB | 0:6c5b9198f119 | 74 | xbee.putc(dato[5]); |
SolManB | 0:6c5b9198f119 | 75 | xbee.putc(dato[6]); |
SolManB | 0:6c5b9198f119 | 76 | xbee.putc(dato[7]); |
SolManB | 0:6c5b9198f119 | 77 | xbee.putc(char(t)); |
SolManB | 0:6c5b9198f119 | 78 | |
SolManB | 0:6c5b9198f119 | 79 | } |
SolManB | 0:6c5b9198f119 | 80 | |
SolManB | 0:6c5b9198f119 | 81 | void Lect() |
SolManB | 0:6c5b9198f119 | 82 | { |
SolManB | 0:6c5b9198f119 | 83 | while(1) |
SolManB | 0:6c5b9198f119 | 84 | { |
SolManB | 0:6c5b9198f119 | 85 | ax=x.read_u16(); |
SolManB | 0:6c5b9198f119 | 86 | ay=y.read_u16(); |
SolManB | 0:6c5b9198f119 | 87 | az=z.read_u16(); |
SolManB | 0:6c5b9198f119 | 88 | b1 = (int)(bat1.read()*100.0f); |
SolManB | 0:6c5b9198f119 | 89 | b2 = (int)(bat2.read()*100.0f); |
SolManB | 0:6c5b9198f119 | 90 | |
SolManB | 0:6c5b9198f119 | 91 | epoch_time = rtc.get_epoch(); |
SolManB | 0:6c5b9198f119 | 92 | strftime(fecha, 19, "%d/%m/%y\t%H:%M:%S", localtime(&epoch_time)); |
SolManB | 0:6c5b9198f119 | 93 | strftime(time1,3,"%S", localtime(&epoch_time)); |
SolManB | 0:6c5b9198f119 | 94 | |
SolManB | 0:6c5b9198f119 | 95 | seg1=atoi(time1); |
SolManB | 0:6c5b9198f119 | 96 | if(seg1 != seg2){seg2=seg1;t.stop(); t.reset();t.start();} |
SolManB | 0:6c5b9198f119 | 97 | milis=t.read_ms(); |
SolManB | 0:6c5b9198f119 | 98 | |
SolManB | 0:6c5b9198f119 | 99 | //Sensor de temperatura |
SolManB | 0:6c5b9198f119 | 100 | int b=0; |
SolManB | 0:6c5b9198f119 | 101 | if(b1<b2) |
SolManB | 0:6c5b9198f119 | 102 | b=b1/10; |
SolManB | 0:6c5b9198f119 | 103 | else |
SolManB | 0:6c5b9198f119 | 104 | b=b2/10; |
SolManB | 0:6c5b9198f119 | 105 | sprintf(dato1,"%d\t%d\t%d\t%s\t%d\n",ax,ay,az,fecha,milis); |
SolManB | 0:6c5b9198f119 | 106 | //sprintf(dato,"%c%c%c%c%c%c%c%c",(int)(ax/256),(int)(ax%256),(int)(ay/256),(int)(ay%256),(int)(az/256),(int)(az%256),(b),(char)((int)(milis/10))); |
SolManB | 0:6c5b9198f119 | 107 | //envio(); |
SolManB | 0:6c5b9198f119 | 108 | if(i==0)//Archivo de crear la rutina |
SolManB | 0:6c5b9198f119 | 109 | { |
SolManB | 0:6c5b9198f119 | 110 | strftime(nombre,30,"/fs/%y%m%d_%H%M%S.txt", localtime(&epoch_time)); |
SolManB | 0:6c5b9198f119 | 111 | fp= fopen(nombre, "a+"); |
SolManB | 0:6c5b9198f119 | 112 | if(fp == NULL) error("Could not open file for write\n"); |
SolManB | 0:6c5b9198f119 | 113 | } |
SolManB | 0:6c5b9198f119 | 114 | |
SolManB | 0:6c5b9198f119 | 115 | fprintf(fp,dato1); |
SolManB | 0:6c5b9198f119 | 116 | |
SolManB | 0:6c5b9198f119 | 117 | i++; |
SolManB | 0:6c5b9198f119 | 118 | if(i==6000)//Para cerrar el archivo |
SolManB | 0:6c5b9198f119 | 119 | { |
SolManB | 0:6c5b9198f119 | 120 | fclose(fp); |
SolManB | 0:6c5b9198f119 | 121 | i=0; |
SolManB | 0:6c5b9198f119 | 122 | led2=!led2; |
SolManB | 0:6c5b9198f119 | 123 | } |
SolManB | 0:6c5b9198f119 | 124 | Thread::wait(9); |
SolManB | 0:6c5b9198f119 | 125 | } |
SolManB | 0:6c5b9198f119 | 126 | } |
SolManB | 0:6c5b9198f119 | 127 | |
SolManB | 0:6c5b9198f119 | 128 | int main() |
SolManB | 0:6c5b9198f119 | 129 | { |
SolManB | 0:6c5b9198f119 | 130 | sd.init(); |
SolManB | 0:6c5b9198f119 | 131 | fs.mount(&sd); |
SolManB | 0:6c5b9198f119 | 132 | xbee.baud(115200); |
SolManB | 0:6c5b9198f119 | 133 | pc.baud(921600); |
SolManB | 0:6c5b9198f119 | 134 | Lectura.start(callback(Lect)); |
SolManB | 0:6c5b9198f119 | 135 | while(1) |
SolManB | 0:6c5b9198f119 | 136 | {} |
SolManB | 0:6c5b9198f119 | 137 | sd.deinit(); |
SolManB | 0:6c5b9198f119 | 138 | fs.unmount(); |
SolManB | 0:6c5b9198f119 | 139 | } |