0

I have an Arduino Yun where I host a webpage. Now I want to display the wifi signal quality on that page. I managed to dig in the system and I am able to pop up this JSON-like array

{
  "conncount": 5,
  "leases": {

  },
  "wan": {
    "proto": "dhcp",
    "ipaddr": "192.168.0.13",
    "link": "/cgi-bin/luci/;stok=235c7aba0f5cbf5cf752ad119ece74e6/admin/network/network/lan",
    "netmask": "255.255.255.0",
    "gwaddr": "192.168.0.1",
    "expires": -1,
    "uptime": 4407,
    "ifname": "wlan0",
    "dns": [
      "192.168.0.1"
    ]
  },
  "membuffers": 15708,
  "connmax": 16384,
  "memfree": 1576,
  "uptime": 4481,
  "wifinets": [
    {
      "device": "radio0",
      "networks": [
        {
          "ifname": "wlan0",
          "encryption": "WPA2 PSK (CCMP)",
          "bssid": "04:A1:51:D3:BC:E8",
          "mode": "Client",
          "quality": 70,
          "noise": -95,
          "ssid": "Skynet-ITA",
          "link": "/cgi-bin/luci/;stok=235c7aba0f5cbf5cf752ad119ece74e6/admin/network/wireless/radio0.network1",
          "assoclist": {
            "04:A1:51:D3:BC:E8": {
              "rx_short_gi": true,
              "noise": -95,
              "rx_mcs": 7,
              "tx_40mhz": true,
              "rx_40mhz": true,
              "tx_rate": 135000,
              "tx_packets": 14825,
              "tx_short_gi": true,
              "rx_packets": 101935,
              "tx_mcs": 6,
              "inactive": 360,
              "rx_rate": 150000,
              "signal": -61
            }
          },
          "txpoweroff": 0,
          "bitrate": 135,
          "txpower": 16,
          "name": "Client \u0022Skynet-ITA\u0022",
          "channel": 6,
          "country": "NL",
          "signal": -61,
          "up": true,
          "frequency": "2.437"
        }
      ],
      "name": "Generic 802.11bgn Wireless Controller (radio0)",
      "up": true
    }
  ],
  "memtotal": 61116,
  "localtime": "Wed Feb  3 12:09:13 2016",
  "memcached": 17596,
  "loadavg": [
    0.04052734375,
    0.125,
    0.20263671875
  ]
}

Unfortunately, this is the way it comes and I can't change it nor do I know how it is produced.

Is there a way to extract the bit that says "Quality": 70?

I tried it with the following code but no luck (it came back as undefined). I used data[3] as it looks like multiple arrays and Quality is in the 4th one.

var q;

$(document).ready(function() {
var refresh = setInterval(getData, 5000);
});

function getData() {
$.ajax({
        type: "POST",
        url: "/cgi-bin/luci/;stok=6ea83154346f9c42e99ac14ae8856b2b?status=1&_=0.16447926725451678",
        dataType: "JSON",
        cahce: "false",
        success: function(data){
        q  = data[3].quality;
}
});
}
3
  • 3
    That value is at data.wifinets[0].networks[0].quality, If you copy/paste into jsonlint.com, and click validate. Its easier to see the structure Commented Feb 3, 2016 at 14:14
  • Thanks for the realignment of my array. I could almost spot it immediately now with the help of the answers below! Sometimes it's easier than you think it is :) Commented Feb 3, 2016 at 14:40
  • Anyone knows why my refreshed div still says undefined though? Commented Feb 3, 2016 at 15:00

3 Answers 3

3

Try to parse it with a json parser (for instance http://json.parser.online.fr/)

Then, you can follow the path for the quality:

data.wifinets[0].networks[0].quality
Sign up to request clarification or add additional context in comments.

1 Comment

It's easier to read when put into a json parser. Op's code wasn't formatted when he submitted his question.
1

try this

 var quality=data['wifinets'][0]['networks'][0]['quality'];
 alert(quality);

2 Comments

Did the job for me. Not to difficult in the end once you see the structure ;) Alert comes up nicely only the div-refresh still say undefined. Bit more coding needed I guess
thanks @Arjan_IO . Feel free to comment here if anything more needed :)
1

The simplest way that works for me is just pasting this json into Chrome developer console like: data = { "conncount": 5, ... }. Chrome presents objects in a readable form. You can work your way through it with expanding collapsed objects/arrays.

enter image description here

2 Comments

Sorry to ask. But is this a program or part of the Chrome browser? In case of the latter, had FF this function too?
This is a part of Google Chrome browser. FF and IE also have this. Just press F12.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.