Remote Weather Tracking using NodeMCU and Thingspeak with DS18B20 and BMP280

Overview

Temperature plays an important role in any aspect. Example in preparing a meal or a food, temperature is important because it can affect the quality, flavor and freshness of the food. Another example is in wine and beer making, temperature plays an important part in the quality of the final product.

Barometric pressure has important effects on water chemistry and weather conditions. High barometric pressure supports sunny, clear, and favorable weather conditions, but lower levels promotes rainy and cloudy weather conditions. This atmospheric parameter has been used for hundreds of years to forecast weather conditions.

In this tutorial we made an online measuring or monitoring of Barometric Pressure and Temperature.

Here is the output in Thingspeak:

Note:

Thingspeak updates the data every 15 seconds.

Hardware Components


  • DS18B20 Temperature Sensor Probe
  • 
    
  • BMP280-3.3 Barometric Pressure Altitude Sensor
  • 
    
  • Jumper Wires (Male to Male)
  • 
    
  • Jumper Wires (Male to Female
  • 
    

    These items are available here at CreateLabz

    Software Components

    Application Discussion

    ESP8266 NodeMCU

    NodeMCU is a development board which runs on the ESP8266 with the Espressif Non-OS SDK, and hardware based on the ESP-12 module. The device features 4MB of flash memory, 80MHz of system clock, around 50k of usable RAM and an on chip Wifi Transceiver.

    ESP8266 can be used as an external Wifi module, using the standard AT Command set Firmware by connecting it to any microcontroller using the serial UART, or directly serve as a Wifi-enabled micro controller, by programming a new firmware using the provided SDK.

    DS18B20 Digital Temperature sensor

    The DS18B20 is a 1-wire programmable Temperature sensor from maxim integrated. It is widely used to measure temperature in hard environments like in chemical solutions, mines or soil etc. The constriction of the sensor is rugged and also can be purchased with a waterproof option making the mounting process easy. It can measure a wide range of temperature from -55°C to +125° with a decent accuracy of ±5°C. Each sensor has a unique address and requires only one pin of the MCU to transfer data so it a very good choice for measuring temperature at multiple points without compromising much of your digital pins on the microcontroller.

    How does it work:

    The sensor works with the method of 1-Wire communication. It requires only the data pin connected to the microcontroller with a pull up resistor and the other two pins are used for power. The pull-up resistor is used to keep the line in high state when the bus is not in use. The temperature value measured by the sensor will be stored in a 2-byte register inside the sensor. This data can be read by using the 1- wire method by sending in a sequence of data.

    BMP280 Barometric Pressure Altitude Sensor

    BMP280 is an environmental sensor with temperature, barometric pressure that is the next generation upgrade to the BMP085/BMP180/BMP183. This sensor is great for all sorts of weather sensing and can even be used in both I2C and SPI . This sensor has high accuracy and low cost making it an ideal solution for precision pressure measurements of up to ± 1 hPa and a temperature of up to ± 1.0 ° C. Because the pressure changes with altitude and pressure measurements are very accurate, you can use this sensor and the altimeter ± 1 meter accuracy.

    Thingspeak

    ThingSpeak is IoT Cloud platform where you can send sensor data to the cloud. You can also analyze and visualize your data with MATLAB or other software, including making your own applications.
    ThingSpeak includes a Web Service (REST API) that lets you collect and store sensor data in the cloud and develop Internet of Things applications. It works with Arduino, Raspberry Pi and MATLAB (premade libraries and APIs exists) But it should work with all kind of Programming Languages, since it uses a REST API and HTTP.

    In this tutorial, Thingspeak is integrated with Arduino by using an API key on the Arduino code. To setup an API key, a Thingspeak account must be opened first. The below procedure describes the setup process for Thingspeak.

     

    Software Setup

    Create an account in Thingspeak

    Go to Thingspeak.com

    Then click Sign Up.

    Fill in the necessary information, then click continue.

    Just check to use this email for Mathworks Account, the click continue.

    Verify your email, then click continue.

    Enter a User ID and Password. Click Continue.

    Just click OK.

    Select on how do you plan to use Thingspeak, then click OK.

    Click New Channel, to create your own channel.

    Enter the channel name and the field names, then click Save Channel.

    Next is click on API Keys to get the API Key.

    The Key above the Generate New Write API Key button is the API Key that we are going to use in our code.

    ESP8266 NodeMCU on Arduino IDE Setup

    Open your Arduino IDE, then click on the File and then click the Preferences. And copy the link below to the Additional Boards Manager URLs.

    http://arduino.esp8266.com/stable/package_esp8266com_index.json

    Then click OK.

    Next is click the Tools and then go to Board:, then click on the Board Manager.

    Enter ESP8266 in the search bar then click install.

     

    Hardware Setup

    DS18B20 Setup

    Connect the GND to the GND of the NodeMCU and the VCC to 3V3 of the NodeMCU. Connect the DQ to the GPIO 14 (D5) of the NodeMCU. And also place a 4.7k ohms resistor between the VCC and DQ of the DS18B20.

    BMP280 Setup

    Connect the SCK to GPIO 5 (D1) of the NodeMCU, next is connect the SDI to GPIO 4 (D2), then connect the CS to GPIO 0 (D3) and the SDO to the GPIO 2 (D4). And Connect the VCC to 3v3 and the GND to the GND of the NodeMCU.

     

    Code

    Libraries Included:

    Arduino Code

    #include <Wire.h>
    #include <SPI.h>
    #include <Adafruit_Sensor.h>
    #include <Adafruit_BMP280.h>
    #include <ESP8266WiFi.h>
    #include <OneWire.h>
    #include <DallasTemperature.h>
    
    #define myPeriodic 15
    #define ONE_WIRE_BUS D5
    #define BMP_SCK D1
    #define BMP_MISO D4
    #define BMP_MOSI D2
    #define BMP_CS D3
    
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature DS18B20(&oneWire);
    Adafruit_BMP280 bme(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);
    
    const char* server = "api.thingspeak.com";
    String apiKey = "API-Key";
    const char* MY_SSID = "WIFI-SSID";
    const char* MY_PWD = "WIFI-PASSWORD";
    int sent = 0;
    WiFiClient client;
    
    void setup() {
      Serial.begin(115200);
      Serial.println(F("BMP280 test"));
      if (!bme.begin()) {
        Serial.println("Could not find a valid BMP280 sensor, check wiring!");
        while (1);
      }
      connectWifi();
    }
    
    void loop() {
      float temp;
      float pressure;
      DS18B20.requestTemperatures();
      temp = DS18B20.getTempCByIndex(0);
      pressure = bme.readPressure() / 1000;
      Serial.print(String(sent) + " Temperature: ");
      Serial.println(temp);
      Serial.print(" Pressure= ");
      Serial.print(pressure);
      Serial.println(" KPa");
    
      if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com
      {
        String postStr = apiKey;
        postStr += "&field1=";
        postStr += String(temp);
        postStr += "&field2=";
        postStr += String(pressure);
        postStr += "\r\n\r\n";
    
        client.print("POST /update HTTP/1.1\n");
        client.print("Host: api.thingspeak.com\n");
        client.print("Connection: close\n");
        client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
        client.print("Content-Type: application/x-www-form-urlencoded\n");
        client.print("Content-Length: ");
        client.print(postStr.length());
        client.print("\n\n");
        client.print(postStr);
      }
      sent++;
      client.stop();
    
      int count = myPeriodic;
      while (count--)
        delay(1000);
    
    
    }
    
    void connectWifi()
    {
      Serial.print("Connecting to " + *MY_SSID);
      WiFi.begin(MY_SSID, MY_PWD);
      while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.print(".");
      }
    
      Serial.println("");
      Serial.println("Connected");
      Serial.println("");
    }

    Note:

    Change the “API-key” to your own API Key in your Thingspeak account. And “WIFI-SSID ” and “WIFI-PASSWORD” to your own Wifi network SSID and PASSWORD.

    Code Breakdown

    #define myPeriodic 15
    #define ONE_WIRE_BUS D5
    #define BMP_SCK D1
    #define BMP_MISO D4
    #define BMP_MOSI D2
    #define BMP_CS D3

    This part of the code is where we assign the SPI bus to the NodeMCU pins. And the timer we set for 15 seconds. But you can change the timer, but the minimum interval is 15 seconds, since Thingspeak updates the data every 15 seconds.

    void setup() {
      Serial.begin(115200);
      Serial.println(F("BMP280 test"));
      if (!bme.begin()) {
        Serial.println("Could not find a valid BMP280 sensor, check wiring!");
        while (1);
      }
      connectWifi();
    }

    In this code we begin the serial communication and set the baud rate to 115200 bps and check if the BMP280 is working. And then call the connectWifi() function.

    float temp;
      float pressure;
      DS18B20.requestTemperatures();
      temp = DS18B20.getTempCByIndex(0);
      pressure = bme.readPressure() / 1000;
      Serial.print(String(sent) + " Temperature: ");
      Serial.println(temp);
      Serial.print(" Pressure= ");
      Serial.print(pressure);
      Serial.println(" KPa");

    In this code we start reading the data that we get from DS18B20 and BMP280 and store it in the temp and pressure variables. And then display the temperature and pressure values in the serial monitor.

    if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com
      {
        String postStr = apiKey;
        postStr += "&field1=";
        postStr += String(temp);
        postStr += "&field2=";
        postStr += String(pressure);
        postStr += "\r\n\r\n";
    
        client.print("POST /update HTTP/1.1\n");
        client.print("Host: api.thingspeak.com\n");
        client.print("Connection: close\n");
        client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
        client.print("Content-Type: application/x-www-form-urlencoded\n");
        client.print("Content-Length: ");
        client.print(postStr.length());
        client.print("\n\n");
        client.print(postStr);
      }

    In this code, we check if we were able to connect to the thingspeak server, then sends our data to the server. We display the temperature data’s on the field 1 of our channel in thingspeak and the pressure data’s on the field 2.

    int count = myPeriodic;
      while (count--)
        delay(1000);

    This code is our timer, myPeriodic is equal to 15. Therefore we have a 15 seconds timer, before the loop can run again.

    void connectWifi()
    {
      Serial.print("Connecting to " + *MY_SSID);
      WiFi.begin(MY_SSID, MY_PWD);
      while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.print(".");
      }
    
      Serial.println("");
      Serial.println("Connected");
      Serial.println("");
    }

    This code is the connectWifi() function. In this code we connect our device to the Wifi and check if we are able to connect.

    Conclusion

    IoT allows us to use affordable wireless technology and transmit the data into the cloud at a component level. It also provides a place to save data as well as management and security. Like in this project we were able to track or monitor the weather easily, anywhere as long as an internet connection is available.

    Reference

    [1] https://blog.hannainst.com/the-importance-of-temperature/

    [2] https://www.fondriest.com/news/barometricpressure.htm

    [3]https://components101.com/sensors/ds18b20-temperature-sensor

    [4] https://www.openimpulse.com/blog/products-page/product-category/bmp280-barometric-pressure-sensor-module-2/

    [5] https://en.wikipedia.org/wiki/NodeMCU

    [6] http://archive.fabacademy.org/archives/2016/doc/networking-esp8266.html

    The post Remote Weather Tracking using NodeMCU and Thingspeak with DS18B20 and BMP280 appeared first on CreateLabz.

    Barometric pressureBarometric pressure sensorBmp280Ds18b20Esp8266IotKnowledgebaseNodemcuRemote weather trackingTemperatureTemperature sensorThingspeak

    Leave a comment

    All comments are moderated before being published