Particle IoT

Cloud API: Interface Photon with DHT 22

particle logo

Interface Photon with DHT 22 Sensor

Hi Readers! This post could be super exiting for you. Interface DHT 22 with Particle Photon, rather then just interfacing, we will host the sensor datas like temperature, humidity on the cloud. Which can be accessed from any where in the world through a REST API call.

Materials Required


Connection diagram


Materials Needed

  • Jumper Wires
  • DHT 22 Sensor
  • Resistor 10k Ohm
  • Battery/Power supply for Particle Photon

Customise the display

Modify the example code as shown to get customised output.

Code for reading tempeature and humidity which must be flashed into the device

The variables "temperature, humidity, and farenheit" inside the setup function are accesable via REST Api call.


#define DHTPIN 2     // what pin we are connected to
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

double temperature = 0;
double farenheit = 0;
double humidity = 0;

void setup() {
    dht.begin();
    Particle.variable("temperature", temperature); // api variables 
    Particle.variable("farenheit", farenheit);
    Particle.variable("humidity", humidity);
}

void loop() {
    // wait a few second between the measurement 
    delay(2000);

    //Reading temperature or humidlity takes about 250 millisecond
    //Sensor variable also may be 2 second old 

    humidlity = dht.humidity();
    temperature = dht.temperature();
    farenheit = dht.farenheit();
}
                           

Particle Cloud API The Particle Cloud API is a REST API. REST means a lot of things, but first and foremost it means that we use the URL in the way that it's intended: as a "Uniform Resource Locator". Every device has a URL, which can be used to GET variables, POST a function call, or PUT new firmware.

GET a variable value

      Request the current value of a variable exposed by the device. Variables can be read on a device you own, or for any device that is part of a product you are a team member of.

Protocol and Host

"https://api.particle.io"

Definition

GET /v1/devices/:deviceID/:varName

Request Arguments
deviceId Required Device ID
varName Required Variable Name

Example Request

$ curl

"https://api.particle.io/v1/devices/0123456789abcdef01234567/temperature?access_token=xxxxxxxx"

Web application

Temperature, humidity, Farenheit values are exposed to the internet. It can be accessed via an API call to the Particle cloud service. I have written a sample web application to display the readings on a webpage. This is a simpe HTML, CSS, Jquery code. I have made AJAX call to query the cloud API's.

sourcecode.txt


Video

The below video will show how the app actually works