Hey All,
I've been working on learning how to get this new WiFi enabled chip to do some really cool stuff, and had an awesome time working through the beginners guide by Pieter P(thank you!). I worked through most of his examples with little issues.
After getting inspired to make some somewhat simple tweaks to the code which used a webform button to turn on and off the light, I've hit a wall.
My goal is to, instead of simply toggling on and off the light when I click the button, I want to add an input field to enter a number which changes a "Blink Rate". Further along for OTA there is a simple blink example program that sparked the idea. My code is largely a compilation of chunks of tutorial code, with my modifications and some reorganization.
Where I'm getting stuck is in retrieving the value from the input field upon submitting the form. I've done a good bit of research and cant seem to find a simple example of how to do this, though it doesn't seem like it should be too hard. Admittedly, I'm not super familiar with the HTML GET, POST, REQUEST functions at all, so this could be a large part of the problem.
Anyway, here is my code atm:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266WebServer.h>
#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endif
const char* ssid = "George";
const char* password = "8027340280";
const byte led = 2;
ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80
void setup() {
Serial.begin(115200);
Serial.println("Booting");
startWiFi();
startOTA();
startServer();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
pinMode(led, OUTPUT);
digitalWrite(led, 1);
}
unsigned long previousTime = millis();
const unsigned long interval = 500;
void loop() {
ArduinoOTA.handle();
server.handleClient();
unsigned long diff = millis() - previousTime;
if(diff > interval) {
digitalWrite(led, !digitalRead(led)); // Change the state of the LED
previousTime += diff;
}
}
void handleRoot() { // When URI / is requested, send a web page with a text box for input of blink rate, and a submit button.
server.send(200, "text/html", "<form action=\"/LED\" method=\"POST\">Update LED Blink Rate(ms): <input type=\"text\" name=\"blinkRate\">
<input type=\"submit\" value=\"Update LED\"></form>");
}
void handleLED() { // If a POST request is made to URI /LED
interval = ???? // Read the input from "blinkRate" and update the interval variable
}
void startServer(){
server.on("/", HTTP_GET, handleRoot); // Call the 'handleRoot' function when a client requests URI "/"
server.on("/LED", HTTP_POST, handleLED); // Call the 'handleLED' function when a POST request is made to URI "/LED"
server.onNotFound([]() {
server.send(404, "text/plain", "404: Not Found");
});
server.begin(); // Actually start the server
Serial.println("HTTP server started");
}
void startWiFi(){
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
}
void startOTA(){
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_SPIFFS
type = "filesystem";
}
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
}
I'm pretty confident all I need is a hand with the handleLED() function. I'd like to know the best way to go about doing this as either a GET or a POST, and which is better for this type of setting.
Thank you in advance, you've helped me out tremendously in the past!
Bennett