c - 无法使用 ESP8266 上传 HTML 页面

标签 c embedded esp8266 arduino-ide arduino-esp8266

我正在尝试显示 SHT31 的温度和湿度读数HTML 页面上的传感器, 但是每当我尝试在网络浏览器中写入 IP 并进行搜索时,就会显示错误 - “无法访问此站点”。

我正在编写代码(如下所述)以将温度传感器的 I2C 模块与 Adafruit Huzzah ESP8266 连接和接口(interface)。使用库“ESP8266WiFi.h”、“Wire.h”

#include <ESP8266WiFi.h>
#include <Wire.h>

// SHT31 I2C address is 0x44(68)
#define Addr 0x44
// Replace with your network credentials
const char* ssid     = "SSID";
const char* password = "Password";
float Ctemp,Ftemp,humid;

// Set web server port number to 80
WiFiServer server(80);

// Variable to store the HTTP request
String header;

void setup() 
{
  Wire.begin(2,14);
  Serial.begin(115200);
  // 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();
}

void loop(){
  temptask();
  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();


            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta charset=\"utf-8\">");
            client.println("<meta name = \"viewport\" content = \"width = device-width, initial-scale = 1.0\">");
            client.println("<body style= \"background-color:#EDEEF2\">");
            client.println("<h1 style= \"color:blue\">Temperature and Humidity</h1>");
            client.println( "<h3 style= \"font-family:verdana; color:blue\">Temperature in Celcius:</h3>");
            client.println("<p style=\"font-size:160%; font-family:Lucida Console\">"+String(Ctemp,1)+"</p>");
            client.println("<h3 style= \"font-family:verdana; color:blue\">Temperature in Fahrenheit:</h3>");
            client.println("<p style=\"font-size:160%; font-family:Lucida Console\">"+String(Ftemp,1)+"</p>");
            client.println("<h3 style= \"font-family:verdana; color:blue\">Humidity:</h3>");
            client.println("<p style=\"font-size:160%; font-family:Lucida Console\">"+String(humid,1)+"</p>");
            client.println("</body>");
            client.println("</html>");
            break;
          } 
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }
}
}



void temptask(){
 unsigned int data[6];

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Send 16-bit command byte
  Wire.write(0x2C);
  Wire.write(0x06);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(300);

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 6 bytes of data
  Wire.requestFrom(Addr, 6);

  // Read 6 bytes of data
  // temp msb, temp lsb, temp crc, hum msb, hum lsb, hum crc
  if (Wire.available() == 6)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read();
  }
  // Convert the data
  int temp = (data[0] * 256) + data[1];
  float cTemp = -45.0 + (175.0 * temp / 65535.0);
  float fTemp = (cTemp * 1.8) + 32.0;
  float humidity = (100.0 * ((data[3] * 256.0) + data[4])) / 65535.0;  
  Ctemp = cTemp;
  Ftemp = fTemp;
  humidity = humid;

   Serial.print("Temperature in C:\t");
   Serial.println(String(cTemp,1));
   Serial.print("Temperature in F:\t");
   Serial.println(String(fTemp,1));
   Serial.print("Humidity:\t ");
   Serial.println(String(humidity,1));
 }

任何有关导致此问题的建议都将非常感激。

最佳答案

我已经更新了你的代码,现在工作正常,请看一看。

#include <ESP8266WiFi.h>
#include <Wire.h>
#include <ESP8266WebServer.h>

// SHT31 I2C address is 0x44(68)
#define Addr 0x44

// Replace with your network credentials
const char* ssid     = "DcubeAirtel";
const char* password = "D@Airtel190";
float Ctemp,Ftemp,humid;

// Set web server port number to 80
WiFiServer server(80);

// Variable to store the HTTP request
String header;

void setup() 
{
  Wire.begin(2,14);
  Serial.begin(115200);
  // 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();
}

void loop(){
  temptask();
  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,
       String c = client.readStringUntil('\r');             // read a byte, then
       Serial.println(c);
     }                    // print it out the serial monitor
       // 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:
        // 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();

          String PAGE2 = 
 "<!DOCTYPE html>"
 "<html>"
 "<head>"
 "<meta charset=\"utf-8\">"
 "<meta name = \"viewport\" content = \"width = device-width, initial-scale = 1.0\">"
 "<title> My first HTML page</title>"
 "</head>"
 "<body style= \"background-color:#EDEEF2\">"
 "<h1 style= \"color:blue\">Temperature and Humidity</h1>"
 "<h3 style= \"font-family:verdana; color:blue\">Temperature in Celcius:</h3>"
 "<p style=\"font-size:160%; font-family:Lucida Console\">"+String(Ctemp,1)+
 "</p>"
 "<h3 style= \"font-family:verdana; color:blue\">Temperature in Fahrenheit:</h3>"
 "<p style=\"font-size:160%; font-family:Lucida Console\">"+String(Ftemp,1)+
 "</p>"
 "<h3 style= \"font-family:verdana; color:blue\">Humidity:</h3>"
 "<p style=\"font-size:160%; font-family:Lucida Console\">"+String(humid,1)+
 "</p>"
 "</body>"
 "</html>";
        // Display the HTML web page
        client.println(PAGE2);

      } 
}
// Clear the header variable

// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
 }
void temptask(){
  unsigned int data[6];

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Send 16-bit command byte
  Wire.write(0x2C);
  Wire.write(0x06);
  // Stop I2C transmission
  Wire.endTransmission();
  delay(300);

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 6 bytes of data
  Wire.requestFrom(Addr, 6);

 // Read 6 bytes of data
 // temp msb, temp lsb, temp crc, hum msb, hum lsb, hum crc
 if (Wire.available() == 6)
 {
   data[0] = Wire.read();
   data[1] = Wire.read();
   data[2] = Wire.read();
   data[3] = Wire.read();
   data[4] = Wire.read();
   data[5] = Wire.read();
  }
  // Convert the data
  int temp = (data[0] * 256) + data[1];
  float cTemp = -45.0 + (175.0 * temp / 65535.0);
  float fTemp = (cTemp * 1.8) + 32.0;
  float humidity = (100.0 * ((data[3] * 256.0) + data[4])) / 65535.0;  
  Ctemp = cTemp;
  Ftemp = fTemp;
  humidity = humid;

  Serial.print("Temperature in C:\t");
  Serial.println(String(cTemp,1));
  Serial.print("Temperature in F:\t");
  Serial.println(String(fTemp,1));
  Serial.print("Humidity:\t ");
  Serial.println(String(humidity,1));
}

工作原理如下:

  1. 首先读取服务器发送的字符直到下一行
  2. 现在刷新服务器
  3. 现在将您的内容写入服务器。

关于c - 无法使用 ESP8266 上传 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52982967/

相关文章:

lua - Lua脚本在执行前是否加载到内存中?

lua - 如何使用 NodeMCU 收听通过设备广播的其他 WiFi 网络 SSID?

c++ - 使用 Arduino UNO R3 将 ESP8266-01 连接到 WiFi 网络

c - 从动态分配的内存打印时值发生变化

c - SPI EP93xx(带 Linux 的 TS7200 板)

c++ - C++ 的最小条件日志记录

c - 结构对齐问题

c - 在没有 VirtualBox 的情况下使用 VirtualBox SDK

c - `-Wextra` 标志在 clang 编译器中意味着什么?

c - 为什么第一个数组的整体被添加到第二个数组中,位于我分配给它的两个值(从第一个值开始)之上?