teste de publish
Dependencies: DS1820 HighSpeedAnalogIn devices mbed
main.cpp@0:1c0a769988ee, 2017-03-24 (annotated)
- Committer:
- brunofgc
- Date:
- Fri Mar 24 15:54:41 2017 +0000
- Revision:
- 0:1c0a769988ee
- Child:
- 1:0e0967c88590
Saidas digitais com pwm ok, entradas analogicas ok
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 | 0:1c0a769988ee | 9 | |
brunofgc | 0:1c0a769988ee | 10 | DigitalOut ESP_CH_PD(P0_6); |
brunofgc | 0:1c0a769988ee | 11 | DigitalOut ESP_RESET(P0_0); |
brunofgc | 0:1c0a769988ee | 12 | DigitalOut ESP_ESPGPIO_2(P0_1); |
brunofgc | 0:1c0a769988ee | 13 | |
brunofgc | 0:1c0a769988ee | 14 | uint32_t pwmPeriod; |
brunofgc | 0:1c0a769988ee | 15 | |
brunofgc | 0:1c0a769988ee | 16 | extern "C" void mbed_reset(); |
brunofgc | 0:1c0a769988ee | 17 | |
brunofgc | 0:1c0a769988ee | 18 | //Devices |
brunofgc | 0:1c0a769988ee | 19 | device ** dispositivos = NULL; |
brunofgc | 0:1c0a769988ee | 20 | uint8_t devices=0; |
brunofgc | 0:1c0a769988ee | 21 | uint8_t alarmes=0; |
brunofgc | 0:1c0a769988ee | 22 | uint8_t qtdSchedules=0; |
brunofgc | 0:1c0a769988ee | 23 | //Devices |
brunofgc | 0:1c0a769988ee | 24 | |
brunofgc | 0:1c0a769988ee | 25 | //Variáveis para envio de arquivo em qualquer lugar do código |
brunofgc | 0:1c0a769988ee | 26 | extern arquivoSD *arquivoEnvioPointer = NULL; |
brunofgc | 0:1c0a769988ee | 27 | |
brunofgc | 0:1c0a769988ee | 28 | //Definições de Hardware acessÃveis em qualquer modulo |
brunofgc | 0:1c0a769988ee | 29 | DigitalOut led1(P1_0); |
brunofgc | 0:1c0a769988ee | 30 | DigitalOut led2(P1_1); |
brunofgc | 0:1c0a769988ee | 31 | DigitalOut led3(P1_4); |
brunofgc | 0:1c0a769988ee | 32 | DigitalOut led4(P1_8); |
brunofgc | 0:1c0a769988ee | 33 | bool hardwareReset = false; |
brunofgc | 0:1c0a769988ee | 34 | |
brunofgc | 0:1c0a769988ee | 35 | /* |
brunofgc | 0:1c0a769988ee | 36 | AnalogIn ai4(p20); |
brunofgc | 0:1c0a769988ee | 37 | AnalogIn ai3(p19); |
brunofgc | 0:1c0a769988ee | 38 | AnalogIn ai2(p18); |
brunofgc | 0:1c0a769988ee | 39 | AnalogIn ai1(p17); |
brunofgc | 0:1c0a769988ee | 40 | |
brunofgc | 0:1c0a769988ee | 41 | |
brunofgc | 0:1c0a769988ee | 42 | AnalogIn ai4(P1_31); |
brunofgc | 0:1c0a769988ee | 43 | AnalogIn ai3(P1_30); |
brunofgc | 0:1c0a769988ee | 44 | AnalogIn ai2(P0_26); |
brunofgc | 0:1c0a769988ee | 45 | AnalogIn ai1(P0_25); |
brunofgc | 0:1c0a769988ee | 46 | */ |
brunofgc | 0:1c0a769988ee | 47 | |
brunofgc | 0:1c0a769988ee | 48 | MediaMovel FiltroAI4(5); |
brunofgc | 0:1c0a769988ee | 49 | MediaMovel FiltroAI3(5); |
brunofgc | 0:1c0a769988ee | 50 | MediaMovel FiltroAI2(5); |
brunofgc | 0:1c0a769988ee | 51 | MediaMovel FiltroAI1(5); |
brunofgc | 0:1c0a769988ee | 52 | |
brunofgc | 0:1c0a769988ee | 53 | uint16_t aiFiltrada[4]; |
brunofgc | 0:1c0a769988ee | 54 | bool entradasDigitais[9]; |
brunofgc | 0:1c0a769988ee | 55 | |
brunofgc | 0:1c0a769988ee | 56 | CircularBuffer bufModem(maxBufInModem); |
brunofgc | 0:1c0a769988ee | 57 | CircularBuffer bufPC(128); |
brunofgc | 0:1c0a769988ee | 58 | CircularBuffer sdCardBuf(2048); |
brunofgc | 0:1c0a769988ee | 59 | |
brunofgc | 0:1c0a769988ee | 60 | uint8_t enviaDadosPorAlarme = 255; |
brunofgc | 0:1c0a769988ee | 61 | bool reportaMudancaAoServer; |
brunofgc | 0:1c0a769988ee | 62 | bool executaComandoServer; |
brunofgc | 0:1c0a769988ee | 63 | //typeConfig flashConfig; |
brunofgc | 0:1c0a769988ee | 64 | |
brunofgc | 0:1c0a769988ee | 65 | Serial pc(USBTX, USBRX); //Instancia de recurso Serial uart pc |
brunofgc | 0:1c0a769988ee | 66 | |
brunofgc | 0:1c0a769988ee | 67 | DigitalOut max_de(P2_7); //Pino de habilitação max485 para porta Energy Metering ModBus |
brunofgc | 0:1c0a769988ee | 68 | Serial modBusMaster1Serial(P2_0,P2_1); //Recurso serial para porta Energy Metering ModBus |
brunofgc | 0:1c0a769988ee | 69 | |
brunofgc | 0:1c0a769988ee | 70 | /* |
brunofgc | 0:1c0a769988ee | 71 | //Interrupções externas e botões |
brunofgc | 0:1c0a769988ee | 72 | InterruptIn int_wh(P2_11); |
brunofgc | 0:1c0a769988ee | 73 | InterruptIn int_varh(P2_12); |
brunofgc | 0:1c0a769988ee | 74 | |
brunofgc | 0:1c0a769988ee | 75 | void whCount(void){ |
brunofgc | 0:1c0a769988ee | 76 | whled=1; |
brunofgc | 0:1c0a769988ee | 77 | wait(0.2); |
brunofgc | 0:1c0a769988ee | 78 | whled=0; |
brunofgc | 0:1c0a769988ee | 79 | } |
brunofgc | 0:1c0a769988ee | 80 | |
brunofgc | 0:1c0a769988ee | 81 | void varhCount(void){ |
brunofgc | 0:1c0a769988ee | 82 | varhled=1; |
brunofgc | 0:1c0a769988ee | 83 | wait(0.2); |
brunofgc | 0:1c0a769988ee | 84 | varhled=0; |
brunofgc | 0:1c0a769988ee | 85 | }*/ |
brunofgc | 0:1c0a769988ee | 86 | //Definições de Hardware acessÃveis em qualquer modulo |
brunofgc | 0:1c0a769988ee | 87 | |
brunofgc | 0:1c0a769988ee | 88 | /* |
brunofgc | 0:1c0a769988ee | 89 | void loadStandardConfig(){ |
brunofgc | 0:1c0a769988ee | 90 | strcpy(flashConfig.apnList[0],"claro.com.br"); |
brunofgc | 0:1c0a769988ee | 91 | strcpy(flashConfig.apnList[1],"zap.vivo.com.br"); |
brunofgc | 0:1c0a769988ee | 92 | strcpy(flashConfig.apnList[2],"tim.br"); |
brunofgc | 0:1c0a769988ee | 93 | strcpy(flashConfig.apnList[3],"generica.oi.com.br"); |
brunofgc | 0:1c0a769988ee | 94 | |
brunofgc | 0:1c0a769988ee | 95 | strcpy(flashConfig.login[0],"claro"); |
brunofgc | 0:1c0a769988ee | 96 | strcpy(flashConfig.login[1],"vivo"); |
brunofgc | 0:1c0a769988ee | 97 | strcpy(flashConfig.login[2],"tim"); |
brunofgc | 0:1c0a769988ee | 98 | strcpy(flashConfig.login[3],"oi"); |
brunofgc | 0:1c0a769988ee | 99 | |
brunofgc | 0:1c0a769988ee | 100 | strcpy(flashConfig.senha[0],"claro"); |
brunofgc | 0:1c0a769988ee | 101 | strcpy(flashConfig.senha[1],"vivo"); |
brunofgc | 0:1c0a769988ee | 102 | strcpy(flashConfig.senha[2],"tim"); |
brunofgc | 0:1c0a769988ee | 103 | strcpy(flashConfig.senha[3],"oi"); |
brunofgc | 0:1c0a769988ee | 104 | |
brunofgc | 0:1c0a769988ee | 105 | strcpy(flashConfig.serverAddress,"www.vitaesolucoes.com.br"); |
brunofgc | 0:1c0a769988ee | 106 | |
brunofgc | 0:1c0a769988ee | 107 | strcpy(flashConfig.serverPort,"80"); |
brunofgc | 0:1c0a769988ee | 108 | |
brunofgc | 0:1c0a769988ee | 109 | flashConfig.periodoDeEnvioDeDadosMinutos=15; //Padrão 15 minutos |
brunofgc | 0:1c0a769988ee | 110 | flashConfig.ultimoEstadoLigadoDesligado=0; //Padrão de fábrica desligado |
brunofgc | 0:1c0a769988ee | 111 | } |
brunofgc | 0:1c0a769988ee | 112 | */ |
brunofgc | 0:1c0a769988ee | 113 | |
brunofgc | 0:1c0a769988ee | 114 | void leiturasDigitais(){ |
brunofgc | 0:1c0a769988ee | 115 | entradasDigitais[0] = ED1; |
brunofgc | 0:1c0a769988ee | 116 | entradasDigitais[1] = ED2; |
brunofgc | 0:1c0a769988ee | 117 | entradasDigitais[2] = ED3; |
brunofgc | 0:1c0a769988ee | 118 | entradasDigitais[3] = ED4; |
brunofgc | 0:1c0a769988ee | 119 | entradasDigitais[4] = ED5; |
brunofgc | 0:1c0a769988ee | 120 | entradasDigitais[5] = ED6; |
brunofgc | 0:1c0a769988ee | 121 | entradasDigitais[6] = ED7; |
brunofgc | 0:1c0a769988ee | 122 | entradasDigitais[7] = ED8; |
brunofgc | 0:1c0a769988ee | 123 | entradasDigitais[8] = ED9; |
brunofgc | 0:1c0a769988ee | 124 | } |
brunofgc | 0:1c0a769988ee | 125 | |
brunofgc | 0:1c0a769988ee | 126 | |
brunofgc | 0:1c0a769988ee | 127 | /* |
brunofgc | 0:1c0a769988ee | 128 | AnalogIn ai4(p20); |
brunofgc | 0:1c0a769988ee | 129 | AnalogIn ai3(p19); |
brunofgc | 0:1c0a769988ee | 130 | AnalogIn ai2(p18); |
brunofgc | 0:1c0a769988ee | 131 | AnalogIn ai1(p17); |
brunofgc | 0:1c0a769988ee | 132 | |
brunofgc | 0:1c0a769988ee | 133 | |
brunofgc | 0:1c0a769988ee | 134 | AnalogIn ai4(P1_31); |
brunofgc | 0:1c0a769988ee | 135 | AnalogIn ai3(P1_30); |
brunofgc | 0:1c0a769988ee | 136 | AnalogIn ai2(P0_26); |
brunofgc | 0:1c0a769988ee | 137 | AnalogIn ai1(P0_25); |
brunofgc | 0:1c0a769988ee | 138 | */ |
brunofgc | 0:1c0a769988ee | 139 | HighSpeedAnalogIn analogInObject(P1_31,P1_30,P0_26,P0_25); |
brunofgc | 0:1c0a769988ee | 140 | |
brunofgc | 0:1c0a769988ee | 141 | void leiturasAnalogicas(){ |
brunofgc | 0:1c0a769988ee | 142 | float ai1_filtrado=0.0; |
brunofgc | 0:1c0a769988ee | 143 | float ai2_filtrado=0.0; |
brunofgc | 0:1c0a769988ee | 144 | float ai3_filtrado=0.0; |
brunofgc | 0:1c0a769988ee | 145 | float ai4_filtrado=0.0; |
brunofgc | 0:1c0a769988ee | 146 | |
brunofgc | 0:1c0a769988ee | 147 | uint8_t i; |
brunofgc | 0:1c0a769988ee | 148 | |
brunofgc | 0:1c0a769988ee | 149 | //LPC_SC->PCONP |= (1 << 12); //Set the PDADC bit residing in the 12th bit of the PCONP register |
brunofgc | 0:1c0a769988ee | 150 | |
brunofgc | 0:1c0a769988ee | 151 | for(i=0;i<5;i++){ |
brunofgc | 0:1c0a769988ee | 152 | ai1_filtrado += analogInObject.read_u16(P0_25); |
brunofgc | 0:1c0a769988ee | 153 | ai2_filtrado += analogInObject.read_u16(P0_26); |
brunofgc | 0:1c0a769988ee | 154 | ai3_filtrado += analogInObject.read_u16(P1_30); |
brunofgc | 0:1c0a769988ee | 155 | ai4_filtrado += analogInObject.read_u16(P1_31); |
brunofgc | 0:1c0a769988ee | 156 | } |
brunofgc | 0:1c0a769988ee | 157 | |
brunofgc | 0:1c0a769988ee | 158 | ai1_filtrado = ai1_filtrado/5; |
brunofgc | 0:1c0a769988ee | 159 | ai2_filtrado = ai2_filtrado/5; |
brunofgc | 0:1c0a769988ee | 160 | ai3_filtrado = ai3_filtrado/5; |
brunofgc | 0:1c0a769988ee | 161 | ai4_filtrado = ai4_filtrado/5; |
brunofgc | 0:1c0a769988ee | 162 | |
brunofgc | 0:1c0a769988ee | 163 | |
brunofgc | 0:1c0a769988ee | 164 | aiFiltrada[3] = ai4_filtrado; |
brunofgc | 0:1c0a769988ee | 165 | aiFiltrada[2] = ai3_filtrado; |
brunofgc | 0:1c0a769988ee | 166 | aiFiltrada[1] = ai2_filtrado; |
brunofgc | 0:1c0a769988ee | 167 | aiFiltrada[0] = ai1_filtrado; |
brunofgc | 0:1c0a769988ee | 168 | |
brunofgc | 0:1c0a769988ee | 169 | |
brunofgc | 0:1c0a769988ee | 170 | /*aiFiltrada[3] = FiltroAI4.get((uint16_t)floor(ai4_double)); |
brunofgc | 0:1c0a769988ee | 171 | aiFiltrada[2] = FiltroAI3.get((uint16_t)floor(ai3_double)); |
brunofgc | 0:1c0a769988ee | 172 | aiFiltrada[1] = FiltroAI2.get((uint16_t)floor(ai2_double)); |
brunofgc | 0:1c0a769988ee | 173 | aiFiltrada[0] = FiltroAI1.get((uint16_t)floor(ai1_double));*/ |
brunofgc | 0:1c0a769988ee | 174 | |
brunofgc | 0:1c0a769988ee | 175 | } |
brunofgc | 0:1c0a769988ee | 176 | |
brunofgc | 0:1c0a769988ee | 177 | //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 | 178 | osThreadId idThreadTimers; |
brunofgc | 0:1c0a769988ee | 179 | void threadTimers(void const *args){ |
brunofgc | 0:1c0a769988ee | 180 | while(true){ |
brunofgc | 0:1c0a769988ee | 181 | osSignalWait(0x1,1000); //Executo compulsoriamente mesmo que não haja sinal num periodo de 1 segundo |
brunofgc | 0:1c0a769988ee | 182 | if(serialPC::startTimer_pacote){ |
brunofgc | 0:1c0a769988ee | 183 | serialPC::startTimer_pacote = false; |
brunofgc | 0:1c0a769988ee | 184 | osTimerStart(serialPC::timer_pacote,250); |
brunofgc | 0:1c0a769988ee | 185 | } |
brunofgc | 0:1c0a769988ee | 186 | if(serialModem::startTimer_pacote){ |
brunofgc | 0:1c0a769988ee | 187 | serialModem::startTimer_pacote = false; |
brunofgc | 0:1c0a769988ee | 188 | osTimerStart(serialModem::timer_pacote,modemCom::timeOutModem); |
brunofgc | 0:1c0a769988ee | 189 | } |
brunofgc | 0:1c0a769988ee | 190 | if(modBusMaster1::startThreadModBusMaster){ |
brunofgc | 0:1c0a769988ee | 191 | modBusMaster1::startThreadModBusMaster = false; |
brunofgc | 0:1c0a769988ee | 192 | osTimerStart(modBusMaster1::timer_pacote,4); |
brunofgc | 0:1c0a769988ee | 193 | } |
brunofgc | 0:1c0a769988ee | 194 | } |
brunofgc | 0:1c0a769988ee | 195 | } |
brunofgc | 0:1c0a769988ee | 196 | osThreadDef(threadTimers, osPriorityNormal, DEFAULT_STACK_SIZE/10); |
brunofgc | 0:1c0a769988ee | 197 | |
brunofgc | 0:1c0a769988ee | 198 | osThreadId idThreadProcessaPacote; |
brunofgc | 0:1c0a769988ee | 199 | void threadProcessaPacote(void const *args){ |
brunofgc | 0:1c0a769988ee | 200 | while(true){ |
brunofgc | 0:1c0a769988ee | 201 | osSignalWait(0x1,osWaitForever); //Executo apenas ao receber o sinal |
brunofgc | 0:1c0a769988ee | 202 | modemCom::processaPacote(NULL); |
brunofgc | 0:1c0a769988ee | 203 | } |
brunofgc | 0:1c0a769988ee | 204 | } |
brunofgc | 0:1c0a769988ee | 205 | osThreadDef(threadProcessaPacote, osPriorityNormal, DEFAULT_STACK_SIZE); |
brunofgc | 0:1c0a769988ee | 206 | |
brunofgc | 0:1c0a769988ee | 207 | |
brunofgc | 0:1c0a769988ee | 208 | //Thread que gerencia as comunicacoes (RAD - 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 | 0:1c0a769988ee | 213 | char aux[30]; |
brunofgc | 0:1c0a769988ee | 214 | while(true){ |
brunofgc | 0:1c0a769988ee | 215 | osSignalWait(0x1,1000); //Executo compulsoriamente mesmo que não haja sinal num periodo de 1 segundo |
brunofgc | 0:1c0a769988ee | 216 | |
brunofgc | 0:1c0a769988ee | 217 | if(hardwareReset){ |
brunofgc | 0:1c0a769988ee | 218 | //Resetando |
brunofgc | 0:1c0a769988ee | 219 | diversos::wdt.kick(0.1); |
brunofgc | 0:1c0a769988ee | 220 | while(true); |
brunofgc | 0:1c0a769988ee | 221 | } |
brunofgc | 0:1c0a769988ee | 222 | |
brunofgc | 0:1c0a769988ee | 223 | |
brunofgc | 0:1c0a769988ee | 224 | //if((modemCom::status.SRING)&&(!modemCom::status.serverConnected)){ |
brunofgc | 0:1c0a769988ee | 225 | if(modemCom::status.SRINGsockEntrante){ |
brunofgc | 0:1c0a769988ee | 226 | modemCom::status.SRINGsockEntrante=false; |
brunofgc | 0:1c0a769988ee | 227 | modemCom::atendeSRING(connID); |
brunofgc | 0:1c0a769988ee | 228 | } |
brunofgc | 0:1c0a769988ee | 229 | |
brunofgc | 0:1c0a769988ee | 230 | if(eventosRTC::rotina10Segundos){ |
brunofgc | 0:1c0a769988ee | 231 | //Verifica se IP mudou e se estou conectado!; |
brunofgc | 0:1c0a769988ee | 232 | |
brunofgc | 0:1c0a769988ee | 233 | //Verifica se esta conectado; |
brunofgc | 0:1c0a769988ee | 234 | /* |
brunofgc | 0:1c0a769988ee | 235 | if(!modemCom::sendToModem("at#sgact?\r",1,&modemCom::status.associated,500,2)){ |
brunofgc | 0:1c0a769988ee | 236 | //Não conectado reportar ao server uma vez que uma nova conexão irá mudar o IP |
brunofgc | 0:1c0a769988ee | 237 | reportaMudancaAoServer = true; |
brunofgc | 0:1c0a769988ee | 238 | } |
brunofgc | 0:1c0a769988ee | 239 | */ |
brunofgc | 0:1c0a769988ee | 240 | eventosRTC::rotina10Segundos = false; |
brunofgc | 0:1c0a769988ee | 241 | } |
brunofgc | 0:1c0a769988ee | 242 | |
brunofgc | 0:1c0a769988ee | 243 | if(eventosRTC::rotina1hora){ |
brunofgc | 0:1c0a769988ee | 244 | //Faço aqui o que for devido. |
brunofgc | 0:1c0a769988ee | 245 | pc.printf("Enviando dados de hora completa ao server.\n"); |
brunofgc | 0:1c0a769988ee | 246 | modemCom::status.emComunicacao = true; |
brunofgc | 0:1c0a769988ee | 247 | diversos::wdt.kick(300.0); |
brunofgc | 0:1c0a769988ee | 248 | modemCom::enviaDados(dadosArmazenados); |
brunofgc | 0:1c0a769988ee | 249 | diversos::wdt.kick(90.0); |
brunofgc | 0:1c0a769988ee | 250 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 251 | eventosRTC::rotina1hora = false; |
brunofgc | 0:1c0a769988ee | 252 | } |
brunofgc | 0:1c0a769988ee | 253 | |
brunofgc | 0:1c0a769988ee | 254 | if(sdCard::deleteSentFiles){ |
brunofgc | 0:1c0a769988ee | 255 | sdCard::deleteSentFiles = false; |
brunofgc | 0:1c0a769988ee | 256 | sdCard::finalizaEnvio(); |
brunofgc | 0:1c0a769988ee | 257 | } |
brunofgc | 0:1c0a769988ee | 258 | |
brunofgc | 0:1c0a769988ee | 259 | if(arquivoEnvioPointer!=NULL){ |
brunofgc | 0:1c0a769988ee | 260 | pc.printf("Enviando o arquivo <%s> ao server.\n",arquivoEnvioPointer->nome); |
brunofgc | 0:1c0a769988ee | 261 | if(modemCom::postFileCommandMode("www.vitaesolucoes.com.br","/drome/parser/index.php",arquivoEnvioPointer)){ |
brunofgc | 0:1c0a769988ee | 262 | pc.printf("Enviado o arquivo <%s>.\n",arquivoEnvioPointer->nome); |
brunofgc | 0:1c0a769988ee | 263 | arquivoEnvioPointer = NULL; |
brunofgc | 0:1c0a769988ee | 264 | }else{ |
brunofgc | 0:1c0a769988ee | 265 | pc.printf("Erro enviando o arquivo <%s>.\n",arquivoEnvioPointer->nome); |
brunofgc | 0:1c0a769988ee | 266 | } |
brunofgc | 0:1c0a769988ee | 267 | } |
brunofgc | 0:1c0a769988ee | 268 | led4=!led4; |
brunofgc | 0:1c0a769988ee | 269 | modemCom::timeOutModem = 30; |
brunofgc | 0:1c0a769988ee | 270 | |
brunofgc | 0:1c0a769988ee | 271 | //Reseto o watchdogTimer; |
brunofgc | 0:1c0a769988ee | 272 | diversos::wdt.kick(); |
brunofgc | 0:1c0a769988ee | 273 | } |
brunofgc | 0:1c0a769988ee | 274 | } |
brunofgc | 0:1c0a769988ee | 275 | osThreadDef(threadComunicacoes, osPriorityNormal, (DEFAULT_STACK_SIZE * 2)); |
brunofgc | 0:1c0a769988ee | 276 | |
brunofgc | 0:1c0a769988ee | 277 | void RTC_POR_SEGUNDO(void){ |
brunofgc | 0:1c0a769988ee | 278 | time_t seconds; |
brunofgc | 0:1c0a769988ee | 279 | seconds = time(NULL); |
brunofgc | 0:1c0a769988ee | 280 | |
brunofgc | 0:1c0a769988ee | 281 | eventosRTC::rotina1segundo=true; |
brunofgc | 0:1c0a769988ee | 282 | |
brunofgc | 0:1c0a769988ee | 283 | if((seconds%10)==0){ |
brunofgc | 0:1c0a769988ee | 284 | eventosRTC::rotina10Segundos = true; |
brunofgc | 0:1c0a769988ee | 285 | osSignalSet(idThreadComunicacoes, 0x1); |
brunofgc | 0:1c0a769988ee | 286 | } |
brunofgc | 0:1c0a769988ee | 287 | |
brunofgc | 0:1c0a769988ee | 288 | if((seconds%60)==0){ |
brunofgc | 0:1c0a769988ee | 289 | eventosRTC::minutos = true; |
brunofgc | 0:1c0a769988ee | 290 | } |
brunofgc | 0:1c0a769988ee | 291 | |
brunofgc | 0:1c0a769988ee | 292 | //if((seconds%900)==0){ |
brunofgc | 0:1c0a769988ee | 293 | if((seconds%900)==0){ |
brunofgc | 0:1c0a769988ee | 294 | eventosRTC::rotina15Minutos = true; |
brunofgc | 0:1c0a769988ee | 295 | } |
brunofgc | 0:1c0a769988ee | 296 | |
brunofgc | 0:1c0a769988ee | 297 | if((seconds%3600)==0){ //Atuando a cada 3 minutos |
brunofgc | 0:1c0a769988ee | 298 | //Entro aqui a cada hora |
brunofgc | 0:1c0a769988ee | 299 | eventosRTC::rotina1hora = true; |
brunofgc | 0:1c0a769988ee | 300 | } |
brunofgc | 0:1c0a769988ee | 301 | } |
brunofgc | 0:1c0a769988ee | 302 | |
brunofgc | 0:1c0a769988ee | 303 | //Timeout timeOutModem; |
brunofgc | 0:1c0a769988ee | 304 | void chamaProcessaPacotePeloTimeOut(){ |
brunofgc | 0:1c0a769988ee | 305 | modemCom::processaPacote(NULL); |
brunofgc | 0:1c0a769988ee | 306 | } |
brunofgc | 0:1c0a769988ee | 307 | |
brunofgc | 0:1c0a769988ee | 308 | void loadDevices(){ |
brunofgc | 0:1c0a769988ee | 309 | if(sdCard::abreArquivo(&sdCard::devices,"r")){ |
brunofgc | 0:1c0a769988ee | 310 | criaDevices(sdCard::devices.fp); |
brunofgc | 0:1c0a769988ee | 311 | if(devices){ |
brunofgc | 0:1c0a769988ee | 312 | configuraDevices(sdCard::devices.fp); |
brunofgc | 0:1c0a769988ee | 313 | printf("Dispositivos configurados.\n"); |
brunofgc | 0:1c0a769988ee | 314 | testaTudoDevices(); |
brunofgc | 0:1c0a769988ee | 315 | } |
brunofgc | 0:1c0a769988ee | 316 | sdCard::fechaArquivo(&sdCard::devices); |
brunofgc | 0:1c0a769988ee | 317 | } |
brunofgc | 0:1c0a769988ee | 318 | } |
brunofgc | 0:1c0a769988ee | 319 | |
brunofgc | 0:1c0a769988ee | 320 | void inicializaMaquina(){ |
brunofgc | 0:1c0a769988ee | 321 | pwmPeriod = 100000; |
brunofgc | 0:1c0a769988ee | 322 | SD1.period_us(pwmPeriod); |
brunofgc | 0:1c0a769988ee | 323 | modemCom::atendendoWebServer = false; |
brunofgc | 0:1c0a769988ee | 324 | serialPC::serialPC_init(); |
brunofgc | 0:1c0a769988ee | 325 | pc.printf("Versao 07/02/2016.\r\n"); |
brunofgc | 0:1c0a769988ee | 326 | modemCom::timeOutModem = 30; |
brunofgc | 0:1c0a769988ee | 327 | serialModem::serial_init(); |
brunofgc | 0:1c0a769988ee | 328 | numeroDeBytesPorEnvio = 768; |
brunofgc | 0:1c0a769988ee | 329 | //loadStandardConfig(); //Carregando standardConfig //DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG |
brunofgc | 0:1c0a769988ee | 330 | modBusMaster1::modBusMaster(&modBusMaster1Serial,19200,&max_de); |
brunofgc | 0:1c0a769988ee | 331 | |
brunofgc | 0:1c0a769988ee | 332 | /* |
brunofgc | 0:1c0a769988ee | 333 | //Interrupções externas e botões |
brunofgc | 0:1c0a769988ee | 334 | int_wh.mode(PullNone); |
brunofgc | 0:1c0a769988ee | 335 | int_varh.mode(PullNone); |
brunofgc | 0:1c0a769988ee | 336 | |
brunofgc | 0:1c0a769988ee | 337 | int_wh.fall(&whCount); |
brunofgc | 0:1c0a769988ee | 338 | int_varh.fall(&varhCount);*/ |
brunofgc | 0:1c0a769988ee | 339 | |
brunofgc | 0:1c0a769988ee | 340 | //DEBUG |
brunofgc | 0:1c0a769988ee | 341 | pc.printf("Inicializando DROME.\n"); |
brunofgc | 0:1c0a769988ee | 342 | |
brunofgc | 0:1c0a769988ee | 343 | //Debug debug debug debug debug debug debug debug debug debug debug debug |
brunofgc | 0:1c0a769988ee | 344 | if(sdCard::init()){ |
brunofgc | 0:1c0a769988ee | 345 | pc.printf("SD Card inicializado com sucesso.\n"); |
brunofgc | 0:1c0a769988ee | 346 | |
brunofgc | 0:1c0a769988ee | 347 | //Carregando a lista de dispositivos |
brunofgc | 0:1c0a769988ee | 348 | loadDevices(); |
brunofgc | 0:1c0a769988ee | 349 | }else{ |
brunofgc | 0:1c0a769988ee | 350 | pc.printf("Nao foi possivel inicializar o SD Card.\n"); |
brunofgc | 0:1c0a769988ee | 351 | } |
brunofgc | 0:1c0a769988ee | 352 | |
brunofgc | 0:1c0a769988ee | 353 | |
brunofgc | 0:1c0a769988ee | 354 | |
brunofgc | 0:1c0a769988ee | 355 | |
brunofgc | 0:1c0a769988ee | 356 | |
brunofgc | 0:1c0a769988ee | 357 | //Timers por minuto |
brunofgc | 0:1c0a769988ee | 358 | RTC::attach(&RTC_POR_SEGUNDO, RTC::Second); |
brunofgc | 0:1c0a769988ee | 359 | //Chamada de threads |
brunofgc | 0:1c0a769988ee | 360 | //Thread dos timers! |
brunofgc | 0:1c0a769988ee | 361 | idThreadTimers = osThreadCreate(osThread(threadTimers), NULL); |
brunofgc | 0:1c0a769988ee | 362 | //Thread das comunicacoes |
brunofgc | 0:1c0a769988ee | 363 | idThreadComunicacoes = osThreadCreate(osThread(threadComunicacoes), NULL); |
brunofgc | 0:1c0a769988ee | 364 | //Thread da funcao processaPacote |
brunofgc | 0:1c0a769988ee | 365 | idThreadProcessaPacote = osThreadCreate(osThread(threadProcessaPacote), NULL); |
brunofgc | 0:1c0a769988ee | 366 | //Chamada de threads |
brunofgc | 0:1c0a769988ee | 367 | serialModem::configBaud(); |
brunofgc | 0:1c0a769988ee | 368 | } |
brunofgc | 0:1c0a769988ee | 369 | |
brunofgc | 0:1c0a769988ee | 370 | void verificaSaidasDigitais(){ |
brunofgc | 0:1c0a769988ee | 371 | SD_OE_R = 1; |
brunofgc | 0:1c0a769988ee | 372 | } |
brunofgc | 0:1c0a769988ee | 373 | |
brunofgc | 0:1c0a769988ee | 374 | Serial modem(P0_10,P0_11); //Instancia de recurso Serial uart Modem |
brunofgc | 0:1c0a769988ee | 375 | //Inicio do programa; |
brunofgc | 0:1c0a769988ee | 376 | int main() { |
brunofgc | 0:1c0a769988ee | 377 | //Inicializando maquina |
brunofgc | 0:1c0a769988ee | 378 | |
brunofgc | 0:1c0a769988ee | 379 | /*uint16_t leInicioESP=150000; |
brunofgc | 0:1c0a769988ee | 380 | while(leInicioESP){ |
brunofgc | 0:1c0a769988ee | 381 | if(modem.readable()){ |
brunofgc | 0:1c0a769988ee | 382 | char r = modem.getc(); |
brunofgc | 0:1c0a769988ee | 383 | pc.putc(r); |
brunofgc | 0:1c0a769988ee | 384 | } |
brunofgc | 0:1c0a769988ee | 385 | wait(0.0001); |
brunofgc | 0:1c0a769988ee | 386 | leInicioESP--; |
brunofgc | 0:1c0a769988ee | 387 | }*/ |
brunofgc | 0:1c0a769988ee | 388 | |
brunofgc | 0:1c0a769988ee | 389 | diversos::wdt.kick(90.0); |
brunofgc | 0:1c0a769988ee | 390 | |
brunofgc | 0:1c0a769988ee | 391 | modem.baud(76800); |
brunofgc | 0:1c0a769988ee | 392 | ESP_CH_PD = 1; |
brunofgc | 0:1c0a769988ee | 393 | ESP_ESPGPIO_2 = 1; |
brunofgc | 0:1c0a769988ee | 394 | ESP_RESET = 1; |
brunofgc | 0:1c0a769988ee | 395 | |
brunofgc | 0:1c0a769988ee | 396 | |
brunofgc | 0:1c0a769988ee | 397 | reportaMudancaAoServer = true; |
brunofgc | 0:1c0a769988ee | 398 | modemCom::status.emComunicacao=false; |
brunofgc | 0:1c0a769988ee | 399 | |
brunofgc | 0:1c0a769988ee | 400 | wait(5); |
brunofgc | 0:1c0a769988ee | 401 | inicializaMaquina(); |
brunofgc | 0:1c0a769988ee | 402 | modemCom::inicializaModem(); |
brunofgc | 0:1c0a769988ee | 403 | modemCom::conectaWiFi(); |
brunofgc | 0:1c0a769988ee | 404 | wait(2); |
brunofgc | 0:1c0a769988ee | 405 | modemCom::enviaDados(dadosStatus); |
brunofgc | 0:1c0a769988ee | 406 | |
brunofgc | 0:1c0a769988ee | 407 | while(true){ |
brunofgc | 0:1c0a769988ee | 408 | if(eventosRTC::rotina1segundo){ |
brunofgc | 0:1c0a769988ee | 409 | eventosRTC::rotina1segundo = false; |
brunofgc | 0:1c0a769988ee | 410 | |
brunofgc | 0:1c0a769988ee | 411 | if(enviaDadosPorAlarme!=255){ |
brunofgc | 0:1c0a769988ee | 412 | enviaDadosPorAlarme--; |
brunofgc | 0:1c0a769988ee | 413 | if(enviaDadosPorAlarme==0){ |
brunofgc | 0:1c0a769988ee | 414 | eventosRTC::rotina1hora=true; |
brunofgc | 0:1c0a769988ee | 415 | enviaDadosPorAlarme=255; |
brunofgc | 0:1c0a769988ee | 416 | } |
brunofgc | 0:1c0a769988ee | 417 | } |
brunofgc | 0:1c0a769988ee | 418 | //pc.printf("Passo 1.\r\n"); |
brunofgc | 0:1c0a769988ee | 419 | leiturasAnalogicas(); |
brunofgc | 0:1c0a769988ee | 420 | //pc.printf("Passo 2.\r\n"); |
brunofgc | 0:1c0a769988ee | 421 | leiturasDigitais(); |
brunofgc | 0:1c0a769988ee | 422 | //pc.printf("Passo 3.\r\n"); |
brunofgc | 0:1c0a769988ee | 423 | verifyAlarms(); |
brunofgc | 0:1c0a769988ee | 424 | //pc.printf("Passo 4.\r\n"); |
brunofgc | 0:1c0a769988ee | 425 | verificaSaidasDigitais(); |
brunofgc | 0:1c0a769988ee | 426 | pc.printf("\nMemoria RAM disponivel = %lu\n",diversos::memAvailable()); |
brunofgc | 0:1c0a769988ee | 427 | time_t seconds = time(NULL); |
brunofgc | 0:1c0a769988ee | 428 | printf("%s\n", ctime(&seconds)); |
brunofgc | 0:1c0a769988ee | 429 | |
brunofgc | 0:1c0a769988ee | 430 | //pc.printf("\nEntrada 3 = %lu.\nEntrada 2 = %lu.\nEntrada 1 = %lu.\n\n",aiFiltrada[2],aiFiltrada[1],aiFiltrada[0]); |
brunofgc | 0:1c0a769988ee | 431 | |
brunofgc | 0:1c0a769988ee | 432 | //Debug teste de saidas digitais. |
brunofgc | 0:1c0a769988ee | 433 | /* |
brunofgc | 0:1c0a769988ee | 434 | DigitalIn SD_FAULT(P1_9); |
brunofgc | 0:1c0a769988ee | 435 | DigitalOut SD_OE_R(P1_10); |
brunofgc | 0:1c0a769988ee | 436 | |
brunofgc | 0:1c0a769988ee | 437 | DigitalOut SD1(P1_18); |
brunofgc | 0:1c0a769988ee | 438 | DigitalOut SD2(P1_20); |
brunofgc | 0:1c0a769988ee | 439 | */ |
brunofgc | 0:1c0a769988ee | 440 | |
brunofgc | 0:1c0a769988ee | 441 | /*if(SD_FAULT){ |
brunofgc | 0:1c0a769988ee | 442 | printf("Estado de SD_FAULT = 1.\r\n"); |
brunofgc | 0:1c0a769988ee | 443 | }else{printf("Estado de SD_FAULT = 0.\r\n");} |
brunofgc | 0:1c0a769988ee | 444 | SD_OE_R = 1; |
brunofgc | 0:1c0a769988ee | 445 | |
brunofgc | 0:1c0a769988ee | 446 | SD1.period(0.001); |
brunofgc | 0:1c0a769988ee | 447 | SD2.period(0.0001); |
brunofgc | 0:1c0a769988ee | 448 | |
brunofgc | 0:1c0a769988ee | 449 | SD1.write(1); |
brunofgc | 0:1c0a769988ee | 450 | for(int cont = 0; cont<11; cont++){ |
brunofgc | 0:1c0a769988ee | 451 | SD2.write((float)cont/10); |
brunofgc | 0:1c0a769988ee | 452 | wait(0.5); |
brunofgc | 0:1c0a769988ee | 453 | printf("Intensidade = %f.\r\n",(float)(cont/10)); |
brunofgc | 0:1c0a769988ee | 454 | }*/ |
brunofgc | 0:1c0a769988ee | 455 | } |
brunofgc | 0:1c0a769988ee | 456 | |
brunofgc | 0:1c0a769988ee | 457 | if(eventosRTC::rotina10Segundos){ |
brunofgc | 0:1c0a769988ee | 458 | //Executo abaixo a rotina de 10 segundos |
brunofgc | 0:1c0a769988ee | 459 | verifySchedules(); |
brunofgc | 0:1c0a769988ee | 460 | eventosRTC::rotina10Segundos = false; |
brunofgc | 0:1c0a769988ee | 461 | } |
brunofgc | 0:1c0a769988ee | 462 | |
brunofgc | 0:1c0a769988ee | 463 | if(eventosRTC::minutos){ |
brunofgc | 0:1c0a769988ee | 464 | //Executo abaixo a rotina de 1 minuto |
brunofgc | 0:1c0a769988ee | 465 | writeReadingsToSD(); |
brunofgc | 0:1c0a769988ee | 466 | eventosRTC::minutos = false; |
brunofgc | 0:1c0a769988ee | 467 | } |
brunofgc | 0:1c0a769988ee | 468 | |
brunofgc | 0:1c0a769988ee | 469 | if(executaComandoServer){ |
brunofgc | 0:1c0a769988ee | 470 | executaComandoServer = false; |
brunofgc | 0:1c0a769988ee | 471 | commands::exec(connID); |
brunofgc | 0:1c0a769988ee | 472 | } |
brunofgc | 0:1c0a769988ee | 473 | |
brunofgc | 0:1c0a769988ee | 474 | if(modemCom::atendendoWebServer){ |
brunofgc | 0:1c0a769988ee | 475 | modemCom::atendendoWebServer = false; |
brunofgc | 0:1c0a769988ee | 476 | pc.printf("Atendendo webServer.\r\n"); |
brunofgc | 0:1c0a769988ee | 477 | modemCom::webServer(); |
brunofgc | 0:1c0a769988ee | 478 | } |
brunofgc | 0:1c0a769988ee | 479 | /* |
brunofgc | 0:1c0a769988ee | 480 | if(enviaSendData){ |
brunofgc | 0:1c0a769988ee | 481 | enviaSendData = false; |
brunofgc | 0:1c0a769988ee | 482 | modemCom::sendBufferCommandMode(connID,"sendData\r\n",10); |
brunofgc | 0:1c0a769988ee | 483 | } |
brunofgc | 0:1c0a769988ee | 484 | */ |
brunofgc | 0:1c0a769988ee | 485 | /*if(eventosRTC::rotina1hora){ |
brunofgc | 0:1c0a769988ee | 486 | osDelay(1000); //Para dar tempo de a threadComunicações ver que tinha uma rotina de 1 hora agendada. |
brunofgc | 0:1c0a769988ee | 487 | eventosRTC::rotina1hora = false; |
brunofgc | 0:1c0a769988ee | 488 | //Executo abaixo a rotina de 1 hora |
brunofgc | 0:1c0a769988ee | 489 | }*/ |
brunofgc | 0:1c0a769988ee | 490 | } |
brunofgc | 0:1c0a769988ee | 491 | } |