DEV Community

Conall Laverty
Conall Laverty

Posted on

Send Events from an ESP8266 to Wia

Hi All,

Today we're going to look at sending data from an ESP8266 to Wia. This ESP8266 IoT demo will go through all the steps required to go from scratch all the way to publishing your own events and locations. Let's get started!

Create Your Wia Account

If you haven't already got one, click here to sign up.

Setup a Space

Once logged in, setup a new Space for your devices. I'm going to call mine My Devices. When your Space is ready, you should see a screen like the one below.

Screen Shot 2018-01-01 at 12.38.44

Create a Device

Create a new Device by entering it's name in the panel. You will then be redirected to the Device Overview page. Here you will see the Device's secret key (it should start with d_sk). Keep note of this, we're going to use it later.

Screen Shot 2018-01-04 at 12.10.31

Connecting to the Board

Note: If your board has a micro USB port, you can probably skip this step.

Connect your board to laptop/desktop via a USB to TTL cable/adapter. Here's a pinout showing the different connections.

Wia Blog Illustration 1600x1052 03B

Connections should be like this:

  • ESP8266 ESP-01 <–> USB TTL
  • GND - GND
  • TX - RX
  • RX - TX
  • VCC - 3.3V (+)
  • CH_PD - 3.3V (+)
  • GPIO0 - GND (While flashing board)

Note: You may need to install FTDI drivers to make your device appear. Check with the manufacturer of your adapter for the latest versions. If you get stuck here, tweet us for support.

Setup Your Environment

  • Install the Arduino IDE. You can download it for Mac OS X, Windows and Linux here.
  • Start the Arduino application and open Preferences.
  • Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into the Additional Board Manager URLs. If you need more than one, they can be separated with commas.

Screen Shot 2018-01-04 at 12.03.11

  • Go to Tools > Board > Boards Manager.
  • Search for esp8266. When found, click Install.

Screen Shot 2018-01-04 at 12.04.59

  • Select your ESPP8266 board type by going to Tools > Board, then choosing your type. For this example I am using NodeMCU 1.0 (ESP-12E Module).
  • Note: Check that Upload Speed is set to 115200 and the correct Port is selected.

Screen Shot 2018-01-04 at 12.06.55

Create the Sketch

  • Click on File > New to create a new Sketch.
  • Copy and paste the Publish Event example code below. You can also view it on GitHub here. In that directly you will see how to publish both Events and Locations.
/**
 * PublishEvent.ino
 *
 *  Created on: 09.01.2017
 *
 */

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#define USE_SERIAL Serial

ESP8266WiFiMulti WiFiMulti;

const char* ssid     = "your-ssid";
const char* password = "your-password";

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

void setup() {
    USE_SERIAL.begin(115200);
    //USE_SERIAL.setDebugOutput(true);

    WiFi.mode(WIFI_STA);
    WiFiMulti.addAP(ssid, password);
}

void loop() {
    // wait for WiFi connection
    if((WiFiMulti.run() == WL_CONNECTED)) {
        HTTPClient http;

        USE_SERIAL.print("[HTTP] begin...\n");

        // configure wia rest api
        http.begin("http://api.wia.io/v1/events");

        USE_SERIAL.print("[HTTP] POST...\n");

        // set authorization token
        http.addHeader("Authorization", "Bearer " + String(device_secret_key));

        // set content-type to json
        http.addHeader("Content-Type", "application/json");

        // start connection and send HTTP headers. replace name and data values with your own.
        int httpCode = http.POST("{\"name\":\"temperature\",\"data\":21.5}");

        // httpCode will be negative on error
        if(httpCode > 0) {
            // HTTP header has been send and Server response header has been handled
            USE_SERIAL.printf("[HTTP] POST... code: %d\n", httpCode);

            // file found at server
            if(httpCode == HTTP_CODE_OK) {
                String payload = http.getString();
                USE_SERIAL.println(payload);
            }
        } else {
            USE_SERIAL.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }

        http.end();
    }

    delay(10000);
}
Enter fullscreen mode Exit fullscreen mode

Replace the following values of the following variables:

  • ssid with your WiFi network name.
  • password with your WiFi network password.
  • device_secret_key with your device secret key from earlier (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 ESP8266.

Screen Shot 2018-01-04 at 12.16.34

That's it folks!

If you need any help with getting setup, tweet us or email support@wia.io

References

Espressif Systems are a world-leading Internet-of-Things company. They built the widely popular ESP8266 and ESP32 chips. They are an innovative team of chip-design specialists, software/firmware developers and marketers. And they are committed to providing the best IoT devices and software platforms in the industry.

Top comments (0)