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