SHARE
TWEET

ESP8266-01 BME280 MQTT

a guest Oct 13th, 2019 87 in 333 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <Wire.h>
  4. #include <Adafruit_Sensor.h>
  5. #include <Adafruit_BME280.h>
  6.  
  7. #define SEALEVELPRESSURE_HPA (1013.25)
  8. #define I2C_SDA_PIN 0
  9. #define I2C_SCL_PIN 2
  10.  
  11. // Aufbau der Schaltung:
  12. // http://www.forward.com.au/pfod/ESP8266/GPIOpins/ESP8266_01_pin_magic.html
  13. // "Best Trick – Use I2C" Variante 1 (GPIO0 und GPIO2)
  14.  
  15.  
  16. const char* ssid = "ssid";
  17. const char* password = "password ";
  18. const char* mqtt_server = "x.x.x.x";
  19.  
  20. Adafruit_BME280 bme;
  21. float temperature, humidity, pressure, altitude;
  22.  
  23. WiFiClient espClient;
  24. PubSubClient client(espClient);
  25. long lastMsg = 0;
  26. char msg[50];
  27. int value = 0;
  28.  
  29.  
  30. void setup()
  31. {
  32.   Serial.begin(74880);
  33.   delay(100);
  34.  
  35.   Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN);
  36.  
  37.   if (!bme.begin(0x76)) {
  38.     Serial.println("Could not find a valid BME280 sensor, check wiring!");
  39.     while (true) {
  40.       delay(1000);
  41.     }
  42.   } else {
  43.     Serial.println("BME280 sensor found!");
  44.   }
  45.  
  46.   Serial.println("Connecting to ");
  47.   Serial.println(ssid);
  48.  
  49.   //connect to your local wi-fi network
  50.   WiFi.begin(ssid, password);
  51.  
  52.   //check wi-fi is connected to wi-fi network
  53.   while (WiFi.status() != WL_CONNECTED) {
  54.     delay(1000);
  55.     Serial.print(".");
  56.   }
  57.   Serial.println("");
  58.   Serial.println("WiFi connected..!");
  59.   Serial.print("Got IP: ");
  60.   Serial.println(WiFi.localIP());
  61.  
  62.   randomSeed(micros());
  63.  
  64.   client.setServer(mqtt_server, 1883);
  65. }
  66.  
  67.  
  68. */
  69. void reconnect() {
  70.   // Loop until we're reconnected
  71.   while (!client.connected()) {
  72.     Serial.print("Attempting MQTT connection...");
  73.     // Create a random client ID
  74.     String clientId = "ESP8266Client-";
  75.     clientId += String(random(0xffff), HEX);
  76.     // Attempt to connect
  77.     if (client.connect(clientId.c_str())) {
  78.       Serial.println("connected");
  79.       // Once connected, publish an announcement...
  80.       //client.publish("outTopic", "hello world");
  81.       // ... and resubscribe
  82.       //client.subscribe("inTopic");
  83.     } else {
  84.       Serial.print("failed, rc=");
  85.       Serial.print(client.state());
  86.       Serial.println(" try again in 5 seconds");
  87.       // Wait 5 seconds before retrying
  88.       delay(5000);
  89.     }
  90.   }
  91. }
  92.  
  93. void loop()
  94. {
  95.   if (!client.connected()) {
  96.     reconnect();
  97.   }
  98.   client.loop();
  99.  
  100.   // read BME280 sensor every 10 seconds
  101.   long now = millis();
  102.   if (now - lastMsg > 10000) {
  103.     lastMsg = now;
  104.  
  105.     String msg = "";
  106.     char msgTemperatur[25];
  107.     char msgFeutigkeit[25];
  108.  
  109.     msg = bme.readTemperature();
  110.     msg.toCharArray(msgTemperatur, 25);
  111.  
  112.     msg = bme.readHumidity();
  113.     msg = msg + "%";
  114.     msg.toCharArray(msgFeutigkeit, 25);
  115.  
  116.     client.publish("Temperatur", msgTemperatur);
  117.     client.publish("Feuchtigkeit", msgFeutigkeit);
  118.   }
  119. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top