Problem with ajax and arduino

Hi,
I am new at Arduino stuff and i have huge problem at getting values from my Yun. In my project i have a counter that's in loop function and just counts to 1000 ( it will be relapsed with sensor). Every time i wont to access to a value over jQuery and Ajax, it returns

"NetworkError: 500 OK - http://192.168.1.20/arduino/hbz1"

I have tried with jsonp it dosent return anything. It dosent return any value even if i just try to access it from web browser.

Thanks for help

What if you put the url in your browser? What's the error message?

I just get the blank page. The error message i get from Firebug. Even if i put just url in browser, the message is the same. Also Serial.println dosent print any messages to my Serial monitor! When i hard code output in sketch then i get what i coded, any other way, even with variables i get nothing and that error in Firebug

The Serial.println issue may mean the problem is starting from the sketch. Can you post the whole sketch?

Also, can you access the linux side (using YunSerialMonitor or via SSH), type

cat /etc/arduino/openwrt-yun-release

and paste here the output?

Here is my sketch:

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

YunServer server;
int count_max = 1000;
int current_count;

void setup() 
{
 Serial.begin(9600);
 pinMode(13,OUTPUT);
 digitalWrite(13,HIGH);
 Bridge.begin();
 server.noListenOnLocalhost();
 server.begin();
 digitalWrite(13,LOW);
}

void loop() {
  
YunClient client = server.accept();  

//this is counter
if(current_count < count_max)
{
  digitalWrite(13,HIGH);
  current_count++;
  Serial.print(String(current_count));
  digitalWrite(13,LOW);
}

  
if (client)
{
  Serial.println("client on");
   digitalWrite(13,HIGH);
    process(client);
    digitalWrite (13, LOW);
  }
  delay(15);
}


void process (YunClient client)
{
  String command = client.readStringUntil('\r');
  if(command == "hbz1")
  {
      Serial.print(String(current_count));
      digitalWrite(13,LOW);
      client.println("Status: 200");
      client.println("Content-type: application/json; charset=utf-8");
      client.println();
      client.println("{\"hbz1\":\""+String(current_count)+"\"}");
      digitalWrite (13, HIGH);
    
  } 
}

I dont know whats wrong in it. If my Serial Monitor worked, i cold see Serial.print statements.

mladenkoml:

  String command = client.readStringUntil('\r');

if(command == "hbz1")
}

Are you sure the URI passed into the 32u4 sketch is terminated with a CR? if it isn't then your sketch returns nothing back to the server. It would explain the Error 500.

If it doubt strip your sketch back to something basic and add logic. Can see where it breaks more easily then.

I made a couple of changes and it works for me.

void setup() {
  Bridge.begin();
  server.listenOnLocalhost();
  server.begin();
}

void loop() {
  YunClient client = server.accept();

  if (current_count < count_max) {
    current_count++;
  }
  
  if (client) {
    process(client);
    client.stop();
  }
  
  delay(15);
}

'process' was as you wrote it.