Easy, Cheap Web enabled Temperature and Humidity Sensor – With Ubidots

This article is an accompaniment to this youtube video (https://www.youtube.com/watch?v=yqmOp7m4szA). See below the code for the two examples mentioned in the video. Don’t forget to visit Ubidots: http://bit.ly/ubidotsCC

The parts I use in this video:

Wemos D1 Mini: http://geni.us/wemosd1mini
Temp and Hum Sensor: http://geni.us/dht22
Soldering iron I use: http://geni.us/hakkofx888d

Wemos D1 Mini and DHT22 (AM2032) Sensor


//Make sure you setup the ESP8266 Board
//File - Preferences - Additional Board References URL
//http://arduino.esp8266.com/versions/2.3.0/package_esp8266com_index.json

#include "DHT.h" //https://github.com/adafruit/DHT-sensor-library

#define DHTPIN 5 //Pin D1

#define DHTTYPE DHT22 //Temp and Humidity Sensor we are using


DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  Serial.println("Initialised...");

  dht.begin();
}

void loop() {

  delay(1500);
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();


  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print("   Temperature: ");
  Serial.println(t);
}

IOT: Connecting to WiFi and uploading to Ubidots

#include "DHT.h" //https://github.com/adafruit/DHT-sensor-library
#include <ESP8266WiFi.h>
#include "UbidotsESPMQTT.h"
#define DHTPIN 5 //Pin D1

#define DHTTYPE DHT22 //Temp and Humidity Sensor we are using


#define TOKEN "xxxxxxxxxxxxxxxxxxxxxxxxx" // Your Ubidots TOKEN

#define WIFINAME "SSID" //Your SSID
#define WIFIPASS "PASSWORD" //Your Wifi Pass

DHT dht(DHTPIN, DHTTYPE);


Ubidots client(TOKEN);

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();
}

void setup() {
  client.setDebug(true);
  Serial.begin(115200);
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  dht.begin();
}

void loop() {


  float h = dht.readHumidity(); //Humidity in %
  float t = dht.readTemperature(); //Temp in Celsius

  if (isnan(h) || isnan(t)) { //Did the sensor read fail?
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print("   Temperature: ");
  Serial.println(t);

  if(!client.connected()){
      client.reconnect();
  }

  client.add("temperature", t);
  client.add("humidity", h);
  client.ubidotsPublish("Temp_Humidity_SmallGH");
  client.loop();

  delay(60000);
  


}

14 thoughts on “Easy, Cheap Web enabled Temperature and Humidity Sensor – With Ubidots”

  1. Hi, thanks for the videos and info. I wanted to ask if you are going to do the how to for the greenhouse stand alone system. Also I am from Kenya so I don’t know if you can do a dummys version.. Haha. All this is new to me and would like to make my autonomous system for open air farming.

    1. I think that would be a good idea actually….a beginner’s guide to garden automation. I will have to think about how I approach that. However I will be talking through my changes I am making to my automated irrigation very soon. I was going to do some work on that today.

  2. Hi
    I just wanted to drop you a quick note to say thanks for sharing this content. From watching your video I managed to get my first D1 mini project to report a temperature to Ubidots today. Had I managed to copy and paste the API key correctly it would have happened even faster, but the troubleshooting at least made me understand the code better. I’ve been watching loads of similar videos to try and get me to the point to dust off the soldering iron and get started; this was the one that achieve that!
    Thanks again

  3. Just found your Channel and found the Temperature and Humidity video to be very useful! Thank you. Obviously I will need to do more research but can you please comment if it’s possible to hook up ONE Arduino to multiple sets of D1 Mini + sensors in order to monitor several greenhouses? Thank you, Sir.

      1. Tried to compile the code and to upload it, but it shows a “FatalError: Failed to connect to ESP8266. Time out waiting for packet header” error. Would you be willing to take a look at the error message and see what might have gone wrong? http://tiny.cc/esp8266error

  4. Thank you SO MUCH!! Your answer gave me so much excitement! The truth is, I had ALREADY saved your playlist before your reply, ha ha! Looks like this will be our project in the next year once the components get here!
    By the way, it’s pretty awesome that ubidots allows the stem option for students 😉
    Will keep you updated. Jonathan

  5. This little project made me start looking in to arduino again. Years ago I guess 2005, I programmed a lot of things with the arduino. But never made something useful. Now 2021 I just made this cheap web-enabled meter with a bme280 and directly put it to use in my tent. Thank you Shaun for inspiring me to do this.

  6. HI CHILICHUMP
    where i can get the UbidotESPPMQTT Library??
    please help me
    I’ve download it from github but there are some errors
    C:\Users\User\Documents\Arduino\libraries\pubsubclient-master\src/PubSubClient.h:152:9: error: initializing argument 2 of ‘bool PubSubClient::publish(String, const uint8_t*, uint32_t, bool)’ [-fpermissive]
    bool publish(String topic, const uint8_t *payload, uint32_t plength, bool retained = false);
    ^

Leave a Comment

Your email address will not be published. Required fields are marked *