Arduino MKR1000 Tutorial – How to use Arduino MKR1000 in IoT projects

This Arduino MKR1000 tutorial describes how to program Arduino to send tweets. In more details,  in this Arduino project, we will use Arduino MKR1000. This is an interesting Internet of things project where Arduino MKR1000 will use cloud services to send tweets.  During the Arduino programming tutorial, we will use Temboo that provides a set of interesting services in order to send tweets.

What will we learn during this Arduino MKR1000 tutorial?

We will learn several aspects related to the Arduino MKR1000 as:

  • How to connect DHT11 and BMP180 to Arduino MRK1000
  • How to send data to the cloud

Arduino MKR1000 tutorial Project Overview

Before digging into the Arduino MKR1000 tutorial details and describing how to invoke a cloud service from Arduino MKR1000, it is useful to have a project overview.
This Arduino IoT project exploits two different sensors:

  • A temperature and humidity sensor connected to Arduino (DHT11/DHT22)
  • A pressure sensor connected to Arduino (BMP 180)

The figure below shows the wiring details, describing the connections between MKR1000 and these two sensors:

Arduino MKR1000 tutorial

Moreover, this project uses Temboo IoT platform and Twitter so if you want to test it you have to create two free accounts.

Arduino MKR1000 Specifications

As you may already know Arduino MKR1000 is the last dev board specifically made for IoT projects. Arduin MKR1000 offers a practical solution for makers that want to build IoT projects. This dev board has WiFi built-in. Arduino MKR1000 uses Atmel ATSAMW25 SoC. This SoC is specifically designed for IoT projects. The main features of this SoC are:

  • 32-bit
  • 48 MHz
  • ARM core
  • 3.3 V

It is important to note that this dev board uses 3.3V instead of 5V as Arduino Uno. It is important to remember it because the I/O pin voltage must be lower than 3.3V otherwise there is the risk to damage the board.

Read Temperature Humidity Pressure using Arduino MKR1000

As described in the post how to program Arduino to monitor environment, to simplify the work we can import the DHT11 Library. The Arduino programming code to read data from the temperature sensor and the barometric sensor is shown below:

[cpp] #include "DHT.h"
#include <Adafruit_BMP085.h>

#define DHTPIN 2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP085 bmp;
void setup() {
dht.begin();
bmp.begin();

}

void loop() {
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
float presPa = bmp.readPressure();
float presMb = presPa * 1013.25 / 101325;
}
[/cpp]

In this sketch, the DHT11 sensor is connected to the PIN 2, while BMP180 has 4 connections: GROUND, VCC(3.3V), SDA, and CLOCK. The connection with MKR1000 is very easy.

How to configure Twitter to allow connections from Arduino

To use Twitter from Arduino MKR1000, it is fundamental to create an app on Twitter. To this purpose, it is just necessary to provide a little information. Once the app is configured providing all the information required,  we can retrieve the keys to authenticate our Arduino MKR1000.

The first step is creating the twitter app:
twitter create app

The next step is creating the keys to use later in Temboo:

twitter access token

twitter token

Ok..remember the code above because you have to use it in your Temboo configuration.

Now it is time to go to the Arduino code. As a first step, it is necessary to provide information to Temboo choreo. This process is simple, you have just to copy and paste the values retrieved during the app configuration step:

configure tempo and MKR1000

 

How to program Arduino to send tweets

Before starting to send tweets from Arduino, it is necessary to describe how to connect Arduino to the WiFi.

How to connect Arduino MRK1000 to the WiFi

Before sending tweets invoking Temboo services, it is necessary to establish a WiFi connection using the code below:

[cpp] #include <SPI.h>
#include <WiFi101.h>
char ssid[] = "xxxxx"; // your network SSID (name)
char pass[] = "pppp"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio’s status

WiFiClient client;

void setup() {
Serial.begin(9600);
Serial.print("Starting…");
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don’t continue:
while (true);
}

// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);
}
[/cpp]

This code is the same used in the Arduino MKR1000 examples. Replace the ssid and the pass variable with the value used in your WiFi network. Now everything is ready and we can use the choreo code to send tweets:

[sociallocker] [cpp] void loop() {
String msgText = "Temp is" + String(t,2) + "C and humidity " + String(h,2)
+ " Press is " + String(presMb,2) + ". Have fun!";

Serial.println("Msg ["+msgText+"]");
TembooChoreo SendDirectMessageChoreo(client);

// Invoke the Temboo client
SendDirectMessageChoreo.begin();

// Set Temboo account credentials
SendDirectMessageChoreo.setAccountName(TEMBOO_ACCOUNT);
SendDirectMessageChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
SendDirectMessageChoreo.setAppKey(TEMBOO_APP_KEY);

// Set Choreo inputs
String AccessTokenValue = "……";
SendDirectMessageChoreo.addInput("AccessToken", AccessTokenValue);
String ConsumerKeyValue = "…….";
SendDirectMessageChoreo.addInput("ConsumerKey", ConsumerKeyValue);
String ConsumerSecretValue = "……";
SendDirectMessageChoreo.addInput("ConsumerSecret", ConsumerSecretValue);
String ScreenNameValue = "survivingwithan";
SendDirectMessageChoreo.addInput("ScreenName", ScreenNameValue);
String TextValue = "Temp is…"; // Add the value read from sensors
SendDirectMessageChoreo.addInput("Text", msgText);
String AccessTokenSecretValue = "….";
SendDirectMessageChoreo.addInput("AccessTokenSecret", AccessTokenSecretValue);

// Identify the Choreo to run
SendDirectMessageChoreo.setChoreo("/Library/Twitter/DirectMessages/SendDirectMessage");

// Run the Choreo; when results are available, print them to serial
SendDirectMessageChoreo.run();

while(SendDirectMessageChoreo.available()) {
char c = SendDirectMessageChoreo.read();
Serial.print(c);
}
SendDirectMessageChoreo.close();

Serial.println("\nWaiting…\n");
}
[/cpp] [/sociallocker]

Running the sketch on Arduino MKR1000 the result is shown below:

 

mrk1000 tweet message

and

 

tweet from mkr1000

 

At the end of this Arduino tutorial, hopefully, you know how to create an Arduino IoT project using Arduino MKR1000 and how to integrate Arduino MKR1000 with external systems. Moreover, you know how to invoke a remote service using Arduino MRK1000. This Arduino MKR1000 project can be further extended and we can use the same method to send other kinds of alerts like alarms or we can acquire different data type using other sensors. Moreover, this Arduino tutorial describes how to use Arduino temperature sensor DHT11 and how to use the bmp180 pressure sensor.