Add option to set up for wifi or AP on compile #53
							
								
								
									
										83
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										83
									
								
								src/main.cpp
									
									
									
									
									
								
							@@ -38,8 +38,8 @@ const int alarmPin = 4; // pin to monitor for RTC alarms
 | 
			
		||||
const int network = 1;
 | 
			
		||||
// Connect to existing network
 | 
			
		||||
// Read from config.h
 | 
			
		||||
const char* ssid = WIFI_SSID;
 | 
			
		||||
const char* password = WIFI_PASSWORD;
 | 
			
		||||
//const char* ssid = WIFI_SSID;
 | 
			
		||||
//const char* password = WIFI_PASSWORD;
 | 
			
		||||
// Create a new access point
 | 
			
		||||
// Replace with your desired network credentials
 | 
			
		||||
const char* ssid_ap     = "vulpes";
 | 
			
		||||
@@ -59,6 +59,9 @@ const char* PARAM_RUNNING = "programRunning";
 | 
			
		||||
const char* PARAM_STEPLENGTH = "inputStepLength";
 | 
			
		||||
const char* PARAM_CYCLEID = "inputCycleID";
 | 
			
		||||
const char* PARAM_NTRANS = "inputNtransmitters";
 | 
			
		||||
const char* PARAM_NETWORK = "inputNetwork";
 | 
			
		||||
const char* PARAM_SSID = "inputSSID";
 | 
			
		||||
const char* PARAM_PASSWORD = "inputPassword";
 | 
			
		||||
 | 
			
		||||
// Global variables
 | 
			
		||||
int yourInputSend;
 | 
			
		||||
@@ -74,6 +77,9 @@ bool programRunning;
 | 
			
		||||
int yourInputStepLength;
 | 
			
		||||
int yourInputCycleID;
 | 
			
		||||
int yourInputNtransmitters;
 | 
			
		||||
int yourInputNetwork;
 | 
			
		||||
String yourInputSSID;
 | 
			
		||||
String yourInputPassword;
 | 
			
		||||
long start_millis = 0;
 | 
			
		||||
long stop_millis = 0;
 | 
			
		||||
long pause_until_millis = 0;
 | 
			
		||||
@@ -123,6 +129,7 @@ const char index_html[] PROGMEM = R"rawliteral(
 | 
			
		||||
      // Fill in the other form fields
 | 
			
		||||
      document.getElementById("send-program").value = %inputSend%;
 | 
			
		||||
      document.getElementById("message").value = %inputMsg%;
 | 
			
		||||
      document.getElementById("network").value = %inputNetwork%;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  </script></head><body>
 | 
			
		||||
@@ -177,6 +184,23 @@ const char index_html[] PROGMEM = R"rawliteral(
 | 
			
		||||
    <input type="submit" value="Submit"">
 | 
			
		||||
  </form>
 | 
			
		||||
  <iframe style="display:none" name="hidden-form" id="hidden-form"></iframe>
 | 
			
		||||
 | 
			
		||||
  <br><hr>
 | 
			
		||||
  <h2>Network Settings</h2>
 | 
			
		||||
  <form action="/get2" accept-charset=utf-8>
 | 
			
		||||
    <p>Network Access<br>
 | 
			
		||||
    AP URL (http, not http<b>s</b>): <a href="http://192.168.0.1">http://192.168.0.1</a><br>
 | 
			
		||||
    <select name="inputNetwork" id="network">
 | 
			
		||||
      <option value="0">Access Point</option>
 | 
			
		||||
      <option value="1">Existing Wireless Network</option>
 | 
			
		||||
    </select><br>
 | 
			
		||||
    Existing Network SSID: <input type="text" name="inputSSID" value = "%inputSSID%"><br>
 | 
			
		||||
    Existing Network Password: <input type="password" name="inputPassword" value = "%inputPassword%"><br>
 | 
			
		||||
    </p>
 | 
			
		||||
  <input type="submit" value="Submit"">
 | 
			
		||||
  </form>
 | 
			
		||||
 | 
			
		||||
  <iframe style="display:none" name="hidden-form02" id="hidden-form02"></iframe>
 | 
			
		||||
  <script type="text/javascript">
 | 
			
		||||
    
 | 
			
		||||
  </script>
 | 
			
		||||
@@ -243,6 +267,15 @@ String processor(const String& var){
 | 
			
		||||
  else if(var == "inputNtransmitters"){
 | 
			
		||||
    return readFile(SPIFFS, "/inputNtransmitters.txt");
 | 
			
		||||
  }
 | 
			
		||||
  else if(var == "inputNetwork"){
 | 
			
		||||
    return readFile(SPIFFS, "/inputNetwork.txt");
 | 
			
		||||
  }
 | 
			
		||||
  else if(var == "inputSSID"){
 | 
			
		||||
    return readFile(SPIFFS, "/inputSSID.txt");
 | 
			
		||||
  }
 | 
			
		||||
  else if(var == "inputPassword"){
 | 
			
		||||
    return readFile(SPIFFS, "/inputPassword.txt");
 | 
			
		||||
  }
 | 
			
		||||
  else if(var == "inputFloat"){
 | 
			
		||||
    return readFile(SPIFFS, "/inputFloat.txt");
 | 
			
		||||
  } else if(var == "inputStartTimeUnix"){
 | 
			
		||||
@@ -331,6 +364,9 @@ void setup() {
 | 
			
		||||
  yourInputStepLength = readFile(SPIFFS, "/inputStepLength.txt").toInt();
 | 
			
		||||
  yourInputCycleID = readFile(SPIFFS, "/inputCycleID.txt").toInt();
 | 
			
		||||
  yourInputNtransmitters = readFile(SPIFFS, "/inputNtransmitters.txt").toInt();
 | 
			
		||||
  yourInputNetwork = readFile(SPIFFS, "/inputNetwork.txt").toInt();
 | 
			
		||||
  yourInputSSID = readFile(SPIFFS, "/inputSSID.txt");
 | 
			
		||||
  yourInputPassword = readFile(SPIFFS, "/inputPassword.txt");
 | 
			
		||||
  // Set WPM from saved value
 | 
			
		||||
  sender_blink.setWPM(yourInputWPM);
 | 
			
		||||
  sender_key.setWPM(yourInputWPM);
 | 
			
		||||
@@ -358,18 +394,22 @@ void setup() {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  WiFi.setHostname("vulpes");
 | 
			
		||||
  if (network == 0){
 | 
			
		||||
  if (yourInputNetwork == 1){
 | 
			
		||||
    // Attach to existing wifi
 | 
			
		||||
    WiFi.mode(WIFI_STA);
 | 
			
		||||
    WiFi.begin(ssid, password);
 | 
			
		||||
    const char* ssid_char = yourInputSSID.c_str();
 | 
			
		||||
    const char* password_char = yourInputPassword.c_str();
 | 
			
		||||
    WiFi.begin(ssid_char, password_char);
 | 
			
		||||
    if (WiFi.waitForConnectResult() != WL_CONNECTED) {
 | 
			
		||||
      Serial.println("WiFi Failed!");
 | 
			
		||||
      return;
 | 
			
		||||
      // If you fail to connect, act as new access point
 | 
			
		||||
      WiFi.softAPConfig(local_ip, gateway, subnet);
 | 
			
		||||
      WiFi.softAP(ssid_ap, password_ap);
 | 
			
		||||
      //return;
 | 
			
		||||
    }
 | 
			
		||||
    Serial.println();
 | 
			
		||||
    Serial.print("IP Address: ");
 | 
			
		||||
    Serial.println(WiFi.localIP());
 | 
			
		||||
  } else if (network == 1){
 | 
			
		||||
  } else if (yourInputNetwork == 0){
 | 
			
		||||
    // Act as new access point
 | 
			
		||||
    WiFi.softAPConfig(local_ip, gateway, subnet);
 | 
			
		||||
    WiFi.softAP(ssid_ap, password_ap);
 | 
			
		||||
@@ -382,6 +422,7 @@ void setup() {
 | 
			
		||||
    request->send_P(200, "text/html", index_html, processor);
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  // Form 1 
 | 
			
		||||
  // Send a GET request to <ESP_IP>/get?inputCustomMsg=<inputMessage>
 | 
			
		||||
  server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
 | 
			
		||||
    String inputMessage;
 | 
			
		||||
@@ -528,6 +569,34 @@ void setup() {
 | 
			
		||||
    // https://techtutorialsx.com/2018/01/14/esp32-arduino-http-server-external-and-internal-redirects/
 | 
			
		||||
    request->redirect("/");
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  // Form 2
 | 
			
		||||
  server.on("/get2", HTTP_GET, [] (AsyncWebServerRequest *request) {
 | 
			
		||||
    String inputMessage;
 | 
			
		||||
    /// GET inputNetwork value on <ESP_IP>/get2?inputNetwork=<inputMessage>
 | 
			
		||||
    if (request->hasParam(PARAM_NETWORK)) {
 | 
			
		||||
      inputMessage = request->getParam(PARAM_NETWORK)->value();
 | 
			
		||||
      writeFile(SPIFFS, "/inputNetwork.txt", inputMessage.c_str());
 | 
			
		||||
      yourInputNetwork = inputMessage.toInt();
 | 
			
		||||
      Serial.println(yourInputNetwork);
 | 
			
		||||
    }
 | 
			
		||||
    /// GET inputSSID value on <ESP_IP>/get2?inputSSID=<inputMessage>
 | 
			
		||||
    if (request->hasParam(PARAM_SSID)) {
 | 
			
		||||
      inputMessage = request->getParam(PARAM_SSID)->value();
 | 
			
		||||
      writeFile(SPIFFS, "/inputSSID.txt", inputMessage.c_str());
 | 
			
		||||
      yourInputSSID = inputMessage;
 | 
			
		||||
      Serial.println(yourInputSSID);
 | 
			
		||||
    }
 | 
			
		||||
    /// GET inputNetwork value on <ESP_IP>/get2?inputNetwork=<inputMessage>
 | 
			
		||||
    if (request->hasParam(PARAM_PASSWORD)) {
 | 
			
		||||
      inputMessage = request->getParam(PARAM_PASSWORD)->value();
 | 
			
		||||
      writeFile(SPIFFS, "/inputPassword.txt", inputMessage.c_str());
 | 
			
		||||
      yourInputPassword = inputMessage;
 | 
			
		||||
      Serial.println(yourInputPassword);
 | 
			
		||||
    }
 | 
			
		||||
    request->redirect("/");
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  server.onNotFound(notFound);
 | 
			
		||||
  server.begin();
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user