Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Tweet From Arduino MKR1000

DZone's Guide to

How to Tweet From Arduino MKR1000

Why Tweet from your phone when you can Tweet from the cloud? This post describes how to use Arduino MKR1000 to send tweets.

· IoT Zone
Free Resource

As a cloud platform, this tutorial uses Temboo that provides a set of interesting services.

Send Tweets From Arduino MRK1000: Project Overview

Before digging into the project details and how to invoke a cloud service from Arduino MKR1000, it is useful to have a project overview. In this project Arduino MKR1000 is connected to two different sensors:

  • DHT11 to measure the temperature and humidity
  • BMP180 to measure the pressure

The figure below shows the schematics describing the connections between MKR1000 and the two sensors. The image below shows the dev boards with the connections.

mrk1000 and sensors


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

send tweets from arduino mkr1000

Arduino/Genuino MKR1000

As you may already know Arduino/Genuino MKR1000 is the last dev board specifically made for IoT projects. It is designed to offer a practical solution for makers that want to build IoT projects. This dev board has WIFI built-in. Arduino/Genuino 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 of damaging it. MKR1000 has a built-in WIFI module so it is very simple to connect it to the network without using shield.

Read Temperature Humidity Pressure Using MKR1000

You should already know how to read temperature and humidity using MKR1000. Otherwise, remember to import DHT11 library to simplify the work. To read temperature, humidity and pressure the sketch uses this piece of code:


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


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

Create Twitter App

To send tweets from Arduino MKR1000, it is fundamental to create a twitter app. To create an app, it is necessary to provide some information. This information is useful to get 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.

Tweets From Arduino MKR1000: Sketch

Now it is time to go to the Arduino code and implement send tweets from Arduino MKR1000. 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.

configure tempo and MKR1000


To establish a WIFI connection is very simple:


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


This code is the same used in the Arduino MKR1000 examples. Now everything is ready to import the Choreo code:


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 = "test";
    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");
}


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

mrk1000 tweet message

and

tweet from mkr1000
At the end of this post, you know how to send tweets from Arduino MKR1000 and how to invoke a remote service from Arduino. There are other applications of this project, such as sending alarms or other kinds of information.

Topics:
tweets ,arduino ,mkr1000 ,temboo ,cloud

Published at DZone with permission of Francesco Azzola, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}