Arduino Uno with Wifi – Remote Control Car

I’m working on building a remote control car using the Arduino Uno with WiFi and the FeeTech Vehicle platform.

#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");
          }
        }
      }
    }
  }
}

Be the first to comment

Leave a Reply