Integrate Sketch to Wifi101

I attempted to put the logic as shown in the code, into the simple web server example. My goal is to be able to either initiate or hold the system from the computer.

This code was unsuccessful, and I’m not sure of any other way to go about it. Can someone suggest a method of programming this? Is there a way to write this into a separate chunk of code that can be accessed by the main loop? Or is there a simpler way to add the web server control to a basic sketch?

/*
  WiFi Web Server LED Blink

 A simple web server that lets you blink an LED via the web.
 This sketch will print the IP address of your WiFi Shield (once connected)
 to the Serial monitor. From there, you can open that address in a web browser
 to turn on and off the LED on pin 9.

 If the IP address of your shield is yourAddress:
 http://yourAddress/H turns the LED on
 http://yourAddress/L turns it off

 This example is written for a network using WPA encryption. For
 WEP or WPA, change the Wifi.begin() call accordingly.

 Circuit:
 * WiFi shield attached
 * LED attached to pin 9

 created 25 Nov 2012
 by Tom Igoe
 */
#include <SPI.h>
#include <WiFi101.h>
#include <SabertoothSimplified.h>
const int limit1 = 49;
const int sensor = 47;
const int limit2 = 43;
const int button = 39;
const int greenLED = 35;
const int ledpin = 13;
SabertoothSimplified ST;
int phoneTrip;
int foodPressed;
int eot1;
int eot2;
char ssid[] = "x";      //  your network SSID (name)
char pass[] = "x";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
  pinMode(ledpin, OUTPUT);
  pinMode(sensor, INPUT_PULLUP);
  pinMode(limit1, INPUT_PULLUP);
  pinMode(limit2, INPUT_PULLUP);
  pinMode(greenLED, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  SabertoothTXPinSerial.begin(9600);
  Serial.begin(9600);      // initialize serial communication
  //pinMode(9, OUTPUT);      // set the LED pin mode

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true);       // don't continue
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status
}


void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

/*This portion is the html code attempt*/
            client.println("<!DOCTYPE html>");      //web page is made using html
            client.println("<html>");
            client.println("<head>");
            client.println("<title>x</title>");
/*-------------------------------*/

            // the content of the HTTP response follows the header:
            client.print("<B> Control </B>
");
            client.print("Click <a href=\"/H\">here</a> to run the motor
");
            client.print("Click <a href=\"/L\">here</a> to stop the motor
");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")){
          phoneTrip = digitalRead(sensor);
          foodPressed = digitalRead(button);
          eot1 = digitalRead(limit1);
          eot2 = digitalRead(limit2);
          digitalWrite(ledpin, phoneTrip);
          digitalWrite(greenLED, foodPressed);
          
          static enum {CLOSED,OPENING,OPEN,CLOSING} state;

              switch (state) {
                case CLOSED:
                  if (phoneTrip == 1){ //if the sensor is tripped
                    state = OPENING;
                  }
                  break;
                case OPENING:
                ST.motor(1,127);
                  if (eot1 == 1){
                    ST.motor(1,0);
                    state = OPEN;
                  }
                  break;
                case OPEN:
                  if(foodPressed == 1){
                    delay(5000);
                    state = CLOSING;
                  }
                  break;
                case CLOSING:
                  ST.motor(1,-127);
                  if (eot2 == 1){
                    ST.motor(1,0);
                    state = CLOSED;
                  }
                  break;
              }
        }
        if (currentLine.endsWith("GET /L")){
          ST.motor(1,0);
          }
}
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}

I have created a sketch that is designed to drive a motor

But you didn’t show it…

as well as control a bunch of sensors.

How do you “control” input devices?

I’d like to be able to “initiate” this loop using wifi.

So, you want to wait in setup() until a client connects. Then, regardless of what the client wants, start running some function. Did I miss something?

Is there a way that I can take my existing sketch and integrate it with the web server

Probably.

