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
- 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 that
Upload Speed
is set to115200
and the correct Port is selected.
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
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 labeled
PublishEventViaMQTT.ino
. You can also view it on GitHub here. This code publishes an event to your Device every 10 seconds
Replace the following values of the following variables:
your-ssid
- with your WiFi network name
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!
Comments