#include #include #include "FS.h" #include "SD.h" #include "SPI.h" #include #include #include #include "esp_wpa2.h" /* #define EAP_ANONYMOUS_IDENTITY "" //anonymous identity #define EAP_IDENTITY "262677@ufrgs.br" //user identity #define EAP_PASSWORD "gui19962011:;!?" //eduroam user password const char* ssid = "eduroam"; // eduroam SSID const char* host = "ufrgs.br"; //external server domain for HTTPS connection int counter = 0; const char* test_root_ca = \ "-----BEGIN CERTIFICATE-----\n" \ "MIIFCTCCA/GgAwIBAgIQV0cXGWPWQYpD97TkhRl8LzANBgkqhkiG9w0BAQsFADCB\n" \ "DELMAkGA1UEBhMCQlIxHzAdBgNVBAgTFlJpbyBHcmFuZGUgZG8gU3VsIC0gUlMx\n" \ "FTATBgNVBAcTDFBvcnRvIEFsZWdyZTE6MDgGA1UEChMxVW5pdmVyc2lkYWRlIEZl\n" \ "ZGVyYWwgZG8gUmlvIEdyYW5kZSBkbyBTdWwgLSBVRlJHUzEvMC0GA1UECxMmQ2Vu\n" \ "dHJvIGRlIFByb2Nlc3NhbWVudG8gZGUgRGFkb3MgLSBDUEQxHDAaBgNVBAMTE0FD\n" \ "IFJhaXogZGEgVUZSR1MgdjIwHhcNMTIwNDA5MTQyNTQ2WhcNMzIwNDA5MTQzNTQ1\n" \ "WjCB0DELMAkGA1UEBhMCQlIxHzAdBgNVBAgTFlJpbyBHcmFuZGUgZG8gU3VsIC0g\n" \ "UlMxFTATBgNVBAcTDFBvcnRvIEFsZWdyZTE6MDgGA1UEChMxVW5pdmVyc2lkYWRl\n" \ "IEZlZGVyYWwgZG8gUmlvIEdyYW5kZSBkbyBTdWwgLSBVRlJHUzEvMC0GA1UECxMm\n" \ "Q2VudHJvIGRlIFByb2Nlc3NhbWVudG8gZGUgRGFkb3MgLSBDUEQxHDAaBgNVBAMT\n" \ "E0FDIFJhaXogZGEgVUZSR1MgdjIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK\n" \ "AoIBAQDvCmjv3cCM0wZHaF7fHlFIQwbFimXNG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\n" \ "-----END CERTIFICATE-----\n"; // You can use x.509 client certificates if you want //const char* test_client_key = ""; //to verify the client //const char* test_client_cert = ""; //to verify the client */ const char* ssid = "CLARO_2G75B792"; const char* password = "EF75B792"; long timezone = 1; byte daysavetime = 1; #ifndef ESP32 #error This code is designed to run on ESP32 platform, not Arduino nor ESP8266! Please check your Tools->Board setting. #elif ( ARDUINO_ESP32S2_DEV || ARDUINO_FEATHERS2 || ARDUINO_ESP32S2_THING_PLUS || ARDUINO_MICROS2 || \ ARDUINO_METRO_ESP32S2 || ARDUINO_MAGTAG29_ESP32S2 || ARDUINO_FUNHOUSE_ESP32S2 || \ ARDUINO_ADAFRUIT_FEATHER_ESP32S2_NOPSRAM ) #define USING_ESP32_S2_TIMER_INTERRUPT true #endif // These define's must be placed at the beginning before #include "ESP32TimerInterrupt.h" // _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4 // Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system. // Don't define TIMER_INTERRUPT_DEBUG > 2. Only for special ISR debugging only. Can hang the system. #define TIMER_INTERRUPT_DEBUG 0 #define _TIMERINTERRUPT_LOGLEVEL_ 0 #include "ESP32TimerInterrupt.h" #include "ESP32_ISR_Timer.h" #include // https://github.com/jfturcot/SimpleTimer int x[4]={0, 1, 2, 3}, y[4]={ 1, 2, 3, 4 }, sel_ADC[4]={ 0x48, 0x49, 0x4A, 0x4B }, sel = 0, nR=0, pisca = 5000; double MFC_16i[16], MFC_8d[8]; float ADJ_gain = 0.1875F; /* ADS1115 @ +/- 6.144V gain (16-bit results) */ String DATA; String HORA; String TENSAO_DIFF ="CH01; CH23;"; String TENSAO_CH = "CH0; CH1; CH2; CH3"; String NOME_PASTA; String NOME_TXT_INFO; String NOME_TXT_DIFF; String NOME_TXT_CH; String DADOS_SALVOS; #define led_pin 2 #define CLR 39 #define A 34 #define B 35 #define CK 36 #define RST 0 // Init ESP32 timer 1 ESP32Timer ITimer(1); // Init ESP32_ISR_Timer ESP32_ISR_Timer ISR_Timer; #define SIMPLE_TIMER_MS 10000L #if USING_ESP32_S2_TIMER_INTERRUPT void IRAM_ATTR TimerHandler(void * timerNo) #else void IRAM_ATTR TimerHandler() #endif { #if USING_ESP32_S2_TIMER_INTERRUPT ///////////////////////////////////////////////////////// // Always call this for ESP32-S2 before processing ISR TIMER_ISR_START(timerNo); ///////////////////////////////////////////////////////// #endif #if USING_ESP32_S2_TIMER_INTERRUPT ///////////////////////////////////////////////////////// // Always call this for ESP32-S2 after processing ISR TIMER_ISR_END(timerNo); ///////////////////////////////////////////////////////// #endif } Adafruit_ADS1115 ads; /* Use this for the 16-bit version */ //WiFiClientSecure client; //wifi eduroam // Init SimpleTimer SimpleTimer simpleTimer; void setup() { Serial.begin(115200); Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); Serial.println("Contacting Time Server"); /* WiFi.disconnect(true); //disconnect form wifi to set new wifi connection WiFi.mode(WIFI_STA); //init wifi mode esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_ANONYMOUS_IDENTITY, strlen(EAP_ANONYMOUS_IDENTITY)); //provide identity esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide username esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD)); //provide password esp_wifi_sta_wpa2_ent_enable(); // Diferença entre IDE WiFi.begin(ssid); //connect to wifi while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); counter++; if (counter >= 60) { //after 30 seconds timeout - RST board (on unsucessful connection) ESP.restart(); } } client.setCACert(test_root_ca); //client.setCertificate(test_client_cert); // for client verification - certificate //client.setPrivateKey(test_client_key); // for client verification - private key Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address set: "); Serial.println(WiFi.localIP()); //print LAN IP */ configTime(3600*timezone, daysavetime*3600, "time.nist.gov", "0.pool.ntp.org", "1.pool.ntp.org"); abre_SD (); // You need this timer for non-critical tasks. Avoid abusing ISR if not absolutely necessary. simpleTimer.setInterval(SIMPLE_TIMER_MS, envio_dados); pinMode(led_pin, OUTPUT); digitalWrite(led_pin, LOW); pinMode(CLR, OUTPUT); pinMode(A, OUTPUT); pinMode(B, OUTPUT); pinMode(CK, OUTPUT); pinMode(RST, INPUT); } void loop() { R_rotina(); digitalWrite(led_pin, HIGH); //Blink LED to show that the interrupt works simpleTimer.run(); delay(pisca); digitalWrite(led_pin, LOW); delay(pisca); } void Inicia_MFC (int n) { ads.begin(sel_ADC[n]); ads.setGain(GAIN_TWOTHIRDS); } void Le_MFC () { for (int i = 0; i < 2; i++) { Inicia_MFC (i); MFC_8d[i]=ads.readADC_Differential_0_1(); MFC_8d[i+4]=ads.readADC_Differential_2_3(); } for (int i = 0; i < 2; i++){ Inicia_MFC (i); for (int j = 0; j < 4; j++) MFC_16i[i*4+j]=ads.readADC_SingleEnded(j); } } void envio_dados() { sel=!sel; Le_MFC (); Serial.println(" Inicio da envio "); if (sel==0) { save_DATA_HORA (); appendFile(SD, NOME_TXT_DIFF.c_str(), DATA.c_str()); appendFile(SD, NOME_TXT_DIFF.c_str(), HORA.c_str()); for (int i = 0; i < 2; i++) { Serial.print(" ADC_"); Serial.print(i); Serial.print(" _MFC_1"); Serial.print(" "); Serial.print(MFC_8d[i]*ADJ_gain); Serial.print(" mV "); Serial.print(" Resistor nro: "); Serial.println(nR); TENSAO_DIFF = String (MFC_8d[i]*ADJ_gain) + "; "; escreve_TENSAO_DIFF (); Serial.print(" ADC_"); Serial.print(i); Serial.print(" _MFC_2"); Serial.print(" "); Serial.print(MFC_8d[i+4]*ADJ_gain);Serial.print(" mV "); Serial.print(" Resistor nro: "); Serial.println(nR); TENSAO_DIFF = String (MFC_8d[i+4]*ADJ_gain) + "; "; escreve_TENSAO_DIFF (); } appendFile(SD, NOME_TXT_DIFF.c_str(), "\n"); } else { save_DATA_HORA (); appendFile(SD, NOME_TXT_CH.c_str(), DATA.c_str()); appendFile(SD, NOME_TXT_CH.c_str(), HORA.c_str()); for (int i = 0; i < 8; i++) { Serial.print(" ADC_"); Serial.print(i/4); Serial.print(" _MFC_"); Serial.print(i%4); Serial.print(" "); Serial.print(MFC_16i[i]*ADJ_gain); Serial.print(" mV "); Serial.print(" Resistor nro: "); Serial.println(nR); TENSAO_CH = String (MFC_16i[i]*ADJ_gain) + "; "; escreve_TENSAO_CH (); } appendFile(SD, NOME_TXT_CH.c_str(), "\n"); } Serial.println(" Fim da envio "); } //////////////////////////////////////////////////////////////////////////////////////////////////////////// void save_DATA_HORA (){ struct tm tmstruct; tmstruct.tm_year = 0; tmstruct.tm_mon = 0; tmstruct.tm_mday = 0; tmstruct.tm_hour = 0; tmstruct.tm_min = 0; tmstruct.tm_sec = 0; getLocalTime(&tmstruct, 5000); Serial.printf("\nNow is : %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct.tm_year)+1900,( tmstruct.tm_mon)+1, tmstruct.tm_mday,tmstruct.tm_hour , tmstruct.tm_min, tmstruct.tm_sec); DATA = String((tmstruct.tm_year)+1900) + "/" + String(( tmstruct.tm_mon)+1) + "/" + String(tmstruct.tm_mday) + "; " ; HORA = String(tmstruct.tm_hour) + ":" + String(tmstruct.tm_min) + ":" + String(tmstruct.tm_sec) + "; " ; Serial.println(""); } void nome_PASTA () { struct tm tmstruct; //delay(100); tmstruct.tm_year = 0; tmstruct.tm_mon = 0; tmstruct.tm_mday = 0; tmstruct.tm_hour = 0; tmstruct.tm_min = 0; tmstruct.tm_sec = 0; getLocalTime(&tmstruct, 5000); NOME_PASTA = "/EXP-" + String((tmstruct.tm_year)+1900) + "-" + String(( tmstruct.tm_mon)+1) + "-" + String(tmstruct.tm_mday)+ "-" + String(tmstruct.tm_hour) + "-" + String(tmstruct.tm_min) + "-" + String(tmstruct.tm_sec); } void nome_TXT () { struct tm tmstruct; //delay(500); tmstruct.tm_year = 0; tmstruct.tm_mon = 0; tmstruct.tm_mday = 0; tmstruct.tm_hour = 0; tmstruct.tm_min = 0; tmstruct.tm_sec = 0; getLocalTime(&tmstruct, 5000); NOME_TXT_INFO = String(NOME_PASTA) + "/INFO-" + String(tmstruct.tm_hour) + "-" + String(tmstruct.tm_min) + "-" + String(tmstruct.tm_sec) + ".txt"; NOME_TXT_DIFF = String(NOME_PASTA) + "/v_DIFF-" + String(tmstruct.tm_hour) + "-" + String(tmstruct.tm_min) + "-" + String(tmstruct.tm_sec) + ".txt"; NOME_TXT_CH = String(NOME_PASTA) + "/v_CH-" + String(tmstruct.tm_hour) + "-" + String(tmstruct.tm_min) + "-" + String(tmstruct.tm_sec) + ".txt"; } void abre_SD (){ if(!SD.begin()){ Serial.println("Card Mount Failed"); pisca = 100; return; } uint8_t cardType = SD.cardType(); if(cardType == CARD_NONE){ Serial.println("No SD card attached"); pisca = 100; return; } Serial.print("SD Card Type: "); if(cardType == CARD_MMC){ Serial.println("MMC"); } else if(cardType == CARD_SD){ Serial.println("SDSC"); } else if(cardType == CARD_SDHC){ Serial.println("SDHC"); } else { Serial.println("UNKNOWN"); } uint64_t cardSize = SD.cardSize() / (1024 * 1024); Serial.printf("SD Card Size: %lluMB\n", cardSize); listDir(SD, "/", 0); //removeDir(SD, "mydir"); //removeDir(SD, "EXP-1970-1-1-0"); nome_PASTA (); nome_TXT (); listDir(SD, "/", 0); createDir(SD, NOME_PASTA.c_str()); new_Info_Geral (); } void new_Info_Geral (){ writeFile (SD, NOME_TXT_INFO.c_str(), "Experimento: 01 \n"); appendFile(SD, NOME_TXT_INFO.c_str(), "Data Inicio: \n"); appendFile(SD, NOME_TXT_INFO.c_str(), "Hora inicio: \n"); appendFile(SD, NOME_TXT_INFO.c_str(), "Responsavel: \n"); appendFile(SD, NOME_TXT_INFO.c_str(), "Organismo nome: \n"); writeFile(SD, NOME_TXT_DIFF.c_str(), "Data; Hora; DATA_0_MFC_01; DATA_0_MFC_23; DATA_1_MFC_01; DATA_1_MFC_23 \n"); writeFile(SD, NOME_TXT_CH.c_str(), "Data; Hota; DATA-0_MFC_0; DATA-0_MFC_1; DATA-0_MFC_2; DATA-2_MFC_3; DATA-1_MFC_0; DATA-1_MFC_1; DATA-1_MFC_2; DATA-1_MFC_3 \n"); } void escreve_TENSAO_DIFF (){ appendFile(SD, NOME_TXT_DIFF.c_str(), TENSAO_DIFF.c_str()); } void escreve_TENSAO_CH (){ appendFile(SD, NOME_TXT_CH.c_str(), TENSAO_CH.c_str()); } ///////////////////////////////////////////////////////// void listDir(fs::FS &fs, const char * dirname, uint8_t levels){ Serial.printf("Listing directory: %s\n", dirname); File root = fs.open(dirname); if(!root){ Serial.println("Failed to open directory"); return; } if(!root.isDirectory()){ Serial.println("Not a directory"); return; } File file = root.openNextFile(); while(file){ if(file.isDirectory()){ Serial.print(" DIR : "); Serial.print (file.name()); time_t t= file.getLastWrite(); struct tm * tmstruct = localtime(&t); Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct->tm_year)+1900,( tmstruct->tm_mon)+1, tmstruct->tm_mday,tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec); if(levels){ listDir(fs, file.path(), levels -1); } } else { Serial.print(" FILE: "); Serial.print(file.name()); Serial.print(" SIZE: "); Serial.print(file.size()); time_t t= file.getLastWrite(); struct tm * tmstruct = localtime(&t); Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct->tm_year)+1900,( tmstruct->tm_mon)+1, tmstruct->tm_mday,tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec); } file = root.openNextFile(); } } void createDir(fs::FS &fs, const char * path){ Serial.printf("Creating Dir: %s\n", path); if(fs.mkdir(path)){ Serial.println("Dir created"); //pisca = 500; } else { Serial.println("mkdir failed"); //pisca = 250; } } void removeDir(fs::FS &fs, const char * path){ Serial.printf("Removing Dir: %s\n", path); if(fs.rmdir(path)){ Serial.println("Dir removed"); //pisca = 500; } else { Serial.println("rmdir failed"); //pisca = 250; } } void readFile(fs::FS &fs, const char * path){ Serial.printf("Reading file: %s\n", path); File file = fs.open(path); if(!file){ Serial.println("Failed to open file for reading"); return; } Serial.print("Read from file: "); while(file.available()){ Serial.write(file.read()); } file.close(); } void writeFile(fs::FS &fs, const char * path, const char * message){ Serial.printf("Writing file: %s\n", path); File file = fs.open(path, FILE_WRITE); if(!file){ Serial.println("Failed to open file for writing"); //pisca = 1000; return; } if(file.print(message)){ Serial.println("File written"); //pisca = 500; } else { Serial.println("Write failed"); //pisca = 1000; } file.close(); } void appendFile(fs::FS &fs, const char * path, const char * message){ Serial.printf("Appending to file: %s\n", path); File file = fs.open(path, FILE_APPEND); if(!file){ Serial.println("Failed to open file for appending"); //pisca = 2000; return; } if(file.print(message)){ Serial.println("Message appended"); //pisca = 500; } else { Serial.println("Append failed"); //pisca = 2000; } file.close(); } void renameFile(fs::FS &fs, const char * path1, const char * path2){ Serial.printf("Renaming file %s to %s\n", path1, path2); if (fs.rename(path1, path2)) { Serial.println("File renamed"); } else { Serial.println("Rename failed"); } } void deleteFile(fs::FS &fs, const char * path){ Serial.printf("Deleting file: %s\n", path); if(fs.remove(path)){ Serial.println("File deleted"); } else { Serial.println("Delete failed"); } } void R_rotina() { if (digitalRead(RST)==LOW) { while (digitalRead(RST)==LOW); R_inicio(); nR=0; } else{ if (nR==0) { R_estado0(); R_estado1(); R_estado2(); nR = nR+1; } else { if (nR<=8) { R_estado2(); nR = nR+1; } else { nR=0; } } } } void R_inicio() { digitalWrite(CLR, HIGH); digitalWrite(A, LOW); digitalWrite(B, LOW); digitalWrite(CK, LOW); delay (50); digitalWrite(CLR, LOW); delay (50); digitalWrite(CLR, HIGH); } void R_estado0() { digitalWrite(A, LOW); digitalWrite(B, LOW); digitalWrite(CK, LOW); delay (50); } void R_estado1() { digitalWrite(A, LOW); digitalWrite(B, HIGH); digitalWrite(CK, LOW); delay (50); digitalWrite(CK, HIGH); delay (50); } void R_estado2() { digitalWrite(A, HIGH); digitalWrite(B, LOW); digitalWrite(CK, LOW); delay (50); digitalWrite(CK, HIGH); delay (50); }