Spivey
Published © MIT

Publish Any Event to Wia Using Your SparkFun ESP32 Thing

Learn how to set up your SparkFun ESP32 thing and post an event to Wia in a few easy steps.

EasyFull instructions provided1 hour90
Publish Any Event to Wia Using Your SparkFun ESP32 Thing

Things used in this project

Hardware components

SparkFun ESP32 Thing
SparkFun ESP32 Thing
×1
Micro-USB to USB Cable (Generic)
Micro-USB to USB Cable (Generic)
×1

Software apps and online services

Wia
Wia

Story

Read more

Schematics

Sparkfun ESP32 Thing

13907 02 t00bdtj19n

Code

Publish an Event to Wia

Arduino
#include <ArduinoJson.h>
#include <WiFi.h>
#include <ArduinoHttpClient.h>
#include <Arduino.h>


const char* ssid     = ""; // Your WiFi ssid
const char* password = ""; //Your Wifi password

WiFiClient client;
int status = WL_IDLE_STATUS;

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

// 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(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, password);
    // wait 10 seconds for connection:
    delay(10000);
  }
  
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

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);
  data.printTo(Serial);
  httpClient.endRequest();

}

Credits

Spivey

Spivey

44 projects • 23 followers
Tourist in a Tutu || US Born || Melbourne/Mexico/California Raised || New Yorker at ❤️ || SF to Dublin to work for Wia the best #IoT startup

Comments