0

Hello everyone so for my job I need to basically code a fish that will have three input switches glued onto the fish. For each of the switches if they are pressed they will go left right and the third option is random (either left or right) respectively. This coincides with a respective LED turning on when one of the three switches are pressed. So far the fish moving continuously, the motor part of the code and their input switches work as intended.

However the user interface aspect of the code is giving me issues since when I look at the user interface there are supposed to be a means of controlling the fish using the options given in the interface (left, right, random, forward, backward, stop) and with the output saying robot is doing "x" with it also saying either LED1, 2, or 3 is on if an input switch is pressed. However when I pull up the interface I get something different than what I get which I will show below alongside the code. How do I fix this (also stp, "step" is a dummy variable that was used for practice, I will remove that later to replace with the "left", "right", and "random" options)

Here is what shows up on the interface: enter image description here

Also I have no means of testing if the motors or LED's are working since I left all my tools in the university before the pandemic all I have is the Arduino chip and a board to connect it to. At the moment all my bosses want is for the user interface to work.

Here is the code in question:

#include <ESP8266WiFi.h>
#include <Servo.h>

const char* ssid = "Fios-sLZ9m";
const char* password = "ute368cat7293pie";

WiFiServer server(80);

int Pin = 16; // GPIO13
int var = 0;
int D1 = 5;
int D2 = 4;
int D4 = 2;
int D5 = 14;
int D6 = 12;
int D7 = 13;
int temp1 = 0;
int temp2 = 0;
int temp3 = 0;
long randturn = 0;

int value = 0;
Servo servoMotor;

void setup() {
  Serial.begin(115200);
  delay(10);

  randomSeed(analogRead(0));
  pinMode(D1, INPUT);
  pinMode(D2, OUTPUT);
  pinMode(D4, INPUT);
  pinMode(D5, OUTPUT);
  pinMode(D6, INPUT);
  pinMode(D7, OUTPUT);
  pinMode(Pin,INPUT);
  //pinMode(LED_BUILTIN, OUTPUT);
  //digitalWrite(LED_BUILTIN, LOW);

  // Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
// 
WiFi.begin(ssid, password);
// 
while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
}
  Serial.println("");
  Serial.println("WiFi connected");
// 
//  Start the server
  server.begin();
  Serial.println("Server started");
// 
  // Print the IP address
  Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");

  servoMotor.attach(Pin);

}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  temp1 = digitalRead(D1);
  temp2 = digitalRead(D4);
  temp3 = digitalRead(D6);
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }

  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();

  // Match the request

  servoMotor.attach(Pin);
  servoMotor.write(90);
  delay(200);
  servoMotor.write(-90);
  delay(200);

  if(temp1 == HIGH) {
    digitalWrite(D2,HIGH); //LED 1 TURNS ON
    servoMotor.attach(Pin); //turn to the right
    servoMotor.write(180);
     servoMotor.write(270);
    delay(200);
    servoMotor.write(90);
  }
  else{
    digitalWrite(D2,LOW);
  }

  if(temp2 == HIGH) {
    digitalWrite(D5,HIGH); //LED 2 TURNS ON
    servoMotor.attach(Pin); //turn to the the left
    servoMotor.write(-180);
    servoMotor.write(-270);
    delay(200);
    servoMotor.write(-90);
}
  else {
    digitalWrite(D5,LOW);
}

  if(temp3 == HIGH) { 
    digitalWrite(D7,HIGH); //LED 3 TURNS ON
    servoMotor.attach(Pin); //random turn
    servoMotor.write(random(-90,90));
}
  else{
    digitalWrite(D7,LOW);
}

  while (value == LOW) {
  if (request.indexOf("/LED=ON") != -1)  {
    //digitalWrite(LED_BUILTIN, HIGH);
    value = HIGH;
    servoMotor.attach(Pin);
    servoMotor.write(0);
    delay(1000);

  }
  if (request.indexOf("/LED=OFF") != -1)  {
    //digitalWrite(LED_BUILTIN, LOW);
    value = LOW;
    servoMotor.attach(Pin);
    servoMotor.write(180);
    delay(1000);

  }
  if (request.indexOf("/LED=STP") != -1){
      var = 1;
      while(var < 10){
    //digitalWrite(LED_BUILTIN, LOW);
    value = LOW;
    servoMotor.attach(Pin);
    servoMotor.write(90);
    delay(200);
    servoMotor.write(-90);
    delay(200);
    var=var+1;
    }
  }

// Set ledPin according to the request
//digitalWrite(ledPin, value);

  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");

  client.print("Robot is now: ");

  if(value == HIGH) {
    client.print("Moving Forward");
  } else if(value == LOW) {
    client.print("Moving Backward");
  } else if(request.indexOf("/LED=STP") != -1) {
    client.print("Moving in Step");
  }else {
    client.print("Stopped");
  }

  if(temp1 == HIGH) {
    client.print("Led_1 is on");
  }else if (temp1 == LOW);{
    client.print("Led_1 is off");
  }

  if(temp2 == HIGH) {
    client.print("Led_2 is on");
  }else if (temp2 == LOW);{
    client.print("Led_2 is off");
  }

  if(temp3 == HIGH) {
    client.print("Led_3 is on");
  }else if (temp3 == LOW);{
    client.print("Led_3 is off");
  }
//
  client.println("<br><br>");
  client.println("<a href=\"/LED=ON\"\"><button>Move Forward </button></a>");
  client.println("<a href=\"/LED=STP\"\"><button>Step </button></a>");
  client.println("<a href=\"/LED=OFF\"\"><button>Move Backward </button></a><br />");
  client.println("<a href=\"/LED=STOP\"\"><button>STOP </button></a><br />");
  client.println("<a href=\"/Temp1=HIGH\"\"><Input>LED_1 ON </Input></a><br />");  
  client.println("<a href=\"/Temp2=HIGH\"\"><Input>LED_2 ON </Input></a><br />");
  client.println("<a href=\"/Temp3=HIGH\"\"><Input>LED_3 ON </Input></a><br />");
  client.println("</html>");

  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
  }
}
  • UI is on the browser, it has to do with the way you create your html and use css or even Javascript to control the rendering. BTW, you never describe what exactly wrong with your UI, and how it should be behave. – hcheung May 8 at 1:44
  • I'm sorry there is no issue with the code its just the output. The options that should show up in the image is Forward, Backward, Left, Right, Random, and STOP with the input that are pressed to say that the Robot is doing x thing (when an option is pressed) with its respective led on – Ruben Ulloa May 8 at 4:04
  • If there's no problem with the code then there wouldn't be a problem with the output. The output is always exactly what the code says it should be. If you don't get the output you want then you don't have the code you want. – Delta_G May 8 at 5:02
  • You need to attach the servo ONCE in setup, not every time you move it. Have you looked at any of the servo examples? – Delta_G May 8 at 5:03
  • 1
    Don't manually manipulate the client connection. Instead use the ESP8266WebServer library. – Majenko May 8 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.