teste de publish
Dependencies: DS1820 HighSpeedAnalogIn devices mbed
main.cpp@22:cb832a9bc704, 2018-02-06 (annotated)
- Committer:
- brunofgc
- Date:
- Tue Feb 06 22:07:10 2018 +0000
- Revision:
- 22:cb832a9bc704
- Parent:
- 21:b9315cdd9275
- Child:
- 23:f3c87afb6548
Dando Pau em reprogramar (devices.cfg)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
brunofgc | 0:1c0a769988ee | 1 | #include "mbed.h" //API mBed com diversos drivers para os periféricos LPC1768 |
brunofgc | 0:1c0a769988ee | 2 | #include "stdio.h" |
brunofgc | 0:1c0a769988ee | 3 | #include "cmsis_os.h" //Nucleo do Real Time Operational System |
brunofgc | 0:1c0a769988ee | 4 | #include "main.h" //Inicializações de maquina e demais ajustes de hardware |
brunofgc | 0:1c0a769988ee | 5 | #include "funcoesSDCard.h" //Funções para manipulação do SDCard que incluem rotinas de inicialização, setar pastas e arquivos e demais usos |
brunofgc | 0:1c0a769988ee | 6 | #include "RTC.h" //API de uso das interrupções do Real Time Clock |
brunofgc | 0:1c0a769988ee | 7 | #include "serialPC.h" //Funções de comunicação entre RAD e PC via usb |
brunofgc | 0:1c0a769988ee | 8 | #include "modbusMaster1.h" |
brunofgc | 13:b9183b4bc049 | 9 | //#include "lpc17xx_nvic.h" |
brunofgc | 13:b9183b4bc049 | 10 | |
brunofgc | 0:1c0a769988ee | 11 | |
brunofgc | 7:ae9c47f62946 | 12 | //Definições de Hardware acessÃveis em qualquer modulo |
brunofgc | 7:ae9c47f62946 | 13 | /*LEDs de informação visual*/ |
brunofgc | 7:ae9c47f62946 | 14 | DigitalOut ledLigado(P1_0); |
brunofgc | 7:ae9c47f62946 | 15 | DigitalOut ledEmComunicacao(P1_1); |
brunofgc | 7:ae9c47f62946 | 16 | DigitalOut ledConectadoInternet(P1_4); |
brunofgc | 7:ae9c47f62946 | 17 | DigitalOut ledUsoGeral(P1_8); |
brunofgc | 7:ae9c47f62946 | 18 | /*LEDs de informação visual*/ |
brunofgc | 7:ae9c47f62946 | 19 | |
brunofgc | 7:ae9c47f62946 | 20 | /*Pinos de controle do modulo WiFi*/ |
brunofgc | 0:1c0a769988ee | 21 | DigitalOut ESP_CH_PD(P0_6); |
brunofgc | 0:1c0a769988ee | 22 | DigitalOut ESP_RESET(P0_0); |
brunofgc | 0:1c0a769988ee | 23 | DigitalOut ESP_ESPGPIO_2(P0_1); |
brunofgc | 7:ae9c47f62946 | 24 | /*Pinos de controle do modulo WiFi*/ |
brunofgc | 0:1c0a769988ee | 25 | |
brunofgc | 7:ae9c47f62946 | 26 | /*Interrupções externas para contagem de pulsos rapidos*/ |
brunofgc | 6:d4ebbaaba295 | 27 | InterruptIn eInt1(P2_11); |
brunofgc | 6:d4ebbaaba295 | 28 | InterruptIn eInt2(P2_12); |
brunofgc | 6:d4ebbaaba295 | 29 | InterruptIn eInt3(P2_13); |
brunofgc | 7:ae9c47f62946 | 30 | /*Interrupções externas para contagem de pulsos rapidos*/ |
brunofgc | 6:d4ebbaaba295 | 31 | |
brunofgc | 21:b9315cdd9275 | 32 | /*Leituras analógicas*/ |
brunofgc | 21:b9315cdd9275 | 33 | /*AnalogIn ain1(p17); |
brunofgc | 21:b9315cdd9275 | 34 | AnalogIn ain2(p18); |
brunofgc | 21:b9315cdd9275 | 35 | AnalogIn ain3(p19); |
brunofgc | 21:b9315cdd9275 | 36 | AnalogIn ain4(p20);*/ |
brunofgc | 21:b9315cdd9275 | 37 | /*Leituras analógicas*/ |
brunofgc | 21:b9315cdd9275 | 38 | |
brunofgc | 7:ae9c47f62946 | 39 | //Função para reset do ARM |
brunofgc | 0:1c0a769988ee | 40 | extern "C" void mbed_reset(); |
brunofgc | 0:1c0a769988ee | 41 | |
brunofgc | 0:1c0a769988ee | 42 | //Devices |
brunofgc | 0:1c0a769988ee | 43 | device ** dispositivos = NULL; |
brunofgc | 0:1c0a769988ee | 44 | uint8_t devices=0; |
brunofgc | 0:1c0a769988ee | 45 | uint8_t alarmes=0; |
brunofgc | 2:55b7b466e742 | 46 | uint8_t leituras=0; |
brunofgc | 0:1c0a769988ee | 47 | uint8_t qtdSchedules=0; |
brunofgc | 5:7801f913384e | 48 | uint8_t qtdScheduleExceptions=0; |
brunofgc | 0:1c0a769988ee | 49 | //Devices |
brunofgc | 0:1c0a769988ee | 50 | |
brunofgc | 0:1c0a769988ee | 51 | //Variáveis para envio de arquivo em qualquer lugar do código |
brunofgc | 0:1c0a769988ee | 52 | extern arquivoSD *arquivoEnvioPointer = NULL; |
brunofgc | 0:1c0a769988ee | 53 | |
brunofgc | 7:ae9c47f62946 | 54 | uint32_t act; |
brunofgc | 7:ae9c47f62946 | 55 | bool boolExecAct; |
brunofgc | 7:ae9c47f62946 | 56 | bool inicializaModemBool; |
brunofgc | 21:b9315cdd9275 | 57 | char debug = false; |
brunofgc | 0:1c0a769988ee | 58 | bool hardwareReset = false; |
brunofgc | 0:1c0a769988ee | 59 | |
brunofgc | 0:1c0a769988ee | 60 | |
brunofgc | 0:1c0a769988ee | 61 | MediaMovel FiltroAI4(5); |
brunofgc | 0:1c0a769988ee | 62 | MediaMovel FiltroAI3(5); |
brunofgc | 0:1c0a769988ee | 63 | MediaMovel FiltroAI2(5); |
brunofgc | 0:1c0a769988ee | 64 | MediaMovel FiltroAI1(5); |
brunofgc | 0:1c0a769988ee | 65 | |
brunofgc | 0:1c0a769988ee | 66 | uint16_t aiFiltrada[4]; |
brunofgc | 0:1c0a769988ee | 67 | bool entradasDigitais[9]; |
brunofgc | 0:1c0a769988ee | 68 | |
brunofgc | 14:c0162ab2a951 | 69 | CircularBuffer bufModem(maxBufInModem) __attribute__ ((section("AHBSRAM0"))); |
brunofgc | 0:1c0a769988ee | 70 | CircularBuffer bufPC(128); |
brunofgc | 2:55b7b466e742 | 71 | CircularBuffer sdCardBuf(1024); |
brunofgc | 0:1c0a769988ee | 72 | |
brunofgc | 0:1c0a769988ee | 73 | uint8_t enviaDadosPorAlarme = 255; |
brunofgc | 2:55b7b466e742 | 74 | //bool reportaMudancaAoServer; |
brunofgc | 0:1c0a769988ee | 75 | bool executaComandoServer; |
brunofgc | 7:ae9c47f62946 | 76 | |
brunofgc | 0:1c0a769988ee | 77 | //typeConfig flashConfig; |
brunofgc | 0:1c0a769988ee | 78 | |
brunofgc | 7:ae9c47f62946 | 79 | //Instancia das portas Seriais do ARM |
brunofgc | 0:1c0a769988ee | 80 | Serial pc(USBTX, USBRX); //Instancia de recurso Serial uart pc |
brunofgc | 7:ae9c47f62946 | 81 | Serial modem(P0_10,P0_11); //Instancia de recurso Serial uart Modem |
brunofgc | 0:1c0a769988ee | 82 | DigitalOut max_de(P2_7); //Pino de habilitação max485 para porta Energy Metering ModBus |
brunofgc | 0:1c0a769988ee | 83 | Serial modBusMaster1Serial(P2_0,P2_1); //Recurso serial para porta Energy Metering ModBus |
brunofgc | 0:1c0a769988ee | 84 | |
brunofgc | 0:1c0a769988ee | 85 | /* |
brunofgc | 0:1c0a769988ee | 86 | //Interrupções externas e botões |
brunofgc | 0:1c0a769988ee | 87 | InterruptIn int_wh(P2_11); |
brunofgc | 0:1c0a769988ee | 88 | InterruptIn int_varh(P2_12); |
brunofgc | 0:1c0a769988ee | 89 | |
brunofgc | 0:1c0a769988ee | 90 | void whCount(void){ |
brunofgc | 0:1c0a769988ee | 91 | whled=1; |
brunofgc | 0:1c0a769988ee | 92 | wait(0.2); |
brunofgc | 0:1c0a769988ee | 93 | whled=0; |
brunofgc | 0:1c0a769988ee | 94 | } |
brunofgc | 0:1c0a769988ee | 95 | |
brunofgc | 0:1c0a769988ee | 96 | void varhCount(void){ |
brunofgc | 0:1c0a769988ee | 97 | varhled=1; |
brunofgc | 0:1c0a769988ee | 98 | wait(0.2); |
brunofgc | 0:1c0a769988ee | 99 | varhled=0; |
brunofgc | 0:1c0a769988ee | 100 | }*/ |
brunofgc | 0:1c0a769988ee | 101 | //Definições de Hardware acessÃveis em qualquer modulo |
brunofgc | 0:1c0a769988ee | 102 | |
brunofgc | 0:1c0a769988ee | 103 | /* |
brunofgc | 0:1c0a769988ee | 104 | void loadStandardConfig(){ |
brunofgc | 0:1c0a769988ee | 105 | strcpy(flashConfig.apnList[0],"claro.com.br"); |
brunofgc | 0:1c0a769988ee | 106 | strcpy(flashConfig.apnList[1],"zap.vivo.com.br"); |
brunofgc | 0:1c0a769988ee | 107 | strcpy(flashConfig.apnList[2],"tim.br"); |
brunofgc | 0:1c0a769988ee | 108 | strcpy(flashConfig.apnList[3],"generica.oi.com.br"); |
brunofgc | 0:1c0a769988ee | 109 | |
brunofgc | 0:1c0a769988ee | 110 | strcpy(flashConfig.login[0],"claro"); |
brunofgc | 0:1c0a769988ee | 111 | strcpy(flashConfig.login[1],"vivo"); |
brunofgc | 0:1c0a769988ee | 112 | strcpy(flashConfig.login[2],"tim"); |
brunofgc | 0:1c0a769988ee | 113 | strcpy(flashConfig.login[3],"oi"); |
brunofgc | 0:1c0a769988ee | 114 | |
brunofgc | 0:1c0a769988ee | 115 | strcpy(flashConfig.senha[0],"claro"); |
brunofgc | 0:1c0a769988ee | 116 | strcpy(flashConfig.senha[1],"vivo"); |
brunofgc | 0:1c0a769988ee | 117 | strcpy(flashConfig.senha[2],"tim"); |
brunofgc | 0:1c0a769988ee | 118 | strcpy(flashConfig.senha[3],"oi"); |
brunofgc | 0:1c0a769988ee | 119 | |
brunofgc | 2:55b7b466e742 | 120 | strcpy(flashConfig.serverAddress,serverAddress); |
brunofgc | 0:1c0a769988ee | 121 | |
brunofgc | 0:1c0a769988ee | 122 | strcpy(flashConfig.serverPort,"80"); |
brunofgc | 0:1c0a769988ee | 123 | |
brunofgc | 0:1c0a769988ee | 124 | flashConfig.periodoDeEnvioDeDadosMinutos=15; //Padrão 15 minutos |
brunofgc | 0:1c0a769988ee | 125 | flashConfig.ultimoEstadoLigadoDesligado=0; //Padrão de fábrica desligado |
brunofgc | 0:1c0a769988ee | 126 | } |
brunofgc | 0:1c0a769988ee | 127 | */ |
brunofgc | 0:1c0a769988ee | 128 | |
brunofgc | 0:1c0a769988ee | 129 | void leiturasDigitais(){ |
brunofgc | 0:1c0a769988ee | 130 | entradasDigitais[0] = ED1; |
brunofgc | 0:1c0a769988ee | 131 | entradasDigitais[1] = ED2; |
brunofgc | 0:1c0a769988ee | 132 | entradasDigitais[2] = ED3; |
brunofgc | 0:1c0a769988ee | 133 | entradasDigitais[3] = ED4; |
brunofgc | 0:1c0a769988ee | 134 | entradasDigitais[4] = ED5; |
brunofgc | 0:1c0a769988ee | 135 | entradasDigitais[5] = ED6; |
brunofgc | 0:1c0a769988ee | 136 | entradasDigitais[6] = ED7; |
brunofgc | 0:1c0a769988ee | 137 | entradasDigitais[7] = ED8; |
brunofgc | 0:1c0a769988ee | 138 | entradasDigitais[8] = ED9; |
brunofgc | 0:1c0a769988ee | 139 | } |
brunofgc | 0:1c0a769988ee | 140 | |
brunofgc | 21:b9315cdd9275 | 141 | HighSpeedAnalogIn ain(p17, p18, p19, p20); |
brunofgc | 0:1c0a769988ee | 142 | |
brunofgc | 0:1c0a769988ee | 143 | void leiturasAnalogicas(){ |
brunofgc | 21:b9315cdd9275 | 144 | static uint8_t leituraAtual; |
brunofgc | 21:b9315cdd9275 | 145 | switch(leituraAtual){ |
brunofgc | 21:b9315cdd9275 | 146 | case 0: |
brunofgc | 21:b9315cdd9275 | 147 | aiFiltrada[0] = FiltroAI1.get(ain.read_u16(p17)); |
brunofgc | 21:b9315cdd9275 | 148 | break; |
brunofgc | 21:b9315cdd9275 | 149 | case 1: |
brunofgc | 21:b9315cdd9275 | 150 | aiFiltrada[1] = FiltroAI2.get(ain.read_u16(p18)); |
brunofgc | 21:b9315cdd9275 | 151 | break; |
brunofgc | 21:b9315cdd9275 | 152 | case 2: |
brunofgc | 21:b9315cdd9275 | 153 | aiFiltrada[2] = FiltroAI3.get(ain.read_u16(p19)); |
brunofgc | 21:b9315cdd9275 | 154 | break; |
brunofgc | 21:b9315cdd9275 | 155 | case 3: |
brunofgc | 21:b9315cdd9275 | 156 | aiFiltrada[3] = FiltroAI4.get(ain.read_u16(p20)); |
brunofgc | 21:b9315cdd9275 | 157 | break; |
brunofgc | 0:1c0a769988ee | 158 | } |
brunofgc | 21:b9315cdd9275 | 159 | if(leituraAtual<3){leituraAtual++;} |
brunofgc | 21:b9315cdd9275 | 160 | else{leituraAtual=0;} |
brunofgc | 0:1c0a769988ee | 161 | } |
brunofgc | 0:1c0a769988ee | 162 | |
brunofgc | 21:b9315cdd9275 | 163 | void verificaSaidasDigitais(){ |
brunofgc | 21:b9315cdd9275 | 164 | SD_OE_R = 1; |
brunofgc | 21:b9315cdd9275 | 165 | /*if(SD_FAULT){ |
brunofgc | 21:b9315cdd9275 | 166 | pc.printf("Fault\r\n"); |
brunofgc | 21:b9315cdd9275 | 167 | }else{ |
brunofgc | 21:b9315cdd9275 | 168 | pc.printf("No Fault\r\n"); |
brunofgc | 21:b9315cdd9275 | 169 | }*/ |
brunofgc | 21:b9315cdd9275 | 170 | } |
brunofgc | 4:13ff9c81dc10 | 171 | |
brunofgc | 0:1c0a769988ee | 172 | //Thread timers é utilizada para dar start nos timers de dentro das ISRs de comunicação serial porque não é permitido inicializar osTimers de dentro de ISRs. |
brunofgc | 0:1c0a769988ee | 173 | osThreadId idThreadTimers; |
brunofgc | 0:1c0a769988ee | 174 | void threadTimers(void const *args){ |
brunofgc | 0:1c0a769988ee | 175 | while(true){ |
brunofgc | 0:1c0a769988ee | 176 | osSignalWait(0x1,1000); //Executo compulsoriamente mesmo que não haja sinal num periodo de 1 segundo |
brunofgc | 0:1c0a769988ee | 177 | if(serialPC::startTimer_pacote){ |
brunofgc | 0:1c0a769988ee | 178 | serialPC::startTimer_pacote = false; |
brunofgc | 2:55b7b466e742 | 179 | osTimerStart(serialPC::timer_pacote,2000); |
brunofgc | 0:1c0a769988ee | 180 | } |
brunofgc | 0:1c0a769988ee | 181 | if(serialModem::startTimer_pacote){ |
brunofgc | 0:1c0a769988ee | 182 | serialModem::startTimer_pacote = false; |
brunofgc | 0:1c0a769988ee | 183 | osTimerStart(serialModem::timer_pacote,modemCom::timeOutModem); |
brunofgc | 0:1c0a769988ee | 184 | } |
brunofgc | 0:1c0a769988ee | 185 | if(modBusMaster1::startThreadModBusMaster){ |
brunofgc | 0:1c0a769988ee | 186 | modBusMaster1::startThreadModBusMaster = false; |
brunofgc | 0:1c0a769988ee | 187 | osTimerStart(modBusMaster1::timer_pacote,4); |
brunofgc | 4:13ff9c81dc10 | 188 | } |
brunofgc | 0:1c0a769988ee | 189 | } |
brunofgc | 0:1c0a769988ee | 190 | } |
brunofgc | 9:cf406384efd9 | 191 | osThreadDef(threadTimers, osPriorityNormal, 128); |
brunofgc | 9:cf406384efd9 | 192 | //osThreadDef(threadTimers, osPriorityNormal, DEFAULT_STACK_SIZE/10); |
brunofgc | 0:1c0a769988ee | 193 | |
brunofgc | 0:1c0a769988ee | 194 | osThreadId idThreadProcessaPacote; |
brunofgc | 0:1c0a769988ee | 195 | void threadProcessaPacote(void const *args){ |
brunofgc | 0:1c0a769988ee | 196 | while(true){ |
brunofgc | 6:d4ebbaaba295 | 197 | osSignalWait(0x1,1000); //Executo compulsoriamente mesmo que não haja sinal num periodo de 1 segundo |
brunofgc | 4:13ff9c81dc10 | 198 | if(serialModem::processarPacote){ |
brunofgc | 4:13ff9c81dc10 | 199 | serialModem::processarPacote = false; |
brunofgc | 4:13ff9c81dc10 | 200 | modemCom::processaPacote(NULL); |
brunofgc | 21:b9315cdd9275 | 201 | } |
brunofgc | 4:13ff9c81dc10 | 202 | } |
brunofgc | 0:1c0a769988ee | 203 | } |
brunofgc | 21:b9315cdd9275 | 204 | osThreadDef(threadProcessaPacote, osPriorityNormal, DEFAULT_STACK_SIZE/2); |
brunofgc | 0:1c0a769988ee | 205 | |
brunofgc | 0:1c0a769988ee | 206 | |
brunofgc | 7:ae9c47f62946 | 207 | |
brunofgc | 2:55b7b466e742 | 208 | //Thread que gerencia as comunicacoes (DROME - SERVER) |
brunofgc | 0:1c0a769988ee | 209 | osThreadId idThreadComunicacoes; |
brunofgc | 0:1c0a769988ee | 210 | void threadComunicacoes(void const *args){ |
brunofgc | 0:1c0a769988ee | 211 | //Esta thread faz todo o tratamento de comunicação. |
brunofgc | 0:1c0a769988ee | 212 | |
brunofgc | 7:ae9c47f62946 | 213 | //char aux[30]; |
brunofgc | 7:ae9c47f62946 | 214 | while(true){ |
brunofgc | 19:5559f7570e6f | 215 | osSignalWait(0x1,500); //Executo compulsoriamente mesmo que não haja sinal num periodo de meio segundo |
brunofgc | 7:ae9c47f62946 | 216 | //pc.printf("Thread: COM.\r\n"); |
brunofgc | 15:0f78bf9c13ec | 217 | //if((modemCom::status.SRING)&&(!modemCom::status.serverConnected)){ |
brunofgc | 0:1c0a769988ee | 218 | |
brunofgc | 7:ae9c47f62946 | 219 | if(inicializaModemBool){ |
brunofgc | 7:ae9c47f62946 | 220 | inicializaModemBool = false; |
brunofgc | 7:ae9c47f62946 | 221 | modemCom::conectaWiFi(); |
brunofgc | 7:ae9c47f62946 | 222 | } |
brunofgc | 7:ae9c47f62946 | 223 | |
brunofgc | 7:ae9c47f62946 | 224 | if(hardwareReset){ |
brunofgc | 7:ae9c47f62946 | 225 | //Resetando |
brunofgc | 7:ae9c47f62946 | 226 | diversos::wdt.kick(0.1); |
brunofgc | 7:ae9c47f62946 | 227 | while(true); |
brunofgc | 7:ae9c47f62946 | 228 | } |
brunofgc | 6:d4ebbaaba295 | 229 | |
brunofgc | 18:1eefda1f7736 | 230 | if(eventosRTC::rotinaEnvioDeDados){ |
brunofgc | 0:1c0a769988ee | 231 | //Faço aqui o que for devido. |
brunofgc | 0:1c0a769988ee | 232 | pc.printf("Enviando dados de hora completa ao server.\n"); |
brunofgc | 0:1c0a769988ee | 233 | modemCom::status.emComunicacao = true; |
brunofgc | 7:ae9c47f62946 | 234 | //diversos::wdt.kick(300.0); |
brunofgc | 18:1eefda1f7736 | 235 | //Aproveitando ACT |
brunofgc | 17:9b0eecbacbaa | 236 | if(!modemCom::status.recebendoArquivoDoServer){ |
brunofgc | 18:1eefda1f7736 | 237 | eventosRTC::rotinaEnvioDeDados--; |
brunofgc | 18:1eefda1f7736 | 238 | modemCom::status.periodo = 900; |
brunofgc | 17:9b0eecbacbaa | 239 | if(modemCom::verificaConexao()){ |
brunofgc | 17:9b0eecbacbaa | 240 | //Estou conectado |
brunofgc | 18:1eefda1f7736 | 241 | if(modemCom::enviaDados()){ |
brunofgc | 18:1eefda1f7736 | 242 | eventosRTC::rotinaEnvioDeDados=0; |
brunofgc | 18:1eefda1f7736 | 243 | modemCom::status.periodo = modemCom::status.periodoConfiguracao; //Restaurando o período configurado no sistema |
brunofgc | 18:1eefda1f7736 | 244 | } |
brunofgc | 17:9b0eecbacbaa | 245 | } |
brunofgc | 17:9b0eecbacbaa | 246 | //diversos::wdt.kick(90.0); |
brunofgc | 17:9b0eecbacbaa | 247 | }else{ |
brunofgc | 17:9b0eecbacbaa | 248 | modemCom::status.recebendoArquivoDoServer--; |
brunofgc | 17:9b0eecbacbaa | 249 | } |
brunofgc | 9:cf406384efd9 | 250 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 251 | } |
brunofgc | 0:1c0a769988ee | 252 | |
brunofgc | 0:1c0a769988ee | 253 | if(arquivoEnvioPointer!=NULL){ |
brunofgc | 0:1c0a769988ee | 254 | pc.printf("Enviando o arquivo <%s> ao server.\n",arquivoEnvioPointer->nome); |
brunofgc | 18:1eefda1f7736 | 255 | if(modemCom::postFileCommandMode(modemCom::status.serverIP,modemCom::status.host,"/drome/parser/index.php",arquivoEnvioPointer)){ |
brunofgc | 0:1c0a769988ee | 256 | pc.printf("Enviado o arquivo <%s>.\n",arquivoEnvioPointer->nome); |
brunofgc | 0:1c0a769988ee | 257 | arquivoEnvioPointer = NULL; |
brunofgc | 0:1c0a769988ee | 258 | }else{ |
brunofgc | 0:1c0a769988ee | 259 | pc.printf("Erro enviando o arquivo <%s>.\n",arquivoEnvioPointer->nome); |
brunofgc | 0:1c0a769988ee | 260 | } |
brunofgc | 15:0f78bf9c13ec | 261 | } |
brunofgc | 15:0f78bf9c13ec | 262 | |
brunofgc | 15:0f78bf9c13ec | 263 | if(modemCom::status.SRINGsockEntrante){ |
brunofgc | 15:0f78bf9c13ec | 264 | modemCom::status.SRINGsockEntrante=false; |
brunofgc | 15:0f78bf9c13ec | 265 | modemCom::atendeSRING(modemCom::status.connIDServerCommand); |
brunofgc | 15:0f78bf9c13ec | 266 | } |
brunofgc | 15:0f78bf9c13ec | 267 | |
brunofgc | 7:ae9c47f62946 | 268 | modemCom::timeOutModem = 30; |
brunofgc | 0:1c0a769988ee | 269 | } |
brunofgc | 0:1c0a769988ee | 270 | } |
brunofgc | 9:cf406384efd9 | 271 | osThreadDef(threadComunicacoes, osPriorityNormal, 3072); |
brunofgc | 9:cf406384efd9 | 272 | //osThreadDef(threadComunicacoes, osPriorityNormal, (DEFAULT_STACK_SIZE*2)); |
brunofgc | 0:1c0a769988ee | 273 | |
brunofgc | 0:1c0a769988ee | 274 | void RTC_POR_SEGUNDO(void){ |
brunofgc | 0:1c0a769988ee | 275 | time_t seconds; |
brunofgc | 0:1c0a769988ee | 276 | seconds = time(NULL); |
brunofgc | 19:5559f7570e6f | 277 | osSignalSet(idThreadComunicacoes, 0x1); |
brunofgc | 0:1c0a769988ee | 278 | eventosRTC::rotina1segundo=true; |
brunofgc | 0:1c0a769988ee | 279 | |
brunofgc | 7:ae9c47f62946 | 280 | if(((seconds+5)%10)==0){ |
brunofgc | 19:5559f7570e6f | 281 | eventosRTC::rotina10Segundos = true; |
brunofgc | 0:1c0a769988ee | 282 | } |
brunofgc | 0:1c0a769988ee | 283 | |
brunofgc | 0:1c0a769988ee | 284 | if((seconds%60)==0){ |
brunofgc | 0:1c0a769988ee | 285 | eventosRTC::minutos = true; |
brunofgc | 0:1c0a769988ee | 286 | } |
brunofgc | 0:1c0a769988ee | 287 | |
brunofgc | 19:5559f7570e6f | 288 | if(((seconds-5)%modemCom::status.periodo)==0){ |
brunofgc | 9:cf406384efd9 | 289 | //if((seconds%300)==0){ |
brunofgc | 21:b9315cdd9275 | 290 | eventosRTC::rotinaEnvioDeDados = maxTentativasEnvioDados; |
brunofgc | 0:1c0a769988ee | 291 | } |
brunofgc | 0:1c0a769988ee | 292 | |
brunofgc | 0:1c0a769988ee | 293 | if((seconds%3600)==0){ //Atuando a cada 3 minutos |
brunofgc | 0:1c0a769988ee | 294 | //Entro aqui a cada hora |
brunofgc | 0:1c0a769988ee | 295 | eventosRTC::rotina1hora = true; |
brunofgc | 0:1c0a769988ee | 296 | } |
brunofgc | 0:1c0a769988ee | 297 | } |
brunofgc | 0:1c0a769988ee | 298 | |
brunofgc | 0:1c0a769988ee | 299 | //Timeout timeOutModem; |
brunofgc | 0:1c0a769988ee | 300 | void chamaProcessaPacotePeloTimeOut(){ |
brunofgc | 0:1c0a769988ee | 301 | modemCom::processaPacote(NULL); |
brunofgc | 0:1c0a769988ee | 302 | } |
brunofgc | 0:1c0a769988ee | 303 | |
brunofgc | 0:1c0a769988ee | 304 | void loadDevices(){ |
brunofgc | 0:1c0a769988ee | 305 | if(sdCard::abreArquivo(&sdCard::devices,"r")){ |
brunofgc | 0:1c0a769988ee | 306 | criaDevices(sdCard::devices.fp); |
brunofgc | 0:1c0a769988ee | 307 | if(devices){ |
brunofgc | 0:1c0a769988ee | 308 | configuraDevices(sdCard::devices.fp); |
brunofgc | 2:55b7b466e742 | 309 | printf("Dispositivos configurados.\n"); |
brunofgc | 0:1c0a769988ee | 310 | } |
brunofgc | 21:b9315cdd9275 | 311 | sdCard::fechaArquivo(&sdCard::devices); |
brunofgc | 2:55b7b466e742 | 312 | if(devices){testaTudoDevices();} |
brunofgc | 0:1c0a769988ee | 313 | } |
brunofgc | 0:1c0a769988ee | 314 | } |
brunofgc | 0:1c0a769988ee | 315 | |
brunofgc | 0:1c0a769988ee | 316 | void inicializaMaquina(){ |
brunofgc | 14:c0162ab2a951 | 317 | pwmPeriod = 100000; |
brunofgc | 0:1c0a769988ee | 318 | SD1.period_us(pwmPeriod); |
brunofgc | 0:1c0a769988ee | 319 | modemCom::atendendoWebServer = false; |
brunofgc | 14:c0162ab2a951 | 320 | serialPC::serialPC_init(); |
brunofgc | 22:cb832a9bc704 | 321 | pc.printf("Versao 06/01/2018.\r\n"); |
brunofgc | 0:1c0a769988ee | 322 | modemCom::timeOutModem = 30; |
brunofgc | 14:c0162ab2a951 | 323 | serialModem::serial_init(); |
brunofgc | 14:c0162ab2a951 | 324 | numeroDeBytesPorEnvio = 768; |
brunofgc | 14:c0162ab2a951 | 325 | modBusMaster1::modBusMaster(&modBusMaster1Serial,19200,&max_de); |
brunofgc | 14:c0162ab2a951 | 326 | inicializaSensoresTemperatura(); |
brunofgc | 0:1c0a769988ee | 327 | |
brunofgc | 0:1c0a769988ee | 328 | if(sdCard::init()){ |
brunofgc | 14:c0162ab2a951 | 329 | pc.printf("SD Card inicializado com sucesso.\n"); |
brunofgc | 0:1c0a769988ee | 330 | //Carregando a lista de dispositivos |
brunofgc | 0:1c0a769988ee | 331 | loadDevices(); |
brunofgc | 0:1c0a769988ee | 332 | }else{ |
brunofgc | 0:1c0a769988ee | 333 | pc.printf("Nao foi possivel inicializar o SD Card.\n"); |
brunofgc | 13:b9183b4bc049 | 334 | } |
brunofgc | 13:b9183b4bc049 | 335 | |
brunofgc | 13:b9183b4bc049 | 336 | |
brunofgc | 7:ae9c47f62946 | 337 | //Tick Timer por segundo |
brunofgc | 0:1c0a769988ee | 338 | RTC::attach(&RTC_POR_SEGUNDO, RTC::Second); |
brunofgc | 7:ae9c47f62946 | 339 | |
brunofgc | 7:ae9c47f62946 | 340 | /*Chamada de threads*/ |
brunofgc | 7:ae9c47f62946 | 341 | |
brunofgc | 7:ae9c47f62946 | 342 | //Thread timers |
brunofgc | 0:1c0a769988ee | 343 | idThreadTimers = osThreadCreate(osThread(threadTimers), NULL); |
brunofgc | 7:ae9c47f62946 | 344 | //Thread comunicacoes |
brunofgc | 0:1c0a769988ee | 345 | idThreadComunicacoes = osThreadCreate(osThread(threadComunicacoes), NULL); |
brunofgc | 7:ae9c47f62946 | 346 | //Thread processaPacote |
brunofgc | 0:1c0a769988ee | 347 | idThreadProcessaPacote = osThreadCreate(osThread(threadProcessaPacote), NULL); |
brunofgc | 7:ae9c47f62946 | 348 | |
brunofgc | 13:b9183b4bc049 | 349 | /*Chamada de threads*/ |
brunofgc | 0:1c0a769988ee | 350 | serialModem::configBaud(); |
brunofgc | 0:1c0a769988ee | 351 | } |
brunofgc | 0:1c0a769988ee | 352 | |
brunofgc | 0:1c0a769988ee | 353 | //Inicio do programa; |
brunofgc | 0:1c0a769988ee | 354 | int main() { |
brunofgc | 21:b9315cdd9275 | 355 | debug = false; |
brunofgc | 6:d4ebbaaba295 | 356 | eInt1.rise(&diversos::processaPulsosEDs); |
brunofgc | 6:d4ebbaaba295 | 357 | eInt2.rise(&diversos::processaPulsosEDs); |
brunofgc | 6:d4ebbaaba295 | 358 | eInt3.rise(&diversos::processaPulsosEDs); |
brunofgc | 6:d4ebbaaba295 | 359 | |
brunofgc | 13:b9183b4bc049 | 360 | diversos::wdt.kick(90.0); |
brunofgc | 0:1c0a769988ee | 361 | |
brunofgc | 0:1c0a769988ee | 362 | modem.baud(76800); |
brunofgc | 0:1c0a769988ee | 363 | ESP_CH_PD = 1; |
brunofgc | 0:1c0a769988ee | 364 | ESP_ESPGPIO_2 = 1; |
brunofgc | 0:1c0a769988ee | 365 | ESP_RESET = 1; |
brunofgc | 0:1c0a769988ee | 366 | |
brunofgc | 7:ae9c47f62946 | 367 | wait(2); |
brunofgc | 0:1c0a769988ee | 368 | inicializaMaquina(); |
brunofgc | 0:1c0a769988ee | 369 | modemCom::inicializaModem(); |
brunofgc | 21:b9315cdd9275 | 370 | if(!modemCom::verificaConexao()){ |
brunofgc | 21:b9315cdd9275 | 371 | modemCom::conectaWiFi(); |
brunofgc | 21:b9315cdd9275 | 372 | } |
brunofgc | 18:1eefda1f7736 | 373 | eventosRTC::rotinaEnvioDeDados=maxTentativasEnvioDados; |
brunofgc | 11:631bea162800 | 374 | sdCard::deleteSentFiles=false; |
brunofgc | 21:b9315cdd9275 | 375 | |
brunofgc | 0:1c0a769988ee | 376 | while(true){ |
brunofgc | 21:b9315cdd9275 | 377 | osDelay(100); |
brunofgc | 13:b9183b4bc049 | 378 | if(callBootLoader){ |
brunofgc | 13:b9183b4bc049 | 379 | diversos::wdt.kick(360.0); |
brunofgc | 13:b9183b4bc049 | 380 | SysTick->CTRL &= ~0x00000002; // disable SysTick interrupt |
brunofgc | 13:b9183b4bc049 | 381 | NVIC_DeInit(); |
brunofgc | 13:b9183b4bc049 | 382 | //__disable_irq(); |
brunofgc | 13:b9183b4bc049 | 383 | bootloader_fillUpFlash(&sdCard::newFirmware); |
brunofgc | 13:b9183b4bc049 | 384 | while( ((LPC_UART0->LSR >> 6) &0x1) == 0 ); |
brunofgc | 13:b9183b4bc049 | 385 | //__enable_irq(); |
brunofgc | 13:b9183b4bc049 | 386 | bootLoader(); |
brunofgc | 13:b9183b4bc049 | 387 | } |
brunofgc | 13:b9183b4bc049 | 388 | |
brunofgc | 7:ae9c47f62946 | 389 | //pc.printf("Thread: MAIN.\r\n"); |
brunofgc | 18:1eefda1f7736 | 390 | if((!modemCom::status.recebendoArquivoDoServer)&&(!modemCom::status.emComunicacao)){ |
brunofgc | 18:1eefda1f7736 | 391 | diversos::wdt.kick(); |
brunofgc | 2:55b7b466e742 | 392 | if(eventosRTC::minutos){ |
brunofgc | 2:55b7b466e742 | 393 | //Executo abaixo a rotina de 1 minuto |
brunofgc | 2:55b7b466e742 | 394 | writeReadingsToSD(); |
brunofgc | 15:0f78bf9c13ec | 395 | //if(!modemCom::status.emComunicacao){modemCom::verificaConexao();} |
brunofgc | 2:55b7b466e742 | 396 | eventosRTC::minutos = false; |
brunofgc | 2:55b7b466e742 | 397 | } |
brunofgc | 1:0e0967c88590 | 398 | |
brunofgc | 2:55b7b466e742 | 399 | if(eventosRTC::rotina1segundo){ |
brunofgc | 2:55b7b466e742 | 400 | eventosRTC::rotina1segundo = false; |
brunofgc | 2:55b7b466e742 | 401 | |
brunofgc | 2:55b7b466e742 | 402 | refreshSensoresTemperatura(); |
brunofgc | 2:55b7b466e742 | 403 | |
brunofgc | 2:55b7b466e742 | 404 | if(enviaDadosPorAlarme!=255){ |
brunofgc | 2:55b7b466e742 | 405 | enviaDadosPorAlarme--; |
brunofgc | 2:55b7b466e742 | 406 | if(enviaDadosPorAlarme==0){ |
brunofgc | 18:1eefda1f7736 | 407 | eventosRTC::rotinaEnvioDeDados=maxTentativasEnvioDados; |
brunofgc | 2:55b7b466e742 | 408 | enviaDadosPorAlarme=255; |
brunofgc | 2:55b7b466e742 | 409 | } |
brunofgc | 21:b9315cdd9275 | 410 | } |
brunofgc | 21:b9315cdd9275 | 411 | leiturasAnalogicas(); |
brunofgc | 21:b9315cdd9275 | 412 | verificaSaidasDigitais(); |
brunofgc | 21:b9315cdd9275 | 413 | leiturasDigitais(); |
brunofgc | 21:b9315cdd9275 | 414 | verifyAlarms(); |
brunofgc | 21:b9315cdd9275 | 415 | if(debug){ |
brunofgc | 21:b9315cdd9275 | 416 | pc.printf("\nMemoria RAM disponivel = %lu\n",diversos::memAvailable()); |
brunofgc | 21:b9315cdd9275 | 417 | pc.printf("\nEntrada 4 = %lu.\nEntrada 3 = %lu.\nEntrada 2 = %lu.\nEntrada 1 = %lu.\r\n",aiFiltrada[3],aiFiltrada[2],aiFiltrada[1],aiFiltrada[0]); |
brunofgc | 21:b9315cdd9275 | 418 | pc.printf("Pulsos <%lu><%lu><%lu>.\r\n",pulsosEDs[0],pulsosEDs[1],pulsosEDs[2]); |
brunofgc | 21:b9315cdd9275 | 419 | for(debug=0;debug<9;debug++){ |
brunofgc | 21:b9315cdd9275 | 420 | pc.printf("EntradaDigital[%u]=%u.\r\n",debug,(entradasDigitais[debug]>1)); |
brunofgc | 21:b9315cdd9275 | 421 | } |
brunofgc | 21:b9315cdd9275 | 422 | |
brunofgc | 21:b9315cdd9275 | 423 | } |
brunofgc | 2:55b7b466e742 | 424 | time_t seconds = time(NULL); |
brunofgc | 2:55b7b466e742 | 425 | printf("%s\n", ctime(&seconds)); |
brunofgc | 21:b9315cdd9275 | 426 | |
brunofgc | 2:55b7b466e742 | 427 | } |
brunofgc | 2:55b7b466e742 | 428 | |
brunofgc | 2:55b7b466e742 | 429 | if(eventosRTC::rotina10Segundos){ |
brunofgc | 2:55b7b466e742 | 430 | //Executo abaixo a rotina de 10 segundos |
brunofgc | 2:55b7b466e742 | 431 | eventosRTC::rotina10Segundos = false; |
brunofgc | 7:ae9c47f62946 | 432 | verifySchedules(); |
brunofgc | 7:ae9c47f62946 | 433 | } |
brunofgc | 11:631bea162800 | 434 | |
brunofgc | 11:631bea162800 | 435 | if(sdCard::deleteSentFiles){ |
brunofgc | 11:631bea162800 | 436 | pc.printf("Recebido comando para deletar arquivos enviados.\n"); |
brunofgc | 11:631bea162800 | 437 | sdCard::deleteBanks(sdCard::currentBankSending); |
brunofgc | 11:631bea162800 | 438 | if(sdCard::modificaCurrentBank(sdCard::currentBank,!sdCard::currentBankSending)){ |
brunofgc | 11:631bea162800 | 439 | sdCard::deleteSentFiles=false; |
brunofgc | 11:631bea162800 | 440 | } |
brunofgc | 11:631bea162800 | 441 | } |
brunofgc | 2:55b7b466e742 | 442 | } |
brunofgc | 0:1c0a769988ee | 443 | |
brunofgc | 0:1c0a769988ee | 444 | if(executaComandoServer){ |
brunofgc | 0:1c0a769988ee | 445 | executaComandoServer = false; |
brunofgc | 22:cb832a9bc704 | 446 | commands::exec(modemCom::status.connIDServerCommand); |
brunofgc | 22:cb832a9bc704 | 447 | if(!modemCom::status.recebendoArquivoDoServer){ |
brunofgc | 22:cb832a9bc704 | 448 | pc.printf("Fechando socket chave 4.\r\n"); |
brunofgc | 22:cb832a9bc704 | 449 | modemCom::closeConnection(modemCom::status.connIDServerCommand); |
brunofgc | 22:cb832a9bc704 | 450 | } |
brunofgc | 11:631bea162800 | 451 | } |
brunofgc | 6:d4ebbaaba295 | 452 | |
brunofgc | 0:1c0a769988ee | 453 | if(modemCom::atendendoWebServer){ |
brunofgc | 0:1c0a769988ee | 454 | modemCom::atendendoWebServer = false; |
brunofgc | 15:0f78bf9c13ec | 455 | pc.printf("Atendendo webServer na conexao %u.\r\n",modemCom::status.connIDWebServer); |
brunofgc | 7:ae9c47f62946 | 456 | modemCom::status.emComunicacao=true; |
brunofgc | 15:0f78bf9c13ec | 457 | modemCom::webServer(modemCom::status.connIDWebServer); |
brunofgc | 7:ae9c47f62946 | 458 | modemCom::status.emComunicacao=false; |
brunofgc | 7:ae9c47f62946 | 459 | } |
brunofgc | 2:55b7b466e742 | 460 | |
brunofgc | 7:ae9c47f62946 | 461 | if(boolExecAct){ |
brunofgc | 7:ae9c47f62946 | 462 | dispositivos[0]->execAct(act); |
brunofgc | 7:ae9c47f62946 | 463 | boolExecAct = false; |
brunofgc | 11:631bea162800 | 464 | } |
brunofgc | 0:1c0a769988ee | 465 | } |
brunofgc | 0:1c0a769988ee | 466 | } |