$USD
  • EUR€
  • £GBP
  • $USD
PROJECTS IoT

NodeMCU IoT Environment Monitor

DFRobot May 23 2018 658

 

Project Overview

This NodeMCU IoT project is a simple demonstration on how to send sensor data to the Internet. Powered by a NodeMCU ESP8266 microcontroller, this project is able to show temperature, humidity and atmospheric pressure to any MQTT subscriber.

Introduction

Using MQTT is much faster than using HTTP because of reduced overhead. In fact, MQTT is one of the preferred protocols for Internet of Things.

To use MQTT, we need a broker and there are lots of them! For this project, I used Shiftr.IO. I already detailed how to use the MQTT protocol with the NodeMCU so kindly check that out first.
 

Materials
 

For temperature and humidity, I used SHT1x from DFRobot. For the atmospheric sensor, I used the old reliable BMP085. My Arduino BMP085 tutorial covers how to use this sensor with the Arduino. The NodeMCU uses the same Arduino IDE and code as long as the ESP8266 core is installed.

Another requirement for this project is a free account in Shiftr.IO. I’ve created an “environment-monitor” channel in Shiftr and was promptly given username and passwords which is needed when connecting to the broker.

Also, the MQTT library is required and is downloadable here. Finally, you’ll be needing the SHT1X library and the BMP085 library, both of which (thankfully) are NodeMCU-compatible.

 

Wiring Diagram

The SHT1X library uses software SPI while the BMP085 uses I2C.

The default I2C pins of the NodeMCU are D1 (SCL) and D2 (SDA) or GPIO5 and GPIO4. Naturally, the BMP085’s SDA and SCL pins should be connected to those pins. As for the the SHT1X, I decided to use the D3 and D4 pins for clock and data respectively.

Here is the Fritzing diagram:

Arduino Sketch

The sketch is similar to that in my MQTT tutorial. The first part of the sketch is establishing the WiFi connection. Then, we connect to the MQTT broker through

client.begin("broker.shiftr.io", WiFiclient);
 

Next, I connected to the MQTT channel using

client.connect("environment-monitor", "<username>", "<password>")
 

The username and password is found on the URL of the MQTT channel. For example, if this is your URL: mqtt://7fb2fd2f:[email protected] ( found in namespace settings). Your username is 7fb2fd2f and the password is b9f1e26ae8d3dac8.

To publish data, I used

client.publish("environment-monitor/temperature", (String)temp_c);
 

There is no need to create the /temperature topic as it’s automatically created once you call the function above. Of course, you can publish multiple topics:

client.publish("environment-monitor/temperature", (String)temp_c); client.publish("environment-monitor/humidity", (String)humidity); client.publish("environment-monitor/pressure", (String)pressure);
 

Anyway, here’s the full sketch:

/*NodeMCU IoT Environment Monitor * * By Roland * * From https://www.teachmemicro.com/nodemcu-iot-environment-monitor */ #include <SHT1x.h> #include <Wire.h> #include <Adafruit_BMP085.h> #include <ESP8266WiFi.h> #include <MQTTClient.h> // Replace with your network credentials const char* ssid = "Les Boise Engr. Innovations"; const char* password = "tingkarol"; WiFiClient WiFiclient; MQTTClient client; unsigned long lastMillis = 0; String page = ""; String text = ""; String data; // Specify data and clock connections and instantiate SHT1x object #define dataPin  2 //D4 #define clockPin 0 //D3 SHT1x sht1x(dataPin, clockPin); Adafruit_BMP085 bmp; float temp_c; float humidity; float pressure; void setup() {   Serial.begin(115200); // Open serial connection to report values to host   Serial.println("Starting up");   if (!bmp.begin()) {    Serial.println("Could not find a valid BMP085 sensor, check wiring!");    while (1) {}   }   WiFi.begin(ssid, password); //begin WiFi connection   // Wait for connection  while (WiFi.status() != WL_CONNECTED) {  delay(500);  Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); Serial.print("connecting to MQTT broker..."); client.begin("broker.shiftr.io", WiFiclient); connect(); } void connect() { while (!client.connect("environment-monitor", "7fb2fd2f", "b9f1e26ae8d3dac8")) {   Serial.print("."); } Serial.println("\nconnected!"); client.subscribe("environment-data"); } void loop() {    // Read values from the sensor  temp_c = sht1x.readTemperatureC();  humidity = sht1x.readHumidity();  pressure = bmp.readPressure();    client.loop();  if(!client.connected()) {   connect();  }  if(millis() - lastMillis > 1000) {   lastMillis = millis();   client.publish("environment-monitor/temperature", (String)temp_c);   client.publish("environment-monitor/humidity", (String)humidity);   client.publish("environment-monitor/pressure", (String)pressure);  } }
 

The sensor data is published every second.

Video


 


REVIEW