Serial Monitor with HTML capabilities- 'HTML Serial Monitor'

Hi,

For my personal use, I started making an alternative to the Arduino IDE Serial Monitor with html capabilities. I just finished a first version (programmed in Processing 3.1) called 'HTML Serial Monitor' and would like to know what you think and if needs improvement to be useable by other people.

How it works: In your sketch you can insert html codes (e.g. Serial.println("Warning") ;) or adjust the properties of html elements with javascript or special codes (e.g. Serial.println("[myDiv.b:#FF00FF]") ; to change the background color of the div with id 'myDiv'). 'HTML Serial Monitor' receives these messages and runs a webserver. Your favorite browser connect to this webserver (port 8080) and shows your html enhanced debugging messages as a webpage.

Because html enhanced text is larger, the memory overhead for the Arduino can be greater. I have taken some measures to minimize this like implementing abbreviated notations and the possibility to use local html snippets and html template files (both customizable).

A very basic sketch looks like:

void setup(){   
    Serial.begin(9600);
}

String red="Value is: <b style='color:red;'>";
String grn="Value is: <b style='color:green;'>";
String eol="</b>";

void loop(){
  int val = analogRead(analogPin);
  if (val<512)  Serial.println(red+valueD3+eol);    
  if (val>=512) Serial.println(grn+valueD3+eol);    
  delay (300);
}

It also possible to insert html snippets stored in a local file. Some snippets I made, are shown in the following screenshot:

To minimize the Strings inside your sketch it's also possible to make the monitor load a local file. In this example the current value of each Arduino pin is shown graphically and for a selected pin the history is plotted:

#include <HTMLSerialMonitor.h>
HTMLSerialMonitor SerialM;

void setup(){
    SerialM.begin(9600);

    //We setup the gui and start by clearing the page with [cls] 
    SerialM.println("[cls]");

    //We setup the gui and start by clearing the page with [cls]
    SerialM.println("[sketchpath:"+String(__FILE__)+"]");

    //Setup the pins
    for (int i=2;i<14;i++) pinMode(i,INPUT);
}

String logPin="A0";

void loop(){
  //Digital pins
  for (int i=2;i<14;i++) {
    int dR=digitalRead(i);
    String id="pD"+String(i);    
    String col="red";
    if (dR==1) col="green";
    SerialM.print (id,String(dR));
    SerialM.print (id,"b",col);

    id=String(i);
	//if this pin is to be displayed in graph we need to send value to graph control
    if (id.equals(logPin)){
      SerialM.print("osc",String(255+512*dR)); //rescale pin value for nice step 
    }
  }

  //[Analog pins
  for (int i=0;i<8;i++) {
    String id="pA"+String(i);    
    int aR=analogRead(i);
    String val="rgb(200,200,";
    val+=String(255-aR/4);
    val+=")";
    SerialM.print (id,String(round(aR/103)));
    SerialM.print (id,"b",val);

    id="A"+String(i);    
	//if this pin is to be displayed in graph we need to send value to graph control
    if (id.equals(logPin)){ 
      SerialM.print("osc",String(aR));
    }    
  }
  //]
  
  SerialM.println();
  delay(300);
}

//We need serialEvent to let users change the pin displayed in the graph
//If you use SerialEvent to intercept messages you need to 
//use the available and read method from the HTMLSerialMonitor library 
String inputString="";
void serialEvent() {
  while (SerialM.available()) {
      char inChar=(char) SerialM.read();
      inputString += inChar;
      if (inChar == '\n') { 
        logPin=inputString.substring(0,inputString.length()-1);
        inputString="";
      }          
  }
}

More explanation and all source is available on GitHub - HTMLSerialMonitor.

Because html enhanced text is larger, the memory overhead for the Arduino can be greater. I have taken some measures to minimize this like implementing abbreviated notations and the possibility to use local html snippets and html template files (both customizable).

Pissing away resources using the String class will NOT help.

  if (val<512)  Serial.println(red+valueD3+eol);    
  if (val>=512) Serial.println(grn+valueD3+eol);

The + works only because you are using Strings. It is a poor technique to exploit.

Learn to use char arrays and multiple print()s, and see how much your sketch size drops.

Didn't know this and will adapt the code, thx!

I've been looking for something just like this, so I downloaded it and gave it a try. I couldn't get either of the binaries to work. They would just put up the small window filled with either gray, white, or half the display, and then freeze without launching the server. I then loaded the source into Processing (I have 3.1.1) and it worked, but I found the Snippets demo to be a bit erratic in execution speed. Thinking that this might be because I was running it in the IDE, I exported it to an executable. The executable ran, and I got the serial port configuration page, but when I proceeded to the actual application page, I got a series of error dialogs saying "You try to change " of 'myBar', which does not exist!", where 'myBar' is replaced by various other objects. I am new to java, javascript, and HTML, so I am clueless as to what it means, but I am willing to work with you to get this debugged, as I think it would be an awesome program.

