IoT Wireless Weather Station using Arduino, ESP8266 and ThingSpeak

IoT based Wireless Weather Station using Arduino, ESP8266, and ThingSpeak

Global warming has lead to unpredictable climates; researchers around the world are using weather stations to observe record and analyze weather patterns to study climate changes and provide weather forecasts. These Weather stations normally comprise of few sensors to measure environmental parameters and a monitoring or logging system to analyze these parameters. In this tutorial, we will learn how to build a wireless IoT-based weather station that can measure critical environmental parameters like Temperature, Humidity, and Pressure. Also since our weather station is IoT enabled, we can send these parameters to a ThingSpeak channel (IoT cloud) where we can store, analyze, and access the data remotely. We have also built a similar weather station using Raspberry Pi earlier, which is pretty much similar to this project. 

 

We will be using the Arduino board along with the DHT11 sensor, BMP180 sensor, and an ESP8266 wifi module. The DHT11 sensor senses the temperature and humidity, while the BMP180 sensor calculates the pressure, and ESP8266 is used for internet connectivity. In our previous project, we already learned to use the DHT11 sensor to monitor temperature and humidity with Arduino, here in this project, we are adding another sensor (BMP180) to make a complete weather station using Arduino. Sending these data to ThingSpeak enables live monitoring from anywhere in the world and we can also view the logged data which will be stored on their website and even graph it over time to analyze it.

 

Components Required

  • Arduino Uno
  • ESP8266 Wi-Fi Shield
  • DHT11 Sensor
  • BMP180 Sensor
  • Breadboard
  • Jumper Wires

 

Circuit Diagram

The complete circuit for Arduino based IoT Weather Station is shown below.

Arduino Weather Station Circuit Diagram

The DHT11 sensor is powered by the 5V pin of the Arduino and its data pin is connected to pin 5 for one-wire communication. The BMP180 sensor is powered by the 3.3V pin of Arduino and its data pins SCL (Serial Clock) and SDA (Serial Data) are connected to the A4 and A5 pin of Arduino for I2C communication.

 

The ESP8266 module is also powered by the 3.3V pin of the Arduino and its Tx and Rx pins are connected to Digital pins 2 and 3 of Arduino for serial communication. You can use the below table as a reference for making your connections.

S.NO.

Pin Name

Arduino Pin

1

ESP8266 VCC

3.3V

2

ESP8266 RST

3.3V

3

ESP8266 CH-PD

3.3V

4

ESP8266 RX

TX

5

ESP8266 TX

RX

6

ESP8266 GND

GND

7

BMP180 VCC

5V

8

BMP180 GND

GND

9

BMP180 SDA

A4

10

BMP180 SCL

A5

11

DHT-11 VCC

5V

12

DHT-11 Data

5

13

DHT-11 GND

GND

IoT Wireless Weather Station using Arduino

 

Setting up your ThingSpeak Channel

ThingSpeak is an open data platform that allows you to aggregate, visualize, and analyze live data in the cloud. You can control your devices using ThingSpeak, you can send data to ThingSpeak from your devices, and even you can create instant visualizations of live data, and send alerts using web services like Twitter and Twilio. ThingSpeak has integrated support from the numerical computing software MATLAB. MATLAB allows ThingSpeak users to write and execute MATLAB code to perform preprocessing, visualizations, and analyses. ThingSpeak takes a minimum of 15 seconds to update your readings. We have also done other interesting projects with ThingSpeak like-

 

Step 1: ThingSpeak Account Setup

To create a channel on ThingSpeak, first, you need to Sign up on ThingSpeak. In case if you already have an account on ThingSpeak, sign in using your id and password.

For creating your account go to www.thinspeak.com.

ThingSpeak

 

Click on Sing up if you don’t have an account and if you already have an account, click on sign in. After clicking on signup, fill in your details.

ThingSpeak Sign up

After this, verify your E-mail id and click on continue.

 

Step 2: Create a Channel for Your Data

Once you Sign in after your account verification, Create a new channel by clicking the “New Channel” button.

