javascript problem - "ReferenceError: google is not defined"

Hello everyone,

i am trying to create a plot using stk600 and the enc28j60 on a webpage. I found some code which use the google chart api but i face the following error in the firefox debugger : ReferenceError: google is not defined.
My code is belo

client.print("<!DOCTYPE html>");
client.print("<html>");
client.print("<head>");


client.print ("<script type=""text/javascript" "src""=""https://www.google.com/jsapi"">");
client.print("</script>");
client.print("<script type=""text/javascript"">");

// Load the Visualization API and the piechart package.
client.print("google.load('visualization', '1.0', {'packages':['corechart']});");
client.print("</script>");	
	
client.print("<script type=""text/javascript"">");
// Set a callback to run when the Google Visualization API is loaded.
client.print("google.setOnLoadCallback(drawChart);");


// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
client.print("function drawChart() {");

	// Create the data table.
	client.print("var data = new google.visualization.DataTable();");
	client.print("data.addColumn('string', 'Topping');");
	
	client.print("data.addColumn('number', 'Slices');");
	client.print("data.addRows([");
	client.print("['Mushrooms', 3],");
	client.print("['Onions', 1],");
	client.print("['Olives', 1],");
	client.print("['Zucchini', 1],");
	client.print("['Pepperoni', 2]");
	client.print("]);");

	// Set chart 
	client.print("var options = {'title':'How Much Pizza I Ate Last Night',");
		client.print("'width':400,");
	client.print("'height':300};");

	// Instantiate and draw our chart, passing in some options.
	client.print("var chart = new google.visualization.PieChart(document.getElementById('chart_div'));");
	client.print("chart.draw(data, options);");
client.print("}");
client.print("</script>");

client.print("</head>");
client.print("<body>");

client.print("<div id=""chart_div" "style=""width:400; height:300"">""</div>");
client.print("</body>");


client.print("</html>");
client.stop();

Is that snippet you posted Arduino code or Java code?

If it is Arduino code ...
If the word "google" is just meant to be text that is sent by client.print() then the problem is probably due to the compiler getting mixed up between program code and the text. The usual cause of that is a missing closing bracket or semi-colon (or similar) earlier in the code.

What has your question got to do with Java?

If it is Java code ...
This is an Arduino forum

...R

i wrote java because it uses the javascript google apis. I did not get what you meant. Have i done a syntax mistake? The code without the client.print() which i use for the arduino ide works perfect.

panos21g:
i wrote java because it uses the javascript google apis. I did not get what you meant. Have i done a syntax mistake? The code without the client.print() which i use for the arduino ide works perfect.

Now I am even more confused because java and javascript are two completely different things.

It sounds like you have code for an Arduino that uses something else instead of client.print() - what does it use instead?

And it sounds like you are trying to use the Arduino code, modified to use client.print() on some other computer? - what other computer, and what programming language are you using.

I still don't know if you are looking for help writing code for an Arduino program or something else. If you want help with an Arduino you must post your complete Arduino program.

...R

My friend,

i am writing code for the arduino. All i want to do is draw plots for some measurment i take onto a webpage. I found it easy to try with the google apis for this case. client.print is put before every html line. So far everything that had to do with html wokred without problem Now that i try to create these plots i face the above message. Any thoughts?

Thank u in advance

Ahmm..

I'm stuck too. What you posted does have this client.print. Lots of 'em! But, we can't see where client.print is defined. We need the rest of the Arduino sketch and stuff to see where this is coming from and what it means. ('cause we're all pretending to be talking Arduinos in this forum.)

Where's the definitions?

-jim lee

Alright then, here is the full code. There is an extra html part not include to create tables with measure values. Also ignore the delay and the if at the end.

