3838 * Doxygen: https://www.doxygen.nl/manual/docblocks.html \n
3939 *
4040 * Change Log:
41+ * 2024-03-11 v0.8
42+ * - Added data logger: <IP>/logdata? reads curr_log[] and bemf_log[] from PIC.
43+ * - Added VBsum to "info?" for testing BEMF sum-up for position control.
4144 * 2024-01-29 v0.7.1
4245 * - Processing of flags.version moved to the end of "else-if". (A failed bootload could not be
4346 * repeated, because the initialized "flags.version" always took priority and never was worked off.
@@ -116,6 +119,7 @@ extern int setup_ReadINI (const char *path);
116119extern int setup_WriteCstring (const char *path, const char *identifier, char *s);
117120
118121// *** private function prototypes
122+ extern void getPIClogdata (void );
119123extern void getPICversion (void );
120124
121125// *** data type, constant and macro definitions
@@ -139,7 +143,7 @@ struct FLAGS //!< flags to start tasks within loop()
139143 uint8_t save :1 ; // !< 3 exec save
140144 uint8_t bootload :1 ; // !< 4 update PIC firmware
141145 uint8_t version :1 ; // !< 5 update PIC version
142- uint8_t :1 ; // !< 6
146+ uint8_t logdata :1 ; // !< 6 reads log data from PIC
143147 uint8_t :1 ; // !< 7
144148};
145149
@@ -175,7 +179,7 @@ PubSubClient MQTTclient(espClient);
175179
176180/* * Global variables
177181 * =============== */
178- char ESPversion[32 ] = " v0.7.1 " ; // Version of ESP-Firmware
182+ char ESPversion[32 ] = " v0.8 " ; // Version of ESP-Firmware
179183char PICversion[32 ] = " NN" ; // Version of PIC-Firmware
180184
181185// WiFi credentials: Initial values are used for access point!
@@ -207,12 +211,13 @@ float tempC = 0.0; // temperature in Celsius (DS18B20)
207211float dTemp = 0.0 ; // temperature adjust (ovc.ini)
208212int position[numVZ + 1 ] = {-1 , 0 , 65 , 36 , 100 }; // actual VZ positions (index 0 is dummy)
209213bool refset[numVZ + 1 ]; // home position set?
214+ int vbemf_sum[numVZ + 1 ];
210215
211216// Vars sourced by (html) User Interface
212217struct FLAGS flags; // processing flags (Web UI -> loop)
213218int vz = 0 ; // selected valve zone (motor), [1 .. 4], 0 = none!
214219int set_pos[numVZ + 1 ] = { -1 , 0 , 65 , 36 , 100 }; // valve set positions
215- float max_mA[numVZ + 1 ] = { 0.0 , 25 .0 , 25 .0 , 25 .0 , 25 .0 }; // motor current limits [mA]
220+ float max_mA[numVZ + 1 ] = { 0.0 , 30 .0 , 30 .0 , 30 .0 , 30 .0 }; // motor current limits [mA]
216221
217222char txbuf[64 ];
218223char rxbuf[64 ];
@@ -342,6 +347,7 @@ void setup ()
342347 server.on (" /bootload" , HTTP_GET, webUI_bootload);
343348 server.on (" /move" , HTTP_GET, webUI_move);
344349 server.on (" /home" , HTTP_GET, webUI_home);
350+ server.on (" /logdata" , HTTP_GET, webUI_logdata);
345351 server.on (" /info" , HTTP_GET, webUI_info);
346352 server.on (" /status" , HTTP_GET, webUI_status);
347353 server.on (" /save" , HTTP_GET, webUI_save);
@@ -374,6 +380,7 @@ void loop ()
374380{
375381 unsigned long LoopStamp = millis (); // used to run main loop with constant execution time
376382 int error;
383+ int ival32;
377384 uint16_t uval;
378385 char buf[64 ];
379386 char *p;
@@ -431,7 +438,13 @@ void loop ()
431438 delay (500 );
432439 }
433440 flags.bootload = 0 ;
434- } // if flags.bootload
441+ }
442+
443+ else if (flags.logdata ) // read logdata (curr_log[], bemf_log[]) from PIC
444+ {
445+ getPIClogdata ();
446+ flags.logdata = 0 ;
447+ }
435448
436449 else if (flags.version ) // Update PIC version info
437450 {
@@ -494,8 +507,21 @@ void loop ()
494507 refset[2 ] = REF2 (status) ? 1 : 0 ;
495508 refset[3 ] = REF3 (status) ? 1 : 0 ;
496509 refset[4 ] = REF4 (status) ? 1 : 0 ;
510+ p = strstr (p, " ," );
497511 }
498512 }
513+ if (p) // status word
514+ {
515+ if (sscanf (++p, " 0x%08x" , &ival32) == 1 )
516+ {
517+ vbemf_sum[1 ] = ival32;
518+ // p = strstr(p, ",");
519+ }
520+ else vbemf_sum[1 ] = -1 ;
521+ }
522+ else vbemf_sum[1 ] = -2 ;
523+
524+
499525
500526#ifdef DEBUG_OUTPUT_STATUS
501527 Serial.flush (); // Waits for the transmission of outgoing serial data to complete
@@ -732,3 +758,76 @@ void getPICversion (void)
732758 }
733759
734760} // getPICversion()
761+
762+
763+ /* * @brief Read LogData from PIC and save in LittleFS as file 'logdata.csv'
764+ */
765+ void getPIClogdata (void )
766+ {
767+ unsigned long tstart;
768+ File logfile;
769+ String str;
770+ int error = 0 ;
771+ bool eol;
772+ char c;
773+
774+ logfile = LittleFS.open (" logdata.csv" , " w" );
775+ if (logfile)
776+ {
777+ while (Serial.available () > 0 ) Serial.read (); // flush serial
778+
779+ sprintf (txbuf, " LogData?" ); // send request to PIC
780+ error = cmd2pic ();
781+ if (!error && (strncmp (rxbuf, " LogData:" , 8 ) == 0 )) // compare first N chars of response
782+ {
783+ do
784+ {
785+ rxbuf[0 ] = ' \0 ' ; // flush rxbuf
786+ nrx = 0 ;
787+ eol = 0 ;
788+ tstart = millis ();
789+ for (error = 0 ; !eol && !error; )
790+ {
791+ while (Serial.available () > 0 && !eol)
792+ {
793+ c = Serial.read ();
794+ if (c == ' \r ' ) continue ; // skip
795+ if (c == ' \n ' )
796+ {
797+ if (nrx < 2 ) continue ;
798+ eol = true ;
799+ c = ' \0 ' ;
800+ }
801+ if (nrx < sizeof (rxbuf)) // if not buffer overflow
802+ {
803+ rxbuf[nrx++] = c; // store received char in rxbuf, increment count
804+ }
805+ else rxbuf[sizeof (rxbuf) - 1 ] = ' \0 ' ; // terminate rxbuf
806+ } // while
807+
808+ if ((millis () - tstart) > MAX_ACK_TIME)
809+ {
810+ error = -1 ; // timeout?
811+ }
812+ server.handleClient (); // process WebUI during reception
813+ } // for
814+
815+ if (eol) // we received a record
816+ {
817+ logfile.println (rxbuf);
818+ // logfile.writeString(rxbuf);
819+ }
820+ server.handleClient (); // mandatory
821+ } while (!error); // read until timeout, don't use fix limit
822+ OLED_show (1 , (char *)" Logfile complete!" );
823+ }
824+ logfile.print (" Error: " );
825+ logfile.println (error);
826+ logfile.close ();
827+ }
828+ else
829+ {
830+ OLED_show (1 , (char *)" Can't create logfile" );
831+ }
832+
833+ } // getPIClogdata()
0 commit comments