Create Channel for Weather Station On ThingSpeak

 

After clicking on “New Channel,” enter the Name and Description of the data you want to upload on this channel.

Enter the name of your data ‘Humidity’ in Field1, ‘Temp’ in Field2, and ‘Pressure’ in Field3. If you want to use more Fields, you can check the box next to the Field option and enter the name and description of your data.

After this, click on the save channel button to save your details.

 

Step 3: API Key

To send data to ThingSpeak, we need a unique API key, which we will use later in our code to upload our sensor data to Thingspeak Website.

Click on the “API Keys” button to get your unique API key for uploading your sensor data.

ThingSpeak API Key for Weather Station

Now copy your “Write API Key.” We will use this API key in our code.

 

Code Explanation

The programming part plays a very important role to perform all the operations in a project. As usual, complete code is given at the end. Start the code by including all the required libraries and defining all the variables.

#include <WiFi.h>
#include <DHT.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <stdlib.h>
#include <SFE_BMP180.h>

 

After this, enter the Wi-Fi name, password of your Wi-Fi router, and then also enter the API key that you copied from the ThingSpeak channel.

#define ssid  "Enter Your WiFi Name Here "     // "WiFi Name"
#define pass "WiFi Password"       // "Password"
#define server = "api.thingspeak.com";
String apiKey ="Enter the API Key";

 

In the void setup() function, it connects with the Wi-Fi and starts the BMP180 and DHT11 sensors.

