Spivey
Published © MIT

Publish Any Event to Wia Using Your MKR1000

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

BeginnerFull instructions provided1,146
Publish Any Event to Wia Using Your MKR1000

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
×1

Software apps and online services

Wia
Wia

Hand tools and fabrication machines

Wia Platform

Story

Read more

Schematics

MKR1000

Code

Publish an Event to Wia

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

Credits

Spivey

Spivey

82 projects • 59 followers
Tourist in a Tutu || US Born || Melbourne/Mexico/California Raised || New Yorker at ❤️ || SF to Dublin to be COO of Wia the best IoT startup

Comments