I am rather new to the Arduino Language and really new to the ESP8266.
Goal:
Make a program that can change the frequency and duty cycle of 3 pins using a web address.
For example if I want to change the frequency to 5 and the duty cycles to 25, 50, and 75 respectfully I would enter the web address below:
http://192.168.xx.xxx/?freqVal1=5//?dut ... yCyc3=75//
The first time I enter the above everything works fine. But if I try to set any of the duty cycles to 0 nothing happens and the led's still blink. If I change the frequency to 0 some of the led's turn off sometimes and others are lit.
I am also having some trouble with the wifi having a good connection. If I try to change the values to quickly I get an invalid request.
I know that my code is probably not the best I modified the Wifi Webserver example to try to do what I am wanting. Thank you in advance for any help!
#include <ESP8266WiFi.h>
#define ESP8266_LED 5
#define ESP8266_0 13
#define ESP8266_4 4
const char* ssid = "";
const char* password = "";
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
pinMode(ESP8266_LED, OUTPUT);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println();
Serial.println();
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
String FreqValue;
String FreqValueToSet;
String DutyCycleString;
String DutyCycleToSet;
float FreqValueNew = 1;
char floatbuff[32];
String DutyCycleEntry;
float DutyCycValue;
float DutyCycValueToSet = 1023 ;
char floatbuffDtyCyc[32];
String DutyCycleString2;
String DutyCycleToSet2;
String DutyCycleEntry2;
float DutyCycValue2;
float DutyCycValueToSet2 = 1023;
char floatbuffDtyCyc2[32];
String DutyCycleString3;
String DutyCycleToSet3;
String DutyCycleEntry3;
float DutyCycValue3;
float DutyCycValueToSet3 = 1023;
char floatbuffDtyCyc3[32];
if (req.indexOf("/?freqVal1") != -1){
FreqValue = req.substring(req.indexOf("=") + 1,req.indexOf("//"));
Serial.println("The Frequency Value is:");
Serial.print(FreqValue);
Serial.println();
Serial.println();
//Gets the new frequency number the user entered
FreqValueToSet = FreqValue.substring(0,FreqValue.indexOf("/"));
Serial.println("The Frequency Value is:");
Serial.print(FreqValueToSet);
Serial.println();
Serial.println();
FreqValueToSet.toCharArray(floatbuff, sizeof(floatbuff));
FreqValueNew = atoi(floatbuff);
Serial.println("Frequency is set to: ");
Serial.print(FreqValueNew);
//Set the Frequency
//analogWriteFreq(FreqValueNew);
}
if (req.indexOf("/?dutyCyc1") != -1){
Serial.println();
Serial.println();
Serial.println("Start Duty Cycle 1");
DutyCycleEntry = req.substring(req.indexOf("?dutyCyc1"));
Serial.println("Duty Cycle 1 String:");
Serial.println(DutyCycleEntry );
//Gets the new Duty cycle number the user entered
DutyCycleString = DutyCycleEntry.substring(DutyCycleEntry.indexOf("=") + 1,DutyCycleEntry.indexOf("//"));
Serial.println("Broken Down Duty Cycle 1 String:");
Serial.println(DutyCycleString);
Serial.println();
Serial.println();
DutyCycleToSet = DutyCycleString.substring(DutyCycleString.indexOf("=") + 1);
Serial.println("User's Entry for Duty Cycle 1 is:");
Serial.println(DutyCycleToSet);
Serial.println();
Serial.println();
DutyCycleToSet.toCharArray(floatbuffDtyCyc, sizeof(floatbuffDtyCyc));
DutyCycValue = atoi(floatbuffDtyCyc);
DutyCycValueToSet = 1023 * (DutyCycValue/100);
Serial.println("Percent Duty Cylce: ");
Serial.println(DutyCycValue);
Serial.println();
Serial.println();
Serial.println("Frequency is set to: ");
Serial.println(DutyCycValueToSet);
Serial.println();
Serial.println();
Serial.println();
Serial.println();
}
if (req.indexOf("/?dutyCyc2") != -1){
Serial.println("Start Duty Cycle 2");
DutyCycleEntry2 = req.substring(req.indexOf("?dutyCyc2"));
Serial.println("Duty Cycle 2 String:");
Serial.println(DutyCycleEntry2);
//Gets the new Duty cycle number the user entered
DutyCycleString2 = DutyCycleEntry2.substring(DutyCycleEntry2.indexOf("=") + 1,DutyCycleEntry2.indexOf("//"));
Serial.println("Broken down Duty Cycle 2 String:");
Serial.println(DutyCycleString2);
Serial.println();
Serial.println();
DutyCycleToSet2 = DutyCycleString2.substring(DutyCycleString2.indexOf("=") + 1);
Serial.println("User's Entry for Duty Cycle 2 is:");
Serial.println(DutyCycleToSet2);
Serial.println();
Serial.println();
DutyCycleToSet2.toCharArray(floatbuffDtyCyc2, sizeof(floatbuffDtyCyc2));
DutyCycValue2 = atoi(floatbuffDtyCyc2);
DutyCycValueToSet2 = 1023 * (DutyCycValue2/100);
Serial.println("Percent Duty Cylce: ");
Serial.print(DutyCycValue2);
Serial.println();
Serial.println();
Serial.println("Frequency is set to: ");
Serial.print(DutyCycValueToSet2);
Serial.println();
Serial.println();
Serial.println();
Serial.println();
}
if (req.indexOf("/?dutyCyc3") != -1){
Serial.println("Start Duty Cycle 3");
DutyCycleEntry3 = req.substring(req.indexOf("?dutyCyc3"));
Serial.println("Duty Cycle 3 String:");
Serial.println(DutyCycleEntry3 );
//Gets the new Duty cycle number the user entered
DutyCycleString3 = DutyCycleEntry3.substring(DutyCycleEntry3.indexOf("=") + 1,DutyCycleEntry3.indexOf("//"));
Serial.println("Broken Down Duty Cycle 3 String:");
Serial.println(DutyCycleString3);
Serial.println();
Serial.println();
DutyCycleToSet3 = DutyCycleString3.substring(DutyCycleString3.indexOf("=") + 1);
Serial.println("User's Entry for Duty Cycle 3 is:");
Serial.println(DutyCycleToSet3);
Serial.println();
Serial.println();
DutyCycleToSet3.toCharArray(floatbuffDtyCyc3, sizeof(floatbuffDtyCyc3));
DutyCycValue3 = atoi(floatbuffDtyCyc3);
DutyCycValueToSet3 = 1023 * (DutyCycValue3/100);
Serial.println("Percent Duty Cylce: ");
Serial.print(DutyCycValue3);
Serial.println();
Serial.println();
Serial.println("Frequency is set to: ");
Serial.print(DutyCycValueToSet3);
Serial.println();
Serial.println();
Serial.println();
Serial.println();
}
else{
Serial.println("invalid request");
client.stop();
return;
}
//Set the Duty Cycle
if (DutyCycValueToSet >0){
analogWrite(ESP8266_LED,DutyCycValueToSet);
Serial.println("Set for 1st duty cycle");
}
if (DutyCycValueToSet2 >0){
analogWrite(ESP8266_0,DutyCycValueToSet2);
Serial.println("Set for 2nd duty cycle");
}
if(DutyCycValueToSet3 >0){
analogWrite(ESP8266_4,DutyCycValueToSet3);
Serial.println("Set for 3rd duty cycle");
}
//Set the Frequency
analogWriteFreq(FreqValueNew);
client.flush();
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\Your Frequency is now ";
s += " <br>";
s += FreqValueNew;
s += " <br>";
s += "Your Duty Cycle is:";
s += " <br>";
s += "First:";
s += DutyCycValueToSet;
s += " <br>";
s += "Second:";
s += DutyCycValueToSet2;
s += " <br>";
s += "Third:";
s += DutyCycValueToSet3;
s += "</html>\n";
Serial.println(s);
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disonnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}