Trying to combine two programs, but don't know how to combine their http.onrequest() 's

Dear Imp folk,
I love the Imp. And I built an Imp that reports 5 sensors and controls 4 relays for pumps.

Now, I have two programs that need to be combined. Each one works on its own very nicely, but I really need to have them both operating on my imp at the same time.

Kindly excuse my lack of programming vocabulary. I have begun a course of study to fix that. I mostly copied, cut, pasted and simplified the two programs from some example programs on Sparkfun. They were more specific than the examples on the Electric Imp website.

From the Imp website, I do understand that I can’t have two http.onrequest 's in one program, but that has not helped me figure out a solution.

The first program I used was found at: https://learn.sparkfun.com/tutorials/electric-imp-breakout-hookup-guide/example-2-web-control-request

The second program is found at: https://learn.sparkfun.com/tutorials/electric-imp-breakout-hookup-guide/example-3-web-response

The two programs:
The first of them uses a webpage to control some on/off switches and timers. It uses: http.onrequest(requestHandler); and it uses try, if, and catch stuff.

The second of them reads the pins and reports them to a webpage and it has: http.onrequest(respondImpValues); which calls up the function ‘respondImpValues(request,response){…’ and a whole lot of stuff to allow it to report the imp pins.

I’ve tried to combine the two by adding the stuff implemented by the respondImpValues stuff of the second program inside the try, if…stuff of the first program. That mostly doesn’t work. But… if I comment out either of the http.onrequest lines, then each of the others does work, …but just not at the same time.

Can someone suggest the solution. I apologize in advance for my ignorance.

Thank you.
Jeff

The first one executes based on a person doing something from a website, so it only executes when required.

The 2nd one … do you want the imp to automatically send information about its pins to a website database, email, or text? Or will it be like the first one where it will require a user to “ask” for the pin status?

It’s the 2nd one that needs clarification. I mention this because the imp could be logging things into a database (data collection). Or, you only need to know when you need to know.

Hello museum,
Regarding the 2nd one: it needs to automatically send info about its pins to a website database. That way, it gets graphed at regular intervals. Thanks.

Stupin computer. Meant mlseim

Another question …

So let’s say you have all of the imp device and agent code installed. Do you have an existing webhost account (like with GoDaddy) where you can have all of your HTML pages and PHP scripting to do the data collection and electrical equipment (physical) control?

You will need a webhost account if you want to be able to access your control from the internet (any computer, anywhere in the world).

Your website could also have a log-in, so only people with a password could access it. More PHP scripting would be needed to access the data collection in order to do something with it.

Some people use a remote service (internet of things) to collect data. Some people write HTML into the agent and use that for the control page. I use my shared webhost account for control and data collection (using PHP, MySQLi). I’m not sure how far into this you wish to go.

Question things like:

  1. who is going to be able to control the imp?
  2. who is going to be able to see the data?
  3. how many people are involved in the imp access?
  4. how secure does it need to be?
  5. do people need to be notified (email, text message) for any reason?
  6. will other websites be involved, such as weather underground?
  7. will a remote website be used to schedule anything (like relay switching, etc)?

Yes, I have a web hosting account, too. And I have already made a webpage that picks up the pin values from the agent, and displays them. And I made a separate webpage, with buttons, that can send a request to the agent in order to change the four pump-controlling pins (high or low), and their timers, individually. Both of my pages work, when their respective programs are running in the imp. I have not devoted time yet to customize the web pages for individual access, security, etc. The pages are not so advanced yet as to sending social media notifications or using other websites. But regarding your last question: yes, the purpose is to allow remote website scheduling and switching, and so far that is working fine when I have either half of my eventually-combined programs loaded in the Imp.

I assume you’ve customized the example scripts for yourself.
Can you post both of them …
So there will be 2 agent scripts and 2 device scripts.
Maybe the best way would be to put them into a directory, zip it, and put it on your website. Then give me a URL so I can download it.

We can figure out the best way to combine them. Those examples from 2013 seem a little out-dated, but let’s see what you have now.

I think JSON format is the way to go for sending data both directions. The agent and PHP both work good with JSON.

I want to see what you’ve named for all of your relays and sensors.

OK. Here are the customized example scripts in a zip, plus the two customized example webpages.

@tomswift,

I looked at your scripts.

I want to test sending JSON to the imp agent using PHP.
Now I’m having trouble.
I am going to start a new thread about my JSON problem.

The reason I want to use PHP CURL for posting json is that with your HTML, anyone can view the HTML source and see your agent url. You don’t want people seeing that. So I use PHP CURL to post to the imp. I can post values to the agent using PHP CURL, but I really want to post JSON data.

Your agent can have two parts to it.
Both the sending part, and receiving part.

All of the code below is in the agent.
One part sends out, the other receives when a webpage sends it something.

`
//--------------------------------------------------
// AGENT OUT Sending Device Data to Website API
//--------------------------------------------------
device.on("senddata", function(data) {
// Set URL to your web service

// IMPORTANT:  Make sure URL ends with /

local url = "http://www.yourwebsite.com/irrigation/";
 
// Set Content-Type header to json
local headers = { "Content-Type": "application/json" };
 
// encode data and log
local body = http.jsonencode(data);
server.log(body);
// send data to your web service
http.post(url, headers, body).sendsync();
}); 


//-----------------------------------------------------
// AGENT IN Receiving Data from Website
// ----------------------------------------------------
http.onrequest(function(request, response){
  try 
  {
    // decode the json - if it's invalid json an error will be thrown
    local incoming = http.jsondecode(request.body);
	server.log("Incoming Data: " + incoming.data);

// do stuff here to send data to imp device
// device.send("pin2", data.pin2);

    // send response to whoever hit the agent url
    response.send(200, "OK");
  } 
  catch (e) {
    // send a response indicating invalid JSON
    response.send(500, "Invalid JSON string!");
  }
});
`

Tom,
I started a private “conversation” message (see that).

I appreciate your help. I’m really not sure how to use this new agent. My guess is that I need to change my webpage from producing a ‘get’, into somehow accepting and recording the data sent by the agent. Not sure where to start with that.

And I’m guessing that I need to add to my existing ‘device’ that part of the example led script, such as configuring the pins with their new names,( like pump1 ) and the agent.on stuff, and the timers.

I did try a few things. When I send the pump control webpage, it says my json is bad. I’m thinking that I should add everything that I can to the device side, and complete the Agent In side, and try some more.

It’s gonna take a few days.

OK, got it. Thanks. I’ll try it. Need most of the week .

The bad json in my previous post was my fault. The zipped file example works.

OK, in preparation to use your zipped file examples, it took me over six weeks to learn about Apache and Php. And believe me, I tried relentlessly…but couldn’t get my permissions right. But after all that reading, I finally understood about downloading MAMP, and got that working last night within an hour or so. So, now to your example programs: I’m guessing that I should add the incoming, outgoing and index.php files to the website. And add the AGENT OUT and AGENT IN text together to the Imp Agent. And add the Imp Device text to the Imp, and then run it in my localhost…? to see if they interact. Are they supposed to?

The PHP scripts go into your website, I guess your own server also is a web server. The agent code is for the agent (yes, the 2 sections of In and Out), and the device code for the imp device (which I think you already have working).

But … is your local server accessible from the internet? The agent is on the internet and needs to work with the PHP scripts on your website.

Sounds like you went through a lot, and learned a lot. You might not be out of the woods yet, since PHP requires some configuration. Maybe all of the default settings might be OK?

If you get totally frustrated, maybe you can find a free shared webhost to use temporarily, just for testing.

Hats off to you for having way more patience than I would have had.

OK, answered my own question, and now have it working online with the Imp, and your examples. THANK YOU.