Minimized and with real time in the plots
This commit is contained in:
19
main.go
19
main.go
@@ -12,6 +12,7 @@ import (
|
||||
)
|
||||
|
||||
var dataFile *os.File
|
||||
var startTime time.Time
|
||||
|
||||
type VoltageData struct {
|
||||
Voltage float64 `json:"voltage"`
|
||||
@@ -24,7 +25,7 @@ func initDataFile() {
|
||||
}
|
||||
|
||||
// Generate a filename with timestamp
|
||||
filename := filepath.Join("data", fmt.Sprintf("voltages_%s.txt", time.Now().Format("20060102_150405")))
|
||||
filename := filepath.Join("data", fmt.Sprintf("voltages_%s.csv", time.Now().Format("20060102_150405")))
|
||||
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open data file: %v", err)
|
||||
@@ -32,6 +33,11 @@ func initDataFile() {
|
||||
|
||||
dataFile = file
|
||||
log.Printf("Logging voltage data to %s\n", filename)
|
||||
|
||||
// Write CSV header
|
||||
_, _ = dataFile.WriteString("time_s,voltage\n")
|
||||
|
||||
startTime = time.Now()
|
||||
}
|
||||
|
||||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -54,12 +60,15 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Print voltage to console
|
||||
fmt.Println("Received voltage:", v.Voltage)
|
||||
// Compute relative time
|
||||
relTime := time.Since(startTime).Seconds()
|
||||
|
||||
// Append voltage number to file
|
||||
// Print voltage to console
|
||||
fmt.Printf("Received voltage: %f at time %.3f s\n", v.Voltage, relTime)
|
||||
|
||||
// Append CSV line to file
|
||||
if dataFile != nil {
|
||||
if _, err := dataFile.WriteString(fmt.Sprintf("%f\n", v.Voltage)); err != nil {
|
||||
if _, err := dataFile.WriteString(fmt.Sprintf("%.3f,%.6f\n", relTime, v.Voltage)); err != nil {
|
||||
log.Printf("Failed to write to file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user