Making a mini Weather Station with NodeMCU and BMP280

Subscribe to my newsletter and never miss my upcoming articles

So I really wanted to kick off my Arduino (Code and Electronics) learning tour with some kind of tool which is actually useful. That's when I realized, that making a weather station could be really interesting. Now, to be honest, it wasn't that hard because: - wiring was available online - code (+ libraries) was/were available online - solutions to I2C address problems were online

So this isn't a "look what I made" post but more a "look how you can do that" post. I'll show you my wiring and the code (I modified a bit) to create a self-updating webpage which shows the temperature. Also, I'm going to link the tutorials I used. I'm not an expert in this stuff so don't blame me if something doesn't work or is inefficient.1

What you need

You need:

- 4x Jumper Cables (Female to Male) 
- a Breadboard 
- NodeMCU (V3) Board 
- BMP280 Sensor All of that can easily be found on eBay and Amazon.

The wiring

Before we start with coding, I'm going to show you how I wired the whole thing. It's important to wire the BMP280's:

VCC to 3V on NodeMCU
GND to G on NodeMCU
SCL to D1 on NodeMCU
SDA to D2 on NodeMCU

The software

You need to install the Arduino IDE. Then open the Libraries Manager in the IDE and add "Adafruit BMP280" and "Adafruit Unified Sensor Library" (names could have changed). Sometimes you also need to install the ESP8266 library and the ESP8266 Board Manager manually.

The code

After selecting a NodeMCU board in the "Boards" tab (in tools) you can verify and upload the following code. Thanks to the people listed below for the templates!

Remember to change YOURPASSand YOURSSID to your WIFI password and SSID. There is some DHT sensor stuff in there which you can theoretically remove.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "DHT.h"

// Uncomment one of the lines below for whatever DHT sensor type you're using!
//#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

/*Put your SSID & Password*/
const char* ssid = "YOURSSID";  // Enter SSID here
const char* password = "YOURPASS";  //Enter Password here

ESP8266WebServer server(80);

// DHT Sensor
uint8_t DHTPin = D8; 

// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);                

float Temperature;
float Humidity;

void setup() {
  Serial.begin(115200);
  delay(100);

  pinMode(DHTPin, INPUT);

  dht.begin();              

  Serial.println("Connecting to ");
  Serial.println(ssid);

  //connect to your local wi-fi network
  WiFi.begin(ssid, password);

  //check wi-fi is connected to wi-fi network
  while (WiFi.status() != WL_CONNECTED) {
  delay(1000);
  Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected..!");
  Serial.print("Got IP: ");  Serial.println(WiFi.localIP());

  server.on("/", handle_OnConnect);
  server.onNotFound(handle_NotFound);

  server.begin();
  Serial.println("HTTP server started");

}
void loop() {

  server.handleClient();

}

void handle_OnConnect() {

 Temperature = dht.readTemperature(); // Gets the values of the temperature
  Humidity = dht.readHumidity(); // Gets the values of the humidity 
  server.send(200, "text/html", SendHTML(Temperature,Humidity)); 
}

void handle_NotFound(){
  server.send(404, "text/plain", "Not found");
}

String SendHTML(float Temperaturestat,float Humiditystat){
  String ptr = "<!DOCTYPE html> <html>\n";
  ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  ptr +="<title>ESP8266 Weather Report</title>\n";
  ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
  ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
  ptr +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
  ptr +="</style>\n";
  ptr +="</head>\n";
  ptr +="<body>\n";
  ptr +="<div id=\"webpage\">\n";
  ptr +="<h1>ESP8266 NodeMCU Weather Report</h1>\n";

  ptr +="<p>Temperature: ";
  ptr +=(int)Temperaturestat;
  ptr +="°C</p>";
  ptr +="<p>Humidity: ";
  ptr +=(int)Humiditystat;
  ptr +="%</p>";

  ptr +="</div>\n";
  ptr +="</body>\n";
  ptr +="</html>\n";
  return ptr;
}

Access the Website

To access the website, open the serial monitor (top right, magnifying glass) and select 115200 as a baud rate. You should see a message which shows the IP. Access it using your browser. Remember that the website only works in the WIFI the NodeMCU is connected with. 2

Troubleshooting

If nothing is shown in the serial monitor, get an I2C address finder for Arduino (like this one), upload it to your NodeMCU and let it search there. Then, change the I2C address in the Adafruit BMP280 Library (can most likely be found in Documents/Arduino/libraries/Adafruit_BMP280_Library/Adafruit_BMP280.h). If you're not sure how to do that, google has many answers!

Credits

Thanks to: - Nick Gammon from gammon.com.au - Adafruit - The people from teachmemicro.com (teachmemicro.com/nodemcu-ajax-dynamic-senso.. and teachmemicro.com/display-sensor-data-nodemc..) - The ESP8266 people (github.com/esp8266) - The people from Arduino

See ya!

No Comments Yet