Chat freely about anything...

User avatar
By m4rten
#49920 Hello everyone,

iam very new to ESP + Arduino Programming so please be kind :roll:

my current project which iam doing for a friend should be controlled over a webinterface.
first part:
The Webinterface did allready work: you can enter a temperature, get a confirmation that the esp received it and also you can get a live temperature.
second part:
the regulation of the temperature is a small part of the programm which works on the esp, too.

if i combine both programs in one sketch, it is not possible to join the access point anymore...

i think its somehing with a too big time delay for the programming part, but as i said, iam new to esp and also html programming...

i hope someone can help me.

Code: Select all/*Generelle Infos:
-(Misst analog bis 1 Volt und gibt max. 3,3Volt raus!!)
-analogwert ist aber in aufloesung von 1023


*/

//Bibliotheken
#include <ESP8266WiFi.h>



// WiFi
const char WiFiAPPSK[] = "Grill";
WiFiServer server(80);

//Variablen fuer Web
String req;
String solltemp_str;
int solltemp_int;

//Variablen fuer Steuerung
float temperatur, pt100, spannung_mV, strom;
int steuerwert;
int sollwert = 180;  //Sollwert der Grilltemperatur


void setup(){
 
//Normales Setup
  Serial.begin(9600);
  pinMode(16,OUTPUT);
 
//WiFi
 WiFi.mode(WIFI_AP);
// Do a little work to get a unique-ish name. Append the
// last two bytes of the MAC (HEX'd) to "Thing-":
  uint8_t mac[WL_MAC_ADDR_LENGTH];
  WiFi.softAPmacAddress(mac);
  String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
                 String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
  macID.toUpperCase();
  String AP_NameString = "ESP8266 Thing " + macID;

  char AP_NameChar[AP_NameString.length() + 1];
  memset(AP_NameChar, 0, AP_NameString.length() + 1);

  for (int i = 0; i < AP_NameString.length(); i++)
    AP_NameChar[i] = AP_NameString.charAt(i);

  WiFi.softAP(AP_NameChar, WiFiAPPSK);
  server.begin();

}
void loop(){
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

analogWrite(12,solltemp_int);
 
  // Read the first line of the request
  req = client.readStringUntil('\r');


 
  if ( req.indexOf("?Solltemp=") != -1) {
   
//1 Stellige Temperatureingabe
if( req.length() == 25 ){
   solltemp_int =  req.substring(15,16).toInt();
}

//2 Stellige Temperatureingabe
if( req.length() == 26 ){
   solltemp_int =  req.substring(15,17).toInt();
}

//3 Stellige Temperatureingabe
  if( req.length() == 27 ){
    solltemp_int=  req.substring(15,18).toInt();
}
solltemp_int = constrain(solltemp_int,0,250);

}



  // HTML-Infos------------------------------------------------------------------------------------------------
client.print("<html><head><meta charset=\"utf-8\"><html lang=\"de\">");

//Tabbeschriftung
client.print("<title>Mircos Grill</title>");
//Rahmen (<fieldset>, überschrift des rahmens ist <legend>
client.print("<fieldset><legend><h1>Mircos Grill</h1></legend>");
client.print("<p><form>");
//Eingabefeld, eingabefeld direkt am anfang aktiv(autofocus)
client.print("<input method=POST min=\"0\" max=\"250\" type=\"number\" autofocus=\"1\" name=\"Solltemp\"/><Label>Solltemp in &deg;C</Label>");
client.print("</p>");
client.print("<p>");
//Enterbutton
client.print("<INPUT TYPE=\"submit\" value=\"Senden\">");
//Aktualisieren Button
client.print("<FORM><INPUT TYPE=\"button\" onClick=\"history.go(0)\" VALUE=\"Aktualisieren\"></FORM>");
client.print("</form>");
client.print("<br>");
client.print("Solltemperatur aktuell: <strong>");
client.print(solltemp_int);
client.print(" &deg;C");
client.print("  <Ist-Temperatur Grill:");
client.print(temperatur);
client.print(" &deg;C</strong>");
client.print("</fieldset>");

client.print("</head>");
client.print("<html/>");

  // Send the response to the client
//delay(1);
Serial.println("Client disonnected");
  // The client will actually be disconnected
  // when the function returns and 'client' object is detroyed
//----------------------------------------------------------------------------------------------------------------

temperaturholen();
lueftersteuerung();
ausgabe();

}//void loop ende

void temperaturholen(){
 
spannung_mV = (analogRead(A0))/1.07;
strom = map(spannung_mV,359,678,359,320);  // um die leicht nicht linearen stromverlauf auszugleichen
strom = strom / 100;
pt100 = (spannung_mV/ strom) ; // spannung_mV / 3,4mA = Widerstand am PT100
temperatur = (pt100 - 100) / 0.38; // 100 Ohm sind 0°C also aktueller Widerstand minus 100 um mit faktor auf temperatur zu kommen

}
void lueftersteuerung(){

steuerwert = constrain(map(temperatur,0,solltemp_int,255,0),0,255); //je mehr unterschied zwischen soll und ist ist, desto staerker laeuft der luefter

analogWrite(16,steuerwert);
 
}
void ausgabe(){
Serial.print("\t Temp:");
Serial.print(temperatur);
Serial.print("\t Steuerwert:");
Serial.println(steuerwert);
}

User avatar
By martinayotte
#49923 I'm seeing 2 things that could be the reason.
I'm saying "could" or "maybe" because I didn't verify myself.

First, more than a year ago, I've faced an issue in STA mode when SSID has spaces in it, removing them fixed the issue.
So, it could be the same issue with AP mode.

Second, Password has to be between 8 and 63 characters, but here you have only 5, so, it won't work.

And third point, only to help you having better and more readable code, you don't need this huge piece of code to translate a String into a "char *" along with MAC formatting. Simply use the following code :
Code: Select all  String mac = WiFi.softAPmacAddress();
  String AP_Name = "ESP8266-Thing-" + mac.substring(mac.length() - 2);
  WiFi.softAP(AP_Name, WiFiAPPSK);
User avatar
By m4rten
#49990 Hi i made every thing you said but it doesnt work.

its like this :

i start the esp 12e with that code, it open the AP. i enter that AP with the password. Then i browse to 192.168.4.1 and it shows me the html page. but after that it looses the connection and the AP is offline. anyone got an idea ?

here is the updated code :

Code: Select all/*Generelle Infos:
-(Misst analog bis 1 Volt und gibt max. 3,3Volt raus!!)
-analogwert ist aber in aufloesung von 1023


*/

//Bibliotheken
#include <ESP8266WiFi.h>



// WiFi
const char WiFiAPPSK[] = "Grill";
WiFiServer server(80);

//Variablen fuer Web
String req;
String solltemp_str;
int solltemp_int;

//Variablen fuer Steuerung
float temperatur, pt100, spannung_mV, strom;
int steuerwert;
int sollwert = 180;  //Sollwert der Grilltemperatur


void setup(){
 
//Normales Setup
 // Serial.begin(9600);
  pinMode(16,OUTPUT);
 
//WiFi
 WiFi.mode(WIFI_AP);

  String mac = WiFi.softAPmacAddress();
  String AP_NameString = "ESP8266 Thing " + mac.substring(mac.length() - 2);

  WiFi.softAP("DerGrill","1234567acht");
  server.begin();




 // String AP_Name = "ESP8266-Thing-" + mac.substring(mac.length() - 2);






}
void loop(){
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
}
  // Read the first line of the request
  req = client.readStringUntil('\r');
 
  if ( req.indexOf("?Solltemp=") != -1) {
   
//1 Stellige Temperatureingabe
if( req.length() == 25 ){
   solltemp_int =  req.substring(15,16).toInt();
}

//2 Stellige Temperatureingabe
if( req.length() == 26 ){
   solltemp_int =  req.substring(15,17).toInt();
}

//3 Stellige Temperatureingabe
  if( req.length() == 27 ){
    solltemp_int=  req.substring(15,18).toInt();
}
solltemp_int = constrain(solltemp_int,0,250);

}
  // HTML-Infos------------------------------------------------------------------------------------------------



 
client.print("<html><head><meta charset=\"utf-8\"><html lang=\"de\"><title>Mircos Grill</title>");
client.print("<fieldset><legend><h1>Mircos Grill</h1></legend><p><form>");
client.print("<input method=POST min=\"0\" max=\"250\" type=\"number\" autofocus=\"1\" name=\"Solltemp\"/><Label>Solltemp in &deg;C</Label>");
client.print("</p><p><INPUT TYPE=\"submit\" value=\"Senden\">");
client.print("<FORM><INPUT TYPE=\"button\" onClick=\"history.go(0)\" VALUE=\"Aktualisieren\"></FORM>");
client.print("</form>  <Ist-Temperatur Grill:<br>Solltemperatur aktuell: <strong>");
client.print(solltemp_int);
client.print(" &deg;C  Ist-Temperatur Grill:");
client.print(temperatur);
client.print(" &deg;C</strong></fieldset></head><html/>");
delay(1);
//Serial.println("Ende HTML ");

//----------------------------------------------------------------------------------------------------------------

temperaturholen();
lueftersteuerung();
ausgabe();

}//void loop ende

void temperaturholen(){
 
spannung_mV = (analogRead(A0))/1.07;
strom = map(spannung_mV,359,678,359,320);  // um die leicht nicht linearen stromverlauf auszugleichen
strom = strom / 100;
pt100 = (spannung_mV/ strom) ; // spannung_mV / 3,4mA = Widerstand am PT100
temperatur = (pt100 - 100) / 0.38; // 100 Ohm sind 0°C also aktueller Widerstand minus 100 um mit faktor auf temperatur zu kommen

}
void lueftersteuerung(){
steuerwert = constrain(map(temperatur,0,solltemp_int,255,0),0,255); //je mehr unterschied zwischen soll und ist ist, desto staerker laeuft der luefter
analogWrite(16,steuerwert);
}
void ausgabe(){
Serial.print("\t Temp:");
Serial.print(temperatur);
Serial.print("\t Steuerwert:");
Serial.println(steuerwert);
}