Start Connect ARTIK Cloud with Arduino/Genuino MKR1000

Start Connect ARTIK Cloud with Arduino/Genuino MKR1000 © GPL3+

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

  • 1,060 views
  • 0 comments
  • 4 respects

Components and supplies

Apps and online services

About this project

https://www.arduino.cc/en/Main/ArduinoMKR1000

A simple project let mkr1000 connect to artik cloud, but it took me 2 weeks time.

Since there are few tutorials for using MCU to connect artik cloud through WIFI, I write my way here. Or you can see example here (Not Easy to find the official tutorials).

step 1. Prepare:

You need

  • 1. mkr1000
  • 2. artik cloud account.
  • 3. arduino IDE 1.6.7 or higher
  • 4. WIFI ap
  • 5. Breadboard
  • 6. RGB LED (for this founction)

step 2. Libraries install

You need install arduino libraries

  • 1. MQTT version:1.9.2 author:Joel Gaehwiler
  • 2. ArduinoJson
  • 3. WiFi101

step 3. Build harware

Build like below ( careful mkr1000 output has limit 7mA)

step 4. ARTIK cloud setting

  • 1. Build device type at ARTIK cloud developer

Go to ARTIK cloud developer follow document build device like below

  • 2. Build device at ARTIK cloud

Go to ARTIK cloud use the device type we build above to build a device for your account.

step 5. Code for send data to ARTIK cloud

#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[] = "*********-mrk1000"; // Any Name  
char Device_ID[]   = "c0214c6df6fb4e539a7498cf06d07797"; // DEVICE ID 
char Device_TOKEN[]   = "************"; //  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  
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(); 
} 
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 loop() { 
loadBuffer(); 
MQTT_Artik_Client.publish(MQTT_Publish, buf);// Publishing data to the Artik Cloud  
Serial.println("Publishing.."); 
for (int i =0; i<100;i++){ // delay 10 Mnts ( as per Artik Free package specifications)  
  toggle = !toggle; 
  digitalWrite(BROADLEDPin, toggle); 
  delay(6000); 
}   
} 

This code connect MKR100 to your WIFI and send LED state to ARTIK cloud default RED LED lighting. LED on MKR100 will toggle for check.

step 6. Setting your rule at ARTIK cloud

Set every 10 minute to change LED state from RED to GREEN,

and GREEN to BULE、BULE to RED

step 7. Add code for receive data from ARTIK cloud

Receive data and change LED state with commend.

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

Finished

Here we have finish the project.

Notes

There have some point need to note.

  • 1. Make sure your message format is same with sample. You can check it at

device type. But I don't know how to check it with device type other user build.

  • 2. You need convert receive message from string to char before parse. I use 2 weeks to find it...
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"); 
} 
//**************************** 

Thank you.

Code

MKR1000 artik cloud ledArduino
#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); 
}

Schematics

common anode led
P 20160909 192957

Comments

Similar projects you might like

Temperature Monitoring with Arduino MKR1000 and ARTIK Cloud

Project tutorial by Team FoxLab

  • 7,516 views
  • 2 comments
  • 31 respects

Water Quality Monitoring Using MKR1000 and ARTIK Cloud

Project tutorial by Team Animo!

  • 7,081 views
  • 12 comments
  • 30 respects

Arduino MKR1000 - DHT - Artik cloud

by Team MAKERS-NS

  • 4,298 views
  • 2 comments
  • 20 respects

ARTIK + MKR1000 + DHT11 + MQTT

Project tutorial by vincent wong

  • 3,972 views
  • 12 comments
  • 10 respects

Temperature Logger With Arduino And Artik Cloud

Project tutorial by RAFI RASHEED T C

  • 1,917 views
  • 1 comment
  • 7 respects
Add projectSign up / Login