Muhammad Afzal
Published © Apache-2.0

Salt Warehouses Monitoring with Arduino & Artik Cloud

In this Article i will demonstrate how to connect Arduino MKR1000 with Artik Cloud & monitor salt/Dairy warehouses Temperature & Humidity.

BeginnerProtip2 hours2,242
Salt Warehouses Monitoring with Arduino & Artik Cloud

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
Jumper wires (generic)
Jumper wires (generic)
×3
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

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

Story

Read more

Schematics

Design

Code

Artik_Cloud.ino

C/C++
Connecting Arduino MKR1000 to Artik Cloud and send DHT22 reading to Cloud.
#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 ---------------Replace with Deivce Token-----------------"; //device token

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

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

#define DHTPIN 6    // what pin we're connected to, pin1 is 5th pin from end
#define DHTTYPE DHT22  // DHT 22
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 {
      float t = dht.readTemperature();
      float 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(buf);
       Serial.println("Data posted");
       client.stop();       
  }
 
  delay(1*60*1000); // delay 2 min

}

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

   JsonObject& root = jsonBuffer.createObject(); // create root objects
     root["sdid"] = "------------------Replace with Deivice ID-------"; // FIX Device ID
     root["type"] = "message";

   JsonObject& dataPair = root.createNestedObject("data"); // create nested objects
     dataPair["temp"] = insTemp;  
     dataPair["Humidity"] = 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;
  }
 }

Credits

Muhammad Afzal

Muhammad Afzal

20 projects • 97 followers
I am Software Eng having 9+ Years of experience. Hackster.io & Cayenne Mydevices Ambassador in Pakistan.

Comments