/********* Rui Santos Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-input-data-html-form/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. *********/ // include wifi password #include "config.h" #include #include #include #include #include #include "morse.h" #include // for DS3231 #include // for DS3231 #include // download zip from https://github.com/me-no-dev/ESPAsyncWebServer and install. #include AsyncWebServer server(80); // Assign output variables to GPIO pins //LED_BUILTIN for ESP32 onboard LED, 32 for transmitter keyer const int keyer = 32; const int blinker = LED_BUILTIN; RTC_DS3231 rtc; // set up RTC const int alarmPin = 4; // pin to monitor for RTC alarms // Network options: "0" for existing netowrk, "1" to be an access point const int network = 1; // Connect to existing network // Read from config.h //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"; const char* password_ap = NULL; //"123456789"; //NULL is empty IPAddress local_ip(192,168,0,1); IPAddress gateway(192,168,0,1); IPAddress subnet(255,255,255,0); const char* PARAM_SEND = "inputSend"; const char* PARAM_WPM = "inputWPM"; const char* PARAM_MSG = "inputMsg"; const char* PARAM_CMSG = "inputCustomMsg"; const char* PARAM_FLOAT = "inputFloat"; const char* PARAM_TIME = "inputTimeUnix"; const char* PARAM_START = "inputStartTimeUnix"; 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; int yourInputWPM; int yourInputMsg; int yourInputMsg_old; // to save previous state and check changes String yourInputCustomMsg; float yourInputFloat; uint32_t yourInputTime; //to keep time uint32_t yourInputStartTimeUnix; bool startProgram; 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; // HTML web page to handle input fields const char index_html[] PROGMEM = R"rawliteral( ESP Input Form

Vulpes Radio Orienteering Controller

Local time: . If this is incorrect, your browser is not providing the correct time (Firefox example).

General Settings

Sending program:
Message:
Custom message:
Speed: WPM

Cycle Settings

Only applies when Sending Program is set to "2 - Cycle". You cannot set a cycle start date more than a month in advance.

Cycle start time
Current value:

Step length: milliseconds
Cycle ID:
Number of transmitters:



Network Settings

Network Access:
Existing Wireless Network SSID:
Existing Wireless Network Password:

