如何使ESP32 Web服务器从条件语句中的功能运行?

时间:2019-02-18 03:06:22

标签: arduino webserver esp32

我正在ESP32上设置一个Web服务器,以读取和显示温度传感器并更改设定点变量,最终它将与控制啤酒发酵罐的某些控制代码合并。

如果我注释掉对函数“ web_server()”的调用并取消注释条件if语句,我将在串行监视器上打印“新客户端”和“客户端断开连接”。

如果我按原样运行代码(如果循环中的语句被注释掉),则Web服务器将按常规运行。

// Load Wi-Fi library
#include <WiFi.h>

//Load Temp Sensor Libraries
#include <OneWire.h>
#include <DallasTemperature.h>

//Load EEPROM Library
#include <EEPROM.h>

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

// Replace with your network credentials
const char* ssid     = "YOUR_WIFI_SSID";
const char* password = "YOUR_PASSWORD";

// 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("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");           
            client.println("<link rel=\"icon\" href=\"data:,\">");

            //CSS Styles
            client.println("<style>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("</style></head>");

            // Web Page Heading
            client.println("<body><h1>Laser Snake Temperature</h1>");

            //Set Temp Line
            client.println("Set Temperature =");
            client.println("<inline-block class=\"temp\">" );
            client.println(set_temp);
            client.println("&#8451</inline-block><br><br><br>");         

            //Change Set Temp Line
            client.println("New Set Temperature =");
            client.println("<inline-block class=\"temp\">" );
            client.println(new_set_temp);
            client.println("&#8451</inline-block>");         
            client.println("<a href=\"/set/up\"><button class=\"button\">+</button></a><a href=\"/set/down\"><button class=\"button\">-</button></a><a href=\"/set/submit\"><button class=\"button\">SUBMIT</button></a></p><br>");

            //Fermenter Temp Line
            client.println("Fermenter Temperature =");
            client.println("<inline-block class=\"temp\">" );
            client.println(vat_temp);
            client.println("&#8451</inline-block><br><br><br>");

            //Fridge Temp Line
            client.println("Fridge Temperature =");
            client.println("<inline-block class=\"temp\">" );
            client.println(air_temp);
            client.println("&#8451</inline-block><br><br><br>");
            client.println("</body></html>");

            // 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("");
  }
 }

0 个答案:

没有答案