Community
OpenEnergyMonitor

Community

API returns "username or password empty" response from localhost

Tags: #<Tag:0x00007f6e0f6725f8> #<Tag:0x00007f6e0f672170> #<Tag:0x00007f6e0f671e00> #<Tag:0x00007f6e0f671658> #<Tag:0x00007f6e0f6706b8>

I’m trying to send temperature from a DS18S20 sensor information to EMONCMS using a PHP script. The response is always the following json:
{“success”:false,“message”:“Username or password empty”}

EMONCMS version is: low-write 9.7.5 | 2016.10.14

I’ve tried the following URLs with curl (where $EmonCMSApiKey is my read/write API key):

$url = 'http://localhost/emoncms/input/post.json?node=19&csv=20875&apikey=' . $EmonCMSApiKey  . '';
$url = 'http://localhost/emoncms/input/post.json?apikey=' . $EmonCMSApiKey  . '&node=19&csv=20875';
$url = 'http://localhost/emoncms/input/post.json?node=19&json={20875}&apikey=' . $EmonCMSApiKey  . '';
$url = 'http://localhost/emoncms/input/post.json?apikey=' . $EmonCMSApiKey  . '&node=19&json={20875}';
$url = 'http://localhost/emoncms/input/post.json?node=19&json={"1":20875}&apikey=' . $EmonCMSApiKey  . '';
$url = 'http://localhost/emoncms/input/post.json?apikey=' . $EmonCMSApiKey  . '&node=19&json={"1":20875}';

When I use the URLs in a Firefox Browser from a different PC (replacing ‘localhost’ with the local IP) and while logged into the web interface, the data is logged correctly. Then again, those URLs work without the ApiKey when logged into the browser. It seems that it is only from the RaspberryPi itself that the URL does not work

I also tried adding the ApiKey to the POST data and the header with the same results:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $EmonCMSApiKey));
curl_setopt($ch, CURLOPT_POSTFIELDS, array('apiKey' => $EmonCMSApiKey));

As another test, I tried using wget in bash, but also the same results.

Any ideas why I can’t post to localhost?

thanks!

Hi
This works nicely in my case:

$url = ‘http://192.168.0.132/emoncms/input/post.json?node=31&json={Out_Temp_C:’ . $data[1] . ‘,Boiler_temp_C:’ . $data[5] . ‘}&apikey=yourownapi’;

Regards, Timo

And then also:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
curl_close ($ch);

i’m using local version and providing apikey works without a problem for me in all my programs / components i created. It even works using curl both http get & http post so not sure what you do wrong i did notice your not always using lowercase (in your http post for example) for “apikey” but not sure if it would have an influence. I also noticed your providing double quotes like “1” which i’m not sure would work try just using 1. Also read/write it should always be write apikey with input api as your writing data and never be read api key. not sure what else, can’t realy test anythinf if you don’t provide the full php script to test myselve to see whats wrong.

Changing to the actual IP Address did the trick. Strange that localhost doesn’t work.

thanks.

I seem to be having this same issue and for the life of my can’t see what I’m missing.

script

<html>
<?php

//http://www.wunderground.com/weather/api/d/documentation.html

//Change the weather underground API and city below
//$json_string = file_get_contents("http://api.wunderground.com/api/xxxxxxx/geolookup/conditions/q/Canada/Edmonton.json");
$json_string = file_get_contents("http://api.wunderground.com/api/xxxxxxx/conditions/q/pws:IALBERTA120.json");
$parsed_json = json_decode($json_string);
$location = $parsed_json->{'current_observation'}->{'observation_location'}->{'city'};

$temp_c = $parsed_json->{'current_observation'}->{'temp_c'};
$windchill_c = $parsed_json->{'current_observation'}->{'windchill_c'};
$feelslike_c = $parsed_json->{'current_observation'}->{'feelslike_c'};
$relative_humidity = $parsed_json->{'current_observation'}->{'relative_humidity'};
$pressure_mb = $parsed_json->{'current_observation'}->{'pressure_mb'};
$wind_kph = $parsed_json->{'current_observation'}->{'wind_kph'};
$wind_gust_kph = $parsed_json->{'current_observation'}->{'wind_gust_kph'};
$dewpoint_c = $parsed_json->{'current_observation'}->{'dewpoint_c'};
$visibility_km = $parsed_json->{'current_observation'}->{'visibility_km'};
$precip_1hr_metric = $parsed_json->{'current_observation'}->{'precip_1hr_metric'};
$precip_today_metric = $parsed_json->{'current_observation'}->{'precip_today_metric'};

