Salman Faris
Published © MIT

Temperature Monitoring with Arduino MKR1000 and ARTIK Cloud

Setup an Arduino MKR1000 board to read temperature data and send those readings to ARTIK Cloud for monitoring.

BeginnerFull instructions provided1 hour14,901
Temperature Monitoring with Arduino MKR1000 and ARTIK Cloud

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
×1
Temperature Sensor
Temperature Sensor
×1

Software apps and online services

ARTIK Cloud for IoT
Samsung ARTIK Cloud for IoT

Story

Read more

Schematics

Pin connection

Code

testing the sensor

C/C++
// Simple sensor test programme
int sensorPin = A0;    // select the input pin for the sensor
int sensorValue = 0;   // variable to store the value coming from the sensor
float temperature = 0.0; 
float voltage = 0.0;

void setup() {
  Serial.begin(9600);
  analogReadResolution(10);
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  Serial.print("sensorValue = ");
  Serial.print(sensorValue);
  voltage = sensorValue * (3300/1024); // in milliVolt
  Serial.print(" voltage = ");
  Serial.print(voltage);
  temperature = (voltage - 500 ) / 10;
  Serial.print(" temperature(C) = ");
  Serial.println(temperature);
  delay(100);
}

Send readings to samsung artik

C/C++
#include <WiFi101.h>
#include <ArduinoJson.h>
#include <ArduinoHttpClient.h>
#include <SPI.h>

// ARTIK Cloud REST endpoint
char server[] = "api.artik.cloud";  
int port = 443; // We're using HTTPS

// Device ID tokens
String deviceToken = "Your DeviceToken";
String deviceId = "Your deviceId";
// Your wifi network
char ssid[] = "Your Wifi name";     
char pass[] = "Your wifi oassword";



float temperature = 0.0;
boolean onFire = false;

char buf[200]; // buffer to store the JSON to be sent to the ARTIK cloud

const int LED = 6;
int ledState = 0;

WiFiSSLClient wifi;
HttpClient client = HttpClient(wifi, server, port);

int status = WL_IDLE_STATUS;


void setup() {

  pinMode(LED,OUTPUT);

  Serial.begin(9600);
  while ( status != WL_CONNECTED) { // Keep trying until connected
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                  
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);
  }
}
  
void loop() {
  Serial.println("loop");
  ledToggle();
  
  Serial.println("making POST request");
  String contentType = "application/json";
  String AuthorizationData = "Bearer " + deviceToken; //Device Token

  
  temperature = ((analogRead(A0) * (3300/1024)) -500 ) / 10 ; // in milliVolt
  onFire = false;
  
  int len = loadBuffer(temperature,onFire);  
  Serial.println("Sending data "+String(temperature)); 
  
  // push the data to the ARTIK Cloud
  client.beginRequest();
  client.post("/v1.1/messages"); //, contentType, buf
  client.sendHeader("Authorization", AuthorizationData);
  client.sendHeader("Content-Type", "application/json");
  client.sendHeader("Content-Length", len);
  client.endRequest();
  client.print(buf);

  // read the status code and body of the response
  int statusCode = client.responseStatusCode();
  String response = client.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
  
  Serial.print("Response: ");
  Serial.println(response);

  Serial.println("Wait a bit");

 
  delay(30000); // delay 5 min

}

int loadBuffer(float temp, boolean onFire ) {  
   StaticJsonBuffer<200> jsonBuffer; // reserve spot in memory

   JsonObject& root = jsonBuffer.createObject(); // create root objects
     root["sdid"] =  deviceId;  
     root["type"] = "message";

   JsonObject& dataPair = root.createNestedObject("data"); // create nested objects
     dataPair["Temperature"] = temp;  
     dataPair["onFire"] = onFire;

   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

Salman Faris

Salman Faris

26 projects • 417 followers
Maker | Hardware Hacker | Electronics Enthusiast

Comments