Send Events from an ESP8266 to Wia  
Tags: iot, esp8266
Published: 2018-01-08 00:10:21

---
title: Send Events from an ESP8266 to Wia  
published: true
description: Start sending sensor and location data from your ESP8266 board in minutes
tags: iot, esp8266
---
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](https://dashboard.wia.io/signup "Create your Wia account") 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](//images.contentful.com/i5evrz7h2uo7/1bfPDxMf4s0SyaOSkCEsum/f0dc8f7ebe0076a222b495d36376f4f7/Screen_Shot_2018-01-01_at_12.38.44.png)

### 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](//images.contentful.com/i5evrz7h2uo7/5UKUiYakvu2yCWAGkwOCa6/cbda91da4eedf6c0fe1b324a34e38987/Screen_Shot_2018-01-04_at_12.10.31.png)

### 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](//images.contentful.com/i5evrz7h2uo7/1zvTmj97OQAc46cuC4coGu/34ab2fc5e04dde88b3455b84e7fd8ae1/Wia_Blog_Illustration_1600x1052_03B.png)

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](https://www.twitter.com/wiaio "Wia Twitter") for support.

### Setup Your Environment
- Install the Arduino IDE. You can download it for Mac OS X, Windows and Linux [here](https://www.arduino.cc/en/main/software "Arduino IDE Download"). 
- 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](//images.contentful.com/i5evrz7h2uo7/1nAulaFjAsImkUIOmMuYk6/1aad804141f349bb44dbe6a477d01f48/Screen_Shot_2018-01-04_at_12.03.11.png)

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

![Screen Shot 2018-01-04 at 12.04.59](//images.contentful.com/i5evrz7h2uo7/6hPtzz0sQ8Ue4yEkCQCawm/77fae51d3f6e065145e147418dc94c22/Screen_Shot_2018-01-04_at_12.04.59.png)  

- 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](//images.contentful.com/i5evrz7h2uo7/1tKBKsaU7ew8qmMwqiC2oI/0a3d20c07f7b6a70371c1baa9349fbf3/Screen_Shot_2018-01-04_at_12.06.55.png)

### 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](https://github.com/wiaio/wia-board-examples/tree/master/Arduino/ESP8266). 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);
}
```

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](//images.contentful.com/i5evrz7h2uo7/3zDozXJcEEC8mGkAIUgqWE/fe893983954906786622377197bf18f6/Screen_Shot_2018-01-04_at_12.16.34.png)  

That's it folks!

If you need any help with getting setup, [tweet us](https://twitter.com/wiaio) or email [support@wia.io](mailto:support@wia.io)


*References*  
[Espressif Systems](https://www.espressif.com "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.