IoT World Part 4: Measuring temperature and humidity over time using DHT and Losant

Needed components

 

Creating device and setting up dashboard

Log in your account, in case of not having one, sing up, it’s really fast and easy

Create a device and set Device attributes to

  • Data Type – Number and Name –  temperature
  • Data Type – Number and Name – humidity

or you can use my recipe

Create e dashboard and Time Series Graph with 2 segments

Humidity

 

Temperature

 

The graph

Here are some tutorials on how to get started – Walkthrough, Dashboard



Wiring the ESP8266 and the DHT11

Connect (DHT11) to (ESP8266)

  • DATA to D2
  • VCC to VIN
  • GND to GND

On the schema the 3 pin is not used (NULL)

 

Adding required library

Go to Platform IO Home under <> Libraries search for keypad (in case you don’t know how, check out my article)

Install DHT sensor library by Adafruit Industries

When the installation is finished you need to go to Platform IO top menu and select Rebuild C/C++ Project Index



Flashing the code on the ESP8266

Create a file inside ProjectName/src/ called main.cpp and inside put

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <ESP8266WiFi.h>
#include <Losant.h>

// WiFi credentials.
const char *WIFI_SSID = "wifi-name";
const char *WIFI_PASS = "wifi-password";

// Losant credentials.
const char *LOSANT_DEVICE_ID = "device-id"; // Device id can be found under
                                            // Applications > Devices > Your
                                            // device > Right corner
const char *LOSANT_ACCESS_KEY = "access-key";
const char *LOSANT_ACCESS_SECRET =
    "access-secret"; // To generate Key & Secret go to Applications > Security >
                     // Right corder + Add access key

WiFiClientSecure wifiClient;

// For an unsecure connection to Losant.
// WiFiClient wifiClient;

LosantDevice device(LOSANT_DEVICE_ID);

#define DHTPIN D2 // Pin which is connected to the DHT sensor.

// Uncomment the type of sensor in use:
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE           DHT21     // DHT 21 (AM2301)

DHT_Unified dht(DHTPIN, DHTTYPE);

void connect() {
  // Connect to Wifi.
  Serial.print("Connecting to ");
  Serial.println(WIFI_SSID);

  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println();
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Connect to Losant.
  Serial.println();
  Serial.print("Connecting to Losant...");

  device.connectSecure(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);

  // For an unsecure connection.
  // device.connect(wifiClient, ACCESS_KEY, ACCESS_SECRET);

  while (!device.connected()) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("Connected successfully");
}

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    // wait until Serial is initialized
  }
  // connects to WiFi
  connect();

  // Initialize device.
  dht.begin();

  // Print temperature sensor details.
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.println("Temperature");
  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(" *C");
  Serial.print("Min Value:    ");
  Serial.print(sensor.min_value);
  Serial.println(" *C");
  Serial.print("Resolution:   ");
  Serial.print(sensor.resolution);
  Serial.println(" *C");
  Serial.println("------------------------------------");
  // Print humidity sensor details.
  dht.humidity().getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.println("Humidity");
  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("%");
  Serial.print("Min Value:    ");
  Serial.print(sensor.min_value);
  Serial.println("%");
  Serial.print("Resolution:   ");
  Serial.print(sensor.resolution);
  Serial.println("%");
  Serial.println("------------------------------------");
}

void loop() {
  bool toReconnect = false;

  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Disconnected from WiFi");
    toReconnect = true;
  }

  if (!device.connected()) {
    Serial.println("Disconnected from Losant");
    Serial.println(device.mqttClient.state());
    toReconnect = true;
  }

  if (toReconnect) {
    connect();
  }

  device.loop();

  // Losant uses a JSON protocol. Construct the simple state object.
  // { "button" : true }
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject &root = jsonBuffer.createObject();

  // Get temperature event and print its value.
  sensors_event_t event;
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println("Error reading temperature!");
  } else {
    Serial.print("Temperature: ");
    Serial.print(event.temperature);
    Serial.println(" *C");

    // set temperature to the JSON Buffer
    root["temperature"] = event.temperature;
  }

  // Get humidity event and print its value.
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println("Error reading humidity!");
  } else {
    Serial.print("Humidity: ");
    Serial.print(event.relative_humidity);
    Serial.println("%");

    // set humidity to the JSON Buffer
    root["humidity"] = event.relative_humidity;
  }

  // check if there is any data to be send
  if (!isnan(event.temperature) || !isnan(event.relative_humidity)) {
    // Send the state to Losant.
    device.sendState(root);
  }

  // Pause 30 seconds between measurements
  delay(30000);
}

Project is available in GitHub

git clone https://github.com/beastovest/dht.git

Connected to your ESP8266, on the right in Platform IO there is an arrow button > with tooltip Upload, press it, the upload should take few seconds

In case you get similar message

/dev/ttyUSB0: Access denied

You need to give rights to your current user to read ttyUSB0 port

 

If you have any questions or suggestions, feel free to leave a comment.

Become a Patron!