Internal server error 500 using Bridge Example

Hello people,

I'm having a problem with my Arduino Yùn: the issue consists of an Internal Server Error 500 when connecting trying to read values of pins using the Bridge example:

$ wget http://arduino.local/arduino/digital/13
--2015-04-02 11:21:18--  http://arduino.local/arduino/digital/13
Resolving arduino.local (arduino.local)... 192.168.240.1
Connecting to arduino.local (arduino.local)|192.168.240.1|:80... connected.
HTTP request sent, awaiting response... 500 OK
2015-04-02 11:21:24 ERROR 500: OK.

I'm running latest version of OpenWrtYun (14 november 2014) and I uploaded the example using latest Arduino IDE (1.6.2).

Having a look at uhttpd source code suggest that the error may be caused by one of those conditions:

  • Out of memory
  • Broken Pipe
  • Lua runtime error

As you can see from the output of wget (or using Chrome inspector) there is no clue of which of them caused the error and uhttpd doesn't write any error log...

Any help would be appreciated.

Thank you in advance.

Another possibility is that your sketch is not loaded or running properly. For a request of the form /arduino/ to work properly, the sketch must have a properly set up YunServer object listening on the localhost for incoming connections. I get that same 500 error if I don't have the correct sketch loaded and running.

Might the wrong sketch be loaded? Did you make any changes to the sketch that might prevent it from running properly? Did the sketch initialize the Bridge interface properly?

No mistake in the .ino file: I'm using the Bridge example provided with the IDE and to be 100% sure I just uploaded the following code I took on Github :wink:

I forgot to mention that If pointing my browser to http://arduino.local/arduino/digital/13/0 actually turn off the led... I mean the "arduino part" is working and receiving the message. It's the http response that doesn't come out properly...

/*
  Arduino Yún Bridge example

 This example for the Arduino Yún shows how to use the
 Bridge library to access the digital and analog pins
 on the board through REST calls. It demonstrates how
 you can create your own API when using REST style
 calls through the browser.

 Possible commands created in this shetch:

 * "/arduino/digital/13"     -> digitalRead(13)
 * "/arduino/digital/13/1"   -> digitalWrite(13, HIGH)
 * "/arduino/analog/2/123"   -> analogWrite(2, 123)
 * "/arduino/analog/2"       -> analogRead(2)
 * "/arduino/mode/13/input"  -> pinMode(13, INPUT)
 * "/arduino/mode/13/output" -> pinMode(13, OUTPUT)

 This example code is part of the public domain

 http://arduino.cc/en/Tutorial/Bridge

 */

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

// Listen to the default port 5555, the Yún webserver
// will forward there all the HTTP requests you send
YunServer server;

void setup() {
  // Bridge startup
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Bridge.begin();
  digitalWrite(13, HIGH);

  // Listen for incoming connection only from localhost
  // (no one from the external network could connect)
  server.listenOnLocalhost();
  server.begin();
}

void loop() {
  // Get clients coming from server
  YunClient client = server.accept();

  // There is a new client?
  if (client) {
    // Process request
    process(client);

    // Close connection and free resources.
    client.stop();
  }

  delay(50); // Poll every 50ms
}

void process(YunClient client) {
  // read the command
  String command = client.readStringUntil('/');

  // is "digital" command?
  if (command == "digital") {
    digitalCommand(client);
  }

  // is "analog" command?
  if (command == "analog") {
    analogCommand(client);
  }

  // is "mode" command?
  if (command == "mode") {
    modeCommand(client);
  }
}

void digitalCommand(YunClient client) {
  int pin, value;

  // Read pin number
  pin = client.parseInt();

  // If the next character is a '/' it means we have an URL
  // with a value like: "/digital/13/1"
  if (client.read() == '/') {
    value = client.parseInt();
    digitalWrite(pin, value);
  }
  else {
    value = digitalRead(pin);
  }

  // Send feedback to client
  client.print(F("Pin D"));
  client.print(pin);
  client.print(F(" set to "));
  client.println(value);

  // Update datastore key with the current pin value
  String key = "D";
  key += pin;
  Bridge.put(key, String(value));
}

void analogCommand(YunClient client) {
  int pin, value;

  // Read pin number
  pin = client.parseInt();

  // If the next character is a '/' it means we have an URL
  // with a value like: "/analog/5/120"
  if (client.read() == '/') {
    // Read value and execute command
    value = client.parseInt();
    analogWrite(pin, value);

    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" set to analog "));
    client.println(value);

    // Update datastore key with the current pin value
    String key = "D";
    key += pin;
    Bridge.put(key, String(value));
  }
  else {
    // Read analog pin
    value = analogRead(pin);

    // Send feedback to client
    client.print(F("Pin A"));
    client.print(pin);
    client.print(F(" reads analog "));
    client.println(value);

    // Update datastore key with the current pin value
    String key = "A";
    key += pin;
    Bridge.put(key, String(value));
  }
}