Access Point: Connect to wireless network "vulpes" and point your browser to URL http://192.168.0.1 (http, not https)
Existing Network (advanced): Connect to the same existing network and use the proper IP address (useful if you have access to the router or a serial connection).

)rawliteral"; void notFound(AsyncWebServerRequest *request) { request->send(404, "text/plain", "Not found"); } String readFile(fs::FS &fs, const char * path){ //Serial.printf("Reading file: %s\r\n", path); File file = fs.open(path, "r"); if(!file || file.isDirectory()){ Serial.println("- empty file or failed to open file"); return String(); } //Serial.println("- read from file:"); String fileContent; while(file.available()){ fileContent+=String((char)file.read()); } file.close(); //Serial.println(fileContent); return fileContent; } void writeFile(fs::FS &fs, const char * path, const char * message){ Serial.printf("Writing file: %s\r\n", path); File file = fs.open(path, "w"); if(!file){ Serial.println("- failed to open file for writing"); return; } if(file.print(message)){ Serial.println("- file written"); } else { Serial.println("- write failed"); } file.close(); } // Replaces placeholder in web UI with stored values String processor(const String& var){ //Serial.println(var); if(var == "inputCustomMsg"){ return readFile(SPIFFS, "/inputCustomMsg.txt"); } else if(var == "inputSend"){ return readFile(SPIFFS, "/inputSend.txt"); } else if(var == "inputWPM"){ return readFile(SPIFFS, "/inputWPM.txt"); } else if(var == "inputMsg"){ return readFile(SPIFFS, "/inputMsg.txt"); } else if(var == "inputStepLength"){ return readFile(SPIFFS, "/inputStepLength.txt"); } else if(var == "inputCycleID"){ return readFile(SPIFFS, "/inputCycleID.txt"); } 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"){ // Webform breaks if this value is empty. String temp = readFile(SPIFFS, "/inputStartTimeUnix.txt"); if(temp == ""){ temp = "0"; } return temp; } return String(); } // https://www.thegeekpub.com/276838/how-to-reset-an-arduino-using-code/ void(* resetFunc) (void) = 0; // create a standard reset function // Set up arduinomorse pin and default WPM LEDMorseSender sender_blink(blinker, 10.0f); //f makes it a float LEDMorseSender sender_key(keyer, 10.0f); //================================================================================ // setup(): stuff that only gets done once, after power up (KB1OIQ's description) //================================================================================ void setup() { Serial.begin(115200); // Get arduinomorse ready to go sender_blink.setup(); sender_key.setup(); pinMode(alarmPin, INPUT_PULLUP); // Set alarm pin as pullup if (! rtc.begin()) { Serial.println("Couldn't find RTC"); Serial.flush(); while (1) delay(10); } if (rtc.lostPower()) { Serial.println("RTC lost power, let's set the time!"); // When time needs to be set on a new device, or after a power loss, the // following line sets the RTC to the date & time this sketch was compiled rtc.adjust(DateTime(__DATE__, __TIME__)); // This line sets the RTC with an explicit date & time, for example to set // January 21, 2014 at 3am you would call: //rtc.adjust(DateTime(2023, 9, 2, 17, 32, 0)); } // Report the RTC time after waiting two seconds // https://amiok.net/gitea/W1CDN/vulpes/issues/50#issuecomment-1376 Serial.println("Wait 2s for RTC"); delay(2000); Serial.println("RTC time on startup: "); Serial.println(rtc.now().unixtime()); Serial.println(rtc.now().timestamp()); // Are there any RTC alarms set? DateTime alarm_one = rtc.getAlarm1(); // Get the current time char buff[] = "Alarm 1 set for at hh:mm:ss DDD, DD MMM YYYY"; Serial.print(alarm_one.toString(buff)); Serial.println(" (only HH:MM:SS day-of-month are accurate)"); // Initialize the output variables as outputs pinMode(keyer, OUTPUT); pinMode(blinker, OUTPUT); // Set outputs to LOW digitalWrite(keyer, LOW); digitalWrite(blinker, LOW); // Initialize SPIFFS SPIFFS.begin(true); if(!SPIFFS.begin(true)){ Serial.println("An Error has occurred while mounting SPIFFS"); return; } if(!SPIFFS.begin()){ Serial.println("An Error has occurred while mounting SPIFFS"); return; } // Read in existing data yourInputCustomMsg = readFile(SPIFFS, "/inputCustomMsg.txt"); yourInputSend = readFile(SPIFFS, "/inputSend.txt").toInt(); yourInputWPM = readFile(SPIFFS, "/inputWPM.txt").toFloat(); yourInputMsg = readFile(SPIFFS, "/inputMsg.txt").toInt(); yourInputFloat = readFile(SPIFFS, "/inputFloat.txt").toFloat(); yourInputStartTimeUnix = readFile(SPIFFS, "/inputStartTimeUnix.txt").toInt(); 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); // On restart, keep doing what you were doing before yourInputMsg_old = yourInputMsg; if(yourInputMsg == 0){ sender_blink.setMessage(yourInputCustomMsg); sender_key.setMessage(yourInputCustomMsg); } else if(yourInputMsg == 1){ sender_blink.setMessage(String("moe ")); sender_key.setMessage(String("moe ")); } else if(yourInputMsg == 2){ sender_blink.setMessage(String("moi ")); sender_key.setMessage(String("moi ")); } else if(yourInputMsg == 3){ sender_blink.setMessage(String("mos ")); sender_key.setMessage(String("mos ")); } else if(yourInputMsg == 4){ sender_blink.setMessage(String("moh ")); sender_key.setMessage(String("moh ")); } else if(yourInputMsg == 5){ sender_blink.setMessage(String("mo5 ")); sender_key.setMessage(String("mo5 ")); } WiFi.setHostname("vulpes"); if (yourInputNetwork == 1){ // Attach to existing wifi WiFi.mode(WIFI_STA); 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!"); // If you fail to connect, act as new access point WiFi.softAPConfig(local_ip, gateway, subnet); WiFi.softAP(ssid_ap, password_ap); //return; } Serial.print("IP Address: "); Serial.println(WiFi.localIP()); } else if (yourInputNetwork == 0){ // Act as new access point WiFi.softAPConfig(local_ip, gateway, subnet); WiFi.softAP(ssid_ap, password_ap); } // Send web page with input fields to client server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/html", index_html, processor); }); // Form 1 // Send a GET request to /get?inputCustomMsg= server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) { String inputMessage; // GET inputCustomMsg value on /get?inputCustomMsg= if (request->hasParam(PARAM_CMSG)) { inputMessage = request->getParam(PARAM_CMSG)->value(); // arduinomorse needs lowercase characters std::transform(inputMessage.begin(), inputMessage.end(), inputMessage.begin(), ::tolower); writeFile(SPIFFS, "/inputCustomMsg.txt", inputMessage.c_str()); yourInputCustomMsg = inputMessage; } // GET inputSend value on /get?inputSend= if (request->hasParam(PARAM_SEND)) { inputMessage = request->getParam(PARAM_SEND)->value(); writeFile(SPIFFS, "/inputSend.txt", inputMessage.c_str()); yourInputSend = inputMessage.toInt(); // if not running a program, set the program running off //if(yourInputSend != 2){ // Cease all programs on new input startProgram = false; programRunning = false; //} } // GET inputWPM value on /get?inputWPM= if (request->hasParam(PARAM_WPM)) { inputMessage = request->getParam(PARAM_WPM)->value(); writeFile(SPIFFS, "/inputWPM.txt", inputMessage.c_str()); yourInputWPM = inputMessage.toFloat(); sender_blink.setWPM(yourInputWPM); sender_key.setWPM(yourInputWPM); } // GET inputMsg value on /get?inputMsg= if (request->hasParam(PARAM_MSG)) { inputMessage = request->getParam(PARAM_MSG)->value(); writeFile(SPIFFS, "/inputMsg.txt", inputMessage.c_str()); // save previous state yourInputMsg_old = yourInputMsg; yourInputMsg = inputMessage.toInt(); // Check the message every time the form is submitted. if(yourInputMsg == 0){ sender_blink.setMessage(yourInputCustomMsg); sender_key.setMessage(yourInputCustomMsg); } else if(yourInputMsg == 1){ sender_blink.setMessage(String("moe ")); sender_key.setMessage(String("moe ")); } else if(yourInputMsg == 2){ sender_blink.setMessage(String("moi ")); sender_key.setMessage(String("moi ")); } else if(yourInputMsg == 3){ sender_blink.setMessage(String("mos ")); sender_key.setMessage(String("mos ")); } else if(yourInputMsg == 4){ sender_blink.setMessage(String("moh ")); sender_key.setMessage(String("moh ")); } else if(yourInputMsg == 5){ sender_blink.setMessage(String("mo5 ")); sender_key.setMessage(String("mo5 ")); } } // GET inputStepLength value on /get?inputStepLength= if (request->hasParam(PARAM_STEPLENGTH)) { inputMessage = request->getParam(PARAM_STEPLENGTH)->value(); writeFile(SPIFFS, "/inputStepLength.txt", inputMessage.c_str()); yourInputStepLength = inputMessage.toInt(); } // GET inputCycleID value on /get?inputCycleID= if (request->hasParam(PARAM_CYCLEID)) { inputMessage = request->getParam(PARAM_CYCLEID)->value(); writeFile(SPIFFS, "/inputCycleID.txt", inputMessage.c_str()); yourInputCycleID = inputMessage.toInt(); } // GET inputNtransmitters value on /get?inputNtransmitters= if (request->hasParam(PARAM_NTRANS)) { inputMessage = request->getParam(PARAM_NTRANS)->value(); writeFile(SPIFFS, "/inputNtransmitters.txt", inputMessage.c_str()); yourInputNtransmitters = inputMessage.toInt(); } // GET inputTimeUnix value on /get?inputTimeUnix= if (request->hasParam(PARAM_TIME)) { inputMessage = request->getParam(PARAM_TIME)->value(); //https://stackoverflow.com/a/22733127/2152245 yourInputTime = atol(inputMessage.c_str()); Serial.print("yourInputTime: "); Serial.println(yourInputTime); // update the RTC time rtc.adjust(DateTime(yourInputTime)); DateTime now = rtc.now(); // Might work to fix random errors? If date is far in the future, // try to update again. // replace if with while if you want it to try a bunch... if(now.year() > 2040){ Serial.print("Year is "); Serial.println(now.year()); Serial.println("RTC can't set time. Trying again."); rtc.adjust(DateTime(yourInputTime)); } Serial.print("UTC time from browser: "); Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(" ("); Serial.print(now.dayOfTheWeek()); Serial.print(") "); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.println(); Serial.print("rtc.now().unixtime(): "); Serial.println(rtc.now().unixtime()); } // GET inputFloat value on /get?inputFloat= if (request->hasParam(PARAM_FLOAT)) { inputMessage = request->getParam(PARAM_FLOAT)->value(); writeFile(SPIFFS, "/inputFloat.txt", inputMessage.c_str()); yourInputFloat = inputMessage.toFloat(); } // GET inputStartTimeUnix value on /get?inputStartTimeUnix= if (request->hasParam(PARAM_START)) { inputMessage = request->getParam(PARAM_START)->value(); Serial.println(inputMessage); // if a start time isn't entered, don't overwrite the old one //if(!(inputMessage != NULL && inputMessage[0] == '\0')){ writeFile(SPIFFS, "/inputStartTimeUnix.txt", inputMessage.c_str()); yourInputStartTimeUnix = atol(inputMessage.c_str()); //} Serial.println(yourInputStartTimeUnix); // Use alarm built into RTC rtc.setAlarm1(DateTime(yourInputStartTimeUnix), DS3231_A1_Date); //rtc.setAlarm1(DateTime(2020, 6, 25, 15, 34, 0), DS3231_A2_Date); DateTime alarm_one = rtc.getAlarm1(); // Get the current alarm time char buff[] = "Alarm 1 set for at hh:mm:ss DDD, DD MMM YYYY"; Serial.print(alarm_one.toString(buff)); Serial.println(" (only HH:MM:SS day-of-month are accurate)"); } // 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 /get2?inputNetwork= 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 /get2?inputSSID= 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 /get2?inputNetwork= if (request->hasParam(PARAM_PASSWORD)) { inputMessage = request->getParam(PARAM_PASSWORD)->value(); writeFile(SPIFFS, "/inputPassword.txt", inputMessage.c_str()); yourInputPassword = inputMessage; Serial.println(yourInputPassword); } // Shouldn't need to do this if using this form. //request->redirect("/"); resetFunc(); // reset the Arduino via software function }); server.onNotFound(notFound); server.begin(); } void loop() { // This statement from https://github.com/garrysblog/DS3231-Alarm-With-Adafruit-RTClib-Library/blob/master/DS3231-RTClib-Adafruit-Alarm-Poll-alarmFired/DS3231-RTClib-Adafruit-Alarm-Poll-alarmFired.ino // Check if alarm by polling SQW alarm pin if((yourInputSend == 2) & (digitalRead(alarmPin) == LOW)) { // Print current time and date DateTime now = rtc.now(); // Get the current time char buff[] = "Alarm triggered at hh:mm:ss DDD, DD MMM YYYY"; Serial.println(now.toString(buff)); startProgram = true; // Disable and clear alarm rtc.clearAlarm(1); rtc.clearAlarm(2); // clear the other one just in case } // Once alarm has started the program, set things up to run if(startProgram == true){ //Serial.println("Start sending"); start_millis = millis() + ((yourInputCycleID - 1) * yourInputStepLength); stop_millis = start_millis + yourInputStepLength; if(yourInputCycleID == 1){ pause_until_millis = stop_millis + (yourInputStepLength * (yourInputNtransmitters - 1)); } else { // Subtract 2 rather than 1 here to account for start_millis duration at beginning of repeat. pause_until_millis = stop_millis + (yourInputStepLength * (yourInputNtransmitters - 2)); } programRunning = true; startProgram = false; } // if you want to send continuous code, and it's not sending, then start it up if((yourInputSend == 1)){ // If not sending, start sending. Yes, these need to be separate statements. if (!sender_blink.continueSending()){ sender_blink.startSending(); } if (!sender_key.continueSending()){ sender_key.startSending(); } // if you want to send cycle code and it's not sending, then start it up } else if((yourInputSend == 2) & (programRunning == true)){ if((millis() < start_millis)){ // Shut the pin off manually digitalWrite(blinker, LOW); digitalWrite(keyer, LOW); } else if((millis() >= start_millis) & (millis() <= stop_millis)){ // If not sending, start sending. Yes, these need to be separate statements // for the blinker and keyer. if (!sender_blink.continueSending()){ sender_blink.startSending(); } if (!sender_key.continueSending()){ sender_key.startSending(); } } else if((millis() >= stop_millis) & (millis() <= pause_until_millis)){ // do nothing in this case -- in between cycles // Shut the pin off manually digitalWrite(blinker, LOW); digitalWrite(keyer, LOW); } else if((millis() >= pause_until_millis)){ startProgram = true; } // if the cycle program is not running } else if((yourInputSend == 2) & (programRunning == false)){ // do we need something here? // if you don't want to send code } else if(yourInputSend == 0){ // Shut the pin off manually digitalWrite(blinker, LOW); digitalWrite(keyer, LOW); } }