Here is the code. It’s still a work in progress, but this is most of it.
I need to integrate this so that I can potentially change the state of the machine using the web browser. Sorry if I don’t use correct terminology, I’m very new to Arduino and don’t have much electronics/programming experience.

#include <SabertoothSimplified.h>



const int ledpin = 13;
const int sensor = 5;
const int limit1 = 3;
const int limit2 = 7;
SabertoothSimplified ST;
const int greenLED = 12;
const int button = 9;
int tripped;
int pressed;
int eot1;
int eot2;
/*
eot=0   Limit switch not pressed
eot=1   Limit switch pressed

trip=0  Beam uninterupted
trip=1  Beam interupted
*/

void setup() {
  pinMode(ledpin, OUTPUT);
  pinMode(sensor, INPUT_PULLUP);
  pinMode(limit1, INPUT_PULLUP);
  pinMode(limit2, INPUT_PULLUP);
  pinMode(greenLED, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  SabertoothTXPinSerial.begin(9600);
}

void loop() {
  
  tripped = digitalRead(sensor);
  pressed = digitalRead(button);
  eot1 = digitalRead(limit1);
  eot2 = digitalRead(limit2);
  digitalWrite(ledpin, tripped);
  digitalWrite(greenLED, pressed);
  
  static enum {CLOSED,OPENING,OPEN,CLOSING} state;

  switch (state) {
    case CLOSED:
      if (tripped == 1){ //if the sensor is tripped
        ST.motor(1,127);
        state = OPENING;
      }
      break;
    case OPENING:
      if (eot1 == 1){
        ST.motor(1,0);
        state = OPEN;
      }
      break;
    case OPEN:
      if(pressed == 1){
        delay(5000);
        state = CLOSING;
      }
      break;
    case CLOSING:
      ST.motor(1,-127);
      if (eot2 == 1){
        ST.motor(1,0);
        state = CLOSED;
      }
      break;
  }
}

but this is most of it.

When you post ALL of it, I’ll bother reading it.

What, exactly, is the client supposed to be able to do with the hardware?

The code operates a door to a box. The functionality is all there, but the logic might change. I won't know the final code until I have it all assembled. I'm trying to make sure I can get the electronics working first. I'd like to be able to have some kind of reset-like ability from the computer, so that I could set the box to CLOSED if its in any other state.

I'm attempting to integrate a sketch into a simple web server. I first wrote a sketch to control the system without the Wifi shield. The sketch works as it should, however, once I attach the shield, the sketch no longer works. I'm using the digital pins on the arduino board itself, so the only things running through the shield are the Vin,GND, and TX pins. Do I have to "notify" the arduino board that the wifi shield is attached somewhere in the sketch? Or am I doing something else wrong with the wifi shield?

I tried adding the code based on the Simple Web Server example, and even left the commands at the beginning of the if statement the same. Some of the variables have different names as previously posted, but the logic is the same.

/*
  WiFi Web Server LED Blink

 A simple web server that lets you blink an LED via the web.
 This sketch will print the IP address of your WiFi Shield (once connected)
 to the Serial monitor. From there, you can open that address in a web browser
 to turn on and off the LED on pin 9.

 If the IP address of your shield is yourAddress:
 http://yourAddress/H turns the LED on
 http://yourAddress/L turns it off

 This example is written for a network using WPA encryption. For
 WEP or WPA, change the Wifi.begin() call accordingly.

 Circuit:
 * WiFi shield attached
 * LED attached to pin 9

 created 25 Nov 2012
 by Tom Igoe
 */
#include <SPI.h>
#include <WiFi101.h>
#include <SabertoothSimplified.h>
const int limit1 = 49;
const int sensor = 47;
const int limit2 = 43;
const int button = 39;
const int greenLED = 35;
const int ledpin = 13;
SabertoothSimplified ST;
int phoneTrip;
int foodPressed;
int eot1;
int eot2;
char ssid[] = "inch";      //  your network SSID (name)
char pass[] = "beautiful55";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
  pinMode(ledpin, OUTPUT);
  pinMode(sensor, INPUT_PULLUP);
  pinMode(limit1, INPUT_PULLUP);
  pinMode(limit2, INPUT_PULLUP);
  pinMode(greenLED, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  SabertoothTXPinSerial.begin(9600);
  Serial.begin(9600);      // initialize serial communication
  //pinMode(9, OUTPUT);      // set the LED pin mode

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true);       // don't continue
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status
}


