Project tutorial
Publish Any Event to Wia Using Your MKR GSM 1400

Publish Any Event to Wia Using Your MKR GSM 1400 © MIT

How to set up a MKR GSM 1400 and publish an event or location to Wia.

  • 1,495 views
  • 0 comments
  • 5 respects

Components and supplies

Abx00018 featured wi0j4nqatf
Arduino MKR GSM 1400
×1
Sim Card
Can be any carrier
×1
Antenna
×1
Adafruit industries ada592 image 75px
USB-A to Micro-USB Cable
×1

Apps and online services

About this project

Requirements

  • Your chosen SIM card must have access to the internet
  • Both the antenna and SIM card must be attached to the board.

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 your MKR GSM 1400 board type by going to Tools > Board
  • Select the port that displays MKR GSM 1400If 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)

Create a new Sketch

  • Click on File > New to create a new Sketch
  • Copy and paste the publishEvent.ino or publishLocation.ino from the example code below
  • If your SIM card has a pin, enter it in the PINUMBER field or else leave that field blank
  • To access your Access Point Name(APN), ask your network carrier for the details
  • Alternatively, you can attach your SIM card to your phone, check settings for the APN of the carriernetwork
  • Add the device_secret_key with your device secret key from the Wia Dashboard (the one that begins with d_sk)
  • Verify/Compile the code. If it runs correctly then go to Sketch > Upload to send it to your MKR GSM 1400

Go to the Wia dashboard to view the data coming through to your device.

Code

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

// PIN Number
const char PINNUMBER[]     = ""; //blank if no pin
// APN data: check settings on mobile for sim provider or contact carrier for network settings
const char GPRS_APN[]      = "";
const char GPRS_LOGIN[]    = "";
const char GPRS_PASSWORD[] = "";

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

GSMClient client;
GPRS gprs;
GSM gsmAccess;

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


StaticJsonBuffer<512> jsonBuffer;
HttpClient httpClient = HttpClient(client, server, port);
JsonObject& root = jsonBuffer.createObject();
String response;
int statusCode = 0;
String dataStr;
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 GSM connection to Wia!.");
  // connection state
  boolean connected = false;

  // After starting the modem with GSM.begin()
  // attach the shield to the GPRS network with the APN, login and password
  while (!connected) {
    if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
        (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("connecting...");
  // if you get a connection, report back via serial:
  if (client.connect(server, port)) {
    Serial.println("connected");
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

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");
  }
  // read the status code and body of the response
  statusCode = httpClient.responseStatusCode();
  response = httpClient.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);

  delay(3000); // Wait for 3 seconds to post again
}

void postToWia(JsonObject& data) {
  data.printTo(dataStr);
  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();
  httpClient.print(dataStr);
  httpClient.endRequest();

}

Schematics

MKR GSM 1400
Mkrgsm1400 bb qap98mwzb8

Comments

Similar projects you might like

Publish Any Event to Wia Using Your MKR FOX 1200

Project tutorial by Spivey

  • 465 views
  • 0 comments
  • 2 respects

Publish Any Event to Wia Using Your MKR1000

Project tutorial by Spivey

  • 567 views
  • 1 comment
  • 4 respects

Arduino MKR GSM 1400 and DTMF

by Arduino_Genuino

  • 16,494 views
  • 0 comments
  • 28 respects

MKR GSM 1400 Send Sensor Data to ThingSpeak via JSON

Project tutorial by smgs

  • 1,435 views
  • 0 comments
  • 2 respects

Program MKR Over-the-Air + Goodies: Voice Control, etc.

Project tutorial by Team Flower Platform

  • 9,877 views
  • 7 comments
  • 38 respects

Create Mobile Apps for Android and iOS Connecting to the MKR

Project tutorial by Chandana Pathirage

  • 5,870 views
  • 4 comments
  • 16 respects
Add projectSign up / Login