0

I currently have a basic website hosted by my Raspberry Pi. What I am trying to do is control digital outputs via HTML form buttons. Currently it works while I am on the LAN as the button form action is set to the local IP of the Arduino. I am struggling to figure out how to control these outputs from another network or from my phone.

Am I on the right track to think I need to pass the on/off commands to something like a php script hosted on the RPi and have the php script generate the Post request?

Does anyone have an example to get me pointed in the right direction?

Thanks in advance

sKhan
  • 9,694
  • 16
  • 55
  • 53
  • If you want to be able to access your RPi from outside your LAN, you need to configure your router to relay connections to your RPi when it gets a request from outside. You may also need to make appropriate changes to your Pi's firewall settings. If your ISP doesn't provide you with a static IP, you can use on of the many free Dynamic DNS services like DuckDNS. – pradeepcep Feb 01 '16 at 16:41
  • Thank you for your response. I do have that all set up. I think I need to reword my question. – Kyle Smith Feb 01 '16 at 23:25
  • I need to create a php script that is hosted on the RPI. I need that php script to accept a HTML button value (1 or 0) and send a GET request to my arduino. I can send that GET request directly via the HTML form to the Uno (192.168.1.9) but once I call upon the website outside of my network the GET request still goes to 192.168.1.9, but that does not work for obvious reasons. I guess what it boils down to is I need help creating a php script that will take a HTML form button input and create a GET request for the Arduino. Thanks a lot for the help. – Kyle Smith Feb 01 '16 at 23:30
  • Accept the button value, decide what to do, then make the GET request using maybe `cURL`? Show us what you have tried so far. – pradeepcep Feb 02 '16 at 02:08
  • In index.html I have -
    On
    Off
    – Kyle Smith Feb 03 '16 at 01:39
  • In overrides.php I have - 'value1', 'key2' => 'value2'); // use key 'http' even if you send the request to https://... $options = array( 'http' => array( 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($data), ), ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); if ($result === FALSE) { /* Handle error */ } var_dump($result); ?> – Kyle Smith Feb 03 '16 at 01:39
  • The php script is based off of this post - http://stackoverflow.com/questions/5647461/how-do-i-send-a-post-request-with-php I have tried various keys and values for the $data array. I feel like I am missing something fundamental. The arduino is receiving a POST request, however it does not display any values. Here is what I am recieving on the Arduino serial monitor. POST / HTTP/1.0 Host: 192.168.1.9 Connection: close Content-Length: 3 Content-type: application/x-www-form-urlencoded – Kyle Smith Feb 03 '16 at 01:40
  • I'm not familiar with how to receive a HTTP request on the Arduino; I'll try to read up. Meanwhile, someone else should answer this shortly :) – pradeepcep Feb 03 '16 at 02:20

1 Answers1

1

You can control Arduino GPIO using RPi hosted website.

What you need to learn:

  • I2C/Serial/SPI communication between Arduino & RPi (Sample Tutorial (I2C))
  • CGI enables to execute programs written in C/C++, Python, etc redirect print/println/cout as web-response as well as you can execute system commands to manipulate communication bus, gpio and system calls. To know more about CGI, click here. (Get started with CGI)
    • You can call CGI program from URL just like you call webpage: ("192.168.1.0/cgi-bin/HelloWorld" , for RPi "/usr/lib/cgi-bin/" is folder where CGI programs are stored while "HelloWorld" is your executable).

As an example, assume that you want to turn on-off Arduino's on-board led D13 via webpage hosted on RPi. Webpage contains two buttons, On and Off. When "On" pressed (clicked), call C/C++ or Python code written to command Arduino via I2C. Define a common protocol to follow so that bytes sent by C/C++ or Python can be understand by Arduino. Like, if Arduino receives 1 from master (RPi), it should turn on D13 led and turn off for 0.

Anurag Vasanwala
  • 486
  • 3
  • 12
  • http://www.legomindstormsrobots.com/arduino/connecting-arduino-raspberry-pi-i2c/ does not seem to load any longer, redirects. May like: https://oscarliang.com/raspberry-pi-arduino-connected-i2c/ – Twisty Jul 19 '17 at 17:35