void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

/*This portion is the html code attempt*/
            client.println("<!DOCTYPE html>");      //web page is made using html
            client.println("<html>");
            client.println("<head>");
            client.println("<title>Chip Chop</title>");
/*-------------------------------*/

            // the content of the HTTP response follows the header:
            client.print("<B> Chip Chop </B>
");
            client.print("Click <a href=\"/H\">here</a> to run the motor
");
            client.print("Click <a href=\"/L\">here</a> to stop the motor
");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")){
          phoneTrip = digitalRead(sensor);
          foodPressed = digitalRead(button);
          eot1 = digitalRead(limit1);
          eot2 = digitalRead(limit2);
          digitalWrite(ledpin, phoneTrip);
          digitalWrite(greenLED, foodPressed);
          
          static enum {CLOSED,OPENING,OPEN,CLOSING} state;

              switch (state) {
                case CLOSED:
                  if (phoneTrip == 1){ //if the sensor is tripped
                    state = OPENING;
                  }
                  break;
                case OPENING:
                ST.motor(1,127);
                  if (eot1 == 1){
                    ST.motor(1,0);
                    state = OPEN;
                  }
                  break;
                case OPEN:
                  if(foodPressed == 1){
                    delay(5000);
                    state = CLOSING;
                  }
                  break;
                case CLOSING:
                  ST.motor(1,-127);
                  if (eot2 == 1){
                    ST.motor(1,0);
                    state = CLOSED;
                  }
                  break;
              }
        }
        if (currentLine.endsWith("GET /L")){
          ST.motor(1,0);
          }
}
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}

I suspect you're using digital pins that are also used to control the WiFi101 shield to control parts of your project. It uses the SPI pins (including D10 for CS), pin 5, and pin 7. Are you using any of those pins?

Since you haven't told us how it's wired or posted your code, it's hard for us to comment further.

I attempted to put the logic as shown above into the code for the simple web server example. This resulted in the motor running uncontrolled for a few seconds, then randomly changing to slower speed until the power was cut off.

Is there a way to write the control logic into a separate chunk of code that can be accessed by the main loop?

/*
  WiFi Web Server LED Blink

 A simple web server that lets you blink an LED via the web.
 This sketch will print the IP address of your WiFi Shield (once connected)
 to the Serial monitor. From there, you can open that address in a web browser
 to turn on and off the LED on pin 9.

 If the IP address of your shield is yourAddress:
 http://yourAddress/H turns the LED on
 http://yourAddress/L turns it off

 This example is written for a network using WPA encryption. For
 WEP or WPA, change the Wifi.begin() call accordingly.

 Circuit:
 * WiFi shield attached
 * LED attached to pin 9

 created 25 Nov 2012
 by Tom Igoe
 */
#include <SPI.h>
#include <WiFi101.h>
#include <SabertoothSimplified.h>
const int limit1 = 49;
const int sensor = 47;
const int limit2 = 43;
const int button = 39;
const int greenLED = 35;
const int ledpin = 13;
SabertoothSimplified ST;
int phoneTrip;
int foodPressed;
int eot1;
int eot2;
char ssid[] = "x";      //  your network SSID (name)
char pass[] = "x";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
  pinMode(ledpin, OUTPUT);
  pinMode(sensor, INPUT_PULLUP);
  pinMode(limit1, INPUT_PULLUP);
  pinMode(limit2, INPUT_PULLUP);
  pinMode(greenLED, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  SabertoothTXPinSerial.begin(9600);
  Serial.begin(9600);      // initialize serial communication
  //pinMode(9, OUTPUT);      // set the LED pin mode

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true);       // don't continue
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status
}