echo nl2br ("Current temperature in ${location} is: ${temp_c}\n");
echo nl2br ("Current relative humidity in ${location} is: ${relative_humidity}\n");
echo nl2br ("Current pressure in ${location} is: ${pressure_mb}\n");
echo nl2br ("Current wind speed in ${location} is: ${wind_kph}\n");

//Change the URL and EmonCMS API below

$url = 'http://192.168.1.15/emoncms/input/post.json?node=2&json={humidity:' . $relative_humidity . ',pressure:' . $pressure_mb . ',wind:' . $wind_kph . ',temp:' . $temp_c .',dewpoint:' . $dewpoint_c . ',visibilit
y:' . $visibility_km . ',rain:' . $precip_1hr_metric . ',windchill:' . $windchill_c . ',feelslike:' . $feelslike_c . ',windgust:' . $wind_gust_kph . ',raintoday:' . $precip_today_metric . '}&apikey=XXXXXXXX';

echo $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, "");
$contents = curl_exec ($ch);
curl_close ($ch);
echo $contents

?>
</html>

Output

<html>
Current temperature in Aspen Crescent, Edmonton is: 0.2<br />
Current relative humidity in Aspen Crescent, Edmonton is: 93%<br />
Current pressure in Aspen Crescent, Edmonton is: 1010<br />
Current wind speed in Aspen Crescent, Edmonton is: 0<br />
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{"success":false,"message":"Username or password empty"}</html>

Server Info

Emoncms Version 9.7.7 | 2016.10.29
PHP Version 5.6.27-0+deb8u1 (Zend Version 2.6.0)
Redis Version 2.8.17
HTTP Server Apache/2.4.10 (Raspbian) HTTP/1.1 CGI/1.1 80
Database Version MySQL 5.5.52-0+deb8u1

If I drop the $url into a browser: SUCCESS

192.168.1.58 - - [01/Nov/2016:11:46:22 -0600] "GET /emoncms/input/post.json?node=2&json={humidity:92%,pressure:1010,wind:0,temp:0.1,dewpoint:-1,visibility:N/A,rain:%200,windchill:0,feelslike:0,windgust:0,raintoday:0}&apikey=XXXXXXXXXHTTP/1.1" 200 245 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36"

From php script: FAILURE

192.168.1.15 - - [01/Nov/2016:11:50:13 -0600] "GET /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=XXXXXXXHTTP/1.1" 200 412 "-" "-"

Thank you for the help!!

emonhub reports the following when successful but has done this since the beginning, nothing on a failure

2016-11-01 14:40:40.425|ERROR|input_controller.php|Format error, json value is not numeric
2016-11-01 16:01:08.239|ERROR|input_controller.php|Format error, json value is not numeric
2016-11-01 17:46:22.599|ERROR|input_controller.php|Format error, json value is not numeric

Are you definitely using the correct emoncms readwrite apikey?

Are you logged into emoncms in the browser, at the time you test the url?

Hi Paul,

Yes - definately the correct apikey
Yes - I am logged into the emoncms in the browser when I test the url

Is the url still successful if you log out or open an incognito browser window to try it?

from a incognito is sucessful

192.168.1.58 - - [01/Nov/2016:12:20:11 -0600] “GET /emoncms/input/post.json?node=2&json={humidity:93%,pressure:1010,wind:0,temp:0.2,dewpoint:-1,visibility:N/A,rain:%200,windchill:0,feelslike:0,windgust:0,raintoday:0}&apikey=XXXXXX HTTP/1.1” 200 245 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36”

Is the IP intentionally different? I wondered if you have more than 1 emoncms instance and the apikey is for 192.168.1.58/emoncms not 192.168.1.15/emoncms.

192.168.1.58 is the client/desktop
192.168.1.15 is the raspi/emoncms

The weird this is this has been running solid for the last 2yrs or more and recently it just stopped working. No updates were applied and I was running 9.31. Noticed I was losing data and figured I would update to the latest. Everything else is updating and working correctly.

That’s thrown me, what am I missing? why are you testing a url is good using a different IP?

Does the url work for 192.168.1.15 in the browser?

Do you have 2 emoncms’s? if so do they have the same apikeys?

Sorry Paul,

My raspi/emoncms is headless so I test from my desktop 192.168.1.58.

Below, the log shows 192.168.1.15 (raspi/emoncms) since the script is run locally and from 192.168.1.58 (desktop) when I post it via the browser. I’ll leave the apikey in so you can see.

