element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs Publish an Event with ESP8266 via Wia's MQTT API
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: anpivey
  • Date Created: 3 Aug 2018 10:52 AM Date Created
  • Views 776 views
  • Likes 4 likes
  • Comments 0 comments
  • tutorial
  • mqtt
  • esp8266
  • iot
  • arduino coding
  • api
  • c++
  • wia
Related
Recommended

Publish an Event with ESP8266 via Wia's MQTT API

anpivey
anpivey
3 Aug 2018


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.

image

  • 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

 

image

 

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

 

image

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 that Upload Speed is set to 115200 and the correct Port is selected.

 

image

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

 

image

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 with dev_)
  • your-device_secret_key with your Device secret key from the Wia Dashboard (the one that begins with d_sk)
  • Verify/Compile the code. If it runs correctly then go to Sketch > 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!

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube