I'm currently working on a project where i need to control motors over the Ethernet. I am using an mkr 1000 arduino board and its motor carrier. To create my code I blended/ used as a reference the mkr 1000's sample code and the average motor test sample code. Along with bits and pieces from online arduino codes.
What my code should be doing:
- Setting up appropriate things to run (server, motor carrier)
- opening connections to client
- if a client connects it should show the appropriate GUI
- than respond appropriately to changes in url (caused by Javascript stuff)
I keep getting stuck on the last step.
I double checked my javascript so I am 80% sure the fault doesn't lie there
I littered serial.prints around my arduino code for testing and I know that the setup sections are being run, so that isn't the issue.
The only thing left that I can think of is the part of the code which takes the url and responds to it.
The reference codes I looked at all used the same or some variation of
if (currentLine.endsWith("GET /#/w")) {
Serial.println("Forwards");
M3.setDuty(100);
M2.setDuty(100);
}
So it really confused me when it didn't work.
Any advice?
Note: I already tried the url if statement in the code without the # but it didn't change anything.
Full Code:
#include <WiFi101.h>
#include <SPI.h>
#include <MKRMotorCarrier.h>
static int batteryVoltage;
char ssid[] = "****";
char pass[] = "****";
int keyIndex = 0;
int status = WL_IDLE_STATUS;
WiFiServer server(80);
int LED = LED_BUILTIN;
/*===================================================================================================*/
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
}
Serial.println("start of SETUP");
if (controller.begin())
{
Serial.println("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);
}
pinMode(LED, OUTPUT);
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!!!!!!!!!!!!");
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("Refresh: 1");
Had to take out javascript stuff because post exceeded word limit
client.println("");
break;
}
else {
currentLine = "";
}
}
else if (c != '\r') {
currentLine += c;
}
if (currentLine.endsWith("GET /#/w")) {
Serial.println("Forwards");
M3.setDuty(100);
M2.setDuty(100);
}
else if (currentLine.endsWith("GET /#/s")) {
Serial.println("Backwards");
M3.setDuty(-100);
M2.setDuty(-100);
}
else if (currentLine.endsWith("GET /#/a")) {
Serial.println("Left");
M3.setDuty(100);
M2.setDuty(0);
}
else if (currentLine.endsWith("GET /#/d")) {
Serial.println("Right");
M3.setDuty(0);
M2.setDuty(100);
}
else if (currentLine.endsWith("GET /#/q")) {
Serial.println("Down");
M1.setDuty(-100);
}
else if (currentLine.endsWith("GET /#/e")){
Serial.println("UP");
M1.setDuty(100);
}
else if (currentLine.endsWith("GET /#")){
Serial.println("Everything stop");
M1.setDuty(0);
M2.setDuty(0);
M3.setDuty(0);
}
else if (!client.connected()){
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);
}

