Arduino-WLAN-Setup-via-Serial

From Digipool-Wiki
Jump to: navigation, search

Arduino-WLAN-Setup-via-Serial

This sample code can help you with Arduino projects with a WLAN connection by allowing you to quickly change the WLAN access data via USB cable. Please note that these changes will be lost after every restart. If the code should contain a WLAN-Name and Key, these could optionally be changed temporarily. If no input is made, the Arduino starts after 5 seconds with the values specified in the code.


Serial-Tool

For serial communication, you can use the Arduino IDE or another serial terminal, such as the following webpage
www.serialterminal.com
(Disable "send with /r", enable "send with /n" and disable "echo", 9600 Baud)


Your main program tab


// 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 Tab

Create an additional tab, 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;
    }
  }
}