Setup an Espressif ESP8266 and Publish an Event to Wia
Tags: esp8266, cloud, tutorial, beginners
Published: 2018-06-14 14:03:21

---
title: Setup an Espressif ESP8266 and Publish an Event to Wia
published: true
description: How to setup an Espressif ESP8266 and publish an event to Wia
tags: #esp8266 #cloud #tutorial #beginner
---
###**Connecting to the Board**
`Note:If your board has a micro USB port (as with the NodeMCU breakout boards), you can skip this step, and go to the "Setup Your Environment" section below.`

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

![alt text](https://files.readme.io/675b4ca-Wia_Blog_Illustration_1600x1052_03B_1.png)
*ESP8266 Pinout*

Connections between the ESP and USB TTL are the following:

* ESP8266 <-> USB TTL
* GND <-> GND
* TX <-> RX
* VCC <-> 3.3V(+)
* CH_PD <-> 3.3V(+)
* GPIO0 <-> GND (Only while flashing the board)

###**Setup Your Environment**
* Install the Arduino IDE (Integrated development environment). This is where you can download it for Mac OS X, Windows and Linux [here](https://www.arduino.cc/en/main/software).

Detailed instructions can be found below:
* [Installing the Arduino IDE for Windows](https://www.arduino.cc/en/main/software)
* [Installing the Arduino IDE for Mac](https://www.arduino.cc/en/Guide/MacOSX)
* [Installing the Arduino IDE for Linux](https://www.arduino.cc/en/Guide/Linux)

`Note: New to the Arduino IDE?
If you are completely unfamiliar with the Arduino IDE, watch this video [here](https://www.youtube.com/watch?v=nbD_V4QtNvY) to give you a better understanding of how it works.`

* 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

![alt text](https://files.readme.io/90719ce-Screen_Shot_2018-01-04_at_12.03.11.png)
*Install the ESP8266 Arduino package*

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

![alt text](https://files.readme.io/5293598-Screen_Shot_2018-01-04_at_12.04.59.png)

* Select your ESP8266 board type by going to `Tools > Board`, then choosing your type. For this example, you can use `NodeMCU 1.0 (ESP-12E Module)`
* Check that `Upload Speed` is set to `115200`
* Select the correct port for the board

####**Note: When selecting a port**
For the ESP 8266, the port names tend to have unusual names, e.g:
    On Mac OS: `/dev/tty.usb.modem8232` or `/dev/cu.SLAB_USB_toUART`
    On Windows: `Com4` or `Com3`
    On Linux `/dev/ttyUSB0` or /`dev/ttyACM0`

![alt text](https://files.readme.io/e0d9624-Screen_Shot_2018-01-04_at_12.06.55.png)

####**USB to UART Drivers Required**
If your port is not showing up, you need to install USB to UART Bridge VCP drivers. To do so, go [here](https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers), then download and install the drivers for your operating system. After installing the drivers, restart the Arduino IDE.

#####**Note for Mac users**
You may need to install the driver twice for it to work, as the first install will just remove the existing driver.

#####**Charge-only USB cables**
Another cause of the port not showing up could be the USB cable - make sure that the cable you're using is not a charge-only cable, and that it isn't faulty.

###**Create the Sketch**
####Measuring soil moisture with the NodeMCU Amica
If you came from our 'measuring soil moisture' tutorial, continue with that tutorial [here](https://community.wia.io/d/20-measuring-soil-moisture-with-the-nodemcu-amica-esp8266). Otherwise, please continue with the tutorial below.

* 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*`
```
 /**
 * 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 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 the Wia Dashboard (the one that begins with d_sk).
* Go to `Sketch > Upload` to send it to your ESP8266.
Now go to your device in the Wia dashboard and you should see the data appearing in the debugger.

![alt text](https://files.readme.io/4fed030-Debugger_Wia.png)
*Wia Device Debugger*

###**Common errors**
If you encounter an error like the one denoted in the image below, check if your board and port are correctly set in the `Tools` menu.

![alt text](https://files.readme.io/9d72425-image_16.png)
*Board or Port Failure*