void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

/*This portion is the html code attempt*/
            client.println("<!DOCTYPE html>");      //web page is made using html
            client.println("<html>");
            client.println("<head>");
            client.println("<title>x</title>");
/*-------------------------------*/

            // the content of the HTTP response follows the header:
            client.print("<B> Control </B>
");
            client.print("Click <a href=\"/H\">here</a> to run the motor
");
            client.print("Click <a href=\"/L\">here</a> to stop the motor
");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")){
          phoneTrip = digitalRead(sensor);
          foodPressed = digitalRead(button);
          eot1 = digitalRead(limit1);
          eot2 = digitalRead(limit2);
          digitalWrite(ledpin, phoneTrip);
          digitalWrite(greenLED, foodPressed);
          
          static enum {CLOSED,OPENING,OPEN,CLOSING} state;

              switch (state) {
                case CLOSED:
                  if (phoneTrip == 1){ //if the sensor is tripped
                    state = OPENING;
                  }
                  break;
                case OPENING:
                ST.motor(1,127);
                  if (eot1 == 1){
                    ST.motor(1,0);
                    state = OPEN;
                  }
                  break;
                case OPEN:
                  if(foodPressed == 1){
                    delay(5000);
                    state = CLOSING;
                  }
                  break;
                case CLOSING:
                  ST.motor(1,-127);
                  if (eot2 == 1){
                    ST.motor(1,0);
                    state = CLOSED;
                  }
                  break;
              }
        }
        if (currentLine.endsWith("GET /L")){
          ST.motor(1,0);
          }
}
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}

I attempted to put the logic as shown above into the code for the simple web server example. This resulted in the motor running uncontrolled for a few seconds, then randomly changing to slower speed until the power was cut off.

Is there a way to write the control logic into a separate chunk of code that can be accessed by the main loop?

/*
  WiFi Web Server LED Blink

 A simple web server that lets you blink an LED via the web.
 This sketch will print the IP address of your WiFi Shield (once connected)
 to the Serial monitor. From there, you can open that address in a web browser
 to turn on and off the LED on pin 9.

 If the IP address of your shield is yourAddress:
 http://yourAddress/H turns the LED on
 http://yourAddress/L turns it off

 This example is written for a network using WPA encryption. For
 WEP or WPA, change the Wifi.begin() call accordingly.

 Circuit:
 * WiFi shield attached
 * LED attached to pin 9

 created 25 Nov 2012
 by Tom Igoe
 */
#include <SPI.h>
#include <WiFi101.h>
#include <SabertoothSimplified.h>
const int limit1 = 49;
const int sensor = 47;
const int limit2 = 43;
const int button = 39;
const int greenLED = 35;
const int ledpin = 13;
SabertoothSimplified ST;
int phoneTrip;
int foodPressed;
int eot1;
int eot2;
char ssid[] = "x";      //  your network SSID (name)
char pass[] = "x";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
  pinMode(ledpin, OUTPUT);
  pinMode(sensor, INPUT_PULLUP);
  pinMode(limit1, INPUT_PULLUP);
  pinMode(limit2, INPUT_PULLUP);
  pinMode(greenLED, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  SabertoothTXPinSerial.begin(9600);
  Serial.begin(9600);      // initialize serial communication
  //pinMode(9, OUTPUT);      // set the LED pin mode

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true);       // don't continue
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status
}


void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

/*This portion is the html code attempt*/
            client.println("<!DOCTYPE html>");      //web page is made using html
            client.println("<html>");
            client.println("<head>");
            client.println("<title>x</title>");
