Adrian Shurmer
Published © GPL3+

Environment Monitor Using NodeMcu ESP8266

Using a NodeMcu ESP8266 programmed using Arduino IDE to collect data and send to ThingSpeak.

BeginnerWork in progress24 hours2,267
Environment Monitor Using NodeMcu ESP8266

Things used in this project

Hardware components

Geekcreit Doit NodeMCU ESP8266
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
GY-68 BMP180 Breakout
×1

Software apps and online services

Arduino IDE
Arduino IDE
ThingSpeak Online Service

Story

Read more

Code

Sensor Project Code

Arduino
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <DHT.h>
#define DHTPIN 2 // SO CONNECT THE DHT11/22 SENSOR TO PIN D4 OF THE NODEMCU
#include <ThingSpeak.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#define SLEEP_DELAY_IN_SECONDS 60*30 // every 30 mins 
/* 
   Connections to BMP180
   ====================
   Connect SCL to  D1
   Connect SDA to  D2
   Connect VDD to 3.3V DC
   Connect GROUND to common ground

    Connections to DH11
   ====================
   Connect Signal pin to D4
   Connect middle pin, usually V+ to 3v3 DC pin on NodeMcu
   Connect - pin to GROUND on NodeMcu

*/
//declare some variables
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

char *ssid = "your networks name here";    //  your network SSID (name) 
char *pass = "your network password here";   // your network password
int status = WL_IDLE_STATUS;
WiFiClient  client;
float num = 0;
ESP8266WiFiMulti WiFiMulti;
DHT dht(DHTPIN, DHT11,15);
unsigned long myChannelNumber = your thingspeak channel number; // my thing speak account channel number
const char * myWriteAPIKey = "your thingspeak apikey here";// my thing speak apikey


void setup() {
    Serial.begin(115200); // so we can out put to the screen
    delay(10);

    // We start by connecting to a WiFi network
    WiFiMulti.addAP(ssid, pass);

    Serial.println();
    Serial.println();
    Serial.print("Waiting for WiFi to connect...");

    while(WiFiMulti.run() != WL_CONNECTED) {
    Serial.print(".");
        delay(500);
    }
    // give some feed back that we are connected to router
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    delay(500);

    // connect to thingspeak.com
    if (ThingSpeak.begin(client)) {
    //feed back that we have connected to thingspeak
    Serial.println("connected to ThingSpeak");
    
    dht.begin();
    if(!bmp.begin())
    {
    /* There was a problem detecting the BMP085 ... check your connections */
    Serial.print("Ooops, no BMP180 detected ... Check your wiring or I2C ADDR!");
    while(1);
    }
   
    /* Display some basic information on the BMP180 sensor */
    displaySensorDetails();
    }
    //or we have not!
    else  Serial.println("not connected to ThingSpeak");
    }

// set up complete
 
 
 void loop() {

 
    float pressure = fetchPressure(); /*Get the current pressure from the BMP085 */
    Serial.print("Pressure:    ");
    Serial.print(pressure);
    Serial.println(" mBar");
        
    /* First we get the current temperature from the BMP085 */
    float temperature = fetchTemperature();
    //bmp.getTemperature(&temperature);
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.println(" C");
    
    float humidity = fetchHumidity(); //get humidity from DH11 sensor
    Serial.print("Humidity: ");
    Serial.println(humidity);

    float dewPoint = fetchDewPoint(temperature, humidity);  // calculate dew point fromm temp and humidity data
    Serial.print("Dew Point: ");
    Serial.println(dewPoint);

    // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
    // pieces of information in a channel.  Here, we write to field 1.
    ThingSpeak.setField(1,temperature);
    ThingSpeak.setField(2,pressure);
    ThingSpeak.setField(3,humidity);
    ThingSpeak.setField(4,dewPoint);
    ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
    WiFi.mode(WIFI_OFF);
    //delay(1000*60*30); // Update ThingSpeak 15 min.
    Serial.println("------------------------------------");
    Serial.print("Entering deep sleep mode for ");
    Serial.print(SLEEP_DELAY_IN_SECONDS);
    Serial.println(" seconds...");
    Serial.println("------------------------------------");
    ESP.deepSleep(SLEEP_DELAY_IN_SECONDS * 1000000, WAKE_RF_DEFAULT);
    //ESP.deepSleep(10 * 1000, WAKE_NO_RFCAL);
  }
  



/**************************************************************************/
void displaySensorDetails(void)
{
  sensor_t sensor;
  bmp.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" mPa");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" mPa");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" mPa");  
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}

float fetchHumidity()
{

  float h = dht.readHumidity();
  float t = dht.readTemperature();
    if (isnan(h)) {
    Serial.println("Failed to read from DHT sensor!");
    } else
    {
    return h;
}
}

float fetchPressure()
{
  /* Get a new sensor event */ 
  sensors_event_t event;
  bmp.getEvent(&event);

  if (event.pressure)
  {
    
  float pressure = event.pressure;
 
  
  if (isnan(pressure)) // if its not a number then display error
  {
    Serial.println("Failed to read pressure from BMP180 sensor!");
  }
  else
  return pressure; // return value to calling function
  }
  else
  {
    Serial.println("BMP180 Sensor error");
  }
}


float fetchTemperature()
{
    float temperature;
    bmp.getTemperature(&temperature);
   
    if (isnan(temperature))  // if its not a number then display error
    {
    Serial.println("Failed to read temperature from BMP180 sensor!");
    }
    else
{
    return temperature; // return value to calling function
}
}


float fetchDewPoint(float temperatureC, float humidity)
{

//Calculate dew point

//Specify the constants for water vapor (b) and barometric (c) pressure.
float b = 17.62;
float c = 243.5;
//Calculate the intermediate value 'gamma'
float gamma = log(humidity/100) + b*temperatureC / (c+temperatureC);
//Calculate dew point in Celsius
float dewPoint = c*gamma / (b-gamma);
    if (isnan(dewPoint))  // if its not a number then display error
    {
    Serial.println("Failed to caculate dew point!");
    }
    else
{
    return dewPoint; // return value to calling function
}
}

Credits

Adrian Shurmer
1 project • 1 follower

Comments