Unable to post to raspberry pi locally via http API

I have a Particle Proton that is collecting the power values and it is currently uploading to my online Emoncms account just fine using the below code which is a simple http get API call to post the data. “t” is the power value passed and the nodeID is 0.

request.hostname = "www.emoncms.org";
request.port = 80;
request.path = "/input/post?node="+String(nodeID)+"&json={power:"+String(t)+"}&apikey=REDACTED";
http.get(request, response, headers);

I’ve copied the above method and I’ve tried posting to my local raspberry pi emoncms setup and it just isn’t working.

equest.hostname = "http://localhost/emoncms/";
request.port = 80;
request.path = "/input/post?node="+String(nodeID)+"&json={power:"+String(t)+"}&apikey=REDACTED";
http.get(request, response, headers);

I’m able to post data locally using the API and my browser, but I just don’t understand what it is that I’m missing to having the Proton post. The raspberry pi can ping the Proton so I know they’re able to communicate. What am I missing locally that isn’t a problem when posting the Emoncms cloud? Does the nodeID have to be defined for local posting? If so, why does the cloud Emoncms allow me to set my nodeID to whatever and not have any issues?

Thanks all in advance, and happy holidays!

Bump…

Hi Drew!

I’ve only used the browser type method to post data and have not used the above get method. So I’m not sure I have the best suggestions for you. here is what I’ve used:
https://community.openenergymonitor.org/t/uri-not-acceptable/5928/5?u=jon

 

try hardcoding the node and power (instead of using variables):
“/input/post?node=3&json={power:‘1001’}&apikey=REDACTED”
 

I believe a nodeID is needed. How else would you point the data to the correct input node?

Did you already resolve this? In any case, you need to add the IP-Adress of your emonpi here, instead of localhost. Localhost does only work if your script is also running on your pi.

I haven’t been able to resole the issue even with the changes. I didn’t notice earlier the localhost thing and forgot to post that I had attempted the change.

Here’s the latest as to what I’ve got code wise…

request.hostname = "http://192.168.29.200/emoncms";
request.path = "/input/post?node=6&json={power1:100}";
request.port = 80;
http.get(request, response, headers);
Particle.publish("Local Response",String(response.status));
Particle.publish("Local Response body",String(response.body));

I’ve altered my node 6 to just take one input called power1. As far as my response goes, I’m getting error 400, “400 Bad Request”. Yet when I string the hostname and path together in my browser (http://192.168.29.200/emoncms/input/post?node=6&json={power1:100}), the input shows up in my local emoncms.

Any ideas?

Edit: I have tried with and without the apikey, neither is successful.

First thing is that emoncms.org and the local APIs are slightly different AIUI.

Can you open up the Input page on the local emoncms and click on the API help link in the top right. If you then click the ‘json’ example (check the node does not conflict - if it does copy and edit the link before posting it) - you can delete it after; does the link work?

The 400 error is a server error not an emoncms error.

Just because you can ping it does not mean the web server will accept the connection. Have you any firewall rules setup on the Pi or any other web service like Pi-Hole?

The API is very picky. If possible use a validated JSON object (use https://jsonlint.com/ to check it) and use the fulljson data type. IIRC the json data type only takes values that are not in quotes so {time:10} rather than {time:"10"} or {"time":10} (or any other combination with quotes).

Are you using the EmonPi image?

Hi @Jon

The input API uses the (http) GET method rather than the (http) POST method even though it is posting data. Yes gets me too!

Thanks all. I stumbled across the fix in these threads. Since I’m logging locally, for whatever reason, resolving a hostname does NOT work on my local network so I had to use the IP address instead and then everything worked!

Here’s the code for those that have this issue in the future. Especially since I hate it when I have the same problem and I see old posts with people saying “solved!” without sharing the actual fix.

IPAddress localserver(192,168,29,154); // Your local IP address goes here
request.ip = localserver;
request.path = "/emoncms/input/post?node="+String(nodeID)+"&fulljson={'power':"+String(t)+"}&apikey=**KEY_HERE**";
request.port = 80;
http.get(request, response, headers);
1 Like