How to setup an Arduino UNO with ESP8266 and publish an event to Wia
Tags: iot, arduino, opensource, wia
Published: 2018-05-28 09:18:04

---
title: How to setup an Arduino UNO with ESP8266 and publish an event to Wia
published: true
description: Setup an Arduino UNO with ESP8266 and publish an event to Wia
tags: #Iot #arduino #opensource #wia
---
![alt text](https://s3-eu-west-1.amazonaws.com/wia-flarum-bucket/2018-05-29/1527605155-310926-blog-01.png)

###**Components**

The following components are needed to complete this tutorial:

* Arduino Uno
* ESP 8266
* Micro USB to USB cable
* Generic bread board
* Jumper cables (Male and Female)

###**Setup your environment**
* Install the Arduino IDE (Integrated development environment) which you can download 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: If you are completely unfamiliar with the Arduino IDE, watch [this video](https://www.youtube.com/watch?v=nbD_V4QtNvY) to give you a better understanding of how it works.


In the Arduino IDE: 
* Go to menu: `Tools > Board > Boards Manager`
* Search for `Arduino AVR Boards` when found, click `Install`
![alt text](https://s3-eu-west-1.amazonaws.com/wia-flarum-bucket/2018-05-22/1526995154-868786-selection-119.png)
* Select the `Arduino\Genuino Uno` board type by going to `Tools > Board`
* Select the port that displays `Arduino\Genuino Uno`

If no name is displayed, you can find the port with the following steps:

####**LINUX AND MAC OS X**
* Download and install the FTDI drivers from [here](https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers). Select the appropriate version for your operating system and architecture
* Open a terminal window and run the command `ls /dev/tty*`
* Look for a device with the name that begins with `/dev/tty` e.g. `/dev/tty.usbmodemPy343431` on MAC or `/dev/ttyUSB0/dev/ttyACM0` on Linux

Note: For Linux, you may need to run the two commands below. Once you've completed that, reboot your computer. This will add permissions that will allow you to upload a sketch to the board.

`sudo usermod -a -G tty ${USER}`
`sudo usermod -a -G dialout ${USER}`

**WINDOWS**
* Download and install the FTDI drivers from [here](https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers). Select the appropriate version for your operating system and architecture
* Open the Windows start menu and search for `Device Manager`
* The COM port for the Pycom device will be listed as `USB Serial Device` or something similar
  * Keep note of the COM port (e.g. COM4)

###**Connecting the hardware**
To connect the Arduino Uno to the ESP 8266 you'll need to connect the following pins: 
![alt text](https://s3-eu-west-1.amazonaws.com/wia-flarum-bucket/2018-05-22/1526995407-139516-unoesppins.png)

As the Ardnuino Uno takes male jumper cables while the ESP 8266 requires female jumper cables, a bread board can be used to make the connections. Its also useful as both the `CH_PD` and `VCC` pins on the ESP 8266 require power. 

![alt text](https://s3-eu-west-1.amazonaws.com/wia-flarum-bucket/2018-05-22/1526996228-643696-2d9eabd-unobread.png)

###**Create a new sketch**
In the Arduino IDE:
* Click on `File > New` to create a new Sketch
* bCopy and paste the `publishEvent.ino` from the example code below:
```
#include <SoftwareSerial.h>
#include <ArduinoJson.h>

#define RX 10
#define TX 11
String WIFI_SSID = "your-wifi-ssid";       // Your WiFi ssid
String PASSWORD = "your-wifi-password";         // Password
String DEVICE_SECRET_KEY  = "your-device-secret-key";

String HOST = "api.wia.io";
String PATH = "/v1/events";
String PORT = "80";

int countTrueCommand;
int countTimeCommand;
boolean found = false;

SoftwareSerial esp8266(RX, TX);

StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();

void setup() {
  Serial.begin(9600);
  esp8266.begin(115200);
  esp8266.println("AT");
  Serial.println(esp8266.read());
  sendCommandToESP8266("AT", 5, "OK");
  sendCommandToESP8266("AT+CWMODE=1", 5, "OK");
  sendCommandToESP8266("AT+CWJAP=\"" + WIFI_SSID + "\",\"" + PASSWORD + "\"", 20, "OK");
}

void loop() {
  root["name"] = "temperature";
  root["data"] =  21.5;
  String data;
  root.printTo(data);

  String postRequest = "POST " + PATH  + " HTTP/1.1\r\n" +
                       "Host: " + HOST + "\r\n" +
                       "Accept: *" + "/" + "*\r\n" +
                       "Content-Length: " + data.length() + "\r\n" +
                       "Content-Type: application/json\r\n" +
                       "Authorization: Bearer " + DEVICE_SECRET_KEY + "\r\n" +
                       "\r\n" + data;

  sendCommandToESP8266("AT+CIPMUX=1", 5, "OK");
  sendCommandToESP8266("AT+CIPSTART=0,\"TCP\",\"" + HOST + "\"," + PORT, 15, "OK");
  String cipSend = "AT+CIPSEND=0," + String(postRequest.length());
  sendCommandToESP8266(cipSend, 4, ">");
  sendData(postRequest);
  sendCommandToESP8266("AT+CIPCLOSE=0", 5, "OK");
}

void sendCommandToESP8266(String command, int maxTime, char readReplay[]) {
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while (countTimeCommand < (maxTime * 1))
  {
    esp8266.println(command);
    if (esp8266.find(readReplay))
    {
      found = true;
      break;
    }

    countTimeCommand++;
  }

  if (found == true)
  {
    Serial.println("Success");
    countTrueCommand++;
    countTimeCommand = 0;
  }

  if (found == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }

  found = false;
}

void sendData(String postRequest) {
  Serial.println(postRequest);
  esp8266.println(postRequest);
  delay(1500);
  countTrueCommand++;
}
```
* Add the `WIFI_SSID` and `PASSWORD`
* Add the `device_secret_key` with your device secret key from the Wia Dashboard (the one that begins with `d_sk`)
* `Sketch > Upload` to send it to your Uno

Go to the [Wia dashboard](https://dashboard.wia.io/spaces) to view the data coming through to your device.

If you need any help with getting setup or you don't understand the tutorial, tweet us, email [support@wia.io](support@wia.io) or chat on Intercom.


###**Common errors**
If all the AT commands are failing, it may be due to your ESP 8266 have an outdated firmware version. 
Please follow the tutorial [here](https://nodemcu.readthedocs.io/en/latest/en/flash/) to flash your ESP8266 with an ESP flasher tool.