192.168.1.15 - - [01/Nov/2016:11:44:32 -0600] “POST /emoncms/input/post.json?node=2&json={humidity:92%,pressure:1010,wind:0,temp:0.1,dewpoint:-1,visibility:N/A,rain: 0,windchill:0,feelslike:0,windgust:0,raintoday:0}&apikey=913f557706d069745bfe8dac70040261 HTTP/1.1” 200 412 “-” "-"
192.168.1.58 - - [01/Nov/2016:11:46:22 -0600] “GET /emoncms/input/post.json?node=2&json={humidity:92%,pressure:1010,wind:0,temp:0.1,dewpoint:-1,visibility:N/A,rain:%200,windchill:0,feelslike:0,windgust:0,raintoday:0}&apikey=913f557706d069745bfe8dac70040261 HTTP/1.1” 200 245 “-” "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36"
192.168.1.15 - - [01/Nov/2016:11:50:13 -0600] “GET /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=913f557706d069745bfe8dac70040261 HTTP/1.1” 200 412 “-” “-”

Ahh ok the penny drops, they are “from” not “to” urls.

So for clarity, when entering the “.15” url

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 

directly into an incognito browser on the desktop (.58) it is successful.

Can you successfully “ping” 192.168.1.15 from the pi commandline? Well I guess actually you must be able to, as you are getting the “username or password empty error”. that’s bizarre!

Correct :slight_smile:

if I ping using pi - I get icmp open socket:Operation not permitted

If I sudo ping - I get responses

Would this have anything to do with ufw being installed ? Just guessing…

I followed below for install: (FYI)

[email protected]:~ $ ping 192.168.1.15
ping: icmp open socket: Operation not permitted
[email protected]:~ $ sudo ping 192.168.1.15
PING 192.168.1.15 (192.168.1.15) 56(84) bytes of data.
64 bytes from 192.168.1.15: icmp_seq=1 ttl=64 time=0.224 ms
64 bytes from 192.168.1.15: icmp_seq=2 ttl=64 time=0.180 ms

found the answer to the ping issue:

known bug in one of the recent releases. You can fix it with “sudo chmod u+s /bin/ping”. Then ping should work.


Hi Guys,

Is there anymore information I can gather to find out why this doesn’t work, enable any additional logs?

Hi Bill,

Thanks I’ll try that, but the entire $url works if I paste into a browser window and updates the input correctly, you think its a format issue?

I’ll try and report back.


This inability to post is a pain lol


Hi Bill - no change doing each entry or with POSTFIELDS
Hi Paul - no worries Thank you for the Help

Here is the response from wget:
[email protected]:~ $ 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=913f557706d069745bfe8dac70040261”
–2016-11-02 07:24:48-- 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:%200,windchill:0,feelslike:0,windgust:0,raintoday:0}&apikey=913f557706d069745bfe8dac70040261
Connecting to 192.168.1.15:80… connected.
HTTP request sent, awaiting response… 200 OK
Length: 47 [application/json]
Saving to: ‘STDOUT’

  •                                                  0%[                                                                                                                         ]       0  --.-KB/s             Error: Format error, json value is not numeric
    
  •                                                100%[========================================================================================================================>]      47  --.-KB/s   in 0s
    

2016-11-02 07:24:48 (620 KB/s) - written to stdout [47/47]

with double quotes around key with script:

Current temperature in Aspen Crescent, Edmonton is: 0.3
Current relative humidity in Aspen Crescent, Edmonton is: 93%
Current pressure in Aspen Crescent, Edmonton is: 1015
Current wind speed in Aspen Crescent, Edmonton is: 0
http://192.168.1.15/emoncms/input/post.json?node=2&json={humidity:93%,pressure:1015,wind:0,temp:0.3,dewpoint:-1,visibility:N/A,rain: 0,windchill:0,feelslike:0,windgust:0,raintoday:0}&apikey="913f557706d069745bfe8dac70040261"{"success":false,"message":"Username or password empty"}

Paul - do you have those parse tests you do, could I get a copy and add it to this script. Thx

Could this be a permission setting somewhere, a file or directory that is hit because it is run locally - I’ll double check permissions


Found it!!!

At some point Wunderground changed the value they send for the “RAIN” attribute. It had leading whitespace — which was starring me in the face when I looked at Paul’s post :slight_smile:

added: $precip_1hr_metric = trim($precip_1hr_metric, " ");
removed: double quotes around apikey - indicated invalid key

and voila!!!

Thank you for the help!!

Would still love to see your validation of attributes Paul -thx