/*-------------------------------*/

            // the content of the HTTP response follows the header:
            client.print("<B> Control </B>
");
            client.print("Click <a href=\"/H\">here</a> to run the motor
");
            client.print("Click <a href=\"/L\">here</a> to stop the motor
");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")){
          phoneTrip = digitalRead(sensor);
          foodPressed = digitalRead(button);
          eot1 = digitalRead(limit1);
          eot2 = digitalRead(limit2);
          digitalWrite(ledpin, phoneTrip);
          digitalWrite(greenLED, foodPressed);
          
          static enum {CLOSED,OPENING,OPEN,CLOSING} state;

              switch (state) {
                case CLOSED:
                  if (phoneTrip == 1){ //if the sensor is tripped
                    state = OPENING;
                  }
                  break;
                case OPENING:
                ST.motor(1,127);
                  if (eot1 == 1){
                    ST.motor(1,0);
                    state = OPEN;
                  }
                  break;
                case OPEN:
                  if(foodPressed == 1){
                    delay(5000);
                    state = CLOSING;
                  }
                  break;
                case CLOSING:
                  ST.motor(1,-127);
                  if (eot2 == 1){
                    ST.motor(1,0);
                    state = CLOSED;
                  }
                  break;
              }
        }
        if (currentLine.endsWith("GET /L")){
          ST.motor(1,0);
          }
}
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}

I attempted to put the logic as shown in the code, into the simple web server example. My goal is to be able to either initiate or hold the system from the computer.

This code was unsuccessful, and I’m not sure of any other way to go about it. Can someone suggest a method of programming this? Is there a way to write this into a separate chunk of code that can be accessed by the main loop? Or is there a simpler way to add the web server control to a basic sketch?

/*
  WiFi Web Server LED Blink

 A simple web server that lets you blink an LED via the web.
 This sketch will print the IP address of your WiFi Shield (once connected)
 to the Serial monitor. From there, you can open that address in a web browser
 to turn on and off the LED on pin 9.

 If the IP address of your shield is yourAddress:
 http://yourAddress/H turns the LED on
 http://yourAddress/L turns it off

 This example is written for a network using WPA encryption. For
 WEP or WPA, change the Wifi.begin() call accordingly.

 Circuit:
 * WiFi shield attached
 * LED attached to pin 9

 created 25 Nov 2012
 by Tom Igoe
 */
#include <SPI.h>
#include <WiFi101.h>
#include <SabertoothSimplified.h>
const int limit1 = 49;
const int sensor = 47;
const int limit2 = 43;
const int button = 39;
const int greenLED = 35;
const int ledpin = 13;
SabertoothSimplified ST;
int phoneTrip;
int foodPressed;
int eot1;
int eot2;
char ssid[] = "x";      //  your network SSID (name)
char pass[] = "x";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
  pinMode(ledpin, OUTPUT);
  pinMode(sensor, INPUT_PULLUP);
  pinMode(limit1, INPUT_PULLUP);
  pinMode(limit2, INPUT_PULLUP);
  pinMode(greenLED, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  SabertoothTXPinSerial.begin(9600);
  Serial.begin(9600);      // initialize serial communication
  //pinMode(9, OUTPUT);      // set the LED pin mode

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true);       // don't continue
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status
}


void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

/*This portion is the html code attempt*/
            client.println("<!DOCTYPE html>");      //web page is made using html
            client.println("<html>");
            client.println("<head>");
            client.println("<title>x</title>");
