Send device log messages out via UART
Messages and application-specific errors sent to the log using the imp API methods server.log() and server.error() are only productive when the imp-enabled device on which they are called can access the Internet. This will not be the case when the device’s network or Internet connection are down, or the unit has been disconnected in software, to preserve battery life, for example.
An alternative means of logging in this case leverages one of the imp’s UART serial buses. In addition to sending the message using the imp API methods mentioned above, it can also be sent out on the UART. Any connected computer equipped with a USB-to-serial adaptor and terminal emulation software can receive and display the messages.
The following code provides a class, Logger, that you can use to log to an available UART TX pin. Add the listing to your device code, then call its log(message) method wherever you want a log message sent to both the UART and to server.log(), or error(message) to post the message via UART and server.error().
You can temporarily enable or disable UART logging with, respectively, the class’ enable() and disable() methods, as show in the second listing, which demonstrates how the class is used.
Note You can instance the class with a specific UART, or allow the class to select one for you according to the type of imp you are using — though this could be one you are already using.