Arduino Uno with Wifi – RC Car Controlled By PHP Server

This project uses a server with PHP to send commands to the Arduino Vehicle. The commands are sent use shell_exec and triggering wget on the command line. wget needs to be installed on your server. (Ubuntu: sudo apt-get install wget)

By default wget will run infinite times so we set the try value to 1 (–tries=1).

This could be done “better” with something like cURL, but this works and doesn’t require much additional configuration or learning.

Arduino Code

#include <SPI.h>
#include <WiFiNINA.h>
#include <Servo.h>


Servo lServo;
Servo rServo;

char ssid[] = "Basement";
char pass[] = "0123456789";
IPAddress ip(10, 0, 1, 15);    


int status = WL_IDLE_STATUS;
WiFiServer server(80);

String readString;


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  
  lServo.attach(8);
  rServo.attach(9);

  Serial.begin(9600);

  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }
  digitalWrite(LED_BUILTIN, HIGH);
  WiFi.config(ip);
  server.begin();

  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void loop() {


  WiFiClient client = server.available();
  if (client) {
    Serial.println("new client");

    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        if (readString.length() < 100)
        {
          readString += c;
          Serial.write(c);

          if (c == '\n') {

            client.print("<table>");
            client.println("<tr><td></td><td><a href=\"/?forward\"\">FORWARD</a></td>");
            client.print("<td></td></tr>");
            client.println("<tr><td><a href=\"/?left\"\">LEFT</a></td>");
            client.println("<td><a href=\"/?stop\"\">--STOP--</a></td>");
            client.println("<td><a href=\"/?right\"\">RIGHT</a></td></tr>");
            client.println("<tr><td></td><td><a href=\"/?backward\"\">BACKWARD</a><br /></td><td></td></tr>");
            client.print("</table>");



            delay(1);

            if (readString.indexOf("?forward") > 0)
            {
              lServo.write(85);
              rServo.write(85);

              delay(1);
            }else if (readString.indexOf("?left") > 0){
              
                lServo.write(88);
                rServo.write(80);

                delay(1);
              
             } else if (readString.indexOf("?right") > 0){
              
                lServo.write(80);
                rServo.write(88);

                delay(1);
              
             }else if (readString.indexOf("?backward") > 0){
              
                lServo.write(100);
                rServo.write(100);

                delay(1);
              
             }else {
              if (readString.indexOf("?stop") > 0){
                lServo.write(90);
                rServo.write(90);

                delay(1);
              }
           }

            readString = "";

            delay(1);
            client.stop();
            Serial.println("client disonnected");
          }
        }
      }
    }
  }
}

carControl.php

<?php

$command = $_GET['command'];

shell_exec("wget --tries=1 http://10.0.1.15/?'$command'");

echo $command;
?>

<html>


          <head>
                    <link rel="stylesheet" href="carControlStyle.css" />
          </head>

          <body>
                    <div>          
                    <table>
                              <tr><td></td><td><a href='carControl.php?command=forward'>FORWARD</a></td><td></td></tr>
                              <tr><td><a href='carControl.php?command=left'>LEFT</a></td><td><a href='carControl.php?command=stop'>STOP</a></td><td><a href='carControl.php?command=right'>RIGHT</a></td></tr>
                              <tr><td></td><td><a href='carControl.php?command=back'>BACKWARDS</a></td><td><td></tr>
                    </table>
</div>
                    
          </body>
</html>

carControlStyle.css

td {
          text-align: center;
          font-size: 30;

}

div {
          display: block;
          position:relative;
          width: 370px;
          margin-left: auto;
          margin-right: auto;
          border: 2px solid black;
}

Be the first to comment

Leave a Reply