Hi Guys,
I have put the webserver in to its own function/block and want to call it from a conditional statement if the client connects then run the webserver. I want to do this so I can do the google sheets logging as a seperate function and conditional statement but I am stuck at the first stage of that.
The code works if I just call the function “web_server()” but doesn’t seem to work if I uncomment the conditional statement out. All it does is print to serial that a new client is connected but doesnt run the web server in my browser.
I have tried so many different things and nothing is working, Any ideas?
// Load Wi-Fi library
#include
//Load Temp Sensor Libraries
#include
#include
//Load EEPROM Library
#include
// define the number of bytes you want to access
#define EEPROM_SIZE 1
//Define temp probe pin
#define ONE_WIRE_BUS 15
// set up 1-wire probes
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress AIR_TEMP_SENSOR = {0x28, 0xFF, 0x16, 0x8D, 0x87, 0x16, 0x03, 0x50}; //Test sensor A
DeviceAddress VAT_TEMP_SENSOR = {0x28, 0xFF, 0x0A, 0x2E, 0x68, 0x14, 0x04, 0xA6}; //Test sensor B
//variables for temp sensors
float air_temp;
float vat_temp;
//set temp variables
float set_temp;
float new_set_temp;
//define state variables for testing
const int STATE_ERROR = -1;
const int STATE_RELAX = 0;
const int STATE_IDLE = 1;
const int STATE_COOL = 2;
const int STATE_HEAT = 3;
int state = STATE_COOL;
// Replace with your network credentials
const char* ssid = "ScottGuest2";
const char* password = "scott630";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
WiFiClient client = server.available();
void setup() {
Serial.begin(115200);
//Start EEPROM at defined size (above).
EEPROM.begin(EEPROM_SIZE);
// Set up temperature probes
sensors.setResolution(AIR_TEMP_SENSOR, 11); //resolution of 0.125deg cels,
sensors.setResolution(VAT_TEMP_SENSOR, 11); //takes approx 375ms
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
//set up set temp variables here.
if (EEPROM.read(0) != 255) {
set_temp = EEPROM.read(0);
}
if (EEPROM.read(0) == 255) {
set_temp = 19.0;
}
new_set_temp = set_temp;
}
void loop(){
//Get temps
sensors.requestTemperaturesByAddress(AIR_TEMP_SENSOR);
sensors.requestTemperaturesByAddress(VAT_TEMP_SENSOR);
vat_temp = sensors.getTempC(VAT_TEMP_SENSOR);
air_temp = sensors.getTempC(AIR_TEMP_SENSOR);
//run web server
web_server();
//conditional statement to run webserver
/*WiFiClient client = server.available(); // Listen for incoming clients
if (client) {
web_server();
}*/
}
void web_server(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
//Set Temp Changes By Button
if (header.indexOf("GET /set/up") >= 0) {
Serial.println("Increase New Set Temperature by 0.5");
new_set_temp = new_set_temp + 0.5;
}
else if (header.indexOf("GET /set/down") >= 0) {
Serial.println("Decrease New Set Temperature by 0.5");
new_set_temp = new_set_temp - 0.5;
}
//Submit button for changing set temp
if (header.indexOf("GET /set/submit") >= 0) {
Serial.print("Change Set Temp To ");
set_temp = new_set_temp;
Serial.println(set_temp);
EEPROM.write(0, set_temp);
EEPROM.commit();
Serial.println("EEPROM Write");
}
// Display the HTML web page
client.println("");
client.println("");
client.println("");
//CSS Styles
client.println("html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center; font-size: 15px}");
client.println(".button {background-color: lightgrey; color: black; padding: 10px 20px; text-decoration: none; font-size: 15px; margin: 2px; cursor: pointer; border-color: black; border-style: solid; border-radius: 10px;}");
client.println(".temp {background-color: #4CAF50; color: white; padding: 10px 30px; text-decoration: none; font-size: 15px; margin: 2px; border-color: black; border-style: none; border-radius: 10px;}");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println("");
// Web Page Heading
client.println("Laser Snake Temperature");
//Set Temp Line
client.println("Set Temperature =");
client.println("" );
client.println(set_temp);
client.println("℃
");
//Change Set Temp Line
client.println("New Set Temperature =");
client.println("" );
client.println(new_set_temp);
client.println("℃");
client.println("+-SUBMIT
");
//Fermenter Temp Line
client.println("Fermenter Temperature =");
client.println("" );
client.println(vat_temp);
client.println("℃
");
//Fridge Temp Line
client.println("Fridge Temperature =");
client.println("" );
client.println(air_temp);
client.println("℃
");
client.println("");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
Hi Matt.
What conditional statement are your referring when you say “… but doesn’t seem to work if I uncomment the conditional statement out”? Is that conditional statement on the code already?
Hi again.
I think I’ve understood what you want.
You want to be able to call the web_server() function if you have a new client available? Is that right?
I’ve tested what you want on a simple web server, but the idea can be applied to your web server too.
Here is the code: https://gist.github.com/RuiSantosdotme/844d4c88fa48e95548294719e5de21d1
Basically, you need to pass the client to the web_server() function as follows:
void web_server(WiFiClient client) {
In the loop(), check if a client is available. If it is, run the web_server client with the current client:
void loop(){ WiFiClient client = server.available(); // Listen for incoming clients if (client){ web_server(client); } }
I hope this helps.
Let me know if it works.
Regards,
Sara