void setup() {                
  Wire.begin();
  pressure.begin();
  // enable debug serial

  Serial.begin(9600);
       delay(10);
       dht.begin();

Serial.begin(9600); 
  Serial.println("AT");
  delay(5000);
  if(Serial.find("OK")){
    connectWiFi();

 

Using the void Transmission() function, we calculate the temperature, humidity, and pressure using the BMP180 and DHT11 sensors.

void Trsmission()
{
  int8_t h = dht.readHumidity(); 
  int16_t t = dht.readTemperature(TEMPTYPE); 
  char status;
  double T,P,p0,a;
  status = pressure.startTemperature();
  if (status != 0)
  {
    delay(status);
    status = pressure.getTemperature(T);
    if (status != 0)
    {

      status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);
………………………………………………………………..
………………………………………………………………..

 

These commands are used to connect with the ThingSpeak server and then print the temperature, humidity, and pressure values in different fields.

String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += "184.106.153.149"; // api.thingspeak.com
  cmd += "\",80";
  ser.println(cmd);

 if(ser.find("Error")){
    Serial.println("AT+CIPSTART error");
    return;
…………………………………….  

  // prepare GET string
  String getStr = "GET /update?api_key=";
  getStr += apiKey;
  getStr +="&field1=";
  getStr += String(strTemp);
  getStr +="&field2=";
  getStr += String(strHumid);
  getStr +="&field3=";
  getStr += String(strPres);
  getStr += "\r\n\r\n";

 

Running the IoT Arduino Weather Station

Now connect the Arduino with the laptop and choose the board and port correctly and then click the Upload button. After uploading the code, open the serial monitor. Make the baud rate of the serial monitor 9600. You will see your Wi-Fi Id, password, and temperature, humidity, and pressure values on the serial monitor.

Arduino Output for IoT Wireless Weather Station

 

Now navigate to the ThingSpeak channel and check your channel, you will see the temperature, humidity, and pressure values as shown in the below graphs.

Thingspeak Output for IoT Wireless Weather Station

This is how you can build Arduino Weather Station where the temperature, humidity, and pressure can be monitored from anywhere in the world over the internet. 

Code

#include <WiFi.h>
#include <DHT.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <stdlib.h>
#include <SFE_BMP180.h>
SFE_BMP180 pressure;
#define DHTPIN 5
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#define TEMPTYPE 0 
#define ALTITUDE 160 // Altitude from Bussero (MI) Italy
#define ssid  "Enter Your WiFi Name Here "     // "WiFi Name"
#define pass "WiFi Password"       // "Password"
#define server = "api.thingspeak.com";
String apiKey ="Enter the API Key"; 
char buffer[10];
char t_buffer[10];
char h_buffer[10];
char P_buffer[10];

SoftwareSerial ser(2, 3); // RX, TX
void setup() {                
  Wire.begin();
  pressure.begin();
  // enable debug serial
  
  Serial.begin(9600); 
  Serial.println("AT");
  delay(5000);
  if(Serial.find("OK")){
    connectWiFi();

}
void loop()

  Trsmission(); // ESP8266
  delay(60000); // 60 seconds
    }


void Trsmission()
{
  int8_t h = dht.readHumidity(); 
  int16_t t = dht.readTemperature(TEMPTYPE); 
  char status;
  double T,P,p0,a;
  status = pressure.startTemperature();
  if (status != 0)
  {
    delay(status);
    status = pressure.getTemperature(T);
    if (status != 0)
    {
     
      status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);

        status = pressure.getPressure(P,T);
        if (status != 0)
        {
 
          p0 = pressure.sealevel(P,ALTITUDE); // we're at 1655 meters (Boulder, CO)
          a = pressure.altitude(P,p0);
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }

  float temp = t;
  float humidity = h;
  float Pression = p0;

  String strTemp = dtostrf(temp, 4, 1, t_buffer);
  String strHumid = dtostrf(humidity, 4, 1, h_buffer);
  String strPres = dtostrf(Pression, 4, 2, P_buffer);

  Serial.print("Temperature: ");
  Serial.println(strTemp);
  Serial.print("Humidity: ");
  Serial.println(strHumid);
  Serial.print("Pression: ");
  Serial.println(strPres);
  
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += "184.106.153.149"; // api.thingspeak.com
  cmd += "\",80";
  ser.println(cmd);
  if(ser.find("Error")){
    Serial.println("AT+CIPSTART error");
    return;
  } 
  if(ser.find("Error")){
    Serial.println("AT+CIPSTART error");
    return;
  }
  
  // prepare GET string
  String getStr = "GET /update?api_key=";
  getStr += apiKey;
  getStr +="&field1=";
  getStr += String(strTemp);
  getStr +="&field2=";
  getStr += String(strHumid);
  getStr +="&field3=";
  getStr += String(strPres);
  getStr += "\r\n\r\n";

  // send data length
   cmd = "AT+CIPSEND=";
  cmd += String(getStr.length());
  ser.println(cmd);
  //ser.print(getStr);
if(ser.find(">")){
    ser.print(getStr);
  }
  else{
    ser.println("AT+CIPCLOSE");
    // alert user
    Serial.println("AT+CIPCLOSE");
    ser.println("AT+RST");
  }
  
  char buffer[10] = "";
 }

27 Comments

Why is "connectWifi()" not declared in this scope? I already built the hardware correctly (Already double checked), and the Arduino IDE provided isn't working.

Would like help as soon as possible please. Thanks

Hi, 

connectwifi() function is missing in the code. add the below lines in your and it will work

boolean connectWiFi() {
  Serial.println("AT+CWMODE=1");
  ser.println("AT+CWMODE=1");
  delay(2000);
  String cmd="AT+CWJAP=\"";
  cmd+=SSID;
  cmd+="\",\"";
  cmd+=PASS;
  cmd+="\"";
  Serial.println(cmd);
  ser.println(cmd);
  delay(5000);
  if(ser.find("OK"))  {
    Serial.println("OK");
    return true;    
  }else {
    return false;
  }
}
 

Hi, 

connectwifi() function is missing in the code. add the below lines in your and it will work

boolean connectWiFi() {
  Serial.println("AT+CWMODE=1");
  ser.println("AT+CWMODE=1");
  delay(2000);
  String cmd="AT+CWJAP=\"";
  cmd+=SSID;
  cmd+="\",\"";
  cmd+=PASS;
  cmd+="\"";
  Serial.println(cmd);
  ser.println(cmd);
  delay(5000);
  if(ser.find("OK"))  {
    Serial.println("OK");
    return true;    
  }else {
    return false;
  }
}

can some one please post the correct code

I dont know where to add the WiFi fix above

If I delete the "connectWiFi();" - it still does not compile (see errors below) - Im a novice - any assistance appreciated

/Users/nickz/Google Drive/PROJECTS/WeatherStation/sketch_jun14a/sketch_jun14a.ino: In function 'void setup()':
/Users/nickz/Google Drive/PROJECTS/WeatherStation/sketch_jun14a/sketch_jun14a.ino:31:22: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
if(Serial.find("OK")){
^
sketch_jun14a:35:1: error: a function-definition is not allowed here before '{' token
{
^
sketch_jun14a:42:1: error: a function-definition is not allowed here before '{' token
{
^
sketch_jun14a:131:2: error: expected '}' at end of input
}
^
Multiple libraries were found for "SPI.h"
Used: /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SPI
Multiple libraries were found for "Adafruit_Sensor.h"
Used: /Users/nickz/Documents/Arduino/libraries/Adafruit_Unified_Sensor
Multiple libraries were found for "WiFi.h"
Used: /Applications/Arduino.app/Contents/Java/libraries/WiFi
Multiple libraries were found for "DHT.h"
Used: /Users/nickz/Documents/Arduino/libraries/DHT_sensor_library
Multiple libraries were found for "Wire.h"
Used: /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/Wire
Multiple libraries were found for "SoftwareSerial.h"
Used: /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/SoftwareSerial
Multiple libraries were found for "SFE_BMP180.h"
Used: /Users/nickz/Documents/Arduino/libraries/src
exit status 1
a function-definition is not allowed here before '{' token

Hi Asheesh
I too am struggling with the code. I've picked this project as my first real-world build as I have a need for a weather station and it's good to work on something with a practical use rather than something abstract.

The function-definition and uppercase-lowercase errors is where I'm having problems. Could you possibly re-post the corrected code? In the meantime I'm going to attempt your "IoT Based Temperature and Humidity Monitoring over ThingSpeak using Arduino UNO and ESP8266".

Many thanks!

Ades

I am just writing to let you be aware of what a exceptional experience my friend's child found visiting yuor web blog. She even learned some pieces, which included what it is like to have a wonderful helping nature to let many people with no trouble understand certain very confusing subject matter. You actually did more than my expected results. Thanks for displaying those priceless, safe, explanatory and also unique guidance on your topic to Ethel.

I precisely desired to say thanks yet again. I'm not certain the things that I would have handled in the absence of these tactics discussed by you over such a problem. It actually was an absolute scary difficulty for me, however , witnessing your expert approach you processed that forced me to weep with fulfillment. I'm just happy for the support and then trust you find out what a great job you happen to be getting into teaching the mediocre ones with the aid of your web page. I am certain you have never met all of us.

I want to voice my affection for your kindness for those who really want help with this particular concern. Your personal dedication to passing the message up and down was remarkably good and has frequently allowed individuals just like me to realize their desired goals. Your personal useful guideline entails this much to me and much more to my peers. Thank you; from each one of us.

I precisely needed to thank you so much all over again. I'm not certain the things that I would have gone through without the points shared by you regarding this situation. Completely was the horrifying circumstance for me personally, but seeing this specialised mode you managed it took me to leap for fulfillment. Now i'm thankful for the assistance as well as expect you really know what an amazing job that you're getting into instructing people today with the aid of your blog post. Probably you have never met all of us.

I would like to voice my affection for your kindness giving support to men and women who should have help on this one theme. Your personal dedication to getting the solution all over appears to be especially advantageous and have surely allowed guys just like me to reach their targets. Your interesting guidelines can mean this much a person like me and extremely more to my fellow workers. With thanks; from each one of us.

I needed to create you the little note in order to say thank you again for your personal nice tricks you've discussed at this time. It's simply unbelievably generous of you to offer extensively just what many of us might have marketed for an ebook to help make some cash for their own end, certainly considering the fact that you could possibly have done it in case you decided. These points additionally acted to be a great way to realize that the rest have a similar passion just like my own to grasp much more on the topic of this condition. Certainly there are a lot more pleasant moments ahead for individuals that looked at your blog post.

I am glad for writing to make you be aware of of the wonderful discovery our princess enjoyed browsing your webblog. She figured out plenty of details, not to mention what it's like to have an excellent helping mindset to get other people smoothly grasp selected advanced matters. You really did more than readers' desires. Thank you for presenting these essential, dependable, educational and as well as cool tips on the topic to Gloria.

I'm commenting to make you understand what a amazing experience my cousin's daughter encountered checking your webblog. She realized a lot of pieces, which included what it is like to have an ideal coaching mood to have the mediocre ones easily grasp various tricky issues. You really did more than visitors' expectations. I appreciate you for offering the important, healthy, explanatory and in addition unique thoughts on the topic to Gloria.

My spouse and i were very joyous Ervin managed to carry out his web research with the ideas he got through your web pages. It's not at all simplistic to just be giving out information and facts which often the others have been trying to sell. So we acknowledge we have got the website owner to thank because of that. The illustrations you made, the straightforward website navigation, the friendships you will give support to instill - it's mostly exceptional, and it's making our son and our family do think this matter is brilliant, which is incredibly pressing. Thank you for all the pieces!

Thank you so much for giving everyone such a superb chance to read from this site. It's always so useful and jam-packed with a great time for me personally and my office acquaintances to search your blog nearly three times in 7 days to learn the latest items you have got. And of course, I am usually pleased with the tremendous opinions served by you. Certain 3 points in this post are in truth the very best I've had.

I'm also commenting to let you know what a helpful experience my cousin's girl found browsing your site. She came to understand such a lot of things, which included what it is like to possess a very effective teaching nature to get the others really easily completely grasp a variety of very confusing subject areas. You undoubtedly did more than our own expectations. I appreciate you for rendering those effective, dependable, revealing as well as easy thoughts on that topic to Evelyn.

I simply had to say thanks yet again. I'm not certain the things that I could possibly have achieved in the absence of the actual opinions provided by you directly on that industry. It was actually a very scary concern in my circumstances, nevertheless seeing a new expert mode you treated that took me to weep for gladness. I am happier for this help and thus pray you really know what an amazing job you have been doing training many others through your blog post. I am certain you've never come across any of us.

Needed to create you this tiny word so as to thank you yet again for these splendid thoughts you've documented at this time. It is certainly wonderfully open-handed of you to convey unhampered just what some people could possibly have marketed as an ebook in order to make some bucks for themselves, even more so given that you might well have tried it if you ever considered necessary. The concepts in addition served like a fantastic way to comprehend the rest have similar keenness similar to my very own to know more and more regarding this issue. I know there are numerous more pleasurable moments up front for many who check out your site.

I'm writing to make you understand what a terrific discovery our daughter enjoyed reading through your web site. She learned such a lot of things, not to mention how it is like to possess an ideal giving mindset to have others effortlessly have an understanding of a variety of complicated subject matter. You truly surpassed people's expected results. Thanks for supplying the good, safe, edifying and in addition easy guidance on that topic to Jane.

A lot of thanks for each of your labor on this website. My daughter delights in setting aside time for investigations and it's easy to see why. We learn all relating to the compelling tactic you give precious things on your web blog and therefore welcome response from visitors on that area while our girl is undoubtedly learning a lot. Have fun with the remaining portion of the year. You are always doing a superb job.

I wanted to make a small remark to say thanks to you for those splendid concepts you are posting on this website. My long internet investigation has at the end been honored with high-quality facts and techniques to share with my contacts. I would assume that we readers actually are undeniably endowed to dwell in a fabulous network with very many awesome people with insightful advice. I feel truly blessed to have used the webpage and look forward to so many more excellent times reading here. Thank you again for everything.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.