Temperature Monitoring With Arduino MKR1000 And ARTIK Cloud

Introduction

 
This project sets up an Arduino MKR1000 board, which reads the temperature data and sends those data to Artik Cloud. The project consists of three steps, i.e. setting up Arduino MKR1000 to read the temperature values, create temperature device types and temperate devices in Artik Cloud. The last step is to send the data to Artik Cloud and you can monitor the temperature changes from the Artik dashboard.
 
 
 
Setup Arduino MKR1000 board and the temperature sensor
Upload the code, given below, to check the MKR1000 and temperature sensor.
  1. // Simple sensor test programme    
  2. int sensorPin = A0; // select the input pin for the sensor     
  3. int sensorValue = 0; // variable to store the value coming from the sensor     
  4. float temperature = 0.0;  
  5. float voltage = 0.0;  
  6. void setup() {  
  7.     Serial.begin(9600);  
  8.     analogReadResolution(10);  
  9. }  
  10. void loop() {  
  11.     // read the value from the sensor:     
  12.     sensorValue = analogRead(sensorPin);  
  13.     Serial.print("sensorValue = ");  
  14.     Serial.print(sensorValue);  
  15.     voltage = sensorValue * (3300 / 1024); // in milliVolt     
  16.     Serial.print(" voltage = ");  
  17.     Serial.print(voltage);  
  18.     temperature = (voltage - 500) / 10;  
  19.     Serial.print(" temperature(C) = ");  
  20.     Serial.println(temperature);  
  21.     delay(100);  
  22.  
Connection
 
 
  • TMP +v to MKR1000 - VCC
  • TMP GND to MKR1000 - GND
  • TMP SIG to MKR1000 - A0
You will see the serial monitor, as given below-
 
 
This means our temperature sensor and Arduino MKR1000 works perfectly.
 
Setup Artik Cloud
 
Now, the time to connect to the Cloud (Samsung Artik Cloud) has come.
 
Go to Developer dashboard to create a device type.
  • Sign in to Developer Dashboard.
  • Click the "+ Create Device Type".
  • Name the device type "Arduino Temperature Sensor" and give it a unique name "my.device.arduino.temperature".
  • Click "Create Device Type" to create the device type.
     
     
Now, create a new Manifest for the new device type by following-
  • Click "Arduino Temperature Sensor" in the left column.
  • Click "Manifest" and then "+ New Manifest".
  • Enter Temperature as the Field Name and "Double" as Data Type.
     
  • Click "Manifest" and then "+ New Manifest".
     
  • Click "Save" and then "Next: Actions".
  • Leave Actions as default and click "Save New Manifest".
  • Go to MY ARTIK Cloud to create a new Arduino Temperature Sensor device.
  • Sign in to MY ARTIK Cloud.
  • Click to connect a new device. Select the "Arduino Temperature Sensor" device type.
  • Click "Connect Device..."
  • Click the Settings icon of the newly created device. In the pop-up, click "GENERATE DEVICE TOKEN…".
  • Copy the device ID and device.
  • A token will come on this screen. You will use this in the code.

     
     
  • Click the Settings icon of the newly created device. In the pop-up, click "GENERATE DEVICE TOKEN…".
     
     
  • Now the Artk part is set.
Sending Temperature Readings to ARTIK Cloud
 
Paste your DEVICE ID, given below-
  1. String deviceId = "your deviceId"
paste you DEVICE Token, given below-
  1. String deviceToken = "your deviceToken";  
Paste Your Wi-Fi name, given below-
  1. char ssid[] = "Your wifi name";  
Paste Your Wi-Fi password, given below-
  1. char pass[] = "Your wifi Password";  
When everything is set, upload your code-
  1. #include < WiFi101.h >   
  2. #include < ArduinoJson.h >   
  3. #include < ArduinoHttpClient.h >   
  4. #include < SPI.h >  
  5. // ARTIK Cloud REST endpoint     
  6. char server[] = "api.artik.cloud";  
  7. int port = 443; // We're using HTTPS     
  8. // Device ID tokens     
  9. String deviceToken = "Your deviceToken";  
  10. String deviceId = "Your deviceId";  
  11. // Your wifi network     
  12. char ssid[] = "Your wifi name";  
  13. char pass[] = "Your wifi Password";  
  14. float temperature = 0.0;  
  15. boolean onFire = false;  
  16. char buf[200]; // buffer to store the JSON to be sent to the ARTIK cloud     
  17. const int LED = 6;  
  18. int ledState = 0;  
  19. WiFiSSLClient wifi;  
  20. HttpClient client = HttpClient(wifi, server, port);  
  21. int status = WL_IDLE_STATUS;  
  22. void setup() {  
  23.     pinMode(LED, OUTPUT);  
  24.     Serial.begin(9600);  
  25.     while (status != WL_CONNECTED) { // Keep trying until connected     
  26.         Serial.print("Attempting to connect to Network named: ");  
  27.         Serial.println(ssid);  
  28.         // Connect to WPA/WPA2 network:     
  29.         status = WiFi.begin(ssid, pass);  
  30.     }  
  31. }  
  32. void loop() {  
  33.     Serial.println("loop");  
  34.     ledToggle();  
  35.     Serial.println("making POST request");  
  36.     String contentType = "application/json";  
  37.     String AuthorizationData = "Bearer " + deviceToken; //Device Token     
  38.     temperature = ((analogRead(A0) * (3300 / 1024)) - 500) / 10; // in milliVolt     
  39.     onFire = false;  
  40.     int len = loadBuffer(temperature, onFire);  
  41.     Serial.println("Sending data " + String(temperature));  
  42.     // push the data to the ARTIK Cloud     
  43.     client.beginRequest();  
  44.     client.post("/v1.1/messages"); //, contentType, buf     
  45.     client.sendHeader("Authorization", AuthorizationData);  
  46.     client.sendHeader("Content-Type""application/json");  
  47.     client.sendHeader("Content-Length", len);  
  48.     client.endRequest();  
  49.     client.print(buf);  
  50.     // read the status code and body of the response     
  51.     int statusCode = client.responseStatusCode();  
  52.     String response = client.responseBody();  
  53.     Serial.print("Status code: ");  
  54.     Serial.println(statusCode);  
  55.     Serial.print("Response: ");  
  56.     Serial.println(response);  
  57.     Serial.println("Wait a bit");  
  58.     delay(30000); // delay 5 min     
  59. }  
  60. int loadBuffer(float temp, boolean onFire) {  
  61.     StaticJsonBuffer < 200 > jsonBuffer; // reserve spot in memory     
  62.     JsonObject & root = jsonBuffer.createObject(); // create root objects     
  63.     root["sdid"] = deviceId;  
  64.     root["type"] = "message";  
  65.     JsonObject & dataPair = root.createNestedObject("data"); // create nested objects     
  66.     dataPair["Temperature"] = temp;  
  67.     dataPair["onFire"] = onFire;  
  68.     root.printTo(buf, sizeof(buf)); // JSON-print to buffer     
  69.     return (root.measureLength()); // also return length     
  70. }  
  71. void ledToggle() {  
  72.     if (ledState == 0) {  
  73.         digitalWrite(LED, LOW);  
  74.         ledState = 1;  
  75.     } else {  
  76.         digitalWrite(LED, HIGH);  
  77.         ledState = 0;  
  78.     }  
  79.  
Open serial monitor.
 
 
Now, sign in to My ARTIK Cloud.
 
Click Arduino Temperature Sensor. Click "+/- CHARTS" and check the Temp field to visualize a chart.