Tarantula3DIYables
Published © GPL3+

Weather Station Using Arduino and NodeMCU

In this tutorial I am going to show you guys how to make an Arduino or NodeMCU based Weather Station using DHT11 or DHT22 and OLED Display

BeginnerProtip1 hour4,673
Weather Station Using Arduino and NodeMCU

Things used in this project

Story

Read more

Schematics

gerber_weather_station_v1_0_1_66CAdEtw4Q.zip

Code

Code_With_OLED_NodeMCU.ino

Arduino
/***************** DHT ******************/
#define DHTPIN D3
#include "DHTStable.h"        // DHT library test sketch for DHT22/DHT11 && Arduino "AUTHOR: Rob Tillaart"
                              // URL: https://github.com/RobTillaart/DHTstable
DHTStable DHT;                // Initialize the DHT Library
int HUM  = 0;
int TEMP = 0;
/***************************************/
/**************** OLED *****************/
#include <Wire.h>             // Only needed for Arduino 1.6.5 and earlier
#include "SSD1306.h"          // Alias for `#include "SSD1306Wire.h"`
                              // URL: https://github.com/squix78/esp8266-oled-ssd1306
SSD1306  display(0x3c, 5, 4); // Initialize the OLED display using Wire library
/***************************************/

void setup()
{
  Serial.begin(115200);
  display.init(); // Initialising the UI will init the display too.
  display.flipScreenVertically();
}

void loop() 
{ 
  /****** For DHT22 *******/
  //int chk = DHT.read22(DHTPIN);
  /************************/
  /****** For DHT11 *******/
  int chk = DHT.read11(DHTPIN);
  /************************/
  HUM  = DHT.getHumidity();
  TEMP = DHT.getTemperature();
  
  display.clear();
  drawDHT(); 
  display.display();
  delay (2000);
}

/*************** Generate OLED Display ***************/
void drawDHT() 
{
  int x=0; int y=0;
  display.setFont(ArialMT_Plain_10);
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.drawString(0 + x, 5 + y, "Hum");
  
  display.setFont(ArialMT_Plain_10);
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.drawString(38 + x, y, "OUTDOOR");

  display.setFont(ArialMT_Plain_24);
  String hum = String(HUM) + "%";
  display.drawString(0 + x, 15 + y, hum);
  int humWidth = display.getStringWidth(hum);
  Serial.println(hum);

  display.setFont(ArialMT_Plain_10);
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.drawString(96 + x, 5 + y, "Temp");

  display.setFont(ArialMT_Plain_24);
  String temp = String(TEMP) + "C";
  display.drawString(71 + x, 15 + y, temp);
  int tempWidth = display.getStringWidth(temp);
  Serial.println(temp);
}

Code_With_OLED_Arduino.ino

Arduino
/***************** DHT ******************/
#define DHTPIN 8
#include "DHTStable.h" // DHT library test sketch for DHT22/DHT11 && Arduino "AUTHOR: Rob Tillaart"
                       // URL: https://github.com/RobTillaart/DHTstable
DHTStable DHT;         // Initialize the DHT Library
int HUM  = 0;
int TEMP = 0;
/***************************************/
/**************** OLED *****************/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width,  in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
/***************************************/

void setup() 
{
  Serial.begin(115200);
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  };
}

void loop() 
{
  /****** For DHT22 *******/
  //int chk = DHT.read22(DHTPIN);
  /************************/
  /****** For DHT11 *******/
  int chk = DHT.read11(DHTPIN);
  /************************/
  HUM  = DHT.getHumidity();    // Read the humidity
  TEMP = DHT.getTemperature(); // Read the temperature

  /**** Display Data *****/
  Serial.print("Humidity");Serial.println(HUM);
  Serial.print("Temperature");Serial.println(TEMP);
  display.clearDisplay();
  oledDisplayHeader();
  oledDisplay(3,5,28,HUM,"%");  // Display humidity
  oledDisplay(2,70,16,TEMP,"C");// Display temperature 
  display.display();
  /************************/
  delay(2000); 
}

/*************** Generate OLED Header ***************/
void oledDisplayHeader()
{
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.print("Humidity");
  display.setCursor(60, 0);
  display.print("Temperature");
}

/*************** Generate OLED Display ***************/
void oledDisplay(int size, int x,int y, float value, String unit)
{
  int charLen = 12;
  int xo      = x + charLen*3.2;
  int xunit   = x + charLen*3.6;
  int xval    = x; 
  display.setTextSize(size);
  display.setTextColor(WHITE);
 
  if (unit=="%"){
    display.setCursor(x, y);
    display.print(value,0);
    display.print(unit);
  } else {
    if (value>99){ xval  = x; } 
    else         { xval = x + charLen; };
    display.setCursor(xval, y);
    display.print(value,0);
    display.drawCircle(xo, y + 2, 2, WHITE);  // Print degree symbols
    display.setCursor(xunit, y);
    display.print(unit);
  };
}

Credits

Tarantula3
68 projects • 85 followers
There were 1000+ sperms but I was the fastest one..
Contact
DIYables
0 projects • 87 followers
I would like to invite you to join and add your projects to DIYables platform https://www.hackster.io/diyables
Contact

Comments

Please log in or sign up to comment.