#include <UIPEthernet.h>
float sensorval[8]; 
float temperature[8];
float voltage[8];
EthernetServer server = EthernetServer(100);
String startString;
void setup()
{
  Serial.begin(9600);

  uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
  static  IPAddress myIP(192,168,1,99);

  Ethernet.begin(mac,myIP);

  server.begin();


}
    
 void loop()
{
  size_t size;

  if (EthernetClient client = server.available())
    {
      while((size = client.available()) > 0)
        {
          uint8_t* msg = (uint8_t*)malloc(size);
          size = client.read(msg,size);
          Serial.write(msg,size);
          free(msg);
        
      }
      
         
 for (int i=0;i<3;i++)
 {    
     
      sensorval[i] = analogRead(A0);
      voltage[i] = (sensorval[i]/1024.0) * 3.3;

      temperature[i] = (voltage[i] - .5) * 100;
     
client.print("HTTP/1.0 503 Service Unavailable\r\n");
client.print("Content-Type: text/html\r\n");
client.print("Retry-After: 600\r\n");
client.print("\r\n");
client.print("<!DOCTYPE html>");
client.print("<html>");
client.print("<head>");
client.print("<meta charset=""utf-8""/>");

client.print ("<script type=""text/javascript" "src=""www.google.com/jsapi></script>");

client.print("<script type=""text/javascript"">");

// Load the Visualization API and the piechart package.

client.print("google.load('visualization', '1.0', {'packages':['corechart']});");
client.print("</script>");	
	
client.print("<script type=""text/javascript"">");
// Set a callback to run when the Google Visualization API is loaded.
client.print("google.setOnLoadCallback(drawChart);");


// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
client.print("function drawChart() {");

	// Create the data table.
	client.print("var data = new google.visualization.DataTable();");
	client.print("data.addColumn('string', 'Topping');");
	
	client.print("data.addColumn('number', 'Slices');");
	client.print("data.addRows([");
	client.print("['Mushrooms', 3],");
	client.print("['Onions', 1],");
	client.print("['Olives', 1],");
	client.print("['Zucchini', 1],");
	client.print("['Pepperoni', 2]");
	client.print("]);");

	// Set chart 
	client.print("var options = {'title':'How Much Pizza I Ate Last Night',");
		client.print("'width':400,");
	client.print("'height':300};");

	// Instantiate and draw our chart, passing in some options.
	client.print("var chart = new google.visualization.PieChart(document.getElementById('chart_div'));");
	client.print("chart.draw(data, options);");
client.print("}");
client.print("</script>");

client.print("</head>");
client.print("<body>");

client.print("<div id=""chart_div" "style=""width:400; height:300"">""</div>");
client.print("</body>");


client.print("</html>");
client.stop();
 delay(1000);
 if(i==3)
 i=0;
 }

    }    
}

panos21g:
i am trying to create a plot using stk600 and the enc28j60 on a webpage. I found some code which use the google chart api but i face the following error in the firefox debugger : ReferenceError: google is not defined.

It sounds as if there may be an error in the web page your server is returning. I suggest you look at the source of the page to understand the error message you are getting, and work out what is causing it. Once you know how you want to change the page to correct the problem, implementing that change within the Arduino sketch should be simple enough.

panos21g:
i am trying to create a plot using stk600 and the enc28j60 on a webpage. I found some code which use the google chart api but i face the following error in the firefox debugger : ReferenceError: google is not defined.

My apologies - I missed the reference to firefox debugger.

That suggests the problem is in your javascript code that runs in the browser. If so it is not an Arduino problem.

You would probably be more likely to get assistance from some Forum that deals with javascript.

...R

The reason i write here is that when i run the code without the client.print it runs perfect so i wanted to know i anyone has tried something like this before.

Thank you

panos21g:
The reason i write here is that when i run the code without the client.print it runs perfect so i wanted to know i anyone has tried something like this before.

My guess is that if you don't use client.print() it doesn't send any javascript so there can't be a javascript error.

But you have already been asked to show us your code and you have so far only posted one version of it. We are not mind readers. help us to help you.

...R

Robin i posted my full program code...above. This is the code i compile and load on the arduino...

panos21g:
Robin i posted my full program code...above. This is the code i compile and load on the arduino...

The code in Reply #6 has client.print() all through it. You have said that that causes a problem and omitting it solves the problem. Can you post the code that does NOT have client.print() in it so that we can see the difference.

If your problem is that client.print() is sending invalid javascript to a browser this is not the Forum for advice about correctiing your javascript.

If you know how to formulate valid javascript and you have an Arduino programming problem this is the place to get help. Ideally you should prove that the javascript works when sent from a PC before trying it out with an Arduino. That way you will know that the problem is an Arduino problem.

...R

Sorry i did not understand what you said before. Here is the code without the client.print() and works perfect.

<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Year', 'Sales', 'Expenses'],
          ['2004',  1000,      400],
          ['2005',  1170,      460],
          ['2006',  660,       1120],
          ['2007',  1030,      540]
        ]);

        var options = {
          title: 'Company Performance'
        };

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>

Its a similar code to the above,only the data changes.

This is getting very tedious.

The code you have just posted is NOT Arduino code - it is html/ javascript that runs in your web browser.

I can't help thinking that you are not clear on the distinction between the two.

I have now looked at your HTML code and your Arduino code side by side and I think the problem is that the text that client.print() is required to produce must include quotes (") and since quotes are used at the start and end of client.print() the compiler gets completely confused.

I think quotes that should be included within the text should appear as " so the compiler knows to include them.

...R

Robin when i was talking about the code without the client.print() i meant run the code straight onto the web browser. NOT that it was an arduino code. From examples i have seen so far i did not find a way on how to inlcude html/javascript code in other way rather than puttin client.print() before. If there is any other way please let me know beacause i strongly believe that this is the reason some parts dont work.

Did you read the last 2 paragraphs of my previous reply. Maybe you missed them as I added them as a edit.

when i was talking about the code without the client.print() i meant run the code straight onto the web browser.

The problem I had was that you did not explain this clearly at the outset.
...R