An archive of community.esquilo.io as of Saturday January 26, 2019.

Use HTTP server without webpage

bluerondo7

Hello,
Is there a way to get a string passed to the nut project without using webpage and js?
I want to pass string commands via Iphone app for control and status. I do this with Arduino and send back a text file with status. I might want to send something like: IP/A12B23C88 etc. Something similar to the Arduino webserver sensor demo.

Thanks

patrick

Esquilo RPC calls are done with standard HTTP POST encoded with JSON. Most HTTP clients (i.e. from many languages/environments) can do this.

Here's a nut file that returns a string sent to it:

function echo(params) {
    return params.str;
}

Here's a curl call from the command line:

curl -X POST http://10.0.23.158/erpc -d '{"method": "echo", "params": {"str": "hello"}}'
{"result":"hello"}

Here's a Python call of the same:

#!/usr/bin/env  ptyhon2
import urllib2
import json

rpc = {'method': 'echo', 'params': {'str': 'hello'}}
request = urllib2.Request('http://10.0.23.158/erpc', json.dumps(rpc))
response = json.loads(urllib2.urlopen(request).read())
print response['result']
Scott_Shumate

To add to Patrick's response, you can also use HTTP GET for Esquilo RPC if you find it more convenient. It is not as semantically correct but it makes it easier to formulate the call because the parameters are encoded in the URL instead of in a JSON payload.

To follow the example:

curl http://10.0.0.123/erpc?method=echo&str=hello
{"result":"hello"}
bluerondo7

Thanks guys.
I'll look into these a bit. I didn't know I could run Python but using RPC looks very promising.

robertjensen

This is great information. I was able to make the curl POST and the Python examples work with the example nut file and successfully adapt them to my own nut file. However, the curl GET gives me the a response {"error":"Invalid method"} when I execute it exactly as given in Scott's example (with the appropriate URL change). Is something missing?

patrick

@robertjensen You may need quotes:

curl "http://10.0.23.107/erpc?method=echo&str=hello"
{"result":"hello"}

Another handy way to test the GET is to just put it in your browser URL:

robertjensen

Thanks. That works. I guess that the shell is interfering somehow.

bluerondo7

I used the browser method and it works great. Very surprised how little code is needed to do this. Very cool.

Is there a way to use a static IP address?

Thanks

patrick

You can set a static IP using the "Wi-Fi Network" menu option, "IP Settings" button. Change the drop-down from DHCP to static, and type in the address you want.

I don't remember off the top of my head if you need to reset after for the change to happen. If it doesn't show up right away at the new address, reset the system from the menu item "Reset" or press the reset button.

bturrie

I'm trying to write a garage door nut that detects when the door is left open and allows me to close it from python or a bash script. The monitoring part is working well, but I want to execute the close without a web page. This appears to be exactly whet I need. Unfortunately I'm missing a lot here. I'm new to Esquillo. If I insert the function into a nut and execute the curl command, nothing seems to happen. I definitely get a missing index error for str on the function line "return params.str".

pjackson

If you have this method in your nut:

function doorStatus()
{
    return "open";
}

You can fetch it with curl, like so:

$ curl -d '{"method":"doorStatus"}' http://10.11.23.153/erpc
{"result":"open"}

Or with Python 3, like so:

from urllib.request import Request, urlopen
import json

rpc = {'method': 'doorStatus'}
doorStatusRequest = Request('http://10.11.23.153/erpc', json.dumps(rpc).encode('utf8'))
response = json.loads(urlopen(doorStatusRequest).read().decode('utf8'))
print('Door status: {}'.format(response['result']))

Python output:

Door status: open
bturrie

Thanks, but my earlier post must have been unclear. Reading the status is already working. I want to use a curl or a python post to have my Esquilo set GPIO(17) high which will activate the opener. FYI: I have Arduino code that closes the door with a NodeMCU I can't get it to also read the door status. My Esquillo will read the door status but can't operate the opener yet. I could use both devices but I'd prefer to do both operations in one device. If I do get it working I'll share it online.

bturrie

Nevermind that post it worked after all. Thanks so much! Bruce