How to setup an Arduino UNO with ESP8266 and publish an event to Wia
Austin Spivey
Posted on May 28, 2018
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
Detailed instructions can be found below:
- Installing the Arduino IDE for Windows
- Installing the Arduino IDE for Mac
- Installing the Arduino IDE for Linux Note: If you are completely unfamiliar with the Arduino IDE, watch this video 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, clickInstall
- Select the
Arduino\Genuino Uno
board type by going toTools > 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. 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. 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:
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.
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
andPASSWORD
- Add the
device_secret_key
with your device secret key from the Wia Dashboard (the one that begins withd_sk
) -
Sketch > Upload
to send it to your Uno
Go to the Wia dashboard 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 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 to flash your ESP8266 with an ESP flasher tool.
Posted on May 28, 2018
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.