Weatherstation/Weatherstation1: Difference between revisions

From Federal Burro of Information
Jump to navigationJump to search
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:


I smashed together an ESP-12 and a BME680 and call it weatherstation1
I smashed together an ESP-12 and a BME680 and call it weatherstation1
arduino file: esp12-bme680.ino
<pre>
#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 "SSID"
#define STAPSK  "XXX"
#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");
}
// the loop function runs over and over again forever
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();
}
</pre>


[[image:weatherstation1_hardware.jpg|600px]]
[[image:weatherstation1_hardware.jpg|600px]]




[[image:barometric_pressure.png]]
[[image:barometric_pressure.png|600px]]
 
[[Category:Script]]
[[Category:Computers]]

Latest revision as of 05:11, 1 April 2023

I smashed together an ESP-12 and a BME680 and call it weatherstation1

arduino file: esp12-bme680.ino

#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 "SSID"
#define STAPSK  "XXX"
#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");

}

// the loop function runs over and over again forever
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();
}