void modeCommand(YunClient client) {
  int pin;

  // Read pin number
  pin = client.parseInt();

  // If the next character is not a '/' we have a malformed URL
  if (client.read() != '/') {
    client.println(F("error"));
    return;
  }

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

  if (mode == "input") {
    pinMode(pin, INPUT);
    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" configured as INPUT!"));
    return;
  }

  if (mode == "output") {
    pinMode(pin, OUTPUT);
    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" configured as OUTPUT!"));
    return;
  }

  client.print(F("error: invalid mode "));
  client.print(mode);
}

mondonerd:
No mistake in the .ino file:

Well, it was worth a try.

The L13 LED is turned on after the Bridge starts up. Just to be 100% sure, that light is coming on, right? (Just making sure there wasn't a problem starting the bridge, which would be one cause of the error.)

The L13 LED is turned on after the Bridge starts up. Just to be 100% sure...

Yes. Bridge is working properly and you can find it executed on the Linux side:

$ ssh root@arduino.local
Warning: Permanently added the RSA host key for IP address '192.168.1.121' to the list of known hosts.
root@arduino.local's password: 


BusyBox v1.19.4 (2014-11-13 19:03:47 CET) built-in shell (ash)
Enter 'help' for a list of built-in commands.

  _______                     ________        __
 |       |.-----.-----.-----.|  |  |  |.----.|  |_
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
 |_______||   __|_____|__|__||________||__|  |____|
          |__| W I R E L E S S   F R E E D O M
 -----------------------------------------------------


root@Arduino:~# ps | grep bridge
 3732 root      5840 S    python -u bridge.py
 7991 root      1484 S    grep bridge

OK, good to be sure. That takes care of the causes for when I've seen the 500 error. Sorry, but I tried. Hopefully someone else will have suggestions.

mondonerd:
::::SNIP::::
Having a look at uhttpd source code suggest that the error may be caused by one of those conditions:

  • Out of memory
  • Broken Pipe
  • Lua runtime error

As you can see from the output of wget (or using Chrome inspector) there is no clue of which of them caused the error and uhttpd doesn't write any error log...

Any help would be appreciated.

Thank you in advance.

@mondonerd,

We've seen this before. We'll go through the paces.

Is this a recurring error?
Either way can you recall what software is running at the time?

Jesse

jessemonroy650:
Is this a recurring error?
Either way can you recall what software is running at the time?

Hi jesse,

this is a persistente error. :-/

The software running is the following:

# ps
  PID USER       VSZ STAT COMMAND
    1 root      1496 S    init
    2 root         0 SW   [kthreadd]
    3 root         0 SW   [ksoftirqd/0]
    4 root         0 SW   [kworker/0:0]
    5 root         0 SW   [kworker/u:0]
    6 root         0 SW<  [khelper]
    7 root         0 SW   [kworker/u:1]
   90 root         0 SW   [sync_supers]
   92 root         0 SW   [bdi-default]
   94 root         0 SW<  [kblockd]
  106 root         0 SW   [khubd]
  132 root         0 SW   [kswapd0]
  180 root         0 SW   [fsnotify_mark]
  211 root         0 SW<  [ath79-spi]
  222 root         0 SW   [mtdblock0]
  227 root         0 SW   [mtdblock1]
  232 root         0 SW   [mtdblock2]
  237 root         0 SW   [mtdblock3]
  242 root         0 SW   [mtdblock4]
  247 root         0 SW   [mtdblock5]
  252 root         0 SW   [mtdblock6]
  257 root         0 SW   [mtdblock7]
  310 root         0 SW   [kworker/0:1]
  312 root         0 SW   [kworker/u:2]
  328 root         0 SW   [scsi_eh_0]
  329 root         0 SW   [usb-storage]
  473 root         0 SW   [kworker/0:2]
  527 root         0 SWN  [jffs2_gcd_mtd3]
  529 root         0 SW   [flush-mtd-unmap]
  572 root      1524 S    {rcS} /bin/sh /etc/init.d/rcS S boot
  573 root      1492 S    /bin/ash --login
  575 root      1488 S    logger -s -p 6 -t sysinit
  621 root         0 SW<  [cfg80211]
  705 root      1500 S    /sbin/syslogd -C16
  707 root      1484 S    /sbin/klogd
  709 root       880 S    /sbin/hotplug2 --override --persistent --set-rules-file /etc/hotplug2.rules --set-coldplug-cmd /sbin/udevt
  717 root       872 S    /sbin/ubusd
  739 root      1480 S    /sbin/netifd
  805 root      1492 S    udhcpc -p /var/run/udhcpc-eth1.pid -s /lib/netifd/dhcp.script -f -t 0 -i eth1 -C
  850 root      5840 R    python -u bridge.py
 1169 root      1628 S    wpa_supplicant -B -P /var/run/wifi-wlan0.pid -D nl80211 -i wlan0 -c /var/run/wpa_supplicant-wlan0.conf
 1267 root      1500 S    udhcpc -p /var/run/udhcpc-wlan0.pid -s /lib/netifd/dhcp.script -f -t 0 -i wlan0 -C
 1323 root      1492 S    /sbin/watchdog -t 5 /dev/watchdog
 1419 root      1152 S    /usr/sbin/dropbear -P /var/run/dropbear.1.pid -p 22
 1426 root      1564 S    /usr/sbin/uhttpd -f -h /www -r Arduino -x /cgi-bin -t 60 -T 30 -A 1 -n 1 -p 0.0.0.0:80 -C /etc/uhttpd.crt
 1435 root      1696 S    /usr/sbin/dbus-daemon --system
 1456 nobody     948 S    /usr/sbin/dnsmasq -C /var/etc/dnsmasq.conf
 1462 nobody    2168 S    avahi-daemon: running [Arduino.local]
 1465 root       796 S    /usr/sbin/thd --socket /tmp/triggerhappy.socket --triggers /etc/triggerhappy/triggers.d/ --daemon /dev/inp
 1489 root      1492 S    /usr/sbin/ntpd -n -p 0.openwrt.pool.ntp.org -p 1.openwrt.pool.ntp.org -p 2.openwrt.pool.ntp.org -p 3.openw
 1496 root      1496 S N  {uSDaemon} /bin/sh /sbin/uSDaemon
 1619 root      1220 S    /usr/sbin/dropbear -P /var/run/dropbear.1.pid -p 22
 1645 root      1496 S    -ash
 1776 root      1480 S N  sleep 2
 1777 root      1488 R    ps

And free command output is this:

# free
             total         used         free       shared      buffers
Mem:         61116        32892        28224            0         4396
-/+ buffers:              28496        32620
Swap:            0            0            0

Also monitoring CPU usage using top command show a 10%-21% used by bridge while uhttpd 0%

Thank you in advance for helping me figuring this out :wink:

@mondonerd,

since this is happening with the bridge example, and you say the problem happens when you access the bridge AND the website. The next the to do is add some content to the website and see if you still get the error. Either way, the best thing to do is re-load the system upgrade. It looks like you have a corrupt bit somewhere - likely in memory. You can test the file system by running
** **fsck** **
at the command line.

Jesse

I tried to run the following code and It seems the problem is generated by the Bridge.put() function:

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

YunServer server;

void setup() {
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Bridge.begin();
  digitalWrite(13, HIGH);

  server.listenOnLocalhost();
  server.begin();

}

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

  // There is a new client?
  if (client) {
    String command = client.readString();
    command.trim();

    if (command == "test") {
      client.println("hello world");
      String key = "testing";
      String value = "true";
// if I uncomment this then no output is generated!!!
//      Bridge.put(key, value);
    }
    client.stop();
  }
  delay(50);

}

