Schupp

esp32datalogger

Jan 6th, 2022
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.71 KB | None | 0 0
  1. //#include <avr/pgmspace.h>
  2. //#include <Wire.h>
  3. //#include <SPI.h>
  4. //#include <SD.h>
  5. #include <Streaming.h>
  6. #include <EEPROM.h>
  7. #include "RTClib.h"
  8. #include <Time.h>
  9. #include <TimeLib.h>
  10.  
  11. const uint8_t days_in_month [12] PROGMEM = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  12. const int chipSelect = 10;
  13. //const char string_0[] PROGMEM = "1=0-Fallend 1-Steigend 2-Beide";
  14. //const char string_1[] PROGMEM = "2=Abtast Intervall";
  15. //const char string_2[] PROGMEM = "3=0-Datum+Uhrzeit,1-Timestamp,2-Timestamp+Millis,3-Systemmillis,4-Systemmillis+Differenz";
  16. //const char string_3[] PROGMEM = "4=Ausgabe 0-Serial 1-SD 2-Beides";
  17. //const char string_4[] PROGMEM = "5=Minimale Pulslaenge in Ms";
  18. //const char string_5[] PROGMEM = "6=Entprellung in Ms";
  19. //const char string_6[] PROGMEM = "7=Channel Aktivieren 1=Ein  0=Aus";
  20. //const char string_7[] PROGMEM = "8=Channel wechseln(1-4)";
  21. //const char string_8[] PROGMEM = "9=Speichern!";
  22. //const char string_9[] PROGMEM = "10=RTC setzen unixtime UTC!";
  23. const char string_0[] PROGMEM = "1=0-Falling 1-Rising 2-Both,";
  24. const char string_1[] PROGMEM = "2=Scan Intervall";
  25. const char string_2[] PROGMEM = "3=0-Date+time,1-Timestamp,2-Timestamp+Millis,3-Systemmillis,4-Systemmillis+TimeDiff,";//";
  26. const char string_3[] PROGMEM = "4=Output 0-Serial 1-SD 2-Both";
  27. const char string_4[] PROGMEM = "5=Minimal Signal Duration in Ms";
  28. const char string_5[] PROGMEM = "6=Debounce in Ms";
  29. const char string_6[] PROGMEM = "7=Activate Channel 1=On  0=Off";
  30. const char string_7[] PROGMEM = "8=change Channel (1-4)";
  31. const char string_8[] PROGMEM = "9=Save!";
  32. const char string_9[] PROGMEM = "10=Set RTC unixtime UTC!";
  33. const char* const string_table[] PROGMEM  = {string_0, string_1, string_2, string_3, string_4, string_5, string_6, string_7, string_8, string_9};
  34. //Beispielconfig
  35. int configs[10] = {1, 5, 2, 0, 50, 10, 1, 0, 0, 0};
  36.  
  37. //RTC&Zeit
  38. RTC_DS3231 RTC;
  39.  
  40. String filename = "";
  41.  
  42. #include "SPIFFS.h"
  43. File SD ;
  44. void setup() {
  45.   loadconfigs();
  46.   Serial.begin(115200);
  47.   pinMode(2, INPUT);
  48.   pinMode(3, INPUT);
  49.   pinMode(4, INPUT);
  50.   pinMode(5, INPUT);
  51.   if (! RTC.begin())
  52.   {}
  53.   //RTC.adjust(1580194458+3600);
  54.   setSyncProvider(syncProvider);
  55.   setSyncInterval(1000);
  56. if(!SPIFFS.begin(true)){
  57.       Serial.println("An Error has occurred while mounting SPIFFS");
  58.       return;
  59. }
  60.  
  61.   filename += String(now() / 60 /60 );
  62.   filename += ".csv";
  63.  // attachInterrupt(0, intervallset, CHANGE);
  64. SD  = SPIFFS.open("/"+filename, FILE_WRITE);
  65.  
  66. if(!SD){
  67.      Serial.println("There was an error opening the file for writing");
  68.      return;
  69. }
  70. }
  71.  
  72. #define SECONDS_FROM_1970_TO_2000 946684800
  73. int pins[4] = {2, 3, 4, 5}; //Pinconfig
  74.  
  75. char buffer2[100]; //Serial Buffer
  76. String inputString = ""; //Serial Buffer
  77. boolean stringComplete = false; //Serial Helper
  78.  
  79. int channel = 1; //Menuhelper
  80. boolean menuactive = false; //Menuhelper
  81. int menuchoice = 0; //Menuhelper
  82. int choicevalue = 0; //Menuhelper
  83.  
  84. int laststate[4] = {HIGH, HIGH, HIGH, HIGH}; //Statehelper
  85. int state[4] = {HIGH, HIGH, HIGH, HIGH}; //Statehelper
  86.  
  87. //Testtrash
  88. //int channelorder;
  89. //int chswitch = 1;
  90. //int chswitchbackup;
  91.  
  92. void loop() {
  93.   if (!menuactive)
  94.    intervallset();
  95.   serialmenu();
  96. }
  97.  
  98. //Call between Loops by Serial Input
  99. void serialEvent() {
  100.   while (Serial.available()) {
  101.     // get the new byte:
  102.     char inChar = (char)Serial.read();
  103.     if (!menuactive)
  104.     {
  105.       inputString += inChar;
  106.       if (inChar == '\n') {
  107.         stringComplete = true;
  108.       }
  109.     } else
  110.     {
  111.       if (isDigit(inChar)) {
  112.         inputString += (char)inChar;
  113.       }
  114.       if (inChar == '\n') {
  115.         stringComplete = true;
  116.       }
  117.     }
  118.   }
  119. }
  120.  
  121.  
  122. //MenuPrintout
  123. void menu() {
  124.   Serial.print(hour());
  125.   Serial.print(":");
  126.   Serial.print(minute());
  127.   Serial.print(":");
  128.   Serial.print(second());
  129.   Serial.print("-");
  130.   Serial.print(day());
  131.   Serial.print(".");
  132.   Serial.print(month());
  133.   Serial.print(".");
  134.   Serial.println(year());
  135.   Serial.print("Aktiver Kanal: ");
  136.   Serial.println(channel);
  137.  
  138.   for (int i = 0; i <= 9; i++)
  139.   { int test = ((channel - 1) * 10) + i;
  140.     int tempwert = configs[((((channel - 1) * 10) + i))];
  141.     if (tempwert != -1)
  142.       Serial.print(tempwert);
  143.     spaces(String(tempwert).length());
  144.     strcpy_P(buffer2, (char*)pgm_read_word(&(string_table[i])));
  145.     Serial.println(buffer2);
  146.     delay( 100 );
  147.   }
  148. }
  149.  
  150. //Menucontrol
  151. void serialmenu() {
  152.   // Serial.println(menuchoice);
  153.   if (stringComplete){
  154.     if (!menuactive && inputString == "menu\n")
  155.     {
  156.       menuactive = true;
  157.       stringComplete = false;
  158.       inputString = "";
  159.       //   menuchoice=0;
  160.       menu();
  161.  
  162.     } else if (menuactive && menuchoice == 0)
  163.     {
  164.       menuchoice = inputString.toInt();
  165.       strcpy_P(buffer2, (PGM_P)pgm_read_word(&(string_table[menuchoice - 1])));
  166.       Serial.println(buffer2);
  167.       Serial.println("Neuen Wert eingeben!");
  168.       stringComplete = false;
  169.       inputString = "";
  170.  
  171.     } else if (menuactive && menuchoice == 8)
  172.     {
  173.       choicevalue = inputString.toInt();
  174.       if (choicevalue < 4) {
  175.         channel = choicevalue;
  176.       } else
  177.       {
  178.         Serial.println("Falsche Eingabe");
  179.       }
  180.       stringComplete = false;
  181.       inputString = "";
  182.       menuchoice = 0;
  183.  
  184.       menu();
  185.     } else if (menuactive && menuchoice == 9)
  186.     {
  187.       saveconfigs((channel - 1));
  188.       menuchoice = 0;
  189.       menuactive = false;
  190.       stringComplete = false;
  191.       inputString = "";
  192.       menu();
  193.     } else if (menuactive && menuchoice == 10)
  194.     {
  195.       static time_t tLast;
  196.       time_t t = inputString.toInt() + 3600;
  197.    //   RTC.adjust(t);
  198.       stringComplete = false;
  199.       inputString = "";
  200.       menuchoice = 0;
  201.       menuactive = false;
  202.       menu();
  203.       //dump any extraneous input
  204.  
  205.  
  206.     }
  207.  
  208.     else if (menuactive && menuchoice != 0)
  209.     {
  210.       choicevalue = inputString.toInt();
  211.       configs[((channel - 1) * 10) + (menuchoice - 1)] = choicevalue;
  212.       stringComplete = false;
  213.       inputString = "";
  214.       menuchoice = 0;
  215.  
  216.       menu();
  217.     }}
  218. }
  219.  
  220. //Menuhelper für Zeilen
  221. void spaces(int sizechars) {
  222.   for (int z = 8; z != sizechars; z--)
  223.   {
  224.     Serial.print(" ");
  225.   }
  226.  
  227. }
  228.  
  229. boolean debouncing[4] = {false, false, false, false}; //Entprellt?
  230. boolean mininterval[4] = {true, true, true, true}; //Minimale Impulslänge für Aktion
  231. unsigned long lastintervallmillis[4] = {millis(), millis(), millis(), millis()};
  232. unsigned long changestate[4] = {millis(), millis(), millis(), millis()};
  233. unsigned long lastdebounce[4] = {millis(), millis(), millis(), millis()};
  234. unsigned long lastminpulse[4] = {millis(), millis(), millis(), millis()};
  235. unsigned long mymicros=micros();
  236.  
  237.  
  238. //ControlUnit
  239. void intervallset() {
  240. mymicros=micros();
  241.  //Serial.println(mymicros);
  242.   unsigned long tempmillis = micros();
  243.   for (int i = 0; i <= 3; i++) {
  244.     //Serial.println("start");
  245.          //Serial.println(millis());
  246.    if( configs[(i * 10) + 6] == 1 )
  247.     //Serial.println(micros()-mymicros);
  248.    // int i=0;
  249.    
  250.     if (tempmillis >= lastintervallmillis[i] + configs[(i * 10) + 1] )
  251.     {
  252.        
  253.       //  Serial.println("check state");
  254.       lastintervallmillis[i] = tempmillis;
  255.       int tempstate = digitalRead(pins[i]);
  256.        if(configs[((i * 10) + 3)]==3)
  257.       {
  258.         //Analog einlesen
  259.         Serial.print ("Analog einlesen!");
  260.         }
  261.       else{
  262.       if (tempstate != laststate[i])
  263.       {
  264.         debouncing[i] = true;
  265.         lastminpulse[i] = true;
  266.         laststate[i] = tempstate;
  267.         changestate[i] = tempmillis;
  268.         mininterval[i] = true;
  269.         //Serial.println("Statechange");
  270.       }
  271. //Serial.println(micros()-mymicros);
  272.       if (debouncing[i]) {
  273.         // Serial.println("debouncing");
  274.         if (tempmillis >= changestate[i] + configs[((i * 10) + 5)] && debouncing[i])
  275.         { debouncing[i] = false;
  276.           mininterval[i] = false;
  277.         }
  278.         }
  279.       if (tempmillis >= changestate[i] +  configs[((i * 10) + 4)] && !debouncing[i] && !mininterval[i])
  280.       {
  281.  
  282.         state[i] = laststate[i];
  283.         debouncing[i] = false;
  284.         lastminpulse[i] = false;
  285.         mininterval[i] = true;
  286.         int Flanke = configs[(i * 10)];
  287.         if (Flanke == 2)
  288.         {
  289.           Flanke = tempstate;
  290.         }
  291.         if (state[i] == Flanke) {
  292.           if (configs[((i * 10) + 3)] == 0 || configs[((i * 10) + 3)] == 2)
  293.           {
  294.          //Serial.println(millis());
  295.          String temp=createlogtext(state[i], i, now(), tempmillis);
  296.           char buf[(int)temp.length()+1];
  297.           temp.toCharArray(buf,(int)temp.length()+1 );
  298.        
  299.           }
  300.           if (configs[((i * 10) + 3)] == 1 || configs[((i * 10) + 3)] == 2)  {
  301.            
  302.             if (SD) {
  303.               SD.print( createlogtext(state[i], i, now(), millis()));
  304.               SD.close();
  305.             }
  306.  
  307.           }
  308.         }
  309.    //     Serial.println(micros()-mymicros);
  310.       }
  311.    }
  312.   }
  313.   }
  314. }
  315.  
  316. //Generate Unixtime for RTC Setup
  317. uint32_t get_unixtime(tmElements_t t)
  318. {
  319.   uint8_t i;
  320.   uint16_t d;
  321.   int16_t y;
  322.   uint32_t rv;
  323.   int tmpYear = t.Year + 1970;
  324.   if (tmpYear >= 2000) {
  325.     y = tmpYear - 2000;
  326.   } else {
  327.     return 0;
  328.   }
  329.  
  330.   d = t.Day - 1;
  331.   for (i = 1; i < (int)t.Month; i++) {
  332.     d += pgm_read_byte(days_in_month + i - 1);
  333.   }
  334.   if ((int)t.Month > 2 && y % 4 == 0) {
  335.     d++;
  336.   }
  337.   // count leap days
  338.   d += (365 * y + (y + 3) / 4);
  339.   rv = ((d * 24UL + (int)t.Hour) * 60 + (int)t.Minute) * 60 + (int)t.Second + SECONDS_FROM_1970_TO_2000;
  340.   return rv;
  341. }
  342.  
  343. //print date and time to Serial
  344. void printDateTime(time_t t)
  345. {
  346.   printDate(t);
  347.   Serial << ' ';
  348.   printTime(t);
  349. }
  350.  
  351. //print time to Serial
  352. void printTime(time_t t)
  353. {
  354.   printI00(hour(t), ':');
  355.   printI00(minute(t), ':');
  356.   printI00(second(t), ' ');
  357. }
  358.  
  359. //print date to Serial
  360. void printDate(time_t t)
  361. {
  362.   printI00(day(t), 0);
  363.   Serial << monthShortStr(month(t)) << _DEC(year(t));
  364. }
  365.  
  366. //Print an integer in "00" format (with leading zero),
  367. //followed by a delimiter character to Serial.
  368. //Input value assumed to be between 0 and 99.
  369. void printI00(int val, char delim)
  370. {
  371.   if (val < 10) Serial << '0';
  372.   Serial << _DEC(val);
  373.   if (delim > 0) Serial << delim;
  374.   return;
  375. }
  376.  
  377. void saveconfigs(int channel) {
  378.   for (int i = channel * 10; i <= channel * 10 + 10; i++)
  379.     eepromWriteInt(i * 2, configs[i]);
  380. }
  381.  
  382.  
  383. void loadconfigs() {
  384.   for (int i = 0; i <= 39; i++)
  385.     configs[i] = eepromReadInt(i * 2);
  386. }
  387. void eepromWriteInt(int adr, int wert) {
  388.   byte low, high;
  389.   low = wert & 0xFF;
  390.   high = (wert >> 8) & 0xFF;
  391.   EEPROM.write(adr, low); // dauert 3,3ms
  392.   EEPROM.write(adr + 1, high);
  393.   return;
  394. } //eepromWriteInt
  395. unsigned long lastmicros=millis();
  396. int eepromReadInt(int adr) {
  397.   byte low, high;
  398.   low = EEPROM.read(adr);
  399.   high = EEPROM.read(adr + 1);
  400.   return low + ((high << 8) & 0xFF00);
  401. } //eepromReadInt
  402.  
  403.  
  404. unsigned long laststatemillis1[4];
  405. unsigned long laststatemillis0[4];
  406. String createlogtext(int state, int channel, unsigned long newunixtime, unsigned long newmillis) {
  407.   //"3=0-Datum+Uhrzeit,1-Timestamp,2-TimestampMillis,3-Systemtime";
  408.   String tempstring = "'";
  409.   int checkconfig = configs[((channel - 1) * 10) + 2];
  410.  
  411.   switch (checkconfig) {
  412.     case 0:
  413.  
  414.       tempstring += day();
  415.       tempstring += ".";
  416.       tempstring += month();
  417.       tempstring += ".";
  418.       tempstring += year();
  419.       tempstring += "-";
  420.       tempstring += hour();
  421.       tempstring += ":";
  422.       tempstring += minute();
  423.       tempstring += ":";
  424.       tempstring += second();
  425.       tempstring += "'";
  426.       tempstring += ",";
  427.       tempstring += "'";
  428.       tempstring += channel;
  429.       tempstring += "'";
  430.       tempstring += ",";
  431.       tempstring += "'";
  432.       tempstring += state;
  433.       tempstring += "'";
  434.     tempstring += "\r\n";
  435.       return tempstring;
  436.       break;
  437.     //do something when var equals 1
  438.     case 1:
  439.       tempstring += newunixtime;
  440.       tempstring += "'";
  441.       tempstring += ",";
  442.       tempstring += "'";
  443.       tempstring += channel;
  444.       tempstring += "'";
  445.       tempstring += ",";
  446.       tempstring += "'";
  447.       tempstring += state;
  448.       tempstring += "'";
  449.           tempstring += "\r\n";
  450.       return tempstring;
  451.     case 2:
  452.       tempstring += newunixtime;
  453.       tempstring += "'";
  454.       tempstring += ",";
  455.       tempstring += "'";
  456.       tempstring += newmillis;
  457.       tempstring += "'";
  458.  
  459.       tempstring += ",";
  460.       tempstring += "'";
  461.       tempstring += channel;
  462.       tempstring += "'";
  463.       tempstring += ",";
  464.       tempstring += "'";
  465.       tempstring += state;
  466.       tempstring += "'";
  467.           tempstring += "\r\n";
  468.       return tempstring;
  469.       break;
  470.     case 3:
  471.       tempstring += newmillis-lastmicros;
  472.       lastmicros=newmillis;
  473.       tempstring += "'";
  474.       tempstring += ",";
  475.       tempstring += "'";
  476.       tempstring += channel;
  477.       tempstring += "'";
  478.       tempstring += ",";
  479.       tempstring += "'";
  480.       tempstring += state;
  481.       tempstring += "'";
  482.           tempstring += "\r\n";
  483.       return tempstring;
  484.       break;
  485.     case 4:
  486.       tempstring += newmillis;
  487.       tempstring += "'";
  488.       tempstring += ",";
  489.       tempstring += "'";
  490.       tempstring += channel;
  491.       tempstring += "'";
  492.       tempstring += ",";
  493.       tempstring += "'";
  494.       tempstring += state;
  495.       tempstring += "'";
  496.       tempstring += ",";
  497.       tempstring += "'";
  498.       if (state == 0) {
  499.         tempstring += newmillis - laststatemillis0[channel];
  500.         laststatemillis0[channel] = newmillis;
  501.       } else {
  502.         tempstring += newmillis - laststatemillis1[channel];
  503.         laststatemillis1[channel] = newmillis;
  504.       }
  505.       tempstring += "'";
  506.     tempstring += "\r\n";
  507.       return tempstring;
  508.       break;
  509.     case 5:
  510.       tempstring += newmillis;
  511.       tempstring += "'";
  512.       tempstring += ",";
  513.       tempstring += "'";
  514.       tempstring += channel;
  515.       tempstring += "'";
  516.       tempstring += ",";
  517.       tempstring += "'";
  518.       tempstring += state;
  519.       tempstring += "'";
  520.       tempstring += ",";
  521.       tempstring += "'";
  522.       if (state == 0) {
  523.         tempstring += newmillis - laststatemillis0[channel];
  524.         laststatemillis0[channel] = newmillis;
  525.       } else {
  526.         tempstring += newmillis - laststatemillis1[channel];
  527.         laststatemillis1[channel] = newmillis;
  528.       }
  529.       tempstring += "'";
  530.       tempstring += ",";
  531.       tempstring += "'";
  532.       if (state == 0) {
  533.         tempstring += newmillis - laststatemillis1[channel];
  534.       } else {
  535.         tempstring += newmillis - laststatemillis0[channel];
  536.       }
  537.       tempstring += "'";
  538.       tempstring += "\r\n";
  539.       return tempstring;
  540.       break;
  541.   }
  542.  
  543. }
  544. time_t syncProvider()
  545. {
  546.  return RTC.now().unixtime();
  547. }
  548.  
Advertisement
Add Comment
Please, Sign In to add comment