/*-------------------------------*/

            // the content of the HTTP response follows the header:
            client.print("<B> Control </B>
");
            client.print("Click <a href=\"/H\">here</a> to run the motor
");
            client.print("Click <a href=\"/L\">here</a> to stop the motor
");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")){
          phoneTrip = digitalRead(sensor);
          foodPressed = digitalRead(button);
          eot1 = digitalRead(limit1);
          eot2 = digitalRead(limit2);
          digitalWrite(ledpin, phoneTrip);
          digitalWrite(greenLED, foodPressed);
          
          static enum {CLOSED,OPENING,OPEN,CLOSING} state;

              switch (state) {
                case CLOSED:
                  if (phoneTrip == 1){ //if the sensor is tripped
                    state = OPENING;
                  }
                  break;
                case OPENING:
                ST.motor(1,127);
                  if (eot1 == 1){
                    ST.motor(1,0);
                    state = OPEN;
                  }
                  break;
                case OPEN:
                  if(foodPressed == 1){
                    delay(5000);
                    state = CLOSING;
                  }
                  break;
                case CLOSING:
                  ST.motor(1,-127);
                  if (eot2 == 1){
                    ST.motor(1,0);
                    state = CLOSED;
                  }
                  break;
              }
        }
        if (currentLine.endsWith("GET /L")){
          ST.motor(1,0);
          }
}
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}

what error you are getting? Or what are the issues you are facing with the code?

The code for the system works on its own in a separate sketch. And I can blink the LED in the example simple web server. But when I have the code as shown, the system isn't very responsive to clicking the links on the web page. Basically, the motor just turns uncontrolled, and hiccups a little if I click on the link GET /H that is supposed to engage the logic.

I have now tried to integrate it as shown in this code, where the engage/disengage are separate functions. For some reason when I try to run this while still connected to USB, the serial readout gets stuck halfway through printing a line:

Attempting to connect to Network named: x
SSID: x
IP Address: 192.168.1.213
signal strength (RSSI):-51 dBm
To se

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

char ssid[] = "x";      //  your network SSID (name)
char pass[] = "x";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(80);


const int limit1 = 49;
const int sensor = 47;
const int limit2 = 43;
const int button = 39;
const int greenLED = 35;
const int ledpin = 13;

SabertoothSimplified ST;


int phoneTrip;
int foodPressed;
int eot1;
int eot2;

void setup() {
  Serial.begin(9600);      // initialize serial communication
  pinMode(9, OUTPUT);      // set the LED pin mode

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true);       // don't continue
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();                           // start the web server on port 80
  printWifiStatus();              // you're connected now, so print out the status

  pinMode(ledpin, OUTPUT);
  pinMode(sensor, INPUT_PULLUP);
  pinMode(limit1, INPUT_PULLUP);
  pinMode(limit2, INPUT_PULLUP);
  pinMode(greenLED, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  SabertoothTXPinSerial.begin(9600);

}


void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/H\">here</a> engage the system
");
            client.print("Click <a href=\"/L\">here</a> disengage the system
");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          engageSystem();               // GET /H allows logic to run
        }
        if (currentLine.endsWith("GET /L")) {
          disengageSystem();                // GET /L holds motor
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}

void engageSystem(){
  phoneTrip = digitalRead(sensor);
  foodPressed = digitalRead(button);
  eot1 = digitalRead(limit1);
  eot2 = digitalRead(limit2);
  digitalWrite(ledpin, phoneTrip);
  digitalWrite(greenLED, foodPressed);
  
  static enum {CLOSED,OPENING,OPEN,CLOSING} state;

  switch (state) {
    case CLOSED:
      if (phoneTrip == 1){ //if the sensor is tripped
        state = OPENING;
      }
      break;
    case OPENING:
    ST.motor(1,127);
      if (eot1 == 1){
        ST.motor(1,0);
        state = OPEN;
      }
      break;
    case OPEN:
      if(foodPressed == 1){
        delay(5000);
        state = CLOSING;
      }
      break;
    case CLOSING:
      ST.motor(1,-127);
      if (eot2 == 1){
        ST.motor(1,0);
        state = CLOSED;
      }
      break;
  }
}
   


void disengageSystem(){
  ST.motor(1,0);
}

@cbramo55, stop cross-posting. Threads merged.

@cbramo55 had four threads running. This is the best I could do cleaning up his mess. I believe he has a hardware problem and a software problem. If anyone would like this thread split, just use Report to Moderator with your suggestion. In the meantime @cbramo55 will be taking a short break from the forum.