├── BME280 Wiring.png ├── README.md └── temp_humi_press_sketch.ino /BME280 Wiring.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coding-with-craftsmen/esp8266-BME280/066bda211c4845caf22f205c59214f95d58c77c4/BME280 Wiring.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # esp8266-BME280 2 | This is an arduino example project for the ESP8266 MQTT Hackathon. 3 | The example uses the BME280 Sensor from Adafruit. 4 | 5 | Connect pin 4 from the ESP8266 to the SDI and pin 5 from the ESP8266 to the SCK pins of the BME280 sensor. The VIN should be connected to the 3V and the ground to the ground. 6 | 7 | ![My image](https://github.com/coding-with-craftsmen/esp8266-BME280/blob/master/BME280 Wiring.png) 8 | 9 | The libraries needed to run this project are: 10 | 11 | ESP8266WiFi
12 | Adafruit_MQTT
13 | Adafruit_MQTT_Client
14 | Adafruit_BME280 15 | 16 | The libraries can be imported through the Arduino IDE. Choose the Sketch/Include Library/Manage Liraries menu option to search and add the libraries. 17 | -------------------------------------------------------------------------------- /temp_humi_press_sketch.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define SEALEVELPRESSURE_HPA (1013.25) 9 | Adafruit_BME280 bme; // I2C 10 | //Adafruit_BME280 bme(BME_CS); // hardware SPI 11 | //Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); 12 | /************************* WiFi Access Point *********************************/ 13 | #define WLAN_SSID "*******" 14 | #define WLAN_PASS "*******" 15 | /************************* MQTT Broker Setup *********************************/ 16 | const int MQTT_PORT = 12665; 17 | const char MQTT_SERVER[] PROGMEM = "m11.cloudmqtt.com"; 18 | const char MQTT_CLIENTID[] PROGMEM = "ESP-PUBLISHER-SERVICE"; 19 | const char MQTT_USERNAME[] PROGMEM = "******"; 20 | const char MQTT_PASSWORD[] PROGMEM = "******"; 21 | 22 | WiFiClient client; 23 | 24 | 25 | Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_PORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD); 26 | 27 | /****************************** Feeds ***************************************/ 28 | const char TEMPERATURE_FEED[] PROGMEM = "sweethome/sensors/outdoor/temperature"; 29 | Adafruit_MQTT_Publish temperature_topic = Adafruit_MQTT_Publish(&mqtt, TEMPERATURE_FEED); 30 | const char PRESSURE_FEED[] PROGMEM = "sweethome/sensors/outdoor/pressure"; 31 | Adafruit_MQTT_Publish pressure_topic = Adafruit_MQTT_Publish(&mqtt, PRESSURE_FEED); 32 | 33 | const char HUMIDITY_FEED[] PROGMEM = "sweethome/sensors/outdoor/humidity"; 34 | Adafruit_MQTT_Publish humidity_topic = Adafruit_MQTT_Publish(&mqtt, HUMIDITY_FEED); 35 | 36 | /*************************** Sketch Code ************************************/ 37 | 38 | void setup() { 39 | Serial.begin(115200); 40 | delay(10); 41 | 42 | Serial.println("Sensor Test"); 43 | if (!bme.begin()) 44 | { 45 | Serial.print("Ooops, no BME280 detected ... Check your wiring or I2C ADDR!"); 46 | while (1); 47 | 48 | } 49 | else { 50 | Serial.println("BME280 ready."); 51 | } 52 | // Connect to WiFi access point. 53 | Serial.println(); 54 | Serial.print("Connecting to "); 55 | Serial.println(WLAN_SSID); 56 | WiFi.begin(WLAN_SSID, WLAN_PASS); 57 | while (WiFi.status() != WL_CONNECTED) { 58 | delay(500); 59 | Serial.print("."); 60 | 61 | } 62 | Serial.println(); 63 | 64 | Serial.println("WiFi connected"); 65 | Serial.println("IP address: "); Serial.println(WiFi.localIP()); 66 | } 67 | 68 | void MQTT_connect() { 69 | int8_t ret; 70 | // Stop if already connected. 71 | if (mqtt.connected()) { 72 | return; 73 | 74 | } 75 | Serial.print("Connecting to MQTT... "); 76 | while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected 77 | switch (ret) { 78 | case 1: Serial.println("Wrong protocol"); break; 79 | case 2: Serial.println("ID rejected"); break; 80 | case 3: Serial.println("Server unavailable"); break; 81 | case 4: Serial.println("Bad user/password"); break; 82 | case 5: Serial.println("Not authenticated"); break; 83 | case 6: Serial.println("Failed to subscribe"); break; 84 | default: Serial.print("Couldn't connect to server, code: "); 85 | Serial.println(ret); 86 | break; 87 | 88 | } 89 | Serial.println("Retrying MQTT connection in 5 seconds..."); 90 | mqtt.disconnect(); 91 | delay(5000);// wait 5 seconds 92 | 93 | } 94 | Serial.println("MQTT Connected!"); 95 | } 96 | 97 | void loop() { 98 | 99 | MQTT_connect(); 100 | 101 | 102 | float temperature = bme.readTemperature(); 103 | Serial.print("Temperature: "); 104 | Serial.print(temperature); 105 | Serial.println(" C"); 106 | Serial.print("Publish Temperature: "); 107 | if (! temperature_topic.publish(temperature)) { 108 | Serial.println("Failed"); 109 | 110 | } else { 111 | Serial.println("OK!"); 112 | 113 | } 114 | 115 | float pressure = bme.readPressure() / 100.0F; 116 | Serial.print("Pressure:    "); 117 | Serial.print(pressure); 118 | Serial.println(" hPa"); 119 | Serial.print("Publish Pressure: "); 120 | if (! pressure_topic.publish(pressure)) { 121 | Serial.println("Failed"); 122 | 123 | } else { 124 | Serial.println("OK!"); 125 | 126 | } 127 | 128 | float humidity = bme.readHumidity(); 129 | Serial.print("Humidity:    "); 130 | Serial.print(humidity); 131 | Serial.println(" %"); 132 | Serial.print("Publish Humidity: "); 133 | if (! humidity_topic.publish(humidity)) { 134 | Serial.println("Failed"); 135 | 136 | } else { 137 | Serial.println("OK!"); 138 | 139 | } 140 | 141 | delay(5000); 142 | } 143 | 144 | 145 | 146 | --------------------------------------------------------------------------------