我正在尝试在HTML页面上显示SHT31传感器的温度和湿度读数,
但是每当我尝试在Web浏览器中写入IP并进行搜索时,都会显示错误-“无法访问此站点”。
我正在使用下面的代码(使用库“ ESP8266WiFi.h”,“ Wire.h”将温度传感器的I2C模块与Adafruit Huzzah ESP8266连接和连接)
#include
#include
// 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("");
client.println("
");client.println("");
client.println("
");client.println("
Temperature and Humidity
");client.println( "
Temperature in Celcius:
");client.println("
"+String(Ctemp,1)+"
");client.println("
Temperature in Fahrenheit:
");client.println("
"+String(Ftemp,1)+"
");client.println("
Humidity:
");client.println("
"+String(humid,1)+"
");client.println("");
client.println("");
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));
}
对于导致此问题的原因的任何建议,我们将不胜感激。