Spivey
Published © MIT

Publish Any Event to Wia Using Your ESP32

How to setup an Espressif ESP32 and publish an event to Wia within minutes!

BeginnerFull instructions provided1 hour585
Publish Any Event to Wia Using Your ESP32

Things used in this project

Hardware components

thingSoC ESP32 WiFi Module
thingSoC ESP32 WiFi Module
×1
Micro-USB to USB Cable (Generic)
Micro-USB to USB Cable (Generic)
×1

Software apps and online services

Wia
Wia

Story

Read more

Code

PublishEvent.ino

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);


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() {
  JsonObject& root = jsonBuffer.createObject();
  root["name"] = "temperature";
  root["data"] = 21.5;
Serial.println("connecting...");
  // if you get a connection, report back via serial:
  if (client.connect(server, port)) {
    Serial.println("connected");

    postToWia(root);

  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.available() && !client.connected()) {
    Serial.println();
    client.stop();

    delay(10000);
    //Do nothing forever
    for (;;)
      ;
  }
}

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
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