-->
Page 1 of 3

PWM Help and More Stable Wifi Connectiong

PostPosted: Tue Nov 17, 2015 10:10 am
by Oty1
Hello All,

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!
Code: Select all#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
}

Re: PWM Help and More Stable Wifi Connectiong

PostPosted: Wed Nov 18, 2015 1:30 pm
by Oty1
I have rewritten my code and I think it logically works better.
Code: Select all#include <ESP8266WiFi.h>
#define ESP8266_LED1 5
#define ESP8266_LED2 13
#define ESP8266_LED3 4

const char* ssid = "XXXX";
const char* password = "XXXX";

// 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);

  // prepare LED
  pinMode(ESP8266_LED1, OUTPUT);
  digitalWrite(ESP8266_LED1, 0);
  pinMode(ESP8266_LED2, OUTPUT);
  digitalWrite(ESP8266_LED2, 0);
  pinMode(ESP8266_LED3, OUTPUT);
  digitalWrite(ESP8266_LED3, 0);
 
  // 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("new client");
  while(!client.available()){
    delay(1);
  }
 
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();


//  int maxim = req.length();
//Serial.println(maxim);
//Serial.println("''''''''''");

//Will give you the frequency value
String FVFind = req.substring(req.indexOf("l1"));
//Serial.println();
//Serial.println(FVFind);
String FV = FVFind.substring(3,FVFind.indexOf("/"));
Serial.println();
Serial.println(FV);
Serial.println("''''''''''");
float FVFloat = FV.toFloat();
Serial.println(FVFloat);
Serial.println("''''''''''");

//Will give you the Duty Cycle 1 value
String DC1Find = req.substring(req.indexOf("c1"));
//Serial.println();
//Serial.println(DC1Find);
String DC1 = DC1Find.substring(3,DC1Find.indexOf("/"));
Serial.println();
Serial.println(DC1);
Serial.println("''''''''''");
float DC1Float = DC1.toFloat();
float DC1Percent = 1023 * (DC1Float/100);
Serial.println(DC1Percent);
Serial.println("''''''''''");

//Will give you the Duty Cycle 2 value
String DC2Find = req.substring(req.indexOf("c2"));
//Serial.println();
//Serial.println(DC2Find);
String DC2 = DC2Find.substring(3,DC2Find.indexOf("/"));
Serial.println();
Serial.println(DC2);
Serial.println("''''''''''");
float DC2Float = DC2.toFloat();
float DC2Percent = 1023 * (DC2Float/100);
Serial.println(DC2Percent);
Serial.println("''''''''''");

//Will give you the Duty Cycle 3 value
String DC3Find = req.substring(req.indexOf("c3"));
//Serial.println();
//Serial.println(DC3Find);
String DC3 = DC3Find.substring(3,DC3Find.indexOf("/"));
Serial.println();
Serial.println(DC3);
Serial.println("''''''''''");
float DC3Float = DC3.toFloat();
float DC3Percent = 1023 * (DC3Float/100);
Serial.println(DC3Percent);
Serial.println("''''''''''");

//  // Match the request
//  int val;
//  if (req.indexOf("/?freqVal/0") != -1)
//    val = 0;
//  else if (req.indexOf("/gpio/1") != -1)
//    val = 1;
//  else {
//    Serial.println("invalid request");
//    client.stop();
//    return;
//  }

analogWrite(ESP8266_LED1,DC1Percent);
analogWrite(ESP8266_LED2,DC1Percent);
analogWrite(ESP8266_LED3,DC1Percent);

//Set the Frequency
analogWriteFreq(FVFloat);

//  // Set GPIO2 according to the request
//  digitalWrite(2, val);
 
  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\nGPIO is now ";
 // s += (val)?"high":"low";
  s += "</html>\n";

  // 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
}


So I enter the web address "192.168.XX.XXX/?freqVal1=5//?dutyCyc1=50//?dutyCyc2=100//?dutyCyc3=75//" to set everything and hit enter my serial monitor shows the values correctly and disconnects the client.

Then without me doing anything a new client is connected and a get request is sent " GET /favicon.ico HTTP/1.1".

Does anyone know what that is and possibly how to get rid of it?

Thank you for reading and any help!

Re: PWM Help and More Stable Wifi Connectiong

PostPosted: Wed Nov 18, 2015 2:03 pm
by martinayotte
If you wish that your URL parsing been more HTTP stand, it should look more like :

Code: Select all192.168.XX.XXX/?freqVal1=5&dutyCyc1=50&dutyCyc2=100&dutyCyc3=75


Even if your sketch is acutally working, the standard way will be better if you start using other kind of software.

For the "GET /favicon.ico HTTP/1.1" request, this is normal when using a browser, it try to get an icon for the page tab.
So, every browser will do that, there is no way to get rid of it, simply ignore the request.

Re: PWM Help and More Stable Wifi Connectiong

PostPosted: Wed Nov 18, 2015 2:22 pm
by Oty1
Thank you for your response martinayotte!

I will keep keep that format in mind for future projects.

As for the favicon I did more looking online and found just like you said browsers look for it. To ignore it I tried using an if statement but it doesn't seam to be working.
Code: Select allif (req.indexOf("fav") != -1){
  Serial.println("invalid request");
    client.stop();
    return;
}


Do you have any suggestions on how to ignore the request?