Project tutorial
Publish Any Event to Wia Using Your MKR1000

Publish Any Event to Wia Using Your MKR1000 © MIT

How to setup an MKR1000 and publish an event or location to Wia.

  • 497 views
  • 1 comment
  • 4 respects

Components and supplies

Necessary tools and machines

Apps and online services

About this project

Setup Your Environment

  • Install the Arduino IDE (Integrated development environment). This is whereYou can download it for Mac OS X, Windows and Linux here.

Detailed instructions can be found below:

New to the Arduino IDE?

If you are completely unfamiliar with the Arduino IDE, watch this video here to give you a better understanding of how it works.

In the Arduino IDE:

  • Go to menu: Tools > Board > Boards Manager
  • Search for Arduino SAMD. When found, click Install

Select board and port

In the Arduino IDE:

  • Select the Arduino/Genuino MKR1000 board type by going to Tools > Board
  • Select the port that displays Arduino/Genuino MKR1000

If no name is displayed, you can find the port with the following steps:

Linux and Mac OS X

  • Download and install the FTDI drivers from here. Select the appropriate version for your operating system and architecture.
  • Open a terminal window and run the command ls /dev/tty*
  • Look for a device with the name that begins with /dev/tty e.g. /dev/tty.usbmodemPy343431 on MAC or /dev/ttyUSB0/dev/ttyACM0 on Linux.

For Linux, you may need to run the two commands below. Once you've completed that, reboot your computer. This will add permissions that will allow you to upload a sketch to the board.

sudo usermod -a -G tty ${USER}sudo usermod -a -G dialout ${USER}

Windows

  • Download and install the FTDI drivers from here. Select the appropriate version for your operating system and architecture.
  • Open the Windows start menu and search for Device Manager
  • The COM port for the Pycom device will be listed as USB Serial Device or something similar
  • Keep note of the COM port (e.g. COM4)

Install the required libraries

To complete the tutorial, additional libraries of functionality are required, these can be installed through the Arduino IDE in the following way:

In the Arduino IDE:

  • Go to Sketch > Include Libraries > Manage Libraries

In the search bar, Search for the below libraries. When found, click on the library. A button will appear in the bottom right of the box that will allow you to install the library.

  • WiFI101
  • ArduinoHttpClient
  • ArduinoJson

Create a new Sketch

In the Arduino IDE:

  • Click on File > New to create a new Sketch
  • Copy and paste the publishEvent.ino or publishLocation.ino from the example code
  • Add the ssid and password of your WiFI
  • Add the device_secret_key with your device secret key from the Wia Dashboard (the one that begins with d_sk).

In your Arduino IDE:

  • Go to Sketch > Upload to send it to your MKR 1000

Now go to your device in the Wia dashboard and you should see the data appearing in the debugger.

Code

Publish an Event to WiaArduino
#include <WiFi101.h>
#include <ArduinoJson.h>
#include <ArduinoHttpClient.h>


const char WIFI_SSID[] = "your-wifi-ssid"; // WiFI ssid 
const char WIFI_PASS[] = "your-wifi-password"; //WiFI password

// get this from the wia dashboard. it should start with `d_sk`
const char* device_secret_key = "your-device-secret-key";

WiFiClient client;
int status = WL_IDLE_STATUS;

// Wia API parameters
char server[] = "api.wia.io";
char path[] = "/v1/events";
int port = 80;


StaticJsonBuffer<200> jsonBuffer;
HttpClient httpClient = HttpClient(client, server, port);
JsonObject& root = jsonBuffer.createObject();

void setup() {

  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("Starting WiFI connection to Wia!.");
 
 // 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 SSID: ");
    Serial.println(WIFI_SSID);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(WIFI_SSID, WIFI_PASS);

    // wait 5 seconds for connection:
    delay(5000);
  }
  
}

void loop() {
  root["name"] = "temperature";
  root["data"] = 21.5;

  // if you get a connection, report back via serial:
  if (client.connect(server, port)) {

    postToWia(root);

  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
   delay(3000); // Wait for 3 seconds to post again
}

void postToWia(JsonObject& data){
    size_t len = data.measureLength();
    size_t size = len+1;
    char json[size];
    httpClient.beginRequest();
    httpClient.post(path);
    httpClient.sendHeader("Content-Type", "application/json");
    httpClient.sendHeader("Content-Length", data.measureLength());
    httpClient.sendHeader("Authorization", "Bearer "  + String(device_secret_key));
    httpClient.beginBody();
    data.printTo(httpClient);
    httpClient.endRequest();
  
  }

Schematics

MKR1000
Mkr1000 pinout eo8w1bijoi

Comments

Similar projects you might like

Publish Any Event to Wia Using Your MKR GSM 1400

Project tutorial by Spivey

  • 988 views
  • 0 comments
  • 2 respects

Publish Any Event to Wia Using Your MKR FOX 1200

Project tutorial by Spivey

  • 467 views
  • 0 comments
  • 2 respects

Dash Button Santa with Arduino MKR1000

Project tutorial by Team Programarfacil

  • 1,812 views
  • 0 comments
  • 7 respects

Send MKR1000 Data to Google Sheets

Project tutorial by Stephen Borsay

  • 10,004 views
  • 28 comments
  • 31 respects

Using Python and Arduino MKR1000 for Secure IoT

Project tutorial by Luigi Francesco Cerfeda

  • 9,766 views
  • 1 comment
  • 28 respects

Arduino MKR1000 Weather Station

Project tutorial by Tadeas Dvoracek

  • 8,299 views
  • 4 comments
  • 22 respects
Add projectSign up / Login