Home > Projects > LED Projects > Start Connect ARTIK Cloud with Arduino/Genuino MKR1000

Start Connect ARTIK Cloud with Arduino/Genuino MKR1000

Summary of Start Connect ARTIK Cloud with Arduino/Genuino MKR1000


This project demonstrates connecting an Arduino MKR1000 to Samsung ARTIK Cloud to control RGB LED states via Wi-Fi. The setup involves configuring device types in the cloud, installing specific libraries like MQTT and ArduinoJson, and writing code to publish sensor data and subscribe to remote commands. The system cycles LED colors every 10 minutes based on free package specifications while allowing manual state changes through cloud rules.

Parts used in the Arduino MKR1000 ARTIK Cloud LED Project:

  • Arduino MKR1000
  • Samsung ARTIK Cloud for IoT
  • Arduino IDE 1.6.7 or higher
  • Wi-Fi AP
  • Breadboard
  • RGB LED (common anode)
  • MQTT library version 1.9.2
  • ArduinoJson library
  • WiFi101 library

Using Arduino/ Genuino MKR1000 to connect ARTIK Cloud control I/O light LED.

Start Connect ARTIK Cloud with Arduino Genuino MKR1000

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
× 1

Software apps and online services

ARTIK Cloud for IoT
Samsung ARTIK Cloud for IoT

Story

Schematics

SCH

common anode led

Code

#include <MQTTClient.h>
#include <ArduinoJson.h>
#include <SPI.h>
#include <WiFi101.h>

const char* _SSID     = "********"; //Wi-Fi SSID 
const char* _PASSWORD = "********"; // Wi-Fi Password 

// MQTT - Artik Cloud Server params
char Artik_Cloud_Server[]     = "api.artik.cloud"; // Server 
int  Artik_Cloud_Port         = 8883; // MQTT Port
char Client_Name[] = "momososo-mrk1000"; // Any Name 
char Device_ID[]   = "c0214c6df6fb4e539a7498cf06d07797"; // DEVICE ID
char Device_TOKEN[]   = "f0ee465c3602444e9366e0c8337b414e"; //  DEVICE TOKEN
char MQTT_Publish[]      = "/v1.1/messages/c0214c6df6fb4e539a7498cf06d07797"; // (/v1.1/messages/"DEVICE ID")
char MQTT_Subscription[] = "/v1.1/actions/c0214c6df6fb4e539a7498cf06d07797"; // (/v1.1/actions/"DEVICE ID")

char buf[200]; // Json Data to Artik Cloud 
char receivebuf[200]; // Json Data to Artik Cloud 
WiFiSSLClient client;
MQTTClient MQTT_Artik_Client; // MQTT Protocol

//led SET
const int RED_LEDPin = 0;
const int GREEN_LEDPin = 1;
const int BLUE_LEDPin = 2;
const int BROADLEDPin = 6;
boolean toggle;
int LED_state[3] = {0};

void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
  Serial.print("incoming: ");
  Serial.print(topic);
  Serial.print(" - ");
  Serial.print(payload);
  Serial.println();
  String t = topic;
  //String s = payload;
  //float temp = s.toFloat();

  if(t == MQTT_Subscription)
  {
    parseBuffer(payload);
  }
}

void digitalWriteuser(int Pin, boolean state)
{
  digitalWrite(Pin, state);
  LED_state[Pin]=state;
}

