Метиостанция на основе ESP32 и BME280 c веб интерфейсом

C Sharp > Метиостанция на основе ESP32 и BME280 c веб интерфейсом
12.04.2018 11:01:10


Наиболее часто встречающиеся слова в статье:

[println] [mySensor] [settings] [include] [blank_line] [address] [skipped] [through] [oversampling] [respectively]


Статья:

Метиостанция  на основе ESP32 и BME280 c веб интерфейсом

ESP32

датчик соединяем по схеме   датчик-ESP32

VIN--3.3V
GND--GND
SCL--22
SDA--21
Скетч: [Скачать]
 
#include <Wire.h>
#include <WiFi.h> //библиотека
#include <stdint.h>
#include "C:\Users\iocsha\Documents\Arduino\ESP32\SparkFunBME280.h"
// Replace with your network details
const char* ssid = "i";
const char* password = "11";
float h, t, p, pin, dp;
char temperatureFString[6];
char dpString[6];
char humidityString[6];
char pressureString[7];
char pressureInchString[6];
char *mess;
byte mac[6]; 
//Global sensor object
BME280 mySensor;
 
// Web Server on port 80
WiFiServer server(80);
 
// only runs once on boot
void setup() {
 
 
  // Initializing serial port for debugging purposes
  Serial.begin(57600);
  delay(10);
  //SDA/SCL default to pins 4 & 5 but any two pins can be assigned as SDA/SCL using Wire.begin(SDA,SCL)  по умолчанию 4(SDA) и 5(SCL).
 
  // 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());
 
    //***Driver settings BME280********************************//
  //commInterface can be I2C_MODE or SPI_MODE
  //specify chipSelectPin using arduino pin names
  //specify I2C address.  Can be 0x77(default) or 0x76
 
  //For I2C, enable the following and disable the SPI section
  mySensor.settings.commInterface = I2C_MODE;
  mySensor.settings.I2CAddress = 0x76;
 
  //For SPI enable the following and dissable the I2C section
  //mySensor.settings.commInterface = SPI_MODE;
  //mySensor.settings.chipSelectPin = 10;
 
 
  //***Operation settings*****************************//
  mySensor.settings.runMode = 3; //  3, Normal mode
  mySensor.settings.tStandby = 5; //  0, 0.5ms
  mySensor.settings.filter = 0; //  0, filter off
  //tempOverSample can be:
  //  0, skipped
  //  1 through 5, oversampling *1, *2, *4, *8, *16 respectively
  mySensor.settings.tempOverSample = 1;
  //pressOverSample can be:
  //  0, skipped
  //  1 through 5, oversampling *1, *2, *4, *8, *16 respectively
  mySensor.settings.pressOverSample = 1;
  //humidOverSample can be:
  //  0, skipped
  //  1 through 5, oversampling *1, *2, *4, *8, *16 respectively
  mySensor.settings.humidOverSample = 1;
  Serial.print("Program Started\nBy Ioksha S.A. @2018\n");
  Serial.print("Starting BME280... result of .begin(): 0x");
  delay(10);  //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up.
  //Calling .begin() causes the settings to be loaded
  Serial.println(mySensor.begin(), HEX);
 
  delay(3000);
}
 
 
 
 
// runs over and over again
void loop() {
 
 
  // Listenning for new clients www
  WiFiClient client = server.available();
  String request;
  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();
        request=request+c;
 
        if (c == ''\n'' && blank_line) {
          // вывод датчиков и самой  страницы
          // Получение  значений  датчиков
 
            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><META HTTP-EQUIV=\"refresh\" CONTENT=\"30\"></head>");
            client.println("<body><h1>ESP32 Weather Web Server</h1>");
            client.println("<table border=\"2\" width=\"456\" cellpadding=\"10\"><tbody><tr><td>");
            client.println("<h3>Temperature = ");
            client.println(mySensor.readTempC(), 2);
            client.println("&deg;</h3><h3>Humidity = ");
            client.println(mySensor.readFloatHumidity(),2);
            client.println("%</h3>");
            client.println("<h3>Presure = ");
            client.println(mySensor.readFloatPressure()* 0.0075006375541921, 2);
            client.println("mm.rt.st</h3>");
            client.println("<h3>Alt(m) = ");
            client.println(mySensor.readFloatAltitudeMeters(), 3);
            client.println("m</h3>");
            client.println("<h3>Free memory = ");
            client.println(ESP.getFreeHeap());
            client.println("byte</h3><h3>Wifi status:");
              client.print("SSID: ");
             client.println(WiFi.SSID());
 
             // print your WiFi shield''s IP address:
            IPAddress ip = WiFi.localIP();
            client.print("IP Address: ");
             client.println(ip);
          // print the received signal strength:
            long rssi = WiFi.RSSI();
            client.print("signal strength (RSSI):");
            client.print(rssi);
            client.println(" dBm");
 
            client.println("</h3></td></tr></tbody></table><h3>@2018 Ioksha S. compile 11.04.2018</h3>");
            client.println("</body></html>");
            request="";
            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.");
  }
}