Esp12 dme680.ino

From Federal Burro of Information
Revision as of 20:42, 14 October 2021 by David (talk | contribs) (Created page with " <pre> #include <Wire.h> #include <SPI.h> #include <Adafruit_Sensor.h> #include "Adafruit_BME680.h" #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

#ifndef STASSID
#define STASSID "Octonauts"
#define STAPSK  "aaabbbccc"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

ESP8266WebServer server(80);

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME680 bme;

void handleRoot() {
  digitalWrite(LED_BUILTIN, HIGH);
  Serial.print("request: root");
  server.send(200, "text/plain", "hello from esp8266!");
  digitalWrite(LED_BUILTIN, LOW);
}

void handleMetrics() {
  digitalWrite(LED_BUILTIN, HIGH);
  Serial.print("request: metrics");

  delay(1000);                       // wait for a second
  Serial.print("bme.performReading()");
  if (! bme.performReading()) {
    Serial.println("Failed to perform reading.");
    server.send(200, "text/plain", "Failed to perform reading.");
    return;
  }
  Serial.println("Reading done, sending....");
  // # HELP temperature Temperature in Celcius"
  // # TYPE temperature guage")
  String message = "temperature ";
  message += bme.temperature;
  message += "\n";
  
  // Serial.println("# HELP barometric_pressure in hectopascals (hPa)"
  // Serial.println("# TYPE barometric_pressure guage")
  message += "barometric_pressure ";
  message += (bme.pressure / 100.0);
  message += "\n";

  // Serial.println("# HELP humidity relative humidity percent (%)"
  // Serial.println("# TYPE humidity guage")
  message += "humidity ";
  message += bme.humidity;
  message += "\n";

  // Serial.println("# HELP gas_resistance Gas Resistance in  KOhms"
  // Serial.println("# TYPE gas_resistance guage")
  message += "gas_resistance ";
  message += (bme.gas_resistance / 1000.0);
  message += "\n";

  // Serial.println("# HELP approximate_altitude in meters KOhms"
  // Serial.println("# TYPE approximate_altitude guage")
  message += "approximate_altitude ";
  message += bme.readAltitude(SEALEVELPRESSURE_HPA);
  message += "\n";
  server.send(200, "text/plain", message);
}

void handleNotFound() {
  server.send(404, "text/plain", "Not found");
}

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  while (!Serial);
  
  Serial.println(F("begin wifi"));
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println(F("wifi begined, wait for connected"));
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(F("connected"));
  
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("weatherstation1")) {
    Serial.println("MDNS responder started");
  }

  Serial.println(F("BME680 begin"));
  if (!bme.begin()) {
    Serial.println("Could not find a valid BME680 sensor, check wiring!");
    while (1);
  }
  Serial.println("setting over sampling");
  bme.setTemperatureOversampling(BME680_OS_8X);
  bme.setHumidityOversampling(BME680_OS_2X);
  bme.setPressureOversampling(BME680_OS_4X);
  bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
  bme.setGasHeater(320, 150);
  Serial.println("set over sampling");

  // setting up handlers
  server.on("/", handleRoot);
  server.on("/metrics", handleMetrics);
  Serial.println("setupdone");



/*
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
}
*/

  server.begin();                           // Actually start the server
  Serial.println("HTTP server started");

}

void loop() {
/*
  Serial.println("loop start");
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
*/

  server.handleClient();
  MDNS.update();
}