Commit 8f737304 authored by Guilherme Porto's avatar Guilherme Porto
Browse files

Upload New File

parent 672faa9c
#include <Wire.h>
#include <Adafruit_ADS1X15.h>
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include <time.h>
#include <WiFi.h>
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 <SimpleTimer.h> // 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=8;
int16_t 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 6
//#define A 3
//#define B 4
//#define CK 5
#define RESET 0
// Init ESP32 timer 1
ESP32Timer ITimer(1);
// Init ESP32_ISR_Timer
ESP32_ISR_Timer ISR_Timer;
#define SIMPLE_TIMER_MS 4000L
#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 */
// 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");
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(RESET, INPUT);
}
void loop()
{
//R_rotina();
//envio_dados();
digitalWrite(led_pin, HIGH); //Blink LED to show that the interrupt works
simpleTimer.run();
delay(1000);
digitalWrite(led_pin, LOW);
delay(1000);
}
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(" "); Serial.println(" mV");
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(" "); Serial.println(" mV");
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(" "); Serial.println(" mV");
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");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD card attached");
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");
} else {
Serial.println("mkdir failed");
}
}
void removeDir(fs::FS &fs, const char * path){
Serial.printf("Removing Dir: %s\n", path);
if(fs.rmdir(path)){
Serial.println("Dir removed");
} else {
Serial.println("rmdir failed");
}
}
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");
return;
}
if(file.print(message)){
Serial.println("File written");
} else {
Serial.println("Write failed");
}
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");
return;
}
if(file.print(message)){
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
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(RESET)==HIGH)
{
while (digitalRead(RESET)==HIGH);
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);
}*/
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment