Difference between revisions of "Arduino-WLAN-Setup-via-Serial"

From Digipool-Wiki
Jump to: navigation, search
Line 1: Line 1:
 +
 +
 +
== Your main program tap ==
 +
 
<pre>
 
<pre>
  
Line 24: Line 28:
  
 
</pre>
 
</pre>
 +
 +
<br>
 +
 +
== WLAN-Setup Tap ==
 +
 +
Create an additional tap, call it "WLAN-Setup" and place the following code there:
 +
 +
<pre>
 +
 +
void myWlanSetup() {
 +
  long wlanSetupTimer = 0;
 +
  int newWlan = 0;
 +
  String incomingString;
 +
 +
  Serial.println("");
 +
  int myModue = 0;
 +
  if (WlanName == "") {
 +
    Serial.println("Your programme code does not contain a WLAN-Name.");
 +
  } else {
 +
    Serial.print("Your programme code contains the following WLAN-Name: ");
 +
    Serial.println(WlanName);
 +
    myModue = myModue + 1;
 +
  }
 +
  if (WlanKey == "") {
 +
    Serial.println("Your programme code does not contain a WLAN-Key.");
 +
  } else {
 +
    Serial.print("Your programme code contains the following WLAN-Key: ");
 +
    Serial.println(WlanKey);
 +
    myModue = myModue + 1;
 +
  }
 +
 +
  if (myModue > 1) {
 +
    Serial.println("If you want to change the WLAN setup, answer 'y' within 5 seconds! (Keep in mind that these changes will be lost the next time you reboot.)");
 +
  } else {
 +
    Serial.println("If you want to set up the WLAN, answer 'y' within 5 seconds! (Keep in mind that these changes will be lost the next time you reboot.)");
 +
  }
 +
 +
 +
  wlanSetupTimer = millis();
 +
 +
  while (1 == 1) {
 +
    if (newWlan == 0) {
 +
      if (millis() < wlanSetupTimer + 5000) {
 +
        // wait 5 seconds for input
 +
 +
        // save data only when you receive somthing:
 +
        if (Serial.available() > 0) {
 +
          // read the incoming string:
 +
          incomingString = Serial.readStringUntil('\n');
 +
          if (incomingString == "y") {
 +
            Serial.println("WLAN-Name? ");
 +
            incomingString = "";
 +
            newWlan = 1;
 +
          }
 +
        }
 +
      } else {
 +
        newWlan = 3;
 +
      }
 +
    }
 +
 +
    if (newWlan == 1) {
 +
      if (Serial.available() > 0) {
 +
        // read the incoming string:
 +
        incomingString = Serial.readStringUntil('\n');
 +
        // WlanName = String(incomingByte);
 +
        WlanName = incomingString;
 +
        incomingString = "";
 +
        Serial.println("WLAN-Key? ");
 +
        newWlan = 2;
 +
      }
 +
    }
 +
    if (newWlan == 2) {
 +
      if (Serial.available() > 0) {
 +
        // read the incoming string:
 +
        incomingString = Serial.readStringUntil('\n');
 +
        // WlanKey = String(incomingByte);
 +
        WlanKey = incomingString;
 +
        newWlan = 3;
 +
      }
 +
    }
 +
    if (newWlan == 3) {
 +
      newWlan = 4;
 +
      Serial.print("Current WLAN-Name: ");
 +
      Serial.println(WlanName);
 +
      Serial.print("Current WLAN-Key: ");
 +
      Serial.println(WlanKey);
 +
      Serial.println(" ");
 +
      Serial.println("Your Arduino is now starting up");
 +
      break;
 +
    }
 +
  }
 +
}
 +
 +
</pre>
 +
 +
<br>

Revision as of 18:12, 3 January 2023


Your main program tap


// WLAN-Setup-via-Serial
// For serial communication, you can use the Arduino IDE 
// or another serial terminal, such as the following webpage:
// https://www.serialterminal.com
// Disable "send with /r"
// Enable "send with /n"
// Disable "echo"

String WlanName = "";
String WlanKey = "";

void setup() {

Serial.begin(9600);
myWlanSetup();

}

void loop() {
 // place your code here...
}


WLAN-Setup Tap

Create an additional tap, call it "WLAN-Setup" and place the following code there:


void myWlanSetup() {
  long wlanSetupTimer = 0;
  int newWlan = 0;
  String incomingString;

  Serial.println("");
  int myModue = 0;
  if (WlanName == "") {
    Serial.println("Your programme code does not contain a WLAN-Name.");
  } else {
    Serial.print("Your programme code contains the following WLAN-Name: ");
    Serial.println(WlanName);
    myModue = myModue + 1;
  }
  if (WlanKey == "") {
    Serial.println("Your programme code does not contain a WLAN-Key.");
  } else {
    Serial.print("Your programme code contains the following WLAN-Key: ");
    Serial.println(WlanKey);
    myModue = myModue + 1;
  }

  if (myModue > 1) {
    Serial.println("If you want to change the WLAN setup, answer 'y' within 5 seconds! (Keep in mind that these changes will be lost the next time you reboot.)");
  } else {
    Serial.println("If you want to set up the WLAN, answer 'y' within 5 seconds! (Keep in mind that these changes will be lost the next time you reboot.)");
  }


  wlanSetupTimer = millis();

  while (1 == 1) {
    if (newWlan == 0) {
      if (millis() < wlanSetupTimer + 5000) {
        // wait 5 seconds for input

        // save data only when you receive somthing:
        if (Serial.available() > 0) {
          // read the incoming string:
          incomingString = Serial.readStringUntil('\n');
          if (incomingString == "y") {
            Serial.println("WLAN-Name? ");
            incomingString = "";
            newWlan = 1;
          }
        }
      } else {
        newWlan = 3;
      }
    }

    if (newWlan == 1) {
      if (Serial.available() > 0) {
        // read the incoming string:
        incomingString = Serial.readStringUntil('\n');
        // WlanName = String(incomingByte);
        WlanName = incomingString;
        incomingString = "";
        Serial.println("WLAN-Key? ");
        newWlan = 2;
      }
    }
    if (newWlan == 2) {
      if (Serial.available() > 0) {
        // read the incoming string:
        incomingString = Serial.readStringUntil('\n');
        // WlanKey = String(incomingByte);
        WlanKey = incomingString;
        newWlan = 3;
      }
    }
    if (newWlan == 3) {
      newWlan = 4;
      Serial.print("Current WLAN-Name: ");
      Serial.println(WlanName);
      Serial.print("Current WLAN-Key: ");
      Serial.println(WlanKey);
      Serial.println(" ");
      Serial.println("Your Arduino is now starting up");
      break;
    }
  }
}