Compare commits

63 Commits

Author SHA1 Message Date
ab7020f718 Simple test of voltage measurement. 2023-10-09 20:43:52 -05:00
52b910ed79 Merge pull request 'Add option to set up for wifi or AP on compile' (#53) from access-point into main
Reviewed-on: #53
2023-10-01 11:29:20 -05:00
0478506e87 Add confirmation so people don't click the wrong button. 2023-09-27 20:56:37 -05:00
9679248691 Default to AP if wifi is inaccessible. 2023-09-27 20:43:01 -05:00
db7c0adfed Reboot on network change form. 2023-09-27 20:12:38 -05:00
98536b8e76 Stub out network form. 2023-09-26 20:57:41 -05:00
2f6fd9a05e Remove old file and stub out readme. 2023-09-25 22:06:49 -05:00
4e797b9281 Move folder. 2023-09-25 22:02:03 -05:00
5e3503b497 Use integer network setting to avoid string comparison. 2023-09-25 21:53:20 -05:00
7fc43238cd Simplify SSID. 2023-09-25 21:47:48 -05:00
8106d576be Add option to set up for wifi or ap on compile. 2023-09-25 21:36:37 -05:00
e942576fd8 Merge pull request 'Make sure RTC is counting time on battery' (#51) from rtc-fix01 into main
Reviewed-on: #51
2023-09-25 21:14:57 -05:00
c14215d42f Wait for RTC before querying on boot. 2023-09-25 21:12:25 -05:00
99f6ebfe93 Add delay before reading RTC. 2023-09-19 11:11:46 -05:00
dccd3d27f4 Merge pull request 'Add custom message ability' (#47) from custom-message into main
Reviewed-on: #47
2023-09-16 15:48:03 -05:00
2618d82e12 Add custom message ability 2023-09-16 09:51:39 -05:00
08b1bdf3fd Merge pull request 'Change WPM on webform' (#45) from wpm into main
Reviewed-on: #45
2023-09-16 09:17:17 -05:00
d973bbf19a Clean up. 2023-09-16 09:16:35 -05:00
f4287eba7b Control WPM from webform. 2023-09-15 21:44:19 -05:00
3474d8ba17 Merge pull request 'Key radio as well as blinking LED' (#44) from key-radio into main
Reviewed-on: #44
2023-09-15 21:19:50 -05:00
ffb4163980 Wire up keyer. 2023-09-15 19:58:46 -05:00
3db888e530 Merge pull request 'Set up scheduled program cycles' (#24) from program-cycle into main
Reviewed-on: #24
2023-09-15 16:51:44 -05:00
a93498ac84 Merge pull request 'Use arduinomorse instead of Jled' (#38) from arduinomorse into program-cycle
Reviewed-on: #38
2023-09-15 16:35:46 -05:00
6c1a718a98 Do the math right on cycle timing. 2023-09-15 12:22:02 -05:00
4380a56055 Shelve. 2023-09-14 21:02:28 -05:00
f5aa43ef76 Clean out old Jled and arduino-timer code. 2023-09-14 17:09:55 -05:00
c8f9d823da Add back ability to send continuously. 2023-09-14 16:36:22 -05:00
a47cd1465a Clean up. 2023-09-14 12:46:35 -05:00
73a6b694a9 Cycle appropriately. 2023-09-14 12:44:10 -05:00
06e69bd8c0 Start on schedule and able to stop. 2023-09-13 21:54:43 -05:00
dd085635a2 Shelve. 2023-09-13 21:06:16 -05:00
087ad27c8c Add arduinomorse and dependencies. 2023-09-13 20:24:57 -05:00
5c655a381b Snapshot. 2023-09-13 19:58:45 -05:00
dc765af473 Working snapshot. 2023-09-12 21:32:11 -05:00
3aba0583be Stub out a non-working function to set up Jled sequences. 2023-09-11 21:47:37 -05:00
1bde39e148 Snapshot. 2023-09-11 21:09:27 -05:00
958e71513c Snapshot. 2023-09-10 12:35:02 -05:00
70decbbcca Only send cycles when program is running. 2023-09-09 16:01:41 -05:00
2127d4a75a Add extra space at end of message to workaround Jled bug. 2023-09-09 13:14:28 -05:00
40baa679e1 Working cycle example with hardcoded variables. 2023-09-09 12:53:34 -05:00
e706327623 Shelve. 2023-09-08 22:55:31 -05:00
fbe5a4a6e0 Snapshot. 2023-09-08 21:21:19 -05:00
26a1ee97c0 Delete a bunch of old stuff. 2023-09-08 15:07:57 -05:00
cbdc7ec939 Use Alarm2 instead of Alarm2 to avoid late alarms. 2023-09-08 15:00:48 -05:00
38c1417351 Report time alarm is set (serial). 2023-09-08 11:12:44 -05:00
57a1c1af80 Snapshot. 2023-09-08 10:39:24 -05:00
af4920d634 Snapshot. 2023-09-08 08:39:14 -05:00
a69128397a Snapshot. 2023-09-07 20:20:53 -05:00
f496a10ef2 Try to account for infrequent weird RTC times. 2023-09-06 22:07:48 -05:00
afe5b9338d Keep form fields updated. 2023-09-06 21:52:04 -05:00
a996c13e63 Snapshot to capture working refresh... 2023-09-06 21:36:10 -05:00
dab1590608 Use unix timestamps and convert to/from local in js. 2023-09-06 21:21:05 -05:00
90b3137165 Shelve. 2023-09-06 20:14:16 -05:00
55ea853100 Ignore vscode settings. 2023-09-06 19:24:03 -05:00
b97f48858d Quick fix of empty start time. 2023-09-06 18:08:36 -05:00
b648900c7b Shelve. 2023-09-06 18:02:07 -05:00
c174d7f594 Make date formats match and stop passing an extra variable. 2023-09-06 16:57:57 -05:00
fd61efebba Get on local time, baby. 2023-09-05 21:11:31 -05:00
8a95413224 Snapshot. 2023-09-05 20:41:55 -05:00
458232f08f Get dates lined up, readable, and values refreshed. 2023-09-05 16:55:42 -05:00
a47451b541 Get time and start time aligned. 2023-09-05 15:41:56 -05:00
2060df9691 Refresh webform automatically after submit so values are right. 2023-09-05 11:08:01 -05:00
c65bc22028 Shelve.
Collect unix time from webform, but need to make sure it is UTC.
2023-09-04 21:53:09 -05:00
18 changed files with 1221 additions and 1352 deletions

View File

@ -3,4 +3,5 @@
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
.vscode/*
*/config.h

5
README.md Normal file
View File

@ -0,0 +1,5 @@
# Vulpes
## Access Point
When using as a wireless access point, the network SSID is "vulpes"
with no password. Navigate to http://192.168.0.1 to access webform.

View File

@ -1,183 +0,0 @@
/*********
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 <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <SPIFFS.h>
#include <Preferences.h>
// download zip from https://github.com/me-no-dev/ESPAsyncWebServer and install.
#include <ESPAsyncWebServer.h>
AsyncWebServer server(80);
// Read from config.h
const char* ssid = WIFI_SSID;
const char* password = WIFI_PASSWORD;
const char* PARAM_STRING = "inputString";
const char* PARAM_INT = "inputInt";
const char* PARAM_FLOAT = "inputFloat";
// HTML web page to handle 3 input fields (inputString, inputInt, inputFloat)
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html><head>
<title>ESP Input Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--<script>
function submitMessage() {
alert("Saved value to ESP SPIFFS");
setTimeout(function(){ document.location.reload(false); }, 500);
}
</script>-->
</head><body>
<form action="/get">
inputString (current value %inputString%): <input type="text" name="inputString">
<!--<input type="submit" value="Submit" ">
</form>--><br>
<!--<form action="/get" target="hidden-form">-->
inputInt (current value %inputInt%): <input type="number " name="inputInt">
<!--<input type="submit" value="Submit" ">
</form>--><br>
<!--<form action="/get" target="hidden-form">-->
inputFloat (current value %inputFloat%): <input type="number " name="inputFloat">
<input type="submit" value="Submit" ">
</form>
<!--<iframe style="display:none" name="hidden-form"></iframe>-->
</body></html>)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 with stored values
String processor(const String& var){
//Serial.println(var);
if(var == "inputString"){
return readFile(SPIFFS, "/inputString.txt");
}
else if(var == "inputInt"){
return readFile(SPIFFS, "/inputInt.txt");
}
else if(var == "inputFloat"){
return readFile(SPIFFS, "/inputFloat.txt");
}
return String();
}
void setup() {
Serial.begin(115200);
// Initialize SPIFFS
#ifdef ESP32
if(!SPIFFS.begin(true)){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
#else
if(!SPIFFS.begin()){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
#endif
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Failed!");
return;
}
Serial.println();
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Send web page with input fields to client
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html, processor);
});
// Send a GET request to <ESP_IP>/get?inputString=<inputMessage>
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage;
// GET inputString value on <ESP_IP>/get?inputString=<inputMessage>
if (request->hasParam(PARAM_STRING)) {
inputMessage = request->getParam(PARAM_STRING)->value();
writeFile(SPIFFS, "/inputString.txt", inputMessage.c_str());
}
// GET inputInt value on <ESP_IP>/get?inputInt=<inputMessage>
else if (request->hasParam(PARAM_INT)) {
inputMessage = request->getParam(PARAM_INT)->value();
writeFile(SPIFFS, "/inputInt.txt", inputMessage.c_str());
}
// GET inputFloat value on <ESP_IP>/get?inputFloat=<inputMessage>
else if (request->hasParam(PARAM_FLOAT)) {
inputMessage = request->getParam(PARAM_FLOAT)->value();
writeFile(SPIFFS, "/inputFloat.txt", inputMessage.c_str());
}
else {
inputMessage = "No message sent";
}
Serial.println(inputMessage);
request->send(200, "text/text", inputMessage);
});
server.onNotFound(notFound);
server.begin();
}
void loop() {
// To access your stored values on inputString, inputInt, inputFloat
String yourInputString = readFile(SPIFFS, "/inputString.txt");
Serial.print("*** Your inputString: ");
Serial.println(yourInputString);
int yourInputInt = readFile(SPIFFS, "/inputInt.txt").toInt();
Serial.print("*** Your inputInt: ");
Serial.println(yourInputInt);
float yourInputFloat = readFile(SPIFFS, "/inputFloat.txt").toFloat();
Serial.print("*** Your inputFloat: ");
Serial.println(yourInputFloat);
delay(5000);
}

View File

@ -1,169 +0,0 @@
/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com
*********/
// include wifi password
#include "config.h"
// Load Wi-Fi library
#include <WiFi.h>
// Load Preferences library
#include <Preferences.h>
// Define preferences instance
Preferences prefs;
// Open up preferences with defined namespace
prefs.begin("vulpes", false);
// Replace with your network credentials
const char* ssid = WIFI_SSID;
const char* password = WIFI_PASSWORD;
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the current output state
String output26State = "off";
String output27State = "off";
// Assign output variables to GPIO pins
const int output26 = 26;
const int output27 = 27;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
void setup() {
Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(output26, OUTPUT);
pinMode(output27, OUTPUT);
// Set outputs to LOW
digitalWrite(output26, LOW);
digitalWrite(output27, LOW);
preferences.putString("ssid", ssid);
preferences.putString("password", password);
Serial.println("Network Credentials Saved using Preferences");
preferences.end();
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
currentTime = millis();
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// turns the GPIOs on and off
if (header.indexOf("GET /26/on") >= 0) {
Serial.println("GPIO 26 on");
output26State = "on";
digitalWrite(output26, HIGH);
} else if (header.indexOf("GET /26/off") >= 0) {
Serial.println("GPIO 26 off");
output26State = "off";
digitalWrite(output26, LOW);
} else if (header.indexOf("GET /27/on") >= 0) {
Serial.println("GPIO 27 on");
output27State = "on";
digitalWrite(output27, HIGH);
} else if (header.indexOf("GET /27/off") >= 0) {
Serial.println("GPIO 27 off");
output27State = "off";
digitalWrite(output27, LOW);
}
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #555555;}</style></head>");
// Web Page Heading
client.println("<body><h1>ESP32 Web Server</h1>");
// Display current state, and ON/OFF buttons for GPIO 26
client.println("<p>GPIO 26 - State " + output26State + "</p>");
// If the output26State is off, it displays the ON button
if (output26State=="off") {
client.println("<p><a href=\"/26/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/26/off\"><button class=\"button button2\">OFF</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 27
client.println("<p>GPIO 27 - State " + output27State + "</p>");
// If the output27State is off, it displays the ON button
if (output27State=="off") {
client.println("<p><a href=\"/27/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/27/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}

View File

@ -1,157 +0,0 @@
/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com
*********/
// include wifi password
#include "config.h"
// Load Wi-Fi library
#include <WiFi.h>
// Replace with your network credentials
const char* ssid = WIFI_SSID;
const char* password = WIFI_PASSWORD;
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the current output state
String output26State = "off";
String output27State = "off";
// Assign output variables to GPIO pins
const int output26 = 26;
const int output27 = 27;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
void setup() {
Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(output26, OUTPUT);
pinMode(output27, OUTPUT);
// Set outputs to LOW
digitalWrite(output26, LOW);
digitalWrite(output27, LOW);
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
currentTime = millis();
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// turns the GPIOs on and off
if (header.indexOf("GET /26/on") >= 0) {
Serial.println("GPIO 26 on");
output26State = "on";
digitalWrite(output26, HIGH);
} else if (header.indexOf("GET /26/off") >= 0) {
Serial.println("GPIO 26 off");
output26State = "off";
digitalWrite(output26, LOW);
} else if (header.indexOf("GET /27/on") >= 0) {
Serial.println("GPIO 27 on");
output27State = "on";
digitalWrite(output27, HIGH);
} else if (header.indexOf("GET /27/off") >= 0) {
Serial.println("GPIO 27 off");
output27State = "off";
digitalWrite(output27, LOW);
}
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #555555;}</style></head>");
// Web Page Heading
client.println("<body><h1>ESP32 Web Server</h1>");
// Display current state, and ON/OFF buttons for GPIO 26
client.println("<p>GPIO 26 - State " + output26State + "</p>");
// If the output26State is off, it displays the ON button
if (output26State=="off") {
client.println("<p><a href=\"/26/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/26/off\"><button class=\"button button2\">OFF</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 27
client.println("<p>GPIO 27 - State " + output27State + "</p>");
// If the output27State is off, it displays the ON button
if (output27State=="off") {
client.println("<p><a href=\"/27/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/27/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}

View File

@ -10,20 +10,17 @@
[env:esp32doit-devkit-v1]
platform = espressif32
;build_flags =
; -std=c++11
; -std=gnu++11
board = esp32doit-devkit-v1
framework = arduino
upload_speed = 921600
monitor_speed = 115200
monitor_filters = esp32_exception_decoder
build_type = debug
lib_deps =
me-no-dev/AsyncTCP@^1.1.1
me-no-dev/ESP Async WebServer@^1.2.3
contrem/arduino-timer@^3.0.1
kj7rrv/Telegraph@^1.0.0
jandelgado/JLed@^4.13.0
;adafruit/RTClib@^2.1.1
https://github.com/adafruit/RTClib.git ; >=2.1.2
jandelgado/JLed@^4.13.1
https://github.com/adafruit/RTClib.git
adafruit/Adafruit BusIO@^1.14.3
;jchristensen/DS3232RTC@^2.0.1
erropix/ESP32 AnalogWrite@^0.2

702
src/main.cpp Normal file
View File

@ -0,0 +1,702 @@
/*********
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 <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <SPIFFS.h>
#include <Preferences.h>
#include "morse.h"
#include <Adafruit_BusIO_Register.h> // for DS3231
#include <RTClib.h> // for DS3231
#include <string>
// download zip from https://github.com/me-no-dev/ESPAsyncWebServer and install.
#include <ESPAsyncWebServer.h>
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;
// Battery voltage monitor
const int voltage_pin = 33;
int voltage_pin_value = 0;
// RTC connections
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(
<!DOCTYPE HTML><html><head>
<link rel="icon" href="data:,">
<title>ESP Input Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript">
// Utility from https://webreflection.medium.com/using-the-input-datetime-local-9503e7efdce
Date.prototype.toDatetimeLocal = function toDatetimeLocal() {
var
date = this,
ten = function (i) {
return (i < 10 ? '0' : '') + i;
},
YYYY = date.getFullYear(),
MM = ten(date.getMonth() + 1),
DD = ten(date.getDate()),
HH = ten(date.getHours()),
II = ten(date.getMinutes()),
SS = ten(date.getSeconds())
;
return YYYY + '-' + MM + '-' + DD + 'T' +
HH + ':' + II + ':' + SS;
}
// Submit timestamps as unix seconds when form is submitted
var putDate = function(form) {
form.inputTimeUnix.value = Math.floor(Date.now() / 1000);// - new Date().getTimezoneOffset()*60;
form.inputStartTimeUnix.value = ((Date.parse(js_start_time_unix_entry.value))/1000);
//document.getElementById("js_start_time_unix").value = ((Date.parse(js_start_time_unix_entry.value))/1000);
}
// Fill in page values
window.onload = function() {
s = %inputStartTimeUnix%;
current_start = new Date(s * 1000);
document.getElementById('current-start').innerHTML = current_start.toLocaleString();
// Show the local time as a string
local_time_unix = new Date().toLocaleString();//toUTCString();
document.getElementById('local-time-unix').innerHTML = local_time_unix.toString();
// Fill in the start time field as local time
document.getElementById('js_start_time_unix_entry').value = current_start.toDatetimeLocal();
// 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>
<h1>Vulpes Radio Orienteering Controller</h1>
<p>Local time: <b><span id=local-time-unix></span></b>. If this is incorrect, your browser is not providing the correct time
(<a href="https://support.mozilla.org/en-US/questions/1297208">Firefox example</a>).</p>
<form action="/get" onsubmit="putDate(this);" accept-charset=utf-8>
<h2>General Settings</h2>
<p>Sending program:
<select name="inputSend" id="send-program">
<option value="0" >0 - Off</option>
<option value="1">1 - Continuous</option>
<option value="2">2 - Cycle</option>
</select><br>
Message:
<select name="inputMsg" id="message">
<option value="0">0 - Custom Message</option>
<option value="1">1 - MOE</option>
<option value="2">2 - MOI</option>
<option value="3">3 - MOS</option>
<option value="4">4 - MOH</option>
<option value="5">5 - MO5</option>
</select><br>
Custom message: <input type="text" name="inputCustomMsg" value = "%inputCustomMsg%"><br>
Speed: <input type="number" name="inputWPM" value = %inputWPM%> WPM
</p>
<h2>Cycle Settings</h2>
<p>Only applies when <em>Sending Program</em> is set to "2 - Cycle". You cannot set a cycle start date more than a month in advance.</p>
<p>Cycle start time <input type="datetime-local" id="js_start_time_unix_entry" /><br>
Current value: <b><span id=current-start></span></b>
<!-- JS converts the entered start time to a unix timestamp, and copies that value
to this hidden field so the user doesn't have to see it. -->
<input type="hidden" name="inputStartTimeUnix" id="js_start_time_unix" /></p>
<p>
Step length: <input type="number" name="inputStepLength" min=1000 step=1000 value = %inputStepLength%> milliseconds <br>
Cycle ID: <input type="number" name="inputCycleID" min=1 value = %inputCycleID%><br>
Number of transmitters: <input type="number" name="inputNtransmitters" min=1 value = %inputNtransmitters%><br>
</p>
<!-- This field is hidden so people don't change the submit time (it will be wrong).
The value is automatically filled in with JS. -->
<input type="hidden" name="inputTimeUnix" id="js_time_unix">
<!-- Extra fields just in case I need them -->
<input type="hidden" name="inputFloat" value = %inputFloat%>
<input type="submit" value="Submit"">
</form>
<iframe style="display:none" name="hidden-form" id="hidden-form"></iframe>
<br><hr>
<h2>Network Settings</h2>
<form onsubmit="return confirm('Are you sure you want to change the network and reboot?');" action="/get2" accept-charset=utf-8>
<p>Network Access:
<select name="inputNetwork" id="network">
<option value="0">Access Point</option>
<option value="1">Existing Wireless Network (advanced)</option>
</select><br>
Existing Wireless Network SSID: <input type="text" name="inputSSID" value = "%inputSSID%"><br>
Existing Wireless Network Password: <input type="password" name="inputPassword" value = "%inputPassword%"><br>
</p><p>
Access Point: Connect to wireless network "vulpes" and point your browser to URL <a href="http://192.168.0.1">http://192.168.0.1</a> (http, not http<b>s</b>)<br>
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).<br>
If an existing network can't be connected to, an access point will be set up.
</p>
<input type="submit" value="Submit and Reboot">
</form>
<iframe style="display:none" name="hidden-form02" id="hidden-form02"></iframe>
<script type="text/javascript">
</script>
</body></html>)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! Setting up access point 'vulpes'...");
// If you fail to connect, act as new access point
WiFi.disconnect(true);
WiFi.softAPConfig(local_ip, gateway, subnet);
WiFi.softAP(ssid_ap, password_ap);
// update the file so the webform is right
writeFile(SPIFFS, "/inputNetwork.txt", "0");
//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 <ESP_IP>/get?inputCustomMsg=<inputMessage>
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage;
// GET inputCustomMsg value on <ESP_IP>/get?inputCustomMsg=<inputMessage>
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 <ESP_IP>/get?inputSend=<inputMessage>
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 <ESP_IP>/get?inputWPM=<inputMessage>
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 <ESP_IP>/get?inputMsg=<inputMessage>
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 <ESP_IP>/get?inputStepLength=<inputMessage>
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 <ESP_IP>/get?inputCycleID=<inputMessage>
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 <ESP_IP>/get?inputNtransmitters=<inputMessage>
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 <ESP_IP>/get?inputTimeUnix=<inputMessage>
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 <ESP_IP>/get?inputFloat=<inputMessage>
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 <ESP_IP>/get?inputStartTimeUnix=<inputMessage>
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 <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);
}
// 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() {
// For testing, print the value returned from the voltage circuit.
// This is not voltage, and needs to be scaled to the battery. See KB1OIQ's solution.
voltage_pin_value = analogRead(voltage_pin);
Serial.println(voltage_pin_value);
// 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);
}
}

232
src/morse.cpp Executable file
View File

@ -0,0 +1,232 @@
// Morse Code sending library
#include <morse.h>
// MorseSender
int MorseSender::copyTimings(
morseTiming_t *rawOut,
morseBitmask_t definition)
{
int t = 0;
boolean foundSentinel = false;
for(morseBitmask_t mask = MORSE_BITMASK_HIGH_BIT;
mask > 0; mask = mask >> 1)
{
boolean isDah = (mask & definition) > 0;
if(!foundSentinel)
{
if (isDah) { foundSentinel = true; }
continue;
}
rawOut[2*t] = isDah ? DAH : DIT;
rawOut[2*t + 1] = DIT;
t++;
}
return t;
}
unsigned int MorseSender::fillTimings(char c)
{
int t = 0;
unsigned int start = 0;
if (c >= 'a' && c <= 'z')
{
t = copyTimings(timingBuffer, MORSE_LETTERS[c-'a']);
}
else if (c >= '0' && c <= '9')
{
int n = c - '0';
boolean ditsFirst = (n <= 5);
if (!ditsFirst)
{
n -= 5;
}
while(t < 5)
{
timingBuffer[2*t] = ((t < n) == ditsFirst) ? DIT : DAH;
timingBuffer[2*t + 1] = DIT;
t++;
}
}
else
{
int s = 0;
while(MORSE_PUNCT_ETC[s].c != END)
{
if(MORSE_PUNCT_ETC[s].c == c)
{
t = copyTimings(timingBuffer,
MORSE_PUNCT_ETC[s].timing);
break;
}
s++;
}
if (MORSE_PUNCT_ETC[s].c == END)
{
start = t = 1; // start on a space
}
}
timingBuffer[2*t - 1] = DAH;
timingBuffer[2*t] = END;
/*
Serial.print("Refilled timing buffer for '");
Serial.print(c);
Serial.print("': ");
int i = start;
while(timingBuffer[i] != END)
{
Serial.print((int)timingBuffer[i]);
Serial.print(", ");
i++;
}
Serial.println("END");
*/
return start;
}
// see note in header about pure-virtual-ness
void MorseSender::setOn() {};
void MorseSender::setOff() {};
// noop defaults
void MorseSender::setReady() {};
void MorseSender::setComplete() {};
MorseSender::MorseSender(unsigned int outputPin, float wpm) :
pin(outputPin)
{
setWPM(wpm);
}
void MorseSender::setup() { pinMode(pin, OUTPUT); }
void MorseSender::setWPM(float wpm)
{
setSpeed((morseTiming_t)(1000.0*60.0/(max(1.0f, wpm)*DITS_PER_WORD)));
}
void MorseSender::setSpeed(morseTiming_t duration)
{
DIT = max(duration, (morseTiming_t) 1);
DAH = 3*DIT;
}
void MorseSender::setMessage(const String newMessage)
{
message = newMessage;
// Force startSending() before continueSending().
messageIndex = message.length();
// If a different message was in progress, make sure it stops cleanly.
if (timingIndex % 2 == 0) {
setOff();
}
}
void MorseSender::sendBlocking()
{
//Serial.println("Sending blocking: ");
//Serial.println(message);
startSending();
while(continueSending());
}
void MorseSender::startSending()
{
messageIndex = 0;
if (message.length() == 0) { return; }
timingIndex = fillTimings(message[0]);
setReady();
if (timingIndex % 2 == 0) {
setOn();
//Serial.print("Starting with on, duration=");
} else {
//Serial.print("Starting with off, duration=");
}
lastChangedMillis = millis();
//Serial.println((int)timingBuffer[timingIndex]);
}
boolean MorseSender::continueSending()
{
if(messageIndex >= message.length()) { return false; }
unsigned long elapsedMillis = millis() - lastChangedMillis;
if (elapsedMillis < timingBuffer[timingIndex]) { return true; }
timingIndex++;
if (timingBuffer[timingIndex] == END)
{
messageIndex++;
if(messageIndex >= message.length()) {
setOff();
setComplete();
return false;
}
timingIndex = fillTimings(message[messageIndex]);
}
lastChangedMillis += elapsedMillis;
//Serial.print("Next is ");
if (timingIndex % 2 == 0) {
//Serial.print("(on) ");
setOn();
} else {
//Serial.print("(off) ");
setOff();
}
//Serial.println((int)timingBuffer[timingIndex]);
return true;
}
void *MorseSender::operator new(size_t size) { return malloc(size); }
void MorseSender::operator delete(void* ptr) { if (ptr) free(ptr); }
// SpeakerMorseSender
// void SpeakerMorseSender::setOn() { tone(pin, frequency); }
// void SpeakerMorseSender::setOff() {
// if (carrFrequency == CARRIER_FREQUENCY_NONE) {
// noTone(pin);
// } else {
// tone(pin, carrFrequency);
// }
// }
// void SpeakerMorseSender::setReady() { setOff(); }
// void SpeakerMorseSender::setComplete() { noTone(pin); }
// SpeakerMorseSender::SpeakerMorseSender(
// int outputPin,
// unsigned int toneFrequency,
// unsigned int carrierFrequency,
// float wpm)
// : MorseSender(outputPin, wpm),
// frequency(toneFrequency),
// carrFrequency(carrierFrequency) {};
// LEDMorseSender
void LEDMorseSender::setOn() { digitalWrite(pin, activeLow ? LOW : HIGH); }
void LEDMorseSender::setOff() { digitalWrite(pin, activeLow ? HIGH : LOW); }
LEDMorseSender::LEDMorseSender(int outputPin, bool activeLow, float wpm)
: MorseSender(outputPin, wpm), activeLow(activeLow) {};
LEDMorseSender::LEDMorseSender(int outputPin, float wpm)
: MorseSender(outputPin, wpm), activeLow(false) {};
// PWMMorseSender
void PWMMorseSender::setOn() { analogWrite(pin, brightness); }
void PWMMorseSender::setOff() { analogWrite(pin, 0); }
void PWMMorseSender::setBrightness(byte bright) {
brightness = bright;
}
PWMMorseSender::PWMMorseSender(
int outputPin,
float wpm,
byte bright)
: MorseSender(outputPin, wpm), brightness(bright) {};

276
src/morse.h Executable file
View File

@ -0,0 +1,276 @@
#pragma once
/**
* Generate and send Morse Code on an LED or a speaker. Allow sending
* in a non-blocking manner (by calling a 'continue sending' method
* every so often to turn an LED on/off, or to call tone/noTone appropriately).
*
* All input should be lowercase. Prosigns (SK, KN, etc) have special
* character values #defined.
*
* See also:
* Morse decoder (using binary tree):
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1289074596/15
* Generator (on playground):
* http://www.arduino.cc/playground/Code/Morse
*/
// for malloc and free, for the new/delete operators
#include <stdlib.h>
#include <analogWrite.h>
// Arduino language types
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define WPM_DEFAULT 12.0
// PARIS WPM measurement: 50; CODEX WPM measurement: 60 (Wikipedia:Morse_code)
#define DITS_PER_WORD 50
// Pass to SpeakerMorseSender as carrierFrequency to suppress the carrier.
#define CARRIER_FREQUENCY_NONE 0
// Bitmasks are 1 for dah and 0 for dit, in left-to-right order;
// the sequence proper begins after the first 1 (a sentinel).
// Credit for this scheme to Mark VandeWettering K6HX ( brainwagon.org ).
typedef unsigned int morseTiming_t;
typedef unsigned char morseBitmask_t; // see also MAX_TIMINGS
#define MORSE_BITMASK_HIGH_BIT B10000000
// sentinel
#define END 0
// the most timing numbers any unit will need; ex: k = on,off,on,off,on,end = 5
#define MAX_TIMINGS 15
// Punctuation and Prosigns
#define PROSIGN_SK 'S'
#define PROSIGN_KN 'K'
#define PROSIGN_BT 'B'
typedef struct {
char c;
morseBitmask_t timing;
} specialTiming;
const specialTiming MORSE_PUNCT_ETC[] = {
{'.', B1010101},
{'?', B1001100},
{'/', B110010},
{PROSIGN_SK, B1000101},
{PROSIGN_KN, B110110},
{PROSIGN_BT, B110001},
{END, B1},
};
// Morse Code (explicit declaration of letter timings)
const morseBitmask_t MORSE_LETTERS[26] = {
/* a */ B101,
/* b */ B11000,
/* c */ B11010,
/* d */ B1100,
/* e */ B10,
/* f */ B10010,
/* g */ B1110,
/* h */ B10000,
/* i */ B100,
/* j */ B10111,
/* k */ B1101,
/* l */ B10100,
/* m */ B111,
/* n */ B110,
/* o */ B1111,
/* p */ B10110,
/* q */ B11101,
/* r */ B1010,
/* s */ B1000,
/* t */ B11,
/* u */ B1001,
/* v */ B10001,
/* w */ B1011,
/* x */ B11001,
/* y */ B11011,
/* z */ B11100,
};
/**
* Define the logic of converting characters to on/off timing,
* and encapsulate the state of one sending-in-progress Morse message.
*
* Subclasses define setOn and setOff for (for example) LED and speaker output.
*/
class MorseSender {
protected:
const unsigned int pin;
// The setOn and setOff methods would be pure virtual,
// but that has compiler issues.
// See: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1167672075 .
/**
* Called to set put the output in 'on' state, during a dit or dah.
*/
virtual void setOn();
virtual void setOff();
/**
* Called before sending a message. Used for example to enable a
* carrier. (Noop in the base class.)
*/
virtual void setReady();
virtual void setComplete();
private:
morseTiming_t DIT, DAH;
String message;
// on,off,...,wait,0 list, millis
morseTiming_t timingBuffer[MAX_TIMINGS+1];
// index of the character currently being sent
unsigned int messageIndex;
// timing unit currently being sent
unsigned int timingIndex;
// when this timing unit was started
unsigned long lastChangedMillis;
/**
* Copy definition timings (on only) to raw timings (on/off).
* @return the number of 'on' timings copied
*/
int copyTimings(morseTiming_t *rawOut,
morseBitmask_t definition);
/**
* Fill a buffer with on,off,..,END timings (millis)
* @return the index at which to start within the new timing sequence
*/
unsigned int fillTimings(char c);
public:
/**
* Create a sender which will output to the given pin.
*/
MorseSender(unsigned int outputPin, float wpm=WPM_DEFAULT);
/**
* To be called during the Arduino setup(); set the pin as OUTPUT.
*/
void setup();
/**
* Set the words per minute (based on PARIS timing).
*/
void setWPM(float wpm);
/**
* Set the duration, in milliseconds, of a DIT.
*/
void setSpeed(morseTiming_t duration);
/**
* Set the message to be sent.
* This halts any sending in progress.
*/
void setMessage(const String newMessage);
/**
* Send the entirety of the current message before returning. See the "simple"
* example, which uses sendBlocking to send one message.
*/
void sendBlocking();
/**
* Prepare to send and begin sending the current message. After calling this,
* call continueSending repeatedly until it returns false to finish sending
* the message. See the "speeds" example, which calls startSending and
* continueSending on two different senders.
*/
void startSending();
/**
* Switch outputs on and off (and refill the internal timing buffer)
* as necessary to continue with the sending of the current message.
* This should be called every few milliseconds (at a significantly
* smaller interval than a DIT) to produce a legible fist.
*
* @see startSending, which must be called first
* @return false if sending is complete, otherwise true (keep sending)
*/
boolean continueSending();
void *operator new(size_t size);
void operator delete(void* ptr);
};
/**
* Adapt Morse sending to use the Arduino language tone() and noTone()
* functions, for use with a speaker.
*
* If a carrierFrequency is given, instead of calling noTone, call tone
* with a low frequency. This is useful ex. for maintaining radio links.
*/
class SpeakerMorseSender: public MorseSender {
private:
unsigned int frequency;
unsigned int carrFrequency;
protected:
virtual void setOn();
virtual void setOff();
virtual void setReady();
virtual void setComplete();
public:
// concert A = 440
// middle C = 261.626; higher octaves = 523.251, 1046.502
SpeakerMorseSender(
int outputPin,
unsigned int toneFrequency=1046,
unsigned int carrierFrequency=CARRIER_FREQUENCY_NONE,
float wpm=WPM_DEFAULT);
};
/**
* Sends Morse on a digital output pin.
*/
class LEDMorseSender: public MorseSender {
private:
bool activeLow;
protected:
virtual void setOn();
virtual void setOff();
public:
/**
* Creates a LED Morse code sender with the given GPIO pin. The optional
* boolean activeLow indicates LED is ON with digital LOW value.
* @param outputPin GPIO pin number
* @param activeLow set to true to indicate the LED ON with digital LOW value. default: false
* @param wpm words per minute, default: WPM_DEFAULT
*/
LEDMorseSender(int outputPin, bool activeLow = false, float wpm=WPM_DEFAULT);
/**
* Creates a LED Morse code sender with the given GPIO pin. This constructor is for backward compability.
* @param outputPin GPIO pin number
* @param wpm words per minute
*/
LEDMorseSender(int outputPin, float wpm);
};
/**
* Sends Morse on an analog output pin (using PWM). The brightness value is
* between 0 and 255 and is passed directly to analogWrite.
*/
class PWMMorseSender: public MorseSender {
private:
byte brightness;
protected:
virtual void setOn();
virtual void setOff();
public:
PWMMorseSender(int outputPin, float wpm=WPM_DEFAULT, byte brightness=255);
void setBrightness(byte brightness);
};

View File

@ -1,10 +0,0 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}

View File

@ -1,17 +0,0 @@
# JLed morse example
This examples demonstrates an efficient method to generate morse code on
an micro controller like the Arduino.
The morse example uses the morse alphabet encoded in a binary tree to
generate morse code using a JLed user defined brightness class. The text
to be morsed is transformed into morse code and then transformed into a
sequence of `1` and `0` which are written out to a GPIO controlling a LED or
a sound generator.
![morse example](../../doc/morse.jpg)
## Author
Jan Delgado

View File

@ -1,53 +0,0 @@
// Copyright (c) 2019 Jan Delgado <jdelgado[at]gmx.net>
// https://github.com/jandelgado/jled
#ifndef EXAMPLES_MORSE_BITSET_H_
#define EXAMPLES_MORSE_BITSET_H_
// a simple bit set with capacity of N bits, just enough for the morse demo
class Bitset {
private:
size_t n_;
uint8_t* bits_;
protected:
// returns num bytes needed to store n bits.
static constexpr size_t num_bytes(size_t n) {
return n > 0 ? ((n - 1) >> 3) + 1 : 0;
}
public:
Bitset() : Bitset(0) {}
Bitset(const Bitset& b) : Bitset() { *this = b; }
explicit Bitset(size_t n) : n_(n), bits_{new uint8_t[num_bytes(n)]} {
memset(bits_, 0, num_bytes(n_));
}
Bitset& operator=(const Bitset& b) {
if (&b == this) return *this;
const auto size_new = num_bytes(b.n_);
if (num_bytes(n_) != size_new) {
delete[] bits_;
bits_ = new uint8_t[size_new];
n_ = b.n_;
}
memcpy(bits_, b.bits_, size_new);
return *this;
}
virtual ~Bitset() {
delete[] bits_;
bits_ = nullptr;
}
void set(size_t i, bool val) {
if (val)
bits_[i >> 3] |= (1 << (i & 7));
else
bits_[i >> 3] &= ~(1 << (i & 7));
}
bool test(size_t i) const { return (bits_[i >> 3] & (1 << (i & 7))) != 0; }
size_t size() const { return n_; }
};
#endif // EXAMPLES_MORSE_BITSET_H_

View File

@ -1,121 +0,0 @@
// Copyright (c) 2019 Jan Delgado <jdelgado[at]gmx.net>
// https://github.com/jandelgado/jled
#include <Arduino.h>
#include <inttypes.h>
#include <stddef.h>
#include "bitset.h" // NOLINT
#ifndef EXAMPLES_MORSE_MORSE_H_
#define EXAMPLES_MORSE_MORSE_H_
// The Morse class converts a text sequence into a bit sequence representing
// a morse code sequence.
class Morse {
// pre-ordered tree of morse codes. Bit 1 = 'dah', 0 = 'dit'.
// Position in string corresponds to position in binary tree starting w/ 1
// see https://www.pocketmagic.net/morse-encoder/ for info on encoding
static constexpr auto LATIN =
"*ETIANMSURWDKGOHVF*L*PJBXCYZQ**54*3***2*******16*******7***8*90";
static constexpr auto DURATION_DIT = 1;
static constexpr auto DURATION_DAH = 3 * DURATION_DIT;
static constexpr auto DURATION_PAUSE_CHAR = DURATION_DAH;
static constexpr auto DURATION_PAUSE_WORD = 7 * DURATION_DIT;
protected:
char upper(char c) const { return c >= 'a' && c <= 'z' ? c - 32 : c; }
bool isspace(char c) const { return c == ' '; }
// returns position of char in morse tree. Count starts with 1, i.e.
// E=2, T=3, etc.
size_t treepos(char c) const {
auto i = 1u;
while (LATIN[i++] != c) {
}
return i;
}
// returns uint16_t with size of morse sequence (dit's and dah's) in MSB
// and the morse sequence in the LSB
uint16_t pos_to_morse_code(int code) const {
uint8_t res = 0;
uint8_t size = 0;
while (code > 1) {
size++;
res <<= 1;
res |= (code & 1);
code >>= 1;
}
return res | (size << 8);
}
template <typename F>
uint16_t iterate_sequence(const char* p, F f) const {
// call f(count,val) num times, incrementing count each time
// and returning num afterwards.
auto set = [](int num, int count, bool val, F f) -> int {
for (auto i = 0; i < num; i++) f(count + i, val);
return num;
};
auto bitcount = 0;
while (*p) {
const auto c = upper(*p++);
if (isspace(c)) { // space not part of alphabet, treat separately
bitcount += set(DURATION_PAUSE_WORD, bitcount, false, f);
continue;
}
const auto morse_code = pos_to_morse_code(treepos(upper(c)));
auto code = morse_code & 0xff; // dits (0) and dahs (1)
auto size = morse_code >> 8; // number of dits and dahs in code
while (size--) {
bitcount += set((code & 1) ? DURATION_DAH : DURATION_DIT,
bitcount, true, f);
// pause between symbols := 1 dit
if (size) {
bitcount += set(DURATION_DIT, bitcount, false, f);
}
code >>= 1;
}
if (*p && !isspace(*p)) {
bitcount += set(DURATION_PAUSE_CHAR, bitcount, false, f);
}
}
return bitcount;
}
public:
// returns ith bit of morse sequence
bool test(uint16_t i) const { return bits_->test(i); }
// length of complete morse sequence in in bits
size_t size() const { return bits_->size(); }
Morse() : bits_(new Bitset(0)) {}
explicit Morse(const char* s) {
const auto length = iterate_sequence(s, [](int, bool) -> void {});
auto bits = new Bitset(length);
iterate_sequence(s, [bits](int i, bool v) -> void { bits->set(i, v); });
bits_ = bits;
}
~Morse() { delete bits_; }
// make sure that the following, currently not needed, methods are not used
Morse(const Morse&m) {*this = m;}
Morse& operator=(const Morse&m) {
delete bits_;
bits_ = new Bitset(*m.bits_);
return *this;
}
private:
// stores morse bit sequence
const Bitset* bits_ = nullptr;
};
#endif // EXAMPLES_MORSE_MORSE_H_

View File

@ -1,634 +0,0 @@
/*********
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 <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <SPIFFS.h>
#include <Preferences.h>
#include <arduino-timer.h>
// #include <Telegraph.h>
//#include <morse.h> //arduino morse
//#include <Morse.h> //etherkit morse
#include <jled.h> // jled
#include "jled/morse.h" //jled
#include <Adafruit_BusIO_Register.h> // for DS3231
#include <RTClib.h> // for DS3231
//#include <DS3232RTC.h> //for DS3231
//#include <sstream>
// download zip from https://github.com/me-no-dev/ESPAsyncWebServer and install.
#include <ESPAsyncWebServer.h>
AsyncWebServer server(80);
RTC_DS3231 rtc; // set up RTC
// Read from config.h
const char* ssid = WIFI_SSID;
const char* password = WIFI_PASSWORD;
const char* PARAM_STRING = "inputString";
const char* PARAM_SEND = "inputSend";
const char* PARAM_WPM = "inputWPM";
const char* PARAM_MSG = "inputMsg";
const char* PARAM_FLOAT = "inputFloat";
const char* PARAM_TIME = "inputTimeUnix";
// Global variables
String yourInputString;
int yourInputSend;
int yourInputWPM;
int yourInputMsg;
int yourInputMsg_old; // to save previous state and check changes
float yourInputFloat;
uint32_t yourInputTime; //to keep time
// HTML web page to handle 3 input fields (inputString, inputSend, inputFloat)
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html><head>
<title>ESP Input Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
var putDate = function(form) {
form.inputTimeUnix.value = Math.floor(Date.now() / 1000);
};
</script></head><body>
<form action="/get" target="hidden-form" onsubmit="putDate(this);">
inputString (current value %inputString%): <input type="text" name="inputString" value=%inputString%><br>
Sending program (cycle doesn't work yet) (current value: <b>%inputSend%</b>):
<select name="inputSend" id="send-program">
<option value="0">0 -Off</option>
<option value="1">1 - Continuous</option>
<option value="2">2 - Cycle</option>
</select><br>
Message (current value <b>%inputMsg%</b>):
<select name="inputMsg" id="message">
<option value="0">0 - TEST TEST TEST DE W1CDN</option>
<option value="1">1 - MOE</option>
<option value="2">2 - MOI</option>
<option value="3">3 - MOS</option>
<option value="4">4 - MOH</option>
<option value="5">5 - MO5</option>
</select><br>
WPM (current value %inputWPM%): <input type="number " name="inputWPM" value = %inputWPM%> (doesn't work yet)<br>
Current time (UTC): %inputTimeUnix%
<input type="hidden" name="inputTimeUnix" id="js_time_unix"><br>
inputFloat (current value %inputFloat%): <input type="number " name="inputFloat" value = %inputFloat%><br>
<input type="submit" value="Submit"">
</form>
<iframe style="display:none" name="hidden-form"></iframe>
</body></html>)rawliteral";
// Auxiliary variables to store the current output state
//String output26State = "off";
//String output27State = "off";
// Assign output variables to GPIO pins
const int keyer = 32; //LED_BUILTIN for on-board (dev);//26 for LED; //32 for transmitter keyer
const int blinker = LED_BUILTIN;
// Timers
//auto timer = timer_create_default();
Timer<1> timer;
auto time_until_start = timer_create_default();
// Example from https://github.com/contrem/arduino-timer#examples
bool toggle_led(void *) {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
return true; // keep timer active? true
}
// Toggle GPIO pin (LED or relay)
// bool toggle_gpio_26(void *) {
// if(output26State == "off"){
// output26State = "on";
// digitalWrite(output26, HIGH);
// } else {
// output26State = "off";
// digitalWrite(output26, LOW);
// }
// return true; // keep timer active? true
// }
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 == "inputString"){
return readFile(SPIFFS, "/inputString.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 == "inputFloat"){
return readFile(SPIFFS, "/inputFloat.txt");
} else if(var == "inputTimeUnix"){
return rtc.now().timestamp();
}
return String();
}
// // vvvvv Modify some functions from KB1OIQ's controller.
// // This section hasn't been tested on the hardware.
// //int dit_len = 60 ; //milliseconds; https://morsecode.world/international/timing.html
// //================================================================================
// // stop_26(): set GPIO 26 to LOW. Used for dot(), dash().
// //================================================================================
// bool stop_26(void *){
// output26State = "off";
// digitalWrite(output26, LOW);
// return false; // keep timer active? true
// }
// //================================================================================
// // dit(): transmit a single dit
// //================================================================================
// void dit(int dit_len = 1000) {
// output26State = "on";
// digitalWrite(output26, HIGH);
// timer.in(dit_len, stop_26);
// }
// //================================================================================
// // dah(): transmit a single dah
// //================================================================================
// void dah(int dit_len = 1000) {
// output26State = "on";
// digitalWrite(output26, HIGH);
// timer.in(dit_len * 3, stop_26);
// }
// //================================================================================
// // char_space()): transmit a character space
// //================================================================================
// // A function that does nothing except (hopefully) block the timer.
// bool empty(void *) {
// return false;
// }
// void char_space(int dit_len = 1000) {
// timer.in(dit_len, empty);
// }
// void k(){
// Serial.println("K");
// dah();
// char_space();
// dit();
// char_space();
// dah();
// }
// // ^^^^
// //telegraph
// //Telegraph telegraph(LED_BUILTIN, 10, HIGH);
// Telegraph telegraph26(output26, 10, HIGH);
//arduinomorse
//LEDMorseSender sender(LED_BUILTIN);
//Etherkit Morse
//Morse morse(LED_BUILTIN, 15);
//jled from https://github.com/jandelgado/jled/blob/master/examples/morse/morse_effect.h
class MorseEffect : public jled::BrightnessEvaluator {
Morse morse_;
// duration of a single 'dit' in ms
const uint16_t speed_;
public:
explicit MorseEffect(const char* message, uint16_t speed = 200)
: morse_(message), speed_(speed) {}
uint8_t Eval(uint32_t t) const override {
const auto pos = t / speed_;
if (pos >= morse_.size()) return 0;
return morse_.test(pos) ? 255 : 0;
}
uint16_t Period() const override { return (morse_.size() + 1) * speed_; }
};
// Speed is milliseconds per dit, which is 1000 * (60 / (50 * WPM))
// 60 is 20 wpm, 120 is 10 wpm, 90 is 15 wpm, etc.
// https://morsecode.world/international/timing.html
float wpm = 10;
float ms_per_dit = 1000 * (60 / (50 * wpm));
int word_space_ms = ms_per_dit * 7;
// Hardcoding messages and WPM for now, will come back and make it more flexible.
MorseEffect morseEffectTEST("TEST TEST TEST DE W1CDN", ms_per_dit);
MorseEffect morseEffectMOE("MOE", ms_per_dit);
MorseEffect morseEffectMOI("MOI", ms_per_dit);
MorseEffect morseEffectMOS("MOS", ms_per_dit);
MorseEffect morseEffectMOH("MOH", ms_per_dit);
MorseEffect morseEffectMO5("MO5", ms_per_dit);
// CW for keyer
auto morseTEST =
JLed(keyer).UserFunc(&morseEffectTEST).DelayAfter(word_space_ms).Forever();
auto morseMOE =
JLed(keyer).UserFunc(&morseEffectMOE).DelayAfter(word_space_ms).Forever();
auto morseMOI =
JLed(keyer).UserFunc(&morseEffectMOI).DelayAfter(word_space_ms).Forever();
auto morseMOS =
JLed(keyer).UserFunc(&morseEffectMOS).DelayAfter(word_space_ms).Forever();
auto morseMOH =
JLed(keyer).UserFunc(&morseEffectMOH).DelayAfter(word_space_ms).Forever();
auto morseMO5 =
JLed(keyer).UserFunc(&morseEffectMO5).DelayAfter(word_space_ms).Forever();
auto morseToSend = morseTEST; // set this up to overwrite later
// CW for blinker
auto morseTEST_blink =
JLed(blinker).UserFunc(&morseEffectTEST).DelayAfter(word_space_ms).Forever();
auto morseMOE_blink =
JLed(blinker).UserFunc(&morseEffectMOE).DelayAfter(word_space_ms).Forever();
auto morseMOI_blink =
JLed(blinker).UserFunc(&morseEffectMOI).DelayAfter(word_space_ms).Forever();
auto morseMOS_blink =
JLed(blinker).UserFunc(&morseEffectMOS).DelayAfter(word_space_ms).Forever();
auto morseMOH_blink =
JLed(blinker).UserFunc(&morseEffectMOH).DelayAfter(word_space_ms).Forever();
auto morseMO5_blink =
JLed(blinker).UserFunc(&morseEffectMO5).DelayAfter(word_space_ms).Forever();
auto morseToSend_blink = morseTEST_blink; // set this up to overwrite later
// format and print a time_t value
// void printTime(time_t t)
// {
// char buf[25];
// char m[4]; // temporary storage for month string (DateStrings.cpp uses shared buffer)
// strcpy(m, monthShortStr(month(t)));
// sprintf(buf, "%.2d:%.2d:%.2d %s %.2d %s %d",
// hour(t), minute(t), second(t), dayShortStr(weekday(t)), day(t), m, year(t));
// Serial.println(buf);
// }
//================================================================================
// setup(): stuff that only gets done once, after power up (KB1OIQ's description)
//================================================================================
void setup() {
Serial.begin(115200);
// https://github.com/JChristensen/DS3232RTC/blob/master/examples/TimeRTC/TimeRTC.ino
// rtc.begin();
// setSyncProvider(rtc.get); // the function to get the time from the RTC
// if(timeStatus() != timeSet)
// Serial.println("Unable to sync with the RTC");
// else
// Serial.println("RTC has set the system time");
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));
}
// Timer example, blink main LED
pinMode(LED_BUILTIN, OUTPUT); // set LED pin to OUTPUT
// call the toggle_led function every 10000 millis (10 second)
//timer.every(10000, toggle_led);
// call the toggle_gpio_26 function
//timer.every(1000, toggle_gpio_26);
// 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);
//#ifdef ESP32
if(!SPIFFS.begin(true)){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
//#else
if(!SPIFFS.begin()){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
//#endif
// Make sure files exist, maybe with defaults here
// if(SPIFFS.exists("/inputString.txt") == 0){
// writeFile(SPIFFS, "/inputString.txt", "CQ");
// }
// if(SPIFFS.exists("/inputSend.txt") == 0){
// writeFile(SPIFFS, "/inputSend.txt", "0");
// }
// if(SPIFFS.exists("/inputWPM.txt") == 0){
// writeFile(SPIFFS, "/inputWPM.txt", "10");
// }
// if(SPIFFS.exists("/inputMsg.txt") == 0){
// writeFile(SPIFFS, "/inputMsg.txt", "0");
// }
// if(SPIFFS.exists("/inputFloat.txt") == 0){
// writeFile(SPIFFS, "/inputFloat.txt", "1.1");
// }
// Read in existing data
yourInputString = readFile(SPIFFS, "/inputString.txt");
yourInputSend = readFile(SPIFFS, "/inputSend.txt").toInt();
yourInputWPM = readFile(SPIFFS, "/inputWPM.txt").toInt();
yourInputMsg = readFile(SPIFFS, "/inputMsg.txt").toInt();
yourInputFloat = readFile(SPIFFS, "/inputFloat.txt").toFloat();
// On restart, keep doing what you were doing before
yourInputMsg_old = yourInputMsg;
if(yourInputMsg == 0){
morseToSend = morseTEST;
morseToSend_blink = morseTEST_blink;
} else if(yourInputMsg == 1){
morseToSend = morseMOE;
morseToSend_blink = morseMOE_blink;
} else if(yourInputMsg == 2){
morseToSend = morseMOI;
morseToSend_blink = morseMOI_blink;
} else if(yourInputMsg == 3){
morseToSend = morseMOS;
morseToSend_blink = morseMOS_blink;
} else if(yourInputMsg == 4){
morseToSend = morseMOH;
morseToSend_blink = morseMOH_blink;
} else if(yourInputMsg == 5){
morseToSend = morseMO5;
morseToSend_blink = morseMO5_blink;
}
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Failed!");
return;
}
Serial.println();
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Send web page with input fields to client
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html, processor);
});
// Send a GET request to <ESP_IP>/get?inputString=<inputMessage>
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage;
// GET inputString value on <ESP_IP>/get?inputString=<inputMessage>
if (request->hasParam(PARAM_STRING)) {
inputMessage = request->getParam(PARAM_STRING)->value();
writeFile(SPIFFS, "/inputString.txt", inputMessage.c_str());
yourInputString = inputMessage;
}
// GET inputSend value on <ESP_IP>/get?inputSend=<inputMessage>
if (request->hasParam(PARAM_SEND)) {
inputMessage = request->getParam(PARAM_SEND)->value();
writeFile(SPIFFS, "/inputSend.txt", inputMessage.c_str());
yourInputSend = inputMessage.toInt();
}
// GET inputWPM value on <ESP_IP>/get?inputWPM=<inputMessage>
if (request->hasParam(PARAM_WPM)) {
inputMessage = request->getParam(PARAM_WPM)->value();
writeFile(SPIFFS, "/inputWPM.txt", inputMessage.c_str());
yourInputWPM = inputMessage.toInt();
}
// GET inputMsg value on <ESP_IP>/get?inputMsg=<inputMessage>
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();
}
// GET inputTimeUnix value on <ESP_IP>/get?inputTimeUnix=<inputMessage>
if (request->hasParam(PARAM_TIME)) {
inputMessage = request->getParam(PARAM_TIME)->value();
Serial.println(inputMessage);
//https://stackoverflow.com/a/22733127/2152245
yourInputTime = atol(inputMessage.c_str());
Serial.println(yourInputTime);
// update the RTC time
rtc.adjust(DateTime(yourInputTime));
;
DateTime now = rtc.now();
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();
}
// GET inputFloat value on <ESP_IP>/get?inputFloat=<inputMessage>
if (request->hasParam(PARAM_FLOAT)) {
inputMessage = request->getParam(PARAM_FLOAT)->value();
writeFile(SPIFFS, "/inputFloat.txt", inputMessage.c_str());
yourInputFloat = inputMessage.toFloat();
}
// else {
// inputMessage = "No message sent";
// }
request->send(200, "text/plain", inputMessage);
});
server.onNotFound(notFound);
server.begin();
//telegraph
//telegraph.send("CQ CQ CQ");
//telegraph26.send("CQ CQ CQ DE W1CDN K");
// arduinomorse
// sender.setup();
// sender.setMessage(String("73 de kb3jcy "));
// sender.startSending();
}
void loop() {
// Timers
time_until_start.tick();
timer.tick();
// DateTime now = rtc.now();
// 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();
//arduinomorse
//sender.continueSending();
// See which message we are sending
// Only do this when the message has been updated.
if(yourInputMsg != yourInputMsg_old){
//morseToSend.Stop(JLed::eStopMode::FULL_OFF).Update();
if(yourInputMsg == 0){
morseToSend = morseTEST;
morseToSend_blink = morseTEST_blink;
} else if(yourInputMsg == 1){
morseToSend = morseMOE;
morseToSend_blink = morseMOE_blink;
} else if(yourInputMsg == 2){
morseToSend = morseMOI;
morseToSend_blink = morseMOI_blink;
} else if(yourInputMsg == 3){
morseToSend = morseMOS;
morseToSend_blink = morseMOS_blink;
} else if(yourInputMsg == 4){
morseToSend = morseMOH;
morseToSend_blink = morseMOH_blink;
} else if(yourInputMsg == 5){
morseToSend = morseMO5;
morseToSend_blink = morseMO5_blink;
}
// Keeps the key from locking up
yourInputMsg_old = yourInputMsg;
}
// if you want to send continuous code, and it's not sending, then start it up
if((yourInputSend == 1) & (morseToSend.IsRunning() == false)){
//jled
morseToSend.Reset().Update();
morseToSend_blink.Reset().Update();
//morse.send("CQ CQ CQ DE W1CDN K"); //etherkit morse
//telegraph26.send("CQ CQ CQ DE W1CDN K"); //telegraph
// if you want to send continuous code, and it is sending, keep sending
} else if((yourInputSend == 1) & (morseToSend.IsRunning() == true)){
morseToSend.Update();
morseToSend_blink.Update();
// if you want to send cycle code and it is sending, keep sending
} else if((yourInputSend == 2) & (morseToSend.IsRunning() == true)){
morseToSend.Update();
morseToSend_blink.Update();
// if you want to send cycle code and it's not sending, then start it up
} else if((yourInputSend == 2) & (morseToSend.IsRunning() == true)){
morseToSend.Reset().Update();
morseToSend_blink.Reset().Update();
// if you don't want to send code
} else {
// stop sending and make sure the pin is off
morseToSend.Stop(JLed::eStopMode::FULL_OFF).Update();
morseToSend_blink.Stop(JLed::eStopMode::FULL_OFF).Update();
}
//morseToSend.Update();
// Blink LED according to seconds entered
// if (yourInputInt > 0) {
// Serial.println("GPIO 26 on");
// output26State = "on";
// digitalWrite(output26, HIGH);
// delay(yourInputInt * 1000);
// Serial.println(yourInputInt);
// Serial.println("GPIO 26 off");
// output26State = "off";
// digitalWrite(output26, LOW);
// delay(yourInputInt * 1000);
// } else {
// output26State = "off";
// }
}