teste de publish

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Committer:
brunofgc
Date:
Thu Mar 30 19:54:09 2017 +0000
Revision:
1:0e0967c88590
Parent:
0:1c0a769988ee
Child:
2:55b7b466e742
Antes de corrigir Aloca??o Din?mica Mal feita

Who changed what in which revision?

UserRevisionLine numberNew 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 1:0e0967c88590 230 if(eventosRTC::rotina15Minutos){
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 0:1c0a769988ee 234 diversos::wdt.kick(300.0);
brunofgc 0:1c0a769988ee 235 modemCom::enviaDados(dadosArmazenados);
brunofgc 0:1c0a769988ee 236 diversos::wdt.kick(90.0);
brunofgc 0:1c0a769988ee 237 modemCom::status.emComunicacao = false;
brunofgc 1:0e0967c88590 238 eventosRTC::rotina15Minutos = false;
brunofgc 0:1c0a769988ee 239 }
brunofgc 0:1c0a769988ee 240
brunofgc 0:1c0a769988ee 241 if(sdCard::deleteSentFiles){
brunofgc 0:1c0a769988ee 242 sdCard::deleteSentFiles = false;
brunofgc 0:1c0a769988ee 243 sdCard::finalizaEnvio();
brunofgc 0:1c0a769988ee 244 }
brunofgc 0:1c0a769988ee 245
brunofgc 0:1c0a769988ee 246 if(arquivoEnvioPointer!=NULL){
brunofgc 0:1c0a769988ee 247 pc.printf("Enviando o arquivo <%s> ao server.\n",arquivoEnvioPointer->nome);
brunofgc 0:1c0a769988ee 248 if(modemCom::postFileCommandMode("www.vitaesolucoes.com.br","/drome/parser/index.php",arquivoEnvioPointer)){
brunofgc 0:1c0a769988ee 249 pc.printf("Enviado o arquivo <%s>.\n",arquivoEnvioPointer->nome);
brunofgc 0:1c0a769988ee 250 arquivoEnvioPointer = NULL;
brunofgc 0:1c0a769988ee 251 }else{
brunofgc 0:1c0a769988ee 252 pc.printf("Erro enviando o arquivo <%s>.\n",arquivoEnvioPointer->nome);
brunofgc 0:1c0a769988ee 253 }
brunofgc 0:1c0a769988ee 254 }
brunofgc 0:1c0a769988ee 255 led4=!led4;
brunofgc 0:1c0a769988ee 256 modemCom::timeOutModem = 30;
brunofgc 0:1c0a769988ee 257
brunofgc 0:1c0a769988ee 258 //Reseto o watchdogTimer;
brunofgc 0:1c0a769988ee 259 diversos::wdt.kick();
brunofgc 0:1c0a769988ee 260 }
brunofgc 0:1c0a769988ee 261 }
brunofgc 0:1c0a769988ee 262 osThreadDef(threadComunicacoes, osPriorityNormal, (DEFAULT_STACK_SIZE * 2));
brunofgc 0:1c0a769988ee 263
brunofgc 0:1c0a769988ee 264 void RTC_POR_SEGUNDO(void){
brunofgc 0:1c0a769988ee 265 time_t seconds;
brunofgc 0:1c0a769988ee 266 seconds = time(NULL);
brunofgc 0:1c0a769988ee 267
brunofgc 0:1c0a769988ee 268 eventosRTC::rotina1segundo=true;
brunofgc 0:1c0a769988ee 269
brunofgc 0:1c0a769988ee 270 if((seconds%10)==0){
brunofgc 0:1c0a769988ee 271 eventosRTC::rotina10Segundos = true;
brunofgc 0:1c0a769988ee 272 osSignalSet(idThreadComunicacoes, 0x1);
brunofgc 0:1c0a769988ee 273 }
brunofgc 0:1c0a769988ee 274
brunofgc 0:1c0a769988ee 275 if((seconds%60)==0){
brunofgc 0:1c0a769988ee 276 eventosRTC::minutos = true;
brunofgc 0:1c0a769988ee 277 }
brunofgc 0:1c0a769988ee 278
brunofgc 0:1c0a769988ee 279 //if((seconds%900)==0){
brunofgc 0:1c0a769988ee 280 if((seconds%900)==0){
brunofgc 0:1c0a769988ee 281 eventosRTC::rotina15Minutos = true;
brunofgc 0:1c0a769988ee 282 }
brunofgc 0:1c0a769988ee 283
brunofgc 0:1c0a769988ee 284 if((seconds%3600)==0){ //Atuando a cada 3 minutos
brunofgc 0:1c0a769988ee 285 //Entro aqui a cada hora
brunofgc 0:1c0a769988ee 286 eventosRTC::rotina1hora = true;
brunofgc 0:1c0a769988ee 287 }
brunofgc 0:1c0a769988ee 288 }
brunofgc 0:1c0a769988ee 289
brunofgc 0:1c0a769988ee 290 //Timeout timeOutModem;
brunofgc 0:1c0a769988ee 291 void chamaProcessaPacotePeloTimeOut(){
brunofgc 0:1c0a769988ee 292 modemCom::processaPacote(NULL);
brunofgc 0:1c0a769988ee 293 }
brunofgc 0:1c0a769988ee 294
brunofgc 0:1c0a769988ee 295 void loadDevices(){
brunofgc 0:1c0a769988ee 296 if(sdCard::abreArquivo(&sdCard::devices,"r")){
brunofgc 0:1c0a769988ee 297 criaDevices(sdCard::devices.fp);
brunofgc 0:1c0a769988ee 298 if(devices){
brunofgc 0:1c0a769988ee 299 configuraDevices(sdCard::devices.fp);
brunofgc 0:1c0a769988ee 300 printf("Dispositivos configurados.\n");
brunofgc 0:1c0a769988ee 301 testaTudoDevices();
brunofgc 0:1c0a769988ee 302 }
brunofgc 0:1c0a769988ee 303 sdCard::fechaArquivo(&sdCard::devices);
brunofgc 0:1c0a769988ee 304 }
brunofgc 0:1c0a769988ee 305 }
brunofgc 0:1c0a769988ee 306
brunofgc 0:1c0a769988ee 307 void inicializaMaquina(){
brunofgc 0:1c0a769988ee 308 pwmPeriod = 100000;
brunofgc 0:1c0a769988ee 309 SD1.period_us(pwmPeriod);
brunofgc 0:1c0a769988ee 310 modemCom::atendendoWebServer = false;
brunofgc 0:1c0a769988ee 311 serialPC::serialPC_init();
brunofgc 0:1c0a769988ee 312 pc.printf("Versao 07/02/2016.\r\n");
brunofgc 0:1c0a769988ee 313 modemCom::timeOutModem = 30;
brunofgc 0:1c0a769988ee 314 serialModem::serial_init();
brunofgc 0:1c0a769988ee 315 numeroDeBytesPorEnvio = 768;
brunofgc 0:1c0a769988ee 316 //loadStandardConfig(); //Carregando standardConfig //DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
brunofgc 1:0e0967c88590 317 modBusMaster1::modBusMaster(&modBusMaster1Serial,19200,&max_de);
brunofgc 1:0e0967c88590 318
brunofgc 1:0e0967c88590 319 pc.printf("Inicializando Sensores de temperatura.\r\n");
brunofgc 1:0e0967c88590 320 inicializaSensoresTemperatura();
brunofgc 0:1c0a769988ee 321
brunofgc 0:1c0a769988ee 322 /*
brunofgc 0:1c0a769988ee 323 //Interrupções externas e botões
brunofgc 0:1c0a769988ee 324 int_wh.mode(PullNone);
brunofgc 0:1c0a769988ee 325 int_varh.mode(PullNone);
brunofgc 0:1c0a769988ee 326
brunofgc 0:1c0a769988ee 327 int_wh.fall(&whCount);
brunofgc 0:1c0a769988ee 328 int_varh.fall(&varhCount);*/
brunofgc 0:1c0a769988ee 329
brunofgc 0:1c0a769988ee 330 //DEBUG
brunofgc 0:1c0a769988ee 331 pc.printf("Inicializando DROME.\n");
brunofgc 0:1c0a769988ee 332
brunofgc 0:1c0a769988ee 333 //Debug debug debug debug debug debug debug debug debug debug debug debug
brunofgc 0:1c0a769988ee 334 if(sdCard::init()){
brunofgc 0:1c0a769988ee 335 pc.printf("SD Card inicializado com sucesso.\n");
brunofgc 0:1c0a769988ee 336
brunofgc 0:1c0a769988ee 337 //Carregando a lista de dispositivos
brunofgc 0:1c0a769988ee 338 loadDevices();
brunofgc 0:1c0a769988ee 339 }else{
brunofgc 0:1c0a769988ee 340 pc.printf("Nao foi possivel inicializar o SD Card.\n");
brunofgc 0:1c0a769988ee 341 }
brunofgc 0:1c0a769988ee 342
brunofgc 0:1c0a769988ee 343
brunofgc 0:1c0a769988ee 344
brunofgc 0:1c0a769988ee 345
brunofgc 0:1c0a769988ee 346
brunofgc 0:1c0a769988ee 347 //Timers por minuto
brunofgc 0:1c0a769988ee 348 RTC::attach(&RTC_POR_SEGUNDO, RTC::Second);
brunofgc 0:1c0a769988ee 349 //Chamada de threads
brunofgc 0:1c0a769988ee 350 //Thread dos timers!
brunofgc 0:1c0a769988ee 351 idThreadTimers = osThreadCreate(osThread(threadTimers), NULL);
brunofgc 0:1c0a769988ee 352 //Thread das comunicacoes
brunofgc 0:1c0a769988ee 353 idThreadComunicacoes = osThreadCreate(osThread(threadComunicacoes), NULL);
brunofgc 0:1c0a769988ee 354 //Thread da funcao processaPacote
brunofgc 0:1c0a769988ee 355 idThreadProcessaPacote = osThreadCreate(osThread(threadProcessaPacote), NULL);
brunofgc 0:1c0a769988ee 356 //Chamada de threads
brunofgc 0:1c0a769988ee 357 serialModem::configBaud();
brunofgc 0:1c0a769988ee 358 }
brunofgc 0:1c0a769988ee 359
brunofgc 0:1c0a769988ee 360 void verificaSaidasDigitais(){
brunofgc 0:1c0a769988ee 361 SD_OE_R = 1;
brunofgc 0:1c0a769988ee 362 }
brunofgc 0:1c0a769988ee 363
brunofgc 0:1c0a769988ee 364 Serial modem(P0_10,P0_11); //Instancia de recurso Serial uart Modem
brunofgc 0:1c0a769988ee 365 //Inicio do programa;
brunofgc 0:1c0a769988ee 366 int main() {
brunofgc 0:1c0a769988ee 367 //Inicializando maquina
brunofgc 0:1c0a769988ee 368
brunofgc 0:1c0a769988ee 369 /*uint16_t leInicioESP=150000;
brunofgc 0:1c0a769988ee 370 while(leInicioESP){
brunofgc 0:1c0a769988ee 371 if(modem.readable()){
brunofgc 0:1c0a769988ee 372 char r = modem.getc();
brunofgc 0:1c0a769988ee 373 pc.putc(r);
brunofgc 0:1c0a769988ee 374 }
brunofgc 0:1c0a769988ee 375 wait(0.0001);
brunofgc 0:1c0a769988ee 376 leInicioESP--;
brunofgc 0:1c0a769988ee 377 }*/
brunofgc 0:1c0a769988ee 378
brunofgc 0:1c0a769988ee 379 diversos::wdt.kick(90.0);
brunofgc 0:1c0a769988ee 380
brunofgc 0:1c0a769988ee 381 modem.baud(76800);
brunofgc 0:1c0a769988ee 382 ESP_CH_PD = 1;
brunofgc 0:1c0a769988ee 383 ESP_ESPGPIO_2 = 1;
brunofgc 0:1c0a769988ee 384 ESP_RESET = 1;
brunofgc 0:1c0a769988ee 385
brunofgc 0:1c0a769988ee 386
brunofgc 0:1c0a769988ee 387 reportaMudancaAoServer = true;
brunofgc 0:1c0a769988ee 388 modemCom::status.emComunicacao=false;
brunofgc 0:1c0a769988ee 389
brunofgc 0:1c0a769988ee 390 wait(5);
brunofgc 0:1c0a769988ee 391 inicializaMaquina();
brunofgc 0:1c0a769988ee 392 modemCom::inicializaModem();
brunofgc 0:1c0a769988ee 393 modemCom::conectaWiFi();
brunofgc 0:1c0a769988ee 394 wait(2);
brunofgc 1:0e0967c88590 395 //modemCom::enviaDados(dadosStatus);
brunofgc 1:0e0967c88590 396
brunofgc 1:0e0967c88590 397 pc.printf("Lido um RSSI de %d.\r\n",modemCom::getRSSI());
brunofgc 0:1c0a769988ee 398
brunofgc 0:1c0a769988ee 399 while(true){
brunofgc 1:0e0967c88590 400
brunofgc 1:0e0967c88590 401 if(eventosRTC::minutos){
brunofgc 1:0e0967c88590 402 //Executo abaixo a rotina de 1 minuto
brunofgc 1:0e0967c88590 403 writeReadingsToSD();
brunofgc 1:0e0967c88590 404
brunofgc 1:0e0967c88590 405 eventosRTC::minutos = false;
brunofgc 1:0e0967c88590 406 }
brunofgc 1:0e0967c88590 407
brunofgc 0:1c0a769988ee 408 if(eventosRTC::rotina1segundo){
brunofgc 0:1c0a769988ee 409 eventosRTC::rotina1segundo = false;
brunofgc 0:1c0a769988ee 410
brunofgc 1:0e0967c88590 411 refreshSensoresTemperatura();
brunofgc 1:0e0967c88590 412
brunofgc 0:1c0a769988ee 413 if(enviaDadosPorAlarme!=255){
brunofgc 0:1c0a769988ee 414 enviaDadosPorAlarme--;
brunofgc 0:1c0a769988ee 415 if(enviaDadosPorAlarme==0){
brunofgc 1:0e0967c88590 416 eventosRTC::rotina15Minutos=true;
brunofgc 0:1c0a769988ee 417 enviaDadosPorAlarme=255;
brunofgc 0:1c0a769988ee 418 }
brunofgc 1:0e0967c88590 419 }
brunofgc 1:0e0967c88590 420 leiturasAnalogicas();
brunofgc 1:0e0967c88590 421 leiturasDigitais();
brunofgc 1:0e0967c88590 422 verifyAlarms();
brunofgc 0:1c0a769988ee 423 verificaSaidasDigitais();
brunofgc 0:1c0a769988ee 424 pc.printf("\nMemoria RAM disponivel = %lu\n",diversos::memAvailable());
brunofgc 0:1c0a769988ee 425 time_t seconds = time(NULL);
brunofgc 1:0e0967c88590 426 printf("%s\n", ctime(&seconds));
brunofgc 1:0e0967c88590 427 pc.printf("\nEntrada 3 = %lu.\nEntrada 2 = %lu.\nEntrada 1 = %lu.\n\n",aiFiltrada[2],aiFiltrada[1],aiFiltrada[0]);
brunofgc 1:0e0967c88590 428 }
brunofgc 0:1c0a769988ee 429
brunofgc 0:1c0a769988ee 430 if(eventosRTC::rotina10Segundos){
brunofgc 1:0e0967c88590 431 //Executo abaixo a rotina de 10 segundos
brunofgc 0:1c0a769988ee 432 verifySchedules();
brunofgc 0:1c0a769988ee 433 eventosRTC::rotina10Segundos = false;
brunofgc 1:0e0967c88590 434 }
brunofgc 1:0e0967c88590 435
brunofgc 1:0e0967c88590 436 /*if(eventosRTC::rotina15Minutos){
brunofgc 1:0e0967c88590 437 eventosRTC::rotina15Minutos = false;
brunofgc 1:0e0967c88590 438 //modemCom::writeStatusToSD();
brunofgc 1:0e0967c88590 439 }*/
brunofgc 0:1c0a769988ee 440
brunofgc 0:1c0a769988ee 441 if(executaComandoServer){
brunofgc 0:1c0a769988ee 442 executaComandoServer = false;
brunofgc 0:1c0a769988ee 443 commands::exec(connID);
brunofgc 0:1c0a769988ee 444 }
brunofgc 0:1c0a769988ee 445
brunofgc 0:1c0a769988ee 446 if(modemCom::atendendoWebServer){
brunofgc 0:1c0a769988ee 447 modemCom::atendendoWebServer = false;
brunofgc 0:1c0a769988ee 448 pc.printf("Atendendo webServer.\r\n");
brunofgc 0:1c0a769988ee 449 modemCom::webServer();
brunofgc 0:1c0a769988ee 450 }
brunofgc 0:1c0a769988ee 451 /*
brunofgc 0:1c0a769988ee 452 if(enviaSendData){
brunofgc 0:1c0a769988ee 453 enviaSendData = false;
brunofgc 0:1c0a769988ee 454 modemCom::sendBufferCommandMode(connID,"sendData\r\n",10);
brunofgc 0:1c0a769988ee 455 }
brunofgc 0:1c0a769988ee 456 */
brunofgc 0:1c0a769988ee 457 /*if(eventosRTC::rotina1hora){
brunofgc 0:1c0a769988ee 458 osDelay(1000); //Para dar tempo de a threadComunicações ver que tinha uma rotina de 1 hora agendada.
brunofgc 0:1c0a769988ee 459 eventosRTC::rotina1hora = false;
brunofgc 0:1c0a769988ee 460 //Executo abaixo a rotina de 1 hora
brunofgc 0:1c0a769988ee 461 }*/
brunofgc 0:1c0a769988ee 462 }
brunofgc 0:1c0a769988ee 463 }