Publish Any Event to Wia Using Your MKR1000

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

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

Schematics

Code

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

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top