In this tutorial, we'll go through setting up an ESP8266 board, and publishing an Event to Wia via our MQTT API.
Connecting 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.
- ESP8266 <-> ESP-01 USB TTL
- GND <-> GND
- TX <-> RX
- VCC <-> 3.3V(+)
- CH_PD <-> 3.3V(+)
- GPIO0 <-> GND (Only while flashing the board)
Note: You may need to install FTDI drivers to make your device appear. To do so, go here then download and install the drivers for your operating system.
Set up 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
- Go to
Tools > Board > Boards Manager
- Search for
esp8266
. When found, clickInstall
Select your ESP8266 board type by going to Tools > Board
, then choosing your type. For this example I am using NodeMCU 1.0 (ESP-12E Module)
.You can get the name of your device's port using one of the following steps:
Linux and Mac OS X
- 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
Windows
- Download and install the FTDI drivers from here
- Open the Windows start menu and search for 'Device Manager'
- The COM port for the ESP32 device will be listed as 'USB Serial Device' or something similar
- Keep note of the COM port (e.g. COM4)
Note: Check thatUpload Speed
is set to115200
and the correct Port is selected.
- Keep note of the COM port (e.g. COM4)
Install the required libraries
In the Arduino IDE, Go to Sketch > Include Libraries > Manage Libraries
.
Install each of the following libraries by searching for their name in the search bar within the modal. A button will appear in the bottom right of the box that will allow you to install the library.
- ESP8266WiFi
- PubSubClient
- ArduinoJson
Add a Device and Command in Wia
If you haven't already, you'll need create a Device in your Wia account. If you're new to Wia, check out our tutorial here.
Create the Sketch
- Click on
File > New
to create a new Sketch - Copy and paste the example code below. You can also view it on GitHub here. This code publishes an event to your Device every 10 seconds
#include <ESP8266WiFi.h> #include <PubSubClient.h> #include <string.h> #include <ArduinoJson.h> const char* ssid = "your-ssid"; const char* password = "your-password"; const String deviceId = "your-device-id"; const char* deviceSecretKey = "your-device-secret-key"; const char* mqttServer = "api.wia.io"; const int mqttPort = 1883; String topic = "devices/" + deviceId + "/events/"; WiFiClient espClient; PubSubClient client(espClient); StaticJsonBuffer<200> jsonBuffer; JsonObject& root = jsonBuffer.createObject(); void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.println("Connecting to WiFi.."); } Serial.println("Connected to the WiFi network"); client.setServer(mqttServer, mqttPort); while (!client.connected()) { Serial.println("Connecting to MQTT..."); if (client.connect("ESP8266Client", deviceSecretKey, " ")) { Serial.println("connected"); } else { Serial.print("failed with state "); Serial.print(client.state()); delay(2000); } } } void loop() { char topicChar[256]; topic.toCharArray(topicChar, topic.length()); root["name"] = "temperature"; root["data"] = 21.5; size_t len = root.measureLength(); size_t size = len+1; char message[256]; root.printTo(message, sizeof(message)); Serial.println(message); client.publish(topicChar, message); delay(10000); }
Replace the following values of the following variables:
your-password
- with your WiFi network password
your-device-id
with your Device id from the Wia Dashboard (the one that begins withdev_
)
your-device_secret_key
with your Device secret key from the Wia Dashboard (the one that begins withd_sk
)
Verify/Compile
the code. If it runs correctly then go toSketch > Upload
to send it to your ESP8266
Go to the Wia dashboard to view the data coming through to your device.
Now that you've learned how to publish an event via MQTT, learn how to control your board via MQTT too!