Hi,

"You try to change " of 'myBar', which does not exist!"

This appears when the html-element is not present on the page and the browser did not receive the serial data which adds it to the page e.g. if the browser was opened after the Arduino has send this data in the setup loop. The solution is very simple....refresh the browser page. :slight_smile:

I couldn't get either of the binaries to work.

Weird that the binaries did not work! What system did you run these on? Did you have java installed and if so which version?

..but I found the Snippets demo to be a bit erratic in execution speed. Thinking that this might be because I was running it in the IDE, I exported it to an executable.

I ran the processing app always from within the IDE without any erractic behaviour. Which browser are you using? (I tested Edge/IE10/Chrome but mostly tested on chrome.)

NardJ:
Hi,

This appears when the html-element is not present on the page and the browser did not receive the serial data which adds it to the page e.g. if the browser was opened after the Arduino has send this data in the setup loop. The solution is very simple....refresh the browser page. :slight_smile:

Yep. That seemed to work. For my application I'll have to set things up so that they alwaya occur in the proper order and this never happens.

Weird that the binaries did not work! What system did you run these on? Did you have java installed and if so which version?

I'm using Windows 7 64-bit Home Premium. Java is version 8 update 19. Browser is Firefox ver. 47.0. I tried both the 64 bit and the 32 bit binaries with no luck. The binary I compiled locally seems to run fine (after using the refresh trick).

I ran the processing app always from within the IDE without any erractic behaviour. Which browser are you using? (I tested Edge/IE10/Chrome but mostly tested on chrome.)

The erraticness might be because I have a bunch of other stuff which fires up servers in the background. Most of them are National Instruments LabView related. The application will be run on a "clean" unit with only Windows 7 Embedded. Unfortunately, the hardware has not arrived yet, so I cannot test it.

Well, the industrial unit with the Windows 7 Embedded arrived, so I tried the program on it. No luck. :frowning: I just got a gray screen. I had the latest version of Java installed (Java 8 Build 91 or 92). I really don't have the knowledge, time, or resources to debug this, so I guess I'll be moving on. But I thank you for the attempt, and hopefully someone with more knowledge and resources than I will help you get it going.

Strange, I could not reproduce this, but do you maybe have a retina display (problem fixed, see below)? And to be sure, do you mean a gray window or a gray screen?

I just made a copy of the github master and did testing on some pc setups:

  • My regular laptop-pc running windows 10/64bit which has installed java version 8 update 91 (build 1.8.0_91-b15). Both the 32-bit as well as the 64-bit version of HTMLSerialMonitor worked without problem. I also did a reinstall of java and also no problem.
  • A laptop-pc with a java version 7 update 71 (build 1.7.0_71). This throws a "a java exception has occured" due to a java version being older than the one I used to compile de binaries. This is as expected and normal behaviour, but no gray window.
  • Raspberry pi 3 (Samsung monitor) running Raspbian Jessie with installed java version 8 update 65 (build 1.8.0_65-b17). I ran the arm version of HTMLSerialMonitor and this also works.
  • A very old laptop (I remembered I have in the attic and as I suspected is) running Windows 7/32bit (Home Edition, SP1). I upgraded Java to version 1.8.0_91-b15. Started HTMLSerialMonitor and again no problem.
    On the internet I found however that sometimes this problem is caused by a retina display. I fixed this and uploaded a new version (binaries and source) to github. Maybe this new version 20160710 helps your problem!

If not and you can spare another minute or so, could you run it from the command prompt?

  • windows key + r, cmd
  • go to the directory with the build and
  • type 'java -cp lib* HTMLSerialMonitor'.
    This should run the sketch and show errors it produces in the terminal window. If you post the errors, this could help me fix any errors.

@PaulS: I also improved memory usage in the first Arduino sketch example 'Bare_DecoratedText.ino'. I did not implement this in the other examples since I found it worsened readability too much. Any suggestions to improve memory usage without sacrificing readability are most welcome!

The biggest problem with this so far is it can be exploited so easily!

Don't think I understand you. Exploited by Arduino code?

Will look into it further when I get a chance. We've been having a big relocation effort at work, and I've had my desk moved twice in as many days. Once things settle down I'll play with it some more, as it would be great if it would work. BTW, no, the device does not have a retina screen, and it was just the window that was gray. One thing I want to try is to load Processing onto the device and recompile the sources. That worked before, so it looks like there might be some kind of version mismatch somewhere.