Help to multi links cURL

Hello everyone

I stand with a small setup where I need a button as the default is gray and when pressed late, it will enable multi links via cURL:

192.168.1.10/led?state=TURN+RED+ON

192.168.1.20/led?state=TURN+RED+ON

etc
And turn red and write red is on

When pressed again, it must enable multi links via cURL:

192.168.1.10led?state=TURN+RED+OFF

192.168.1.20led?state=TURN+RED+OFF

etc

192.168.1.8/led?state=TURN+RED+OFF

And get gray again

I do not know anything better than anything but I have been told that cURL php code is the best.

Maybe a simple code for some but I do not know how to start in such a php code :frowning:

Is there anybody inside that can make such a code?

Regards

Jimmy
**** EDITED BY ADMIN (Removed links to local IPs!

So, we removed the H T T P code so the text did not become links…

You have code that does what? You have a website page, assuming PHP that is called led.php.
Then, you want to have buttons on the page to control the LED’s?

Why are you doing this? cURL is only needed if you are reading other pages from another website.
Are you stating that you have a page for each of these IP addresses? Meaning, do you have have a webpage for each of these IP’s and they all switch from page to page to page as you call them or change the buttons? That does not make sense at all.

Please explain what you actually want it to do. So, if you just want buttons with different colors, that is simple to do. If you want each button to call out to other websites (hence the different IP addresses), what are they doing? Are they sending back data to the live page?

IP addresses, even if local 192.168.1.# ones are separate webpages. If you are talking about different computers on your network then, you can just post to them. But, if you use the same controls on each, they would cause issues sending back and forth.

I guess we need to know what you mean by “When pressed, it will enable multi links”??? Does this mean send info to the other website? Lots of ways to do a three-color button switch, but, not sure if that is what you are asking about.

Hi and thanks for the answer

My setup.
I have 8 mcu table where each board controls 4 relays to turn on joints in different colors. Red, yellow, green and blue
It’s no problem to start a color and turn off again on each board but my problem is that I need 1 button that turns the relay on to the red link on all 8 tables

To program each board, I have used this video VIDEO LINK REMOVED BY ADMIN
In addition to these 8 tables I have a pi I use as a web server to a
Create the php pages.

Well, let’s start over.

First, do NOT post a link to a video tutorial! Why? Because that does not show us any code to help you fix your code.

Next, you have a hardware unit acting as a web server. You have a website set up on each one. You have a webpage that you want to control the four external relays. Correct so far?

Now, looking at the website for the tutorial you used to build this, it appears that one of the units will each have a their own IP address. But, the program in the web page has a button. Are you trying to call the external page and POST the button? The simple NodeMCU page that shows up on the webpage when you go to the server is in the server. Meaning it is inside your hardware device. I will guess it is handled using Javascript or something similar. Can you VIEW-SOURCE of one of those pages and see if you can figure out what the buttons do? If they post back to the page, then we can easily set up buttons for you, but, if they are done locally on the server, it would not be as easy as you can’t cross-post JS access. Unless you rewrite the server’s code page to accept posted commands. Easy enough too if you have access to each server’s web pages. Not sure if all this helps or makes sense to you.

Yes i have acces to all site and all are program using get.
All these is on a closet local Network.
The nodemcu respond wend i just write the link in a browser that i posted in ny frist post, and that is why i linked to the video to See what my setup is.

So i am just looking for a botten there Can open 8 links wend you click on it and 8 others wend you clic on it again.

I am not sure about cURL is the right code for this?

I hope i understand it right.?

Cheers Jimmy

Note:
I use iframes to see status on all bords if it helps.

Cheers
Jimmy

Well, you can do it mostly with HTML, input’s, hidden inputs and some slightly odd logic.
Give each button an array name and assign a hidden version to each. That way when you post the form by picking a button, it will have the old values in place and will post those along with the button pressed. The logic of this is tricky. You have to keep the old values in place and check for changes. Then, you have to set the values of the changes and save them for the next pass. Also, you will need to either use indexing for the codes used or perhaps flag them in arrays. I tossed some code together that would do this in one manner. The cURL code is in place, but, had to add some extra code to create the URL to access. Not sure if any of this works. Just created it quickly. Test it first with without the cURL… I have them commented out and have it displaying the URL so you can see if it works for you.

<?PHP
//  Set up an array of URL's for the hardware
$urls = array(  "1"=>"http://192.168.1.10/led?state=TURN+RED+ON", "2"=>"http://192.168.1.20/led?state=TURN+RED+ON", "3"=>"http://192.168.1.30/led?state=TURN+RED+ON", "4"=>"http://192.168.1.40/led?state=TURN+RED+ON",
                "6"=>"http://192.168.1.10/led?state=TURN+RED+OFF", "7"=>"http://192.168.1.20/led?state=TURN+RED+OFF", "8"=>"http://192.168.1.30/led?state=TURN+RED+OFF", "9"=>"http://192.168.1.40/led?state=TURN+RED+OFF"); 

//  Get old values of buttons to set them ( If not set, set to default! )
$saved_buttons = $_POST["saved_buttons"];
if (!isset($saved_buttons[1])) { $saved_buttons[1] = "#1: Not Set"; }
if (!isset($saved_buttons[2])) { $saved_buttons[2] = "#2: Not Set"; }
if (!isset($saved_buttons[3])) { $saved_buttons[3] = "#3: Not Set"; }
if (!isset($saved_buttons[4])) { $saved_buttons[4] = "#4: Not Set"; }
$buttons = $saved_buttons;

//  Check if any button was pressed.  If so, alter that one button's setting and save it.
if (isset($_POST["buttons"])) {
    $pressed_button = $_POST["buttons"];
    foreach ($pressed_button as $button_number => $button_code) {
    $code = substr($button_code, 5, 1);
    if ($code=="o" OR $code=="n") {
        $buttons[$button_number] = "#" . $button_number . ": Off";
        $saved_buttons[$button_number] = "#" . $button_number . ": On";
        $curl_url = $urls[$button_number];
    } elseif ($code=="f") {
        $buttons[$button_number] = "#" . $button_number . ": On";
        $saved_buttons[$button_number] = "#" . $button_number . ": Off";
        $curl_url = $urls[$button_number+5];
    }
echo "URL: " . $curl_url . "<br>";
    //  Now, with the values of the buttons sorted out, send out the currect code to the hardware
//    $ch = curl_init();
//    curl_setopt($ch, CURLOPT_URL, $curl_url); 
//    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//    $content = trim(curl_exec($ch));
//    curl_close($ch);
    }
}
?>
<html>
<body>
    <form method="POST">
        <input type="hidden" name="saved_buttons[1]" value="<?PHP echo $saved_buttons[1]; ?>">
        <button type="submit" name="buttons[1]" value="<?PHP echo $buttons[1]; ?>"><?PHP echo $saved_buttons[1]; ?></button>
        <input type="hidden" name="saved_buttons[2]" value="<?PHP echo $saved_buttons[2]; ?>">
        <button type="submit" name="buttons[2]" value="<?PHP echo $buttons[2]; ?>"><?PHP echo $saved_buttons[2]; ?></button>
        <input type="hidden" name="saved_buttons[3]" value="<?PHP echo $saved_buttons[3]; ?>">
        <button type="submit" name="buttons[3]" value="<?PHP echo $buttons[3]; ?>"><?PHP echo $saved_buttons[3]; ?></button>
        <input type="hidden" name="saved_buttons[4]" value="<?PHP echo $saved_buttons[4]; ?>">
        <button type="submit" name="buttons[4]" value="<?PHP echo $buttons[4]; ?>"><?PHP echo $saved_buttons[4]; ?></button>
    </form>
</body>
</html>

NOTE: There are other ways to keep the old versions. One would be sessions and you could write them out to a text file, but, using the hidden fields work quite well. I could not test the cURL since I do not have your hardware… Hope it works okay for you…

An HTML layout of the gates as buttons and AJAX calls would be how I would tackle it. Single page, not reloads. Just a GET request to the IP?

Hi ErnieAlex and thank you for your reply.
I think maybe I have not spoken correctly?

The code you have posted gives me 4 buttons and unfortunately i’m so new to php so i do not understand how it really works.

What I’m looking for is a button that opens all these on links when you tap it and opens the other off links when you press the button again.

I can see when I press eg. barely 1 so write that link above the button but do not activate the link or send it out so my mcu board receives it?

For example, if I write a on link in my explorer, the mcu table turns on a led as it should and I write an off link in explorer turns the mcu turn it off and it’s the red link on all the mcu boards I have to paired with just one button?

Hope it makes sense?

Regards
Jimmy

hi astonecipher
do you have an exampel code maybe?

cheers
Jimmy

var IPs = {}
$.get( "ajax/test.html", function( data ) {
  $( ".result" ).html( data ); // what to do with the results
});

I have a meeting, but will post more later.

Just remove the // on the cURL code and test it.

//    $ch = curl_init();
//    curl_setopt($ch, CURLOPT_URL, $curl_url); 
//    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//    $content = trim(curl_exec($ch));
//    curl_close($ch);

I mentioned I needed to comment these out as I do not have your hardware.
We can alter the code to turn off all the others first. But, first test the code I posted and see if it turns
them on and off and I will rewrite the code to turn ALL off first and then turn on the one you select.
I was not clear that was the way you wanted it.

Or wait and see Astone’s reply. His way would require two files, but, not a big deal that way either…

More than two lines, but.

$(document).ready(function(){
    var state = "ON";
    $('input[type="button"]').on('click', function(){

      $( [1,2,3,4] ).each(function() {
      var status = state == "ON" ? "Off" : "ON";
      $.get( "http://192.168.1." + this + "0/led?state=TURN+RED+" + status, function( data ) {            
                $( "#b" + this ).removeClass(status == "ON" ? "active" : "inactive");
                $( "#b" + this ).addClass(status == "ON" ? "inactive" : "active" );          
       });
      });  
    });
});
<input type="button" class="inactive" id="b1">
<input type="button" class="inactive" id="b2">
<input type="button" class="inactive" id="b3">
<input type="button" class="inactive" id="b4">
.inactive {
  background-color: grey;
  color: white;
}

.active {
  background-color: red;
}

Because, I would assume if you make a call to a system, it would give you some indication back if it worked, correct?

He needs the URL to be like this. 192.168.1.10, .20, .30, .40 And, the extra code tells it to turn on and off.
He also just noted that it needs to turn all of them off except the one selected. So, it needs to keep the old
one and turn it off. If I am understanding him correctly. And, looking at the website on this device, it does not seem to send back any acknowledgments.

hi ErnieAlex
I have now try to test the code but wend i activate curl code the page just go blank wend i push a botton?
Just to make sure about the code it is all on and all off from one botton.

Sry my spelling i am just from denmark :slight_smile:

Cheers
Jimmy

Alright, I am misunderstanding what is needed then. I was under the thought that a button needed to send something to all listeners?

Astone can alter his code for you probably to do it on one page with little more to change.
I wrote mine very late last night and his is better for your uses. He will just need to change the ajax to call your IP’s as needed.

I will watch for his update…

Updated my example, I may have swapped the meanings of inactive and active.

Nice! Should work for him. I seldom use Javascript because it is not always enabled. Sometimes it is best!

AND, much easier than cURL ! ! !

Sponsor our Newsletter | Privacy Policy | Terms of Service