abhishekghosh
Created March 25, 2019 © CC BY-NC-ND

Widgets on IBM Watson IoT from DHT11 real-time sensor data

This project shows how to visualize real-time data on IBM Watson IoT dashboard with widgets using ESP32 or similar Arduino board.

IntermediateProtip12
Widgets on IBM Watson IoT from DHT11 real-time sensor data

Things used in this project

Hardware components

SparkFun ESP32 Thing
SparkFun ESP32 Thing
Any ESP32 based board will work without modification of code. Any Arduino with Wi-Fi will work with minimal modification of code.
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Resistor 220 ohm
Resistor 220 ohm
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
IBM Watson IoT

Story

Read more

Schematics

ESP32 Arduino with DHT11

Required connection for ESP32 Arduino with DHT11 to connect with IBM Watson IoT

ESP32 IBM Watson IoT Example

In the repository, there is a DHT11 directory with Fritzing file and Arduino INO file. Those will be required in this project.

Code

ESP32 Arduino with DHT11 with IBM Watson IoT

Arduino
Modify the PARAMETERS marked in the code
/**
* A simple IBM IoT example for testing connection
* Onboard LED on pin 2 will blink each time data published
* Serial monitor will give OK output each time data published
* Base code written by Abhishek Ghosh, https://thecustomizewindows.com/ 
* * Needs below 2 steps :
* (1) On IBM IoT dashboard, go to Security > Connection Security > TLS Optional
* (2) Install (i) PubSubClient library (ii) Adafruit_Sensor library (iii) Adafruit DHT-sensor libaray from Arduino IDE
* Adding LED on pin 2 is optional
* Open Serial Monitor to see output
* Also check on IBM IoT dashboard
*/


#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN            15         // Pin which is connected to the DHT sensor.
// type the model of sensor in use, we are using DHT 11, if you are using DHT 21 or DHT 22 change it
#define DHTTYPE           DHT11     
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;

#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h> 

// <------- CHANGE PARAMETERS BELOW THIS LINE ------------>

const char ledPin = 2;

const char* ssid = "your-hot-spot";
const char* password = "password";

#define ORG "YOUR-ORG-NAME-ON-IBM-DASHBOARD"
#define DEVICE_TYPE "YOUR-SET-DEVICE-TYPE"
#define DEVICE_ID "YOUR-SET-DEVICE-ID"
#define TOKEN "YOUR-SET-TOKEN-OR-AUTOGENERATED-TOKEN"

// <------- CHANGE PARAMETERS ABOVE THIS LINE ------------>

char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
char pubTopic[] = "iot-2/evt/status/fmt/json";
char subTopic[] = "iot-2/cmd/test/fmt/String";
char authMethod[] = "use-token-auth";
char token[] = TOKEN;
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;

WiFiClient wifiClient;
PubSubClient client(server, 1883, NULL, wifiClient);

void receivedCallback(char* pubTopic, byte* payload, unsigned int length) {
  Serial.print("Message received: ");
  Serial.println(pubTopic);

  Serial.print("payload: ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  /* we got '1' -> on */
}

void setup() {
    Serial.begin(115200);
    Serial.println();
    pinMode(ledPin, OUTPUT);
    Serial.println("IBM Watson IoT ESP32 Temperature, Humidity With DHT11");
    sensor_t sensor;
    dht.temperature().getSensor(&sensor);
    dht.humidity().getSensor(&sensor);
    delayMS = sensor.min_delay / 1000;
    
    Serial.print("Connecting to "); 
    Serial.print(ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    } 
    Serial.println("");
    
    Serial.print("WiFi connected, IP address: "); 
    Serial.println(WiFi.localIP());

    if (!client.connected()) {
        Serial.print("Reconnecting client to ");
        Serial.println(server);
        while (!client.connect(clientId, authMethod, token)) {
            Serial.print(".");
            delay(500);
        }
        client.setCallback(receivedCallback);
        if (client.subscribe(subTopic)) {
            Serial.println("subscribe to cmd OK");
        } else {
            Serial.println("subscribe to cmd FAILED");
        }
        Serial.println("IBM Watson IoT connected");
    }
}

long lastMsg = 0;
long temperature = 0;
long humidity = 0;

void loop() {
    delay(delayMS);
    sensors_event_t event;  
    client.loop();
    long now = millis();
    if (now - lastMsg > 3000) {
        lastMsg = now;
        dht.temperature().getEvent(&event);
        temperature = (event.temperature);
        dht.humidity().getEvent(&event);
        humidity = (event.relative_humidity);
        
        String payload = "{\"d\":{\"Name\":\"" DEVICE_ID "\"";
               payload += ",\"temperature\":";
               payload += temperature;
               payload += ",\"humidity\":";
               payload += humidity;
               payload += "}}";
        Serial.print("Sending payload: ");
        Serial.println(payload);

        if (client.publish(pubTopic, (char*) payload.c_str())) {
            Serial.println("Publish ok");
        digitalWrite(ledPin, HIGH);
        delay(1000);
        digitalWrite(ledPin, LOW);
        delay(1000);
        } else {
            Serial.println("Publish failed");
        }
    }
}

Credits

abhishekghosh
1 project • 0 followers

Comments