Community
OpenEnergyMonitor

Community

API returns "username or password empty" response from localhost

Tags: #<Tag:0x00007f6e0e941208> #<Tag:0x00007f6e0e941050> #<Tag:0x00007f6e0e940ec0> #<Tag:0x00007f6e0e940d80> #<Tag:0x00007f6e0e940bc8>

Hi CB,

Have you tried sending only one variable in your query string?
e.g. $url = 'http://192.168.1.15/emoncms/input/post.json?node=2&json={humidity:' . relative_humidity'}&apikey=XXXXXXXX';

If that works, then add them back one at a time till you find the troublemaker.

Just a WAG based on this:

412 response code = The precondition given in one or more of the request-header fields evaluated to false when it was tested on the server.

Ref: https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Not sure.

One other thing to try if you haven’t already. Put the fields into CURLOPT_POSTFIELDS
and uncomment curl_setopt($ch, CURLOPT_POST, 1); as well as the CURLOPT_POSTFIELDS line.

Something similar to: https://openenergymonitor.org/emon/node/3675#comment-20537 except you wont need the CURLOPT_HTTPHEADER line

Sorry I disappeared yesterday, I “just popped out” and ended up not getting back til late.

What do you get with

wget -O - "http://192.168.1.15/emoncms/input/post.json?node=2&json={humidity:93%,pressure:1010,wind:0,temp:0.2,dewpoint:-1,visibility:N/A,rain: 0,windchill:0,feelslike:0,windgust:0,raintoday:0}&apikey=XXXXXXXXX"

from the commandline of the pi?

I suggest this test just to confirm if the same url works from the same “local” machine as we know it works from your desktop PC.

I tested your url with my emoncms and found it works fine via wget on the host commandline (and via it’s local browser too), you do get an error message about the “json not numeric” but the data is still posted. I tested this further by removing a char from my apikey and I got “username or password empty error” as expected.

If this is successful it helps confirm there are no dns or routing issues, which would point towards the php script.

You could also try wrapping the apikey in double quotes'}&apikey="XXXXXXXX" ' ;, the error code returned seems quite specific to the authentication/authorization

The “json not numeric” errors are caused by the “visibility:N/A”, this is the 412 error error Bill refers to. When I parse weather wunderground json I include tests so that only fields that exist and have numeric data are added to the url string. The json passed by each weather station can differ and key:value pairs can come and go or be “N/A”, “none” and “null” etc.rather than numeric, in the case of “visibility:N/A” I would expect that to mean visibility isn’t impaired, but emoncms records that as “0” visibility.

The percent symbol doesn’t seem to upset emoncms and the value is accepted, but you probably shouldn’t rely on it,

Glad you got it sorted!

I’m afraid my scripts won’t be much use to you, they are Python not PHP and they also use the bulk upload api (CSV) rather than JSON key:value pairs. I also use a user defined array of “fields” in the conf rather than hard coding, but you maybe able to port parts of it to your script or use it for ideas.

Because of being csv, retaining the order is important so non-numerical values are replaced with null rather than skipped, but I would assume a similar test could be used in php for deciding whether to include the key:value pair.

Here’s an excerpt from a “weather wonderground emonhub interfacer”

        retrieved_data = {}
        try:
            parsed_json = json.loads(self._queue.get())
        except ValueError as e:
            self._log.warning(self.name + " couldn't process data, ValueError: " +
                              str(e.message))
            return

        # Start data frame string with node id
        f =  self._settings['nodeid']

# Extract each value from the returned data
        for datafield in self._settings['datafields']:
            val = str(parsed_json['current_observation'][datafield])
            # Remove "%" symbols
            val = val.replace("%", "")
            # Test for and append only numbers to the data frame string
            try:
                float(val)
                f += " " + str(val)
                continue
            except Exception:
                pass
            # replace any non-numeric values with null 
            f += " null"