Trying to control a DC motor over WiFi. (and failing)

I’m working on a project that requires me to control several DC motors over wifi using an MKR 1000 and its motor carrier. I used the example MKR motor carrier code and a video posted by the founder of arduino (Wireless Control of Devices with the MKR1010 - Arduino LiveCast Series S02E04 - YouTube) as a reference for my code. I experimented with each by myself to make they worked than tried to combine them to create a program that would allow me to control one dc motor over WiFi. It didn’t work, so i went through it multiple times getting rid of simple syntax mistakes and correcting simple errors I had made. It still didn’t work. I did research on parts of the code I didn’t fully understand to try and made a few more fixes but still no dice.

Sorry for the giant body of code but I have no idea which part of it is wrong. I think the problem might be in the the IF statement at the Void Setup as the serial prints in it don’t work.

Everything works fine, it sets up the server and prints most of the right things but as soon as I press a button on the server nothing happens and then it crashes.

#include <WiFi101.h>
#include <SPI.h>
#include <MKRMotorCarrier.h>

static int batteryVoltage;

char ssid[] = "ARROW_015D80";
char pass[] = "************";
int keyIndex = 0;

int status = WL_IDLE_STATUS;
WiFiServer server(80);

char quote = '"';
char slash = '/';

int LED = LED_BUILTIN;


/*===================================================================================================*/

void setup() {
  // put your setup code here, to run once:
  while (!Serial) {
  }
  Serial.begin(9600);
  Serial.println("start of SETUP");

  if (controller.begin())
  {
    Serial.print("MKR Motor Carrier Connected");
  }
  
  else {
    Serial.println("No Motor Carrier");
    while (1);
  }
  
  while ( status != WL_CONNECTED) {
      Serial.print("Attempting to connect to Networked names: ");
      Serial.println(ssid);
      status = WiFi.begin(ssid, pass);
      delay(5000);
    }
    


     
  server.begin();
  Serial.println("Start of SERvER");
  WiFiStatus(); 
  Serial.println("Rebooting");
  controller.reboot();
  delay(500);

  float batteryVoltage = (float)battery.getConverted();
  Serial.print("Battery Voltage:");
  Serial.println(batteryVoltage);
  Serial.print("V, Raw");
  Serial.println(battery.getRaw());
}


/*===================================================================================================*/
void loop() {
  // put your main code here, to run repeatedly:

 
  float batteryVoltage = (float)battery.getConverted();

  if (batteryVoltage < 11)
  {
    Serial.println(" ");
    Serial.println("!!!!!!!!!!!!!LOW BATTERY!!!!!!!!!!!!");
    Serial.println("Shut Down IMMENANT");
    Serial.println("Current Voltage");
    Serial.println(batteryVoltage);
    M3.setDuty(0);
    digitalWrite(LED, LOW);
    delay(500);
  }


  else {

    
    WiFiClient client = server.available();

    if (client) {
      Serial.println("New CLient");
      String currentLine = "";
      while (client.connected()) {
        if (client.available()) {
          char c = client.read();
          Serial.write(c);
          if (c == '\n') {

            if (currentLine.length() == 0) {
              client.println("HTTP/1.1 200 OK");
              client.println("Content-type:text/html");
              client.println("");
              client.println("<!DOCTYPE HTML>");
              client.print("<head>");
              client.print("<link rel=");
              client.print(quote);
              client.print("stylesheet");
              client.print(quote);
              client.print("href=");
              client.print(quote);
              client.print("https://ioio.mah.se/courses/IDK17/student_0007/mkrsheet.css");  //NOTE: link to your own css stylesheet here
              client.print(quote);
              client.print(slash);
              client.print (">");
              client.print("</head>");
              client.print("<body>");
              client.println("<center>

<div class='container'><h1>CONTROL YOUR ARDUINO<h1/></div></center>");
              client.println("<center><div class='container'><left><button class='on' type='submit' value='ON' onmousedown=location.href='/H\'>ON</button>");
              client.println("<button class='off' type='submit' value='OFF' onmousedown=location.href='/L\'>OFF</button></div>
");
              client.println("<button class='off' type='submit' value='OFF' onmousedown=location.href='/O\'>Motor ON</button></div>
");
              client.println("<button class='off' type='submit' value='OFF' onmousedown=location.href='/F\'>Motor OFF</button></div>
");
              client.println("<button class='off' type='submit' value='OFF' onmousedown=location.href='/A\'>REVERSE</button></div>
");
              client.println("</body>");
              client.println("</html>");

              client.println("");

              break;
            }
            else {
              currentLine = "";

            }

            if (currentLine.endsWith("GET /H")) {
              digitalWrite(LED, HIGH);
            }
            if (currentLine.endsWith("GET /L")) {
              digitalWrite(LED, LOW);
            }
            if (currentLine.endsWith("GET /O")) {
              M3.setDuty(30);
            }
            if (currentLine.endsWith("GET /F")) {
              M3.setDuty(0);
            }
            if (currentLine.endsWith("GET /A")) {
              M3.setDuty(-30);
            }
          }
          else if (c != '\r') {
            currentLine += c;
          }
        }
      }
      client.stop();
      Serial.println("Client Disconnected :(");
    }

  }


}
void WiFiStatus() {
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

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

  long rssi = WiFi.RSSI();
  Serial.print("Singal Strenght (RSSI):");
  Serial.println(rssi);
  Serial.println(" dBm");
  Serial.print("TO see this page in action, open to http://");
  Serial.println(ip);
}

Looks like a fun little project.

In your setup function you probably need to do Serial.begin before waiting for the connection while(!Serial) to ensure Serial is available when you start printing.

Did you try the motor functions without the WiFi? Just move the motor at the end of setup and then at the beginning of your loop.

OK, I got your code to run on a Feather M0 WIFI board. I disabled the motor and battery stuff. Here are a few pointer that should help you.

You need to move checks for the currentLine inside the else BEFORE you clear the current Line.
The currentLine contains the message from the server e.g.

GET /H HTTP/1.1

You also need to change the code from endsWith to startsWith. As you can see from the line above. The message starts with "GET /H".

if (currentLine.length() == 0) 
{
...
}
else
{
Serial.print( "currentLine: " ); // This can be commented out, helps understanding the http messages
Serial.println( currentLine );   // This can be commented out
            
if (currentLine.startsWith("GET /H")) {
  Serial.println("LED On");
  digitalWrite(LED, HIGH);
}
// ... check all the other stuff here
            
currentLine = ""; // This clears the current Line, thats why nothing happens in your code
                         // Because now we have worked on the "commands", we need to clear it.

}

Additionaly I needed to add some pin configuration for my wifi module. You might not need that. Check the examples for your board. Also the LED Pin needed to be set to Output. Add to your setup()

pinMode( LED, OUTPUT );

As said before switch the Serial initialization. This should make sure you can read all serial messages and the program waits for you until you open Serial Monitor.

Serial.begin(9600);
while (!Serial) {
}

I can switch the LED on and off and get prints for the motor. Hope this helps.
Make sure you change your WIFI password. :slight_smile:

Klaus_K thank you very much. I was planning on trying to fix it during winter break so you just saved me many hours of staring at broken code. I greatly appreciate you taking your time of day to send me advice. Usually it takes longer for to get a mediocre answer. Again thanks.

Whoops about the WiFi password. I’ll remember to change it in the future