Нашел другой скетч. Пока без зависов.
Код:
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <SPI.h>
#include <ArduinoJson.h>
Adafruit_BMP280 bmp; // I2C
// Replace with your network details
const char* ssid = "ssid";
const char* password = "pwd";
float t, p;
char temperatureString[6];
char pressureString[7];
// Web Server on port 80
WiFiServer server(80);
// only runs once on boot
void setup() {
// Initializing serial port for debugging purposes
Serial.begin(115200);
delay(10);
Wire.begin(0, 2);
Wire.setClock(100000);
// Connecting to WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Starting the web server
server.begin();
Serial.println("Web server running. Waiting for the ESP IP...");
delay(10000);
// Printing the ESP IP address
Serial.println(WiFi.localIP());
Serial.println(F("BME280 test"));
if (!bmp.begin()) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
}
void getWeather() {
t = bmp.readTemperature();
p = (bmp.readPressure() * 7.5006e-3);
dtostrf(t, 5, 1, temperatureString);
dtostrf(p, 6, 1, pressureString);
delay(100);
}
// runs over and over again
void loop() {
// Listenning for new clients
WiFiClient client = server.available();
StaticJsonBuffer<300> JSONbuffer;
JsonObject& JSONencoder = JSONbuffer.createObject();
if (client) {
Serial.println("New client");
// bolean to locate when the http request ends
boolean blank_line = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n' && blank_line) {
getWeather();
JSONencoder["Place"] = "Mub, 7";
JSONencoder["Number"] = "1";
JSONencoder["Temp"] = temperatureString;
JSONencoder["Pres"] = pressureString;
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// your actual web page that displays temperature
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head></head>");
client.println("<body>");
String out;
JSONencoder.printTo(out);
client.println(out);
client.println("</body></html>");
break;
}
if (c == '\n') {
// when starts reading a new line
blank_line = true;
}
else if (c != '\r') {
// when finds a character on the current line
blank_line = false;
}
}
}
// closing the client connection
delay(1);
client.stop();
Serial.println("Client disconnected.");
}
}