What can it mean? :o

mondonerd:
What can it mean? :o

It sounds to me like something is messed up with the server or network configuration of your Yun. The internal operation of the Bridge actually uses local network sockets to communicate between the various processes, and if that configuration got corrupted, it could affect Bridge.put/get, and any of the special web server URLs that start with /arduino/, /data/, or /mailbox/.

Did you make any server or network/routing configuration changes?

At this point, maybe the best bet is to re-apply the latest system image, starting over again from scratch?

ShapeShifter:
At this point, maybe the best bet is to re-apply the latest system image, starting over again from scratch?

I already re-installed the latest system image before making this test I wrote you about :-/

Another info is that /data/get/D13 or /data/put/D13/1 return proper json answer but the digital pin 13 doesn't turn on/off...

mondonerd:
Another info is that /data/get/D13 or /data/put/D13/1 return proper json answer but the digital pin 13 doesn't turn on/off...

To change the led state you have to pass the command to the sketch. The sketch read the commands and then turn on/off the led and updates the key in the bridge.

I'm trying to figure out what is happening. It is really strange, something (what causes the error) is persistent after a complete system update. Just to be sure, did you follow the sysupgrade procedure?

http://arduino.cc/en/Tutorial/YunSysupgrade

Angelo9999:
To change the led state you have to pass the command to the sketch. The sketch read the commands and then turn on/off the led and updates the key in the bridge.

That's the part that is throwing me for a loop as well. Why does this one function, which requires a complete net --> Linux --> Sketch data path work, but the other functions like reading a bridge key value fails?

@mondonerd,
I'm traveling right now, so sorry for the late response. I'll be travelling Weds and Thurs, in case this get extended, but I expect not.

  1. Have you run fsck? Please let me know either way, if errors are reported. TIA

  2. The documentation for the Bridge says it takes approximately three seconds to become ready. Are you waiting more than 3 seconds before calling the web interface?

  3. Could you please try some bridge.get() commands before you do a bridge.put(), and see if that makes a difference?

TIA
Jesse

i am working with up2 intel board connect through arduino.cc online tool. it going good suddenly it create 500 internal server errror. even it is not upload blinking program. please help me.