Arduino MKR1000 - DHT - Artik cloud

Arduino MKR1000 - DHT - Artik cloud © LGPL

Sending temperature and humidity from Arduino MKR1000 to Artik Cloud

  • 4,510 views
  • 2 comments
  • 20 respects

Components and supplies

Apps and online services

About this project

Background

This article describe how to use REST for sending data from Arduino MKR1000 and DHT sensor to Artik Cloud

Prerequisites

You must have basic knowledge about programming Arduino MKR1000. Also you need Artik Cloud (Samsung) account. For Arduino IDE, libraries WiFi101 and ArduinoJSON are required. In this article we are not going through step-by-step tutorial for Artik Cloud. If you are not familiar with Artik Cloud, please review this article with video tutorial for instructions.

Artik Cloud

First step is creating device type (manifest) for DHT sensor at developer dashboard. We have two values : temperature and humidity, and manifest is very simple. You can choose any name for device type, but note it for next step when device will be created. We should create manifest with two field for each value. After finishing all steps you would have device type as shown on next image :

Next is creating Artik Cloud device. Log on into your dashboard with your devices. Create new device for device type you created in first step. After device has been created, click on settings gear and generate token. From this dialog you need to copy-paste device ID and device token into Arduino code.

Code explanation

For sending data to Artik Cloud HTPPS REST API is used. That's why we need <WiFi101> library for Arduino IDE. Another helper library is <ArduinoJSON>. According Artik documentation, when REST API is used, data must be sent in JSON format. Here is example :

{
  "sdid": "4697f11336c540a69ffd6f445061215e",
  "type": "message",
  "data": { "temp" : 22, "humid" : 55}
}

For more information about Artik Cloud REST API you can find here.

Code modification

You have to make some modification in attached code. All changes are to provide your credentials for WiFi and device ID and token. Find these lines and provide your data:

String AuthorizationData = "Authorization: Bearer <YOUR DEVICE TOKEN>";
...
char ssid[] = "SSID";      //  your network SSID (name)
char pass[] = "PASSWORD";
...
root["sdid"] = "<YOUR DEVICE ID>"; 

Code

ArtikDHT.inoC/C++
Arduino code
#include <WiFi101.h>
#include <DHT.h>
#include <ArduinoJson.h>

// Web REST API params
char server[] = "api.artik.cloud";  
int port = 443; //(port 443 is default for HTTPS)

String AuthorizationData = "Authorization: Bearer <YOUR DEVICE TOKEN>";

float insTemp;
char buf[200];
#define LED 6
int ledState = 0;

WiFiSSLClient client;
char ssid[] = "<YOUR SSID>";      //  your network SSID (name)
char pass[] = "<YOUR SSID PASSWORD>";
int status = WL_IDLE_STATUS;

#define DHTPIN 5    // what pin we're connected to, pin1 is 5th pin from end
#define DHTTYPE DHT11  // DHT 11
DHT dht(DHTPIN,DHTTYPE);

void setup() {

  pinMode(LED,OUTPUT);

  Serial.begin(9600);
  while (status != WL_CONNECTED){
    Serial.println("Attempting to connect to WiFi");
    status = WiFi.begin(ssid,pass);
  }
  Serial.println("connected to WiFi ");
  
}
  
void loop() {
  Serial.println("loop");
  ledToggle();
  client.connect(server, port);
  delay(1000);
  if (!client.connected()) { 
    Serial.println(" error ");
  } else {
      int t = dht.readTemperature();
      int h = dht.readHumidity();
      Serial.println("Sending data"+String(t));
      client.println("POST /v1.1/messages HTTP/1.1");
      client.println("Host: api.artik.cloud");
      client.println("Accept: */*");
      client.println("Content-Type: application/json");
      client.println("Connection: close");
      client.println(AuthorizationData);

       // Automated POST data section
       client.print("Content-Length: ");
       client.println(loadBuffer(t,h)); // loads buf, returns length
       client.println();
       client.println(buf);
       Serial.println("Data posted");
       client.stop();       
  }
 
  delay(5*60*1000); // delay 5 min

}

int loadBuffer(int insTemp, int insHumid ) {  
   StaticJsonBuffer<200> jsonBuffer; // reserve spot in memory

   JsonObject& root = jsonBuffer.createObject(); // create root objects
     root["sdid"] = "<YOUR DEVICE ID>"; // FIX 
     root["type"] = "message";

   JsonObject& dataPair = root.createNestedObject("data"); // create nested objects
     dataPair["temp"] = insTemp;  
     dataPair["humid"] = insHumid;

   root.printTo(buf, sizeof(buf)); // JSON-print to buffer

   return (root.measureLength()); // also return length
 }

 void ledToggle(){
  if (ledState == 0){
    digitalWrite(LED,LOW);
    ledState = 1;
  } else {
    digitalWrite(LED,HIGH);
    ledState = 0;
  }
 }

Schematics

MKR1000_DHT11
mkr1000_dht11.fzz
MKR1000_DHT schema
Mkr1000 dht11 bb

Comments

Similar projects you might like

Water Quality Monitoring Using MKR1000 and ARTIK Cloud

Project tutorial by Team Animo!

  • 7,075 views
  • 12 comments
  • 30 respects

Temperature Monitoring with Arduino MKR1000 and ARTIK Cloud

Project tutorial by FoxLab

  • 7,865 views
  • 2 comments
  • 32 respects

ARTIK + MKR1000 + DHT11 + MQTT

Project tutorial by vincent wong

  • 3,880 views
  • 12 comments
  • 10 respects

MKR1000 to Initial State DHT Visulaizations

Project tutorial by Stephen Borsay

  • 848 views
  • 1 comment
  • 3 respects
Add projectSign up / Login