Arduino MKR1000 – DHT – Artik cloud

Sending temperature and humidity from Arduino MKR1000 to Artik Cloud

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1

Software apps and online services

ARTIK Cloud for IoT
Samsung ARTIK Cloud for IoT
Arduino IDE
Arduino IDE

Story

Code

ArtikDHT.ino

C/C++

#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;
  }
 }

A Propos De L'Auteur

Ibrar Ayyub

Je suis expérimenté, rédacteur technique, titulaire d'une Maîtrise en informatique de BZU Multan, Pakistan à l'Université. Avec un arrière-plan couvrant diverses industries, notamment en matière de domotique et de l'ingénierie, j'ai perfectionné mes compétences dans la rédaction claire et concise du contenu. Compétent en tirant parti de l'infographie et des diagrammes, je m'efforce de simplifier des concepts complexes pour les lecteurs. Ma force réside dans une recherche approfondie et de présenter l'information de façon structurée et logique format.

Suivez-Nous:
LinkedinTwitter

Laisser un Commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

fr_FRFrench
Faire défiler vers le Haut