arduinoarduino-idearduino-mkr1000

How would I create a simple Arduino script that allows me to turn on and off LEDBuitIn remotely?


I'm working on a project that includes working with arduino hardware remotely. I would like to learn how to create a simple script that allows me to turn on and off the Led builtin to my MKR1000 wirelessly. I could then use this knowledge in more complicated projects. After doing some research and looking at the arduino library's sample webserver program, I came up with this Frankenstein of code. After working for hours I just keep on making it worse, I could really use some guidance on what I'm doing wrong, why and how to fix it.

My Frankenstein code:

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

char ssid[] = "ARROW_015D80";
char pass[] = "KRR3K47XZXM3NYRHV7GX";

int status = WL_IDLE_STATUS;
int LED = LED_BUILTIN;
int LEDState = digitalRead(LED);

WiFiServer server(80);

void setup() {
   while (!Serial) {
  }
  Serial.begin(9600);
if (WiFi.status() == WL_NO_SHIELD) {
  Serial.println("Yo, where the wifi shield at?");
  while(true);
}
 while (status !=WL_CONNECTED) {
  Serial.print("Connecting to ssid: ");
  Serial.println(ssid);
  status = WiFi.begin(ssid, pass);

  delay(10000);
 }
 server.begin();
 printWiFiStatus();
}

void loop() {
  WiFiClient client = server.available();
  if (client) {
    Serial.println("+1 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");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.print("State of button:");
          if (LEDState == HIGH){
          client.print("ON");       
          }
          if (LEDState == LOW){
        client.print("OFF");
      }
      client.println("<br>");
      client.println("</html>");
      client.println();
break;
}
else {
  currentLine = "";
}
}
else if (c !='\r') {
  currentLine += c;
    }
    if (currentLine.endsWith("GET /H")) {
      digitalWrite(LED, HIGH);
    }
    if (currentLine.endsWith("Get /L")) {
      digitalWrite(LED, LOW);
    }
    if (currentLine.endsWith("Get /stop")){
      client.stop();
      Serial.println ("Client disconnected");
    }
     }


  }

}
}

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

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

  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

According to the videos and the mismatch of information, this program should connect the arduino to the internet, print the arduino's ip address in the serial monitor and I should be able to change the state of the built in LED by changing the end of the ip address search.

Instead, after showing the ip address and displaying the page, with the button state on it. When I try to change the url to change the button state it bugs out. It takes me to the "This page can't be reached" and the serialmonitor bug out.


Solution

  • Never mind, I found a video online that explain clearly how to write the code I'm looking for. It is extremely helpful : https://www.youtube.com/watch?v=H0p7GVPdlyU

    It also link to a page with all the code for it.