Jurze Jurze - 1 year ago 87
Java Question

Processing: Write HTML file from arduino input

I'm trying to make a plant monitor using an Arduino and processing.
Processing writes an html file based on the sensor input by the Arduino.
WinSCP is monitoring the file created for changes and directly uploads trough FTP when the file has changed.

The Arduino is sending the following to processing via serial:


Using the following code in processing I write an html page with this data:

import processing.serial.*;

Serial myPort;
String dataReading = "";
int lol = 0;
String string0 = "<h1>Jurze Plants <img src=\"https://html-online.com/editor/tinymce/plugins/emoticons/img/smiley-laughing.gif\" alt=\"laughing\" /></h1>";
String string1 = "Moisture Level: ";
String string2 = " %<br> Motorstate: ";
String string3 = "<br> Temperature: ";
String string4 = " &deg;C<br> Humidity: ";
String string5 = "%<br>";

void setup() {
size(500, 500);

myPort = new Serial(this, "COM4", 9600);

void draw() {

String [] dataOutput = {};

void serialEvent(Serial myPort) {
dataReading = myPort.readString();
if (dataReading!=null) {
dataOutput = split(dataReading, '\n');
String [] tempfile = {string0,string1,dataOutput[1],string2,dataOutput[2],string3,dataOutput[3],string4,dataOutput[4],string5 };
println("saving to html file...");

The html code I get the first time is :

<h1>Jurze Plants <img src="https://html-online.com/editor/tinymce/plugins/emoticons/img/smiley-laughing.gif" alt="laughing" /></h1>
Moisture Level: 46 %<br>
Motorstate: 0 <br>
Temperature:31.00 &deg;C <br>
Humidity: 35.00% <br>

Though, after it gets the data from the Arduino for the second time it looks like this:

<h1>Jurze Plants <img src="https://html-online.com/editor/tinymce/plugins/emoticons/img/smiley-laughing.gif" alt="laughing" /></h1>
Moisture Level: %<br>
Motorstate: 46 <br>
Temperature:0 &deg;C <br>
Humidity: 31.00% <br>

I guess there is something wrong with the array?
Any help would be highly appreciated! :D

Answer Source

Time to debug your code! (We can't really do this for you, since we don't have your Arduino.)

Step 1: In your serialEvent() function, use the println() function to print out the value of dataReading. Is the value what you expect?

Step 2: Print out the value of dataOutput. Is that what you expect? Print out each index. Are they all what you expect? Check for extra spaces and control characters.

Step 3: Are the indexes what you expect them to be? I see you're starting with index 1 instead of index 0. Is that what you meant to do?

The point is, you have to print out the values of every variable to make sure they're what you expect. When you find the variable with the wrong value, you can trace back through your code to figure out exactly what's happening.