Input output alternatives

Hi this is my first time posting, i loooked through about 12 pages of topics looking for something that adressed this… if it is available please rederict me to it with my heartfelt appologies… if not please help out a newbie…

I want my imp to check on my rain water tank’s level and send me a full, halffull or empty message. That was very easy to do thanks to the awesome help from the sparkfun tutorials (here example 3 Web response using 2 buoys and input pins 8 and 9) sending a nice JSON message to my agent url webpage.
Once I have this information if it is a sunny day I want to water my plants. I do this by turning on a waterpump connected to a rellay conected to a 3.3-5v level shifter connected to the 5 pin on my imp. If it is raining then i do nothing. This was also a breeze thanks again to the imps own tutorials. The led turning on program…( Getting Started: Agents and Internet apps ) Flawless!!!

I made an app in appinventor that would generate the "Turn the LED5 on by browsing to " + http.agenturl() + “?led5=1” command code at the touch of a button. I then made a separate app that would read the JSON information and figure using input pins 8 and 9 and their inputs 00,01,10,11 of the inputs to decide if my water tank was actually full, half full, or close to empty …tied this information to 4 pictures of said watertank in each state… and boom watertank viewer/pump app!!
Ran each set of programs independently and got great results…
combined them and it was not so cool anymore…

For one I realized I needed two http:onrequest() handlers which i now know to be a nono… So I looked at all the stuff on sparkfun and in adafruit, and here, and the wiki and this is never touched… Devices in all exampleas are all sensors… or all outputs… so here I am requiring a bit of each.
There that is my story, i uploaded my code which is not mine, just snippets i took from aother programs (i think I owe several guys beers) once i realized what they were supposed to do, just threw them and they worked…oon their own at least…

Any help would be appreciated, and if you happen to be in the Republic of Panama during lemon season you can have as many as you want from any of the trees close to the pumps.


Maybe this helps…

For a start only one http:onrequest() handler is needed. A useful guide can be found here:

The example given in this link does not fully explain other useful pieces of data attached to the response parameter, namely request.method and request.path. This enables you to capture unique keys and respective values.

function requestHandler(request, response)
local method = request.method.toupper();
local path = request.path.tolower();

    response.header("Access-Control-Allow-Origin", "*");

    if (method == "POST") 
        // any data posted to your agent link will be captured here
        // you can use "path" to handle different data sets if a path is used
        // then can use the following to see what is contained in "request.body"
        // with post you need to "decode" the request.body first
        local data = http.urldecode(request.body);
        // now can analyse the data set
    if (method == "GET") 
        // any data sent to agent using that "?" after agent link will be captured here
        // you can use "path" to handle different data sets if a path is used
        // then can query "request.query" to see what you have, often using

        if ("setting" in request.query) {
             local settingValue = request.query.setting
    response.send(200, "OK");
catch (exception)
    response.send(500, "Internal Server Error: " + exception);


Awesome…thank you for taking the time…

And you can get a full description of the request object passed into the request handler here.

You may also want to take a look at Rocky.

Rocky is a library we’ve written to make defining HTTP handlers a little easier… here’s a really basic blog post on Rocky to help get you started: