Passing an Arduino Variable to JS

Viewed 227

This may be a very simple question for someone out there.

Say I have an int Arduino vairble:

int sensorData = analogRead(sensorPin);

How would I be able to pass that as a JS variable?

client.println("<script> var dat = sensorData </script>"); // this is what I have tried

For more context, I am trying to pass the variable to update a webpage heading; which does work if I pass in a JS variable, but not an Arduino one:

client.println("<h1 id=\"sensorData\"> %SENSORDATA% </h1>");
client.println("document.getElementById(\"sensorData\").innerHTML = dat");

Any help would be greatly appreciated.

1 Answers

The client class has the print and printLn functions.

so you should be able to split it up and do something like:

client.print("<h1 id=\"sensorData\"> ");
client.print(sensorData);
client.println(" </h1>");
Related