The message logger visualizer writes message data to a file. The log to file visualizer writes all serial data to a file, regardless of how it is sent by your Arduino sketch.
The message logger gives your Arduino program more control over what data is written to the log file by storing only the data sent using the message library. You’ll get a cleaner log file containing only the data chosen by your Arduino sketch, uncluttered by other messages such as commands for plotting data. The message logger and message monitor visualizers use the same data format so you can both view and write message data to file from within MegunoLink. Use message channels with a message logger visualizer to support multiple, simultaneous log files containing different messages.
Creating a Message Logger
Select Message Logger
from the visualizer menu or toolbox to create a new visualizer to configure and write a message log file.
You can create multiple Message Logger visualizers to:
- store data from different connections,
- write logs to multiple files,
- write logs filtered by message channels.
Drag the visualizer tab to organize the windows in your MegunoLink project.
Sending Arduino Data to a Message Logger
Use the Message
class in our library to send data and commands from your Arduino sketch to a message logger visualizer. After installing the MegunoLink Arduino library:
- Add
#include "MegunoLink.h"
to the top of your Arduino sketch. - Create a
Message
variable. - Use methods such as
Send
,Begin
andEnd
to send data and commands to the message logger.
Examples for the message logger can be found in Library Examples &rdarr; MegunoLink &rdarr; MessageLogger in MegunoLink. Each example includes an Arduino code file and a MegunoLink project file. The examples include:
SendCSVData
: write comma separate values to a log file.LogToFiles
: uses a serial command to start an experiment that writes 10 measurements to each of 5 separate files.
Send CSV Data Example
The SendCSVData
example demonstrates sending comma separated values to a Message Monitor or Message Logger. The values are separated from the main serial stream making it easier to load them into another program, such as Microsoft Excel.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include "MegunoLink.h" Message MyCSVMessage("Data"); //"Data" = the taget message channel (remember to select this in megunolink) // When logging make sure you specifiy a path for the logfile and enable logging. void setup() { Serial.begin(9600); } void loop() { // Sends the microcontroller time, and the analog value using a comma separated format // perfect for use with tools like excel and matlab MyCSVMessage.Begin(); Serial.print(millis()); Serial.print(","); Serial.print(analogRead(A0)); Serial.print(","); MyCSVMessage.End(); delay(100); } |
Log To Files Example
The LogToFiles
example demonstrates using the Message Logger to log measurements from the Arduino’s analog input. The program uses a serial command to start an experiment that writes 10 measurements to each of 5 files. The Arduino program sends the data to a Message Logger controlling the log filename.
This example can be found under MegunoLink Examples⇒Message Logger⇒LogToFiles
. It includes a companion MegunoLink project that stores the log files in the My Documents\LogToFile\[Date]\ folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
#include "MegunoLink.h" #include "CommandHandler.h" #include "ArduinoTimer.h" Message Msg; CommandHandler<> Cmds; ArduinoTimer ExperimentTimer; int FileNumber = 1; int MeasurementsSaved = 1; const int MeasurementsPerFile = 10; const int NumberOfRuns = 5; bool ExperimentRunning; void StartNewLogFile(int LogFileNumber) { String FileName("Log"); FileName += LogFileNumber; FileName += ".txt"; Msg.LogTo(FileName); MeasurementsSaved = 0; } void StoreMeasurement() { auto Value = analogRead(0); Msg.Send(millis(), Value); ++MeasurementsSaved; } void Cmd_StartExperiment(CommandParameter &p) { ExperimentRunning = true; FileNumber = 0; StartNewLogFile(++FileNumber); } void Cmd_StopExperiment(CommandParameter &p) { Msg.StopLogging(); ExperimentRunning = false; } void setup() { Serial.begin(9600); Cmds.AddCommand(F("Start"), Cmd_StartExperiment); } void loop() { Cmds.Process(); if (ExperimentRunning) { if (ExperimentTimer.TimePassed_Milliseconds(250)) { StoreMeasurement(); } if (MeasurementsSaved == MeasurementsPerFile) { ++FileNumber; if (FileNumber <= NumberOfRuns) { // Start a new experiment run. StartNewLogFile(FileNumber); } else { // Stop the experiment when all runs have been completed ExperimentRunning = false; Msg.StopLogging(); } } } } |
User Interface
The message logger user interface provides controls to choose the log file path start and stop logging, add time-stamps to the lines as they are written.
Visualizer Toolbar
Tool | Description |
---|---|
Connect/disconnect the selected connection. Use the drop-down to select the connection used by this visualizer. | |
Select the channels processed by the visualizer (message logger only). | |
Open the drop-down menu to control date and time-stamping new lines appended to the log file. | |
Delete the log file (only available when not writing to the log file). | |
Enable/disable writing the log file. Open the drop-down menu to enable/disable logging automatically when the connection is opened. A red + is displayed on the toolbar button whenever content is written to the file. | |
Open online documentation for the visualizer. |
Log File Path
The log file path is resolved automatically each time the log file is opened. It can reference well known locations, such as your documents, Dropbox or MegunoLink project path, include the date and/or time, and be changed from your Arduino sketch.
Click the Configure log path button to open the dialog to view and configure the log file path.
The log filename can be set from your Arduino sketch using the Message
class in our Arduino library. Use the LogTo
method to set the filename and start logging. The filename sent from your Arduino sketch is combined with the base path and folder set in MegunoLink to determine the log file path.
Allow serial commands to set filename and Allow serial commands to start and stop logging must be turned on in the log file path dialog for your Arduino sketch to control logging.
Starting and Stopping Logging
To start logging in MegunoLink, check the Enable logging command on the visualizer toolbar. To stop logging uncheck the Enable logging command.
Logging can be started and stopped from your Arduino sketch using the Message
class in our Arduino library. Use the StartLogging
and StopLogging
methods.
Allow serial commands to start and stop logging must be turned on in the log file path dialog for your Arduino sketch to control logging.
MegunoLink can also start logging automatically whenever a connection is opened. Simply check the Start logging automatically when connection is opened menu item. This can be combined with the Restore on Load option in the Connection Manager to open a connection and automatically start logging when the MegunoLink project is opened.
Adding Time/Date to Log
The time and/or date can automatically be added to the log file by selecting one or both of the options on the time-stamp menu.
When enabled, the current date and time are written to the start of each line in the log file.