5 minute Agent Delay with CURL


#1

I am trying to add an automatic shutoff based on reported temperature. When I add this code to the page that the device itself reports its data to, it causes an exact 5 minute (300 seconds) delay, I’m unable to see any reports during this time, then it processes all the backed up requests at once. However, if I put that same code into a page and load it from my computer’s browser, it appears to run instantly and does not disrupt the device. Are there any security issues or reasons that the server would treat my computer/browser differently than it treats an identical request from the imp?

            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
	curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
		'Content-Type: application/json',                                                                                
		'Content-Length: ' . strlen($data_string))                                                                       
	);                                                                                                                   

Thanks in advance.
Jeff


#2

This is rather related to your server, not the imp - the code is running on that server and is unrelated to the imp.

One thing I can see from a quick google is that when you use CURLOPT_POSTFIELDS you appear to be supposed to use CURLOPT_POSTFIELDSIZE to indicate the length, vs rolling your own headers as you are here?

See https://curl.haxx.se/libcurl/c/CURLOPT_POSTFIELDS.html


#3

I’m a bit confused by what you are doing. Is the imp doing the remote shutoff or is the imp doing the reporting? And you are using a PHP script on a website? or an API from somewhere? What exactly is supposed to happen … the process?


#4

I am moving a mechanical actuator to disable a diesel engine. It must pull a cable, and then return to its original position. Without leaving my main page, I want to send a signal to a device pin to set an analog output voltage. Then 15 seconds later I want to reset that voltage to its original output voltage which returns the actuator to its original position.

So, I’m using this in my index.php, and it calls toggle_diesel.php (further below) to send the CURL

<script>
			var refresh_delay = 1000;
		
			var disable_device = function(agentid) {
				
				$("#agent_" + agentid + "_disabler").html("stopping...");
				$("#agent_"+agentid+"_disabler").load(
					"https://something.tech/toggle_diesel.php?agentid="+agentid+"&command=turnoff");
				console.log("https://something.tech/toggle_diesel.php?agentid="+agentid+"&command=turnoff");
				

           		setTimeout(function(){
	           		console.log("Starting " + "https://something.tech/toggle_diesel.php?agentid="+agentid+"&command=turnon");
					$("#agent_" + agentid + "_disabler").html("stopped");
           			$("#agent_" +agentid+ "_disabler") .load(	
           				"https://something.tech/toggle_diesel.php?agentid="+agentid+"&command=turnon");
           			setTimeout(function() { 
           				$("#agent_" + agentid + "_disabler").html("stopped"); 
           			}, 15000);
           		}, 15000);
			}

And I’m using toggle_diesel.php like this:

	        ?php
    //print_r($_REQUEST);

    $data["actuator"]=1;
    $command = $_GET['command'];
    $data[$command]=1;
    //$data["turnoff"]=1;

                                                                     
    //print_r($data);
    $data_string = json_encode($data);

    $id1 = $_GET['agentid'];

    $ch = curl_init('https://agent.electricimp.com/'.$id1.'/');
                                                                         
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($data_string))                                                                       
    );                                                                                                                   

    $result = curl_exec($ch);
    $position = "off";
    if($command == "turnon")
    	$position = "on";
    echo "moving switch to $position<br>";
    ?>

#5

OK, I corrected my reply.


#6

So you have an existing web page that I assume is private (nobody has a link to it)? If you put your imp id in the javascripting on that page, anyone can view the HTML and see your imp id. Do you have at least one limit switch on the mechanism connected to the imp so you can verify the actuator is in the original position?

What you’re doing is very similar to my garage door web app. I have a web page that I can view the door status (using 2 limit switches) and then pulse the door opener to open or close it. It reports back the switches in real-time, like you are wanting to do with JQuery/javascripting.

One other thing I do (and I got a lot of my imp scripting from others here on this forum) is to send an API key with the header. In case anyone else happens to get a hold of my imp id, the PHP sends a key with the CURL POST and the imp has to recognize it. Extra security.

The device electronics: Switches go to imp input pins, and the imp output pin pulses a small relay, which is tied to the garage door opener. Again the same idea you are doing. The imp can switch a large load, like a 12VDC power seat motor (linear actuator) that has a lot of torque … by using a relay w/transistor to switch the higher voltage using a 3.3V imp pin. You probably already have 12VDC at your engine.

Send me a private message/conversation with your email address and I’ll send you copies of my PHP, agent and device.