Advertisement
Skillkiller

MQTT BME280

Sep 10th, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.50 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <SPI.h>
  3. #include <ESP8266WiFi.h>
  4. #include <PubSubClient.h>
  5. #include <Adafruit_Sensor.h>
  6. #include <Adafruit_BME280.h>
  7.  
  8. #define BME_SCK 13
  9. #define BME_MISO 12
  10. #define BME_MOSI 11
  11. #define BME_CS 10
  12.  
  13. const float cToKOffset = 273.15;
  14.  
  15. Adafruit_BME280 bme; // I2C
  16.  
  17. WiFiClient espClient;
  18. PubSubClient client(espClient);
  19.  
  20. String zimmer = "Sebastian Zimmer";
  21.  
  22. const char* ssid = "Dev-Network";
  23. const char* password = "----";
  24. const char* mqtt_server = "192.168.1.109";
  25. const char* mqtt_user = "mqtt";
  26. const char* mqtt_pass = "mqtt";
  27. const char* mqtt_root = "/Zimmer/Sebastians Zimmer";
  28.  
  29. String tversion = "1.0";
  30. #define ALTITUDE 164 //Höhe über dem Meer
  31.  
  32. #define CONFIG_MQTT_TOPIC_GET "/get"
  33. #define CONFIG_MQTT_TOPIC_GET_TEMP "/temperature"
  34. #define CONFIG_MQTT_TOPIC_GET_DEW "/dewpoint"
  35. #define CONFIG_MQTT_TOPIC_GET_HUM "/humidity_abs"
  36. #define CONFIG_MQTT_TOPIC_GET_HUMR "/humidity"
  37. #define CONFIG_MQTT_TOPIC_GET_PRES "/pressure"
  38. #define CONFIG_MQTT_TOPIC_GET_PRESR "/pressure_rel"
  39.  
  40. class PubSubClientWrapper : public PubSubClient{
  41.   private:
  42.   public:
  43.     PubSubClientWrapper(Client& espc);
  44.     bool publish(StringSumHelper topic, String str);
  45.     bool publish(StringSumHelper topic, unsigned int num);
  46.     bool publish(const char* topic, String str);
  47.     bool publish(const char* topic, unsigned int num);
  48.  
  49.     bool publish(StringSumHelper topic, String str, bool retain);
  50.     bool publish(StringSumHelper topic, unsigned int num, bool retain);
  51.     bool publish(const char* topic, String str, bool retain);
  52.     bool publish(const char* topic, unsigned int num, bool retain);
  53. };
  54.  
  55. void setup() {
  56.   // put your setup code here, to run once:
  57.   Serial.begin(9600);
  58.   Serial.setTimeout(20);
  59.  
  60.     // Wait for serial to initialize.
  61.   while(!Serial) {
  62.     delay(1);
  63.   }
  64.  
  65.   Serial.println("Device started!");
  66.   Serial.println("----------------------------------------");
  67.   Serial.print("Temperatur Messung ");
  68.   Serial.println(tversion);
  69.   Serial.print("MQTT: ");
  70.   Serial.println(mqtt_root);
  71.   Serial.println("----------------------------------------");
  72.   Serial.println();
  73.  
  74.   Serial.print("Verbinde Wlan");
  75.   WiFi.begin(ssid, password);
  76.   int t = 12;
  77.   while (WiFi.status() != WL_CONNECTED)
  78.   {
  79.     delay(500);
  80.     if ((t % 29) == 0) {
  81.       Serial.println();
  82.     }
  83.     Serial.print(".");
  84.     t = t + 1;
  85.  
  86.     if(t == 150) {
  87.       Serial.println();
  88.       Serial.print("Wlan nicht verfügbar. Erneuter Versuch in 5 min");
  89.       ESP.deepSleep(300e6);
  90.     }
  91.   }
  92.  
  93.   Serial.println("");
  94.   Serial.println("WiFi connected");
  95.   Serial.println("IP address: ");
  96.   Serial.println(WiFi.localIP());
  97.  
  98.   Serial.println();
  99.   client.setServer(mqtt_server, 1883);
  100.   Serial.print("Attempting MQTT connection");
  101.   String clientId = "ESP-";
  102.   clientId += zimmer;
  103.   if (client.connect(clientId.c_str(), mqtt_user, mqtt_pass)) {
  104.     Serial.println("connected");
  105.   }
  106.  
  107.   bool status;
  108.    
  109.   // default settings
  110.   // (you can also pass in a Wire library object like &Wire2)
  111.   status = bme.begin(0x76);  
  112.   if (!status) {
  113.       Serial.println("Could not find a valid BME280 sensor, check wiring!");
  114.       while (1);
  115.   }
  116. }
  117.  
  118. void loop() {
  119.   // put your main code here, to run repeatedly:
  120.   float temperature = bme.readTemperature();
  121.   float humidity_r = bme.readHumidity();
  122.   float humidity = absoluteHumidity(temperature, humidity_r);
  123.   float pressure_r = bme.readPressure() / 100.0F;
  124.   float pressure = bme.seaLevelForAltitude(ALTITUDE, pressure_r);
  125.  
  126.   client.publish(((String)mqtt_root + CONFIG_MQTT_TOPIC_GET + CONFIG_MQTT_TOPIC_GET_TEMP), (String)temperature);
  127.   client.publish(((String)mqtt_root + CONFIG_MQTT_TOPIC_GET + CONFIG_MQTT_TOPIC_GET_DEW), (String)dew);
  128.   client.publish(((String)mqtt_root + CONFIG_MQTT_TOPIC_GET + CONFIG_MQTT_TOPIC_GET_HUM), (String)humidity);
  129.   client.publish(((String)mqtt_root + CONFIG_MQTT_TOPIC_GET + CONFIG_MQTT_TOPIC_GET_HUMR), (String)humidity_r);
  130.   client.publish(((String)mqtt_root + CONFIG_MQTT_TOPIC_GET + CONFIG_MQTT_TOPIC_GET_PRES), (String)pressure);
  131.   client.publish(((String)mqtt_root + CONFIG_MQTT_TOPIC_GET + CONFIG_MQTT_TOPIC_GET_PRESR), (String)pressure_r);
  132.  
  133.  
  134.   Serial.println();
  135.   ESP.deepSleep(30e6);
  136. }
  137.  
  138. // Relative to absolute humidity
  139. // Based on https://carnotcycle.wordpress.com/2012/08/04/how-to-convert-relative-humidity-to-absolute-humidity/
  140. float absoluteHumidity(float temperature, float humidity) {
  141.   return (13.2471*pow(EULER,17.67*temperature/(temperature+243.5))*humidity/(cToKOffset+temperature));
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement