iam very new to ESP + Arduino Programming so please be kind
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.
/*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 °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(" °C");
client.print(" <Ist-Temperatur Grill:");
client.print(temperatur);
client.print(" °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);
}