Last active
July 10, 2022 20:15
-
-
Save kc1awv/18379ac919bc44fe8fcb0253710fc560 to your computer and use it in GitHub Desktop.
BME680 Sensors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <WiFi.h> | |
#include <PubSubClient.h> | |
#include <Wire.h> | |
#include <Adafruit_BME680.h> | |
#include <Adafruit_Sensor.h> | |
// WiFi SSID / Password | |
#define wifi_ssid "[WIFI_SSID]" | |
#define wifi_password "[WIFI_PASS]" | |
// MQTT Broker IP address, username, password, and client ID | |
#define mqtt_server "[IP_ADDRESS]" | |
#define mqtt_user "[USERNAME]" | |
#define mqtt_password "[PASSWORD]" | |
#define mqtt_clientid "ESP32Client1" | |
// Sensor MQTT Topics | |
#define temperature_topic "livingroom/temperature" | |
#define humidity_topic "livingroom/humidity" | |
#define pressure_topic "livingroom/pressure" | |
#define gas_topic "livingroom/gas" | |
#define output_topic "livingroom/output" | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
long lastMsg = 0; | |
char msg[50]; | |
int value = 0; | |
// BME680 sensor SPI pins | |
#include <SPI.h> | |
#define BME_SCK 18 | |
#define BME_MISO 19 | |
#define BME_MOSI 23 | |
#define BME_CS 5 | |
//Adafruit_BME680 bme(BME_CS); // hardware SPI | |
Adafruit_BME680 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI | |
// Declare floats | |
float temperature = 0; | |
float humidity = 0; | |
float pressure = 0; | |
float gas = 0; | |
// LED Pin | |
const int ledPin = 2; | |
void setup_wifi() { | |
delay(10); | |
// Connect to WiFi network | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(wifi_ssid); | |
WiFi.begin(wifi_ssid, wifi_password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void callback(char* topic, byte* message, unsigned int length) { | |
Serial.print("Message arrived on topic: "); | |
Serial.print(topic); | |
Serial.print(". Message: "); | |
String messageTemp; | |
for (int i = 0; i < length; i++) { | |
Serial.print((char)message[i]); | |
messageTemp += (char)message[i]; | |
} | |
Serial.println(); | |
// If a message is received on the topic */output, you check if the message is either "on" or "off". | |
// Changes the output state according to the message | |
if (String(topic) == output_topic) { | |
Serial.print("Changing output to "); | |
if(messageTemp == "on"){ | |
Serial.println("on"); | |
digitalWrite(ledPin, HIGH); | |
} | |
else if(messageTemp == "off"){ | |
Serial.println("off"); | |
digitalWrite(ledPin, LOW); | |
} | |
} | |
} | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
// Attempt to connect | |
if (client.connect(mqtt_clientid, mqtt_user, mqtt_password)) { | |
Serial.println("connected"); | |
// Subscribe | |
client.subscribe(output_topic); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
void setup() { | |
Serial.begin(115200); | |
// default settings | |
// (you can also pass in a Wire library object like &Wire2) | |
//status = bme.begin(); | |
if (!bme.begin(0x76)) { | |
Serial.println("Could not find a valid BME680 sensor, check wiring!"); | |
while (1); | |
} | |
setup_wifi(); | |
client.setServer(mqtt_server, 1883); | |
client.setCallback(callback); | |
pinMode(ledPin, OUTPUT); | |
} | |
void loop() { | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.loop(); | |
long now = millis(); | |
if (now - lastMsg > 5000) { | |
lastMsg = now; | |
// Temperature in Celsius | |
//temperature = bme.readTemperature(); | |
// Temperature in Fahrenheit | |
temperature = 1.8 * bme.readTemperature() + 32; // Temperature in Fahrenheit | |
// Convert the value to a char array | |
char tempString[8]; | |
dtostrf(temperature, 1, 2, tempString); | |
Serial.print("Temperature: "); | |
Serial.println(tempString); | |
client.publish(temperature_topic, tempString); | |
humidity = bme.readHumidity(); | |
// Convert the value to a char array | |
char humString[8]; | |
dtostrf(humidity, 1, 2, humString); | |
Serial.print("Humidity: "); | |
Serial.println(humString); | |
client.publish(humidity_topic, humString); | |
pressure = bme.readPressure(); | |
// Convert the value to a char array | |
char presString[6]; | |
dtostrf(pressure, 1, 0, presString); | |
Serial.print("Pressure: "); | |
Serial.println(presString); | |
client.publish(pressure_topic, presString); | |
gas = bme.readGas(); | |
// Convert the value to a char array | |
char gasString[5]; | |
dtostrf(gas, 1, 0, gasString); | |
Serial.print("Gas: "); | |
Serial.println(gasString); | |
client.publish(gas_topic, gasString); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment