Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Reports sensor readings every ~30 seconds.
- #include <ESP8266WiFi.h> // Include the Wi-Fi library
- #include <PubSubClient.h> // Include the MQTT library
- #include <Wire.h>
- #include <SPI.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_BME280.h>
- // Defines pins numbers
- const int Base = 4; // D2 - Blue
- const int BME_Clock = 14; // D5 Direct
- const int BME_MOSI = 12; //D6 Direct
- const int BME_CS = 13; //D7 Direct
- const int BME_MISO = 15; // D8 Direct
- // Settings for libraries
- WiFiClient espClient;
- PubSubClient client(espClient);
- Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_Clock); // software SPI
- // All constants in the network settings area blanked out for privacy purposes.
- const char* WiFiSSID = ""; // SSID of network to connect to
- const char* WiFiPassword = ""; // Password for network
- const char* MQTTServer = ""; // Server running MQTT
- const char* MQTTUser = ""; // User for MQTT
- const char* MQTTPassword = ""; // Password for MQTT
- // Defines global variables. Most only need to be global to assign a value only once.
- unsigned long lastUpdate = 0;
- void setup_wifi();
- void reconnect();
- void setup() {
- // Initialize serial communication with computer:
- Serial.begin(115200); // Starts the serial communication
- // Set pin modes
- pinMode(Base, OUTPUT);
- // Connect to the WiFi
- setup_wifi();
- // Settings for MQTT server
- client.setServer(MQTTServer, 1883);
- // Settings for BME280
- bool status;
- status = bme.begin();
- if (!status) {
- Serial.println("Could not find a valid BME280 sensor, check wiring!");
- while (1);
- }
- lastUpdate = millis();
- }
- void loop() {
- // Defines to current time to check against whether status reports should be done this loop
- unsigned long now = millis();
- // Check if connected
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- // Update statuses if it has been at least 30 seconds since last update
- if (now - lastUpdate > 30000) {
- // BME280 read data
- float OutsideTemp = (bme.readTemperature() * 9 / 5 + 32);
- float OutsideHumidity = bme.readHumidity();
- float OutsidePressure = (bme.readPressure() / 100.0F);
- float AdjOutsidePressure = (OutsidePressure * pow(1 - 0.0065 * 345 / (bme.readTemperature() + 0.0065 * 345 + 273.15), -5.257));
- char OutsideTempMQTT[6]; // Buffer for the Temperature output of the BME280 under the deck
- char OutsideHumidityMQTT[6]; // Buffer for the Humidity output of the BME280 under the deck
- char OutsidePressureMQTT[7]; // Buffer for the Pressure output of the BME280 under the deck
- char AdjOutsidePressureMQTT[7]; // Buffer for the Altitude Adjusted Pressure output of the BME280 under the deck
- // BME280 display data
- Serial.println("Temperature (F)\tHumidity (%)\tPressure (mbar)\tAdj Pressure (mbar)");
- Serial.print(OutsideTemp);
- Serial.print(",\t\t");
- Serial.print(OutsideHumidity);
- Serial.print(",\t\t");
- Serial.print(OutsidePressure);
- Serial.print(",\t\t");
- Serial.println(AdjOutsidePressure);
- // Only report Temperature and Humidity over MQTT if they are within the realm of possibility
- if ((OutsideTemp >= -50) && (OutsideTemp <= 150)) {
- dtostrf(OutsideTemp, 5, 1, OutsideTempMQTT);
- client.publish("home/underdeck/Temperature", OutsideTempMQTT);
- }
- if ((OutsideHumidity >= 0) && (OutsideHumidity <= 100)) {
- dtostrf(OutsideHumidity, 5, 1, OutsideHumidityMQTT);
- client.publish("home/underdeck/Humidity", OutsideHumidityMQTT);
- }
- if ((OutsidePressure >= 900) && (OutsidePressure <= 1100)) {
- dtostrf(OutsidePressure, 6, 1, OutsidePressureMQTT);
- dtostrf(AdjOutsidePressure, 6, 1, AdjOutsidePressureMQTT);
- client.publish("home/underdeck/Pressure", OutsidePressureMQTT);
- client.publish("home/underdeck/AdjPressure", AdjOutsidePressureMQTT);
- }
- // Set update reference time to "now"
- lastUpdate = now ;
- digitalWrite(Base, HIGH);
- }
- // Turn off fan 5 seconds before taking reading
- switch (now - lastUpdate){
- case 25000 ... 35000:
- if (Base != LOW){
- digitalWrite(Base, LOW);
- }
- break;
- default:
- if (Base != HIGH){
- digitalWrite(Base, HIGH);
- }
- break;
- }
- }
- void setup_wifi() {
- delay(10);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(WiFiSSID);
- if (WiFi.SSID() != WiFiSSID) {
- WiFi.begin(WiFiSSID, WiFiPassword);
- WiFi.persistent(true);
- WiFi.setAutoConnect(true);
- WiFi.setAutoReconnect(true);
- }
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect
- if (client.connect(MQTTUser, MQTTUser, MQTTPassword)) {
- Serial.println("connected");
- // Once connected, publish an announcement...
- client.publish("hello", "Horae Connected");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement