DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. IoT
  4. Temperature Control With Ubidots MQTT and NodeMcu

Temperature Control With Ubidots MQTT and NodeMcu

Building your own temperature sensor is a common, fun project, and you can set it up to alert you when the temperature gets too hot or too cold.

Juan Jose Bello user avatar by
Juan Jose Bello
·
Jan. 11, 17 · Tutorial
Like (1)
Save
Tweet
Share
5.65K Views

Join the DZone community and get the full member experience.

Join For Free

This guide details how to make a temperature control with Ubidots. In addition, we'll show you how to create different events that notify you through email or SMS when your "variable" (in this case, the temperature) changes its value.

We are going to use a pre-wired and waterproof version of the DS18B20 sensor (OneWire temperature sensor). It's handy when you need to measure something far away or in wet conditions. The OneWire temperature sensor has different versions; one of them has a resistor integrated and others don't, so make sure the version you're using is correct before starting with the project. For the control, we are going to use a NodeMcu using a digital pin. 

Requirements

  • NodeMcu version 1.0
  • DS18B20 OneWire temperature sensor
  • UbidotsESPMQTT library
  • OneWire Library 
  • DallasTemperature Library
  • One 4.7kΩ resistor (in case your DS18B20 doesn't have one)
  • Cables

Setup

1. First, we should choose a version. If your sensor has the resistor integrated, just connect it to the NodeMcu like this:

BLACK WIRE - GND
RED WIRE - Vin
YELLOW WIRE - D3


If not, you have to power the DATA pin through the resistance of 4.7kΩ, and the Vin and GND pins should be connected to each other and the ground. 

Image title


2. Go to the Arduino IDE, click on Files -> Preferences, and enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into the "Additional Board Manager URLs" field. You can add multiple URLs, separating them with commas.

3. Open "Boards Manager" from Tools -> Board menu and install esp8266 platform (and don’t forget to select your NodeMCU 1.0 board from the Tools > Board menu after installation).

4. Download the UbidotsESPMQTT library, OneWire Library, and DallasTemperature Library using these hyperlinks.

5. Now, click on Sketch -> Include Library -> Add .ZIP Library.

6. Select the ZIP files of Ubidots ESPMQTT, OneWire, and DallasTemperature and then “Accept” or “Choose” for all the libraries. If you can't add the libraries, try manually: unzip the downloaded rar/zip and copy the folder from the library to the path C:\Users\ubidots\Documents\Arduino\libraries.

7. Close the Arduino IDE and open it again.

Program the ESP NodeMcu

Once everything is connected correctly, we will go to the IDE and write the following code:

/****************************************
 * Include Libraries
 ****************************************/
#include "UbidotsESPMQTT.h"
#include <OneWire.h>
#include <DallasTemperature.h>
/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "....." // Your Ubidots TOKEN
#define WIFINAME "....." //Your SSID
#define WIFIPASS "....." // Your Wifi Pass
#define MQTTCLIENTNAME "....." // Your MQTT Client Name, it must be unique so we recommend to choose a random ASCCI name
#define Pin D3

int temp;
OneWire ourWire(Pin);
DallasTemperature sensors(&ourWire);

Ubidots client(TOKEN, MQTTCLIENTNAME);
/****************************************
 * Auxiliar Functions
 ****************************************/
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}
/****************************************
 * Main Functions
 ****************************************/
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  client.wifiConnection(WIFINAME, WIFIPASS);
  sensors.begin();
  client.begin(callback);
  }
void loop() {
  // put your main code here, to run repeatedly:
  if(!client.connected()){
      client.reconnect();
      }
  
  sensors.requestTemperatures();       //Prepare the sensor for reading
  
  temp = sensors.getTempCByIndex(0);
  
  Serial.print(sensors.getTempCByIndex(0)); //Read and print the temperature in Celsius
  client.add("temperature", temp); //Insert your variable Labels and the value to be sent
  delay(1000);
  Serial.println("Grados Centigrados");
  client.ubidotsPublish("control");
  
  client.loop();
  }


Results

Now, open your Ubidots account and go to Source. You will see a new Data Source named"control." Click on it.

Image title


Then, you will see the temperature value. 

Image title

Also, you can create an Event that notifies you when the temperature is irregular. Go to Events, click on Add Event, and select your Data Source and Variable.  

The Events can notify you via email, SMS, Telegram, or WebHook. You can also set a variable:

Image title

When your event is ready, you should see these windows:

Image title

In this example, if temperature value is >= 28, Ubidots will send you an email as previously configured, as such:

Image title

It's very simple! With just a few cables you can observe the temperature behavior of your home, whether you're inside or outside. You can even trigger alerts via email/SMS in case of fire.

Now you try! If you haven't signed up with Ubidots, go here.

MQTT

Published at DZone with permission of Juan Jose Bello, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Configure Kubernetes Health Checks
  • Cloud Performance Engineering
  • A Beginner’s Guide To Styling CSS Forms
  • Java REST API Frameworks

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: