Bit of a beginner with html and forms here.
This code only seems to take input value 1 and NOT input value 2!
For example when the ''submit'' button on the web based input screen in clicked I only receive a message that input 1 value has been set!
In addition I'm displaying the web based input onto an LCD and the same, the data does not seem to be passed on.
Any help on what I'm not doing right will be greatly appreciated indeed.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <LiquidCrystal_I2C.h>
// WiFi credentials
const char* ssid = "xxxx";
const char* password = "xxxxxx";
// Web server port
const int serverPort = 80;
// Web server object
ESP8266WebServer server(serverPort);
// Variables to store input numbers
int input1 = 0;
int input2 = 0;
// Variables to store output pins
int output1 = 14;
int output2 = 15;
// Variables to store input pins
int inputPin1 = 13;
int inputPin2 = 15;
// Timer variables
unsigned long timer1 = 0;
unsigned long timer2 = 0;
int buttonState = 0;
LiquidCrystal_I2C lcd(0x26, 20, 4); // LCD address is 0x26
void setup() {
lcd.init();
// Turn on the backlight.
lcd.backlight();
// Move the cursor 5 characters to the right and
// zero characters down (line 1).
lcd.setCursor(0, 0);
// Print HELLO to the screen, starting at 5,0.
lcd.print("Teddy Fox Brewery");
// Move the cursor to the next line and print
// WORLD.
lcd.setCursor(0, 1);
lcd.print("Beer Filler 5000");
// initialize the pushbutton pin as an input
pinMode(input1, INPUT);
// initialize the LED pin as an output
pinMode(output1, OUTPUT);
// Initialize serial monitor
Serial.begin(9600);
// Connect to WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print WiFi network information to Serial and LCD
Serial.println("");
Serial.println("WiFi connected");
lcd.setCursor(0, 2);
lcd.print("WiFi connected");
Serial.print("IP address: ");
lcd.setCursor(0, 3);
lcd.print("IP:");
lcd.setCursor(4, 3);
lcd.print(WiFi.localIP());
Serial.println(WiFi.localIP());
// Initialize web server
server.on("/", handleRoot);
server.on("/input1", handleInput1);
server.on("/input2", handleInput2);
server.begin();
Serial.println("Web server started");
}
// Handle root page request
void handleRoot() {
// Send HTML code for input page
server.send(200, "text/html", "<html><body><h1>Input Page</h1><form action='/input1'><label for='input1'>Input 1:</label><input type='number' id='input1' name='input1'><br><label for='input2'>Input 2:</label><input type='number' id='input2' name='input2'><br><input type='submit' value='Submit'></form></body></html>");
}
// Handle input 1 page request
void handleInput1() {
// Get input 1 value from URL parameter
input1 = server.arg("input1").toInt();
// Send HTML code for success page
server.send(200, "text/html", "<html><body><h1>Success!</h1><p>Input 1 value set to " + String(input1) + " milliseconds.</p></body></html>");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Purge:");
lcd.setCursor(7, 0);
lcd.print(input1);
lcd.print(" m/Sec");
}
// Handle input 2 page request
void handleInput2() {
// Get input 2 value from URL parameter
input2 = server.arg("input2").toInt();
// Send HTML code for success page
server.send(200, "text/html", "<html><body><h1>Success!</h1><p>Input 2 value set to " + String(input2) + " milliseconds.</p></body></html>");
lcd.setCursor(0, 1);
lcd.print("Beer :");
lcd.setCursor(7, 1);
lcd.print(input2);
lcd.print(" m/Sec");
}
void loop() {
// Handle web server requests
server.handleClient();
// Check if input 1 button is pressed
if (digitalRead(inputPin1) == HIGH) {
Serial.println("button 1 pressed");
Serial.println(input1);
//Start timer for output 1
timer1 = millis();
//Turn on output 1
digitalWrite(output1, HIGH);
}
// Check if input 2 button is pressed
if (digitalRead(inputPin2) == HIGH) {
//Start timer for output 2
timer2 = millis();
// Turn on output 2
digitalWrite(output2, HIGH);
}
// Check if timer for output 1 has reached input 1 value
if (millis() - timer1 >= input1) {
// Turn off output 1
digitalWrite(output1, LOW);
}
// Check if timer for output 2 has reached input 2 value
if (millis() - timer2 >= input2) {
// Turn off output 2
digitalWrite(output2, LOW);
}
}