document.getElementById

My issue lies with char not being translated to the buffer.

Here is the underlying code:

char InternaltemperatureSwitch[3] = "On";
....
char buffer[128];
sprintf(buffer,"<script>document.getElementById(\"Current_Internal_Temperature\").innerHTML=%s;</script>", InternaltemperatureSwitch);
Serial.println(InternalTemperatureSwitch);
Serial.println(buffer);
client.println(buffer);

I want it to print "On", but that doesn't seem to happen. With troubleshooting I figured out that InternalTemperatureSwitch is not being transferred correctly.

Serial.println(InternalTemperatureSwitch) says "n" at 38400 baud
Serial.println(buffer) says ""

So the conversion from one array to another isn't working for some reason. Any ideas?

Try:

char InternaltemperatureSwitch[4]; // I assume you want "Off" too, so you'll need 4 elements, not 3, for "Off\0"

strcpy(InternaltemperatureSwitch, "On");

Why do you need a variable at all?

sprintf (buf, ".....%s....", condition ? "On" : "Off") ;

majenko:
Try:

char InternaltemperatureSwitch[4]; // I assume you want "Off" too, so you'll need 4 elements, not 3, for "Off\0"

strcpy(InternaltemperatureSwitch, "On");

I used strcpy and the Serial Monitor showed "On" for the print of InternaltemperatureSwitch, and for the buffer ""
Although the values on my website won't change for some reason. It will work for numbers, but not letters for some reason. Why would this happen?

MarkT:
Why do you need a variable at all?

sprintf (buf, ".....%s....", condition ? "On" : "Off") ;

How would I apply conditions for this syntax? Use a boolean?

milesmiles902:

majenko:
Try:

char InternaltemperatureSwitch[4]; // I assume you want "Off" too, so you'll need 4 elements, not 3, for "Off\0"

strcpy(InternaltemperatureSwitch, "On");

I used strcpy and the Serial Monitor showed "On" for the print of InternaltemperatureSwitch, and for the buffer ""
Although the values on my website won't change for some reason. It will work for numbers, but not letters for some reason. Why would this happen?

Because you don't have any quotes around the string in your JavaScript assignment...?

majenko:
Because you don't have any quotes around the string in your JavaScript assignment...?

Hmm...I am not quite sure if you are speaking of the dynamic java or the static html reference. Here is my Java:

document.getElementById("Current_Internal_Temp").innerHTML=0;

Here is my code that I have in my IDE:

char InternalTemperature[4]; //Array for storage
...
strcpy(InternalTemperature, "On"); //Copy Switch Value
Serial.println(InternalTemperature); //Troubleshooting InternalTemperature in Serial Monitor

char CITbuffer[128]; //Buffer for client
sprintf(CITbuffer,"<script>document.getElementById(\"Current_Internal_Temp\").innerHTML=%s;</script>","InternalTemperature"); 
//Conversion from array to client.

Serial.println(CITbuffer);//Troubleshooting CITbuffer in Serial Monitor
client.println(CITbuffer);

This is the exact code I have, and it still seems to not be printing letters. If I change the %s to a %d I can get it to work for numbers. For some reason it doesn't like %s.

This code...

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

  char InternalTemperature[4]; //Array for storage

  strcpy(InternalTemperature, "On"); //Copy Switch Value
  Serial.println(InternalTemperature); //Troubleshooting InternalTemperature in Serial Monitor

  char CITbuffer[128]; //Buffer for client
  sprintf(CITbuffer,"<script>document.getElementById(\"Current_Internal_Temp\").innerHTML=%s;</script>",InternalTemperature); 

  Serial.println(CITbuffer);//Troubleshooting CITbuffer in Serial Monitor
}

void loop() {
}

...produces this output.

On

Is that what you want?

SurferTim:

On

Is that what you want?

Yes! That is what I want. Although, for some reason it doesn't change the html to the state of the switch. It just stays as "Loading" when a client connects.

I have no clue what the issue is....because the java only changes the html if it is a number and not a letter (or letters).

I am using %s for my pH probe and it works just fine. The "Loading" changes to 3.5, but if I use %s for letters it never changes the html.

IDE Code:

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

  char InternalTemperature[4]; //Array for storage

  strcpy(InternalTemperature, "On"); //Copy Switch Value
  Serial.println(InternalTemperature); //Troubleshooting InternalTemperature in Serial Monitor

  char CITbuffer[128]; //Buffer for client
  sprintf(CITbuffer,"<script>document.getElementById(\"Current_Internal_Temp\").innerHTML=%s;</script>",InternalTemperature); 

  Serial.println(CITbuffer);//Troubleshooting CITbuffer in Serial Monitor
}

void loop() {
}

Java (CIT.js):

document.getElementById("Current_Internal_Temp").innerHTML=0;

HTML reference:

<span id="Current_Internal_Temp">Loading...</span>
...
<script type="text/jscript" src="CIT.js">

This is essentially the references of all the code to go from html->java->IDE. It isn't clear to me what the problem is.

You need double quotes around the new text.

sprintf(CITbuffer,"<script>document.getElementById(\"Current_Internal_Temp\").innerHTML=\"%s\";</script>",InternalTemperature);

Thank you sir. I can now rest in peace.