I need help with a home project

Hi,

I Have uploaded my program to the arduino succesfully. The arduino begins a server. I use the MIT app inventor 2 to send to the http:// address of my arduino an H which should close a relay on pin 6 and send me an analog read of pin A0. It sends the analog read back to me correctly but when it has to close the relay it doesn't want to do that.Inhave also messured the voltage on the pins and when i send the command it doesn't change at all

The program that i used is this.

Note i am a newbie at programming so feel free to correct any of the mistakes i have probably done

Thanks for anyone that bothers to reply

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

int lcontrol = 6;
int lcontrol2 = 7;
int lcontrol3 = 9;
int lcontrol4 = 8;
int lcontrol5 = 10;
char ssid[] = "Name";
char pass[] = "Password";

int keyIndex = 0;

int status = WL_IDLE_STATUS;
 
WiFiServer server(80);

void setup() {
   // Uncommon serial for debugging and to see details of WiFi connection
  // Serial.begin (9600);
  // while (!Serial) {
     // wait for serial port to connect. Needed for native USB port only
//   }
   if  (WiFi.status() == WL_NO_SHIELD) {
    while (true);
   }
   while (status != WL_CONNECTED) {
    status = WiFi.begin(ssid, pass);
    delay(10000);
   }
   server.begin();
}

void loop() {
  WiFiClient client = server.available();

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

          if (currentLine.length() == 0) {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();
            client.print("Value at A0 is ");
            client.print(analogRead(A0));
            client.print("
");
            client.println();
            break;
          }
          else {
            currentLine = "";
          }
        }
        else if (c !='\r'){
          currentLine  += c;
        }
         
          if(currentLine.endsWith("GET/H")) {
            digitalWrite(lcontrol,HIGH);
          }
          if (currentLine.endsWith("GET/L")) {
            digitalWrite(lcontrol,LOW);
          }
          if (currentLine.endsWith("GET/A")) {
            digitalWrite(lcontrol2,HIGH);
          }
          if (currentLine.endsWith("GET/B")) {
            digitalWrite(lcontrol2,LOW);
          }
          if (currentLine.endsWith("GET/C")) {
            digitalWrite(lcontrol3,HIGH);
          }
          if (currentLine.endsWith("GET/D")) {
            digitalWrite(lcontrol3,LOW);
          }
          if (currentLine.endsWith("GET/E")) {
            digitalWrite(lcontrol4,HIGH);
          }
          if (currentLine.endsWith("GET/F")) {
            digitalWrite(lcontrol4,LOW);
          }
          if (currentLine.endsWith("GET/G")) {
            digitalWrite(lcontrol5,HIGH);
          }
          if (currentLine.endsWith("GET/H")) {
            digitalWrite(lcontrol5,LOW);
          }
        }
      }
    }
  }

I had a similar issue with an example from somebody else I helped yesterday.

In the statement below you check whether a http request came with GET/H in it.

if(currentLine.endsWith("GET/H")) { digitalWrite(lcontrol,HIGH);}

For debugging print the variable currentLine.

Serial.println( currentLine );
if(currentLine.endsWith("GET/H")) { digitalWrite(lcontrol,HIGH);}
...

I am pretty sure one of them will look like this. This is the request send from you in http syntax.

GET /H HTTP/1.1

There is a space in between GET and /H. You removed this in your code. Thats why the compare failed.

As you can see from the Serial output you compare the currentLine everytime a new character is received. If you move your code into the

else { currentLine = "" }

you can do the compare only once when the whole http request has been received. But the endsWith is the wrong function to compare. There is a .startsWith() function that solves the issue. You need to move the code before the currentLine gets cleared.

You can add a button to your webpage to send the command easier.

...
client.println("<button class='on' type='submit' value='ON' onmousedown=location.href='/H'>ON
");
client.println("<button class='off' type='submit' value='OFF' onmousedown=location.href='/L'>OFF
");
...

You also need to ensure your pins are outputs. At reset most pins are set to input. Place this in your setup function.

pinMode( lcontrol, OUTPUT );