void setup() {

  Serial.begin(57600);  
  // Wifi Setting
  WiFi.begin(_SSID, _PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  MQTT_Artik_Client.begin(Artik_Cloud_Server, Artik_Cloud_Port, client); // Connect to Artik Server
  while (!MQTT_Artik_Client.connect(Client_Name, Device_ID, Device_TOKEN)) { // Connect to Artik IOT Device 
    Serial.print("*");
    delay(1000);
  }
  MQTT_Artik_Client.subscribe(MQTT_Subscription);

  pinMode(RED_LEDPin, OUTPUT);
  pinMode(GREEN_LEDPin, OUTPUT);
  pinMode(BLUE_LEDPin, OUTPUT);
  pinMode(BROADLEDPin, OUTPUT);
  digitalWriteuser(RED_LEDPin, LOW);
  digitalWriteuser(GREEN_LEDPin, HIGH);
  digitalWriteuser(BLUE_LEDPin, HIGH);
  
}

void loadBuffer(void) {
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& dataPair0 = jsonBuffer.createObject();
  JsonObject& dataPair1 = jsonBuffer.createObject();
  dataPair1["D0"] = LED_state[RED_LEDPin];
  dataPair1["D1"] = LED_state[GREEN_LEDPin];
  dataPair1["D2"] = LED_state[BLUE_LEDPin];
  dataPair0["Digital_I_O"]=dataPair1;
  dataPair0.printTo(buf, sizeof(buf));
  Serial.println(buf);
}
void parseBuffer(String _payload) {
  StaticJsonBuffer<200> jsonBuffer;
  String json = _payload;
  
  json.toCharArray(receivebuf,200);

  //*****************************
  JsonObject& root = jsonBuffer.parseObject(receivebuf);
  if (!root.success()) {
    Serial.println("parseObject() failed");
  }
  //****************************
  
  const char* nameparam = root["actions"][0]["name"];
  boolean actionD0 = root["actions"][0]["parameters"]["D0"];
  byte actionD1 = root["actions"][0]["parameters"]["D1"];
  long actionD2 = root["actions"][0]["parameters"]["D2"];

  Serial.println(nameparam);
  Serial.print("D0:");
  Serial.println(actionD0);
  Serial.print("D1:");
  Serial.println(actionD1);
  Serial.print("D2:");
  Serial.println(actionD2);
  
  if (actionD0 == 1) 
  {
     digitalWriteuser(RED_LEDPin, HIGH);
  }
  if (actionD0 == 0) 
  {
     digitalWriteuser(RED_LEDPin, LOW);
  }
  if (actionD1 == 1) 
  {
     digitalWriteuser(GREEN_LEDPin, HIGH);
  }
  if (actionD1 == 0) 
  {
     digitalWriteuser(GREEN_LEDPin, LOW);
  }
  if (actionD2 == 1) 
  {
     digitalWriteuser(BLUE_LEDPin, HIGH);
  }
  if (actionD2 == 0) 
  {
     digitalWriteuser(BLUE_LEDPin, LOW);
  }
}
void loop() {
  loadBuffer();
  MQTT_Artik_Client.publish(MQTT_Publish, buf);// Publishing data to the Artik Cloud 
  Serial.println("Publishing..");
  //delay(3000);

  for (int i =0; i<100;i++){ // delay 10 Mnts ( as per Artik Free package specifications) 
    MQTT_Artik_Client.loop();
    toggle = !toggle;
    digitalWrite(BROADLEDPin, toggle);
    //Serial.println(buf);
    delay(6000);
  }  
  MQTT_Artik_Client.loop();
  delay(10); 
}

Quick Solutions to Questions related to Arduino MKR1000 ARTIK Cloud LED Project:

  • How do I connect the MKR1000 to ARTIK Cloud?
    Connect using Wi-Fi settings and establish an MQTT connection with the server api.artik.cloud on port 8883.
  • What libraries are required for this project?
    You need MQTT version 1.9.2, ArduinoJson, and WiFi101 libraries installed in the Arduino IDE.
  • Can I control the LED color remotely?
    Yes, you can receive data from ARTIK Cloud via subscription topics to change RED, GREEN, or BLUE LED states.
  • Does the MKR1000 have output limits?
    Yes, be careful as the MKR1000 output has a limit of 7mA.
  • What is the default LED lighting state in the code?
    The default state sets the RED LED to LOW and both GREEN and BLUE LEDs to HIGH.
  • How often does the code publish data by default?
    The loop publishes data every 10 minutes to comply with ARTIK Free package specifications.
  • Do I need to convert strings before parsing JSON?
    Yes, you must convert the received message string to a char array before calling parseObject.
  • How do I set up rules in ARTIK Cloud?
    Set rules to change LED states from RED to GREEN, GREEN to BLUE, and BLUE to RED every 10 minutes.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top